Skip to content

Commit

Permalink
Params nits (#1280)
Browse files Browse the repository at this point in the history
* refactor params config

* rename SetEVMUpgrades

* rename file

---------

Co-authored-by: Darioush Jalali <darioush.jalali@avalabs.org>
  • Loading branch information
ceyonur and darioush authored Aug 14, 2024
1 parent 1bba2c8 commit face38b
Show file tree
Hide file tree
Showing 15 changed files with 267 additions and 263 deletions.
3 changes: 2 additions & 1 deletion core/blockchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os"
"testing"

"github.com/ava-labs/avalanchego/upgrade"
"github.com/ava-labs/subnet-evm/consensus/dummy"
"github.com/ava-labs/subnet-evm/core/rawdb"
"github.com/ava-labs/subnet-evm/core/state"
Expand Down Expand Up @@ -1216,7 +1217,7 @@ func TestEIP3651(t *testing.T) {
funds = new(big.Int).Mul(common.Big1, big.NewInt(params.Ether))
gspec = &Genesis{
Config: params.TestChainConfig,
Timestamp: uint64(params.DefaultGenesisTime.Unix()),
Timestamp: uint64(upgrade.InitiallyActiveTime.Unix()),
Alloc: GenesisAlloc{
addr1: {Balance: funds},
addr2: {Balance: funds},
Expand Down
3 changes: 2 additions & 1 deletion core/state_processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"math/big"
"testing"

"github.com/ava-labs/avalanchego/upgrade"
"github.com/ava-labs/subnet-evm/consensus"
"github.com/ava-labs/subnet-evm/consensus/dummy"
"github.com/ava-labs/subnet-evm/consensus/misc/eip4844"
Expand Down Expand Up @@ -111,7 +112,7 @@ func TestStateProcessorErrors(t *testing.T) {
db = rawdb.NewMemoryDatabase()
gspec = &Genesis{
Config: config,
Timestamp: uint64(params.DefaultGenesisTime.Unix()),
Timestamp: uint64(upgrade.InitiallyActiveTime.Unix()),
Alloc: GenesisAlloc{
common.HexToAddress("0x71562b71999873DB5b286dF957af199Ec94617F7"): GenesisAccount{
Balance: big.NewInt(4000000000000000000), // 4 ether
Expand Down
3 changes: 2 additions & 1 deletion internal/ethapi/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
"testing"
"time"

"github.com/ava-labs/avalanchego/upgrade"
"github.com/ava-labs/subnet-evm/accounts"
"github.com/ava-labs/subnet-evm/commontype"
"github.com/ava-labs/subnet-evm/consensus"
Expand Down Expand Up @@ -1409,7 +1410,7 @@ func setupReceiptBackend(t *testing.T, genBlocks int) (*testBackend, []common.Ha
Config: &config,
ExcessBlobGas: new(uint64),
BlobGasUsed: new(uint64),
Timestamp: uint64(params.DefaultGenesisTime.Unix()),
Timestamp: uint64(upgrade.InitiallyActiveTime.Unix()),
Alloc: core.GenesisAlloc{
acc1Addr: {Balance: big.NewInt(params.Ether)},
acc2Addr: {Balance: big.NewInt(params.Ether)},
Expand Down
217 changes: 68 additions & 149 deletions params/config.go

Large diffs are not rendered by default.

121 changes: 117 additions & 4 deletions params/config_extra.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,45 @@ package params
import (
"encoding/json"
"errors"
"fmt"
"math/big"

"github.com/ava-labs/avalanchego/snow"
"github.com/ava-labs/subnet-evm/commontype"
"github.com/ava-labs/subnet-evm/utils"
"github.com/ethereum/go-ethereum/common"
)

const (
maxJSONLen = 64 * 1024 * 1024 // 64MB

// Consensus Params
RollupWindow uint64 = 10
DynamicFeeExtraDataSize = 80

// For legacy tests
MinGasPrice int64 = 225_000_000_000
TestInitialBaseFee int64 = 225_000_000_000
TestMaxBaseFee int64 = 225_000_000_000
)

var (
errNonGenesisForkByHeight = errors.New("subnet-evm only supports forking by height at the genesis block")

DefaultChainID = big.NewInt(43214)

DefaultFeeConfig = commontype.FeeConfig{
GasLimit: big.NewInt(8_000_000),
TargetBlockRate: 2, // in seconds

MinBaseFee: big.NewInt(25_000_000_000),
TargetGas: big.NewInt(15_000_000),
BaseFeeChangeDenominator: big.NewInt(36),

MinBlockGasCost: big.NewInt(0),
MaxBlockGasCost: big.NewInt(1_000_000),
BlockGasCostStep: big.NewInt(200_000),
}
)

// UpgradeConfig includes the following configs that may be specified in upgradeBytes:
Expand All @@ -31,6 +66,13 @@ type AvalancheContext struct {
SnowCtx *snow.Context
}

// SetEthUpgrades sets the mapped upgrades Avalanche > EVM upgrades) for the chain config.
func (c *ChainConfig) SetEthUpgrades(avalancheUpgrades NetworkUpgrades) {
if avalancheUpgrades.EtnaTimestamp != nil {
c.CancunTime = utils.NewUint64(*avalancheUpgrades.EtnaTimestamp)
}
}

// UnmarshalJSON parses the JSON-encoded data and stores the result in the
// object pointed to by c.
// This is a custom unmarshaler to handle the Precompiles field.
Expand Down Expand Up @@ -138,6 +180,48 @@ func (cu *ChainConfigWithUpgradesJSON) UnmarshalJSON(input []byte) error {
return nil
}

// Verify verifies chain config and returns error
func (c *ChainConfig) Verify() error {
if err := c.FeeConfig.Verify(); err != nil {
return err
}

// Verify the precompile upgrades are internally consistent given the existing chainConfig.
if err := c.verifyPrecompileUpgrades(); err != nil {
return fmt.Errorf("invalid precompile upgrades: %w", err)
}

// Verify the state upgrades are internally consistent given the existing chainConfig.
if err := c.verifyStateUpgrades(); err != nil {
return fmt.Errorf("invalid state upgrades: %w", err)
}

// Verify the network upgrades are internally consistent given the existing chainConfig.
if err := c.verifyNetworkUpgrades(c.SnowCtx.NetworkID); err != nil {
return fmt.Errorf("invalid network upgrades: %w", err)
}

return nil
}

// IsPrecompileEnabled returns whether precompile with [address] is enabled at [timestamp].
func (c *ChainConfig) IsPrecompileEnabled(address common.Address, timestamp uint64) bool {
config := c.getActivePrecompileConfig(address, timestamp)
return config != nil && !config.IsDisabled()
}

// GetFeeConfig returns the original FeeConfig contained in the genesis ChainConfig.
// Implements precompile.ChainConfig interface.
func (c *ChainConfig) GetFeeConfig() commontype.FeeConfig {
return c.FeeConfig
}

// AllowedFeeRecipients returns the original AllowedFeeRecipients parameter contained in the genesis ChainConfig.
// Implements precompile.ChainConfig interface.
func (c *ChainConfig) AllowedFeeRecipients() bool {
return c.AllowFeeRecipients
}

// ToWithUpgradesJSON converts the ChainConfig to ChainConfigWithUpgradesJSON with upgrades explicitly displayed.
// ChainConfig does not include upgrades in its JSON output.
// This is a workaround for showing upgrades in the JSON output.
Expand Down Expand Up @@ -180,9 +264,38 @@ func (c *ChainConfig) SetNetworkUpgradeDefaults() {
c.NetworkUpgrades.setDefaults(c.SnowCtx.NetworkID)
}

// SetEVMUpgrades sets the mapped upgrades Avalanche > EVM upgrades) for the chain config.
func (c *ChainConfig) SetEVMUpgrades(avalancheUpgrades NetworkUpgrades) {
if avalancheUpgrades.EtnaTimestamp != nil {
c.CancunTime = utils.NewUint64(*avalancheUpgrades.EtnaTimestamp)
func (r *Rules) PredicatersExist() bool {
return len(r.Predicaters) > 0
}

func (r *Rules) PredicaterExists(addr common.Address) bool {
_, PredicaterExists := r.Predicaters[addr]
return PredicaterExists
}

// IsPrecompileEnabled returns true if the precompile at [addr] is enabled for this rule set.
func (r *Rules) IsPrecompileEnabled(addr common.Address) bool {
_, ok := r.ActivePrecompiles[addr]
return ok
}

func ptrToString(val *uint64) string {
if val == nil {
return "nil"
}
return fmt.Sprintf("%d", *val)
}

// IsForkTransition returns true if [fork] activates during the transition from
// [parent] to [current].
// Taking [parent] as a pointer allows for us to pass nil when checking forks
// that activate during genesis.
// Note: this works for both block number and timestamp activated forks.
func IsForkTransition(fork *uint64, parent *uint64, current uint64) bool {
var parentForked bool
if parent != nil {
parentForked = isTimestampForked(fork, *parent)
}
currentForked := isTimestampForked(fork, current)
return !parentForked && currentForked
}
41 changes: 21 additions & 20 deletions utils/fork_test.go → params/config_extra_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
// (c) 2024 Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package utils
package params

import (
"testing"

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

Expand All @@ -28,28 +29,28 @@ func TestIsTimestampForked(t *testing.T) {
isForked: false,
},
"zero fork at genesis": {
fork: NewUint64(0),
fork: utils.NewUint64(0),
block: 0,
isForked: true,
},
"pre fork timestamp": {
fork: NewUint64(100),
fork: utils.NewUint64(100),
block: 50,
isForked: false,
},
"at fork timestamp": {
fork: NewUint64(100),
fork: utils.NewUint64(100),
block: 100,
isForked: true,
},
"post fork timestamp": {
fork: NewUint64(100),
fork: utils.NewUint64(100),
block: 150,
isForked: true,
},
} {
t.Run(name, func(t *testing.T) {
res := IsTimestampForked(test.fork, test.block)
res := isTimestampForked(test.fork, test.block)
assert.Equal(t, test.isForked, res)
})
}
Expand All @@ -70,50 +71,50 @@ func TestIsForkTransition(t *testing.T) {
transitioned: false,
},
"activate at genesis": {
fork: NewUint64(0),
fork: utils.NewUint64(0),
parent: nil,
current: 0,
transitioned: true,
},
"nil fork arbitrary transition": {
fork: nil,
parent: NewUint64(100),
parent: utils.NewUint64(100),
current: 101,
transitioned: false,
},
"nil fork transition same timestamp": {
fork: nil,
parent: NewUint64(100),
parent: utils.NewUint64(100),
current: 100,
transitioned: false,
},
"exact match on current timestamp": {
fork: NewUint64(100),
parent: NewUint64(99),
fork: utils.NewUint64(100),
parent: utils.NewUint64(99),
current: 100,
transitioned: true,
},
"current same as parent does not transition twice": {
fork: NewUint64(100),
parent: NewUint64(101),
fork: utils.NewUint64(100),
parent: utils.NewUint64(101),
current: 101,
transitioned: false,
},
"current, parent, and fork same should not transition twice": {
fork: NewUint64(100),
parent: NewUint64(100),
fork: utils.NewUint64(100),
parent: utils.NewUint64(100),
current: 100,
transitioned: false,
},
"current transitions after fork": {
fork: NewUint64(100),
parent: NewUint64(99),
fork: utils.NewUint64(100),
parent: utils.NewUint64(99),
current: 101,
transitioned: true,
},
"current and parent come after fork": {
fork: NewUint64(100),
parent: NewUint64(101),
fork: utils.NewUint64(100),
parent: utils.NewUint64(101),
current: 102,
transitioned: false,
},
Expand Down
6 changes: 3 additions & 3 deletions params/network_upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,19 +103,19 @@ func (n *NetworkUpgrades) Override(o *NetworkUpgrades) {
// IsSubnetEVM returns whether [time] represents a block
// with a timestamp after the SubnetEVM upgrade time.
func (n *NetworkUpgrades) IsSubnetEVM(time uint64) bool {
return utils.IsTimestampForked(n.SubnetEVMTimestamp, time)
return isTimestampForked(n.SubnetEVMTimestamp, time)
}

// IsDurango returns whether [time] represents a block
// with a timestamp after the Durango upgrade time.
func (n *NetworkUpgrades) IsDurango(time uint64) bool {
return utils.IsTimestampForked(n.DurangoTimestamp, time)
return isTimestampForked(n.DurangoTimestamp, time)
}

// IsEtna returns whether [time] represents a block
// with a timestamp after the Etna upgrade time.
func (n *NetworkUpgrades) IsEtna(time uint64) bool {
return utils.IsTimestampForked(n.EtnaTimestamp, time)
return isTimestampForked(n.EtnaTimestamp, time)
}

func (n *NetworkUpgrades) Description() string {
Expand Down
4 changes: 2 additions & 2 deletions params/precompile_upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,15 +170,15 @@ func (c *ChainConfig) GetActivatingPrecompileConfigs(address common.Address, fro
// First check the embedded [upgrade] for precompiles configured
// in the genesis chain config.
if config, ok := c.GenesisPrecompiles[key]; ok {
if utils.IsForkTransition(config.Timestamp(), from, to) {
if IsForkTransition(config.Timestamp(), from, to) {
configs = append(configs, config)
}
}
// Loop over all upgrades checking for the requested precompile config.
for _, upgrade := range upgrades {
if upgrade.Key() == key {
// Check if the precompile activates in the specified range.
if utils.IsForkTransition(upgrade.Timestamp(), from, to) {
if IsForkTransition(upgrade.Timestamp(), from, to) {
configs = append(configs, upgrade.Config)
}
}
Expand Down
3 changes: 1 addition & 2 deletions params/state_upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"
"reflect"

"github.com/ava-labs/subnet-evm/utils"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/common/math"
Expand Down Expand Up @@ -62,7 +61,7 @@ func (c *ChainConfig) verifyStateUpgrades() error {
func (c *ChainConfig) GetActivatingStateUpgrades(from *uint64, to uint64, upgrades []StateUpgrade) []StateUpgrade {
activating := make([]StateUpgrade, 0)
for _, upgrade := range upgrades {
if utils.IsForkTransition(upgrade.BlockTimestamp, from, to) {
if IsForkTransition(upgrade.BlockTimestamp, from, to) {
activating = append(activating, upgrade)
}
}
Expand Down
2 changes: 1 addition & 1 deletion plugin/evm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ func (vm *VM) Initialize(
g.Config.Override(overrides)
}

g.Config.SetEVMUpgrades(g.Config.NetworkUpgrades)
g.Config.SetEthUpgrades(g.Config.NetworkUpgrades)

if err := g.Verify(); err != nil {
return fmt.Errorf("failed to verify genesis: %w", err)
Expand Down
3 changes: 2 additions & 1 deletion plugin/evm/vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
commonEng "github.com/ava-labs/avalanchego/snow/engine/common"
"github.com/ava-labs/avalanchego/snow/engine/enginetest"
"github.com/ava-labs/avalanchego/snow/validators/validatorstest"
"github.com/ava-labs/avalanchego/upgrade"
avalancheConstants "github.com/ava-labs/avalanchego/utils/constants"
"github.com/ava-labs/avalanchego/utils/crypto/bls"
"github.com/ava-labs/avalanchego/utils/formatting"
Expand Down Expand Up @@ -2359,7 +2360,7 @@ func TestTxAllowListDisablePrecompile(t *testing.T) {
if err := genesis.UnmarshalJSON([]byte(genesisJSONSubnetEVM)); err != nil {
t.Fatal(err)
}
enableAllowListTimestamp := params.DefaultGenesisTime // enable at default genesis time
enableAllowListTimestamp := upgrade.InitiallyActiveTime // enable at initially active time
genesis.Config.GenesisPrecompiles = params.Precompiles{
txallowlist.ConfigKey: txallowlist.NewConfig(utils.TimeToNewUint64(enableAllowListTimestamp), testEthAddrs[0:1], nil, nil),
}
Expand Down
Loading

0 comments on commit face38b

Please sign in to comment.