mirror of https://github.com/ethereum/go-ethereum
all: core rework for the merge transition (#23761)
* all: work for eth1/2 transtition * consensus/beacon, eth: change beacon difficulty to 0 * eth: updates * all: add terminalBlockDifficulty config, fix rebasing issues * eth: implemented merge interop spec * internal/ethapi: update to v1.0.0.alpha.2 This commit updates the code to the new spec, moving payloadId into it's own object. It also fixes an issue with finalizing an empty blockhash. It also properly sets the basefee * all: sync polishes, other fixes + refactors * core, eth: correct semantics for LeavePoW, EnterPoS * core: fixed rebasing artifacts * core: light: performance improvements * core: use keyed field (f) * core: eth: fix compilation issues + tests * eth/catalyst: dbetter error codes * all: move Merger to consensus/, remove reliance on it in bc * all: renamed EnterPoS and LeavePoW to ReachTDD and FinalizePoS * core: make mergelogs a function * core: use InsertChain instead of InsertBlock * les: drop merger from lightchain object * consensus: add merger * core: recoverAncestors in catalyst mode * core: fix nitpick * all: removed merger from beacon, use TTD, nitpicks * consensus: eth: add docstring, removed unnecessary code duplication * consensus/beacon: better comment * all: easy to fix nitpicks by karalabe * consensus/beacon: verify known headers to be sure * core: comments * core: eth: don't drop peers who advertise blocks, nitpicks * core: never add beacon blocks to the future queue * core: fixed nitpicks * consensus/beacon: simplify IsTTDReached check * consensus/beacon: correct IsTTDReached check Co-authored-by: rjl493456442 <garyrong0905@gmail.com> Co-authored-by: Péter Szilágyi <peterke@gmail.com>pull/23950/head
parent
519cf98b69
commit
3038e480f5
@ -0,0 +1,376 @@ |
||||
// Copyright 2021 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package beacon |
||||
|
||||
import ( |
||||
"errors" |
||||
"fmt" |
||||
"math/big" |
||||
|
||||
"github.com/ethereum/go-ethereum/common" |
||||
"github.com/ethereum/go-ethereum/consensus" |
||||
"github.com/ethereum/go-ethereum/consensus/misc" |
||||
"github.com/ethereum/go-ethereum/core/state" |
||||
"github.com/ethereum/go-ethereum/core/types" |
||||
"github.com/ethereum/go-ethereum/rpc" |
||||
"github.com/ethereum/go-ethereum/trie" |
||||
) |
||||
|
||||
// Proof-of-stake protocol constants.
|
||||
var ( |
||||
beaconDifficulty = common.Big0 // The default block difficulty in the beacon consensus
|
||||
beaconNonce = types.EncodeNonce(0) // The default block nonce in the beacon consensus
|
||||
) |
||||
|
||||
// Various error messages to mark blocks invalid. These should be private to
|
||||
// prevent engine specific errors from being referenced in the remainder of the
|
||||
// codebase, inherently breaking if the engine is swapped out. Please put common
|
||||
// error types into the consensus package.
|
||||
var ( |
||||
errTooManyUncles = errors.New("too many uncles") |
||||
errInvalidMixDigest = errors.New("invalid mix digest") |
||||
errInvalidNonce = errors.New("invalid nonce") |
||||
errInvalidUncleHash = errors.New("invalid uncle hash") |
||||
) |
||||
|
||||
// Beacon is a consensus engine that combines the eth1 consensus and proof-of-stake
|
||||
// algorithm. There is a special flag inside to decide whether to use legacy consensus
|
||||
// rules or new rules. The transition rule is described in the eth1/2 merge spec.
|
||||
// https://github.com/ethereum/EIPs/blob/master/EIPS/eip-3675.md
|
||||
//
|
||||
// The beacon here is a half-functional consensus engine with partial functions which
|
||||
// is only used for necessary consensus checks. The legacy consensus engine can be any
|
||||
// engine implements the consensus interface (except the beacon itself).
|
||||
type Beacon struct { |
||||
ethone consensus.Engine // Original consensus engine used in eth1, e.g. ethash or clique
|
||||
} |
||||
|
||||
// New creates a consensus engine with the given embedded eth1 engine.
|
||||
func New(ethone consensus.Engine) *Beacon { |
||||
if _, ok := ethone.(*Beacon); ok { |
||||
panic("nested consensus engine") |
||||
} |
||||
return &Beacon{ethone: ethone} |
||||
} |
||||
|
||||
// Author implements consensus.Engine, returning the verified author of the block.
|
||||
func (beacon *Beacon) Author(header *types.Header) (common.Address, error) { |
||||
if !beacon.IsPoSHeader(header) { |
||||
return beacon.ethone.Author(header) |
||||
} |
||||
return header.Coinbase, nil |
||||
} |
||||
|
||||
// VerifyHeader checks whether a header conforms to the consensus rules of the
|
||||
// stock Ethereum consensus engine.
|
||||
func (beacon *Beacon) VerifyHeader(chain consensus.ChainHeaderReader, header *types.Header, seal bool) error { |
||||
reached, _ := IsTTDReached(chain, header.ParentHash, header.Number.Uint64()-1) |
||||
if !reached { |
||||
return beacon.ethone.VerifyHeader(chain, header, seal) |
||||
} |
||||
// Short circuit if the parent is not known
|
||||
parent := chain.GetHeader(header.ParentHash, header.Number.Uint64()-1) |
||||
if parent == nil { |
||||
return consensus.ErrUnknownAncestor |
||||
} |
||||
// Sanity checks passed, do a proper verification
|
||||
return beacon.verifyHeader(chain, header, parent) |
||||
} |
||||
|
||||
// VerifyHeaders is similar to VerifyHeader, but verifies a batch of headers
|
||||
// concurrently. The method returns a quit channel to abort the operations and
|
||||
// a results channel to retrieve the async verifications.
|
||||
// VerifyHeaders expect the headers to be ordered and continuous.
|
||||
func (beacon *Beacon) VerifyHeaders(chain consensus.ChainHeaderReader, headers []*types.Header, seals []bool) (chan<- struct{}, <-chan error) { |
||||
if !beacon.IsPoSHeader(headers[len(headers)-1]) { |
||||
return beacon.ethone.VerifyHeaders(chain, headers, seals) |
||||
} |
||||
var ( |
||||
preHeaders []*types.Header |
||||
postHeaders []*types.Header |
||||
preSeals []bool |
||||
) |
||||
for index, header := range headers { |
||||
if beacon.IsPoSHeader(header) { |
||||
preHeaders = headers[:index] |
||||
postHeaders = headers[index:] |
||||
preSeals = seals[:index] |
||||
break |
||||
} |
||||
} |
||||
// All the headers have passed the transition point, use new rules.
|
||||
if len(preHeaders) == 0 { |
||||
return beacon.verifyHeaders(chain, headers, nil) |
||||
} |
||||
// The transition point exists in the middle, separate the headers
|
||||
// into two batches and apply different verification rules for them.
|
||||
var ( |
||||
abort = make(chan struct{}) |
||||
results = make(chan error, len(headers)) |
||||
) |
||||
go func() { |
||||
var ( |
||||
old, new, out = 0, len(preHeaders), 0 |
||||
errors = make([]error, len(headers)) |
||||
done = make([]bool, len(headers)) |
||||
oldDone, oldResult = beacon.ethone.VerifyHeaders(chain, preHeaders, preSeals) |
||||
newDone, newResult = beacon.verifyHeaders(chain, postHeaders, preHeaders[len(preHeaders)-1]) |
||||
) |
||||
for { |
||||
for ; done[out]; out++ { |
||||
results <- errors[out] |
||||
if out == len(headers)-1 { |
||||
return |
||||
} |
||||
} |
||||
select { |
||||
case err := <-oldResult: |
||||
errors[old], done[old] = err, true |
||||
old++ |
||||
case err := <-newResult: |
||||
errors[new], done[new] = err, true |
||||
new++ |
||||
case <-abort: |
||||
close(oldDone) |
||||
close(newDone) |
||||
return |
||||
} |
||||
} |
||||
}() |
||||
return abort, results |
||||
} |
||||
|
||||
// VerifyUncles verifies that the given block's uncles conform to the consensus
|
||||
// rules of the Ethereum consensus engine.
|
||||
func (beacon *Beacon) VerifyUncles(chain consensus.ChainReader, block *types.Block) error { |
||||
if !beacon.IsPoSHeader(block.Header()) { |
||||
return beacon.ethone.VerifyUncles(chain, block) |
||||
} |
||||
// Verify that there is no uncle block. It's explicitly disabled in the beacon
|
||||
if len(block.Uncles()) > 0 { |
||||
return errTooManyUncles |
||||
} |
||||
return nil |
||||
} |
||||
|
||||
// verifyHeader checks whether a header conforms to the consensus rules of the
|
||||
// stock Ethereum consensus engine. The difference between the beacon and classic is
|
||||
// (a) The following fields are expected to be constants:
|
||||
// - difficulty is expected to be 0
|
||||
// - nonce is expected to be 0
|
||||
// - unclehash is expected to be Hash(emptyHeader)
|
||||
// to be the desired constants
|
||||
// (b) the timestamp is not verified anymore
|
||||
// (c) the extradata is limited to 32 bytes
|
||||
func (beacon *Beacon) verifyHeader(chain consensus.ChainHeaderReader, header, parent *types.Header) error { |
||||
// Ensure that the header's extra-data section is of a reasonable size
|
||||
if len(header.Extra) > 32 { |
||||
return fmt.Errorf("extra-data longer than 32 bytes (%d)", len(header.Extra)) |
||||
} |
||||
// Verify the seal parts. Ensure the mixhash, nonce and uncle hash are the expected value.
|
||||
if header.MixDigest != (common.Hash{}) { |
||||
return errInvalidMixDigest |
||||
} |
||||
if header.Nonce != beaconNonce { |
||||
return errInvalidNonce |
||||
} |
||||
if header.UncleHash != types.EmptyUncleHash { |
||||
return errInvalidUncleHash |
||||
} |
||||
// Verify the block's difficulty to ensure it's the default constant
|
||||
if beaconDifficulty.Cmp(header.Difficulty) != 0 { |
||||
return fmt.Errorf("invalid difficulty: have %v, want %v", header.Difficulty, beaconDifficulty) |
||||
} |
||||
// Verify that the gas limit is <= 2^63-1
|
||||
cap := uint64(0x7fffffffffffffff) |
||||
if header.GasLimit > cap { |
||||
return fmt.Errorf("invalid gasLimit: have %v, max %v", header.GasLimit, cap) |
||||
} |
||||
// Verify that the gasUsed is <= gasLimit
|
||||
if header.GasUsed > header.GasLimit { |
||||
return fmt.Errorf("invalid gasUsed: have %d, gasLimit %d", header.GasUsed, header.GasLimit) |
||||
} |
||||
// Verify that the block number is parent's +1
|
||||
if diff := new(big.Int).Sub(header.Number, parent.Number); diff.Cmp(common.Big1) != 0 { |
||||
return consensus.ErrInvalidNumber |
||||
} |
||||
// Verify the header's EIP-1559 attributes.
|
||||
return misc.VerifyEip1559Header(chain.Config(), parent, header) |
||||
} |
||||
|
||||
// verifyHeaders is similar to verifyHeader, but verifies a batch of headers
|
||||
// concurrently. The method returns a quit channel to abort the operations and
|
||||
// a results channel to retrieve the async verifications. An additional parent
|
||||
// header will be passed if the relevant header is not in the database yet.
|
||||
func (beacon *Beacon) verifyHeaders(chain consensus.ChainHeaderReader, headers []*types.Header, ancestor *types.Header) (chan<- struct{}, <-chan error) { |
||||
var ( |
||||
abort = make(chan struct{}) |
||||
results = make(chan error, len(headers)) |
||||
) |
||||
go func() { |
||||
for i, header := range headers { |
||||
var parent *types.Header |
||||
if i == 0 { |
||||
if ancestor != nil { |
||||
parent = ancestor |
||||
} else { |
||||
parent = chain.GetHeader(headers[0].ParentHash, headers[0].Number.Uint64()-1) |
||||
} |
||||
} else if headers[i-1].Hash() == headers[i].ParentHash { |
||||
parent = headers[i-1] |
||||
} |
||||
if parent == nil { |
||||
select { |
||||
case <-abort: |
||||
return |
||||
case results <- consensus.ErrUnknownAncestor: |
||||
} |
||||
continue |
||||
} |
||||
err := beacon.verifyHeader(chain, header, parent) |
||||
select { |
||||
case <-abort: |
||||
return |
||||
case results <- err: |
||||
} |
||||
} |
||||
}() |
||||
return abort, results |
||||
} |
||||
|
||||
// Prepare implements consensus.Engine, initializing the difficulty field of a
|
||||
// header to conform to the beacon protocol. The changes are done inline.
|
||||
func (beacon *Beacon) Prepare(chain consensus.ChainHeaderReader, header *types.Header) error { |
||||
// Transition isn't triggered yet, use the legacy rules for preparation.
|
||||
reached, err := IsTTDReached(chain, header.ParentHash, header.Number.Uint64()-1) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
if !reached { |
||||
return beacon.ethone.Prepare(chain, header) |
||||
} |
||||
header.Difficulty = beaconDifficulty |
||||
return nil |
||||
} |
||||
|
||||
// Finalize implements consensus.Engine, setting the final state on the header
|
||||
func (beacon *Beacon) Finalize(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header) { |
||||
// Finalize is different with Prepare, it can be used in both block generation
|
||||
// and verification. So determine the consensus rules by header type.
|
||||
if !beacon.IsPoSHeader(header) { |
||||
beacon.ethone.Finalize(chain, header, state, txs, uncles) |
||||
return |
||||
} |
||||
// The block reward is no longer handled here. It's done by the
|
||||
// external consensus engine.
|
||||
header.Root = state.IntermediateRoot(true) |
||||
} |
||||
|
||||
// FinalizeAndAssemble implements consensus.Engine, setting the final state and
|
||||
// assembling the block.
|
||||
func (beacon *Beacon) FinalizeAndAssemble(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header, receipts []*types.Receipt) (*types.Block, error) { |
||||
// FinalizeAndAssemble is different with Prepare, it can be used in both block
|
||||
// generation and verification. So determine the consensus rules by header type.
|
||||
if !beacon.IsPoSHeader(header) { |
||||
return beacon.ethone.FinalizeAndAssemble(chain, header, state, txs, uncles, receipts) |
||||
} |
||||
// Finalize and assemble the block
|
||||
beacon.Finalize(chain, header, state, txs, uncles) |
||||
return types.NewBlock(header, txs, uncles, receipts, trie.NewStackTrie(nil)), nil |
||||
} |
||||
|
||||
// Seal generates a new sealing request for the given input block and pushes
|
||||
// the result into the given channel.
|
||||
//
|
||||
// Note, the method returns immediately and will send the result async. More
|
||||
// than one result may also be returned depending on the consensus algorithm.
|
||||
func (beacon *Beacon) Seal(chain consensus.ChainHeaderReader, block *types.Block, results chan<- *types.Block, stop <-chan struct{}) error { |
||||
if !beacon.IsPoSHeader(block.Header()) { |
||||
return beacon.ethone.Seal(chain, block, results, stop) |
||||
} |
||||
// The seal verification is done by the external consensus engine,
|
||||
// return directly without pushing any block back. In another word
|
||||
// beacon won't return any result by `results` channel which may
|
||||
// blocks the receiver logic forever.
|
||||
return nil |
||||
} |
||||
|
||||
// SealHash returns the hash of a block prior to it being sealed.
|
||||
func (beacon *Beacon) SealHash(header *types.Header) common.Hash { |
||||
return beacon.ethone.SealHash(header) |
||||
} |
||||
|
||||
// CalcDifficulty is the difficulty adjustment algorithm. It returns
|
||||
// the difficulty that a new block should have when created at time
|
||||
// given the parent block's time and difficulty.
|
||||
func (beacon *Beacon) CalcDifficulty(chain consensus.ChainHeaderReader, time uint64, parent *types.Header) *big.Int { |
||||
// Transition isn't triggered yet, use the legacy rules for calculation
|
||||
if reached, _ := IsTTDReached(chain, parent.Hash(), parent.Number.Uint64()); !reached { |
||||
return beacon.ethone.CalcDifficulty(chain, time, parent) |
||||
} |
||||
return beaconDifficulty |
||||
} |
||||
|
||||
// APIs implements consensus.Engine, returning the user facing RPC APIs.
|
||||
func (beacon *Beacon) APIs(chain consensus.ChainHeaderReader) []rpc.API { |
||||
return beacon.ethone.APIs(chain) |
||||
} |
||||
|
||||
// Close shutdowns the consensus engine
|
||||
func (beacon *Beacon) Close() error { |
||||
return beacon.ethone.Close() |
||||
} |
||||
|
||||
// IsPoSHeader reports the header belongs to the PoS-stage with some special fields.
|
||||
// This function is not suitable for a part of APIs like Prepare or CalcDifficulty
|
||||
// because the header difficulty is not set yet.
|
||||
func (beacon *Beacon) IsPoSHeader(header *types.Header) bool { |
||||
if header.Difficulty == nil { |
||||
panic("IsPoSHeader called with invalid difficulty") |
||||
} |
||||
return header.Difficulty.Cmp(beaconDifficulty) == 0 |
||||
} |
||||
|
||||
// InnerEngine returns the embedded eth1 consensus engine.
|
||||
func (beacon *Beacon) InnerEngine() consensus.Engine { |
||||
return beacon.ethone |
||||
} |
||||
|
||||
// SetThreads updates the mining threads. Delegate the call
|
||||
// to the eth1 engine if it's threaded.
|
||||
func (beacon *Beacon) SetThreads(threads int) { |
||||
type threaded interface { |
||||
SetThreads(threads int) |
||||
} |
||||
if th, ok := beacon.ethone.(threaded); ok { |
||||
th.SetThreads(threads) |
||||
} |
||||
} |
||||
|
||||
// IsTTDReached checks if the TotalTerminalDifficulty has been surpassed on the `parentHash` block.
|
||||
// It depends on the parentHash already being stored in the database.
|
||||
// If the parentHash is not stored in the database a UnknownAncestor error is returned.
|
||||
func IsTTDReached(chain consensus.ChainHeaderReader, parentHash common.Hash, number uint64) (bool, error) { |
||||
if chain.Config().TerminalTotalDifficulty == nil { |
||||
return false, nil |
||||
} |
||||
td := chain.GetTd(parentHash, number) |
||||
if td == nil { |
||||
return false, consensus.ErrUnknownAncestor |
||||
} |
||||
return td.Cmp(chain.Config().TerminalTotalDifficulty) >= 0, nil |
||||
} |
@ -0,0 +1,110 @@ |
||||
// Copyright 2021 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package consensus |
||||
|
||||
import ( |
||||
"fmt" |
||||
"sync" |
||||
|
||||
"github.com/ethereum/go-ethereum/core/rawdb" |
||||
"github.com/ethereum/go-ethereum/ethdb" |
||||
"github.com/ethereum/go-ethereum/log" |
||||
"github.com/ethereum/go-ethereum/rlp" |
||||
) |
||||
|
||||
// transitionStatus describes the status of eth1/2 transition. This switch
|
||||
// between modes is a one-way action which is triggered by corresponding
|
||||
// consensus-layer message.
|
||||
type transitionStatus struct { |
||||
LeftPoW bool // The flag is set when the first NewHead message received
|
||||
EnteredPoS bool // The flag is set when the first FinalisedBlock message received
|
||||
} |
||||
|
||||
// Merger is an internal help structure used to track the eth1/2 transition status.
|
||||
// It's a common structure can be used in both full node and light client.
|
||||
type Merger struct { |
||||
db ethdb.KeyValueStore |
||||
status transitionStatus |
||||
mu sync.RWMutex |
||||
} |
||||
|
||||
// NewMerger creates a new Merger which stores its transition status in the provided db.
|
||||
func NewMerger(db ethdb.KeyValueStore) *Merger { |
||||
var status transitionStatus |
||||
blob := rawdb.ReadTransitionStatus(db) |
||||
if len(blob) != 0 { |
||||
if err := rlp.DecodeBytes(blob, &status); err != nil { |
||||
log.Crit("Failed to decode the transition status", "err", err) |
||||
} |
||||
} |
||||
return &Merger{ |
||||
db: db, |
||||
status: status, |
||||
} |
||||
} |
||||
|
||||
// ReachTTD is called whenever the first NewHead message received
|
||||
// from the consensus-layer.
|
||||
func (m *Merger) ReachTTD() { |
||||
m.mu.Lock() |
||||
defer m.mu.Unlock() |
||||
|
||||
if m.status.LeftPoW { |
||||
return |
||||
} |
||||
m.status = transitionStatus{LeftPoW: true} |
||||
blob, err := rlp.EncodeToBytes(m.status) |
||||
if err != nil { |
||||
panic(fmt.Sprintf("Failed to encode the transition status: %v", err)) |
||||
} |
||||
rawdb.WriteTransitionStatus(m.db, blob) |
||||
log.Info("Left PoW stage") |
||||
} |
||||
|
||||
// FinalizePoS is called whenever the first FinalisedBlock message received
|
||||
// from the consensus-layer.
|
||||
func (m *Merger) FinalizePoS() { |
||||
m.mu.Lock() |
||||
defer m.mu.Unlock() |
||||
|
||||
if m.status.EnteredPoS { |
||||
return |
||||
} |
||||
m.status = transitionStatus{LeftPoW: true, EnteredPoS: true} |
||||
blob, err := rlp.EncodeToBytes(m.status) |
||||
if err != nil { |
||||
panic(fmt.Sprintf("Failed to encode the transition status: %v", err)) |
||||
} |
||||
rawdb.WriteTransitionStatus(m.db, blob) |
||||
log.Info("Entered PoS stage") |
||||
} |
||||
|
||||
// TDDReached reports whether the chain has left the PoW stage.
|
||||
func (m *Merger) TDDReached() bool { |
||||
m.mu.RLock() |
||||
defer m.mu.RUnlock() |
||||
|
||||
return m.status.LeftPoW |
||||
} |
||||
|
||||
// PoSFinalized reports whether the chain has entered the PoS stage.
|
||||
func (m *Merger) PoSFinalized() bool { |
||||
m.mu.RLock() |
||||
defer m.mu.RUnlock() |
||||
|
||||
return m.status.EnteredPoS |
||||
} |
@ -0,0 +1,108 @@ |
||||
// Copyright 2021 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package core |
||||
|
||||
import ( |
||||
crand "crypto/rand" |
||||
"errors" |
||||
"math/big" |
||||
mrand "math/rand" |
||||
|
||||
"github.com/ethereum/go-ethereum/common" |
||||
"github.com/ethereum/go-ethereum/common/math" |
||||
"github.com/ethereum/go-ethereum/core/types" |
||||
"github.com/ethereum/go-ethereum/log" |
||||
"github.com/ethereum/go-ethereum/params" |
||||
) |
||||
|
||||
// ChainReader defines a small collection of methods needed to access the local
|
||||
// blockchain during header verification. It's implemented by both blockchain
|
||||
// and lightchain.
|
||||
type ChainReader interface { |
||||
// Config retrieves the header chain's chain configuration.
|
||||
Config() *params.ChainConfig |
||||
|
||||
// GetTd returns the total difficulty of a local block.
|
||||
GetTd(common.Hash, uint64) *big.Int |
||||
} |
||||
|
||||
// ForkChoice is the fork chooser based on the highest total difficulty of the
|
||||
// chain(the fork choice used in the eth1) and the external fork choice (the fork
|
||||
// choice used in the eth2). This main goal of this ForkChoice is not only for
|
||||
// offering fork choice during the eth1/2 merge phase, but also keep the compatibility
|
||||
// for all other proof-of-work networks.
|
||||
type ForkChoice struct { |
||||
chain ChainReader |
||||
rand *mrand.Rand |
||||
|
||||
// preserve is a helper function used in td fork choice.
|
||||
// Miners will prefer to choose the local mined block if the
|
||||
// local td is equal to the extern one. It can be nil for light
|
||||
// client
|
||||
preserve func(header *types.Header) bool |
||||
} |
||||
|
||||
func NewForkChoice(chainReader ChainReader, preserve func(header *types.Header) bool) *ForkChoice { |
||||
// Seed a fast but crypto originating random generator
|
||||
seed, err := crand.Int(crand.Reader, big.NewInt(math.MaxInt64)) |
||||
if err != nil { |
||||
log.Crit("Failed to initialize random seed", "err", err) |
||||
} |
||||
return &ForkChoice{ |
||||
chain: chainReader, |
||||
rand: mrand.New(mrand.NewSource(seed.Int64())), |
||||
preserve: preserve, |
||||
} |
||||
} |
||||
|
||||
// ReorgNeeded returns whether the reorg should be applied
|
||||
// based on the given external header and local canonical chain.
|
||||
// In the td mode, the new head is chosen if the corresponding
|
||||
// total difficulty is higher. In the extern mode, the trusted
|
||||
// header is always selected as the head.
|
||||
func (f *ForkChoice) ReorgNeeded(current *types.Header, header *types.Header) (bool, error) { |
||||
var ( |
||||
localTD = f.chain.GetTd(current.Hash(), current.Number.Uint64()) |
||||
externTd = f.chain.GetTd(header.Hash(), header.Number.Uint64()) |
||||
) |
||||
if localTD == nil || externTd == nil { |
||||
return false, errors.New("missing td") |
||||
} |
||||
// Accept the new header as the chain head if the transition
|
||||
// is already triggered. We assume all the headers after the
|
||||
// transition come from the trusted consensus layer.
|
||||
if ttd := f.chain.Config().TerminalTotalDifficulty; ttd != nil && ttd.Cmp(externTd) <= 0 { |
||||
return true, nil |
||||
} |
||||
// If the total difficulty is higher than our known, add it to the canonical chain
|
||||
// Second clause in the if statement reduces the vulnerability to selfish mining.
|
||||
// Please refer to http://www.cs.cornell.edu/~ie53/publications/btcProcFC.pdf
|
||||
reorg := externTd.Cmp(localTD) > 0 |
||||
if !reorg && externTd.Cmp(localTD) == 0 { |
||||
number, headNumber := header.Number.Uint64(), current.Number.Uint64() |
||||
if number < headNumber { |
||||
reorg = true |
||||
} else if number == headNumber { |
||||
var currentPreserve, externPreserve bool |
||||
if f.preserve != nil { |
||||
currentPreserve, externPreserve = f.preserve(current), f.preserve(header) |
||||
} |
||||
reorg = !currentPreserve && (externPreserve || f.rand.Float64() < 0.5) |
||||
} |
||||
} |
||||
return reorg, nil |
||||
} |
@ -0,0 +1,36 @@ |
||||
// Code generated by github.com/fjl/gencodec. DO NOT EDIT.
|
||||
|
||||
package catalyst |
||||
|
||||
import ( |
||||
"encoding/json" |
||||
|
||||
"github.com/ethereum/go-ethereum/common/hexutil" |
||||
) |
||||
|
||||
var _ = (*payloadResponseMarshaling)(nil) |
||||
|
||||
// MarshalJSON marshals as JSON.
|
||||
func (p PayloadResponse) MarshalJSON() ([]byte, error) { |
||||
type PayloadResponse struct { |
||||
PayloadID hexutil.Uint64 `json:"payloadId"` |
||||
} |
||||
var enc PayloadResponse |
||||
enc.PayloadID = hexutil.Uint64(p.PayloadID) |
||||
return json.Marshal(&enc) |
||||
} |
||||
|
||||
// UnmarshalJSON unmarshals from JSON.
|
||||
func (p *PayloadResponse) UnmarshalJSON(input []byte) error { |
||||
type PayloadResponse struct { |
||||
PayloadID *hexutil.Uint64 `json:"payloadId"` |
||||
} |
||||
var dec PayloadResponse |
||||
if err := json.Unmarshal(input, &dec); err != nil { |
||||
return err |
||||
} |
||||
if dec.PayloadID != nil { |
||||
p.PayloadID = uint64(*dec.PayloadID) |
||||
} |
||||
return nil |
||||
} |
@ -0,0 +1,507 @@ |
||||
// Copyright 2021 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
// This file contains a miner stress test for the eth1/2 transition
|
||||
package main |
||||
|
||||
import ( |
||||
"crypto/ecdsa" |
||||
"errors" |
||||
"io/ioutil" |
||||
"math/big" |
||||
"math/rand" |
||||
"os" |
||||
"path/filepath" |
||||
"time" |
||||
|
||||
"github.com/ethereum/go-ethereum/accounts/keystore" |
||||
"github.com/ethereum/go-ethereum/common" |
||||
"github.com/ethereum/go-ethereum/common/fdlimit" |
||||
"github.com/ethereum/go-ethereum/common/hexutil" |
||||
"github.com/ethereum/go-ethereum/consensus/ethash" |
||||
"github.com/ethereum/go-ethereum/core" |
||||
"github.com/ethereum/go-ethereum/core/types" |
||||
"github.com/ethereum/go-ethereum/crypto" |
||||
"github.com/ethereum/go-ethereum/eth" |
||||
"github.com/ethereum/go-ethereum/eth/catalyst" |
||||
"github.com/ethereum/go-ethereum/eth/downloader" |
||||
"github.com/ethereum/go-ethereum/eth/ethconfig" |
||||
"github.com/ethereum/go-ethereum/les" |
||||
"github.com/ethereum/go-ethereum/log" |
||||
"github.com/ethereum/go-ethereum/miner" |
||||
"github.com/ethereum/go-ethereum/node" |
||||
"github.com/ethereum/go-ethereum/p2p" |
||||
"github.com/ethereum/go-ethereum/p2p/enode" |
||||
"github.com/ethereum/go-ethereum/params" |
||||
) |
||||
|
||||
type nodetype int |
||||
|
||||
const ( |
||||
legacyMiningNode nodetype = iota |
||||
legacyNormalNode |
||||
eth2MiningNode |
||||
eth2NormalNode |
||||
eth2LightClient |
||||
) |
||||
|
||||
func (typ nodetype) String() string { |
||||
switch typ { |
||||
case legacyMiningNode: |
||||
return "legacyMiningNode" |
||||
case legacyNormalNode: |
||||
return "legacyNormalNode" |
||||
case eth2MiningNode: |
||||
return "eth2MiningNode" |
||||
case eth2NormalNode: |
||||
return "eth2NormalNode" |
||||
case eth2LightClient: |
||||
return "eth2LightClient" |
||||
default: |
||||
return "undefined" |
||||
} |
||||
} |
||||
|
||||
var ( |
||||
// transitionDifficulty is the target total difficulty for transition
|
||||
transitionDifficulty = new(big.Int).Mul(big.NewInt(20), params.MinimumDifficulty) |
||||
|
||||
// blockInterval is the time interval for creating a new eth2 block
|
||||
blockInterval = time.Second * 3 |
||||
blockIntervalInt = 3 |
||||
|
||||
// finalizationDist is the block distance for finalizing block
|
||||
finalizationDist = 10 |
||||
) |
||||
|
||||
type ethNode struct { |
||||
typ nodetype |
||||
api *catalyst.ConsensusAPI |
||||
ethBackend *eth.Ethereum |
||||
lesBackend *les.LightEthereum |
||||
stack *node.Node |
||||
enode *enode.Node |
||||
} |
||||
|
||||
func newNode(typ nodetype, genesis *core.Genesis, enodes []*enode.Node) *ethNode { |
||||
var ( |
||||
err error |
||||
api *catalyst.ConsensusAPI |
||||
stack *node.Node |
||||
ethBackend *eth.Ethereum |
||||
lesBackend *les.LightEthereum |
||||
) |
||||
// Start the node and wait until it's up
|
||||
if typ == eth2LightClient { |
||||
stack, lesBackend, api, err = makeLightNode(genesis) |
||||
} else { |
||||
stack, ethBackend, api, err = makeFullNode(genesis) |
||||
} |
||||
if err != nil { |
||||
panic(err) |
||||
} |
||||
for stack.Server().NodeInfo().Ports.Listener == 0 { |
||||
time.Sleep(250 * time.Millisecond) |
||||
} |
||||
// Connect the node to all the previous ones
|
||||
for _, n := range enodes { |
||||
stack.Server().AddPeer(n) |
||||
} |
||||
enode := stack.Server().Self() |
||||
|
||||
// Inject the signer key and start sealing with it
|
||||
stack.AccountManager().AddBackend(keystore.NewPlaintextKeyStore("beacon-stress")) |
||||
store := stack.AccountManager().Backends(keystore.KeyStoreType)[0].(*keystore.KeyStore) |
||||
if _, err := store.NewAccount(""); err != nil { |
||||
panic(err) |
||||
} |
||||
return ðNode{ |
||||
typ: typ, |
||||
api: api, |
||||
ethBackend: ethBackend, |
||||
lesBackend: lesBackend, |
||||
stack: stack, |
||||
enode: enode, |
||||
} |
||||
} |
||||
|
||||
func (n *ethNode) assembleBlock(parentHash common.Hash, parentTimestamp uint64) (*catalyst.ExecutableData, error) { |
||||
if n.typ != eth2MiningNode { |
||||
return nil, errors.New("invalid node type") |
||||
} |
||||
payload, err := n.api.PreparePayload(catalyst.AssembleBlockParams{ |
||||
ParentHash: parentHash, |
||||
Timestamp: uint64(time.Now().Unix()), |
||||
}) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
return n.api.GetPayload(hexutil.Uint64(payload.PayloadID)) |
||||
} |
||||
|
||||
func (n *ethNode) insertBlock(eb catalyst.ExecutableData) error { |
||||
if !eth2types(n.typ) { |
||||
return errors.New("invalid node type") |
||||
} |
||||
newResp, err := n.api.ExecutePayload(eb) |
||||
if err != nil { |
||||
return err |
||||
} else if newResp.Status != "VALID" { |
||||
return errors.New("failed to insert block") |
||||
} |
||||
return nil |
||||
} |
||||
|
||||
func (n *ethNode) insertBlockAndSetHead(parent *types.Header, ed catalyst.ExecutableData) error { |
||||
if !eth2types(n.typ) { |
||||
return errors.New("invalid node type") |
||||
} |
||||
if err := n.insertBlock(ed); err != nil { |
||||
return err |
||||
} |
||||
block, err := catalyst.ExecutableDataToBlock(ed) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
if err := n.api.ConsensusValidated(catalyst.ConsensusValidatedParams{BlockHash: block.Hash(), Status: "VALID"}); err != nil { |
||||
return err |
||||
} |
||||
return nil |
||||
} |
||||
|
||||
type nodeManager struct { |
||||
genesis *core.Genesis |
||||
genesisBlock *types.Block |
||||
nodes []*ethNode |
||||
enodes []*enode.Node |
||||
close chan struct{} |
||||
} |
||||
|
||||
func newNodeManager(genesis *core.Genesis) *nodeManager { |
||||
return &nodeManager{ |
||||
close: make(chan struct{}), |
||||
genesis: genesis, |
||||
genesisBlock: genesis.ToBlock(nil), |
||||
} |
||||
} |
||||
|
||||
func (mgr *nodeManager) createNode(typ nodetype) { |
||||
node := newNode(typ, mgr.genesis, mgr.enodes) |
||||
mgr.nodes = append(mgr.nodes, node) |
||||
mgr.enodes = append(mgr.enodes, node.enode) |
||||
} |
||||
|
||||
func (mgr *nodeManager) getNodes(typ nodetype) []*ethNode { |
||||
var ret []*ethNode |
||||
for _, node := range mgr.nodes { |
||||
if node.typ == typ { |
||||
ret = append(ret, node) |
||||
} |
||||
} |
||||
return ret |
||||
} |
||||
|
||||
func (mgr *nodeManager) startMining() { |
||||
for _, node := range append(mgr.getNodes(eth2MiningNode), mgr.getNodes(legacyMiningNode)...) { |
||||
if err := node.ethBackend.StartMining(1); err != nil { |
||||
panic(err) |
||||
} |
||||
} |
||||
} |
||||
|
||||
func (mgr *nodeManager) shutdown() { |
||||
close(mgr.close) |
||||
for _, node := range mgr.nodes { |
||||
node.stack.Close() |
||||
} |
||||
} |
||||
|
||||
func (mgr *nodeManager) run() { |
||||
if len(mgr.nodes) == 0 { |
||||
return |
||||
} |
||||
chain := mgr.nodes[0].ethBackend.BlockChain() |
||||
sink := make(chan core.ChainHeadEvent, 1024) |
||||
sub := chain.SubscribeChainHeadEvent(sink) |
||||
defer sub.Unsubscribe() |
||||
|
||||
var ( |
||||
transitioned bool |
||||
parentBlock *types.Block |
||||
waitFinalise []*types.Block |
||||
) |
||||
timer := time.NewTimer(0) |
||||
defer timer.Stop() |
||||
<-timer.C // discard the initial tick
|
||||
|
||||
// Handle the by default transition.
|
||||
if transitionDifficulty.Sign() == 0 { |
||||
transitioned = true |
||||
parentBlock = mgr.genesisBlock |
||||
timer.Reset(blockInterval) |
||||
log.Info("Enable the transition by default") |
||||
} |
||||
|
||||
// Handle the block finalization.
|
||||
checkFinalise := func() { |
||||
if parentBlock == nil { |
||||
return |
||||
} |
||||
if len(waitFinalise) == 0 { |
||||
return |
||||
} |
||||
oldest := waitFinalise[0] |
||||
if oldest.NumberU64() > parentBlock.NumberU64() { |
||||
return |
||||
} |
||||
distance := parentBlock.NumberU64() - oldest.NumberU64() |
||||
if int(distance) < finalizationDist { |
||||
return |
||||
} |
||||
nodes := mgr.getNodes(eth2MiningNode) |
||||
nodes = append(nodes, mgr.getNodes(eth2NormalNode)...) |
||||
nodes = append(nodes, mgr.getNodes(eth2LightClient)...) |
||||
for _, node := range append(nodes) { |
||||
node.api.ConsensusValidated(catalyst.ConsensusValidatedParams{BlockHash: oldest.Hash(), Status: catalyst.VALID.Status}) |
||||
} |
||||
log.Info("Finalised eth2 block", "number", oldest.NumberU64(), "hash", oldest.Hash()) |
||||
waitFinalise = waitFinalise[1:] |
||||
} |
||||
|
||||
for { |
||||
checkFinalise() |
||||
select { |
||||
case <-mgr.close: |
||||
return |
||||
|
||||
case ev := <-sink: |
||||
if transitioned { |
||||
continue |
||||
} |
||||
td := chain.GetTd(ev.Block.Hash(), ev.Block.NumberU64()) |
||||
if td.Cmp(transitionDifficulty) < 0 { |
||||
continue |
||||
} |
||||
transitioned, parentBlock = true, ev.Block |
||||
timer.Reset(blockInterval) |
||||
log.Info("Transition difficulty reached", "td", td, "target", transitionDifficulty, "number", ev.Block.NumberU64(), "hash", ev.Block.Hash()) |
||||
|
||||
case <-timer.C: |
||||
producers := mgr.getNodes(eth2MiningNode) |
||||
if len(producers) == 0 { |
||||
continue |
||||
} |
||||
hash, timestamp := parentBlock.Hash(), parentBlock.Time() |
||||
if parentBlock.NumberU64() == 0 { |
||||
timestamp = uint64(time.Now().Unix()) - uint64(blockIntervalInt) |
||||
} |
||||
ed, err := producers[0].assembleBlock(hash, timestamp) |
||||
if err != nil { |
||||
log.Error("Failed to assemble the block", "err", err) |
||||
continue |
||||
} |
||||
block, _ := catalyst.ExecutableDataToBlock(*ed) |
||||
|
||||
nodes := mgr.getNodes(eth2MiningNode) |
||||
nodes = append(nodes, mgr.getNodes(eth2NormalNode)...) |
||||
nodes = append(nodes, mgr.getNodes(eth2LightClient)...) |
||||
|
||||
for _, node := range nodes { |
||||
if err := node.insertBlockAndSetHead(parentBlock.Header(), *ed); err != nil { |
||||
log.Error("Failed to insert block", "type", node.typ, "err", err) |
||||
} |
||||
} |
||||
log.Info("Create and insert eth2 block", "number", ed.Number) |
||||
parentBlock = block |
||||
waitFinalise = append(waitFinalise, block) |
||||
timer.Reset(blockInterval) |
||||
} |
||||
} |
||||
} |
||||
|
||||
func main() { |
||||
log.Root().SetHandler(log.LvlFilterHandler(log.LvlInfo, log.StreamHandler(os.Stderr, log.TerminalFormat(true)))) |
||||
fdlimit.Raise(2048) |
||||
|
||||
// Generate a batch of accounts to seal and fund with
|
||||
faucets := make([]*ecdsa.PrivateKey, 16) |
||||
for i := 0; i < len(faucets); i++ { |
||||
faucets[i], _ = crypto.GenerateKey() |
||||
} |
||||
// Pre-generate the ethash mining DAG so we don't race
|
||||
ethash.MakeDataset(1, filepath.Join(os.Getenv("HOME"), ".ethash")) |
||||
|
||||
// Create an Ethash network based off of the Ropsten config
|
||||
genesis := makeGenesis(faucets) |
||||
manager := newNodeManager(genesis) |
||||
defer manager.shutdown() |
||||
|
||||
manager.createNode(eth2NormalNode) |
||||
manager.createNode(eth2MiningNode) |
||||
manager.createNode(legacyMiningNode) |
||||
manager.createNode(legacyNormalNode) |
||||
manager.createNode(eth2LightClient) |
||||
|
||||
// Iterate over all the nodes and start mining
|
||||
time.Sleep(3 * time.Second) |
||||
if transitionDifficulty.Sign() != 0 { |
||||
manager.startMining() |
||||
} |
||||
go manager.run() |
||||
|
||||
// Start injecting transactions from the faucets like crazy
|
||||
time.Sleep(3 * time.Second) |
||||
nonces := make([]uint64, len(faucets)) |
||||
for { |
||||
// Pick a random mining node
|
||||
nodes := manager.getNodes(eth2MiningNode) |
||||
|
||||
index := rand.Intn(len(faucets)) |
||||
node := nodes[index%len(nodes)] |
||||
|
||||
// Create a self transaction and inject into the pool
|
||||
tx, err := types.SignTx(types.NewTransaction(nonces[index], crypto.PubkeyToAddress(faucets[index].PublicKey), new(big.Int), 21000, big.NewInt(100000000000+rand.Int63n(65536)), nil), types.HomesteadSigner{}, faucets[index]) |
||||
if err != nil { |
||||
panic(err) |
||||
} |
||||
if err := node.ethBackend.TxPool().AddLocal(tx); err != nil { |
||||
panic(err) |
||||
} |
||||
nonces[index]++ |
||||
|
||||
// Wait if we're too saturated
|
||||
if pend, _ := node.ethBackend.TxPool().Stats(); pend > 2048 { |
||||
time.Sleep(100 * time.Millisecond) |
||||
} |
||||
} |
||||
} |
||||
|
||||
// makeGenesis creates a custom Ethash genesis block based on some pre-defined
|
||||
// faucet accounts.
|
||||
func makeGenesis(faucets []*ecdsa.PrivateKey) *core.Genesis { |
||||
genesis := core.DefaultRopstenGenesisBlock() |
||||
genesis.Difficulty = params.MinimumDifficulty |
||||
genesis.GasLimit = 25000000 |
||||
|
||||
genesis.Config.ChainID = big.NewInt(18) |
||||
genesis.Config.EIP150Hash = common.Hash{} |
||||
genesis.BaseFee = big.NewInt(params.InitialBaseFee) |
||||
genesis.Config.TerminalTotalDifficulty = transitionDifficulty |
||||
|
||||
genesis.Alloc = core.GenesisAlloc{} |
||||
for _, faucet := range faucets { |
||||
genesis.Alloc[crypto.PubkeyToAddress(faucet.PublicKey)] = core.GenesisAccount{ |
||||
Balance: new(big.Int).Exp(big.NewInt(2), big.NewInt(128), nil), |
||||
} |
||||
} |
||||
return genesis |
||||
} |
||||
|
||||
func makeFullNode(genesis *core.Genesis) (*node.Node, *eth.Ethereum, *catalyst.ConsensusAPI, error) { |
||||
// Define the basic configurations for the Ethereum node
|
||||
datadir, _ := ioutil.TempDir("", "") |
||||
|
||||
config := &node.Config{ |
||||
Name: "geth", |
||||
Version: params.Version, |
||||
DataDir: datadir, |
||||
P2P: p2p.Config{ |
||||
ListenAddr: "0.0.0.0:0", |
||||
NoDiscovery: true, |
||||
MaxPeers: 25, |
||||
}, |
||||
UseLightweightKDF: true, |
||||
} |
||||
// Create the node and configure a full Ethereum node on it
|
||||
stack, err := node.New(config) |
||||
if err != nil { |
||||
return nil, nil, nil, err |
||||
} |
||||
econfig := ðconfig.Config{ |
||||
Genesis: genesis, |
||||
NetworkId: genesis.Config.ChainID.Uint64(), |
||||
SyncMode: downloader.FullSync, |
||||
DatabaseCache: 256, |
||||
DatabaseHandles: 256, |
||||
TxPool: core.DefaultTxPoolConfig, |
||||
GPO: ethconfig.Defaults.GPO, |
||||
Ethash: ethconfig.Defaults.Ethash, |
||||
Miner: miner.Config{ |
||||
GasFloor: genesis.GasLimit * 9 / 10, |
||||
GasCeil: genesis.GasLimit * 11 / 10, |
||||
GasPrice: big.NewInt(1), |
||||
Recommit: 10 * time.Second, // Disable the recommit
|
||||
}, |
||||
LightServ: 100, |
||||
LightPeers: 10, |
||||
LightNoSyncServe: true, |
||||
} |
||||
ethBackend, err := eth.New(stack, econfig) |
||||
if err != nil { |
||||
return nil, nil, nil, err |
||||
} |
||||
_, err = les.NewLesServer(stack, ethBackend, econfig) |
||||
if err != nil { |
||||
log.Crit("Failed to create the LES server", "err", err) |
||||
} |
||||
err = stack.Start() |
||||
return stack, ethBackend, catalyst.NewConsensusAPI(ethBackend, nil), err |
||||
} |
||||
|
||||
func makeLightNode(genesis *core.Genesis) (*node.Node, *les.LightEthereum, *catalyst.ConsensusAPI, error) { |
||||
// Define the basic configurations for the Ethereum node
|
||||
datadir, _ := ioutil.TempDir("", "") |
||||
|
||||
config := &node.Config{ |
||||
Name: "geth", |
||||
Version: params.Version, |
||||
DataDir: datadir, |
||||
P2P: p2p.Config{ |
||||
ListenAddr: "0.0.0.0:0", |
||||
NoDiscovery: true, |
||||
MaxPeers: 25, |
||||
}, |
||||
UseLightweightKDF: true, |
||||
} |
||||
// Create the node and configure a full Ethereum node on it
|
||||
stack, err := node.New(config) |
||||
if err != nil { |
||||
return nil, nil, nil, err |
||||
} |
||||
lesBackend, err := les.New(stack, ðconfig.Config{ |
||||
Genesis: genesis, |
||||
NetworkId: genesis.Config.ChainID.Uint64(), |
||||
SyncMode: downloader.LightSync, |
||||
DatabaseCache: 256, |
||||
DatabaseHandles: 256, |
||||
TxPool: core.DefaultTxPoolConfig, |
||||
GPO: ethconfig.Defaults.GPO, |
||||
Ethash: ethconfig.Defaults.Ethash, |
||||
LightPeers: 10, |
||||
}) |
||||
if err != nil { |
||||
return nil, nil, nil, err |
||||
} |
||||
err = stack.Start() |
||||
return stack, lesBackend, catalyst.NewConsensusAPI(nil, lesBackend), err |
||||
} |
||||
|
||||
func eth2types(typ nodetype) bool { |
||||
if typ == eth2LightClient || typ == eth2NormalNode || typ == eth2MiningNode { |
||||
return true |
||||
} |
||||
return false |
||||
} |
Loading…
Reference in new issue