Skip to content

Commit

Permalink
Add association logic in simulate (#1609)
Browse files Browse the repository at this point in the history
* Add association logic in simulate

* associate for traceBlock
  • Loading branch information
codchen authored May 1, 2024
1 parent 717925e commit 9646724
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 6 deletions.
3 changes: 3 additions & 0 deletions evmrpc/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,9 @@ func generateTxData() {
ChainID: chainId,
})
UnconfirmedTx = unconfirmedTxBuilder.GetTx()

tracerTestTxFrom := common.HexToAddress("0x5b4eba929f3811980f5ae0c5d04fa200f837df4e")
EVMKeeper.SetAddressMapping(Ctx, sdk.AccAddress(tracerTestTxFrom[:]), tracerTestTxFrom)
}

func buildTx(txData ethtypes.DynamicFeeTx) (client.TxBuilder, *ethtypes.Transaction) {
Expand Down
33 changes: 33 additions & 0 deletions 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 @@ -288,6 +289,20 @@ func (b *Backend) StateAtTransaction(ctx context.Context, block *ethtypes.Block,
// set block context time as of the block time (block time is the time of the CURRENT block)
blockContext.Time = block.Time()

// 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, vm.BlockContext{}, nil, nil, 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, vm.BlockContext{}, nil, nil, err
}
}

if idx == txIndex {
return tx, *blockContext, statedb, emptyRelease, nil
}
Expand All @@ -307,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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ replace (
github.com/cosmos/cosmos-sdk => github.com/sei-protocol/sei-cosmos v0.3.5
github.com/cosmos/iavl => github.com/sei-protocol/sei-iavl v0.1.9
github.com/cosmos/ibc-go/v3 => github.com/sei-protocol/sei-ibc-go/v3 v3.3.0
github.com/ethereum/go-ethereum => github.com/sei-protocol/go-ethereum v1.13.5-sei-15
github.com/ethereum/go-ethereum => github.com/sei-protocol/go-ethereum v1.13.5-sei-16
github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.3-alpha.regen.1
github.com/sei-protocol/sei-db => github.com/sei-protocol/sei-db v0.0.35
// Latest goleveldb is broken, we have to stick to this version
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1343,8 +1343,8 @@ github.com/seccomp/libseccomp-golang v0.9.2-0.20220502022130-f33da4d89646/go.mod
github.com/securego/gosec/v2 v2.11.0 h1:+PDkpzR41OI2jrw1q6AdXZCbsNGNGT7pQjal0H0cArI=
github.com/securego/gosec/v2 v2.11.0/go.mod h1:SX8bptShuG8reGC0XS09+a4H2BoWSJi+fscA+Pulbpo=
github.com/segmentio/fasthash v1.0.3/go.mod h1:waKX8l2N8yckOgmSsXJi7x1ZfdKZ4x7KRMzBtS3oedY=
github.com/sei-protocol/go-ethereum v1.13.5-sei-15 h1:VSFQrbWnSDCPCQzsYDW3k07EP3yPZb+4xAcED9kSKpg=
github.com/sei-protocol/go-ethereum v1.13.5-sei-15/go.mod h1:kcRZmuzRn1lVejiFNTz4l4W7imnpq1bDAnuKS/RyhbQ=
github.com/sei-protocol/go-ethereum v1.13.5-sei-16 h1:bPQw44//5XHDZWfwO98g2Hie5HguxYZY+AiRsYMBdVg=
github.com/sei-protocol/go-ethereum v1.13.5-sei-16/go.mod h1:kcRZmuzRn1lVejiFNTz4l4W7imnpq1bDAnuKS/RyhbQ=
github.com/sei-protocol/goutils v0.0.2 h1:Bfa7Sv+4CVLNM20QcpvGb81B8C5HkQC/kW1CQpIbXDA=
github.com/sei-protocol/goutils v0.0.2/go.mod h1:iYE2DuJfEnM+APPehr2gOUXfuLuPsVxorcDO+Tzq9q8=
github.com/sei-protocol/sei-cosmos v0.3.5 h1:ibWj4uM3YeKLaGKfl1oWj36nkJo/2aZSyS7xo8Ixh6E=
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 9646724

Please sign in to comment.