blockchain - truffle - 进行local network的 debug

访问量: 776

refer to: https://trufflesuite.com/guides/debugging-an-example-smart-contract/

1. 创建一个truffle项目

2. 创建一个简单的contract

3. 创建对应的migration, deploy script

都准备好了。我就不细写了。

4. 在本地开启 local network

$ truffle develop    

可以看到本地跑起了一个节点, 端口位 9545

Truffle Develop started at http://127.0.0.1:9545/

Accounts:
(0) 0xf4957298188e7480e400d258cd8a543236157216
(1) 0xd28834118873106d52e6785890f9ef9ed5ecc626
(2) 0x1ad4c765c2e36c4dcba858ec66ed3116003c00cb
(3) 0x3e9be6290cd7bb518b54ce38237b52fac5e580fa
(4) 0x9faaf1a8d2c9a5132c74f98bc42e78b03c63c9cd
(5) 0x0c4624e52e3950d3db8c2cb1b27ea228ebdc7ba4
(6) 0xdaa7d4b92498f8d7f39422bf3ebc3ced0ad1ce64
(7) 0xa6ea85cfae2cdedd21d0204943632f720d70b484
(8) 0x972d745c4138ad9969d4b555f543605f6706ec2b
(9) 0xdd28a0dd0cf85bb2dc9d23250c7b144fec3e7e42

Private Keys:
(0) feb022ab9f0aa3beea108b13f8e853a8c374dbe99184cfc07d6bcc6165926426
(1) 5c16906b9f42047578d0faaf67b7ad734b11aea79643c820673c0c1c02f57433
(2) 193ad489ee665662826ab8adaa341f328999eb88500e5516d712c737925c29d0
(3) f78caa156ad8c8641fcddc46c0b97060e754c987cad5b35fbf2f782457a8c1ea
(4) a7d301c685d3248f577db8fc373d5034a1d1f9de140c04a4ad40dd4e07f757d7
(5) 9fbde5fdb4dd4b82c154cb00587e8d3a1cdcfab504497b9e4a2188c9d45422af
(6) 5c8df18d4ea20bb6e6193ac4f01920cd47a2d66ec191e98800441da227fbbe55
(7) 7e1df6b6c3a8c5d7f98d5173e38bcf2d687bba144de8172bf078167b1f34b812
(8) efe91702b6ccfebbe47130a8aee6e58fc28285a43bf99e330ae15bff0bd97b99
(9) 065679e0310cc2221060710cb806aba0fbd2371490e251d142962006d877a8a1

Mnemonic: crisp arch lobster oblige style solve extend thrive round mystery ??? ??? 

⚠️  Important ⚠️  : This mnemonic was created for you by Truffle. It is not secure.
Ensure you do not use it on production blockchains, or else you risk losing funds.

5. 在本地进行部署

(这一步要求你的build目录中已经存在了json ,如果遇到问题的话记得先运行 truffle compile )

truffle> migrate  

truffle(develop)> migrate

Compiling your contracts...
===========================
> Everything is up to date, there is nothing to compile.


Starting migrations...
======================
> Network name:    'develop'
> Network id:      5777
> Block gas limit: 6721975 (0x6691b7)
.....

4_deploy_MyTestNftC.js
======================

   Deploying 'MyTestNftC'
   ----------------------
   > transaction hash:    0xfa1df4bf2920fc063741a013264725ab181a7a6110739966f09dfceaec0cecf7
   > Blocks: 0            Seconds: 0
   > contract address:    0x3024daC2914A4497a3362Fc926DaeF55cCdd5Cee
   > block number:        7
   > block timestamp:     1655708906
   > account:             0xf4957298188E7480E400D258cD8a543236157216
   > balance:             99.973898036265837958
   > gas used:            2730825 (0x29ab49)
   > gas price:           2.994489856 gwei
   > value sent:          0 ETH
   > total cost:          0.0081774277610112 ETH

   > Saving migration to chain.
   > Saving artifacts
   -------------------------------------
   > Total cost:     0.0081774277610112 ETH

Summary
=======
> Total deployments:   4
> Final cost:          0.025772278684171387 ETH

6. 随便调用一下 contract的方法

truffle(develop)> MyTestNftC.deployed().then(function(instance){ return instance.minter.call()})
'0xf4957298188E7480E400D258cD8a543236157216'
// 可以看到,这个地址就是 local network的第一个account address

7. 进行debug: truffle develop

这样就会在本地开启一个local node (注意不是ganache ,而是类似于hardhat的一个节点,该节点是 truffle内置的 )

truffle develop
Truffle Develop started at http://127.0.0.1:9545/

Accounts:
(0) 0xf4957298188e7480e400d258cd8a543236157216
(1) 0xd28834118873106d52e6785890f9ef9ed5ecc626
(2) 0x1ad4c765c2e36c4dcba858ec66ed3116003c00cb
(3) 0x3e9be6290cd7bb518b54ce38237b52fac5e580fa
(4) 0x9faaf1a8d2c9a5132c74f98bc42e78b03c63c9cd
(5) 0x0c4624e52e3950d3db8c2cb1b27ea228ebdc7ba4
(6) 0xdaa7d4b92498f8d7f39422bf3ebc3ced0ad1ce64
(7) 0xa6ea85cfae2cdedd21d0204943632f720d70b484
(8) 0x972d745c4138ad9969d4b555f543605f6706ec2b
(9) 0xdd28a0dd0cf85bb2dc9d23250c7b144fec3e7e42

Private Keys:
(0) feb022ab9f0aa3beea108b13f8e853a8c374dbe99184cfc07d6bcc61659264??
(1) 5c16906b9f42047578d0faaf67b7ad734b11aea79643c820673c0c1c02f574??
(2) 193ad489ee665662826ab8adaa341f328999eb88500e5516d712c737925c29??
(3) f78caa156ad8c8641fcddc46c0b97060e754c987cad5b35fbf2f782457a8c1??
(4) a7d301c685d3248f577db8fc373d5034a1d1f9de140c04a4ad40dd4e07f757??
(5) 9fbde5fdb4dd4b82c154cb00587e8d3a1cdcfab504497b9e4a2188c9d45422??
(6) 5c8df18d4ea20bb6e6193ac4f01920cd47a2d66ec191e98800441da227fbbe??
(7) 7e1df6b6c3a8c5d7f98d5173e38bcf2d687bba144de8172bf078167b1f34b8??
(8) efe91702b6ccfebbe47130a8aee6e58fc28285a43bf99e330ae15bff0bd97b??
(9) 065679e0310cc2221060710cb806aba0fbd2371490e251d142962006d877a8??

Mnemonic: crisp arch lobster oblige style solve extend thrive round mystery skate ???

输入contract的名字,就可以看到一系列的输出(很长)

truffle(develop)> MyTestNftC
[Function: TruffleContract] {
  _constructorMethods: {
    configureNetwork: [Function: configureNetwork],
    setProvider: [Function: setProvider],
    new: [Function: new],
   .... 省略200行

8 .输入 migrate, 就可以部署所有的contract到该节点

truffle(develop)> migrate

Compiling your contracts...
===========================
> Everything is up to date, there is nothing to compile.


Starting migrations...
======================
> Network name:    'develop'
> Network id:      5777
> Block gas limit: 6721975 (0x6691b7)


1_initial_migration.js
======================

   Deploying 'Migrations'
   ----------------------
   > transaction hash:    0x4373f0a645d018015b2934832f02585182b8a13779867bf7ef3ad271e3f8541d
   > Blocks: 0            Seconds: 0
   > contract address:    0x5Cf388FDB08100C7d106Cce3E225F76a759d7239
   > block number:        1

...  省略很多行

   > Saving migration to chain.
   > Saving artifacts
   -------------------------------------
   > Total cost:     0.0081774277610112 ETH

Summary
=======
> Total deployments:   4
> Final cost:          0.025772278684171387 ETH

9. 调用一些方法 试试:

// 查看该NFT的minter

truffle(develop)> MyTestNftC.deployed().then(function(instance){ return instance.minter.call()})

'0xf4957298188E7480E400D258cD8a543236157216'

// mint一个新的出来

truffle(develop)> MyTestNftC.deployed().then(function(instance){ return instance.mint('0xf4957298188E7480E400D258cD8a543236157216')})
{
  tx: '0xf97eef4b25999569ab0c72534e79d54a72c764e67a6ebe69d3a6eef17986c8ba',
  receipt: {
    transactionHash: '0xf97eef4b25999569ab0c72534e79d54a72c764e67a6ebe69d3a6eef17986c8ba',
    transactionIndex: 0,
    blockNumber: 9,
    blockHash: '0x6e434ebf6022217d4256bd7bc74eeac9c9f3401cd2834e410f71d273678d3e9c',
    from: '0xf4957298188e7480e400d258cd8a543236157216',
    to: '0x3024dac2914a4497a3362fc926daef55ccdd5cee',
    cumulativeGasUsed: 96444,
    gasUsed: 96444,
    contractAddress: null,
    logs: [ [Object], [Object] ],
    logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000004000000000040000000000000000000000000008000000000000000000040000000000000000000000000000020000000000000000000800000000000002000000000010000000000000000000020000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000001000000000000000000000000002000000000000000000000000000000000004000000000000000060000002000000000000000000020000000000000000000000000000000000000000',
    status: true,
    effectiveGasPrice: '0xae3a49ce',
    type: '0x2',
    rawLogs: [ [Object], [Object] ]
  },
  logs: [
    {
      address: '0x3024daC2914A4497a3362Fc926DaeF55cCdd5Cee',
      blockHash: '0x6e434ebf6022217d4256bd7bc74eeac9c9f3401cd2834e410f71d273678d3e9c',
      blockNumber: 9,
      logIndex: 0,
      removed: false,
      transactionHash: '0xf97eef4b25999569ab0c72534e79d54a72c764e67a6ebe69d3a6eef17986c8ba',
      transactionIndex: 0,
      id: 'log_0baa05a9',
      event: 'Transfer',
      args: [Result]
    },
    {
      address: '0x3024daC2914A4497a3362Fc926DaeF55cCdd5Cee',
      blockHash: '0x6e434ebf6022217d4256bd7bc74eeac9c9f3401cd2834e410f71d273678d3e9c',
      blockNumber: 9,
      logIndex: 1,
      removed: false,
      transactionHash: '0xf97eef4b25999569ab0c72534e79d54a72c764e67a6ebe69d3a6eef17986c8ba',
      transactionIndex: 0,
      id: 'log_5e90e085',
      event: 'LogMint',
      args: [Result]
    }
  ]
}

调用 balanceOf 方法;

可以看出, balanceOf 返回的是一个对象

truffle(develop)> MyTestNftC.deployed().then(function(instance){ return instance.balanceOf('0xf4957298188E7480E400D258cD8a543236157216') })
BN { negative: 0, words: [ 6, <1 empty item> ], length: 1, red: null }

可以进一步打印出该结果

truffle(develop)> 
MyTestNftC
    .deployed()
    .then(function(instance){ return instance.balanceOf("0xf4957298188E7480E400D258cD8a543236157216") })
    .then(function(value) { return value.toNumber()})

6   (这个 就是结果了)

9. 进行Debug (必须要拿到tx才行)

输入 debug <tx id> 就可以了。
n 是下一步
q 是退出
p 是打印变量
其实我很不适应,入行17年没做过debug, 感觉效率太低,我都是打log.
debug 0x36cf56afbc6024751058b64fb1e160f496aef699a6ca04dd037db23e02fb9fea

Starting Truffle Debugger...

Addresses affected:
 0x3024daC2914A4497a3362Fc926DaeF55cCdd5Cee - MyTestNftC

Commands:
(enter) last command entered (step next)
(o) step over, (i) step line / step into, (u) step out, (n) step next
(c) continue until breakpoint, (Y) reset & continue to previous error
(y) (if at end) reset & continue to final error
(;) step instruction (include number to step multiple)
(g) turn on generated sources, (G) turn off generated sources except via `;`
(p) print instruction & state (`p [mem|cal|sto]*`; see docs for more)
(l) print additional source context (`l [+] [-]`)
(s) print stacktrace, (h) print this help
(q) quit, (r) reset, (t) load new transaction, (T) unload transaction
(b) add breakpoint (`b [[:]]`; see docs for more)
(B) remove breakpoint (similar to adding, or `B all` to remove all)
(+) add watch expression (`+:`), (-) remove watch expression (-:)
(?) list existing watch expressions and breakpoints
(v) print variables and values (`v [bui|glo|con|loc]*`)
(:) evaluate expression - see `v`


MyTestNftC.sol:

7:
8:
9: contract MyTestNftC is ERC721URIStorage {
   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

debug(develop:0x36cf56af...)> n

MyTestNftC.sol:

27:
28:     // function mint(address to) external onlyMinter {
29:     function mint(address to) external {
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

debug(develop:0x36cf56af...)> n

MyTestNftC.sol:

29:     function mint(address to) external {
30:
31:         uint256 tokenId = _tokensCount + 1;
                                             ^

debug(develop:0x36cf56af...)> p

Stack:
  000000000000000000000000000000000000000000000000000000006a627842 (3 from top)
  000000000000000000000000000000000000000000000000000000000000025b (2 from top)
  000000000000000000000000f4957298188e7480e400d258cd8a543236157216 (1 from top)
  0000000000000000000000000000000000000000000000000000000000000000 (top)

Instructions:
...
JUMP
JUMPDEST
PUSH1 0x00
-> PUSH1 0x01 (PC=2235, 0x08bb)
PUSH1 0x07
SLOAD
PUSH2 0x08c9
...

Step 141/704
55893 gas remaining

MyTestNftC.sol:

29:     function mint(address to) external {
30:
31:         uint256 tokenId = _tokensCount + 1;
                                             ^

debug(develop:0x36cf56af...)> n

MyTestNftC.sol:

29:     function mint(address to) external {
30:
31:         uint256 tokenId = _tokensCount + 1;
                              ^^^^^^^^^^^^

debug(develop:0x36cf56af...)> n

MyTestNftC.sol:

29:     function mint(address to) external {
30:
31:         uint256 tokenId = _tokensCount + 1;
                              ^^^^^^^^^^^^^^^^

debug(develop:0x36cf56af...)> n

MyTestNftC.sol:

29:     function mint(address to) external {
30:
31:         uint256 tokenId = _tokensCount + 1;
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

debug(develop:0x36cf56af...)> n

MyTestNftC.sol:

订阅/RSS Feed

Subscribe