Skip to content

Commit 54d7790

Browse files
committed
core/state, core/vm, eth/tracers: move state log mechanisms to separate layer
core/state: wip move state log mechanism in to a separate layer core/state: fix tests core/state: fix miscalc in OnBalanceChange eth/tracers: fix tests core/state: re-enable verkle witness generation internal/ethapi: fix simulation + new logging schema
1 parent 4c4219e commit 54d7790

File tree

21 files changed

+265
-107
lines changed

21 files changed

+265
-107
lines changed

consensus/beacon/consensus.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"github.com/ethereum/go-ethereum/core/state"
2929
"github.com/ethereum/go-ethereum/core/tracing"
3030
"github.com/ethereum/go-ethereum/core/types"
31+
"github.com/ethereum/go-ethereum/core/vm"
3132
"github.com/ethereum/go-ethereum/params"
3233
"github.com/ethereum/go-ethereum/rpc"
3334
"github.com/ethereum/go-ethereum/trie"
@@ -349,7 +350,7 @@ func (beacon *Beacon) Prepare(chain consensus.ChainHeaderReader, header *types.H
349350
}
350351

351352
// Finalize implements consensus.Engine and processes withdrawals on top.
352-
func (beacon *Beacon) Finalize(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, body *types.Body) {
353+
func (beacon *Beacon) Finalize(chain consensus.ChainHeaderReader, header *types.Header, state vm.StateDB, body *types.Body) {
353354
if !beacon.IsPoSHeader(header) {
354355
beacon.ethone.Finalize(chain, header, state, body)
355356
return

consensus/clique/clique.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import (
3636
"github.com/ethereum/go-ethereum/consensus/misc/eip1559"
3737
"github.com/ethereum/go-ethereum/core/state"
3838
"github.com/ethereum/go-ethereum/core/types"
39+
"github.com/ethereum/go-ethereum/core/vm"
3940
"github.com/ethereum/go-ethereum/crypto"
4041
"github.com/ethereum/go-ethereum/ethdb"
4142
"github.com/ethereum/go-ethereum/log"
@@ -580,7 +581,7 @@ func (c *Clique) Prepare(chain consensus.ChainHeaderReader, header *types.Header
580581

581582
// Finalize implements consensus.Engine. There is no post-transaction
582583
// consensus rules in clique, do nothing here.
583-
func (c *Clique) Finalize(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, body *types.Body) {
584+
func (c *Clique) Finalize(chain consensus.ChainHeaderReader, header *types.Header, state vm.StateDB, body *types.Body) {
584585
// No block rewards in PoA, so the state remains as is
585586
}
586587

consensus/consensus.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/ethereum/go-ethereum/common"
2424
"github.com/ethereum/go-ethereum/core/state"
2525
"github.com/ethereum/go-ethereum/core/types"
26+
"github.com/ethereum/go-ethereum/core/vm"
2627
"github.com/ethereum/go-ethereum/params"
2728
"github.com/ethereum/go-ethereum/rpc"
2829
)
@@ -88,7 +89,7 @@ type Engine interface {
8889
//
8990
// Note: The state database might be updated to reflect any consensus rules
9091
// that happen at finalization (e.g. block rewards).
91-
Finalize(chain ChainHeaderReader, header *types.Header, state *state.StateDB, body *types.Body)
92+
Finalize(chain ChainHeaderReader, header *types.Header, state vm.StateDB, body *types.Body)
9293

9394
// FinalizeAndAssemble runs any post-transaction state modifications (e.g. block
9495
// rewards or process withdrawals) and assembles the final block.

consensus/ethash/consensus.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"github.com/ethereum/go-ethereum/core/state"
3232
"github.com/ethereum/go-ethereum/core/tracing"
3333
"github.com/ethereum/go-ethereum/core/types"
34+
"github.com/ethereum/go-ethereum/core/vm"
3435
"github.com/ethereum/go-ethereum/params"
3536
"github.com/ethereum/go-ethereum/rlp"
3637
"github.com/ethereum/go-ethereum/trie"
@@ -502,7 +503,7 @@ func (ethash *Ethash) Prepare(chain consensus.ChainHeaderReader, header *types.H
502503
}
503504

504505
// Finalize implements consensus.Engine, accumulating the block and uncle rewards.
505-
func (ethash *Ethash) Finalize(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, body *types.Body) {
506+
func (ethash *Ethash) Finalize(chain consensus.ChainHeaderReader, header *types.Header, state vm.StateDB, body *types.Body) {
506507
// Accumulate any block and uncle rewards
507508
accumulateRewards(chain.Config(), state, header, body.Uncles)
508509
}
@@ -565,7 +566,7 @@ func (ethash *Ethash) SealHash(header *types.Header) (hash common.Hash) {
565566
// accumulateRewards credits the coinbase of the given block with the mining
566567
// reward. The total reward consists of the static block reward and rewards for
567568
// included uncles. The coinbase of each uncle block is also rewarded.
568-
func accumulateRewards(config *params.ChainConfig, stateDB *state.StateDB, header *types.Header, uncles []*types.Header) {
569+
func accumulateRewards(config *params.ChainConfig, stateDB vm.StateDB, header *types.Header, uncles []*types.Header) {
569570
// Select the correct block reward based on chain progression
570571
blockReward := FrontierBlockReward
571572
if config.IsByzantium(header.Number) {

consensus/misc/dao.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,10 @@ import (
2121
"errors"
2222
"math/big"
2323

24-
"github.com/ethereum/go-ethereum/core/state"
2524
"github.com/ethereum/go-ethereum/core/tracing"
2625
"github.com/ethereum/go-ethereum/core/types"
26+
"github.com/ethereum/go-ethereum/core/vm"
2727
"github.com/ethereum/go-ethereum/params"
28-
"github.com/holiman/uint256"
2928
)
3029

3130
var (
@@ -74,15 +73,16 @@ func VerifyDAOHeaderExtraData(config *params.ChainConfig, header *types.Header)
7473
// ApplyDAOHardFork modifies the state database according to the DAO hard-fork
7574
// rules, transferring all balances of a set of DAO accounts to a single refund
7675
// contract.
77-
func ApplyDAOHardFork(statedb *state.StateDB) {
76+
func ApplyDAOHardFork(statedb vm.StateDB) {
7877
// Retrieve the contract to refund balances into
7978
if !statedb.Exist(params.DAORefundContract) {
8079
statedb.CreateAccount(params.DAORefundContract)
8180
}
8281

8382
// Move every DAO account and extra-balance account funds into the refund contract
8483
for _, addr := range params.DAODrainList() {
85-
statedb.AddBalance(params.DAORefundContract, statedb.GetBalance(addr), tracing.BalanceIncreaseDaoContract)
86-
statedb.SetBalance(addr, new(uint256.Int), tracing.BalanceDecreaseDaoAccount)
84+
balance := statedb.GetBalance(addr)
85+
statedb.AddBalance(params.DAORefundContract, balance, tracing.BalanceIncreaseDaoContract)
86+
statedb.SubBalance(addr, balance, tracing.BalanceDecreaseDaoAccount)
8787
}
8888
}

core/blockchain.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1895,10 +1895,13 @@ func (bc *BlockChain) processBlock(block *types.Block, statedb *state.StateDB, s
18951895
bc.logger.OnBlockEnd(blockEndErr)
18961896
}()
18971897
}
1898-
1898+
var wStateDb = vm.StateDB(statedb)
1899+
if w := statedb.Wrapped(); w != nil {
1900+
wStateDb = w
1901+
}
18991902
// Process block using the parent state as reference point
19001903
pstart := time.Now()
1901-
res, err := bc.processor.Process(block, statedb, bc.vmConfig)
1904+
res, err := bc.processor.Process(block, wStateDb, bc.vmConfig)
19021905
if err != nil {
19031906
bc.reportBlock(block, res, err)
19041907
return nil, err

core/state/state_object.go

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"time"
2424

2525
"github.com/ethereum/go-ethereum/common"
26-
"github.com/ethereum/go-ethereum/core/tracing"
2726
"github.com/ethereum/go-ethereum/core/types"
2827
"github.com/ethereum/go-ethereum/crypto"
2928
"github.com/ethereum/go-ethereum/log"
@@ -208,19 +207,18 @@ func (s *stateObject) GetCommittedState(key common.Hash) common.Hash {
208207
}
209208

210209
// SetState updates a value in account storage.
211-
func (s *stateObject) SetState(key, value common.Hash) {
210+
// It returns the previous value
211+
func (s *stateObject) SetState(key, value common.Hash) common.Hash {
212212
// If the new value is the same as old, don't set. Otherwise, track only the
213213
// dirty changes, supporting reverting all of it back to no change.
214214
prev, origin := s.getState(key)
215215
if prev == value {
216-
return
216+
return prev
217217
}
218218
// New value is different, update and journal the change
219219
s.db.journal.storageChange(s.address, key, prev, origin)
220220
s.setState(key, value, origin)
221-
if s.db.logger != nil && s.db.logger.OnStorageChange != nil {
222-
s.db.logger.OnStorageChange(s.address, key, prev, value)
223-
}
221+
return prev
224222
}
225223

226224
// setState updates a value in account dirty storage. The dirtiness will be
@@ -448,33 +446,25 @@ func (s *stateObject) commit() (*accountUpdate, *trienode.NodeSet, error) {
448446

449447
// AddBalance adds amount to s's balance.
450448
// It is used to add funds to the destination account of a transfer.
451-
func (s *stateObject) AddBalance(amount *uint256.Int, reason tracing.BalanceChangeReason) {
449+
// returns the previous balance
450+
func (s *stateObject) AddBalance(amount *uint256.Int) uint256.Int {
452451
// EIP161: We must check emptiness for the objects such that the account
453452
// clearing (0,0,0 objects) can take effect.
454453
if amount.IsZero() {
455454
if s.empty() {
456455
s.touch()
457456
}
458-
return
457+
return *(s.Balance())
459458
}
460-
s.SetBalance(new(uint256.Int).Add(s.Balance(), amount), reason)
459+
return s.SetBalance(new(uint256.Int).Add(s.Balance(), amount))
461460
}
462461

463-
// SubBalance removes amount from s's balance.
464-
// It is used to remove funds from the origin account of a transfer.
465-
func (s *stateObject) SubBalance(amount *uint256.Int, reason tracing.BalanceChangeReason) {
466-
if amount.IsZero() {
467-
return
468-
}
469-
s.SetBalance(new(uint256.Int).Sub(s.Balance(), amount), reason)
470-
}
471-
472-
func (s *stateObject) SetBalance(amount *uint256.Int, reason tracing.BalanceChangeReason) {
462+
// SetBalance sets the balance for the object, and returns the prevous balance
463+
func (s *stateObject) SetBalance(amount *uint256.Int) uint256.Int {
464+
prev := *s.data.Balance
473465
s.db.journal.balanceChange(s.address, s.data.Balance)
474-
if s.db.logger != nil && s.db.logger.OnBalanceChange != nil {
475-
s.db.logger.OnBalanceChange(s.address, s.Balance().ToBig(), amount.ToBig(), reason)
476-
}
477466
s.setBalance(amount)
467+
return prev
478468
}
479469

480470
func (s *stateObject) setBalance(amount *uint256.Int) {
@@ -547,10 +537,6 @@ func (s *stateObject) CodeSize() int {
547537

548538
func (s *stateObject) SetCode(codeHash common.Hash, code []byte) {
549539
s.db.journal.setCode(s.address)
550-
if s.db.logger != nil && s.db.logger.OnCodeChange != nil {
551-
// TODO remove prevcode from this callback
552-
s.db.logger.OnCodeChange(s.address, common.BytesToHash(s.CodeHash()), nil, codeHash, code)
553-
}
554540
s.setCode(codeHash, code)
555541
}
556542

@@ -562,9 +548,6 @@ func (s *stateObject) setCode(codeHash common.Hash, code []byte) {
562548

563549
func (s *stateObject) SetNonce(nonce uint64) {
564550
s.db.journal.nonceChange(s.address, s.data.Nonce)
565-
if s.db.logger != nil && s.db.logger.OnNonceChange != nil {
566-
s.db.logger.OnNonceChange(s.address, s.data.Nonce, nonce)
567-
}
568551
s.setNonce(nonce)
569552
}
570553

core/state/state_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323

2424
"github.com/ethereum/go-ethereum/common"
2525
"github.com/ethereum/go-ethereum/core/rawdb"
26-
"github.com/ethereum/go-ethereum/core/tracing"
2726
"github.com/ethereum/go-ethereum/core/types"
2827
"github.com/ethereum/go-ethereum/crypto"
2928
"github.com/ethereum/go-ethereum/triedb"
@@ -48,11 +47,11 @@ func TestDump(t *testing.T) {
4847

4948
// generate a few entries
5049
obj1 := s.state.getOrNewStateObject(common.BytesToAddress([]byte{0x01}))
51-
obj1.AddBalance(uint256.NewInt(22), tracing.BalanceChangeUnspecified)
50+
obj1.AddBalance(uint256.NewInt(22))
5251
obj2 := s.state.getOrNewStateObject(common.BytesToAddress([]byte{0x01, 0x02}))
5352
obj2.SetCode(crypto.Keccak256Hash([]byte{3, 3, 3, 3, 3, 3, 3}), []byte{3, 3, 3, 3, 3, 3, 3})
5453
obj3 := s.state.getOrNewStateObject(common.BytesToAddress([]byte{0x02}))
55-
obj3.SetBalance(uint256.NewInt(44), tracing.BalanceChangeUnspecified)
54+
obj3.SetBalance(uint256.NewInt(44))
5655

5756
// write some of them to the trie
5857
s.state.updateStateObject(obj1)
@@ -106,13 +105,13 @@ func TestIterativeDump(t *testing.T) {
106105

107106
// generate a few entries
108107
obj1 := s.state.getOrNewStateObject(common.BytesToAddress([]byte{0x01}))
109-
obj1.AddBalance(uint256.NewInt(22), tracing.BalanceChangeUnspecified)
108+
obj1.AddBalance(uint256.NewInt(22))
110109
obj2 := s.state.getOrNewStateObject(common.BytesToAddress([]byte{0x01, 0x02}))
111110
obj2.SetCode(crypto.Keccak256Hash([]byte{3, 3, 3, 3, 3, 3, 3}), []byte{3, 3, 3, 3, 3, 3, 3})
112111
obj3 := s.state.getOrNewStateObject(common.BytesToAddress([]byte{0x02}))
113-
obj3.SetBalance(uint256.NewInt(44), tracing.BalanceChangeUnspecified)
112+
obj3.SetBalance(uint256.NewInt(44))
114113
obj4 := s.state.getOrNewStateObject(common.BytesToAddress([]byte{0x00}))
115-
obj4.AddBalance(uint256.NewInt(1337), tracing.BalanceChangeUnspecified)
114+
obj4.AddBalance(uint256.NewInt(1337))
116115

117116
// write some of them to the trie
118117
s.state.updateStateObject(obj1)
@@ -200,7 +199,7 @@ func TestCreateObjectRevert(t *testing.T) {
200199

201200
state.CreateAccount(addr)
202201
so0 := state.getStateObject(addr)
203-
so0.SetBalance(uint256.NewInt(42), tracing.BalanceChangeUnspecified)
202+
so0.SetBalance(uint256.NewInt(42))
204203
so0.SetNonce(43)
205204
so0.SetCode(crypto.Keccak256Hash([]byte{'c', 'a', 'f', 'e'}), []byte{'c', 'a', 'f', 'e'})
206205
state.setStateObject(so0)

0 commit comments

Comments
 (0)