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>
This commit is contained in:
committed by
GitHub
parent
519cf98b69
commit
3038e480f5
@ -59,6 +59,7 @@ type LightChain struct {
|
||||
chainHeadFeed event.Feed
|
||||
scope event.SubscriptionScope
|
||||
genesisBlock *types.Block
|
||||
forker *core.ForkChoice
|
||||
|
||||
bodyCache *lru.Cache // Cache for the most recent block bodies
|
||||
bodyRLPCache *lru.Cache // Cache for the most recent block bodies in RLP encoded format
|
||||
@ -92,6 +93,7 @@ func NewLightChain(odr OdrBackend, config *params.ChainConfig, engine consensus.
|
||||
blockCache: blockCache,
|
||||
engine: engine,
|
||||
}
|
||||
bc.forker = core.NewForkChoice(bc, nil)
|
||||
var err error
|
||||
bc.hc, err = core.NewHeaderChain(odr.Database(), config, bc.engine, bc.getProcInterrupt)
|
||||
if err != nil {
|
||||
@ -369,6 +371,42 @@ func (lc *LightChain) postChainEvents(events []interface{}) {
|
||||
}
|
||||
}
|
||||
|
||||
func (lc *LightChain) InsertHeader(header *types.Header) error {
|
||||
// Verify the header first before obtaining the lock
|
||||
headers := []*types.Header{header}
|
||||
if _, err := lc.hc.ValidateHeaderChain(headers, 100); err != nil {
|
||||
return err
|
||||
}
|
||||
// Make sure only one thread manipulates the chain at once
|
||||
lc.chainmu.Lock()
|
||||
defer lc.chainmu.Unlock()
|
||||
|
||||
lc.wg.Add(1)
|
||||
defer lc.wg.Done()
|
||||
|
||||
_, err := lc.hc.WriteHeaders(headers)
|
||||
log.Info("Inserted header", "number", header.Number, "hash", header.Hash())
|
||||
return err
|
||||
}
|
||||
|
||||
func (lc *LightChain) SetChainHead(header *types.Header) error {
|
||||
lc.chainmu.Lock()
|
||||
defer lc.chainmu.Unlock()
|
||||
|
||||
lc.wg.Add(1)
|
||||
defer lc.wg.Done()
|
||||
|
||||
if err := lc.hc.Reorg([]*types.Header{header}); err != nil {
|
||||
return err
|
||||
}
|
||||
// Emit events
|
||||
block := types.NewBlockWithHeader(header)
|
||||
lc.chainFeed.Send(core.ChainEvent{Block: block, Hash: block.Hash()})
|
||||
lc.chainHeadFeed.Send(core.ChainHeadEvent{Block: block})
|
||||
log.Info("Set the chain head", "number", block.Number(), "hash", block.Hash())
|
||||
return nil
|
||||
}
|
||||
|
||||
// InsertHeaderChain attempts to insert the given header chain in to the local
|
||||
// chain, possibly creating a reorg. If an error is returned, it will return the
|
||||
// index number of the failing header as well an error describing what went wrong.
|
||||
@ -396,25 +434,23 @@ func (lc *LightChain) InsertHeaderChain(chain []*types.Header, checkFreq int) (i
|
||||
lc.wg.Add(1)
|
||||
defer lc.wg.Done()
|
||||
|
||||
status, err := lc.hc.InsertHeaderChain(chain, start)
|
||||
status, err := lc.hc.InsertHeaderChain(chain, start, lc.forker)
|
||||
if err != nil || len(chain) == 0 {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
// Create chain event for the new head block of this insertion.
|
||||
var (
|
||||
events = make([]interface{}, 0, 1)
|
||||
lastHeader = chain[len(chain)-1]
|
||||
block = types.NewBlockWithHeader(lastHeader)
|
||||
)
|
||||
switch status {
|
||||
case core.CanonStatTy:
|
||||
events = append(events, core.ChainEvent{Block: block, Hash: block.Hash()})
|
||||
lc.chainFeed.Send(core.ChainEvent{Block: block, Hash: block.Hash()})
|
||||
lc.chainHeadFeed.Send(core.ChainHeadEvent{Block: block})
|
||||
case core.SideStatTy:
|
||||
events = append(events, core.ChainSideEvent{Block: block})
|
||||
lc.chainSideFeed.Send(core.ChainSideEvent{Block: block})
|
||||
}
|
||||
lc.postChainEvents(events)
|
||||
|
||||
return 0, err
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user