Skip to content

Commit 00ecaef

Browse files
maouehudpatil
authored andcommitted
Update that brings go-ethereum with live tracer support (#1375)
* Update that brings `go-ethereum` with live tracer support This only brings the dependency (will need to wait for a merge + tag of the branch before merging this PR) and the required changes to adapt `DBImpl` to support the new interface. Relates to: - sei-protocol/go-ethereum#15 - #1344 * Added missing instrumentation when state changes in `DBImpl`
1 parent 1c46e1d commit 00ecaef

14 files changed

+107
-49
lines changed

app/eth_replay.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"github.com/cosmos/cosmos-sdk/client"
1212
sdk "github.com/cosmos/cosmos-sdk/types"
13+
"github.com/ethereum/go-ethereum/core/tracing"
1314
ethtypes "github.com/ethereum/go-ethereum/core/types"
1415
"github.com/ethereum/go-ethereum/params"
1516
ethtests "github.com/ethereum/go-ethereum/tests"
@@ -81,7 +82,7 @@ func Replay(a *App) {
8182
for _, w := range b.Withdrawals() {
8283
amount := new(big.Int).SetUint64(w.Amount)
8384
amount = amount.Mul(amount, big.NewInt(params.GWei))
84-
s.AddBalance(w.Address, amount)
85+
s.AddBalance(w.Address, amount, tracing.BalanceIncreaseWithdrawal)
8586
}
8687
_, _ = s.Finalize()
8788
for _, tx := range b.Txs {

evmrpc/simulate.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ func (b *Backend) HeaderByNumber(ctx context.Context, bn rpc.BlockNumber) (*etht
263263
return b.getHeader(big.NewInt(height)), nil
264264
}
265265

266-
func (b *Backend) StateAtTransaction(ctx context.Context, block *ethtypes.Block, txIndex int, reexec uint64) (*core.Message, vm.BlockContext, vm.StateDB, tracers.StateReleaseFunc, error) {
266+
func (b *Backend) StateAtTransaction(ctx context.Context, block *ethtypes.Block, txIndex int, reexec uint64) (*ethtypes.Transaction, vm.BlockContext, vm.StateDB, tracers.StateReleaseFunc, error) {
267267
emptyRelease := func() {}
268268
// Short circuit if it's genesis block.
269269
if block.Number().Int64() == 0 {
@@ -289,7 +289,7 @@ func (b *Backend) StateAtTransaction(ctx context.Context, block *ethtypes.Block,
289289
blockContext.Time = block.Time()
290290

291291
if idx == txIndex {
292-
return msg, *blockContext, statedb, emptyRelease, nil
292+
return tx, *blockContext, statedb, emptyRelease, nil
293293
}
294294
// Not yet the searched for transaction, execute on top of the current state
295295
vmenv := vm.NewEVM(*blockContext, txContext, statedb, b.ChainConfig(), vm.Config{})

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ replace (
349349
github.com/cosmos/cosmos-sdk => github.com/sei-protocol/sei-cosmos v0.2.79-seiv2-hotfix
350350
github.com/cosmos/iavl => github.com/sei-protocol/sei-iavl v0.1.9
351351
github.com/cosmos/ibc-go/v3 => github.com/sei-protocol/sei-ibc-go/v3 v3.3.0
352-
github.com/ethereum/go-ethereum => github.com/sei-protocol/go-ethereum v1.13.5-sei-12
352+
github.com/ethereum/go-ethereum => github.com/sei-protocol/go-ethereum v1.13.5-sei-9.0.20240327165640-6ab0d196bac6
353353
github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.3-alpha.regen.1
354354
github.com/sei-protocol/sei-db => github.com/sei-protocol/sei-db v0.0.35
355355
// Latest goleveldb is broken, we have to stick to this version

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1343,8 +1343,8 @@ github.com/seccomp/libseccomp-golang v0.9.2-0.20220502022130-f33da4d89646/go.mod
13431343
github.com/securego/gosec/v2 v2.11.0 h1:+PDkpzR41OI2jrw1q6AdXZCbsNGNGT7pQjal0H0cArI=
13441344
github.com/securego/gosec/v2 v2.11.0/go.mod h1:SX8bptShuG8reGC0XS09+a4H2BoWSJi+fscA+Pulbpo=
13451345
github.com/segmentio/fasthash v1.0.3/go.mod h1:waKX8l2N8yckOgmSsXJi7x1ZfdKZ4x7KRMzBtS3oedY=
1346-
github.com/sei-protocol/go-ethereum v1.13.5-sei-12 h1:sWGD3sV9F3Ilgmn8le8lhpfAwSi6n57I921cYuNvLuo=
1347-
github.com/sei-protocol/go-ethereum v1.13.5-sei-12/go.mod h1:kcRZmuzRn1lVejiFNTz4l4W7imnpq1bDAnuKS/RyhbQ=
1346+
github.com/sei-protocol/go-ethereum v1.13.5-sei-9.0.20240327165640-6ab0d196bac6 h1:AU9VAvZsTGibuzzVmoX3taGOWjPJJct5ypXDg6vKKu0=
1347+
github.com/sei-protocol/go-ethereum v1.13.5-sei-9.0.20240327165640-6ab0d196bac6/go.mod h1:kcRZmuzRn1lVejiFNTz4l4W7imnpq1bDAnuKS/RyhbQ=
13481348
github.com/sei-protocol/goutils v0.0.2 h1:Bfa7Sv+4CVLNM20QcpvGb81B8C5HkQC/kW1CQpIbXDA=
13491349
github.com/sei-protocol/goutils v0.0.2/go.mod h1:iYE2DuJfEnM+APPehr2gOUXfuLuPsVxorcDO+Tzq9q8=
13501350
github.com/sei-protocol/sei-cosmos v0.2.79-seiv2-hotfix h1:Yx2W7/xrGk13rpZfs+bkjx229b9LCbcCpcwTrzbz26Y=

x/evm/module_test.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
sdk "github.com/cosmos/cosmos-sdk/types"
88
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
99
"github.com/ethereum/go-ethereum/common"
10+
"github.com/ethereum/go-ethereum/core/tracing"
1011
ethtypes "github.com/ethereum/go-ethereum/core/types"
1112
testkeeper "github.com/sei-protocol/sei-chain/testutil/keeper"
1213
"github.com/sei-protocol/sei-chain/x/evm"
@@ -28,19 +29,19 @@ func TestABCI(t *testing.T) {
2829
m.BeginBlock(ctx, abci.RequestBeginBlock{})
2930
// 1st tx
3031
s := state.NewDBImpl(ctx.WithTxIndex(1), k, false)
31-
s.SubBalance(evmAddr1, big.NewInt(10000000000000))
32-
s.AddBalance(evmAddr2, big.NewInt(8000000000000))
32+
s.SubBalance(evmAddr1, big.NewInt(10000000000000), tracing.BalanceChangeUnspecified)
33+
s.AddBalance(evmAddr2, big.NewInt(8000000000000), tracing.BalanceChangeUnspecified)
3334
feeCollectorAddr, err := k.GetFeeCollectorAddress(ctx)
3435
require.Nil(t, err)
35-
s.AddBalance(feeCollectorAddr, big.NewInt(2000000000000))
36+
s.AddBalance(feeCollectorAddr, big.NewInt(2000000000000), tracing.BalanceChangeUnspecified)
3637
surplus, err := s.Finalize()
3738
require.Nil(t, err)
3839
require.Equal(t, sdk.ZeroInt(), surplus)
3940
k.AppendToEvmTxDeferredInfo(ctx.WithTxIndex(1), ethtypes.Bloom{}, common.Hash{}, surplus)
4041
// 3rd tx
4142
s = state.NewDBImpl(ctx.WithTxIndex(3), k, false)
42-
s.SubBalance(evmAddr2, big.NewInt(5000000000000))
43-
s.AddBalance(evmAddr1, big.NewInt(5000000000000))
43+
s.SubBalance(evmAddr2, big.NewInt(5000000000000), tracing.BalanceChangeUnspecified)
44+
s.AddBalance(evmAddr1, big.NewInt(5000000000000), tracing.BalanceChangeUnspecified)
4445
surplus, err = s.Finalize()
4546
require.Nil(t, err)
4647
require.Equal(t, sdk.ZeroInt(), surplus)
@@ -54,8 +55,8 @@ func TestABCI(t *testing.T) {
5455
m.BeginBlock(ctx, abci.RequestBeginBlock{})
5556
// 2nd tx
5657
s = state.NewDBImpl(ctx.WithTxIndex(2), k, false)
57-
s.SubBalance(evmAddr2, big.NewInt(3000000000000))
58-
s.AddBalance(evmAddr1, big.NewInt(2000000000000))
58+
s.SubBalance(evmAddr2, big.NewInt(3000000000000), tracing.BalanceChangeUnspecified)
59+
s.AddBalance(evmAddr1, big.NewInt(2000000000000), tracing.BalanceChangeUnspecified)
5960
surplus, err = s.Finalize()
6061
require.Nil(t, err)
6162
require.Equal(t, sdk.NewInt(1000000000000), surplus)

x/evm/state/balance.go

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,17 @@ import (
55

66
sdk "github.com/cosmos/cosmos-sdk/types"
77
"github.com/ethereum/go-ethereum/common"
8+
"github.com/ethereum/go-ethereum/core/tracing"
89
"github.com/sei-protocol/sei-chain/x/evm/types"
910
)
1011

11-
func (s *DBImpl) SubBalance(evmAddr common.Address, amt *big.Int) {
12+
func (s *DBImpl) SubBalance(evmAddr common.Address, amt *big.Int, reason tracing.BalanceChangeReason) {
1213
s.k.PrepareReplayedAddr(s.ctx, evmAddr)
1314
if amt.Sign() == 0 {
1415
return
1516
}
1617
if amt.Sign() < 0 {
17-
s.AddBalance(evmAddr, new(big.Int).Neg(amt))
18+
s.AddBalance(evmAddr, new(big.Int).Neg(amt), reason)
1819
return
1920
}
2021

@@ -28,16 +29,25 @@ func (s *DBImpl) SubBalance(evmAddr common.Address, amt *big.Int) {
2829
if s.err != nil {
2930
return
3031
}
32+
33+
if s.logger != nil && s.logger.OnBalanceChange != nil {
34+
// We could modify AddWei instead so it returns us the old/new balance directly.
35+
newBalance := s.GetBalance(evmAddr)
36+
oldBalance := new(big.Int).Add(newBalance, amt)
37+
38+
s.logger.OnBalanceChange(evmAddr, oldBalance, newBalance, reason)
39+
}
40+
3141
s.tempStateCurrent.surplus = s.tempStateCurrent.surplus.Add(sdk.NewIntFromBigInt(amt))
3242
}
3343

34-
func (s *DBImpl) AddBalance(evmAddr common.Address, amt *big.Int) {
44+
func (s *DBImpl) AddBalance(evmAddr common.Address, amt *big.Int, reason tracing.BalanceChangeReason) {
3545
s.k.PrepareReplayedAddr(s.ctx, evmAddr)
3646
if amt.Sign() == 0 {
3747
return
3848
}
3949
if amt.Sign() < 0 {
40-
s.SubBalance(evmAddr, new(big.Int).Neg(amt))
50+
s.SubBalance(evmAddr, new(big.Int).Neg(amt), reason)
4151
return
4252
}
4353

@@ -51,6 +61,15 @@ func (s *DBImpl) AddBalance(evmAddr common.Address, amt *big.Int) {
5161
if s.err != nil {
5262
return
5363
}
64+
65+
if s.logger != nil && s.logger.OnBalanceChange != nil {
66+
// We could modify AddWei instead so it returns us the old/new balance directly.
67+
newBalance := s.GetBalance(evmAddr)
68+
oldBalance := new(big.Int).Sub(newBalance, amt)
69+
70+
s.logger.OnBalanceChange(evmAddr, oldBalance, newBalance, reason)
71+
}
72+
5473
s.tempStateCurrent.surplus = s.tempStateCurrent.surplus.Sub(sdk.NewIntFromBigInt(amt))
5574
}
5675

@@ -62,7 +81,7 @@ func (s *DBImpl) GetBalance(evmAddr common.Address) *big.Int {
6281
}
6382

6483
// should only be called during simulation
65-
func (s *DBImpl) SetBalance(evmAddr common.Address, amt *big.Int) {
84+
func (s *DBImpl) SetBalance(evmAddr common.Address, amt *big.Int, reason tracing.BalanceChangeReason) {
6685
if !s.simulation {
6786
panic("should never call SetBalance in a non-simulation setting")
6887
}

x/evm/state/balance_test.go

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"testing"
66

77
sdk "github.com/cosmos/cosmos-sdk/types"
8+
"github.com/ethereum/go-ethereum/core/tracing"
89
testkeeper "github.com/sei-protocol/sei-chain/testutil/keeper"
910
"github.com/sei-protocol/sei-chain/x/evm/state"
1011
"github.com/sei-protocol/sei-chain/x/evm/types"
@@ -16,17 +17,17 @@ func TestAddBalance(t *testing.T) {
1617
db := state.NewDBImpl(ctx, k, false)
1718
seiAddr, evmAddr := testkeeper.MockAddressPair()
1819
require.Equal(t, big.NewInt(0), db.GetBalance(evmAddr))
19-
db.AddBalance(evmAddr, big.NewInt(0))
20+
db.AddBalance(evmAddr, big.NewInt(0), tracing.BalanceChangeUnspecified)
2021

2122
// set association
2223
k.SetAddressMapping(db.Ctx(), seiAddr, evmAddr)
2324
require.Equal(t, big.NewInt(0), db.GetBalance(evmAddr))
24-
db.AddBalance(evmAddr, big.NewInt(10000000000000))
25+
db.AddBalance(evmAddr, big.NewInt(10000000000000), tracing.BalanceChangeUnspecified)
2526
require.Nil(t, db.Err())
2627
require.Equal(t, db.GetBalance(evmAddr), big.NewInt(10000000000000))
2728

2829
_, evmAddr2 := testkeeper.MockAddressPair()
29-
db.SubBalance(evmAddr2, big.NewInt(-5000000000000)) // should redirect to AddBalance
30+
db.SubBalance(evmAddr2, big.NewInt(-5000000000000), tracing.BalanceChangeUnspecified) // should redirect to AddBalance
3031
require.Nil(t, db.Err())
3132
require.Equal(t, db.GetBalance(evmAddr), big.NewInt(10000000000000))
3233
require.Equal(t, db.GetBalance(evmAddr2), big.NewInt(5000000000000))
@@ -37,42 +38,42 @@ func TestSubBalance(t *testing.T) {
3738
db := state.NewDBImpl(ctx, k, false)
3839
seiAddr, evmAddr := testkeeper.MockAddressPair()
3940
require.Equal(t, big.NewInt(0), db.GetBalance(evmAddr))
40-
db.SubBalance(evmAddr, big.NewInt(0))
41+
db.SubBalance(evmAddr, big.NewInt(0), tracing.BalanceChangeUnspecified)
4142

4243
// set association
4344
k.SetAddressMapping(db.Ctx(), seiAddr, evmAddr)
4445
require.Equal(t, big.NewInt(0), db.GetBalance(evmAddr))
4546
amt := sdk.NewCoins(sdk.NewCoin(k.GetBaseDenom(ctx), sdk.NewInt(20)))
4647
k.BankKeeper().MintCoins(db.Ctx(), types.ModuleName, amt)
4748
k.BankKeeper().SendCoinsFromModuleToAccount(db.Ctx(), types.ModuleName, seiAddr, amt)
48-
db.SubBalance(evmAddr, big.NewInt(10000000000000))
49+
db.SubBalance(evmAddr, big.NewInt(10000000000000), tracing.BalanceChangeUnspecified)
4950
require.Nil(t, db.Err())
5051
require.Equal(t, db.GetBalance(evmAddr), big.NewInt(10000000000000))
5152

5253
_, evmAddr2 := testkeeper.MockAddressPair()
5354
amt = sdk.NewCoins(sdk.NewCoin(k.GetBaseDenom(ctx), sdk.NewInt(10)))
5455
k.BankKeeper().MintCoins(db.Ctx(), types.ModuleName, amt)
5556
k.BankKeeper().SendCoinsFromModuleToAccount(db.Ctx(), types.ModuleName, sdk.AccAddress(evmAddr2[:]), amt)
56-
db.AddBalance(evmAddr2, big.NewInt(-5000000000000)) // should redirect to SubBalance
57+
db.AddBalance(evmAddr2, big.NewInt(-5000000000000), tracing.BalanceChangeUnspecified) // should redirect to SubBalance
5758
require.Nil(t, db.Err())
5859
require.Equal(t, db.GetBalance(evmAddr), big.NewInt(10000000000000))
5960
require.Equal(t, db.GetBalance(evmAddr2), big.NewInt(5000000000000))
6061

6162
// insufficient balance
62-
db.SubBalance(evmAddr2, big.NewInt(10000000000000))
63+
db.SubBalance(evmAddr2, big.NewInt(10000000000000), tracing.BalanceChangeUnspecified)
6364
require.NotNil(t, db.Err())
6465
}
6566

6667
func TestSetBalance(t *testing.T) {
6768
k, ctx := testkeeper.MockEVMKeeper()
6869
db := state.NewDBImpl(ctx, k, true)
6970
_, evmAddr := testkeeper.MockAddressPair()
70-
db.SetBalance(evmAddr, big.NewInt(10000000000000))
71+
db.SetBalance(evmAddr, big.NewInt(10000000000000), tracing.BalanceChangeUnspecified)
7172
require.Equal(t, big.NewInt(10000000000000), db.GetBalance(evmAddr))
7273

7374
seiAddr2, evmAddr2 := testkeeper.MockAddressPair()
7475
k.SetAddressMapping(db.Ctx(), seiAddr2, evmAddr2)
75-
db.SetBalance(evmAddr2, big.NewInt(10000000000000))
76+
db.SetBalance(evmAddr2, big.NewInt(10000000000000), tracing.BalanceChangeUnspecified)
7677
require.Equal(t, big.NewInt(10000000000000), db.GetBalance(evmAddr2))
7778
}
7879

@@ -82,55 +83,55 @@ func TestSurplus(t *testing.T) {
8283

8384
// test negative usei surplus negative wei surplus
8485
db := state.NewDBImpl(ctx, k, false)
85-
db.AddBalance(evmAddr, big.NewInt(1_000_000_000_001))
86+
db.AddBalance(evmAddr, big.NewInt(1_000_000_000_001), tracing.BalanceChangeUnspecified)
8687
_, err := db.Finalize()
8788
require.NotNil(t, err)
8889
require.Contains(t, err.Error(), "negative surplus value")
8990

9091
// test negative usei surplus positive wei surplus (negative total)
9192
db = state.NewDBImpl(ctx, k, false)
92-
db.AddBalance(evmAddr, big.NewInt(1_000_000_000_000))
93-
db.SubBalance(evmAddr, big.NewInt(1))
93+
db.AddBalance(evmAddr, big.NewInt(1_000_000_000_000), tracing.BalanceChangeUnspecified)
94+
db.SubBalance(evmAddr, big.NewInt(1), tracing.BalanceChangeUnspecified)
9495
_, err = db.Finalize()
9596
require.NotNil(t, err)
9697
require.Contains(t, err.Error(), "negative surplus value")
9798

9899
// test negative usei surplus positive wei surplus (positive total)
99100
db = state.NewDBImpl(ctx, k, false)
100-
db.AddBalance(evmAddr, big.NewInt(1_000_000_000_000))
101-
db.SubBalance(evmAddr, big.NewInt(2))
102-
db.SubBalance(evmAddr, big.NewInt(999_999_999_999))
101+
db.AddBalance(evmAddr, big.NewInt(1_000_000_000_000), tracing.BalanceChangeUnspecified)
102+
db.SubBalance(evmAddr, big.NewInt(2), tracing.BalanceChangeUnspecified)
103+
db.SubBalance(evmAddr, big.NewInt(999_999_999_999), tracing.BalanceChangeUnspecified)
103104
surplus, err := db.Finalize()
104105
require.Nil(t, err)
105106
require.Equal(t, sdk.OneInt(), surplus)
106107

107108
// test positive usei surplus negative wei surplus (negative total)
108109
db = state.NewDBImpl(ctx, k, false)
109-
db.SubBalance(evmAddr, big.NewInt(1_000_000_000_000))
110-
db.AddBalance(evmAddr, big.NewInt(2))
111-
db.AddBalance(evmAddr, big.NewInt(999_999_999_999))
110+
db.SubBalance(evmAddr, big.NewInt(1_000_000_000_000), tracing.BalanceChangeUnspecified)
111+
db.AddBalance(evmAddr, big.NewInt(2), tracing.BalanceChangeUnspecified)
112+
db.AddBalance(evmAddr, big.NewInt(999_999_999_999), tracing.BalanceChangeUnspecified)
112113
_, err = db.Finalize()
113114
require.NotNil(t, err)
114115
require.Contains(t, err.Error(), "negative surplus value")
115116

116117
// test positive usei surplus negative wei surplus (positive total)
117118
db = state.NewDBImpl(ctx, k, false)
118-
db.SubBalance(evmAddr, big.NewInt(1_000_000_000_000))
119-
db.AddBalance(evmAddr, big.NewInt(999_999_999_999))
119+
db.SubBalance(evmAddr, big.NewInt(1_000_000_000_000), tracing.BalanceChangeUnspecified)
120+
db.AddBalance(evmAddr, big.NewInt(999_999_999_999), tracing.BalanceChangeUnspecified)
120121
surplus, err = db.Finalize()
121122
require.Nil(t, err)
122123
require.Equal(t, sdk.OneInt(), surplus)
123124

124125
// test snapshots
125126
db = state.NewDBImpl(ctx, k, false)
126-
db.SubBalance(evmAddr, big.NewInt(1_000_000_000_000))
127-
db.AddBalance(evmAddr, big.NewInt(999_999_999_999))
127+
db.SubBalance(evmAddr, big.NewInt(1_000_000_000_000), tracing.BalanceChangeUnspecified)
128+
db.AddBalance(evmAddr, big.NewInt(999_999_999_999), tracing.BalanceChangeUnspecified)
128129
db.Snapshot()
129-
db.SubBalance(evmAddr, big.NewInt(1_000_000_000_000))
130-
db.AddBalance(evmAddr, big.NewInt(999_999_999_999))
130+
db.SubBalance(evmAddr, big.NewInt(1_000_000_000_000), tracing.BalanceChangeUnspecified)
131+
db.AddBalance(evmAddr, big.NewInt(999_999_999_999), tracing.BalanceChangeUnspecified)
131132
db.Snapshot()
132-
db.SubBalance(evmAddr, big.NewInt(1_000_000_000_000))
133-
db.AddBalance(evmAddr, big.NewInt(999_999_999_999))
133+
db.SubBalance(evmAddr, big.NewInt(1_000_000_000_000), tracing.BalanceChangeUnspecified)
134+
db.AddBalance(evmAddr, big.NewInt(999_999_999_999), tracing.BalanceChangeUnspecified)
134135
surplus, err = db.Finalize()
135136
require.Nil(t, err)
136137
require.Equal(t, sdk.NewInt(3), surplus)

x/evm/state/check_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"math/big"
55
"testing"
66

7+
"github.com/ethereum/go-ethereum/core/tracing"
78
testkeeper "github.com/sei-protocol/sei-chain/testutil/keeper"
89
"github.com/sei-protocol/sei-chain/x/evm/state"
910
"github.com/stretchr/testify/require"
@@ -35,11 +36,11 @@ func TestEmpty(t *testing.T) {
3536
require.True(t, statedb.Empty(addr))
3637

3738
// has balance
38-
statedb.AddBalance(addr, big.NewInt(1000000000000))
39+
statedb.AddBalance(addr, big.NewInt(1000000000000), tracing.BalanceChangeUnspecified)
3940
require.False(t, statedb.Empty(addr))
4041

4142
// has non-zero nonce
42-
statedb.SubBalance(addr, big.NewInt(1000000000000))
43+
statedb.SubBalance(addr, big.NewInt(1000000000000), tracing.BalanceChangeUnspecified)
4344
statedb.SetNonce(addr, 1)
4445
require.False(t, statedb.Empty(addr))
4546

x/evm/state/code.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package state
22

33
import (
44
"github.com/ethereum/go-ethereum/common"
5+
"github.com/ethereum/go-ethereum/crypto"
56
)
67

78
func (s *DBImpl) GetCodeHash(addr common.Address) common.Hash {
@@ -16,6 +17,15 @@ func (s *DBImpl) GetCode(addr common.Address) []byte {
1617

1718
func (s *DBImpl) SetCode(addr common.Address, code []byte) {
1819
s.k.PrepareReplayedAddr(s.ctx, addr)
20+
21+
if s.logger != nil && s.logger.OnCodeChange != nil {
22+
// The SetCode method could be modified to return the old code/hash directly.
23+
oldCode := s.GetCode(addr)
24+
oldHash := s.GetCodeHash(addr)
25+
26+
s.logger.OnCodeChange(addr, oldHash, oldCode, common.Hash(crypto.Keccak256(code)), code)
27+
}
28+
1929
s.k.SetCode(s.ctx, addr, code)
2030
}
2131

x/evm/state/log.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ type Logs struct {
1212
func (s *DBImpl) AddLog(l *ethtypes.Log) {
1313
l.Index = uint(len(s.GetAllLogs()))
1414
s.tempStateCurrent.logs = append(s.tempStateCurrent.logs, l)
15+
16+
if s.logger != nil && s.logger.OnLog != nil {
17+
s.logger.OnLog(l)
18+
}
1519
}
1620

1721
func (s *DBImpl) GetAllLogs() []*ethtypes.Log {

x/evm/state/nonce.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,11 @@ func (s *DBImpl) GetNonce(addr common.Address) uint64 {
1111

1212
func (s *DBImpl) SetNonce(addr common.Address, nonce uint64) {
1313
s.k.PrepareReplayedAddr(s.ctx, addr)
14+
15+
if s.logger != nil && s.logger.OnNonceChange != nil {
16+
// The SetCode method could be modified to return the old code/hash directly.
17+
s.logger.OnNonceChange(addr, s.GetNonce(addr), nonce)
18+
}
19+
1420
s.k.SetNonce(s.ctx, addr, nonce)
1521
}

0 commit comments

Comments
 (0)