Skip to content

Commit bba773e

Browse files
authored
move state upgrade related functions to own file (#684)
1 parent 199a5d9 commit bba773e

File tree

2 files changed

+72
-70
lines changed

2 files changed

+72
-70
lines changed

params/precompile_upgrade.go

Lines changed: 0 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -148,26 +148,6 @@ func (c *ChainConfig) verifyPrecompileUpgrades() error {
148148
return nil
149149
}
150150

151-
// verifyStateUpgrades checks [c.StateUpgrades] is well formed:
152-
// - the specified blockTimestamps must monotonically increase
153-
func (c *ChainConfig) verifyStateUpgrades() error {
154-
var previousUpgradeTimestamp *big.Int
155-
for i, upgrade := range c.StateUpgrades {
156-
upgradeTimestamp := upgrade.BlockTimestamp
157-
// Verify the upgrade's timestamp is greater than 0 (to avoid confusion with genesis).
158-
if upgradeTimestamp.Cmp(common.Big0) <= 0 {
159-
return fmt.Errorf("StateUpgrade[%d]: config block timestamp (%v) must be greater than 0", i, upgradeTimestamp)
160-
}
161-
162-
// Verify specified timestamps are strictly monotonically increasing.
163-
if previousUpgradeTimestamp != nil && upgradeTimestamp.Cmp(previousUpgradeTimestamp) <= 0 {
164-
return fmt.Errorf("StateUpgrade[%d]: config block timestamp (%v) <= previous timestamp (%v)", i, upgradeTimestamp, previousUpgradeTimestamp)
165-
}
166-
previousUpgradeTimestamp = upgradeTimestamp
167-
}
168-
return nil
169-
}
170-
171151
// getActivePrecompileConfig returns the most recent precompile config corresponding to [address].
172152
// If none have occurred, returns nil.
173153
func (c *ChainConfig) getActivePrecompileConfig(address common.Address, blockTimestamp *big.Int) precompileconfig.Config {
@@ -207,18 +187,6 @@ func (c *ChainConfig) GetActivatingPrecompileConfigs(address common.Address, fro
207187
return configs
208188
}
209189

210-
// GetActivatingStateUpgrades returns all state upgrades configured to activate during the
211-
// state transition from a block with timestamp [from] to a block with timestamp [to].
212-
func (c *ChainConfig) GetActivatingStateUpgrades(from *big.Int, to *big.Int, upgrades []StateUpgrade) []StateUpgrade {
213-
activating := make([]StateUpgrade, 0)
214-
for _, upgrade := range upgrades {
215-
if utils.IsForkTransition(upgrade.BlockTimestamp, from, to) {
216-
activating = append(activating, upgrade)
217-
}
218-
}
219-
return activating
220-
}
221-
222190
// CheckPrecompilesCompatible checks if [precompileUpgrades] are compatible with [c] at [headTimestamp].
223191
// Returns a ConfigCompatError if upgrades already activated at [headTimestamp] are missing from
224192
// [precompileUpgrades]. Upgrades not already activated may be modified or absent from [precompileUpgrades].
@@ -277,44 +245,6 @@ func (c *ChainConfig) checkPrecompileCompatible(address common.Address, precompi
277245
return nil
278246
}
279247

280-
// CheckStateUpgradesCompatible checks if [stateUpgrades] are compatible with [c] at [headTimestamp].
281-
func (c *ChainConfig) CheckStateUpgradesCompatible(stateUpgrades []StateUpgrade, lastTimestamp *big.Int) *ConfigCompatError {
282-
// All active upgrades (from nil to [lastTimestamp]) must match.
283-
activeUpgrades := c.GetActivatingStateUpgrades(nil, lastTimestamp, c.StateUpgrades)
284-
newUpgrades := c.GetActivatingStateUpgrades(nil, lastTimestamp, stateUpgrades)
285-
286-
// Check activated upgrades are still present.
287-
for i, upgrade := range activeUpgrades {
288-
if len(newUpgrades) <= i {
289-
// missing upgrade
290-
return newCompatError(
291-
fmt.Sprintf("missing StateUpgrade[%d]", i),
292-
upgrade.BlockTimestamp,
293-
nil,
294-
)
295-
}
296-
// All upgrades that have activated must be identical.
297-
if !upgrade.Equal(&newUpgrades[i]) {
298-
return newCompatError(
299-
fmt.Sprintf("StateUpgrade[%d]", i),
300-
upgrade.BlockTimestamp,
301-
newUpgrades[i].BlockTimestamp,
302-
)
303-
}
304-
}
305-
// then, make sure newUpgrades does not have additional upgrades
306-
// that are already activated. (cannot perform retroactive upgrade)
307-
if len(newUpgrades) > len(activeUpgrades) {
308-
return newCompatError(
309-
fmt.Sprintf("cannot retroactively enable StateUpgrade[%d]", len(activeUpgrades)),
310-
nil,
311-
newUpgrades[len(activeUpgrades)].BlockTimestamp, // this indexes to the first element in newUpgrades after the end of activeUpgrades
312-
)
313-
}
314-
315-
return nil
316-
}
317-
318248
// EnabledStatefulPrecompiles returns current stateful precompile configs that are enabled at [blockTimestamp].
319249
func (c *ChainConfig) EnabledStatefulPrecompiles(blockTimestamp *big.Int) Precompiles {
320250
statefulPrecompileConfigs := make(Precompiles)

params/state_upgrade.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
package params
55

66
import (
7+
"fmt"
78
"math/big"
89
"reflect"
910

11+
"github.com/ava-labs/subnet-evm/utils"
1012
"github.com/ethereum/go-ethereum/common"
1113
"github.com/ethereum/go-ethereum/common/hexutil"
1214
"github.com/ethereum/go-ethereum/common/math"
@@ -32,3 +34,73 @@ type StateUpgradeAccount struct {
3234
func (s *StateUpgrade) Equal(other *StateUpgrade) bool {
3335
return reflect.DeepEqual(s, other)
3436
}
37+
38+
// verifyStateUpgrades checks [c.StateUpgrades] is well formed:
39+
// - the specified blockTimestamps must monotonically increase
40+
func (c *ChainConfig) verifyStateUpgrades() error {
41+
var previousUpgradeTimestamp *big.Int
42+
for i, upgrade := range c.StateUpgrades {
43+
upgradeTimestamp := upgrade.BlockTimestamp
44+
// Verify the upgrade's timestamp is greater than 0 (to avoid confusion with genesis).
45+
if upgradeTimestamp.Cmp(common.Big0) <= 0 {
46+
return fmt.Errorf("StateUpgrade[%d]: config block timestamp (%v) must be greater than 0", i, upgradeTimestamp)
47+
}
48+
49+
// Verify specified timestamps are strictly monotonically increasing.
50+
if previousUpgradeTimestamp != nil && upgradeTimestamp.Cmp(previousUpgradeTimestamp) <= 0 {
51+
return fmt.Errorf("StateUpgrade[%d]: config block timestamp (%v) <= previous timestamp (%v)", i, upgradeTimestamp, previousUpgradeTimestamp)
52+
}
53+
previousUpgradeTimestamp = upgradeTimestamp
54+
}
55+
return nil
56+
}
57+
58+
// GetActivatingStateUpgrades returns all state upgrades configured to activate during the
59+
// state transition from a block with timestamp [from] to a block with timestamp [to].
60+
func (c *ChainConfig) GetActivatingStateUpgrades(from *big.Int, to *big.Int, upgrades []StateUpgrade) []StateUpgrade {
61+
activating := make([]StateUpgrade, 0)
62+
for _, upgrade := range upgrades {
63+
if utils.IsForkTransition(upgrade.BlockTimestamp, from, to) {
64+
activating = append(activating, upgrade)
65+
}
66+
}
67+
return activating
68+
}
69+
70+
// CheckStateUpgradesCompatible checks if [stateUpgrades] are compatible with [c] at [headTimestamp].
71+
func (c *ChainConfig) CheckStateUpgradesCompatible(stateUpgrades []StateUpgrade, lastTimestamp *big.Int) *ConfigCompatError {
72+
// All active upgrades (from nil to [lastTimestamp]) must match.
73+
activeUpgrades := c.GetActivatingStateUpgrades(nil, lastTimestamp, c.StateUpgrades)
74+
newUpgrades := c.GetActivatingStateUpgrades(nil, lastTimestamp, stateUpgrades)
75+
76+
// Check activated upgrades are still present.
77+
for i, upgrade := range activeUpgrades {
78+
if len(newUpgrades) <= i {
79+
// missing upgrade
80+
return newCompatError(
81+
fmt.Sprintf("missing StateUpgrade[%d]", i),
82+
upgrade.BlockTimestamp,
83+
nil,
84+
)
85+
}
86+
// All upgrades that have activated must be identical.
87+
if !upgrade.Equal(&newUpgrades[i]) {
88+
return newCompatError(
89+
fmt.Sprintf("StateUpgrade[%d]", i),
90+
upgrade.BlockTimestamp,
91+
newUpgrades[i].BlockTimestamp,
92+
)
93+
}
94+
}
95+
// then, make sure newUpgrades does not have additional upgrades
96+
// that are already activated. (cannot perform retroactive upgrade)
97+
if len(newUpgrades) > len(activeUpgrades) {
98+
return newCompatError(
99+
fmt.Sprintf("cannot retroactively enable StateUpgrade[%d]", len(activeUpgrades)),
100+
nil,
101+
newUpgrades[len(activeUpgrades)].BlockTimestamp, // this indexes to the first element in newUpgrades after the end of activeUpgrades
102+
)
103+
}
104+
105+
return nil
106+
}

0 commit comments

Comments
 (0)