Skip to content
This repository was archived by the owner on Nov 25, 2025. It is now read-only.

Commit 1755289

Browse files
authored
chore(core): split our code from state_processor.go in state_processor_ext.go (#781)
- Move our code from state_processor.go to state_processor_ext.go - Slight modifications to state_processor.go to make it more alike geth master
1 parent 73cd6e3 commit 1755289

File tree

2 files changed

+82
-69
lines changed

2 files changed

+82
-69
lines changed

core/state_processor.go

Lines changed: 7 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,13 @@
2727
package core
2828

2929
import (
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-
}

core/state_processor_ext.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// (c) 2025, Ava Labs, Inc.
2+
package core
3+
4+
import (
5+
"encoding/json"
6+
"fmt"
7+
8+
"github.com/ava-labs/coreth/core/extstate"
9+
"github.com/ava-labs/coreth/core/state"
10+
"github.com/ava-labs/coreth/params"
11+
"github.com/ava-labs/coreth/precompile/contract"
12+
"github.com/ava-labs/coreth/precompile/modules"
13+
"github.com/ava-labs/libevm/log"
14+
)
15+
16+
// ApplyPrecompileActivations checks if any of the precompiles specified by the chain config are enabled or disabled by the block
17+
// transition from [parentTimestamp] to the timestamp set in [blockContext]. If this is the case, it calls [Configure]
18+
// to apply the necessary state transitions for the upgrade.
19+
// This function is called within genesis setup to configure the starting state for precompiles enabled at genesis.
20+
// In block processing and building, ApplyUpgrades is called instead which also applies state upgrades.
21+
func ApplyPrecompileActivations(c *params.ChainConfig, parentTimestamp *uint64, blockContext contract.ConfigurationBlockContext, statedb *state.StateDB) error {
22+
blockTimestamp := blockContext.Timestamp()
23+
// Note: RegisteredModules returns precompiles sorted by module addresses.
24+
// This ensures that the order we call Configure for each precompile is consistent.
25+
// This ensures even if precompiles read/write state other than their own they will observe
26+
// an identical global state in a deterministic order when they are configured.
27+
extra := params.GetExtra(c)
28+
for _, module := range modules.RegisteredModules() {
29+
for _, activatingConfig := range extra.GetActivatingPrecompileConfigs(module.Address, parentTimestamp, blockTimestamp, extra.PrecompileUpgrades) {
30+
// If this transition activates the upgrade, configure the stateful precompile.
31+
// (or deconfigure it if it is being disabled.)
32+
if activatingConfig.IsDisabled() {
33+
log.Info("Disabling precompile", "name", module.ConfigKey)
34+
statedb.SelfDestruct(module.Address)
35+
// Calling Finalise here effectively commits Suicide call and wipes the contract state.
36+
// This enables re-configuration of the same contract state in the same block.
37+
// Without an immediate Finalise call after the Suicide, a reconfigured precompiled state can be wiped out
38+
// since Suicide will be committed after the reconfiguration.
39+
statedb.Finalise(true)
40+
} else {
41+
var printIntf interface{}
42+
marshalled, err := json.Marshal(activatingConfig)
43+
if err == nil {
44+
printIntf = string(marshalled)
45+
} else {
46+
printIntf = activatingConfig
47+
}
48+
49+
log.Info("Activating new precompile", "name", module.ConfigKey, "config", printIntf)
50+
// Set the nonce of the precompile's address (as is done when a contract is created) to ensure
51+
// that it is marked as non-empty and will not be cleaned up when the statedb is finalized.
52+
statedb.SetNonce(module.Address, 1)
53+
// Set the code of the precompile's address to a non-zero length byte slice to ensure that the precompile
54+
// can be called from within Solidity contracts. Solidity adds a check before invoking a contract to ensure
55+
// that it does not attempt to invoke a non-existent contract.
56+
statedb.SetCode(module.Address, []byte{0x1})
57+
extstatedb := &extstate.StateDB{VmStateDB: statedb}
58+
if err := module.Configure(params.GetExtra(c), activatingConfig, extstatedb, blockContext); err != nil {
59+
return fmt.Errorf("could not configure precompile, name: %s, reason: %w", module.ConfigKey, err)
60+
}
61+
}
62+
}
63+
}
64+
return nil
65+
}
66+
67+
// ApplyUpgrades checks if any of the precompile or state upgrades specified by the chain config are activated by the block
68+
// transition from [parentTimestamp] to the timestamp set in [header]. If this is the case, it calls [Configure]
69+
// to apply the necessary state transitions for the upgrade.
70+
// This function is called:
71+
// - in block processing to update the state when processing a block.
72+
// - in the miner to apply the state upgrades when producing a block.
73+
func ApplyUpgrades(c *params.ChainConfig, parentTimestamp *uint64, blockContext contract.ConfigurationBlockContext, statedb *state.StateDB) error {
74+
return ApplyPrecompileActivations(c, parentTimestamp, blockContext, statedb)
75+
}

0 commit comments

Comments
 (0)