Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support of BEP-440 #530

Merged
merged 4 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
21 changes: 11 additions & 10 deletions cmd/state/exec3/state_recon.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ import (
"context"
"encoding/binary"
"fmt"
"github.com/erigontech/erigon/core/systemcontracts"
"sync"

"github.com/erigontech/erigon/core/systemcontracts"

"github.com/erigontech/erigon-lib/common/datadir"

"github.com/RoaringBitmap/roaring/roaring64"
Expand Down Expand Up @@ -344,24 +345,24 @@ func (rw *ReconWorker) runTxTask(txTask *state.TxTask) error {
}
} else if txTask.TxIndex == -1 {
// Block initialisation
if rw.isPoSA && !rw.chainConfig.IsFeynman(header.Number.Uint64(), header.Time) {
lastBlockTime := header.Time - rw.chainConfig.Parlia.Period
parent := rw.chain.GetHeaderByHash(header.ParentHash)
if parent != nil {
lastBlockTime = parent.Time
}
systemcontracts.UpgradeBuildInSystemContract(rw.chainConfig, header.Number, lastBlockTime, header.Time, ibs, rw.logger)
}

syscall := func(contract libcommon.Address, data []byte, ibState *state.IntraBlockState, header *types.Header, constCall bool) ([]byte, error) {
return core.SysCallContract(contract, data, rw.chainConfig, ibState, header, rw.engine, constCall /* constCall */)
}

rw.engine.Initialize(rw.chainConfig, rw.chain, header, ibs, syscall, rw.logger, nil)
if err = ibs.FinalizeTx(rules, noop); err != nil {
if _, readError := rw.stateReader.ReadError(); !readError {
return err
}
}
if rw.isPoSA && !rw.chainConfig.IsFeynman(header.Number.Uint64(), header.Time) {
lastBlockTime := header.Time - rw.chainConfig.Parlia.Period
parent := rw.chain.GetHeaderByHash(header.ParentHash)
if parent != nil {
lastBlockTime = parent.Time
}
systemcontracts.UpgradeBuildInSystemContract(rw.chainConfig, header.Number, lastBlockTime, header.Time, ibs, rw.logger)
}
} else {
if rw.isPoSA {
if isSystemTx, err := rw.posa.IsSystemTransaction(txTask.Tx, txTask.Header); err != nil {
Expand Down
4 changes: 1 addition & 3 deletions consensus/misc/eip2935.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,15 @@ package misc
import (
"github.com/holiman/uint256"

"github.com/erigontech/erigon-lib/chain"
libcommon "github.com/erigontech/erigon-lib/common"
"github.com/erigontech/erigon-lib/log/v3"

"github.com/erigontech/erigon/consensus"
"github.com/erigontech/erigon/core/state"
"github.com/erigontech/erigon/core/types"
"github.com/erigontech/erigon/params"
)

func StoreBlockHashesEip2935(header *types.Header, state *state.IntraBlockState, config *chain.Config, headerReader consensus.ChainHeaderReader) {
func StoreBlockHashesEip2935(header *types.Header, state *state.IntraBlockState) {
if state.GetCodeSize(params.HistoryStorageAddress) == 0 {
log.Debug("[EIP-2935] No code deployed to HistoryStorageAddress before call to store EIP-2935 history")
return
Expand Down
13 changes: 10 additions & 3 deletions consensus/parlia/parlia.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@ import (
"encoding/hex"
"errors"
"fmt"
"github.com/erigontech/erigon/consensus/parlia/finality"
"github.com/erigontech/erigon/core/tracing"
"github.com/erigontech/erigon/core/vm/evmtypes"
"io"
"math/big"
"sort"
"strings"
"sync"
"time"

"github.com/erigontech/erigon/consensus/parlia/finality"
"github.com/erigontech/erigon/core/tracing"
"github.com/erigontech/erigon/core/vm/evmtypes"

"github.com/erigontech/erigon/crypto/cryptopool"
"github.com/erigontech/erigon/turbo/services"

Expand Down Expand Up @@ -911,6 +912,12 @@ func (p *Parlia) Initialize(config *chain.Config, chain consensus.ChainHeaderRea
if err = p.verifyTurnLength(chain, header, state); err != nil {
return err
}

// store block hashes for EIP-2935 (BEP440) upgrade
if config.IsPrague(header.Time) {
misc.StoreBlockHashesEip2935(header, state)
}

// update validators every day
if p.chainConfig.IsFeynman(header.Number.Uint64(), header.Time) && isBreatheBlock(parentHeader.Time, header.Time) {
// we should avoid update validators in the Feynman upgrade block
Expand Down
10 changes: 6 additions & 4 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ import (
"cmp"
"encoding/json"
"fmt"
"github.com/erigontech/erigon/core/systemcontracts"
"reflect"
"slices"
"time"

"github.com/erigontech/erigon/core/systemcontracts"

"golang.org/x/crypto/sha3"

"github.com/erigontech/erigon-lib/chain"
Expand Down Expand Up @@ -503,13 +504,14 @@ func FinalizeBlockExecution(
func InitializeBlockExecution(engine consensus.Engine, chain consensus.ChainHeaderReader, header, parent *types.Header,
cc *chain.Config, ibs *state.IntraBlockState, logger log.Logger, tracer *tracing.Hooks,
) error {
engine.Initialize(cc, chain, header, ibs, func(contract libcommon.Address, data []byte, ibState *state.IntraBlockState, header *types.Header, constCall bool) ([]byte, error) {
return SysCallContract(contract, data, cc, ibState, header, engine, constCall)
}, logger, tracer)
if !cc.IsFeynman(header.Number.Uint64(), header.Time) {
systemcontracts.UpgradeBuildInSystemContract(cc, header.Number, parent.Time, header.Time, ibs, logger)
}

engine.Initialize(cc, chain, header, ibs, func(contract libcommon.Address, data []byte, ibState *state.IntraBlockState, header *types.Header, constCall bool) ([]byte, error) {
return SysCallContract(contract, data, cc, ibState, header, engine, constCall)
}, logger, tracer)

noop := state.NewNoopWriter()
ibs.FinalizeTx(cc.Rules(header.Number.Uint64(), header.Time), noop)
return nil
Expand Down
5 changes: 3 additions & 2 deletions core/systemcontracts/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ package systemcontracts

import (
"fmt"
"math/big"
"strconv"

"github.com/erigontech/erigon-lib/chain"
libcommon "github.com/erigontech/erigon-lib/common"
"github.com/erigontech/erigon-lib/log/v3"
"github.com/erigontech/erigon/core/state"
"github.com/erigontech/erigon/core/types"
"math/big"
"strconv"
)

type UpgradeConfig struct {
Expand Down
10 changes: 5 additions & 5 deletions erigon-lib/chain/chain_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ func (c *Config) IsHaber(num uint64, time uint64) bool {
return c.IsLondon(num) && isForked(c.HaberTime, time)
}

// IsHaber returns whether time is either equal to the Haber fork time or greater.
// IsHaber returns whether time is either equal to the HaberFix fork time or greater.
func (c *Config) IsHaberFix(num uint64, time uint64) bool {
return c.IsLondon(num) && isForked(c.HaberFixTime, time)
}
Expand All @@ -513,12 +513,12 @@ func (c *Config) IsOnHaberFix(currentBlockNumber *big.Int, lastBlockTime uint64,
return !c.IsHaberFix(lastBlockNumber.Uint64(), lastBlockTime) && c.IsHaberFix(currentBlockNumber.Uint64(), currentBlockTime)
}

// IsBohr returns whether time is either equal to the Haber fork time or greater.
// IsBohr returns whether time is either equal to the Bohr fork time or greater.
func (c *Config) IsBohr(num uint64, time uint64) bool {
return c.IsLondon(num) && isForked(c.BohrTime, time)
}

// IsOnBohr returns whether currentBlockTime is either equal to the HaberFix fork time or greater firstly.
// IsOnBohr returns whether currentBlockTime is either equal to the Bohr fork time or greater firstly.
func (c *Config) IsOnBohr(currentBlockNumber *big.Int, lastBlockTime uint64, currentBlockTime uint64) bool {
lastBlockNumber := new(big.Int)
if currentBlockNumber.Cmp(big.NewInt(1)) >= 0 {
Expand All @@ -527,12 +527,12 @@ func (c *Config) IsOnBohr(currentBlockNumber *big.Int, lastBlockTime uint64, cur
return !c.IsBohr(lastBlockNumber.Uint64(), lastBlockTime) && c.IsBohr(currentBlockNumber.Uint64(), currentBlockTime)
}

// IsPascal returns whether time is either equal to the Haber fork time or greater.
// IsPascal returns whether time is either equal to the Pascal fork time or greater.
func (c *Config) IsPascal(num uint64, time uint64) bool {
return c.IsLondon(num) && isForked(c.PascalTime, time)
}

// IsOnPascal returns whether currentBlockTime is either equal to the HaberFix fork time or greater firstly.
// IsOnPascal returns whether currentBlockTime is either equal to the Pascal fork time or greater firstly.
func (c *Config) IsOnPascal(currentBlockNumber *big.Int, lastBlockTime uint64, currentBlockTime uint64) bool {
lastBlockNumber := new(big.Int)
if currentBlockNumber.Cmp(big.NewInt(1)) >= 0 {
Expand Down
8 changes: 7 additions & 1 deletion params/chainspecs/bsc.json

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion params/chainspecs/chapel.json

Large diffs are not rendered by default.