Skip to content

Commit

Permalink
Rewrote configs
Browse files Browse the repository at this point in the history
I misunderstood how the OptionalNetworkUpgrades configuration was supposed to
be read. The list of upgrades are stored in the code, just the timestamp is
being read from the config (they can be nullable). They are identical to
mandatory upgrades, in terms of configurations.
  • Loading branch information
nytzuga committed Oct 13, 2023
1 parent 665d206 commit 5a1af29
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 19 deletions.
16 changes: 8 additions & 8 deletions params/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,17 +444,17 @@ func (c *ChainConfig) Verify() error {
return nil
}

type Fork struct {
name string `serialize:"true"`
block *big.Int `serialize:"true"` // some go-ethereum forks use block numbers
timestamp *uint64 `serialize:"true"` // Avalanche forks use timestamps
optional bool `serialize:"true"` // if true, the fork may be nil and next fork is still allowed
type fork struct {
name string
block *big.Int // some go-ethereum forks use block numbers
timestamp *uint64 // Avalanche forks use timestamps
optional bool // if true, the fork may be nil and next fork is still allowed
}

// CheckConfigForkOrder checks that we don't "skip" any forks, geth isn't pluggable enough
// to guarantee that forks can be implemented in a different order than on official networks
func (c *ChainConfig) CheckConfigForkOrder() error {
ethForks := []Fork{
ethForks := []fork{
{name: "homesteadBlock", block: c.HomesteadBlock},
{name: "eip150Block", block: c.EIP150Block},
{name: "eip155Block", block: c.EIP155Block},
Expand Down Expand Up @@ -493,8 +493,8 @@ func (c *ChainConfig) CheckConfigForkOrder() error {

// checkForks checks that forks are enabled in order and returns an error if not
// [blockFork] is true if the fork is a block number fork, false if it is a timestamp fork
func checkForks(forks []Fork, blockFork bool) error {
lastFork := Fork{}
func checkForks(forks []fork, blockFork bool) error {
lastFork := fork{}
for _, cur := range forks {
if blockFork && cur.block != nil && common.Big0.Cmp(cur.block) != 0 {
return errNonGenesisForkByHeight
Expand Down
35 changes: 30 additions & 5 deletions params/network_upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
package params

import (
"math/big"

"github.com/ava-labs/subnet-evm/utils"
)

Expand Down Expand Up @@ -50,21 +52,44 @@ func (m *MandatoryNetworkUpgrades) CheckMandatoryCompatible(newcfg *MandatoryNet
return nil
}

func (m *MandatoryNetworkUpgrades) mandatoryForkOrder() []Fork {
return []Fork{
func (m *MandatoryNetworkUpgrades) mandatoryForkOrder() []fork {
return []fork{
{name: "subnetEVMTimestamp", timestamp: m.SubnetEVMTimestamp},
{name: "dUpgradeTimestamp", timestamp: m.DUpgradeTimestamp},
}
}

type OptionalFork struct {
name string `serialize:"true"`
block big.Int `serialize:"true"`
timestamp uint64 `serialize:"true"`
}

type OptionalNetworkUpgrades struct {
Updates []Fork `json:"serialize,omitempty" serialize:"true"`
Forks []OptionalFork `serialize:"true"`
}

func (n *OptionalNetworkUpgrades) CheckOptionalCompatible(newcfg *OptionalNetworkUpgrades, time uint64) *ConfigCompatError {
return nil
}

func (n *OptionalNetworkUpgrades) optionalForkOrder() []Fork {
return n.Updates
func (n *OptionalNetworkUpgrades) optionalForkOrder() []fork {
forks := make([]fork, len(n.Forks))
for i, n := range n.Forks {
var block *big.Int
var timestamp *uint64
if n.block.BitLen() > 0 {
block = &n.block
}
if n.timestamp != 0 {
timestamp = &n.timestamp
}
forks[i] = fork{
name: n.name,
block: block,
timestamp: timestamp,
optional: true,
}
}
return []fork{}
}
13 changes: 7 additions & 6 deletions plugin/evm/message/handshake/upgrade_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type rawPrecompileUpgrade struct {
}

type networkUpgradeConfigMessage struct {
OptionalNetworkUpgrades []params.Fork `serialize:"true"`
OptionalNetworkUpgrades params.OptionalNetworkUpgrades `serialize:"true"`

// Config for modifying state as a network upgrade.
StateUpgrades []params.StateUpgrade `serialize:"true"`
Expand Down Expand Up @@ -64,7 +64,7 @@ func ParseUpgradeConfigMessage(bytes []byte) (*params.UpgradeConfig, error) {
}

return &params.UpgradeConfig{
OptionalNetworkUpgrades: &params.OptionalNetworkUpgrades{Updates: config.OptionalNetworkUpgrades},
OptionalNetworkUpgrades: &config.OptionalNetworkUpgrades,
StateUpgrades: config.StateUpgrades,
PrecompileUpgrades: PrecompileUpgrades,
}, nil
Expand Down Expand Up @@ -92,16 +92,17 @@ func UpgradeConfigToNetworkMessage(config *params.UpgradeConfig) (*UpgradeConfig
})
}

optionalNetworkUpgrades := make([]params.Fork, 0)
if config.OptionalNetworkUpgrades != nil {
optionalNetworkUpgrades = config.OptionalNetworkUpgrades.Updates
optionalNetworkUpgrades := config.OptionalNetworkUpgrades
if optionalNetworkUpgrades == nil {
optionalNetworkUpgrades = &params.OptionalNetworkUpgrades{}
}

wrappedConfig := networkUpgradeConfigMessage{
OptionalNetworkUpgrades: optionalNetworkUpgrades,
OptionalNetworkUpgrades: *optionalNetworkUpgrades,
StateUpgrades: config.StateUpgrades,
PrecompileUpgrades: PrecompileUpgrades,
}

bytes, err := Codec.Marshal(Version, wrappedConfig)
if err != nil {
return nil, err
Expand Down
4 changes: 4 additions & 0 deletions plugin/evm/message/handshake/upgrade_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ func TestSerialize(t *testing.T) {
config3, err := ParseUpgradeConfigMessage(message2.Bytes)
require.NoError(t, err)

message3, err := UpgradeConfigToNetworkMessage(config3)
require.NoError(t, err)

require.Equal(t, config, config3)
require.Equal(t, message.Hash, message2.Hash)
require.Equal(t, message2.Hash, message3.Hash)
}

0 comments on commit 5a1af29

Please sign in to comment.