Skip to content

Commit

Permalink
fix evm outtx hash mismatch
Browse files Browse the repository at this point in the history
  • Loading branch information
ws4charlie committed Feb 13, 2024
1 parent ae22f1e commit b1c6c24
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
8 changes: 8 additions & 0 deletions zetaclient/evm/evm_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,7 @@ func (ob *ChainClient) checkTxInclusion(tx *ethtypes.Transaction, blockNumber ui
}
txAtIndex := block.Transactions()[txIndex]
if txAtIndex.Hash() != tx.Hash() {
ob.RemoveCachedBlock(blockNumber) // clean stale block from cache
return fmt.Errorf("transaction at index %d has different hash %s, txHash %s nonce %d block %d",
txIndex, txAtIndex.Hash().Hex(), tx.Hash(), tx.Nonce(), blockNumber)
}
Expand All @@ -810,6 +811,7 @@ func (ob *ChainClient) checkTxInclusion(tx *ethtypes.Transaction, blockNumber ui
}
txAtIndex := blockRPC.Transactions[txIndex]
if ethcommon.HexToHash(txAtIndex.Hash) != tx.Hash() {
ob.RemoveCachedBlock(blockNumber) // clean stale block from cache
return fmt.Errorf("transaction at index %d has different hash %s, txHash %s nonce %d block %d",
txIndex, txAtIndex.Hash, tx.Hash(), tx.Nonce(), blockNumber)
}
Expand Down Expand Up @@ -1520,3 +1522,9 @@ func (ob *ChainClient) GetBlockByNumberCached(blockNumber uint64) (*ethtypes.Blo
ob.blockCache.Add(blockNumber, block)
return block, nil, false, false, nil
}

// RemoveCachedBlock remove block from cache
func (ob *ChainClient) RemoveCachedBlock(blockNumber uint64) {
ob.blockCache.Remove(blockNumber)
ob.blockCacheV3.Remove(blockNumber)
}
37 changes: 37 additions & 0 deletions zetaclient/evm/evm_client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package evm

import (
"math/big"
"testing"

ethtypes "github.com/ethereum/go-ethereum/core/types"
lru "github.com/hashicorp/golang-lru"
"github.com/stretchr/testify/require"
)

func TestEVMBlockCache(t *testing.T) {
// create client
blockCache, err := lru.New(1000)
require.NoError(t, err)
blockCacheV3, err := lru.New(1000)
require.NoError(t, err)
ob := ChainClient{
blockCache: blockCache,
blockCacheV3: blockCacheV3,
}

// delete non-existing block should not panic
blockNumber := int64(10388180)
// #nosec G701 possible nummber
ob.RemoveCachedBlock(uint64(blockNumber))

// add a block
header := &ethtypes.Header{
Number: big.NewInt(blockNumber),
}
block := ethtypes.NewBlock(header, nil, nil, nil, nil)
ob.blockCache.Add(blockNumber, block)

// delete the block should not panic
ob.RemoveCachedBlock(uint64(blockNumber))
}

0 comments on commit b1c6c24

Please sign in to comment.