Skip to content

Commit

Permalink
Fix revert reason message (#703)
Browse files Browse the repository at this point in the history
* Fix revert reason message

* Fix revert reason message

* fix
  • Loading branch information
ToniRamirezM committed Jun 9, 2022
1 parent 7b61286 commit b752d52
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 14 deletions.
15 changes: 1 addition & 14 deletions state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
69 changes: 69 additions & 0 deletions state/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
8 changes: 8 additions & 0 deletions test/contracts/auto/Revert.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.4;

contract Revert {
constructor () {
revert("Today is not juernes");
}
}

0 comments on commit b752d52

Please sign in to comment.