Skip to content
This repository has been archived by the owner on Apr 10, 2024. It is now read-only.

Commit

Permalink
Merge pull request #3 from binance-chain/bsc-validator-support
Browse files Browse the repository at this point in the history
Validator support for BSC
  • Loading branch information
dmitry123 committed Dec 17, 2021
2 parents 6ebcb28 + 7b07e99 commit 3245254
Show file tree
Hide file tree
Showing 19 changed files with 358 additions and 157 deletions.
5 changes: 3 additions & 2 deletions cmd/hack/hack.go
Original file line number Diff line number Diff line change
Expand Up @@ -3774,10 +3774,11 @@ func runBlock(ibs *state.IntraBlockState, txnWriter state.StateWriter, blockWrit

if !vmConfig.ReadOnly {
// Finalize the block, applying any consensus engine specific extras (e.g. block rewards)
if _, err := engine.FinalizeAndAssemble(chainConfig, header, ibs, block.Transactions(), block.Uncles(), receipts, nil, nil, nil, nil); err != nil {
txs := block.Transactions()
if _, err := engine.FinalizeAndAssemble(chainConfig, header, ibs, &txs, block.Uncles(), &receipts, nil, nil, nil, nil); err != nil {
return nil, fmt.Errorf("finalize of block %d failed: %w", block.NumberU64(), err)
}

block = types.NewBlock(block.Header(), txs, block.Uncles(), receipts)
if err := ibs.CommitBlock(rules, blockWriter); err != nil {
return nil, fmt.Errorf("committing block %d failed: %w", block.NumberU64(), err)
}
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 @@ -698,7 +698,8 @@ func runBlock(ibs *state.IntraBlockState, txnWriter state.StateWriter, blockWrit

if !vmConfig.ReadOnly {
// Finalize the block, applying any consensus engine specific extras (e.g. block rewards)
if _, err := engine.FinalizeAndAssemble(chainConfig, header, ibs, block.Transactions(), block.Uncles(), receipts, nil, nil, nil, nil); err != nil {
tx := block.Transactions()
if _, err := engine.FinalizeAndAssemble(chainConfig, header, ibs, &tx, block.Uncles(), &receipts, nil, nil, nil, nil); err != nil {
return nil, fmt.Errorf("finalize of block %d failed: %w", block.NumberU64(), err)
}

Expand Down
46 changes: 30 additions & 16 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,14 @@ import (

"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon-lib/txpool"
"github.com/ledgerwatch/erigon/eth/protocols/eth"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/urfave/cli"

"github.com/ledgerwatch/erigon/eth/protocols/eth"

"github.com/ledgerwatch/log/v3"

"github.com/ledgerwatch/erigon/common"
"github.com/ledgerwatch/erigon/common/paths"
"github.com/ledgerwatch/erigon/consensus/ethash"
Expand All @@ -52,7 +55,6 @@ import (
"github.com/ledgerwatch/erigon/p2p/nat"
"github.com/ledgerwatch/erigon/p2p/netutil"
"github.com/ledgerwatch/erigon/params"
"github.com/ledgerwatch/log/v3"
)

func init() {
Expand Down Expand Up @@ -595,12 +597,14 @@ func setNodeUserIdentCobra(f *pflag.FlagSet, cfg *node.Config) {
// setBootstrapNodes creates a list of bootstrap nodes from the command line
// flags, reverting to pre-configured ones if none have been specified.
func setBootstrapNodes(ctx *cli.Context, cfg *p2p.Config) {
urls := params.MainnetBootnodes
var urls []string
if ctx.GlobalIsSet(BootnodesFlag.Name) {
urls = SplitAndTrim(ctx.GlobalString(BootnodesFlag.Name))
} else {
chain := ctx.GlobalString(ChainFlag.Name)
switch chain {
case params.MainnetChainName:
urls = params.MainnetBootnodes
case params.RopstenChainName:
urls = params.RopstenBootnodes
case params.RinkebyChainName:
Expand Down Expand Up @@ -634,13 +638,14 @@ func setBootstrapNodes(ctx *cli.Context, cfg *p2p.Config) {
// setBootstrapNodesV5 creates a list of bootstrap nodes from the command line
// flags, reverting to pre-configured ones if none have been specified.
func setBootstrapNodesV5(ctx *cli.Context, cfg *p2p.Config) {
urls := params.MainnetBootnodes
var urls []string
if ctx.GlobalIsSet(BootnodesFlag.Name) {
urls = SplitAndTrim(ctx.GlobalString(BootnodesFlag.Name))
} else {

chain := ctx.GlobalString(ChainFlag.Name)
switch chain {
case params.MainnetChainName:
urls = params.MainnetBootnodes
case params.RopstenChainName:
urls = params.RopstenBootnodes
case params.RinkebyChainName:
Expand Down Expand Up @@ -855,9 +860,15 @@ func setEtherbase(ctx *cli.Context, cfg *ethconfig.Config) {
setSigKey(ctx, cfg)
}

if ctx.GlobalString(ChainFlag.Name) == params.FermionChainName {
chainsWithValidatorMode := map[string]bool{
params.FermionChainName: true,
params.BSCChainName: true,
params.RialtoChainName: true,
params.ChapelChainName: true,
}
if _, ok := chainsWithValidatorMode[ctx.GlobalString(ChainFlag.Name)]; ok {
if ctx.GlobalIsSet(MiningEnabledFlag.Name) && !ctx.GlobalIsSet(MinerSigningKeyFileFlag.Name) {
panic(fmt.Sprintf("Flag --%s is required in %s chain with --%s flag", MinerSigningKeyFileFlag.Name, params.FermionChainName, MiningEnabledFlag.Name))
panic(fmt.Sprintf("Flag --%s is required in %s chain with --%s flag", MinerSigningKeyFileFlag.Name, ChainFlag.Name, MiningEnabledFlag.Name))
}
setSigKey(ctx, cfg)
if cfg.Miner.SigKey != nil {
Expand Down Expand Up @@ -902,13 +913,13 @@ func SetP2PConfig(ctx *cli.Context, cfg *p2p.Config, nodeName, dataDir string) {
cfg.NetRestrict = list
}

if ctx.GlobalString(ChainFlag.Name) == params.DevChainName {
// --dev mode can't use p2p networking.
// cfg.MaxPeers = 0 // It can have peers otherwise local sync is not possible
cfg.ListenAddr = ":0"
cfg.NoDiscovery = true
cfg.DiscoveryV5 = false
}
//if ctx.GlobalString(ChainFlag.Name) == params.DevChainName {
// --dev mode can't use p2p networking.
// cfg.MaxPeers = 0 // It can have peers otherwise local sync is not possible
//cfg.ListenAddr = ":0"
//cfg.NoDiscovery = true
//cfg.DiscoveryV5 = false
//}
}

// SetNodeConfig applies node-related command line flags to the config.
Expand Down Expand Up @@ -1369,8 +1380,11 @@ func SetEthConfig(ctx *cli.Context, nodeConfig *node.Config, cfg *ethconfig.Conf
log.Info("Using developer account", "address", developer)

// Create a new developer genesis block or reuse existing one
cfg.Genesis = core.DeveloperGenesisBlock(uint64(ctx.GlobalInt(DeveloperPeriodFlag.Name)), developer)
log.Info("Using custom developer period", "seconds", cfg.Genesis.Config.Clique.Period)
cfg.Genesis = core.DefaultChapelGenesisBlock()
extraData := make([]byte, 32+20+65)
copy(extraData[32:], developer[:])
cfg.Genesis.ExtraData = extraData
log.Info("Using custom developer period", "seconds", cfg.Genesis.Config.Parlia.Period)
if !ctx.GlobalIsSet(MinerGasPriceFlag.Name) {
cfg.Miner.GasPrice = big.NewInt(1)
}
Expand Down
16 changes: 8 additions & 8 deletions consensus/aura/aura.go
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,7 @@ func (c *AuRa) VerifySeal(chain consensus.ChainHeaderReader, header *types.Heade

// Prepare implements consensus.Engine, preparing all the consensus fields of the
// header for running the transactions on top.
func (c *AuRa) Prepare(chain consensus.ChainHeaderReader, header *types.Header) error {
func (c *AuRa) Prepare(chain consensus.ChainHeaderReader, header *types.Header, state *state.IntraBlockState) error {
return nil
/// If the block isn't a checkpoint, cast a random vote (good enough for now)
//header.Coinbase = common.Address{}
Expand Down Expand Up @@ -833,7 +833,7 @@ func (c *AuRa) Initialize(config *params.ChainConfig, chain consensus.ChainHeade
}

//word `signal epoch` == word `pending epoch`
func (c *AuRa) Finalize(config *params.ChainConfig, header *types.Header, state *state.IntraBlockState, txs []types.Transaction, uncles []*types.Header, r types.Receipts, e consensus.EpochReader, chain consensus.ChainHeaderReader, syscall consensus.SystemCall) error {
func (c *AuRa) Finalize(config *params.ChainConfig, header *types.Header, state *state.IntraBlockState, _ *types.Transactions, uncles []*types.Header, receipts *types.Receipts, e consensus.EpochReader, chain consensus.ChainHeaderReader, syscall consensus.SystemCall) error {
// accumulateRewards retrieves rewards for a block and applies them to the coinbase accounts for miner and uncle miners
beneficiaries, _, rewards, err := AccumulateRewards(config, c, header, uncles, syscall)
if err != nil {
Expand All @@ -846,15 +846,15 @@ func (c *AuRa) Finalize(config *params.ChainConfig, header *types.Header, state

// check_and_lock_block -> check_epoch_end_signal (after enact)
if header.Number.Uint64() >= DEBUG_LOG_FROM {
fmt.Printf("finalize1: %d,%d\n", header.Number.Uint64(), len(r))
fmt.Printf("finalize1: %d,%d\n", header.Number.Uint64(), len(*receipts))
}
pendingTransitionProof, err := c.cfg.Validators.signalEpochEnd(header.Number.Uint64() == 0, header, r)
pendingTransitionProof, err := c.cfg.Validators.signalEpochEnd(header.Number.Uint64() == 0, header, *receipts)
if err != nil {
return err
}
if pendingTransitionProof != nil {
if header.Number.Uint64() >= DEBUG_LOG_FROM {
fmt.Printf("insert_pending_trancition: %d,receipts=%d, lenProof=%d\n", header.Number.Uint64(), len(r), len(pendingTransitionProof))
fmt.Printf("insert_pending_trancition: %d,receipts=%d, lenProof=%d\n", header.Number.Uint64(), len(*receipts), len(pendingTransitionProof))
}
if err = e.PutPendingEpoch(header.Hash(), header.Number.Uint64(), pendingTransitionProof); err != nil {
return err
Expand Down Expand Up @@ -976,12 +976,12 @@ func allHeadersUntil(chain consensus.ChainHeaderReader, from *types.Header, to c
//}

// FinalizeAndAssemble implements consensus.Engine
func (c *AuRa) FinalizeAndAssemble(chainConfig *params.ChainConfig, header *types.Header, state *state.IntraBlockState, txs []types.Transaction, uncles []*types.Header, r types.Receipts,
func (c *AuRa) FinalizeAndAssemble(chainConfig *params.ChainConfig, header *types.Header, state *state.IntraBlockState, txs *types.Transactions, uncles []*types.Header, receipts *types.Receipts,
e consensus.EpochReader, chain consensus.ChainHeaderReader, syscall consensus.SystemCall, call consensus.Call) (*types.Block, error) {
c.Finalize(chainConfig, header, state, txs, uncles, r, e, chain, syscall)
c.Finalize(chainConfig, header, state, txs, uncles, receipts, e, chain, syscall)

// Assemble and return the final block for sealing
return types.NewBlock(header, txs, uncles, r), nil
return types.NewBlock(header, *txs, uncles, *receipts), nil
}

// Authorize injects a private key into the consensus engine to mint new blocks
Expand Down
8 changes: 4 additions & 4 deletions consensus/clique/clique.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ func (c *Clique) VerifySeal(chain consensus.ChainHeaderReader, header *types.Hea

// Prepare implements consensus.Engine, preparing all the consensus fields of the
// header for running the transactions on top.
func (c *Clique) Prepare(chain consensus.ChainHeaderReader, header *types.Header) error {
func (c *Clique) Prepare(chain consensus.ChainHeaderReader, header *types.Header, state *state.IntraBlockState) error {

// If the block isn't a checkpoint, cast a random vote (good enough for now)
header.Coinbase = common.Address{}
Expand Down Expand Up @@ -369,21 +369,21 @@ func (c *Clique) Initialize(config *params.ChainConfig, chain consensus.ChainHea

// Finalize implements consensus.Engine, ensuring no uncles are set, nor block
// rewards given.
func (c *Clique) Finalize(config *params.ChainConfig, header *types.Header, state *state.IntraBlockState, txs []types.Transaction, uncles []*types.Header, r types.Receipts, e consensus.EpochReader, chain consensus.ChainHeaderReader, syscall consensus.SystemCall) error {
func (c *Clique) Finalize(config *params.ChainConfig, header *types.Header, state *state.IntraBlockState, txs *types.Transactions, uncles []*types.Header, r *types.Receipts, e consensus.EpochReader, chain consensus.ChainHeaderReader, syscall consensus.SystemCall) error {
// No block rewards in PoA, so the state remains as is and uncles are dropped
header.UncleHash = types.CalcUncleHash(nil)
return nil
}

// FinalizeAndAssemble implements consensus.Engine, ensuring no uncles are set,
// nor block rewards given, and returns the final block.
func (c *Clique) FinalizeAndAssemble(chainConfig *params.ChainConfig, header *types.Header, state *state.IntraBlockState, txs []types.Transaction, uncles []*types.Header, receipts types.Receipts,
func (c *Clique) FinalizeAndAssemble(chainConfig *params.ChainConfig, header *types.Header, state *state.IntraBlockState, txs *types.Transactions, uncles []*types.Header, receipts *types.Receipts,
e consensus.EpochReader, chain consensus.ChainHeaderReader, syscall consensus.SystemCall, call consensus.Call) (*types.Block, error) {
// No block rewards in PoA, so the state remains as is and uncles are dropped
header.UncleHash = types.CalcUncleHash(nil)

// Assemble and return the final block for sealing
return types.NewBlock(header, txs, nil, receipts), nil
return types.NewBlock(header, *txs, nil, *receipts), nil
}

// Authorize injects a private key into the consensus engine to mint new blocks
Expand Down
8 changes: 4 additions & 4 deletions consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ type Engine interface {

// Prepare initializes the consensus fields of a block header according to the
// rules of a particular engine. The changes are executed inline.
Prepare(chain ChainHeaderReader, header *types.Header) error
Prepare(chain ChainHeaderReader, header *types.Header, state *state.IntraBlockState) error

// Initialize runs any pre-transaction state modifications (e.g. epoch start)
Initialize(config *params.ChainConfig, chain ChainHeaderReader, e EpochReader, header *types.Header, txs []types.Transaction, uncles []*types.Header, syscall SystemCall)
Expand All @@ -104,15 +104,15 @@ type Engine interface {
//
// Note: The block header and state database might be updated to reflect any
// consensus rules that happen at finalization (e.g. block rewards).
Finalize(config *params.ChainConfig, header *types.Header, state *state.IntraBlockState, txs []types.Transaction, uncles []*types.Header, r types.Receipts, e EpochReader, chain ChainHeaderReader, syscall SystemCall) error
Finalize(config *params.ChainConfig, header *types.Header, state *state.IntraBlockState, txs *types.Transactions, uncles []*types.Header, receipts *types.Receipts, e EpochReader, chain ChainHeaderReader, syscall SystemCall) error

// FinalizeAndAssemble runs any post-transaction state modifications (e.g. block
// rewards) and assembles the final block.
//
// Note: The block header and state database might be updated to reflect any
// consensus rules that happen at finalization (e.g. block rewards).
FinalizeAndAssemble(config *params.ChainConfig, header *types.Header, state *state.IntraBlockState, txs []types.Transaction,
uncles []*types.Header, receipts types.Receipts, e EpochReader, chain ChainHeaderReader, syscall SystemCall, call Call) (*types.Block, error)
FinalizeAndAssemble(config *params.ChainConfig, header *types.Header, state *state.IntraBlockState, txs *types.Transactions,
uncles []*types.Header, receipts *types.Receipts, e EpochReader, chain ChainHeaderReader, syscall SystemCall, call Call) (*types.Block, error)

// Seal generates a new sealing request for the given input block and pushes
// the result into the given channel.
Expand Down
10 changes: 5 additions & 5 deletions consensus/ethash/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ func (ethash *Ethash) verifySeal(header *types.Header, fulldag bool) error { //n

// Prepare implements consensus.Engine, initializing the difficulty field of a
// header to conform to the ethash protocol. The changes are done inline.
func (ethash *Ethash) Prepare(chain consensus.ChainHeaderReader, header *types.Header) error {
func (ethash *Ethash) Prepare(chain consensus.ChainHeaderReader, header *types.Header, state *state.IntraBlockState) error {
parent := chain.GetHeader(header.ParentHash, header.Number.Uint64()-1)
if parent == nil {
return consensus.ErrUnknownAncestor
Expand All @@ -608,21 +608,21 @@ func (ethash *Ethash) Initialize(config *params.ChainConfig, chain consensus.Cha

// Finalize implements consensus.Engine, accumulating the block and uncle rewards,
// setting the final state on the header
func (ethash *Ethash) Finalize(config *params.ChainConfig, header *types.Header, state *state.IntraBlockState, txs []types.Transaction, uncles []*types.Header, r types.Receipts, e consensus.EpochReader, chain consensus.ChainHeaderReader, syscall consensus.SystemCall) error {
func (ethash *Ethash) Finalize(config *params.ChainConfig, header *types.Header, state *state.IntraBlockState, txs *types.Transactions, uncles []*types.Header, r *types.Receipts, e consensus.EpochReader, chain consensus.ChainHeaderReader, syscall consensus.SystemCall) error {
// Accumulate any block and uncle rewards and commit the final state root
accumulateRewards(config, state, header, uncles)
return nil
}

// FinalizeAndAssemble implements consensus.Engine, accumulating the block and
// uncle rewards, setting the final state and assembling the block.
func (ethash *Ethash) FinalizeAndAssemble(chainConfig *params.ChainConfig, header *types.Header, state *state.IntraBlockState, txs []types.Transaction, uncles []*types.Header, r types.Receipts,
func (ethash *Ethash) FinalizeAndAssemble(chainConfig *params.ChainConfig, header *types.Header, state *state.IntraBlockState, txs *types.Transactions, uncles []*types.Header, r *types.Receipts,
e consensus.EpochReader, chain consensus.ChainHeaderReader, syscall consensus.SystemCall, call consensus.Call) (*types.Block, error) {

// Finalize block
ethash.Finalize(chainConfig, header, state, txs, uncles, r, e, chain, syscall)
_ = ethash.Finalize(chainConfig, header, state, txs, uncles, r, e, chain, syscall)
// Header seems complete, assemble into a block and return
return types.NewBlock(header, txs, uncles, r), nil
return types.NewBlock(header, *txs, uncles, *r), nil
}

// SealHash returns the hash of a block prior to it being sealed.
Expand Down
Loading

0 comments on commit 3245254

Please sign in to comment.