trufflesuite / truffle-migrate

On-chain migrations management
17 stars 17 forks source link

Truffle migrate tries to read the contract code before it's available #15

Closed biern closed 6 years ago

biern commented 6 years ago

Most of the time migrations work just fine, however, today I was constantly getting hit with:

Running migration: 1_initial_migration.js
  Deploying Migrations...
  ... 0x(...)
Error encountered, bailing. Network state unknown. Review successful transactions manually.
Error: The contract code couldn't be stored, please check your gas amount.

That's when deploying just the builtin Migrations contract. I've tried to run it multiple times but it had been constantly failinig with this problem, at 20 - 12 Gwei gas price.

After some debugging with node inspect I think I found the code responsible for the problem:

                contract._eth.getTransactionReceipt(contract.transactionHash, function(e, receipt){
                    if(receipt && !callbackFired) {

// Here's the problem -
// Code is still not yet available at this point.
// `code=0x`
                        contract._eth.getCode(receipt.contractAddress, function(e, code){
(...)
                            } else {
                                if(callback)
                                    callback(new Error('The contract code couldn\'t be stored, please check your gas amount.'));
                                else
                                    throw new Error('The contract code couldn\'t be stored, please check your gas amount.');
                            }
                        });
                    }
                });

It looks like the contract address and tx receipt is available much sooner than the actual contract code. I've confirmed it with etherscan and truffle console and it seems to be the case. The contract code is eventually available, just at a later point. I'm not aware of how the migration flow should look like but it seems there might be a problem.

Using truffle 3.4.9 and node 8.5.0

TripleSpeeder commented 6 years ago

I am seeing the same behaviour on mainnet. Here is the result from truffle migrate --network live --verbose-rpc:

Using network 'live'.

Running migration: 1_initial_migration.js
   > {
   >   "jsonrpc": "2.0",
   >   "id": 1,
   >   "method": "eth_accounts",
   >   "params": []
   > }
 <   {
 <     "jsonrpc": "2.0",
 <     "result": [
 <       "0x4e63a18b72bddca9cf9e5d906e285b8b60e9c876",
 <       "0xc81c8dfbb52b145eac415b431b36b918cdd0741f",
 <       "0xff4ef5e6211cca4678484185c3ab4f637d5f0882"
 <     ],
 <     "id": 1
 <   }
   > {
   >   "jsonrpc": "2.0",
   >   "id": 2,
   >   "method": "net_version",
   >   "params": []
   > }
 <   {
 <     "jsonrpc": "2.0",
 <     "result": "1",
 <     "id": 2
 <   }
  Deploying Migrations...
   > {
   >   "jsonrpc": "2.0",
   >   "id": 3,
   >   "method": "net_version",
   >   "params": []
   > }
 <   {
 <     "jsonrpc": "2.0",
 <     "result": "1",
 <     "id": 3
 <   }
   > {
   >   "jsonrpc": "2.0",
   >   "id": 4,
   >   "method": "eth_sendTransaction",
   >   "params": [
   >     {
   >       "from": "0xc81c8dfbb52b145eac415b431b36b918cdd0741f",
   >       "gas": "0x47e7c4",
   >       "gasPrice": "0x174876e800",
   >       "data": "0x6060604052341561000f57600080fd5b5b60008054600160a060020a03191633600160a060020a03161790555b5b6101b38061003c6000396000f300606060405263ffffffff60e060020a6000350416630900f0108114610045578063445df0ac146100665780638da5cb5b1461008b578063fdacd576146100ba575b600080fd5b341561005057600080fd5b610064600160a060020a03600435166100d2565b005b341561007157600080fd5b610079610150565b60405190815260200160405180910390f35b341561009657600080fd5b61009e610156565b604051600160a060020a03909116815260200160405180910390f35b34156100c557600080fd5b610064600435610165565b005b6000805433600160a060020a039081169116141561014a5781905080600160a060020a031663fdacd57660015460405160e060020a63ffffffff84160281526004810191909152602401600060405180830381600087803b151561013557600080fd5b6102c65a03f1151561014657600080fd5b5050505b5b5b5050565b60015481565b600054600160a060020a031681565b60005433600160a060020a03908116911614156101825760018190555b5b5b505600a165627a7a7230582018837a6c024b1ac60ac2ebf9d811c88c5794885073c67cd9295c95bd03c6c70c0029"
   >     }
   >   ]
   > }
 <   {
 <     "jsonrpc": "2.0",
 <     "result": "0xb041764657568152114dfe5116826256611a09e761d959b92182357850a34f81",
 <     "id": 4
 <   }
  ... 0xb041764657568152114dfe5116826256611a09e761d959b92182357850a34f81
   > {
   >   "jsonrpc": "2.0",
   >   "id": 5,
   >   "method": "eth_newBlockFilter",
   >   "params": []
   > }
 <   {
 <     "jsonrpc": "2.0",
 <     "result": "0x2",
 <     "id": 5
 <   }
   > [
   >   {
   >     "jsonrpc": "2.0",
   >     "id": 6,
   >     "method": "eth_getFilterChanges",
   >     "params": [
   >       "0x2"
   >     ]
   >   }
   > ]
 <   [
 <     {
 <       "jsonrpc": "2.0",
 <       "result": [
 <         "0xf2cf8629f0b309fc3281e36e0b5b2ecf07bbba9087af1e0927e40c2acedbc359"
 <       ],
 <       "id": 6
 <     }
 <   ]
   > {
   >   "jsonrpc": "2.0",
   >   "id": 7,
   >   "method": "eth_getTransactionReceipt",
   >   "params": [
   >     "0xb041764657568152114dfe5116826256611a09e761d959b92182357850a34f81"
   >   ]
   > }
 <   {
 <     "jsonrpc": "2.0",
 <     "result": {
 <       "blockHash": null,
 <       "blockNumber": null,
 <       "contractAddress": "0x4a7b544f28c58a02295fdc0c05085e84a4c6b13b",
 <       "cumulativeGasUsed": "0x2ecf0",
 <       "gasUsed": "0x2ecf0",
 <       "logs": [],
 <       "logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
 <       "root": null,
 <       "status": 1,
 <       "transactionHash": "0xb041764657568152114dfe5116826256611a09e761d959b92182357850a34f81",
 <       "transactionIndex": "0x0"
 <     },
 <     "id": 7
 <   }
   > {
   >   "jsonrpc": "2.0",
   >   "id": 8,
   >   "method": "eth_getCode",
   >   "params": [
   >     "0x4a7b544f28c58a02295fdc0c05085e84a4c6b13b",
   >     "latest"
   >   ]
   > }
 <   {
 <     "jsonrpc": "2.0",
 <     "result": "0x",
 <     "id": 8
 <   }
   > {
   >   "jsonrpc": "2.0",
   >   "id": 9,
   >   "method": "eth_uninstallFilter",
   >   "params": [
   >     "0x2"
   >   ]
   > }
Error encountered, bailing. Network state unknown. Review successful transactions manually.
Error: The contract code couldn't be stored, please check your gas amount.

So the transaction receipt clearly says "contractAddress": "0x4a7b544f28c58a02295fdc0c05085e84a4c6b13b",, but the following rpc call eth_getCode returns "0x".

This might not be a problem of truffle, but maybe a problem of parity? This is happening with parity 1.8.2 (Parity//v1.8.2-beta-1b6588cb2-20171025/x86_64-linux-gnu/rustc1.21.0).

TripleSpeeder commented 6 years ago

Looking further this seems to be a long-standing open issue with parity and/or web3 having different opinions: https://github.com/paritytech/parity/issues/5538

I successfully run a migration when starting parity in geth compatibility mode (parameter --geth).

biern commented 6 years ago

Thanks for your insights @TripleSpeeder . I was indeed using parity without the --geth parameter, versions 1.6.9 - 1.7.5. I will try that on next occasion.

dylanseago commented 6 years ago

@TripleSpeeder using the --geth parameter worked for me too, thank you!

nguyenhoaibao commented 6 years ago

I'm using geth and I'm also facing with this problem. Could you let me know how can I fix this?

Geth version: 1.7.3-stable Truffle version: 4.0.5

cgewecke commented 6 years ago

@biern Thanks for reporting this and getting a thread going about how to resolve.

Closing for house-keeping, this is a Parity / Web3 compatibility issue that be fixed by setting a flag on the client.