Skip to content

Commit

Permalink
associate for traceBlock
Browse files Browse the repository at this point in the history
  • Loading branch information
codchen committed May 1, 2024
1 parent 3fee83f commit 44293e1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
23 changes: 22 additions & 1 deletion evmrpc/simulate.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rpc"
"github.com/sei-protocol/sei-chain/utils"
"github.com/sei-protocol/sei-chain/x/evm/ante"
"github.com/sei-protocol/sei-chain/x/evm/keeper"
"github.com/sei-protocol/sei-chain/x/evm/state"
"github.com/sei-protocol/sei-chain/x/evm/types"
Expand Down Expand Up @@ -297,7 +298,9 @@ func (b *Backend) StateAtTransaction(ctx context.Context, block *ethtypes.Block,
if !associatedNow {
return nil, vm.BlockContext{}, nil, nil, fmt.Errorf("address %s is not associated in the latest height", msg.From.Hex())
}
b.keeper.SetAddressMapping(statedb.Ctx(), seiAddr, msg.From)
if err := ante.NewEVMPreprocessDecorator(b.keeper, b.keeper.AccountKeeper()).AssociateAddresses(statedb.Ctx(), seiAddr, msg.From, nil); err != nil {
return nil, vm.BlockContext{}, nil, nil, err
}
}

if idx == txIndex {
Expand All @@ -319,6 +322,24 @@ func (b *Backend) StateAtTransaction(ctx context.Context, block *ethtypes.Block,
func (b *Backend) StateAtBlock(ctx context.Context, block *ethtypes.Block, reexec uint64, base vm.StateDB, readOnly bool, preferDisk bool) (vm.StateDB, tracers.StateReleaseFunc, error) {
emptyRelease := func() {}
statedb := state.NewDBImpl(b.ctxProvider(block.Number().Int64()-1), b.keeper, true)
signer := ethtypes.MakeSigner(b.ChainConfig(), block.Number(), block.Time())
for _, tx := range block.Transactions() {
msg, _ := core.TransactionToMessage(tx, signer, block.BaseFee())

// set address association for the sender if not present. Note that here we take the shortcut
// of querying from the latest height with the assumption that if this tx has been processed
// at all then its association must be present in the latest height
_, associated := b.keeper.GetSeiAddress(statedb.Ctx(), msg.From)
if !associated {
seiAddr, associatedNow := b.keeper.GetSeiAddress(b.ctxProvider(LatestCtxHeight), msg.From)
if !associatedNow {
return nil, emptyRelease, fmt.Errorf("address %s is not associated in the latest height", msg.From.Hex())
}
if err := ante.NewEVMPreprocessDecorator(b.keeper, b.keeper.AccountKeeper()).AssociateAddresses(statedb.Ctx(), seiAddr, msg.From, nil); err != nil {
return nil, emptyRelease, err
}
}
}
return statedb, emptyRelease, nil
}

Expand Down
6 changes: 3 additions & 3 deletions x/evm/ante/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,15 @@ func (p *EVMPreprocessDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate
return ctx, sdkerrors.Wrap(sdkerrors.ErrInsufficientFunds, "account needs to have at least 1Sei to force association")
}
}
if err := p.associateAddresses(ctx, seiAddr, evmAddr, pubkey); err != nil {
if err := p.AssociateAddresses(ctx, seiAddr, evmAddr, pubkey); err != nil {
return ctx, err
}
return ctx.WithPriority(antedecorators.EVMAssociatePriority), nil // short-circuit without calling next
} else if isAssociated {
// noop; for readability
} else {
// not associatedTx and not already associated
if err := p.associateAddresses(ctx, seiAddr, evmAddr, pubkey); err != nil {
if err := p.AssociateAddresses(ctx, seiAddr, evmAddr, pubkey); err != nil {
return ctx, err
}
if p.evmKeeper.EthReplayConfig.Enabled {
Expand All @@ -107,7 +107,7 @@ func (p *EVMPreprocessDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate
return next(ctx, tx, simulate)
}

func (p *EVMPreprocessDecorator) associateAddresses(ctx sdk.Context, seiAddr sdk.AccAddress, evmAddr common.Address, pubkey cryptotypes.PubKey) error {
func (p *EVMPreprocessDecorator) AssociateAddresses(ctx sdk.Context, seiAddr sdk.AccAddress, evmAddr common.Address, pubkey cryptotypes.PubKey) error {
p.evmKeeper.SetAddressMapping(ctx, seiAddr, evmAddr)
if acc := p.accountKeeper.GetAccount(ctx, seiAddr); acc.GetPubKey() == nil {
if err := acc.SetPubKey(pubkey); err != nil {
Expand Down

0 comments on commit 44293e1

Please sign in to comment.