Skip to content

Commit

Permalink
Merge pull request #1 from node-real/devel
Browse files Browse the repository at this point in the history
OPS-2955 Upgrade BSC erigon to v1.1.10
  • Loading branch information
MakarovSg authored Dec 14, 2023
2 parents 38ab5a4 + 7eb76f6 commit ef957f0
Show file tree
Hide file tree
Showing 34 changed files with 513 additions and 95 deletions.
2 changes: 1 addition & 1 deletion cmd/evm/internal/t8ntool/transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ func Main(ctx *cli.Context) error {
prestate.Env.Random = nil
}

if chainConfig.IsShanghai(prestate.Env.Timestamp) && prestate.Env.Withdrawals == nil {
if chainConfig.IsShanghai(prestate.Env.Number, prestate.Env.Timestamp) && prestate.Env.Withdrawals == nil {
return NewError(ErrorVMConfig, errors.New("Shanghai config but missing 'withdrawals' in env section"))
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/evm/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ import (
"github.com/ledgerwatch/erigon-lib/common/hexutility"
"github.com/ledgerwatch/erigon-lib/kv/kvcfg"
"github.com/ledgerwatch/erigon-lib/kv/memdb"
"github.com/ledgerwatch/erigon/cmd/utils"
"github.com/ledgerwatch/erigon/cmd/utils/flags"
"github.com/ledgerwatch/erigon/core/types"
"github.com/ledgerwatch/log/v3"
"github.com/urfave/cli/v2"

"github.com/ledgerwatch/erigon/cmd/evm/internal/compiler"
"github.com/ledgerwatch/erigon/cmd/utils"
"github.com/ledgerwatch/erigon/common"
"github.com/ledgerwatch/erigon/core"
"github.com/ledgerwatch/erigon/core/state"
Expand Down
2 changes: 1 addition & 1 deletion cmd/rpcdaemon/commands/eth_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (api *APIImpl) CallBundle(ctx context.Context, txHashes []common.Hash, stat
}
ibs := state.New(stateReader)

parent := rawdb.ReadHeader(tx, hash, stateBlockNumber)
parent, _ := api.headerByRPCNumber(rpc.BlockNumber(stateBlockNumber), tx)
if parent == nil {
return nil, fmt.Errorf("block %d(%x) not found", stateBlockNumber, hash)
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/rpcdaemon/commands/tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,7 @@ func (api *PrivateDebugAPIImpl) traceBlockDiff(ctx context.Context, blockNrOrHas
func (api *PrivateDebugAPIImpl) runBlockDiff(dbtx kv.Tx, engine consensus.Engine, ibs *state.IntraBlockState, txnWriter state.StateWriter, blockWriter state.StateWriter,
chainConfig *chain2.Config, getHeader func(hash common.Hash, number uint64) *types.Header, block *types.Block, vmConfig vm.Config, trace bool) (types.Receipts, error) {
header := block.Header()
parent := getHeader(header.ParentHash, header.Number.Uint64()-1)
excessDataGas := header.ParentExcessDataGas(getHeader)
vmConfig.TraceJumpDest = true
gp := new(core.GasPool).AddGas(block.GasLimit())
Expand All @@ -648,7 +649,7 @@ func (api *PrivateDebugAPIImpl) runBlockDiff(dbtx kv.Tx, engine consensus.Engine
if chainConfig.DAOForkBlock != nil && chainConfig.DAOForkBlock.Cmp(block.Number()) == 0 {
misc.ApplyDAOHardFork(ibs)
}
systemcontracts.UpgradeBuildInSystemContract(chainConfig, header.Number, ibs)
systemcontracts.UpgradeBuildInSystemContract(chainConfig, header.Number, parent.Time, header.Time, ibs)
rules := chainConfig.Rules(block.NumberU64(), block.Time())
posa, okPosa := engine.(consensus.PoSA)
//balanceDiff, balanceResult := *ibs.GetBalance(consensus.SystemAddress), new(uint256.Int)
Expand Down
2 changes: 1 addition & 1 deletion cmd/sentry/sentry/sentry_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func (cs *MultiClient) SendHeaderRequest(ctx context.Context, req *headerdownloa
Id: proto_sentry.MessageId_GET_BLOCK_HEADERS_66,
Data: bytes,
},
MaxPeers: 50,
MaxPeers: 5,
}
sentPeers, err1 := cs.sentries[i].SendMessageByMinBlock(ctx, &outreq, &grpc.EmptyCallOption{})
if err1 != nil {
Expand Down
3 changes: 2 additions & 1 deletion cmd/state/commands/opcode_tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,7 @@ func OpcodeTracer(genesis *types.Genesis, blockNum uint64, chaindata string, num
func runBlock(engine consensus.Engine, ibs *state.IntraBlockState, txnWriter state.StateWriter, blockWriter state.StateWriter,
chainConfig *chain2.Config, getHeader func(hash libcommon.Hash, number uint64) *types.Header, block *types.Block, vmConfig vm.Config, trace bool) (types.Receipts, error) {
header := block.Header()
parent := getHeader(header.ParentHash, header.Number.Uint64()-1)
excessDataGas := header.ParentExcessDataGas(getHeader)
vmConfig.TraceJumpDest = true
gp := new(core.GasPool).AddGas(block.GasLimit())
Expand All @@ -711,7 +712,7 @@ func runBlock(engine consensus.Engine, ibs *state.IntraBlockState, txnWriter sta
if chainConfig.DAOForkBlock != nil && chainConfig.DAOForkBlock.Cmp(block.Number()) == 0 {
misc.ApplyDAOHardFork(ibs)
}
systemcontracts.UpgradeBuildInSystemContract(chainConfig, header.Number, ibs)
systemcontracts.UpgradeBuildInSystemContract(chainConfig, header.Number, parent.Time, header.Time, ibs)
rules := chainConfig.Rules(block.NumberU64(), block.Time())
for i, tx := range block.Transactions() {
ibs.Prepare(tx.Hash(), block.Hash(), i)
Expand Down
3 changes: 2 additions & 1 deletion cmd/state/exec3/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ func (rw *Worker) RunTxTaskNoLock(txTask *exec22.TxTask) {
daoForkTx := rw.chainConfig.DAOForkBlock != nil && rw.chainConfig.DAOForkBlock.Uint64() == txTask.BlockNum && txTask.TxIndex == -1
var err error
header := txTask.Header
parent := rw.getHeader(header.ParentHash, header.Number.Uint64()-1)
if txTask.BlockNum == 0 && txTask.TxIndex == -1 {
//fmt.Printf("txNum=%d, blockNum=%d, Genesis\n", txTask.TxNum, txTask.BlockNum)
// Genesis block
Expand All @@ -157,7 +158,7 @@ func (rw *Worker) RunTxTaskNoLock(txTask *exec22.TxTask) {
// Block initialisation
//fmt.Printf("txNum=%d, blockNum=%d, initialisation of the block\n", txTask.TxNum, txTask.BlockNum)
if rw.isPoSA {
systemcontracts.UpgradeBuildInSystemContract(rw.chainConfig, header.Number, ibs)
systemcontracts.UpgradeBuildInSystemContract(rw.chainConfig, header.Number, parent.Time, header.Time, ibs)
}
syscall := func(contract libcommon.Address, data []byte) ([]byte, error) {
return core.SysCallContract(contract, data, *rw.chainConfig, ibs, header, rw.engine, false /* constCall */, nil /*excessDataGas*/)
Expand Down
3 changes: 2 additions & 1 deletion cmd/state/exec3/state_recon.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,8 @@ func (rw *ReconWorker) runTxTask(txTask *exec22.TxTask) error {
} else if txTask.TxIndex == -1 {
// Block initialisation
if rw.isPoSA {
systemcontracts.UpgradeBuildInSystemContract(rw.chainConfig, txTask.Header.Number, ibs)
parent := rw.chain.GetHeader(txTask.Header.ParentHash, txTask.Header.Number.Uint64()-1)
systemcontracts.UpgradeBuildInSystemContract(rw.chainConfig, txTask.Header.Number, parent.Time, txTask.Header.Time, ibs)
}
syscall := func(contract libcommon.Address, data []byte) ([]byte, error) {
return core.SysCallContract(contract, data, *rw.chainConfig, ibs, txTask.Header, rw.engine, false /* constCall */, nil /*excessDataGas*/)
Expand Down
25 changes: 21 additions & 4 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package utils

import (
"context"
"crypto/ecdsa"
"fmt"
"math/big"
Expand All @@ -39,6 +40,7 @@ import (
common2 "github.com/ledgerwatch/erigon/common"
"github.com/ledgerwatch/erigon/consensus/ethash/ethashcfg"
"github.com/ledgerwatch/erigon/eth/gasprice/gaspricecfg"
"github.com/ledgerwatch/erigon/node"
"github.com/ledgerwatch/log/v3"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
Expand Down Expand Up @@ -1568,15 +1570,30 @@ func SetEthConfig(ctx *cli.Context, nodeConfig *nodecfg.Config, cfg *ethconfig.C
default:
genesis := core.GenesisBlockByChainName(chain)
genesisHash := params.GenesisHashByChainName(chain)
if (genesis == nil) || (genesisHash == nil) {
Fatalf("ChainDB name is not recognized: %s", chain)
return
if (genesis == nil) || (genesisHash == nil) { // custom genesis
chainKv, err := node.OpenDatabase(nodeConfig, kv.ChainDB)
if err != nil {
panic(err)
}
err = chainKv.View(context.Background(), func(tx kv.Tx) error {
genesis, err = core.ReadGenesis(tx)
return err
})

if err != nil {
panic(err)
}
chainKv.Close()
}
cfg.Genesis = genesis
if !ctx.IsSet(NetworkIdFlag.Name) {
cfg.NetworkID = params.NetworkIDByChainName(chain)
}
SetDNSDiscoveryDefaults(cfg, *genesisHash)
if genesisHash != nil {
SetDNSDiscoveryDefaults(cfg, *genesisHash)
} else {
SetDNSDiscoveryDefaults(cfg, libcommon.Hash{})
}
case "":
if cfg.NetworkID == 1 {
SetDNSDiscoveryDefaults(cfg, params.MainnetGenesisHash)
Expand Down
47 changes: 45 additions & 2 deletions consensus/misc/eip1559.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ import (
"fmt"
"math/big"

"github.com/ledgerwatch/erigon-lib/chain"
"github.com/ledgerwatch/erigon-lib/common"

"github.com/ethereum/go-ethereum/common/math"
"github.com/ledgerwatch/erigon-lib/chain"
"github.com/ledgerwatch/erigon/core/types"
"github.com/ledgerwatch/erigon/params"
)
Expand All @@ -45,7 +47,48 @@ func VerifyEip1559Header(config *chain.Config, parent, header *types.Header) err

// CalcBaseFee calculates the basefee of the header.
func CalcBaseFee(config *chain.Config, parent *types.Header) *big.Int {
return new(big.Int).SetUint64(params.InitialBaseFee)
if config.Parlia != nil {
return new(big.Int).SetUint64(params.InitialBaseFee)
}
// If the current block is the first EIP-1559 block, return the InitialBaseFee.
if !config.IsLondon(parent.Number.Uint64()) {
return new(big.Int).SetUint64(params.InitialBaseFee)
}

var (
parentGasTarget = parent.GasLimit / params.ElasticityMultiplier
parentGasTargetBig = new(big.Int).SetUint64(parentGasTarget)
baseFeeChangeDenominator = new(big.Int).SetUint64(getBaseFeeChangeDenominator(config.Bor, parent.Number.Uint64()))
)
// If the parent gasUsed is the same as the target, the baseFee remains unchanged.
if parent.GasUsed == parentGasTarget {
return new(big.Int).Set(parent.BaseFee)
}

if parent.GasUsed > parentGasTarget {
// If the parent block used more gas than its target, the baseFee should increase.
// max(1, parentBaseFee * gasUsedDelta / parentGasTarget / baseFeeChangeDenominator)
gasUsedDelta := new(big.Int).SetUint64(parent.GasUsed - parentGasTarget)
x := new(big.Int).Mul(parent.BaseFee, gasUsedDelta)
y := x.Div(x, parentGasTargetBig)
baseFeeDelta := math.BigMax(
x.Div(y, baseFeeChangeDenominator),
common.Big1,
)
return x.Add(parent.BaseFee, baseFeeDelta)
} else {
// Otherwise if the parent block used less gas than its target, the baseFee should decrease.
// max(0, parentBaseFee * gasUsedDelta / parentGasTarget / baseFeeChangeDenominator)
gasUsedDelta := new(big.Int).SetUint64(parentGasTarget - parent.GasUsed)
x := new(big.Int).Mul(parent.BaseFee, gasUsedDelta)
y := x.Div(x, parentGasTargetBig)
baseFeeDelta := x.Div(y, baseFeeChangeDenominator)

return math.BigMax(
x.Sub(parent.BaseFee, baseFeeDelta),
common.Big0,
)
}
}

func getBaseFeeChangeDenominator(borConfig *chain.BorConfig, number uint64) uint64 {
Expand Down
28 changes: 23 additions & 5 deletions consensus/parlia/parlia.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ import (
)

const (
inMemorySnapshots = 128 // Number of recent snapshots to keep in memory
inMemorySnapshots = 256 // Number of recent snapshots to keep in memory
inMemorySignatures = 4096 // Number of recent block signatures to keep in memory

CheckpointInterval = 1024 // Number of blocks after which to save the snapshot to the database
Expand Down Expand Up @@ -707,6 +707,13 @@ func (p *Parlia) prepareValidators(header *types.Header, chain consensus.ChainHe
}
} else {
header.Extra = append(header.Extra, byte(len(newValidators)))
if p.chainConfig.IsOnLuban(header.Number) {
voteAddressMap = make(map[libcommon.Address]*types.BLSPublicKey, len(newValidators))
var zeroBlsKey types.BLSPublicKey
for _, validator := range newValidators {
voteAddressMap[validator] = &zeroBlsKey
}
}
for _, validator := range newValidators {
header.Extra = append(header.Extra, validator.Bytes()...)
header.Extra = append(header.Extra, voteAddressMap[validator].Bytes()...)
Expand Down Expand Up @@ -750,7 +757,7 @@ func (p *Parlia) snapshot(chain consensus.ChainHeaderReader, number uint64, hash
}
}
}
if number == 0 || (number%p.config.Epoch == 0 && (len(headers) > params.FullImmutabilityThreshold)) {
if number == 0 || (number%p.config.Epoch == 0 && (len(headers) > params.FullImmutabilityThreshold/10)) {
// Headers included into the snapshots have to be trusted as checkpoints
checkpoint := chain.GetHeader(hash, number)
if checkpoint != nil {
Expand All @@ -761,8 +768,11 @@ func (p *Parlia) snapshot(chain consensus.ChainHeaderReader, number uint64, hash
}
// new snapshot
snap = newSnapshot(p.config, p.signatures, number, hash, validators, voteAddrs)
if err := snap.store(p.db); err != nil {
return nil, err
if snap.Number%CheckpointInterval == 0 { // snapshot will only be loaded when snap.Number%checkpointInterval == 0
if err := snap.store(p.db); err != nil {
return nil, err
}
log.Info("Stored checkpoint snapshot to disk", "number", number, "hash", hash)
}
break
}
Expand Down Expand Up @@ -899,6 +909,13 @@ func (p *Parlia) verifyValidators(header, parentHeader *types.Header, state *sta
return errMismatchingEpochValidators
}
validatorsBytes = make([]byte, validatorsNumber*validatorBytesLength)
if p.chainConfig.IsOnLuban(header.Number) {
voteAddressMap = make(map[libcommon.Address]*types.BLSPublicKey, len(newValidators))
var zeroBlsKey types.BLSPublicKey
for _, validator := range newValidators {
voteAddressMap[validator] = &zeroBlsKey
}
}
for i, validator := range newValidators {
copy(validatorsBytes[i*validatorBytesLength:], validator.Bytes())
copy(validatorsBytes[i*validatorBytesLength+length.Addr:], voteAddressMap[validator].Bytes())
Expand Down Expand Up @@ -1395,7 +1412,8 @@ func (p *Parlia) distributeIncoming(val libcommon.Address, state *state.IntraBlo
state.SetBalance(consensus.SystemAddress, u256.Num0)
state.AddBalance(coinbase, balance)

doDistributeSysReward := state.GetBalance(systemcontracts.SystemRewardContract).Cmp(maxSystemBalance) < 0
doDistributeSysReward := !p.chainConfig.IsKepler(header.Number.Uint64(), header.Time) &&
state.GetBalance(systemcontracts.SystemRewardContract).Cmp(maxSystemBalance) < 0
if doDistributeSysReward {
var rewards = new(uint256.Int)
rewards = rewards.Rsh(balance, systemRewardPercent)
Expand Down
3 changes: 3 additions & 0 deletions consensus/parlia/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,9 @@ func (s *Snapshot) updateAttestation(header *types.Header, chainConfig *chain.Co
}

// Update attestation
// Two scenarios for s.Attestation being nil:
// 1) The first attestation is assembled.
// 2) The snapshot on disk is missing, prompting the creation of a new snapshot using `newSnapshot`.
if s.Attestation != nil && attestation.Data.SourceNumber+1 != attestation.Data.TargetNumber {
s.Attestation.TargetNumber = attestation.Data.TargetNumber
s.Attestation.TargetHash = attestation.Data.TargetHash
Expand Down
2 changes: 1 addition & 1 deletion consensus/serenity/serenity.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ func (s *Serenity) verifyHeader(chain consensus.ChainHeaderReader, header, paren
}

// Verify existence / non-existence of withdrawalsHash
shanghai := chain.Config().IsShanghai(header.Time)
shanghai := chain.Config().IsShanghai(header.Number.Uint64(), header.Time)
if shanghai && header.WithdrawalsHash == nil {
return fmt.Errorf("missing withdrawalsHash")
}
Expand Down
3 changes: 2 additions & 1 deletion core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ func ExecuteBlockEphemerallyForBSC(
block.Uncles()
ibs := state.New(stateReader)
header := block.Header()
parent := chainReader.GetHeader(header.ParentHash, header.Number.Uint64()-1)
usedGas := new(uint64)
gp := new(GasPool)
gp.AddGas(block.GasLimit())
Expand All @@ -107,7 +108,7 @@ func ExecuteBlockEphemerallyForBSC(
if chainConfig.DAOForkBlock != nil && chainConfig.DAOForkBlock.Cmp(block.Number()) == 0 {
misc.ApplyDAOHardFork(ibs)
}
systemcontracts.UpgradeBuildInSystemContract(chainConfig, header.Number, ibs)
systemcontracts.UpgradeBuildInSystemContract(chainConfig, header.Number, parent.Time, header.Time, ibs)
noop := state.NewNoopWriter()
posa, isPoSA := engine.(consensus.PoSA)
//fmt.Printf("====txs processing start: %d====\n", block.NumberU64())
Expand Down
2 changes: 1 addition & 1 deletion core/chain_makers.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ func GenerateChain(config *chain.Config, parent *types.Block, engine consensus.E
misc.ApplyDAOHardFork(ibs)
}
}
systemcontracts.UpgradeBuildInSystemContract(config, b.header.Number, ibs)
systemcontracts.UpgradeBuildInSystemContract(config, b.header.Number, b.parent.Time(), b.header.Time, ibs)
// Execute any user modifications to the block
if gen != nil {
gen(i, b)
Expand Down
Loading

0 comments on commit ef957f0

Please sign in to comment.