Skip to content

Commit

Permalink
Use tendermint store to get Tx hashes instead of storing explicitly
Browse files Browse the repository at this point in the history
  • Loading branch information
codchen committed Nov 5, 2024
1 parent 38418af commit 055d715
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 7 deletions.
19 changes: 18 additions & 1 deletion evmrpc/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down
36 changes: 33 additions & 3 deletions evmrpc/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ 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"
"github.com/ethereum/go-ethereum/eth/filters"
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"
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions evmrpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions x/evm/keeper/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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
Expand Down

0 comments on commit 055d715

Please sign in to comment.