Skip to content

Commit ec06411

Browse files
committed
core/state, core/vm: make vm.statedb interface larger
1 parent 83dda6f commit ec06411

File tree

9 files changed

+66
-34
lines changed

9 files changed

+66
-34
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) {

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/statedb_logger.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,3 +223,21 @@ func (s *stateDBLogger) Witness() *stateless.Witness {
223223
func (s *stateDBLogger) Finalise(deleteEmptyObjects bool) {
224224
s.inner.Finalise(deleteEmptyObjects)
225225
}
226+
227+
// GetLogs returns the logs matching the specified transaction hash, and annotates
228+
// them with the given blockNumber and blockHash.
229+
func (s *stateDBLogger) GetLogs(txHash common.Hash, blockNumber uint64, blockHash common.Hash) []*types.Log {
230+
return s.inner.GetLogs(txHash, blockNumber, blockHash)
231+
}
232+
233+
// TxIndex returns the current transaction index set by SetTxContext.
234+
func (s *stateDBLogger) TxIndex() int {
235+
return s.inner.TxIndex()
236+
}
237+
func (s *stateDBLogger) IntermediateRoot(deleteEmptyObjects bool) common.Hash {
238+
return s.inner.IntermediateRoot(deleteEmptyObjects)
239+
}
240+
241+
func (s stateDBLogger) SetTxContext(txHash common.Hash, txIndex int) {
242+
s.inner.SetTxContext(txHash, txIndex)
243+
}

core/state_processor.go

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222

2323
"github.com/ethereum/go-ethereum/common"
2424
"github.com/ethereum/go-ethereum/consensus/misc"
25-
"github.com/ethereum/go-ethereum/core/state"
2625
"github.com/ethereum/go-ethereum/core/types"
2726
"github.com/ethereum/go-ethereum/core/vm"
2827
"github.com/ethereum/go-ethereum/crypto"
@@ -53,7 +52,7 @@ func NewStateProcessor(config *params.ChainConfig, chain *HeaderChain) *StatePro
5352
// Process returns the receipts and logs accumulated during the process and
5453
// returns the amount of gas that was used in the process. If any of the
5554
// transactions failed to execute due to insufficient gas it will return an error.
56-
func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg vm.Config) (*ProcessResult, error) {
55+
func (p *StateProcessor) Process(block *types.Block, statedb vm.StateDB, cfg vm.Config) (*ProcessResult, error) {
5756
var (
5857
receipts types.Receipts
5958
usedGas = new(uint64)
@@ -64,15 +63,9 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg
6463
gp = new(GasPool).AddGas(block.GasLimit())
6564
)
6665

67-
var usedStateDb vm.StateDB
68-
if w := statedb.Wrapped(); w != nil {
69-
usedStateDb = w
70-
} else {
71-
usedStateDb = statedb
72-
}
7366
// Mutate the block and state according to any hard-fork specs
7467
if p.config.DAOForkSupport && p.config.DAOForkBlock != nil && p.config.DAOForkBlock.Cmp(block.Number()) == 0 {
75-
misc.ApplyDAOHardFork(usedStateDb)
68+
misc.ApplyDAOHardFork(statedb)
7669
}
7770
var (
7871
context vm.BlockContext
@@ -82,12 +75,12 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg
8275
// Apply pre-execution system calls.
8376
context = NewEVMBlockContext(header, p.chain, nil)
8477

85-
vmenv := vm.NewEVM(context, vm.TxContext{}, usedStateDb, p.config, cfg)
78+
vmenv := vm.NewEVM(context, vm.TxContext{}, statedb, p.config, cfg)
8679
if beaconRoot := block.BeaconRoot(); beaconRoot != nil {
87-
ProcessBeaconBlockRoot(*beaconRoot, vmenv, usedStateDb)
80+
ProcessBeaconBlockRoot(*beaconRoot, vmenv, statedb)
8881
}
8982
if p.config.IsPrague(block.Number(), block.Time()) {
90-
ProcessParentBlockHash(block.ParentHash(), vmenv, usedStateDb)
83+
ProcessParentBlockHash(block.ParentHash(), vmenv, statedb)
9184
}
9285

9386
// Iterate over and process the individual transactions
@@ -130,7 +123,7 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg
130123
// ApplyTransactionWithEVM attempts to apply a transaction to the given state database
131124
// and uses the input parameters for its environment similar to ApplyTransaction. However,
132125
// this method takes an already created EVM instance as input.
133-
func ApplyTransactionWithEVM(msg *Message, config *params.ChainConfig, gp *GasPool, statedb *state.StateDB, blockNumber *big.Int, blockHash common.Hash, tx *types.Transaction, usedGas *uint64, evm *vm.EVM) (receipt *types.Receipt, err error) {
126+
func ApplyTransactionWithEVM(msg *Message, config *params.ChainConfig, gp *GasPool, statedb vm.StateDB, blockNumber *big.Int, blockHash common.Hash, tx *types.Transaction, usedGas *uint64, evm *vm.EVM) (receipt *types.Receipt, err error) {
134127
if evm.Config.Tracer != nil && evm.Config.Tracer.OnTxStart != nil {
135128
evm.Config.Tracer.OnTxStart(evm.GetVMContext(), tx, msg.From)
136129
if evm.Config.Tracer.OnTxEnd != nil {
@@ -139,16 +132,10 @@ func ApplyTransactionWithEVM(msg *Message, config *params.ChainConfig, gp *GasPo
139132
}()
140133
}
141134
}
142-
var usedStateDb vm.StateDB
143-
if w := statedb.Wrapped(); w != nil {
144-
usedStateDb = w
145-
} else {
146-
usedStateDb = statedb
147-
}
148135

149136
// Create a new context to be used in the EVM environment.
150137
txContext := NewEVMTxContext(msg)
151-
evm.Reset(txContext, usedStateDb)
138+
evm.Reset(txContext, statedb)
152139

153140
// Apply the transaction to the current state (included in the env).
154141
result, err := ApplyMessage(evm, msg, gp)
@@ -159,7 +146,7 @@ func ApplyTransactionWithEVM(msg *Message, config *params.ChainConfig, gp *GasPo
159146
// Update the state with pending changes.
160147
var root []byte
161148
if config.IsByzantium(blockNumber) {
162-
usedStateDb.Finalise(true)
149+
statedb.Finalise(true)
163150
} else {
164151
root = statedb.IntermediateRoot(config.IsEIP158(blockNumber)).Bytes()
165152
}
@@ -169,7 +156,7 @@ func ApplyTransactionWithEVM(msg *Message, config *params.ChainConfig, gp *GasPo
169156
}
170157

171158
// MakeReceipt generates the receipt object for a transaction given its execution result.
172-
func MakeReceipt(evm *vm.EVM, result *ExecutionResult, statedb *state.StateDB, blockNumber *big.Int, blockHash common.Hash, tx *types.Transaction, usedGas uint64, root []byte) *types.Receipt {
159+
func MakeReceipt(evm *vm.EVM, result *ExecutionResult, statedb vm.StateDB, blockNumber *big.Int, blockHash common.Hash, tx *types.Transaction, usedGas uint64, root []byte) *types.Receipt {
173160
// Create a new receipt for the transaction, storing the intermediate root and gas used
174161
// by the tx.
175162
receipt := &types.Receipt{Type: tx.Type(), PostState: root, CumulativeGasUsed: usedGas}
@@ -193,9 +180,12 @@ func MakeReceipt(evm *vm.EVM, result *ExecutionResult, statedb *state.StateDB, b
193180

194181
// Merge the tx-local access event into the "block-local" one, in order to collect
195182
// all values, so that the witness can be built.
196-
if statedb.GetTrie().IsVerkle() {
197-
statedb.AccessEvents().Merge(evm.AccessEvents)
198-
}
183+
//
184+
// TODO (@holiman): Add this back (but not necessarily _here_)
185+
//
186+
//if statedb.GetTrie().IsVerkle() {
187+
// statedb.AccessEvents().Merge(evm.AccessEvents)
188+
//}
199189

200190
// Set the receipt logs and create the bloom filter.
201191
receipt.Logs = statedb.GetLogs(tx.Hash(), blockNumber.Uint64(), blockHash)
@@ -210,7 +200,7 @@ func MakeReceipt(evm *vm.EVM, result *ExecutionResult, statedb *state.StateDB, b
210200
// and uses the input parameters for its environment. It returns the receipt
211201
// for the transaction, gas used and an error if the transaction failed,
212202
// indicating the block was invalid.
213-
func ApplyTransaction(config *params.ChainConfig, bc ChainContext, author *common.Address, gp *GasPool, statedb *state.StateDB, header *types.Header, tx *types.Transaction, usedGas *uint64, cfg vm.Config) (*types.Receipt, error) {
203+
func ApplyTransaction(config *params.ChainConfig, bc ChainContext, author *common.Address, gp *GasPool, statedb vm.StateDB, header *types.Header, tx *types.Transaction, usedGas *uint64, cfg vm.Config) (*types.Receipt, error) {
214204
msg, err := TransactionToMessage(tx, types.MakeSigner(config, header.Number, header.Time), header.BaseFee)
215205
if err != nil {
216206
return nil, err

core/types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ type Processor interface {
4848
// Process processes the state changes according to the Ethereum rules by running
4949
// the transaction messages using the statedb and applying any rewards to both
5050
// the processor (coinbase) and any included uncles.
51-
Process(block *types.Block, statedb *state.StateDB, cfg vm.Config) (*ProcessResult, error)
51+
Process(block *types.Block, statedb vm.StateDB, cfg vm.Config) (*ProcessResult, error)
5252
}
5353

5454
// ProcessResult contains the values computed by Process.

core/vm/interface.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,23 @@ type StateDB interface {
9191

9292
Witness() *stateless.Witness
9393
Finalise(bool)
94+
// IntermediateRoot computes the current root hash of the state trie.
95+
// It is called in
96+
// - between transactions (pre-byzantium)
97+
// - nowadays only after all transactions,
98+
// in order to obtain the state root hash .
99+
IntermediateRoot(deleteEmptyObjects bool) common.Hash
100+
101+
// GetLogs returns the logs matching the specified transaction hash, and annotates
102+
// them with the given blockNumber and blockHash.
103+
GetLogs(txHash common.Hash, blockNumber uint64, blockHash common.Hash) []*types.Log
104+
// TxIndex returns the current transaction index set by SetTxContext.
105+
TxIndex() int
106+
107+
// SetTxContext sets the current transaction hash and index which are
108+
// used when the EVM emits new state logs. It should be invoked before
109+
// transaction execution.
110+
SetTxContext(txHash common.Hash, txIndex int)
94111
}
95112

96113
// CallContext provides a basic interface for the EVM calling conventions. The EVM

0 commit comments

Comments
 (0)