Skip to content

Commit 4f92ca3

Browse files
authored
Use versioned precompiles in tracing (#2122)
* Add back legacy precompile versions * rebase * wip * tests * test * test
1 parent 90532b1 commit 4f92ca3

File tree

21 files changed

+500
-94
lines changed

21 files changed

+500
-94
lines changed

app/app.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ func New(
631631
app.EvmKeeper = *evmkeeper.NewKeeper(keys[evmtypes.StoreKey],
632632
tkeys[evmtypes.TransientStoreKey], app.GetSubspace(evmtypes.ModuleName), app.receiptStore, app.BankKeeper,
633633
&app.AccountKeeper, &app.StakingKeeper, app.TransferKeeper,
634-
wasmkeeper.NewDefaultPermissionKeeper(app.WasmKeeper), &app.WasmKeeper, &app.ConfidentialTransfersKeeper)
634+
wasmkeeper.NewDefaultPermissionKeeper(app.WasmKeeper), &app.WasmKeeper, &app.ConfidentialTransfersKeeper, &app.UpgradeKeeper)
635635
app.BankKeeper.RegisterRecipientChecker(app.EvmKeeper.CanAddressReceive)
636636

637637
bApp.SetPreCommitHandler(app.HandlePreCommit)
@@ -730,6 +730,7 @@ func New(
730730

731731
if enableCustomEVMPrecompiles {
732732
customPrecompiles := precompiles.GetCustomPrecompiles(
733+
LatestUpgrade,
733734
&app.EvmKeeper,
734735
app.BankKeeper,
735736
bankkeeper.NewMsgServerImpl(app.BankKeeper),
@@ -748,8 +749,7 @@ func New(
748749
app.ConfidentialTransfersKeeper,
749750
ctkeeper.NewMsgServerImpl(app.ConfidentialTransfersKeeper),
750751
)
751-
752-
app.EvmKeeper.SetCustomPrecompiles(customPrecompiles)
752+
app.EvmKeeper.SetCustomPrecompiles(customPrecompiles, LatestUpgrade)
753753
}
754754

755755
/**** Module Options ****/
@@ -1877,14 +1877,14 @@ func (app *App) RegisterTxService(clientCtx client.Context) {
18771877

18781878
func (app *App) RPCContextProvider(i int64) sdk.Context {
18791879
if i == evmrpc.LatestCtxHeight {
1880-
return app.GetCheckCtx()
1880+
return app.GetCheckCtx().WithIsTracing(true)
18811881
}
18821882
ctx, err := app.CreateQueryContext(i, false)
18831883
if err != nil {
18841884
app.Logger().Error(fmt.Sprintf("failed to create query context for EVM; using latest context instead: %v+", err.Error()))
1885-
return app.GetCheckCtx()
1885+
return app.GetCheckCtx().WithIsTracing(true)
18861886
}
1887-
return ctx.WithIsEVM(true)
1887+
return ctx.WithIsEVM(true).WithIsTracing(true)
18881888
}
18891889

18901890
// RegisterTendermintService implements the Application.RegisterTendermintService method.

app/upgrades.go

+2
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ var upgradesList = []string{
119119
"v6.0.5",
120120
}
121121

122+
var LatestUpgrade = upgradesList[len(upgradesList)-1]
123+
122124
// if there is an override list, use that instead, for integration tests
123125
func overrideList() {
124126
// if there is an override list, use that instead, for integration tests

evmrpc/simulate.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -381,12 +381,12 @@ func (b *Backend) StateAtBlock(ctx context.Context, block *ethtypes.Block, reexe
381381
return statedb, emptyRelease, nil
382382
}
383383

384-
func (b *Backend) GetEVM(_ context.Context, msg *core.Message, stateDB vm.StateDB, _ *ethtypes.Header, vmConfig *vm.Config, blockCtx *vm.BlockContext) *vm.EVM {
384+
func (b *Backend) GetEVM(_ context.Context, msg *core.Message, stateDB vm.StateDB, h *ethtypes.Header, vmConfig *vm.Config, blockCtx *vm.BlockContext) *vm.EVM {
385385
txContext := core.NewEVMTxContext(msg)
386386
if blockCtx == nil {
387387
blockCtx, _ = b.keeper.GetVMBlockContext(b.ctxProvider(LatestCtxHeight).WithIsEVM(true).WithEVMEntryViaWasmdPrecompile(wasmd.IsWasmdCall(msg.To)), core.GasPool(b.RPCGasCap()))
388388
}
389-
return vm.NewEVM(*blockCtx, txContext, stateDB, b.ChainConfig(), *vmConfig, b.keeper.CustomPrecompiles())
389+
return vm.NewEVM(*blockCtx, txContext, stateDB, b.ChainConfig(), *vmConfig, b.keeper.CustomPrecompiles(b.ctxProvider(h.Number.Int64())))
390390
}
391391

392392
func (b *Backend) CurrentHeader() *ethtypes.Header {
@@ -442,8 +442,8 @@ func (b *Backend) getHeader(blockNumber *big.Int) *ethtypes.Header {
442442
return header
443443
}
444444

445-
func (b *Backend) GetCustomPrecompiles(int64) map[common.Address]vm.PrecompiledContract {
446-
return b.keeper.CustomPrecompiles()
445+
func (b *Backend) GetCustomPrecompiles(h int64) map[common.Address]vm.PrecompiledContract {
446+
return b.keeper.CustomPrecompiles(b.ctxProvider(h))
447447
}
448448

449449
type Engine struct {

evmrpc/tests/mock_upgrade.go

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package tests
2+
3+
import (
4+
sdk "github.com/cosmos/cosmos-sdk/types"
5+
"github.com/sei-protocol/sei-chain/app"
6+
)
7+
8+
func mockUpgrade(version string, height int64) func(ctx sdk.Context, a *app.App) {
9+
return func(ctx sdk.Context, a *app.App) {
10+
a.UpgradeKeeper.SetDone(ctx.WithBlockHeight(height), version)
11+
}
12+
}

evmrpc/tests/tracers_test.go

+36
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package tests
22

33
import (
4+
"encoding/json"
45
"testing"
56

7+
"github.com/ethereum/go-ethereum/common/hexutil"
8+
ethtypes "github.com/ethereum/go-ethereum/core/types"
9+
"github.com/ethereum/go-ethereum/lib/ethapi"
610
"github.com/stretchr/testify/require"
711
)
812

@@ -19,3 +23,35 @@ func TestTraceBlockByNumber(t *testing.T) {
1923
},
2024
)
2125
}
26+
27+
func TestTraceHistoricalPrecompiles(t *testing.T) {
28+
from := getAddrWithMnemonic(mnemonic1)
29+
txData := jsonExtractAsBytesFromArray(0).(*ethtypes.DynamicFeeTx)
30+
SetupTestServer([][][]byte{{}, {}, {}}, mnemonicInitializer(mnemonic1), mockUpgrade("v5.5.2", 1), mockUpgrade("v6.0.5", 3)).Run(
31+
func(port int) {
32+
args := ethapi.TransactionArgs{
33+
From: &from,
34+
To: txData.To,
35+
Gas: (*hexutil.Uint64)(&txData.Gas),
36+
GasPrice: (*hexutil.Big)(txData.GasFeeCap),
37+
Nonce: (*hexutil.Uint64)(&txData.Nonce),
38+
Input: (*hexutil.Bytes)(&txData.Data),
39+
ChainID: (*hexutil.Big)(txData.ChainID),
40+
}
41+
bz, err := json.Marshal(args)
42+
require.Nil(t, err)
43+
// error when traced on a block prior to v6.0.5
44+
res := sendRequestWithNamespace("debug", port, "traceCall", bz, "0x2", map[string]interface{}{
45+
"timeout": "60s", "tracer": "flatCallTracer",
46+
})
47+
errMsg := res["result"].([]interface{})[0].(map[string]interface{})["error"].(string)
48+
require.Contains(t, errMsg, "no method with id")
49+
// no error when traced on a block post v6.0.5
50+
res = sendRequestWithNamespace("debug", port, "traceCall", bz, "0x3", map[string]interface{}{
51+
"timeout": "60s", "tracer": "flatCallTracer",
52+
})
53+
resultMap := res["result"].([]interface{})[0].(map[string]interface{})
54+
require.NotContains(t, resultMap, "error")
55+
},
56+
)
57+
}

evmrpc/tests/tx.go

+27
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package tests
22

33
import (
4+
"bytes"
45
"fmt"
56
"math/big"
7+
"os"
68
"strings"
79

810
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
@@ -11,6 +13,7 @@ import (
1113
"github.com/ethereum/go-ethereum/common"
1214
ethtypes "github.com/ethereum/go-ethereum/core/types"
1315
"github.com/sei-protocol/sei-chain/precompiles"
16+
"github.com/sei-protocol/sei-chain/precompiles/json"
1417
"github.com/sei-protocol/sei-chain/precompiles/pointer"
1518
testkeeper "github.com/sei-protocol/sei-chain/testutil/keeper"
1619
"github.com/sei-protocol/sei-chain/x/evm/artifacts/wsei"
@@ -80,3 +83,27 @@ func transferCW20Msg(mnemonic string, cw20Addr string) sdk.Msg {
8083
Msg: []byte(fmt.Sprintf("{\"transfer\":{\"recipient\":\"%s\",\"amount\":\"100\"}}", recipient.String())),
8184
}
8285
}
86+
87+
func jsonExtractAsBytesFromArray(nonce uint64) ethtypes.TxData {
88+
abiBz, err := os.ReadFile("../../precompiles/json/abi.json")
89+
if err != nil {
90+
panic(err)
91+
}
92+
newAbi, err := abi.JSON(bytes.NewReader(abiBz))
93+
if err != nil {
94+
panic(err)
95+
}
96+
input, err := newAbi.Pack("extractAsBytesFromArray", []byte("[\"1\"]"), uint16(0))
97+
if err != nil {
98+
panic(err)
99+
}
100+
to := common.HexToAddress(json.JSONAddress)
101+
return &ethtypes.DynamicFeeTx{
102+
Nonce: nonce,
103+
GasFeeCap: big.NewInt(1000000000),
104+
Gas: 50000,
105+
To: &to,
106+
Data: input,
107+
ChainID: chainId,
108+
}
109+
}

evmrpc/tests/utils.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ func formatParam(p interface{}) string {
176176
}
177177
return fmt.Sprintf("{%s}", strings.Join(kvs, ","))
178178
default:
179-
panic("did not match on type")
179+
return fmt.Sprintf("%s", p)
180180
}
181181
}
182182

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ replace (
357357
github.com/CosmWasm/wasmvm => github.com/sei-protocol/sei-wasmvm v1.5.4-sei.0.0.1
358358
github.com/coinbase/kryptology => github.com/sei-protocol/coinbase-kryptology v0.0.0-20241210171554-278d19024e41
359359
github.com/confio/ics23/go => github.com/cosmos/cosmos-sdk/ics23/go v0.8.0
360-
github.com/cosmos/cosmos-sdk => github.com/sei-protocol/sei-cosmos v0.3.56-0.20250313190228-9fb9a4fd8636
360+
github.com/cosmos/cosmos-sdk => github.com/sei-protocol/sei-cosmos v0.3.56
361361
github.com/cosmos/iavl => github.com/sei-protocol/sei-iavl v0.2.0
362362
github.com/cosmos/ibc-go/v3 => github.com/sei-protocol/sei-ibc-go/v3 v3.3.5
363363
github.com/ethereum/go-ethereum => github.com/sei-protocol/go-ethereum v1.13.5-sei-30

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -1358,8 +1358,8 @@ github.com/sei-protocol/go-ethereum v1.13.5-sei-30 h1:8aIlzdH1LpbVLTzXRJvO/Crmxh
13581358
github.com/sei-protocol/go-ethereum v1.13.5-sei-30/go.mod h1:kcRZmuzRn1lVejiFNTz4l4W7imnpq1bDAnuKS/RyhbQ=
13591359
github.com/sei-protocol/goutils v0.0.2 h1:Bfa7Sv+4CVLNM20QcpvGb81B8C5HkQC/kW1CQpIbXDA=
13601360
github.com/sei-protocol/goutils v0.0.2/go.mod h1:iYE2DuJfEnM+APPehr2gOUXfuLuPsVxorcDO+Tzq9q8=
1361-
github.com/sei-protocol/sei-cosmos v0.3.56-0.20250313190228-9fb9a4fd8636 h1:9RMstipSzuAgpwz8IcM+LvZB2frXSBolu8CRj660fI4=
1362-
github.com/sei-protocol/sei-cosmos v0.3.56-0.20250313190228-9fb9a4fd8636/go.mod h1:Z+0XynKuhMu9m2XHIvUBwk4A+iLPc4YnzFlabpMOWqw=
1361+
github.com/sei-protocol/sei-cosmos v0.3.56 h1:ADju29g0LtSnn5tS7OG09++DUKOlcPapC4TshrMnqnk=
1362+
github.com/sei-protocol/sei-cosmos v0.3.56/go.mod h1:Z+0XynKuhMu9m2XHIvUBwk4A+iLPc4YnzFlabpMOWqw=
13631363
github.com/sei-protocol/sei-cryptography v0.0.0-20241210192144-b20fa09be987 h1:l2EcEUO2rThRIvqIlnJQvqSOwutSbuY01xf1gbgx4qM=
13641364
github.com/sei-protocol/sei-cryptography v0.0.0-20241210192144-b20fa09be987/go.mod h1:274GmYGRLVKlf/XcOx4SI2kmq1tJQYzlqboK9RNA9Q8=
13651365
github.com/sei-protocol/sei-db v0.0.48 h1:BgSF5jq9hiPz0JMmbf5f34CnZijqTO+3zOo8IG62lFY=

precompiles/common/legacy/v575/expected_keepers.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,14 @@ type EVMKeeper interface {
4848
GetERC721CW721Pointer(ctx sdk.Context, cw721Address string) (addr common.Address, version uint16, exists bool)
4949
SetCode(ctx sdk.Context, addr common.Address, code []byte)
5050
UpsertERCNativePointer(
51-
ctx sdk.Context, evm *vm.EVM, suppliedGas uint64, token string, metadata utils.ERCMetadata,
52-
) (contractAddr common.Address, remainingGas uint64, err error)
51+
ctx sdk.Context, evm *vm.EVM, token string, metadata utils.ERCMetadata,
52+
) (contractAddr common.Address, err error)
5353
UpsertERCCW20Pointer(
54-
ctx sdk.Context, evm *vm.EVM, suppliedGas uint64, cw20Addr string, metadata utils.ERCMetadata,
55-
) (contractAddr common.Address, remainingGas uint64, err error)
54+
ctx sdk.Context, evm *vm.EVM, cw20Addr string, metadata utils.ERCMetadata,
55+
) (contractAddr common.Address, err error)
5656
UpsertERCCW721Pointer(
57-
ctx sdk.Context, evm *vm.EVM, suppliedGas uint64, cw721Addr string, metadata utils.ERCMetadata,
58-
) (contractAddr common.Address, remainingGas uint64, err error)
57+
ctx sdk.Context, evm *vm.EVM, cw721Addr string, metadata utils.ERCMetadata,
58+
) (contractAddr common.Address, err error)
5959
}
6060

6161
type AccountKeeper interface {

precompiles/pointer/legacy/v575/pointer.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,12 @@ func (p PrecompileExecutor) AddNative(ctx sdk.Context, method *ethabi.Method, ca
113113
}
114114
}
115115
}
116-
contractAddr, remainingGas, err := p.evmKeeper.UpsertERCNativePointer(ctx, evm, suppliedGas, token, utils.ERCMetadata{Name: name, Symbol: symbol, Decimals: decimals})
116+
contractAddr, err := p.evmKeeper.UpsertERCNativePointer(ctx, evm, token, utils.ERCMetadata{Name: name, Symbol: symbol, Decimals: decimals})
117117
if err != nil {
118118
return nil, 0, err
119119
}
120120
ret, err = method.Outputs.Pack(contractAddr)
121+
remainingGas = pcommon.GetRemainingGas(ctx, p.evmKeeper)
121122
return
122123
}
123124

@@ -143,11 +144,12 @@ func (p PrecompileExecutor) AddCW20(ctx sdk.Context, method *ethabi.Method, call
143144
}
144145
name := formattedRes["name"].(string)
145146
symbol := formattedRes["symbol"].(string)
146-
contractAddr, remainingGas, err := p.evmKeeper.UpsertERCCW20Pointer(ctx, evm, suppliedGas, cwAddr, utils.ERCMetadata{Name: name, Symbol: symbol})
147+
contractAddr, err := p.evmKeeper.UpsertERCCW20Pointer(ctx, evm, cwAddr, utils.ERCMetadata{Name: name, Symbol: symbol})
147148
if err != nil {
148149
return nil, 0, err
149150
}
150151
ret, err = method.Outputs.Pack(contractAddr)
152+
remainingGas = pcommon.GetRemainingGas(ctx, p.evmKeeper)
151153
return
152154
}
153155

@@ -173,10 +175,11 @@ func (p PrecompileExecutor) AddCW721(ctx sdk.Context, method *ethabi.Method, cal
173175
}
174176
name := formattedRes["name"].(string)
175177
symbol := formattedRes["symbol"].(string)
176-
contractAddr, remainingGas, err := p.evmKeeper.UpsertERCCW721Pointer(ctx, evm, suppliedGas, cwAddr, utils.ERCMetadata{Name: name, Symbol: symbol})
178+
contractAddr, err := p.evmKeeper.UpsertERCCW721Pointer(ctx, evm, cwAddr, utils.ERCMetadata{Name: name, Symbol: symbol})
177179
if err != nil {
178180
return nil, 0, err
179181
}
180182
ret, err = method.Outputs.Pack(contractAddr)
183+
remainingGas = pcommon.GetRemainingGas(ctx, p.evmKeeper)
181184
return
182185
}

precompiles/pointer/pointer_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func TestAddNative(t *testing.T) {
3434
require.Nil(t, err)
3535
statedb := state.NewDBImpl(ctx, &testApp.EvmKeeper, true)
3636
blockCtx, _ := testApp.EvmKeeper.GetVMBlockContext(ctx, core.GasPool(suppliedGas))
37-
evm := vm.NewEVM(*blockCtx, vm.TxContext{}, statedb, cfg, vm.Config{}, testApp.EvmKeeper.CustomPrecompiles())
37+
evm := vm.NewEVM(*blockCtx, vm.TxContext{}, statedb, cfg, vm.Config{}, testApp.EvmKeeper.CustomPrecompiles(ctx))
3838
_, g, err := p.RunAndCalculateGas(evm, caller, caller, append(p.GetExecutor().(*pointer.PrecompileExecutor).AddNativePointerID, args...), suppliedGas, nil, nil, false, false)
3939
require.NotNil(t, err)
4040
require.NotNil(t, statedb.GetPrecompileError())
@@ -54,7 +54,7 @@ func TestAddNative(t *testing.T) {
5454
}},
5555
})
5656
statedb = state.NewDBImpl(ctx, &testApp.EvmKeeper, false)
57-
evm = vm.NewEVM(*blockCtx, vm.TxContext{}, statedb, cfg, vm.Config{}, testApp.EvmKeeper.CustomPrecompiles())
57+
evm = vm.NewEVM(*blockCtx, vm.TxContext{}, statedb, cfg, vm.Config{}, testApp.EvmKeeper.CustomPrecompiles(ctx))
5858
ret, g, err := p.RunAndCalculateGas(evm, caller, caller, append(p.GetExecutor().(*pointer.PrecompileExecutor).AddNativePointerID, args...), suppliedGas, nil, nil, false, false)
5959
require.Nil(t, err)
6060
require.Equal(t, uint64(8889527), g)
@@ -83,7 +83,7 @@ func TestAddNative(t *testing.T) {
8383
testApp.EvmKeeper.DeleteERC20NativePointer(statedb.Ctx(), "test", version)
8484
testApp.EvmKeeper.SetERC20NativePointerWithVersion(statedb.Ctx(), "test", pointerAddr, version-1)
8585
statedb = state.NewDBImpl(statedb.Ctx(), &testApp.EvmKeeper, true)
86-
evm = vm.NewEVM(*blockCtx, vm.TxContext{}, statedb, cfg, vm.Config{}, testApp.EvmKeeper.CustomPrecompiles())
86+
evm = vm.NewEVM(*blockCtx, vm.TxContext{}, statedb, cfg, vm.Config{}, testApp.EvmKeeper.CustomPrecompiles(ctx))
8787
_, _, err = p.RunAndCalculateGas(evm, caller, caller, append(p.GetExecutor().(*pointer.PrecompileExecutor).AddNativePointerID, args...), suppliedGas, nil, nil, false, false)
8888
require.Nil(t, err)
8989
require.Nil(t, statedb.GetPrecompileError())

0 commit comments

Comments
 (0)