Skip to content

Commit

Permalink
Optimize replay (#1505)
Browse files Browse the repository at this point in the history
* Optimize replay

* check state
  • Loading branch information
codchen authored and udpatil committed Apr 17, 2024
1 parent 41ecf82 commit 398a71d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
1 change: 1 addition & 0 deletions app/eth_replay.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ func Replay(a *App) {
a.Logger().Info(fmt.Sprintf("Verifying tx %s", tx.Hash().Hex()))
if tx.To() != nil {
a.EvmKeeper.VerifyBalance(ctx, *tx.To())
a.EvmKeeper.VerifyState(ctx, *tx.To())
}
a.EvmKeeper.VerifyTxResult(ctx, tx.Hash())
}
Expand Down
17 changes: 17 additions & 0 deletions x/evm/keeper/replay.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core"
"github.com/sei-protocol/sei-chain/x/evm/types"
)

func (k *Keeper) VerifyBalance(ctx sdk.Context, addr common.Address) {
Expand Down Expand Up @@ -87,3 +88,19 @@ func (k *Keeper) VerifyAccount(ctx sdk.Context, addr common.Address, accountData
panic(fmt.Sprintf("nonce mismatch for address %s: expected %d, got %d", addr.Hex(), nonce, k.GetNonce(ctx, addr)))
}
}

func (k *Keeper) VerifyState(ctx sdk.Context, addr common.Address) {
store := k.PrefixStore(ctx, types.StateKey(addr))
iter := store.Iterator(nil, nil)
defer iter.Close()
for ; iter.Valid(); iter.Next() {
key := common.BytesToHash(iter.Key())
ethVal, err := k.EthClient.StorageAt(ctx.Context(), addr, key, k.ReplayBlock.Number())
if err != nil {
panic(err)
}
if !bytes.Equal(iter.Value(), ethVal) {
panic(fmt.Sprintf("state mismatch for address %s hash %s: expected %X but got %X", addr.Hex(), key.Hex(), ethVal, iter.Value()))
}
}
}
7 changes: 1 addition & 6 deletions x/evm/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"fmt"
"math"
"math/big"

// this line is used by starport scaffolding # 1

Expand Down Expand Up @@ -213,11 +212,7 @@ func (am AppModule) EndBlock(ctx sdk.Context, _ abci.RequestEndBlock) []abci.Val
}
coinbase = am.keeper.GetSeiAddressOrDefault(ctx, block.BlockHeader.Coinbase)
} else if am.keeper.EthReplayConfig.Enabled {
block, err := am.keeper.EthClient.BlockByNumber(ctx.Context(), big.NewInt(ctx.BlockHeight()+am.keeper.GetReplayInitialHeight(ctx)))
if err != nil {
panic(fmt.Sprintf("error getting block at height %d", ctx.BlockHeight()+am.keeper.GetReplayInitialHeight(ctx)))
}
coinbase = am.keeper.GetSeiAddressOrDefault(ctx, block.Header_.Coinbase)
coinbase = am.keeper.GetSeiAddressOrDefault(ctx, am.keeper.ReplayBlock.Header_.Coinbase)
am.keeper.SetReplayedHeight(ctx)
} else {
coinbase = am.keeper.AccountKeeper().GetModuleAddress(authtypes.FeeCollectorName)
Expand Down

0 comments on commit 398a71d

Please sign in to comment.