[Ethereum] How to debug transaction from truffle test on development network

contract-debuggingsoliditytruffletruffle-test

I am trying to debug a failing solidity function, which I am calling from the JS test files with truffle.

When I run my test, I am successfully able to get a transaction id. However, my solidity function does not appear to be performing the desired behavior so I would like to debug it.

I am aware that I can run

truffle develop
> debug transactionId 

At which point, it returns and error saying that it cannot find the transaction id.

My understanding is that running truffle develop only connects me to the local testnet running on port 9545, but my test code is communicating with a different network, which is why no transaction with that id is found.

I've attempted to run truffle develop on a different port than 9545, however according to the documentation:

This will spawn a development blockchain locally on port 9545, regardless of what your truffle-config.js configuration file calls for.

So I think there are two potential solutions:

  1. I can run my JS test code on the develop network running on port 9545
  2. Or I attach the debugger behind the truffle develop command to the "test network"

One challenge with solution two is that it is unclear to me what port the test network is running on, and it also doesn't seem to exist beyond the duration that the test are actually running.

I've also tried running the following:

truffle develop
> test
> debug txId

Which then prints out a new transaction id, but the transaction is still not found.

I've also tried:

truffle test --network development

Which also does not work.

Any help would be greatly appreciated.


Some additional information since posting:

When I try to run truffle debug txId while running the ganache GUI, the gui spits out the following error:

RangeError: Invalid string length
    at JSON.stringify (<anonymous>)
    at /node_modules/ganache-core/lib/httpServer.js:103:57
    at Object.intermediary [as callback] (/node_modules/ganache-core/lib/provider.js:114:5)
    at self.engine.sendAsync (/node_modules/ganache-core/lib/provider.js:169:12)
    at /node_modules/ganache-core/node_modules/web3-provider-engine/index.js:152:9
    at /node_modules/ganache-core/node_modules/async/internal/once.js:12:16
    at replenish (/node_modules/ganache-core/node_modules/async/internal/eachOfLimit.js:61:25)
    at /node_modules/ganache-core/node_modules/async/internal/eachOfLimit.js:71:9
    at eachLimit (/node_modules/ganache-core/node_modules/async/eachLimit.js:43:36)
    at /node_modules/ganache-core/node_modules/async/internal/doLimit.js:9:16
    at Object.end [as callback] (/node_modules/ganache-core/node_modules/web3-provider-engine/index.js:127:5)
    at intermediary (/node_modules/ganache-core/lib/statemanager.js:401:12)
    at /node_modules/ganache-core/lib/blockchain_double.js:900:11
    at /node_modules/ganache-core/lib/blockchain_double.js:649:9
    at commmitIfNeeded (/node_modules/ganache-core/lib/blockchain_double.js:640:11)
    at /node_modules/ganache-core/lib/blockchain_double.js:644:7
    at /node_modules/ganache-core/node_modules/async/internal/once.js:12:16
    at replenish (/node_modules/ganache-core/node_modules/async/internal/eachOfLimit.js:61:25)
    at /node_modules/ganache-core/node_modules/async/internal/eachOfLimit.js:71:9
    at eachLimit (/node_modules/ganache-core/node_modules/async/eachLimit.js:43:36)
    at /node_modules/ganache-core/node_modules/async/internal/doLimit.js:9:16
    at VM.AsyncEventEmitter.emit (/node_modules/ganache-core/node_modules/async-eventemitter/lib/AsyncEventEmitter.js:42:3)

Perhaps this is a bug with ganache?

Best Answer

If you defined a development network in your truffle-config, Ganache will use it for tests:

truffle-config.js:

module.exports = {
  networks: {
    development: {
      host: "localhost",
      port: 8545,
      network_id: "*" // Match any network id
    }
  }
};

See here for how to write your configuration: https://truffleframework.com/docs/truffle/reference/configuration

And here for relevant Truffle source code: https://github.com/trufflesuite/truffle/blob/develop/packages/truffle-core/lib/commands/test.js#L58

Otherwise, if you haven't defined a development config Ganache will use port 7545:

https://github.com/trufflesuite/truffle/blob/develop/packages/truffle-core/lib/commands/test.js#L150

I suggest to define a development network config, and use that network name when starting your truffle console (that's different from truffle develop command):

truffle console --network development

See: https://truffleframework.com/docs/truffle/reference/truffle-commands#console

Then you would be connected to the correct Ganache instance for debugging.

Related Topic