From b752d52bcc68aa23648e1ac35be06c5b23609499 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Toni=20Ram=C3=ADrez?= <58293609+ToniRamirezM@users.noreply.github.com> Date: Thu, 9 Jun 2022 09:42:55 +0200 Subject: [PATCH] Fix revert reason message (#703) * Fix revert reason message * Fix revert reason message * fix --- state/state.go | 15 +------- state/state_test.go | 69 ++++++++++++++++++++++++++++++++++ test/contracts/auto/Revert.sol | 8 ++++ 3 files changed, 78 insertions(+), 14 deletions(-) create mode 100644 test/contracts/auto/Revert.sol diff --git a/state/state.go b/state/state.go index 8f1479a474..0f6b793adc 100644 --- a/state/state.go +++ b/state/state.go @@ -334,22 +334,9 @@ func (s *State) EstimateGas(transaction *types.Transaction, senderAddress common return false, err } - if testResult.Err != nil { - // Check the application error. - // Gas apply errors are valid, and should be ignored - if isGasApplyError(testResult.Err) && shouldOmitErr { - // Specifying the transaction failed, but not providing an error - // is an indication that a valid error occurred due to low gas, - // which will increase the lower bound for the search - return true, nil - } - - return true, testResult.Err - } - // Check if an out of gas error happened during EVM execution if testResult.Failed() { - if isGasEVMError(testResult.Err) && shouldOmitErr { + if (isGasEVMError(testResult.Err) || isGasApplyError(testResult.Err)) && shouldOmitErr { // Specifying the transaction failed, but not providing an error // is an indication that a valid error occurred due to low gas, // which will increase the lower bound for the search diff --git a/state/state_test.go b/state/state_test.go index b4db759e9c..cf7dd3f728 100644 --- a/state/state_test.go +++ b/state/state_test.go @@ -2503,3 +2503,72 @@ func getMethodID(signature string) ([]byte, error) { } return hashCall.Sum(nil)[:4], nil } + +func TestRevertMessage(t *testing.T) { + var chainIDSequencer = new(big.Int).SetInt64(400) + var sequencerAddress = common.HexToAddress("0x617b3a3528F9cDd6630fd3301B9c8911F7Bf063D") + var sequencerPvtKey = "0x28b2b0318721be8c8339199172cd7cc8f5e273800a35616ec893083a4b32c02e" + var sequencerBalance = 50000 + scByteCode, err := testutils.ReadBytecode("Revert/Revert.bin") + require.NoError(t, err) + + // Init database instance + err = dbutils.InitOrReset(cfg) + require.NoError(t, err) + + // Create State db + stateDb, err = db.NewSQLDB(cfg) + require.NoError(t, err) + + // Create State tree + store := tree.NewPostgresStore(stateDb) + mt := tree.NewMerkleTree(store, tree.DefaultMerkleTreeArity) + scCodeStore := tree.NewPostgresSCCodeStore(stateDb) + stateTree := tree.NewStateTree(mt, scCodeStore) + + // Create state + st := state.NewState(stateCfg, state.NewPostgresStorage(stateDb), stateTree) + + genesisBlock := types.NewBlock(&types.Header{Number: big.NewInt(0)}, []*types.Transaction{}, []*types.Header{}, []*types.Receipt{}, &trie.StackTrie{}) + genesisBlock.ReceivedAt = time.Now() + genesis := state.Genesis{ + Block: genesisBlock, + Balances: make(map[common.Address]*big.Int), + } + + genesis.Balances[sequencerAddress] = new(big.Int).SetInt64(int64(sequencerBalance)) + err = st.SetGenesis(ctx, genesis, "") + require.NoError(t, err) + + // Register Sequencer + sequencer := state.Sequencer{ + Address: sequencerAddress, + URL: "http://www.address.com", + ChainID: chainIDSequencer, + BlockNumber: genesisBlock.Header().Number.Uint64(), + } + + err = st.AddSequencer(ctx, sequencer, "") + assert.NoError(t, err) + + // Smart Contract + tx := types.NewTx(&types.LegacyTx{ + Nonce: 0, + To: nil, + Value: new(big.Int).SetUint64(0), + Gas: uint64(sequencerBalance), + GasPrice: new(big.Int).SetUint64(1), + Data: common.Hex2Bytes(scByteCode), + }) + + privateKey, err := crypto.HexToECDSA(strings.TrimPrefix(sequencerPvtKey, "0x")) + require.NoError(t, err) + auth, err := bind.NewKeyedTransactorWithChainID(privateKey, chainIDSequencer) + require.NoError(t, err) + + signedTx, err := auth.Signer(auth.From, tx) + require.NoError(t, err) + gas, err := st.EstimateGas(signedTx, sequencerAddress) + assert.Equal(t, uint64(0x0), gas) + assert.Contains(t, err.Error(), "juernes") +} diff --git a/test/contracts/auto/Revert.sol b/test/contracts/auto/Revert.sol new file mode 100644 index 0000000000..72699db145 --- /dev/null +++ b/test/contracts/auto/Revert.sol @@ -0,0 +1,8 @@ +// SPDX-License-Identifier: GPL-3.0 +pragma solidity ^0.8.4; + +contract Revert { + constructor () { + revert("Today is not juernes"); + } +} \ No newline at end of file