Skip to content
This repository was archived by the owner on Dec 4, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -1413,7 +1413,7 @@ func (b *Blockchain) Close() error {

// CalculateBaseFee calculates the basefee of the header.
func (b *Blockchain) CalculateBaseFee(parent *types.Header) uint64 {
if !b.config.Params.Forks.IsLondon(parent.Number) {
if !b.config.Params.Forks.IsActive(chain.London, parent.Number) {
return chain.GenesisBaseFee
}

Expand Down
3 changes: 1 addition & 2 deletions blockchain/blockchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1365,12 +1365,11 @@ func TestBlockchain_CalculateBaseFee(t *testing.T) {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
t.Parallel()

fork := chain.Fork(5)
blockchain := Blockchain{
config: &chain.Chain{
Params: &chain.Params{
Forks: &chain.Forks{
London: &fork,
chain.London: chain.NewFork(5),
},
},
Genesis: &chain.Genesis{
Expand Down
9 changes: 5 additions & 4 deletions blockchain/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,14 @@ func NewTestBlockchain(t *testing.T, headers []*types.Header) *Blockchain {
Number: 0,
GasLimit: 0,
}
forksAvail := &chain.Forks{
chain.EIP155: chain.NewFork(0),
chain.Homestead: chain.NewFork(0),
}
config := &chain.Chain{
Genesis: genesis,
Params: &chain.Params{
Forks: &chain.Forks{
EIP155: chain.NewFork(0),
Homestead: chain.NewFork(0),
},
Forks: forksAvail,
BlockGasTarget: defaultBlockGasTarget,
},
}
Expand Down
93 changes: 38 additions & 55 deletions chain/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,70 +74,43 @@ func (p *Params) GetEngine() string {
return ""
}

// Forks specifies when each fork is activated
type Forks struct {
Homestead *Fork `json:"homestead,omitempty"`
Byzantium *Fork `json:"byzantium,omitempty"`
Constantinople *Fork `json:"constantinople,omitempty"`
Petersburg *Fork `json:"petersburg,omitempty"`
Istanbul *Fork `json:"istanbul,omitempty"`
London *Fork `json:"london,omitempty"`
EIP150 *Fork `json:"EIP150,omitempty"`
EIP158 *Fork `json:"EIP158,omitempty"`
EIP155 *Fork `json:"EIP155,omitempty"`
}

func (f *Forks) active(ff *Fork, block uint64) bool {
if ff == nil {
return false
}

return ff.Active(block)
}

func (f *Forks) IsHomestead(block uint64) bool {
return f.active(f.Homestead, block)
}

func (f *Forks) IsByzantium(block uint64) bool {
return f.active(f.Byzantium, block)
}

func (f *Forks) IsConstantinople(block uint64) bool {
return f.active(f.Constantinople, block)
}
// predefined forks
const (
Homestead = "homestead"
Byzantium = "byzantium"
Constantinople = "constantinople"
Petersburg = "petersburg"
Istanbul = "istanbul"
London = "london"
EIP150 = "EIP150"
EIP158 = "EIP158"
EIP155 = "EIP155"
)

func (f *Forks) IsPetersburg(block uint64) bool {
return f.active(f.Petersburg, block)
}
// Forks is map which contains all forks and their starting blocks from genesis
type Forks map[string]*Fork

func (f *Forks) IsLondon(block uint64) bool {
return f.active(f.London, block)
}
func (f *Forks) IsActive(name string, block uint64) bool {
ff := (*f)[name]

func (f *Forks) IsEIP150(block uint64) bool {
return f.active(f.EIP150, block)
return ff != nil && ff.Active(block)
}

func (f *Forks) IsEIP158(block uint64) bool {
return f.active(f.EIP158, block)
}

func (f *Forks) IsEIP155(block uint64) bool {
return f.active(f.EIP155, block)
func (f *Forks) SetFork(name string, value *Fork) {
(*f)[name] = value
}

func (f *Forks) At(block uint64) ForksInTime {
return ForksInTime{
Homestead: f.active(f.Homestead, block),
Byzantium: f.active(f.Byzantium, block),
Constantinople: f.active(f.Constantinople, block),
Petersburg: f.active(f.Petersburg, block),
Istanbul: f.active(f.Istanbul, block),
London: f.active(f.London, block),
EIP150: f.active(f.EIP150, block),
EIP158: f.active(f.EIP158, block),
EIP155: f.active(f.EIP155, block),
Homestead: f.IsActive(Homestead, block),
Byzantium: f.IsActive(Byzantium, block),
Constantinople: f.IsActive(Constantinople, block),
Petersburg: f.IsActive(Petersburg, block),
Istanbul: f.IsActive(Istanbul, block),
London: f.IsActive(London, block),
EIP150: f.IsActive(EIP150, block),
EIP158: f.IsActive(EIP158, block),
EIP155: f.IsActive(EIP155, block),
}
}

Expand All @@ -157,6 +130,7 @@ func (f Fork) Int() *big.Int {
return big.NewInt(int64(f))
}

// ForksInTime should contain all supported forks by current edge version
type ForksInTime struct {
Homestead,
Byzantium,
Expand All @@ -169,6 +143,8 @@ type ForksInTime struct {
EIP155 bool
}

// AllForksEnabled should contain all supported forks by current edge version
// All forks should be available from block 0
var AllForksEnabled = &Forks{
Homestead: NewFork(0),
EIP150: NewFork(0),
Expand All @@ -180,3 +156,10 @@ var AllForksEnabled = &Forks{
Istanbul: NewFork(0),
London: NewFork(0),
}

// IsForkAvailable returns true if some fork defined by its name is supported by current edge version
func IsForkAvailable(name string) bool {
_, found := (*AllForksEnabled)[name]

return found
}
2 changes: 1 addition & 1 deletion command/genesis/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ func (p *genesisParams) initGenesisConfig() error {
// Disable london hardfork if burn contract address is not provided
enabledForks := chain.AllForksEnabled
if len(p.burnContracts) == 0 {
enabledForks.London = nil
enabledForks.SetFork(chain.London, nil)
}

chainConfig := &chain.Chain{
Expand Down
2 changes: 1 addition & 1 deletion command/genesis/polybft_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func (p *genesisParams) generatePolyBftChainConfig(o command.OutputFormatter) er
// Disable london hardfork if burn contract address is not provided
enabledForks := chain.AllForksEnabled
if len(p.burnContracts) == 0 {
enabledForks.London = nil
enabledForks.SetFork(chain.London, nil)
}

chainConfig := &chain.Chain{
Expand Down
3 changes: 3 additions & 0 deletions consensus/polybft/consensus_runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/0xPolygon/polygon-edge/consensus/polybft/validator"
"github.com/0xPolygon/polygon-edge/consensus/polybft/wallet"
"github.com/0xPolygon/polygon-edge/contracts"
"github.com/0xPolygon/polygon-edge/forkmanager"
"github.com/0xPolygon/polygon-edge/txrelayer"
"github.com/0xPolygon/polygon-edge/types"

Expand Down Expand Up @@ -270,6 +271,8 @@ func (c *consensusRuntime) OnBlockInserted(fullBlock *types.FullBlock) {
c.lock.Lock()
defer c.lock.Unlock()

forkmanager.GetInstance().SetCurrentBlock(fullBlock.Block.Number() + 1)

if c.lastBuiltBlock != nil && c.lastBuiltBlock.Number >= fullBlock.Block.Number() {
c.logger.Debug("on block inserted already handled",
"current", c.lastBuiltBlock.Number, "block", fullBlock.Block.Number())
Expand Down
21 changes: 21 additions & 0 deletions consensus/polybft/polybft.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/0xPolygon/polygon-edge/consensus/polybft/validator"
"github.com/0xPolygon/polygon-edge/consensus/polybft/wallet"
"github.com/0xPolygon/polygon-edge/contracts"
"github.com/0xPolygon/polygon-edge/forkmanager"
"github.com/0xPolygon/polygon-edge/helper/common"
"github.com/0xPolygon/polygon-edge/helper/progress"
"github.com/0xPolygon/polygon-edge/network"
Expand Down Expand Up @@ -361,6 +362,26 @@ func GenesisPostHookFactory(config *chain.Chain, engineName string) func(txn *st
}
}

func ForkManagerFactory(forks *chain.Forks) error {
Comment thread
vcastellm marked this conversation as resolved.
fm := forkmanager.GetInstance()

for name, block := range *forks {
Comment thread
igorcrevar marked this conversation as resolved.
Outdated
if !chain.IsForkAvailable(name) {
return fmt.Errorf("fork is not available: %s", name)
}

fm.RegisterFork(forkmanager.ForkName(name))

if block != nil {
if err := fm.ActivateFork(forkmanager.ForkName(name), (uint64)(*block)); err != nil {
return err
}
}
}

return nil
}

// Initialize initializes the consensus (e.g. setup data)
func (p *Polybft) Initialize() error {
p.logger.Info("initializing polybft...")
Expand Down
17 changes: 17 additions & 0 deletions forkmanager/fork.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package forkmanager

type ForkHandlerName string

type ForkName string
Comment thread
Stefan-Ethernal marked this conversation as resolved.
Outdated

type Fork struct {
Name ForkName
FromBlockNumber uint64
IsActive bool
Comment thread
Nemanja0x marked this conversation as resolved.
Outdated
Handlers map[ForkHandlerName]interface{}
}

type ForkActiveHandler struct {
FromBlockNumber uint64
Handler interface{}
}
Loading