Truffle: Mismatch in default networks

Created on 18 Feb 2019  ·  16Comments  ·  Source: trufflesuite/truffle

Issue

Default networks mismatch in a way likely to confuse newcomers.

Steps to Reproduce

  1. Start Ganache.
  2. npm uninstall truffle -g
  3. npm install truffle -g (this and the previous ensures running the most current version, now 5.0.4)
  4. mkdir metacoin
  5. cd metacoin
  6. truffle unbox metacoin
    >√ Preparing to download
    >√ Downloading
    >√ Cleaning up temporary files
    >√ Setting up box
    >
    >Unbox successful. Sweet!
    >
    >Commands:
    >
    > Compile contracts: truffle compile
    > Migrate contracts: truffle migrate
    > Test contracts: truffle test
  7. truffle compile
    >Compiling .\contracts\ConvertLib.sol...
    >Compiling .\contracts\MetaCoin.sol...
    >Compiling .\contracts\Migrations.sol...
    >Compiling .\contracts\ConvertLib.sol...
    >Writing artifacts to .\build\contracts
  8. truffle migrate

    Compiling .\contracts\ConvertLib.sol...
    Writing artifacts to .\build\contracts

    ⚠️ Important ⚠️
    If you're using an HDWalletProvider, it must be Web3 1.0 enabled or your migration will hang.

    Starting migrations...

    Network name: 'ganache'
    Network id: 5777
    Block gas limit: 6721975

    1_initial_migration.js

    Deploying 'Migrations'


    transaction hash: 0x2f3f2a67c37f35907b4745dafb410c583ff0dfa185492e220a3e54ef57e7f8a3
    Blocks: 0 Seconds: 0
    contract address: 0x6C25486555d40B00cf18e33c2B93297Ce7D7b575
    account: 0xd4f6Fe9db007AeE5b7d15C46C1CaD1897e4A4948
    balance: 97.77270564
    gas used: 284908
    gas price: 20 gwei
    value sent: 0 ETH
    total cost: 0.00569816 ETH

    Saving artifacts


    Total cost: 0.00569816 ETH

    2_deploy_contracts.js

    Deploying 'ConvertLib'


    transaction hash: 0xd6dbdd6aa1752f97e56172e69e4f04a67fd36e98cf4602a5e3342860fb9f890a
    Blocks: 0 Seconds: 0
    contract address: 0x1Bd564E5ACd148390AB4f5aC778f286D903db472
    account: 0xd4f6Fe9db007AeE5b7d15C46C1CaD1897e4A4948
    balance: 97.77046982
    gas used: 111791
    gas price: 20 gwei
    value sent: 0 ETH
    total cost: 0.00223582 ETH

    Linking


    • Contract: MetaCoin <--> Library: ConvertLib (at address: 0x1Bd564E5ACd148390AB4f5aC778f286D903db472)

      Deploying 'MetaCoin'


    transaction hash: 0x1d6ee528901adc0bb5cd3c83673f132cc1a875d3c4a0e44fb98e5e3a65577fe4
    Blocks: 0 Seconds: 0
    contract address: 0xe27eeE1929a55EBAa9a5a71ee893838d05C1A4F3
    account: 0xd4f6Fe9db007AeE5b7d15C46C1CaD1897e4A4948
    balance: 97.76329838
    gas used: 358572
    gas price: 20 gwei
    value sent: 0 ETH
    total cost: 0.00717144 ETH

    Saving artifacts


    Total cost: 0.00940726 ETH

    Summary

    Total deployments: 3
    Final cost: 0.01510542 ETH

  9. truffle test

    Error: The network id specified in the truffle config (4447) does not match the one returned by the network (5777). Ensure that both the network and the provider are properly configured.
    at detectNetworkId (...\packages\truffle-core\lib\environment.js:71:1)
    at
    at process._tickCallback (internal/process/next_tick.js:160:7)
    Truffle v5.0.4 (core: 5.0.4)
    Node v9.3.0

Expected Behavior

The expected behavior is that the three listed and suggested commands, which "unbox" indicates are ready, should actually be runnable without error.

If truffle migrate uses Ganache automatically, without error, I would expect truffle test to do the same.
To get this to work as documented, you have to first manually uncomment the content of truffle-config.js.

If you skip step 1 ("Start Ganache"), step 8 (truffle migrate) has the following error:

Could not connect to your Ethereum client with the following parameters:
- host > 127.0.0.1
- port > 7545
- network_id > 5777
Please check that your Ethereum client:
- is running
- is accepting RPC connections (i.e., "--rpc" option is used in geth)
- is accessible over the network
- is properly configured in your Truffle configuration file (truffle-config.js)

Truffle v5.0.4 (core: 5.0.4)
Node v9.3.0

Actual Results

You cannot run the three suggested commands out of the box. If Ganache is started, you'll get an error at the truffle test stage because network configuration information is being pulled from different places, unintuitively. If Ganche is not started, you'll get an error at the truffle migrate stage.

The error message reports a mismatch of two network ID numbers, neither of which come from code in the user's project, a confusing and frustrating introduction to the technology. The error does not do a good job of helping the user figure out what the problem is and/or how to fix it (e.g. uncomment truffle-config.js default values).

Potential Solutions

A. One possible fix that might come to mind is to just uncomment the Ganache configuration details by default, instead of requiring the user do the manual workaround. This is easy. However, that is not allowed per requirement here, explained as being on the basis that "we want the boxes to work out of the box for testing." However, the boxes do not work "out of the box" for testing. Commenting out that default configuration makes it harder when somebody has a client running, and does not make it easier when they don't.

B. The migrate step appears to take default network information from truffle/packages/truffle-core/lib/environment.js lines 22-33. The test step could use the same logic and draw on the same defaults.

C. We could intentionally decide that we do not want example projects to work "out of the box" and remove the default from environment.js, replacing it with a more helpful error message with instructions to add or uncomment a network configuration in truffle-config.js, instead of a confusing error message about a network id mismatch.

D. Other options?

The issue is filed against this project because option A was discussed and ruled out in the box-specific project, and options B and C affect code in this project.

Environment

  • Operating System: Windows 10 pro
  • Ethereum client: Ganache
  • Truffle version (truffle version): v5.0.4
  • node version (node --version): v9.3.0
  • npm version (npm --version): v6.8.0
help wanted needs requirements priority5 🌀

Most helpful comment

Try following the exact steps in the original post above, except uncomment the contents of truffle-config.js between steps 6 and 7. Just before you start step 7, the config file should match this.

All 16 comments

Thanks for this thorough write-up, and for including potential solutions! Certainly makes our jobs easier. We will review this and come up with next steps from our perspective and discuss them here.

I still get the same error when I try to test my contracts. is there any solution. Please Help !!!

@lundrimazemi Uncomment the contents of truffle-config.js, as in the commit above.

/**

  • Use this file to configure your truffle project. It's seeded with some
  • common settings for different networks and features like migrations,
  • compilation and testing. Uncomment the ones you need or modify
  • them to suit your project as necessary.
    *
  • More information about configuration can be found at:
    *
  • truffleframework.com/docs/advanced/configuration
    *
  • To deploy via Infura you'll need a wallet provider (like truffle-hdwallet-provider)
  • to sign your transactions before they're sent to a remote public node. Infura API
  • keys are available for free at: infura.io/register
    *
  • You'll also need a mnemonic - the twelve word phrase the wallet uses to generate
  • public/private key pairs. If you're publishing your code to GitHub make sure you load this
  • phrase from a file you've .gitignored so it doesn't accidentally become public.
    *
    */

// const HDWalletProvider = require('truffle-hdwallet-provider');
// const infuraKey = "fj4jll3k.....";
//
// const fs = require('fs');
// const mnemonic = fs.readFileSync(".secret").toString().trim();
const path = require("path");
module.exports = {
contracts_build_directory: path.join(__dirname, "client/src/contracts"),

/**

  • Networks define how you connect to your ethereum client and let you set the
  • defaults web3 uses to send transactions. If you don't specify one truffle
  • will spin up a development blockchain for you on port 9545 when you
  • run develop or test. You can ask a truffle command to use a specific
  • network from the command line, e.g
    *
  • $ truffle test --network
    */

networks: {
// Useful for testing. The development name is special - truffle uses it by default
// if it's defined here and no other network is specified at the command line.
// You should run a client (like ganache-cli, geth or parity) in a separate terminal
// tab if you use this network and you must also set the host, port and network_id
// options below to some value.
//
ganache: {
host: "127.0.0.1", // Localhost (default: none)
port: 7545, // Standard Ethereum port (default: none)
network_id: "5777", // Any network (default: none)
},

// Another network with more advanced options...
// advanced: {
  // port: 8777,             // Custom port
  // network_id: 1342,       // Custom network
  // gas: 8500000,           // Gas sent with each transaction (default: ~6700000)
  // gasPrice: 20000000000,  // 20 gwei (in wei) (default: 100 gwei)
  // from: <address>,        // Account to send txs from (default: accounts[0])
  // websockets: true        // Enable EventEmitter interface for web3 (default: false)
// },

// Useful for deploying to a public network.
// NB: It's important to wrap the provider as a function.
// ropsten: {
  // provider: () => new HDWalletProvider(mnemonic, `https://ropsten.infura.io/${infuraKey}`),
  // network_id: 3,       // Ropsten's id
  // gas: 5500000,        // Ropsten has a lower block limit than mainnet
  // confirmations: 2,    // # of confs to wait between deployments. (default: 0)
  // timeoutBlocks: 200,  // # of blocks before a deployment times out  (minimum/default: 50)
  // skipDryRun: true     // Skip dry run before migrations? (default: false for public nets )
// },

// Useful for private networks
// private: {
  // provider: () => new HDWalletProvider(mnemonic, `https://network.io`),
  // network_id: 2111,   // This network is yours, in the cloud.
  // production: true    // Treats this network as if it was a public net. (default: false)
// }

},

// Set default mocha options here, use special reporters etc.
mocha: {
// timeout: 100000
},

// Configure your compilers
compilers: {
solc: {
// version: "0.5.1", // Fetch exact version from solc-bin (default: truffle's version)
// docker: true, // Use "0.5.1" you've installed locally with docker (default: false)
// settings: { // See the solidity docs for advice about optimization and evmVersion
// optimizer: {
// enabled: false,
// runs: 200
// },
// evmVersion: "byzantium"
// }
}
}
}

still it is not working, or i should uncomment something else

Try following the exact steps in the original post above, except uncomment the contents of truffle-config.js between steps 6 and 7. Just before you start step 7, the config file should match this.

Thank you sir @wbt

Hello I am a noob and hit this wall when I was trying truffle migrate. Thanks for OP and this page for solving this problem for me, and that has wasted quite a bit of time.

@gnidan How have those closed-door discussions gone? Can we implement one of the proposed solutions, as people are getting confused (probably including many who can't find this particular page and the fork with a solution)?

Does the truffle test command not work out of the box on the latest version of Truffle?
It seems like I'm able to unbox the metacoin project and run test. Let me know, maybe it got changed in one of the latest versions?

@eggplantzzz I'm not on the latest version at the moment due to blockers like #1942, which apparently will not be addressed anytime soon.

However, looking at the newbies coming in and experiencing the problem, it appears to still be a problem.

There are very detailed reproduction steps above which could be tried with different versions, starting with the reported one.

There is also no specific reason to believe this is fixed absent any pointer to a change in the code.
Metacoin-box/truffle-config.js still has the default networks commented out in contrast to my fork which is fixing it for people. In /packages/truffle-core/lib/environment.js the relevant lines have been moved from lines 22-33 down into a helper function setUpConfig at lines 138-149, but it's apparently otherwise still the same code.

So I suspect this issue with the code still exists, in addition to the issue with the unreported closed-door discussions leaving users in the dark.

This is a really bad "getting started" user experience, that I hit also. If Ethereum wants to hit Joe's 1 million developers goal, this type of UX won't do. It sends the massage to developers that the tools aren't ready yet.
Once you use Ganache once, then you cannot get the happy path truffle test command to work without adding a ganache config and using --network ganache switch.

Thanks @wbt .

I found a workaround on top of @lundrimazemi 's solution. After adding ganache to the network config run the command truffle test --network ganache to point to the correct network.

ganache: {
      host: "127.0.0.1", // Localhost (default: none)
      port: 7545, // Standard Ethereum port (default: none)
      network_id: "5777", // Any network (default: none)
},

I think this issue might not be a problem anymore; since truffle test with no --network now uses an ephemeral port in the 5-digit range (meaning there won't be any collision by default).

Can anyone confirm?

Thanks!

Closing this for issue maintenance. If anyone discovers that this problem still exists, please do let us know! Thanks all!

Was this page helpful?
0 / 5 - 0 ratings