以太坊 Truffle Debugger

目标

体验新版 truffle debugger,搭建开发环境,新建truffle项目,写智能合约,然后使用 debugger 来调试。

开发环境

  • Truffle v4.1.14
  • Ganache 1.2.1

创建项目

mkdir fibonacci
cd fibonacci

truffle init

启动 Ganache,修改 truffle 配置文件:

// truffle.js
module.exports = {
  networks: {
    development: {
      host: "127.0.0.1",
      port: 7545,
      network_id: "*"
    }
  }
};

合约代码

contracts/Fibonacci.sol

// contracts/Fibonacci.sol

pragma solidity ^0.4.22;

contract Fibonacci {

uint[] fibseries;

  // n = how many in the series to return
  function generateFib(uint n) public {

    // set 1st and 2nd entries
    fibseries.push(1);
    fibseries.push(1);

    // generate subsequent entries
    for (uint i=2; i < n ; i++) {
      fibseries.push(fibseries[i-1] + fibseries[i-2]);
    }

  }

}

迁移脚本

migrations/2_deploy_contracts.js

var Fibonacci = artifacts.require("Fibonacci");

module.exports = function(deployer) {
  deployer.deploy(Fibonacci);
};

发布

进入 truffle console,执行命令:

truffle console

进入后会看到提示:

truffle(development)>

编译:

truffle(development)> compile
Compiling ./contracts/Fibonacci.sol...
Compiling ./contracts/Migrations.sol...
Writing artifacts to ./build/contracts

迁移:

truffle(development)> migrate
Using network 'development'.

Running migration: 1_initial_migration.js
  Deploying Migrations...
  ... 0xc1fea5d1fa37c847455fad73de6e5c8b0335fb9bcf002fc7f4f15c6cad086964
  Migrations: 0x590ac68a92c37ab3783137f3aa137c9ab55b7430
Saving successful migration to network...
  ... 0xfb876be80567f541f39298674cdb1d11125b4854cdcc4f395c1e86d7ee3d60fc
Saving artifacts...
Running migration: 2_deploy_contracts.js
  Deploying Fibonacci...
  ... 0x90f7fd08baddec67a5e24b223fb0bf4f53327b60e666cdb68fa9a43e8a77b817
  Fibonacci: 0xdf32b5bac77c2998587e81f787667b869fd06190
Saving successful migration to network...
  ... 0xe9a042998d81eb0e2e84d00d2df51966e0fb3072467de7931f4a3dcb42d1935f
Saving artifacts...

Ganache 的 Transactions 界面中就可以看到新建的合约事务:

调试

合约调用:

truffle(development)> Fibonacci.deployed().then(function(instance){return instance.generateFib(10);});
{ tx: '0xfe40ddce72ad25f4ca14ba2675b6340c96f662c3b2cf88b3e780287213ef35fa',
  receipt:
   { transactionHash: '0xfe40ddce72ad25f4ca14ba2675b6340c96f662c3b2cf88b3e780287213ef35fa',
     transactionIndex: 0,
     blockHash: '0xebb585005947ec7fa188c9b3783eebebc7086129efbf21ae297ad257f0187f7f',
     blockNumber: 5,
     gasUsed: 298373,
     cumulativeGasUsed: 298373,
     contractAddress: null,
     logs: [],
     status: '0x1',
     logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000' },
  logs: [] }

返回了事务信息,使用 debug 需要事务ID,就是返回信息中的 tx 的值,进入 debug 模式:

debug 0xfe40ddce72ad25f4ca14ba2675b6340c96f662c3b2cf88b3e780287213ef35fa

效果:

truffle(development)> debug 0xfe40ddce72ad25f4ca14ba2675b6340c96f662c3b2cf88b3e780287213ef35fa
Compiling ./contracts/Fibonacci.sol...
Compiling ./contracts/Migrations.sol...

Gathering transaction data...

Addresses affected:
 0xdf32b5bac77c2998587e81f787667b869fd06190 - Fibonacci

Commands:
(enter) last command entered (step next)
(o) step over, (i) step into, (u) step out, (n) step next
(;) step instruction, (p) print instruction, (h) print this help, (q) quit
(b) toggle breakpoint, (c) continue until breakpoint
(+) add watch expression (`+:`), (-) remove watch expression (-:)
(?) list existing watch expressions
(v) print variables and values, (:) evaluate expression - see `v`


Fibonacci.sol:

1: pragma solidity ^0.4.22;
2:
3: contract Fibonacci {
   ^^^^^^^^^^^^^^^^^^^^

给出了debug帮助说明,下面体验几个常用的命令:

(1)下一步 step next

回车 enter” 就是下一步

(2)查看变量

输入 v 会列出当前所有变量及其值:

debug(development:0xfe40ddce...)> v

  fibseries: []

多按几次回车,让程序多走几步,再查看变量:

debug(development:0xfe40ddce...)> v

          i: 0
          n: 10
  fibseries: []

如果变量太多,我们只关注某个变量,使用 +:,例如:

debug(development:0xfe40ddce...)> +:i
0

debug(development:0xfe40ddce...)> +:n
10

debug(development:0xfe40ddce...)> +:fibseries
[]

debug(development:0xfe40ddce...)>

Fibonacci.sol:

10:     // set 1st and 2nd entries
11:     fibseries.push(1);
12:     fibseries.push(1);
        ^^^^^^^^^

:i
  0

:n
  10

:fibseries
  [ 1 ]

还可以设置变量组合:

debug(development:0xfe40ddce...)> +:{ i, fibseries }
{ i: 0, fibseries: [ 1 ] }

debug(development:0xfe40ddce...)>

Fibonacci.sol:

10:     // set 1st and 2nd entries
11:     fibseries.push(1);
12:     fibseries.push(1);
                       ^

:i
  0

:n
  10

:fibseries
  [ 1 ]

:{ i, fibseries }
  { i: 0, fibseries: [ 1 ] }

可以移除某个变量:

debug(development:0xfe40ddce...)> -:i
debug(development:0xfe40ddce...)> -:n
debug(development:0xfe40ddce...)> -:fibseries

结果变为:

debug(development:0xfe40ddce...)>

Fibonacci.sol:

13:
14:     // generate subsequent entries
15:     for (uint i=2; i < n ; i++) {
             ^^^^^^^^

:{ i, fibseries }
  { i: 0, fibseries: [ 1, 1 ] }

你可能感兴趣的:(以太坊 Truffle Debugger)