2727package core
2828
2929import (
30- "encoding/json"
3130 "fmt"
3231 "math/big"
3332
@@ -36,13 +35,9 @@ import (
3635 "github.com/ava-labs/libevm/crypto"
3736 "github.com/ava-labs/libevm/log"
3837 "github.com/ava-labs/subnet-evm/consensus"
39- "github.com/ava-labs/subnet-evm/core/extstate"
4038 "github.com/ava-labs/subnet-evm/core/state"
4139 "github.com/ava-labs/subnet-evm/core/types"
4240 "github.com/ava-labs/subnet-evm/params"
43- "github.com/ava-labs/subnet-evm/precompile/contract"
44- "github.com/ava-labs/subnet-evm/precompile/modules"
45- "github.com/ava-labs/subnet-evm/stateupgrade"
4641)
4742
4843// StateProcessor is a basic Processor, which takes care of transitioning
@@ -97,6 +92,7 @@ func (p *StateProcessor) Process(block *types.Block, parent *types.Header, state
9792 if beaconRoot := block .BeaconRoot (); beaconRoot != nil {
9893 ProcessBeaconBlockRoot (* beaconRoot , vmenv , statedb )
9994 }
95+
10096 // Iterate over and process the individual transactions
10197 for i , tx := range block .Transactions () {
10298 msg , err := TransactionToMessage (tx , signer , header .BaseFee )
@@ -129,7 +125,6 @@ func applyTransaction(msg *Message, config *params.ChainConfig, gp *GasPool, sta
129125 if err != nil {
130126 return nil , err
131127 }
132-
133128 // Update the state with pending changes.
134129 var root []byte
135130 if config .IsByzantium (blockNumber ) {
@@ -186,7 +181,7 @@ func ApplyTransaction(config *params.ChainConfig, bc ChainContext, blockContext
186181
187182// ProcessBeaconBlockRoot applies the EIP-4788 system call to the beacon block root
188183// contract. This method is exported to be used in tests.
189- func ProcessBeaconBlockRoot (beaconRoot common.Hash , vmenv * vm.EVM , statedb * state.StateDB ) {
184+ func ProcessBeaconBlockRoot (beaconRoot common.Hash , evm * vm.EVM , statedb * state.StateDB ) {
190185 // If EIP-4788 is enabled, we need to invoke the beaconroot storage contract with
191186 // the new root
192187 msg := & Message {
@@ -198,102 +193,8 @@ func ProcessBeaconBlockRoot(beaconRoot common.Hash, vmenv *vm.EVM, statedb *stat
198193 To : & params .BeaconRootsStorageAddress ,
199194 Data : beaconRoot [:],
200195 }
201- vmenv .Reset (NewEVMTxContext (msg ), statedb )
196+ evm .Reset (NewEVMTxContext (msg ), statedb )
202197 statedb .AddAddressToAccessList (params .BeaconRootsStorageAddress )
203- _ , _ , _ = vmenv .Call (vm .AccountRef (msg .From ), * msg .To , msg .Data , 30_000_000 , common .U2560 )
198+ _ , _ , _ = evm .Call (vm .AccountRef (msg .From ), * msg .To , msg .Data , 30_000_000 , common .U2560 )
204199 statedb .Finalise (true )
205200}
206-
207- // ApplyPrecompileActivations checks if any of the precompiles specified by the chain config are enabled or disabled by the block
208- // transition from [parentTimestamp] to the timestamp set in [blockContext]. If this is the case, it calls [Configure]
209- // to apply the necessary state transitions for the upgrade.
210- // This function is called within genesis setup to configure the starting state for precompiles enabled at genesis.
211- // In block processing and building, ApplyUpgrades is called instead which also applies state upgrades.
212- func ApplyPrecompileActivations (c * params.ChainConfig , parentTimestamp * uint64 , blockContext contract.ConfigurationBlockContext , statedb * state.StateDB ) error {
213- blockTimestamp := blockContext .Timestamp ()
214- // Note: RegisteredModules returns precompiles sorted by module addresses.
215- // This ensures that the order we call Configure for each precompile is consistent.
216- // This ensures even if precompiles read/write state other than their own they will observe
217- // an identical global state in a deterministic order when they are configured.
218- extra := params .GetExtra (c )
219- for _ , module := range modules .RegisteredModules () {
220- for _ , activatingConfig := range extra .GetActivatingPrecompileConfigs (module .Address , parentTimestamp , blockTimestamp , extra .PrecompileUpgrades ) {
221- // If this transition activates the upgrade, configure the stateful precompile.
222- // (or deconfigure it if it is being disabled.)
223- if activatingConfig .IsDisabled () {
224- log .Info ("Disabling precompile" , "name" , module .ConfigKey )
225- statedb .SelfDestruct (module .Address )
226- // Calling Finalise here effectively commits Suicide call and wipes the contract state.
227- // This enables re-configuration of the same contract state in the same block.
228- // Without an immediate Finalise call after the Suicide, a reconfigured precompiled state can be wiped out
229- // since Suicide will be committed after the reconfiguration.
230- statedb .Finalise (true )
231- } else {
232- var printIntf interface {}
233- marshalled , err := json .Marshal (activatingConfig )
234- if err == nil {
235- printIntf = string (marshalled )
236- } else {
237- printIntf = activatingConfig
238- }
239-
240- log .Info ("Activating new precompile" , "name" , module .ConfigKey , "config" , printIntf )
241- // Set the nonce of the precompile's address (as is done when a contract is created) to ensure
242- // that it is marked as non-empty and will not be cleaned up when the statedb is finalized.
243- statedb .SetNonce (module .Address , 1 )
244- // Set the code of the precompile's address to a non-zero length byte slice to ensure that the precompile
245- // can be called from within Solidity contracts. Solidity adds a check before invoking a contract to ensure
246- // that it does not attempt to invoke a non-existent contract.
247- statedb .SetCode (module .Address , []byte {0x1 })
248- extstatedb := & extstate.StateDB {VmStateDB : statedb }
249- if err := module .Configure (params .GetExtra (c ), activatingConfig , extstatedb , blockContext ); err != nil {
250- return fmt .Errorf ("could not configure precompile, name: %s, reason: %w" , module .ConfigKey , err )
251- }
252- }
253- }
254- }
255- return nil
256- }
257-
258- // applyStateUpgrades checks if any of the state upgrades specified by the chain config are activated by the block
259- // transition from [parentTimestamp] to the timestamp set in [header]. If this is the case, it calls [Configure]
260- // to apply the necessary state transitions for the upgrade.
261- func applyStateUpgrades (c * params.ChainConfig , parentTimestamp * uint64 , blockContext contract.ConfigurationBlockContext , statedb * state.StateDB ) error {
262- // Apply state upgrades
263- configExtra := params .GetExtra (c )
264- for _ , upgrade := range configExtra .GetActivatingStateUpgrades (parentTimestamp , blockContext .Timestamp (), configExtra .StateUpgrades ) {
265- log .Info ("Applying state upgrade" , "blockNumber" , blockContext .Number (), "upgrade" , upgrade )
266- if err := stateupgrade .Configure (& upgrade , c , statedb , blockContext ); err != nil {
267- return fmt .Errorf ("could not configure state upgrade: %w" , err )
268- }
269- }
270- return nil
271- }
272-
273- // ApplyUpgrades checks if any of the precompile or state upgrades specified by the chain config are activated by the block
274- // transition from [parentTimestamp] to the timestamp set in [header]. If this is the case, it calls [Configure]
275- // to apply the necessary state transitions for the upgrade.
276- // This function is called:
277- // - in block processing to update the state when processing a block.
278- // - in the miner to apply the state upgrades when producing a block.
279- func ApplyUpgrades (c * params.ChainConfig , parentTimestamp * uint64 , blockContext contract.ConfigurationBlockContext , statedb * state.StateDB ) error {
280- if err := ApplyPrecompileActivations (c , parentTimestamp , blockContext , statedb ); err != nil {
281- return err
282- }
283- return applyStateUpgrades (c , parentTimestamp , blockContext , statedb )
284- }
285-
286- type blockContext struct {
287- number * big.Int
288- timestamp uint64
289- }
290-
291- func NewBlockContext (number * big.Int , timestamp uint64 ) * blockContext {
292- return & blockContext {
293- number : number ,
294- timestamp : timestamp ,
295- }
296- }
297-
298- func (bc * blockContext ) Number () * big.Int { return bc .number }
299- func (bc * blockContext ) Timestamp () uint64 { return bc .timestamp }
0 commit comments