From b1c6c24ffca3f3e11804bd2414bc070b2a209c1e Mon Sep 17 00:00:00 2001 From: Charlie Chen Date: Mon, 12 Feb 2024 18:37:04 -0600 Subject: [PATCH] fix evm outtx hash mismatch --- zetaclient/evm/evm_client.go | 8 +++++++ zetaclient/evm/evm_client_test.go | 37 +++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 zetaclient/evm/evm_client_test.go diff --git a/zetaclient/evm/evm_client.go b/zetaclient/evm/evm_client.go index 97b29495f6..db13df1f6f 100644 --- a/zetaclient/evm/evm_client.go +++ b/zetaclient/evm/evm_client.go @@ -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) } @@ -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) } @@ -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) +} diff --git a/zetaclient/evm/evm_client_test.go b/zetaclient/evm/evm_client_test.go new file mode 100644 index 0000000000..730cba83f9 --- /dev/null +++ b/zetaclient/evm/evm_client_test.go @@ -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 := ðtypes.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)) +}