Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -1602,6 +1602,7 @@ func (app *App) ProcessBlock(ctx sdk.Context, txs [][]byte, req BlockProcessRequ
evmTxs := make([]*evmtypes.MsgEVMTransaction, len(txs)) // nil for non-EVM txs
txResults := make([]*abci.ExecTxResult, len(txs))
typedTxs := app.DecodeTransactionsConcurrently(ctx, txs)
app.EvmKeeper.SetTypedTxs(req.GetHeight(), typedTxs)

prioritizedTxs, otherTxs, prioritizedTypedTxs, otherTypedTxs, prioritizedIndices, otherIndices := app.PartitionPrioritizedTxs(ctx, txs, typedTxs)

Expand Down Expand Up @@ -1865,14 +1866,10 @@ func (app *App) RPCContextProvider(i int64) sdk.Context {
func (app *App) RegisterTendermintService(clientCtx client.Context) {
tmservice.RegisterTendermintService(app.BaseApp.GRPCQueryRouter(), clientCtx, app.interfaceRegistry)
txConfigProvider := func(height int64) client.TxConfig {
if app.ChainID != "pacific-1" {
return app.encodingConfig.TxConfig
if evmrpc.IsPreV606Upgrade(app.ChainID, height) {
return app.legacyEncodingConfig.TxConfig
}
// use current for post v6.0.6 heights
if height >= v606UpgradeHeight {
return app.encodingConfig.TxConfig
}
return app.legacyEncodingConfig.TxConfig
return app.encodingConfig.TxConfig
}

if app.evmRPCConfig.HTTPEnabled {
Expand Down
2 changes: 0 additions & 2 deletions app/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,5 +186,3 @@ func (app App) RegisterUpgradeHandlers() {
})
}
}

const v606UpgradeHeight = 151573570
69 changes: 43 additions & 26 deletions evmrpc/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
tmClient rpcclient.Client
keeper *keeper.Keeper
ctxProvider func(int64) sdk.Context
txConfigProvider func(int64) client.TxConfig
txDecoder sdk.TxDecoder
legacyTxDecoder sdk.TxDecoder
connectionType ConnectionType
namespace string
includeShellReceipts bool
Expand All @@ -55,7 +56,8 @@
tmClient: tmClient,
keeper: k,
ctxProvider: ctxProvider,
txConfigProvider: txConfigProvider,
txDecoder: txConfigProvider(V606UpgradeHeight + 1).TxDecoder(),
legacyTxDecoder: txConfigProvider(V606UpgradeHeight - 1).TxDecoder(),
connectionType: connectionType,
includeShellReceipts: false,
includeBankTransfers: false,
Expand All @@ -75,7 +77,8 @@
tmClient: tmClient,
keeper: k,
ctxProvider: ctxProvider,
txConfigProvider: txConfigProvider,
txDecoder: txConfigProvider(V606UpgradeHeight + 1).TxDecoder(),
legacyTxDecoder: txConfigProvider(V606UpgradeHeight - 1).TxDecoder(),
connectionType: connectionType,
includeShellReceipts: true,
includeBankTransfers: false,
Expand Down Expand Up @@ -151,7 +154,11 @@
if err != nil {
return nil, err
}
return EncodeTmBlock(a.ctxProvider, a.txConfigProvider, block, blockRes, a.keeper, fullTx, a.includeBankTransfers, includeSyntheticTxs, isPanicTx)
txDecoder := a.txDecoder
if IsPreV606Upgrade(a.ctxProvider(LatestCtxHeight).ChainID(), int64(block.Block.Height)) {

Check failure on line 158 in evmrpc/block.go

View workflow job for this annotation

GitHub Actions / lint

unnecessary conversion (unconvert)
txDecoder = a.legacyTxDecoder
}
return EncodeTmBlock(a.ctxProvider, txDecoder, block, blockRes, a.keeper, fullTx, a.includeBankTransfers, includeSyntheticTxs, isPanicTx)
}

func (a *BlockAPI) GetBlockByNumber(ctx context.Context, number rpc.BlockNumber, fullTx bool) (result map[string]interface{}, returnErr error) {
Expand Down Expand Up @@ -206,7 +213,11 @@
if err != nil {
return nil, err
}
return EncodeTmBlock(a.ctxProvider, a.txConfigProvider, block, blockRes, a.keeper, fullTx, a.includeBankTransfers, includeSyntheticTxs, isPanicTx)
txDecoder := a.txDecoder
if IsPreV606Upgrade(a.ctxProvider(LatestCtxHeight).ChainID(), int64(block.Block.Height)) {

Check failure on line 217 in evmrpc/block.go

View workflow job for this annotation

GitHub Actions / lint

unnecessary conversion (unconvert)
txDecoder = a.legacyTxDecoder
}
return EncodeTmBlock(a.ctxProvider, txDecoder, block, blockRes, a.keeper, fullTx, a.includeBankTransfers, includeSyntheticTxs, isPanicTx)
}

func (a *BlockAPI) GetBlockReceipts(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (result []map[string]interface{}, returnErr error) {
Expand All @@ -225,13 +236,11 @@

// Get all tx hashes for the block
height := block.Block.Header.Height
sdkCtx := a.ctxProvider(LatestCtxHeight)
signer := ethtypes.MakeSigner(
types.DefaultChainConfig().EthereumConfig(a.keeper.ChainID(sdkCtx)),
big.NewInt(sdkCtx.BlockHeight()),
uint64(sdkCtx.BlockTime().Unix()),
)
txHashes := getTxHashesFromBlock(a.ctxProvider, a.txConfigProvider, a.keeper, block, signer, shouldIncludeSynthetic(a.namespace))
txDecoder := a.txDecoder
if IsPreV606Upgrade(a.ctxProvider(LatestCtxHeight).ChainID(), int64(block.Block.Height)) {

Check failure on line 240 in evmrpc/block.go

View workflow job for this annotation

GitHub Actions / lint

unnecessary conversion (unconvert)
txDecoder = a.legacyTxDecoder
}
txHashes := getTxHashesFromBlock(a.ctxProvider, txDecoder, a.keeper, block, shouldIncludeSynthetic(a.namespace), true)
// Get tx receipts for all hashes in parallel
wg := sync.WaitGroup{}
mtx := sync.Mutex{}
Expand All @@ -250,7 +259,7 @@
mtx.Unlock()
}
} else {
encodedReceipt, err := encodeReceipt(a.ctxProvider, a.txConfigProvider, receipt, a.keeper, block, a.includeShellReceipts, signer)
encodedReceipt, err := encodeReceipt(a.ctxProvider, txDecoder, receipt, a.keeper, block, a.includeShellReceipts)
if err != nil {
mtx.Lock()
returnErr = err
Expand Down Expand Up @@ -278,7 +287,7 @@

func EncodeTmBlock(
ctxProvider func(int64) sdk.Context,
txConfigProvider func(int64) client.TxConfig,
txDecoder sdk.TxDecoder,
block *coretypes.ResultBlock,
blockRes *coretypes.ResultBlockResults,
k *keeper.Keeper,
Expand All @@ -301,30 +310,30 @@
chainConfig := types.DefaultChainConfig().EthereumConfig(k.ChainID(ctx))
transactions := []interface{}{}
latestCtx := ctxProvider(LatestCtxHeight)
signer := ethtypes.MakeSigner(
types.DefaultChainConfig().EthereumConfig(k.ChainID(latestCtx)),
big.NewInt(latestCtx.BlockHeight()),
uint64(latestCtx.BlockTime().Unix()),
)
msgs := filterTransactions(k, ctxProvider, txConfigProvider, block, signer, includeSyntheticTxs, includeBankTransfers)
msgs := filterTransactions(k, ctxProvider, txDecoder, block, includeSyntheticTxs, includeBankTransfers, true)

blockBloom := make([]byte, ethtypes.BloomByteLength)
for _, msg := range msgs {
blockGasUsed += blockRes.TxsResults[msg.index].GasUsed
switch m := msg.msg.(type) {
case *types.MsgEVMTransaction:
ethtx, _ := m.AsTransaction()
hash := ethtx.Hash()
receipt, _ := k.GetReceipt(latestCtx, hash)
if !fullTx {
transactions = append(transactions, hash.Hex())
} else {
newTx := ethapi.NewRPCTransaction(ethtx, blockhash, number.Uint64(), uint64(blockTime.Second()), uint64(len(transactions)), baseFeePerGas, chainConfig)
newTx := ethapi.NewRPCTransaction(ethtx, blockhash, number.Uint64(), uint64(blockTime.Unix()), uint64(len(transactions)), baseFeePerGas, chainConfig)
replaceFrom(newTx, receipt)
transactions = append(transactions, newTx)
}
receipt, _ := k.GetReceipt(latestCtx, hash)
or := make([]byte, ethtypes.BloomByteLength)
bitutil.ORBytes(or, blockBloom, receipt.LogsBloom[:])
bloom := ethtypes.Bloom{}
bloom.SetBytes(receipt.LogsBloom)
bitutil.ORBytes(or, blockBloom, bloom[:])
blockBloom = or
// derive gas used from receipt as TxResult.GasUsed may not be accurate
// for ante-failing EVM txs.
blockGasUsed += int64(receipt.GasUsed)
case *wasmtypes.MsgExecuteContract:
th := sha256.Sum256(block.Block.Txs[msg.index])
receipt, _ := k.GetReceipt(latestCtx, th)
Expand All @@ -350,8 +359,11 @@
})
}
or := make([]byte, ethtypes.BloomByteLength)
bitutil.ORBytes(or, blockBloom, receipt.LogsBloom[:])
bloom := ethtypes.Bloom{}
bloom.SetBytes(receipt.LogsBloom)
bitutil.ORBytes(or, blockBloom, bloom[:])
blockBloom = or
blockGasUsed += blockRes.TxsResults[msg.index].GasUsed
case *banktypes.MsgSend:
th := sha256.Sum256(block.Block.Txs[msg.index])
if !fullTx {
Expand All @@ -373,6 +385,7 @@
rpcTx.TransactionIndex = (*hexutil.Uint64)(&ti)
transactions = append(transactions, rpcTx)
}
blockGasUsed += blockRes.TxsResults[msg.index].GasUsed
}
}
if len(transactions) == 0 {
Expand Down Expand Up @@ -420,8 +433,12 @@
func (a *BlockAPI) getEvmTxCount(txs tmtypes.Txs, height int64) *hexutil.Uint {
cnt := 0
// Only count eth txs
txDecoder := a.txDecoder
if IsPreV606Upgrade(a.ctxProvider(LatestCtxHeight).ChainID(), height) {
txDecoder = a.legacyTxDecoder
}
for _, tx := range txs {
ethtx := getEthTxForTxBz(tx, a.txConfigProvider(height).TxDecoder())
ethtx := getEthTxForTxBz(tx, txDecoder)
if ethtx != nil {
cnt += 1
}
Expand Down
52 changes: 33 additions & 19 deletions evmrpc/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"encoding/json"
"errors"
"fmt"
"math/big"
"sort"
"sync"
"time"
Expand All @@ -20,7 +19,6 @@ import (
"github.com/hashicorp/golang-lru/v2/expirable"
"github.com/sei-protocol/sei-chain/utils/metrics"
"github.com/sei-protocol/sei-chain/x/evm/keeper"
"github.com/sei-protocol/sei-chain/x/evm/types"
evmtypes "github.com/sei-protocol/sei-chain/x/evm/types"
rpcclient "github.com/tendermint/tendermint/rpc/client"
"github.com/tendermint/tendermint/rpc/coretypes"
Expand Down Expand Up @@ -70,6 +68,20 @@ func getCachedReceipt(blockHeight int64, txHash common.Hash) (*evmtypes.Receipt,
return nil, false
}

func getOrSetCachedReceipt(ctx sdk.Context, k *keeper.Keeper, block *coretypes.ResultBlock, txHash common.Hash) (*evmtypes.Receipt, bool) {
blockHeight := block.Block.Height
receipt, found := getCachedReceipt(blockHeight, txHash)
if found {
return receipt, true
}
receipt, err := k.GetReceipt(ctx, txHash)
if err != nil {
return nil, false
}
setCachedReceipt(blockHeight, block, txHash, receipt)
return receipt, true
}

// LoadOrStore ensures atomic cache entry creation (like sync.Map.LoadOrStore)
func loadOrStoreCacheEntry(blockHeight int64, block *coretypes.ResultBlock) *BlockCacheEntry {
// Fast path: try to get existing entry
Expand Down Expand Up @@ -276,7 +288,15 @@ func NewFilterAPI(tmClient rpcclient.Client, k *keeper.Keeper, ctxProvider func(
}

shutdownCtx, shutdownCancel := context.WithCancel(context.Background())
logFetcher := &LogFetcher{tmClient: tmClient, k: k, ctxProvider: ctxProvider, txConfigProvider: txConfigProvider, filterConfig: filterConfig, includeSyntheticReceipts: shouldIncludeSynthetic(namespace)}
logFetcher := &LogFetcher{
tmClient: tmClient,
k: k,
ctxProvider: ctxProvider,
txDecoder: txConfigProvider(V606UpgradeHeight + 1).TxDecoder(),
legacyTxDecoder: txConfigProvider(V606UpgradeHeight - 1).TxDecoder(),
filterConfig: filterConfig,
includeSyntheticReceipts: shouldIncludeSynthetic(namespace),
}
filters := make(map[ethrpc.ID]filter)
api := &FilterAPI{
namespace: namespace,
Expand Down Expand Up @@ -629,7 +649,8 @@ func (a *FilterAPI) Cleanup() {
type LogFetcher struct {
tmClient rpcclient.Client
k *keeper.Keeper
txConfigProvider func(int64) client.TxConfig
txDecoder sdk.TxDecoder
legacyTxDecoder sdk.TxDecoder
ctxProvider func(int64) sdk.Context
filterConfig *FilterConfig
includeSyntheticReceipts bool
Expand Down Expand Up @@ -817,22 +838,15 @@ func (f *LogFetcher) collectLogs(block *coretypes.ResultBlock, crit filters.Filt
ctx := f.ctxProvider(block.Block.Height)
totalLogs := uint(0)
evmTxIndex := 0
signer := ethtypes.MakeSigner(
types.DefaultChainConfig().EthereumConfig(f.k.ChainID(ctx)),
big.NewInt(ctx.BlockHeight()),
uint64(ctx.BlockTime().Unix()),
)

for _, hash := range getTxHashesFromBlock(f.ctxProvider, f.txConfigProvider, f.k, block, signer, f.includeSyntheticReceipts) {
receipt, found := getCachedReceipt(block.Block.Height, hash.hash)
decoder := f.txDecoder
if IsPreV606Upgrade(ctx.ChainID(), block.Block.Height) {
decoder = f.legacyTxDecoder
}
for _, hash := range getTxHashesFromBlock(f.ctxProvider, decoder, f.k, block, f.includeSyntheticReceipts, false) {
receipt, found := getOrSetCachedReceipt(ctx, f.k, block, hash.hash)
if !found {
var err error
receipt, err = f.k.GetReceipt(ctx, hash.hash)
if err != nil {
ctx.Logger().Error(fmt.Sprintf("collectLogs: unable to find receipt for hash %s", hash.hash.Hex()))
continue
}
setCachedReceipt(block.Block.Height, block, hash.hash, receipt)
ctx.Logger().Error(fmt.Sprintf("collectLogs: unable to find receipt for hash %s", hash.hash.Hex()))
continue
}

txLogs := keeper.GetLogsForTx(receipt, totalLogs)
Expand Down
2 changes: 1 addition & 1 deletion evmrpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ func NewEVMWebSocketServer(
},
{
Namespace: "eth",
Service: NewSubscriptionAPI(tmClient, k, ctxProvider, &LogFetcher{tmClient: tmClient, k: k, ctxProvider: ctxProvider, txConfigProvider: txConfigProvider}, &SubscriptionConfig{subscriptionCapacity: 100, newHeadLimit: config.MaxSubscriptionsNewHead}, &FilterConfig{timeout: config.FilterTimeout, maxLog: config.MaxLogNoBlock, maxBlock: config.MaxBlocksForLog}, ConnectionTypeWS),
Service: NewSubscriptionAPI(tmClient, k, ctxProvider, &LogFetcher{tmClient: tmClient, k: k, ctxProvider: ctxProvider, txDecoder: txConfigProvider(V606UpgradeHeight + 1).TxDecoder(), legacyTxDecoder: txConfigProvider(V606UpgradeHeight - 1).TxDecoder()}, &SubscriptionConfig{subscriptionCapacity: 100, newHeadLimit: config.MaxSubscriptionsNewHead}, &FilterConfig{timeout: config.FilterTimeout, maxLog: config.MaxLogNoBlock, maxBlock: config.MaxBlocksForLog}, ConnectionTypeWS),
},
{
Namespace: "web3",
Expand Down
35 changes: 25 additions & 10 deletions evmrpc/simulate.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,8 @@ type Backend struct {
*eth.EthAPIBackend
ctxProvider func(int64) sdk.Context
txConfigProvider func(int64) client.TxConfig
txDecoder sdk.TxDecoder
legacyTxDecoder sdk.TxDecoder
keeper *keeper.Keeper
tmClient rpcclient.Client
config *SimulateConfig
Expand All @@ -226,6 +228,8 @@ func NewBackend(ctxProvider func(int64) sdk.Context, keeper *keeper.Keeper, txCo
ctxProvider: ctxProvider,
keeper: keeper,
txConfigProvider: txConfigProvider,
txDecoder: txConfigProvider(V606UpgradeHeight + 1).TxDecoder(),
legacyTxDecoder: txConfigProvider(V606UpgradeHeight - 1).TxDecoder(),
tmClient: tmClient,
config: config,
app: app,
Expand Down Expand Up @@ -261,7 +265,11 @@ func (b *Backend) GetTransaction(ctx context.Context, txHash common.Hash) (tx *e
}
txIndex := hexutil.Uint(receipt.TransactionIndex)
tmTx := block.Block.Txs[int(txIndex)]
tx = getEthTxForTxBz(tmTx, b.txConfigProvider(block.Block.Height).TxDecoder())
txDecoder := b.txDecoder
if IsPreV606Upgrade(sdkCtx.ChainID(), block.Block.Height) {
txDecoder = b.legacyTxDecoder
}
tx = getEthTxForTxBz(tmTx, txDecoder)
blockHash = common.BytesToHash(block.Block.Header.Hash().Bytes())
return tx, blockHash, uint64(txHeight), uint64(txIndex), nil
}
Expand Down Expand Up @@ -300,18 +308,17 @@ func (b Backend) BlockByNumber(ctx context.Context, bn rpc.BlockNumber) (*ethtyp
sdkCtx := b.ctxProvider(LatestCtxHeight)
var txs []*ethtypes.Transaction
var metadata []tracersutils.TraceBlockMetadata
signer := ethtypes.MakeSigner(
types.DefaultChainConfig().EthereumConfig(b.keeper.ChainID(sdkCtx)),
big.NewInt(sdkCtx.BlockHeight()),
uint64(sdkCtx.BlockTime().Unix()),
)
msgs := filterTransactions(b.keeper, b.ctxProvider, b.txConfigProvider, tmBlock, signer, false, false)
txDecoder := b.txDecoder
if IsPreV606Upgrade(sdkCtx.ChainID(), blockNum) {
txDecoder = b.legacyTxDecoder
}
msgs := filterTransactions(b.keeper, b.ctxProvider, txDecoder, tmBlock, false, false, true)
idxToMsgs := make(map[int]sdk.Msg, len(msgs))
for _, msg := range msgs {
idxToMsgs[msg.index] = msg.msg
}
for i := range blockRes.TxsResults {
decoded, err := b.txConfigProvider(blockRes.Height).TxDecoder()(tmBlock.Block.Txs[i])
decoded, err := txDecoder(tmBlock.Block.Txs[i])
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -405,7 +412,11 @@ func (b *Backend) StateAtTransaction(ctx context.Context, block *ethtypes.Block,
return nil, vm.BlockContext{}, nil, emptyRelease, errors.New("transaction not found")
}
tx := txs[txIndex]
sdkTx, err := b.txConfigProvider(block.Number().Int64()).TxDecoder()(tx)
txDecoder := b.txDecoder
if IsPreV606Upgrade(stateDB.(*state.DBImpl).Ctx().ChainID(), block.Number().Int64()) {
txDecoder = b.legacyTxDecoder
}
sdkTx, err := txDecoder(tx)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -439,11 +450,15 @@ func (b *Backend) ReplayTransactionTillIndex(ctx context.Context, block *ethtype
if txIndex < 0 {
return state.NewDBImpl(sdkCtx.WithIsEVM(true), b.keeper, true), tmBlock.Block.Txs, nil
}
txDecoder := b.txDecoder
if IsPreV606Upgrade(sdkCtx.ChainID(), block.Number().Int64()) {
txDecoder = b.legacyTxDecoder
}
for idx, tx := range tmBlock.Block.Txs {
if idx > txIndex {
break
}
sdkTx, err := b.txConfigProvider(block.Number().Int64()).TxDecoder()(tx)
sdkTx, err := txDecoder(tx)
if err != nil {
panic(err)
}
Expand Down
Loading
Loading