Skip to content

Commit 679427a

Browse files
committed
feat(core): state journal wrapper ethereum#30441 ethereum#33978
add some more changes for live tracing API: - Hook OnSystemCallStartV2 was introduced with VMContext as parameter. - Hook OnBlockHashRead was introduced. - GetCodeHash was added to the state interface - The new WrapWithJournal construction helps with tracking EVM reverts in the tracer.
1 parent f5fe86c commit 679427a

23 files changed

Lines changed: 766 additions & 57 deletions

core/genesis.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ func hashAlloc(ga *types.GenesisAlloc) (common.Hash, error) {
113113
statedb.AddBalance(addr, account.Balance, tracing.BalanceIncreaseGenesisBalance)
114114
}
115115
statedb.SetCode(addr, account.Code)
116-
statedb.SetNonce(addr, account.Nonce)
116+
statedb.SetNonce(addr, account.Nonce, tracing.NonceChangeGenesis)
117117
for key, value := range account.Storage {
118118
statedb.SetState(addr, key, value)
119119
}
@@ -134,7 +134,7 @@ func flushAlloc(ga *types.GenesisAlloc, db ethdb.Database, blockhash common.Hash
134134
statedb.AddBalance(addr, account.Balance, tracing.BalanceIncreaseGenesisBalance)
135135
}
136136
statedb.SetCode(addr, account.Code)
137-
statedb.SetNonce(addr, account.Nonce)
137+
statedb.SetNonce(addr, account.Nonce, tracing.NonceChangeGenesis)
138138
for key, value := range account.Storage {
139139
statedb.SetState(addr, key, value)
140140
}

core/state/statedb.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ func (s *StateDB) SetBalance(addr common.Address, amount *big.Int, _ tracing.Bal
450450
}
451451
}
452452

453-
func (s *StateDB) SetNonce(addr common.Address, nonce uint64) {
453+
func (s *StateDB) SetNonce(addr common.Address, nonce uint64, _ tracing.NonceChangeReason) {
454454
stateObject := s.GetOrNewStateObject(addr)
455455
if stateObject != nil {
456456
stateObject.SetNonce(nonce)

core/state/statedb_fuzz_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func newStateTestAction(addr common.Address, r *rand.Rand, index int) testAction
6767
{
6868
name: "SetNonce",
6969
fn: func(a testAction, s *StateDB) {
70-
s.SetNonce(addr, uint64(a.args[0]))
70+
s.SetNonce(addr, uint64(a.args[0]), tracing.NonceChangeUnspecified)
7171
},
7272
args: make([]int64, 1),
7373
},

core/state/statedb_hooked.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,12 @@ func (s *hookedStateDB) AddBalance(addr common.Address, amount *big.Int, reason
166166
return prev
167167
}
168168

169-
func (s *hookedStateDB) SetNonce(address common.Address, nonce uint64) {
169+
func (s *hookedStateDB) SetNonce(address common.Address, nonce uint64, reason tracing.NonceChangeReason) {
170170
prev := s.inner.GetNonce(address)
171-
s.inner.SetNonce(address, nonce)
172-
if s.hooks.OnNonceChange != nil {
171+
s.inner.SetNonce(address, nonce, reason)
172+
if s.hooks.OnNonceChangeV2 != nil {
173+
s.hooks.OnNonceChangeV2(address, prev, nonce, reason)
174+
} else if s.hooks.OnNonceChange != nil {
173175
s.hooks.OnNonceChange(address, prev, nonce)
174176
}
175177
}

core/state/statedb_hooked_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func TestHooks(t *testing.T) {
112112
})
113113
sdb.AddBalance(common.Address{0xaa}, big.NewInt(100), tracing.BalanceChangeUnspecified)
114114
sdb.SubBalance(common.Address{0xaa}, big.NewInt(50), tracing.BalanceChangeTransfer)
115-
sdb.SetNonce(common.Address{0xaa}, 1337)
115+
sdb.SetNonce(common.Address{0xaa}, 1337, tracing.NonceChangeGenesis)
116116
sdb.SetCode(common.Address{0xaa}, []byte{0x13, 37})
117117
sdb.SetState(common.Address{0xaa}, common.HexToHash("0x01"), common.HexToHash("0x11"))
118118
sdb.SetState(common.Address{0xaa}, common.HexToHash("0x01"), common.HexToHash("0x22"))

core/state/statedb_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func TestUpdateLeaks(t *testing.T) {
4646
for i := byte(0); i < 255; i++ {
4747
addr := common.BytesToAddress([]byte{i})
4848
state.AddBalance(addr, big.NewInt(int64(11*i)), tracing.BalanceChangeUnspecified)
49-
state.SetNonce(addr, uint64(42*i))
49+
state.SetNonce(addr, uint64(42*i), tracing.NonceChangeUnspecified)
5050
if i%2 == 0 {
5151
state.SetState(addr, common.BytesToHash([]byte{i, i, i}), common.BytesToHash([]byte{i, i, i, i}))
5252
}
@@ -79,7 +79,7 @@ func TestIntermediateLeaks(t *testing.T) {
7979

8080
modify := func(state *StateDB, addr common.Address, i, tweak byte) {
8181
state.SetBalance(addr, big.NewInt(int64(11*i)+int64(tweak)), tracing.BalanceChangeUnspecified)
82-
state.SetNonce(addr, uint64(42*i+tweak))
82+
state.SetNonce(addr, uint64(42*i+tweak), tracing.NonceChangeUnspecified)
8383
if i%2 == 0 {
8484
state.SetState(addr, common.Hash{i, i, i, 0}, common.Hash{})
8585
state.SetState(addr, common.Hash{i, i, i, tweak}, common.Hash{i, i, i, i, tweak})
@@ -336,7 +336,7 @@ func newTestAction(addr common.Address, r *rand.Rand) testAction {
336336
{
337337
name: "SetNonce",
338338
fn: func(a testAction, s *StateDB) {
339-
s.SetNonce(addr, uint64(a.args[0]))
339+
s.SetNonce(addr, uint64(a.args[0]), tracing.NonceChangeUnspecified)
340340
},
341341
args: make([]int64, 1),
342342
},

core/state/statedb_utils.go

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

66
"github.com/XinFinOrg/XDPoSChain/common"
7+
"github.com/XinFinOrg/XDPoSChain/core/tracing"
78
"github.com/XinFinOrg/XDPoSChain/core/types"
89
"github.com/XinFinOrg/XDPoSChain/crypto"
910
)
@@ -154,7 +155,7 @@ func (s *StateDB) GetVoterCap(candidate, voter common.Address) *big.Int {
154155

155156
func (s *StateDB) IncrementMintedRecordNonce() {
156157
nonce := s.GetNonce(common.MintedRecordAddressBinary)
157-
s.SetNonce(common.MintedRecordAddressBinary, nonce+1)
158+
s.SetNonce(common.MintedRecordAddressBinary, nonce+1, tracing.NonceChangeUnspecified)
158159
}
159160

160161
var (

core/state_processor.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ func ApplySignTransaction(msg *Message, config *params.ChainConfig, statedb *sta
570570
return nil, 0, false, ErrNonceTooLow
571571
}
572572
// Only increment the nonce for real transactions.
573-
statedb.SetNonce(from, nonce+1)
573+
statedb.SetNonce(from, nonce+1, tracing.NonceChangeEoACall)
574574
}
575575
// Create a new receipt for the transaction, storing the intermediate root and gas used by the tx
576576
// based on the eip phase, we're passing whether the root touch-delete accounts.
@@ -675,7 +675,7 @@ func ProcessParentBlockHash(prevHash common.Hash, evm *vm.EVM) {
675675
evm.StateDB.CreateAccount(params.HistoryStorageAddress)
676676
}
677677
if evm.StateDB.GetNonce(params.HistoryStorageAddress) == 0 {
678-
evm.StateDB.SetNonce(params.HistoryStorageAddress, 1)
678+
evm.StateDB.SetNonce(params.HistoryStorageAddress, 1, tracing.NonceChangeUnspecified)
679679
}
680680
evm.StateDB.SetCode(params.HistoryStorageAddress, params.HistoryStorageCode)
681681

core/state_processor_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,7 @@ func TestProcessParentBlockHash(t *testing.T) {
608608
coinbase = common.Address{}
609609
)
610610
test := func(statedb *state.StateDB) {
611-
statedb.SetNonce(params.HistoryStorageAddress, 1)
611+
statedb.SetNonce(params.HistoryStorageAddress, 1, tracing.NonceChangeUnspecified)
612612
statedb.SetCode(params.HistoryStorageAddress, params.HistoryStorageCode)
613613
statedb.IntermediateRoot(true)
614614

core/state_transition.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ func (st *StateTransition) TransitionDb(owner common.Address) (*ExecutionResult,
459459
ret, _, st.gasRemaining, vmerr = st.evm.Create(msg.From, msg.Data, st.gasRemaining, value)
460460
} else {
461461
// Increment the nonce for the next transaction
462-
st.state.SetNonce(msg.From, st.state.GetNonce(msg.From)+1)
462+
st.state.SetNonce(msg.From, st.state.GetNonce(msg.From)+1, tracing.NonceChangeEoACall)
463463

464464
// Apply EIP-7702 authorizations.
465465
if msg.SetCodeAuthorizations != nil {
@@ -567,7 +567,7 @@ func (st *StateTransition) applyAuthorization(msg *Message, auth *types.SetCodeA
567567
}
568568

569569
// Update nonce and account code.
570-
st.state.SetNonce(authority, auth.Nonce+1)
570+
st.state.SetNonce(authority, auth.Nonce+1, tracing.NonceChangeAuthorization)
571571
if auth.Address == (common.Address{}) {
572572
// Delegation to zero address means clear.
573573
st.state.SetCode(authority, nil)

0 commit comments

Comments
 (0)