From 055d715cc323a831ba5a3827b5bb415f313fb22d Mon Sep 17 00:00:00 2001 From: codchen Date: Wed, 30 Oct 2024 16:03:15 +0800 Subject: [PATCH] Use tendermint store to get Tx hashes instead of storing explicitly --- evmrpc/block.go | 19 ++++++++++++++++++- evmrpc/filter.go | 36 +++++++++++++++++++++++++++++++++--- evmrpc/server.go | 6 +++--- x/evm/keeper/tx.go | 2 ++ 4 files changed, 56 insertions(+), 7 deletions(-) diff --git a/evmrpc/block.go b/evmrpc/block.go index 6a51d6414..360c86829 100644 --- a/evmrpc/block.go +++ b/evmrpc/block.go @@ -154,7 +154,24 @@ func (a *BlockAPI) GetBlockReceipts(ctx context.Context, blockNrOrHash rpc.Block // Get all tx hashes for the block height := block.Block.Header.Height - txHashes := a.keeper.GetTxHashesOnHeight(a.ctxProvider(height), height) + txHashes := []common.Hash{} + for i, tx := range block.Block.Data.Txs { + sdkTx, err := a.txConfig.TxDecoder()(tx) + if err != nil { + fmt.Printf("error decoding tx %d in block %d, skipping\n", i, height) + continue + } + if len(sdkTx.GetMsgs()) == 0 { + continue + } + if evmTx, ok := sdkTx.GetMsgs()[0].(*types.MsgEVMTransaction); ok { + if evmTx.IsAssociateTx() { + continue + } + ethtx, _ := evmTx.AsTransaction() + txHashes = append(txHashes, ethtx.Hash()) + } + } // Get tx receipts for all hashes in parallel wg := sync.WaitGroup{} mtx := sync.Mutex{} diff --git a/evmrpc/filter.go b/evmrpc/filter.go index 167118ede..14d096a4c 100644 --- a/evmrpc/filter.go +++ b/evmrpc/filter.go @@ -8,6 +8,7 @@ import ( "sync" "time" + "github.com/cosmos/cosmos-sdk/client" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/ethereum/go-ethereum/common" ethtypes "github.com/ethereum/go-ethereum/core/types" @@ -15,6 +16,7 @@ import ( ethrpc "github.com/ethereum/go-ethereum/rpc" "github.com/sei-protocol/sei-chain/utils" "github.com/sei-protocol/sei-chain/x/evm/keeper" + "github.com/sei-protocol/sei-chain/x/evm/types" rpcclient "github.com/tendermint/tendermint/rpc/client" "github.com/tendermint/tendermint/rpc/coretypes" tmtypes "github.com/tendermint/tendermint/types" @@ -63,8 +65,8 @@ type EventItemDataWrapper struct { Value json.RawMessage `json:"value"` } -func NewFilterAPI(tmClient rpcclient.Client, k *keeper.Keeper, ctxProvider func(int64) sdk.Context, filterConfig *FilterConfig, connectionType ConnectionType, namespace string) *FilterAPI { - logFetcher := &LogFetcher{tmClient: tmClient, k: k, ctxProvider: ctxProvider, filterConfig: filterConfig, includeSyntheticReceipts: shouldIncludeSynthetic(namespace)} +func NewFilterAPI(tmClient rpcclient.Client, k *keeper.Keeper, ctxProvider func(int64) sdk.Context, txConfig client.TxConfig, filterConfig *FilterConfig, connectionType ConnectionType, namespace string) *FilterAPI { + logFetcher := &LogFetcher{tmClient: tmClient, k: k, ctxProvider: ctxProvider, txConfig: txConfig, filterConfig: filterConfig, includeSyntheticReceipts: shouldIncludeSynthetic(namespace)} filters := make(map[ethrpc.ID]filter) api := &FilterAPI{ namespace: namespace, @@ -276,6 +278,7 @@ func (a *FilterAPI) UninstallFilter( type LogFetcher struct { tmClient rpcclient.Client k *keeper.Keeper + txConfig client.TxConfig ctxProvider func(int64) sdk.Context filterConfig *FilterConfig includeSyntheticReceipts bool @@ -358,7 +361,34 @@ func (f *LogFetcher) FindBlockesByBloom(begin, end int64, filters [][]bloomIndex func (f *LogFetcher) FindLogsByBloom(height int64, filters [][]bloomIndexes) (res []*ethtypes.Log) { ctx := f.ctxProvider(LatestCtxHeight) - txHashes := f.k.GetTxHashesOnHeight(ctx, height) + block, err := blockByNumberWithRetry(context.Background(), f.tmClient, &height, 1) + if err != nil { + fmt.Printf("error getting block when querying logs: %s\n", err) + return + } + + if block == nil { + fmt.Printf("no block found when querying logs for height %d\n", height) + return + } + txHashes := []common.Hash{} + for i, tx := range block.Block.Data.Txs { + sdkTx, err := f.txConfig.TxDecoder()(tx) + if err != nil { + fmt.Printf("error decoding tx %d in block %d, skipping\n", i, height) + continue + } + if len(sdkTx.GetMsgs()) == 0 { + continue + } + if evmTx, ok := sdkTx.GetMsgs()[0].(*types.MsgEVMTransaction); ok { + if evmTx.IsAssociateTx() { + continue + } + ethtx, _ := evmTx.AsTransaction() + txHashes = append(txHashes, ethtx.Hash()) + } + } for _, hash := range txHashes { receipt, err := f.k.GetReceipt(ctx, hash) if err != nil { diff --git a/evmrpc/server.go b/evmrpc/server.go index a76045e29..9691d5b07 100644 --- a/evmrpc/server.go +++ b/evmrpc/server.go @@ -87,11 +87,11 @@ func NewEVMHTTPServer( }, { Namespace: "eth", - Service: NewFilterAPI(tmClient, k, ctxProvider, &FilterConfig{timeout: config.FilterTimeout, maxLog: config.MaxLogNoBlock, maxBlock: config.MaxBlocksForLog}, ConnectionTypeHTTP, "eth"), + Service: NewFilterAPI(tmClient, k, ctxProvider, txConfig, &FilterConfig{timeout: config.FilterTimeout, maxLog: config.MaxLogNoBlock, maxBlock: config.MaxBlocksForLog}, ConnectionTypeHTTP, "eth"), }, { Namespace: "sei", - Service: NewFilterAPI(tmClient, k, ctxProvider, &FilterConfig{timeout: config.FilterTimeout, maxLog: config.MaxLogNoBlock, maxBlock: config.MaxBlocksForLog}, ConnectionTypeHTTP, "sei"), + Service: NewFilterAPI(tmClient, k, ctxProvider, txConfig, &FilterConfig{timeout: config.FilterTimeout, maxLog: config.MaxLogNoBlock, maxBlock: config.MaxBlocksForLog}, ConnectionTypeHTTP, "sei"), }, { Namespace: "sei", @@ -184,7 +184,7 @@ func NewEVMWebSocketServer( }, { Namespace: "eth", - Service: NewSubscriptionAPI(tmClient, &LogFetcher{tmClient: tmClient, k: k, ctxProvider: ctxProvider}, &SubscriptionConfig{subscriptionCapacity: 100, newHeadLimit: config.MaxSubscriptionsNewHead}, &FilterConfig{timeout: config.FilterTimeout, maxLog: config.MaxLogNoBlock, maxBlock: config.MaxBlocksForLog}, ConnectionTypeWS), + Service: NewSubscriptionAPI(tmClient, &LogFetcher{tmClient: tmClient, k: k, ctxProvider: ctxProvider, txConfig: txConfig}, &SubscriptionConfig{subscriptionCapacity: 100, newHeadLimit: config.MaxSubscriptionsNewHead}, &FilterConfig{timeout: config.FilterTimeout, maxLog: config.MaxLogNoBlock, maxBlock: config.MaxBlocksForLog}, ConnectionTypeWS), }, { Namespace: "web3", diff --git a/x/evm/keeper/tx.go b/x/evm/keeper/tx.go index 1c086d565..2a5c90093 100644 --- a/x/evm/keeper/tx.go +++ b/x/evm/keeper/tx.go @@ -6,6 +6,7 @@ import ( "github.com/sei-protocol/sei-chain/x/evm/types" ) +// deprecated func (k *Keeper) GetTxHashesOnHeight(ctx sdk.Context, height int64) (res []common.Hash) { store := ctx.KVStore(k.storeKey) bz := store.Get(types.TxHashesKey(height)) @@ -18,6 +19,7 @@ func (k *Keeper) GetTxHashesOnHeight(ctx sdk.Context, height int64) (res []commo return } +// deprecated func (k *Keeper) SetTxHashesOnHeight(ctx sdk.Context, height int64, hashes []common.Hash) { if len(hashes) == 0 { return