Skip to content
This repository has been archived by the owner on Jan 21, 2022. It is now read-only.

estimateGas bug - minimal example #96

Closed
kumavis opened this issue May 30, 2016 · 12 comments
Closed

estimateGas bug - minimal example #96

kumavis opened this issue May 30, 2016 · 12 comments
Assignees

Comments

@kumavis
Copy link
Contributor

kumavis commented May 30, 2016

0. initialize

I reccomend npm i -g jsonogram for pretty printing json

testrpc -m "pencil blouse boat super visual window huge image flip usual debris hospital"

1. publish contract

curl -X POST -d '{"method": "eth_sendTransaction", "params": [{"from": "0x435d00bd45e771cc663da08d8f3b9173414cd24c","data": "6060604052600160a060020a03321660009081526020819052604090206127109055609f80602d6000396000f3606060405260e060020a600035046390b98a1181146024578063f8b2cb4f146050575b005b606c60043560243533600160a060020a0316600090815260208190526040812054829010156076576099565b600160a060020a03600435166000908152602081905260409020545b6060908152602090f35b604080822080548490039055600160a060020a0384168252902080548201905560015b9291505056"}]}' localhost:8545 | jsonogram

2. estimate tx gas

curl -X POST -d '{"method": "eth_estimateGas", "params": [{"from": "0x435d00bd45e771cc663da08d8f3b9173414cd24c","to":"0xc01508458a7bb72d63f996561115069a2c328202","data":"0x90b98a11000000000000000000000000b0a34f864f14506216e7178df35e031161cc3c9e0000000000000000000000000000000000000000000000000000000000000001"}]}' localhost:8545 | jsonogram

-> "0x82b5" 33461

3. send tx and check gas used

curl -X POST -d '{"method": "eth_sendTransaction", "params": [{"from": "0x435d00bd45e771cc663da08d8f3b9173414cd24c","to":"0xc01508458a7bb72d63f996561115069a2c328202","data":"0x90b98a11000000000000000000000000b0a34f864f14506216e7178df35e031161cc3c9e0000000000000000000000000000000000000000000000000000000000000001"}]}' localhost:8545 | jsonogram
curl -X POST -d '{"method": "eth_getTransactionByHash", "params": ["0x850bc690fcca104b4f0f3b9523ba65e9d920a9c98d7587efd4e0fba62cd5043d"]}' http://localhost:8545/ | jsonogram

-> "0x47e7c4" 4712388

4. compare gas

"0x82b5" 33461 !== "0x47e7c4" 4712388

ZOMG

@kumavis
Copy link
Contributor Author

kumavis commented May 31, 2016

explicitly setting a high gas or gasLimit parameter for eth_estimateGas does not change the estimate

curl -X POST -d '{"method": "eth_estimateGas", "params": [{"gas":"0x47e7c4", "from": "0x435d00bd45e771cc663da08d8f3b9173414cd24c","to":"0xc01508458a7bb72d63f996561115069a2c328202","data":"0x90b98a11000000000000000000000000b0a34f864f14506216e7178df35e031161cc3c9e0000000000000000000000000000000000000000000000000000000000000001"}]}' localhost:8545 | jsonogram

@kumavis
Copy link
Contributor Author

kumavis commented May 31, 2016

estimateGas vm trace initially looks healthy
https://gist.github.com/kumavis/b5ce65afe69faf7ba7ea7fe69be53520

@kumavis
Copy link
Contributor Author

kumavis commented May 31, 2016

so to make things weirder: publishing the same contract to morden + estimating gas via geth offers a different number:

publish:

{"from": "0x5fda30bb72b8dfe20e48a00dfc108d0915be9bb0","data": "6060604052600160a060020a03321660009081526020819052604090206127109055609f80602d6000396000f3606060405260e060020a600035046390b98a1181146024578063f8b2cb4f146050575b005b606c60043560243533600160a060020a0316600090815260208190526040812054829010156076576099565b600160a060020a03600435166000908152602081905260409020545b6060908152602090f35b604080822080548490039055600160a060020a0384168252902080548201905560015b9291505056"}

produces: http://testnet.etherscan.io/address/0x272c19a069cdfea31e913524501494c26830a3f1

estimating gas of a transfer tx against this MetaCoin contract

curl -X POST -d '{"id":1234, "method": "eth_estimateGas", "params": [{"from": "0x5fda30bb72b8dfe20e48a00dfc108d0915be9bb0","to":"0x272c19a069cdfea31e913524501494c26830a3f1","data":"0x90b98a11000000000000000000000000b0a34f864f14506216e7178df35e031161cc3c9e0000000000000000000000000000000000000000000000000000000000000001"}]}' https://morden.infura.io | jsonogram

gives us a new number
-> "0xbd4d" 48461

what the math??

actually executing the tx
http://testnet.etherscan.io/tx/0x2d933ff8da5a61da181f2c0228eab1c3513cc665854f5ab6d2983d7c611119f3
yields the same number
-> "0xbd4d" 48461

@danfinlay
Copy link
Contributor

I've replicated this failing example with the testrpc test suite on this branch.

Interestingly, the gas estimation was correct when I used the testrpc internal sample contract, but did occur when using @kumavis's example transaction.

@danfinlay
Copy link
Contributor

I'm probably going crazy here, but the ratio of wrongness is approximately the square root of 2.

actual / estimated = 48844 / 34524 = 1.4147839184335536
Math.sqrt(2) = 1.4142135623730951

@danfinlay
Copy link
Contributor

@tcoulter Could you please take a look at this, and at least figure out if this is a problem with testrpc or ethereumjs-vm? This has broken the interaction between MetaMask and testrpc, and I was hoping to show that off at our presentation tomorrow.

@kumavis
Copy link
Contributor Author

kumavis commented Jun 2, 2016

@FlySwatter looks like your test "should return the same value from estimateGas as eth_sendTransaction actually uses for another contract" is setup incorrectly

it falls through all entry points (3fa4f245, 55241077) and ends (035 STOP)

seems like the tx is pointing at a contract with an incompatible abi

should return the same value from estimateGas as eth_sendTransaction actually uses for another contract
000  PUSH1 60
002  PUSH1 40
004  MSTORE 
005  PUSH1 e0
007  PUSH1 02
009  EXP 
010  PUSH1 00
012  CALLDATALOAD 
013  DIV 
014  PUSH4 3fa4f245
019  DUP2 
020  EQ 
021  PUSH1 24
023  JUMPI 
024  DUP1 
025  PUSH4 55241077
030  EQ 
031  PUSH1 2c
033  JUMPI 
034  JUMPDEST 
035  STOP 

@kumavis
Copy link
Contributor Author

kumavis commented Jun 2, 2016

so for my original setup, the stack traces for estimateGas and processing the tx are 100% the same
https://www.diffchecker.com/etmg8ehh

which means the issue is 100% related to ethereumjs/ethereumjs-monorepo#64

@kumavis
Copy link
Contributor Author

kumavis commented Jun 2, 2016

continuing the thread on ethereumjs/ethereumjs-monorepo#64

@danfinlay
Copy link
Contributor

doh

@kumavis
Copy link
Contributor Author

kumavis commented Jun 2, 2016

despite how many times I wrote 100% up there, it turns out it was unrelated to ethereumjs/ethereumjs-monorepo#64 !

@kumavis
Copy link
Contributor Author

kumavis commented Jun 2, 2016

fixed in 7.7.3
MetaMask/web3-provider-engine@f98982b

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants