2727package core
2828
2929import (
30- "encoding/json"
3130 "fmt"
3231 "math/big"
3332
3433 "github.com/ava-labs/coreth/consensus"
35- "github.com/ava-labs/coreth/core/extstate"
3634 "github.com/ava-labs/coreth/core/state"
3735 "github.com/ava-labs/coreth/core/types"
3836 "github.com/ava-labs/coreth/params"
39- "github.com/ava-labs/coreth/precompile/contract"
40- "github.com/ava-labs/coreth/precompile/modules"
4137 "github.com/ava-labs/libevm/common"
4238 "github.com/ava-labs/libevm/core/vm"
4339 "github.com/ava-labs/libevm/crypto"
@@ -93,23 +89,27 @@ func (p *StateProcessor) Process(block *types.Block, parent *types.Header, state
9389 vmenv = vm .NewEVM (context , vm.TxContext {}, statedb , p .config , cfg )
9490 signer = types .MakeSigner (p .config , header .Number , header .Time )
9591 )
92+
9693 if beaconRoot := block .BeaconRoot (); beaconRoot != nil {
9794 ProcessBeaconBlockRoot (* beaconRoot , vmenv , statedb )
9895 }
96+
9997 // Iterate over and process the individual transactions
10098 for i , tx := range block .Transactions () {
10199 msg , err := TransactionToMessage (tx , signer , header .BaseFee )
102100 if err != nil {
103101 return nil , nil , 0 , fmt .Errorf ("could not apply tx %d [%v]: %w" , i , tx .Hash ().Hex (), err )
104102 }
105103 statedb .SetTxContext (tx .Hash (), i )
104+
106105 receipt , err := applyTransaction (msg , p .config , gp , statedb , blockNumber , blockHash , tx , usedGas , vmenv )
107106 if err != nil {
108107 return nil , nil , 0 , fmt .Errorf ("could not apply tx %d [%v]: %w" , i , tx .Hash ().Hex (), err )
109108 }
110109 receipts = append (receipts , receipt )
111110 allLogs = append (allLogs , receipt .Logs ... )
112111 }
112+
113113 // Finalize the block, applying any consensus engine specific extras (e.g. block rewards)
114114 if err := p .engine .Finalize (p .bc , block , parent , statedb , receipts ); err != nil {
115115 return nil , nil , 0 , fmt .Errorf ("engine finalization check failed: %w" , err )
@@ -128,7 +128,6 @@ func applyTransaction(msg *Message, config *params.ChainConfig, gp *GasPool, sta
128128 if err != nil {
129129 return nil , err
130130 }
131-
132131 // Update the state with pending changes.
133132 var root []byte
134133 if config .IsByzantium (blockNumber ) {
@@ -185,7 +184,7 @@ func ApplyTransaction(config *params.ChainConfig, bc ChainContext, blockContext
185184
186185// ProcessBeaconBlockRoot applies the EIP-4788 system call to the beacon block root
187186// contract. This method is exported to be used in tests.
188- func ProcessBeaconBlockRoot (beaconRoot common.Hash , vmenv * vm.EVM , statedb * state.StateDB ) {
187+ func ProcessBeaconBlockRoot (beaconRoot common.Hash , evm * vm.EVM , statedb * state.StateDB ) {
189188 // If EIP-4788 is enabled, we need to invoke the beaconroot storage contract with
190189 // the new root
191190 msg := & Message {
@@ -197,69 +196,8 @@ func ProcessBeaconBlockRoot(beaconRoot common.Hash, vmenv *vm.EVM, statedb *stat
197196 To : & params .BeaconRootsStorageAddress ,
198197 Data : beaconRoot [:],
199198 }
200- vmenv .Reset (NewEVMTxContext (msg ), statedb )
199+ evm .Reset (NewEVMTxContext (msg ), statedb )
201200 statedb .AddAddressToAccessList (params .BeaconRootsStorageAddress )
202- _ , _ , _ = vmenv .Call (vm .AccountRef (msg .From ), * msg .To , msg .Data , 30_000_000 , common .U2560 )
201+ _ , _ , _ = evm .Call (vm .AccountRef (msg .From ), * msg .To , msg .Data , 30_000_000 , common .U2560 )
203202 statedb .Finalise (true )
204203}
205-
206- // ApplyPrecompileActivations checks if any of the precompiles specified by the chain config are enabled or disabled by the block
207- // transition from [parentTimestamp] to the timestamp set in [blockContext]. If this is the case, it calls [Configure]
208- // to apply the necessary state transitions for the upgrade.
209- // This function is called within genesis setup to configure the starting state for precompiles enabled at genesis.
210- // In block processing and building, ApplyUpgrades is called instead which also applies state upgrades.
211- func ApplyPrecompileActivations (c * params.ChainConfig , parentTimestamp * uint64 , blockContext contract.ConfigurationBlockContext , statedb * state.StateDB ) error {
212- blockTimestamp := blockContext .Timestamp ()
213- // Note: RegisteredModules returns precompiles sorted by module addresses.
214- // This ensures that the order we call Configure for each precompile is consistent.
215- // This ensures even if precompiles read/write state other than their own they will observe
216- // an identical global state in a deterministic order when they are configured.
217- extra := params .GetExtra (c )
218- for _ , module := range modules .RegisteredModules () {
219- for _ , activatingConfig := range extra .GetActivatingPrecompileConfigs (module .Address , parentTimestamp , blockTimestamp , extra .PrecompileUpgrades ) {
220- // If this transition activates the upgrade, configure the stateful precompile.
221- // (or deconfigure it if it is being disabled.)
222- if activatingConfig .IsDisabled () {
223- log .Info ("Disabling precompile" , "name" , module .ConfigKey )
224- statedb .SelfDestruct (module .Address )
225- // Calling Finalise here effectively commits Suicide call and wipes the contract state.
226- // This enables re-configuration of the same contract state in the same block.
227- // Without an immediate Finalise call after the Suicide, a reconfigured precompiled state can be wiped out
228- // since Suicide will be committed after the reconfiguration.
229- statedb .Finalise (true )
230- } else {
231- var printIntf interface {}
232- marshalled , err := json .Marshal (activatingConfig )
233- if err == nil {
234- printIntf = string (marshalled )
235- } else {
236- printIntf = activatingConfig
237- }
238-
239- log .Info ("Activating new precompile" , "name" , module .ConfigKey , "config" , printIntf )
240- // Set the nonce of the precompile's address (as is done when a contract is created) to ensure
241- // that it is marked as non-empty and will not be cleaned up when the statedb is finalized.
242- statedb .SetNonce (module .Address , 1 )
243- // Set the code of the precompile's address to a non-zero length byte slice to ensure that the precompile
244- // can be called from within Solidity contracts. Solidity adds a check before invoking a contract to ensure
245- // that it does not attempt to invoke a non-existent contract.
246- statedb .SetCode (module .Address , []byte {0x1 })
247- extstatedb := & extstate.StateDB {VmStateDB : statedb }
248- if err := module .Configure (params .GetExtra (c ), activatingConfig , extstatedb , blockContext ); err != nil {
249- return fmt .Errorf ("could not configure precompile, name: %s, reason: %w" , module .ConfigKey , err )
250- }
251- }
252- }
253- }
254- return nil
255- }
256-
257- // ApplyUpgrades checks if any of the precompile or state upgrades specified by the chain config are activated by the block
258- // transition from [parentTimestamp] to the timestamp set in [header]. If this is the case, it calls [Configure]
259- // to apply the necessary state transitions for the upgrade.
260- // This function is called:
261- // - in block processing to update the state when processing a block.
262- // - in the miner to apply the state upgrades when producing a block.
263- func ApplyUpgrades (c * params.ChainConfig , parentTimestamp * uint64 , blockContext contract.ConfigurationBlockContext , statedb * state.StateDB ) error {
264- return ApplyPrecompileActivations (c , parentTimestamp , blockContext , statedb )
265- }
0 commit comments