Skip to content

Commit 4cc9b43

Browse files
authored
Merge pull request #518 from bane-labs/revert-finalized-behavior
*: revert customized behaviors about finalized block
2 parents 4ed40ed + 1c902b3 commit 4cc9b43

File tree

16 files changed

+47
-249
lines changed

16 files changed

+47
-249
lines changed

consensus/consensus.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,3 @@ type Engine interface {
118118
// Close terminates any background threads maintained by the consensus engine.
119119
Close() error
120120
}
121-
122-
// PoS consensus engine.
123-
type PoS interface {
124-
Engine
125-
126-
// GetFinalizedHeader retrieves the finalized header for a given header.
127-
GetFinalizedHeader(chain ChainHeaderReader, header *types.Header) *types.Header
128-
}

consensus/dbft/dbft.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2959,15 +2959,3 @@ func unpackContractExecutionResult(res interface{}, result *core.ExecutionResult
29592959
}
29602960
return contractAbi.UnpackIntoInterface(&res, method, result.Return())
29612961
}
2962-
2963-
// GetFinalizedHeader returns highest finalized block header.
2964-
func (c *DBFT) GetFinalizedHeader(chain consensus.ChainHeaderReader, header *types.Header) *types.Header {
2965-
if chain == nil || header == nil {
2966-
return nil
2967-
}
2968-
if !chain.Config().IsNeoXDKG(header.Number) || header.Number.Uint64() < 1 {
2969-
return chain.GetHeaderByNumber(0)
2970-
}
2971-
2972-
return chain.GetHeaderByNumber(header.Number.Uint64() - 1)
2973-
}

core/blockchain.go

Lines changed: 14 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -244,16 +244,15 @@ type BlockChain struct {
244244
statedb *state.CachingDB // State database to reuse between imports (contains state cache)
245245
txIndexer *txIndexer // Transaction indexer, might be nil if not enabled
246246

247-
hc *HeaderChain
248-
rmLogsFeed event.Feed
249-
chainFeed event.Feed
250-
chainHeadFeed event.Feed
251-
logsFeed event.Feed
252-
blockProcFeed event.Feed
253-
finalizedHeaderFeed event.Feed
254-
blockProcCounter int32
255-
scope event.SubscriptionScope
256-
genesisBlock *types.Block
247+
hc *HeaderChain
248+
rmLogsFeed event.Feed
249+
chainFeed event.Feed
250+
chainHeadFeed event.Feed
251+
logsFeed event.Feed
252+
blockProcFeed event.Feed
253+
blockProcCounter int32
254+
scope event.SubscriptionScope
255+
genesisBlock *types.Block
257256

258257
// This mutex synchronizes chain write operations.
259258
// Readers don't need to take it, they can just read the database.
@@ -525,17 +524,6 @@ func (bc *BlockChain) empty() bool {
525524
return true
526525
}
527526

528-
// GetFinalizedNumber returns the highest finalized number before the specific block.
529-
func (bc *BlockChain) GetFinalizedNumber(header *types.Header) uint64 {
530-
if p, ok := bc.engine.(consensus.PoS); ok {
531-
if finalizedHeader := p.GetFinalizedHeader(bc, header); finalizedHeader != nil {
532-
return finalizedHeader.Number.Uint64()
533-
}
534-
}
535-
536-
return 0
537-
}
538-
539527
// loadLastState loads the last known chain state from the database. This method
540528
// assumes that the chain manager mutex is held.
541529
func (bc *BlockChain) loadLastState() error {
@@ -611,7 +599,8 @@ func (bc *BlockChain) loadLastState() error {
611599

612600
// Issue a status log for the user
613601
var (
614-
currentSnapBlock = bc.CurrentSnapBlock()
602+
currentSnapBlock = bc.CurrentSnapBlock()
603+
currentFinalBlock = bc.CurrentFinalBlock()
615604

616605
headerTd = bc.GetTd(headHeader.Hash(), headHeader.Number.Uint64())
617606
blockTd = bc.GetTd(headBlock.Hash(), headBlock.NumberU64())
@@ -624,14 +613,9 @@ func (bc *BlockChain) loadLastState() error {
624613
snapTd := bc.GetTd(currentSnapBlock.Hash(), currentSnapBlock.Number.Uint64())
625614
log.Info("Loaded most recent local snap block", "number", currentSnapBlock.Number, "hash", currentSnapBlock.Hash(), "td", snapTd, "age", common.PrettyAge(time.Unix(int64(currentSnapBlock.Time), 0)))
626615
}
627-
if p, ok := bc.engine.(consensus.PoS); ok {
628-
if currentFinalizedHeader := p.GetFinalizedHeader(bc, headHeader); currentFinalizedHeader != nil {
629-
bc.currentFinalBlock.Store(currentFinalizedHeader)
630-
if currentFinalizedBlock := bc.GetBlockByHash(currentFinalizedHeader.Hash()); currentFinalizedBlock != nil {
631-
finalTd := bc.GetTd(currentFinalizedBlock.Hash(), currentFinalizedBlock.NumberU64())
632-
log.Info("Loaded most recent local finalized block", "number", currentFinalizedBlock.Number(), "hash", currentFinalizedBlock.Hash(), "root", currentFinalizedBlock.Root(), "td", finalTd, "age", common.PrettyAge(time.Unix(int64(currentFinalizedBlock.Time()), 0)))
633-
}
634-
}
616+
if currentFinalBlock != nil {
617+
finalTd := bc.GetTd(currentFinalBlock.Hash(), currentFinalBlock.Number.Uint64())
618+
log.Info("Loaded most recent local finalized block", "number", currentFinalBlock.Number, "hash", currentFinalBlock.Hash(), "td", finalTd, "age", common.PrettyAge(time.Unix(int64(currentFinalBlock.Time), 0)))
635619
}
636620
if pivot := rawdb.ReadLastPivotNumber(bc.db); pivot != nil {
637621
log.Info("Loaded last snap-sync pivot marker", "number", *pivot)
@@ -1716,24 +1700,13 @@ func (bc *BlockChain) writeBlockAndSetHead(block *types.Block, receipts []*types
17161700
if len(logs) > 0 {
17171701
bc.logsFeed.Send(logs)
17181702
}
1719-
1720-
var finalizedHeader *types.Header
1721-
if p, ok := bc.Engine().(consensus.PoS); ok {
1722-
if finalizedHeader = p.GetFinalizedHeader(bc, block.Header()); finalizedHeader != nil {
1723-
bc.SetFinalized(finalizedHeader)
1724-
}
1725-
}
1726-
17271703
// In theory, we should fire a ChainHeadEvent when we inject
17281704
// a canonical block, but sometimes we can insert a batch of
17291705
// canonical blocks. Avoid firing too many ChainHeadEvents,
17301706
// we will fire an accumulated ChainHeadEvent and disable fire
17311707
// event here.
17321708
if emitHeadEvent {
17331709
bc.chainHeadFeed.Send(ChainHeadEvent{Header: block.Header()})
1734-
if finalizedHeader != nil {
1735-
bc.finalizedHeaderFeed.Send(FinalizedHeaderEvent{finalizedHeader})
1736-
}
17371710
}
17381711
}
17391712
return status, nil
@@ -1827,11 +1800,6 @@ func (bc *BlockChain) insertChain(chain types.Blocks, setHead bool, makeWitness
18271800
defer func() {
18281801
if lastCanon != nil && bc.CurrentBlock().Hash() == lastCanon.Hash() {
18291802
bc.chainHeadFeed.Send(ChainHeadEvent{Header: lastCanon.Header()})
1830-
if p, ok := bc.Engine().(consensus.PoS); ok {
1831-
if finalizedHeader := p.GetFinalizedHeader(bc, lastCanon.Header()); finalizedHeader != nil {
1832-
bc.finalizedHeaderFeed.Send(FinalizedHeaderEvent{finalizedHeader})
1833-
}
1834-
}
18351803
}
18361804
}()
18371805
// Start the parallel header verifier

core/blockchain_reader.go

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -54,29 +54,13 @@ func (bc *BlockChain) CurrentSnapBlock() *types.Header {
5454
// CurrentFinalBlock retrieves the current finalized block of the canonical
5555
// chain. The block is retrieved from the blockchain's internal cache.
5656
func (bc *BlockChain) CurrentFinalBlock() *types.Header {
57-
if p, ok := bc.engine.(consensus.PoS); ok {
58-
currentHeader := bc.CurrentHeader()
59-
if currentHeader == nil {
60-
return nil
61-
}
62-
return p.GetFinalizedHeader(bc, currentHeader)
63-
}
64-
65-
return nil
57+
return bc.currentFinalBlock.Load()
6658
}
6759

6860
// CurrentSafeBlock retrieves the current safe block of the canonical
6961
// chain. The block is retrieved from the blockchain's internal cache.
7062
func (bc *BlockChain) CurrentSafeBlock() *types.Header {
71-
if p, ok := bc.engine.(consensus.PoS); ok {
72-
currentHeader := bc.CurrentHeader()
73-
if currentHeader == nil {
74-
return nil
75-
}
76-
return p.GetFinalizedHeader(bc, currentHeader)
77-
}
78-
79-
return nil
63+
return bc.currentSafeBlock.Load()
8064
}
8165

8266
// HasHeader checks if a block header is present in the database or not, caching
@@ -477,8 +461,3 @@ func (bc *BlockChain) SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscript
477461
func (bc *BlockChain) SubscribeBlockProcessingEvent(ch chan<- bool) event.Subscription {
478462
return bc.scope.Track(bc.blockProcFeed.Subscribe(ch))
479463
}
480-
481-
// SubscribeFinalizedHeaderEvent registers a subscription of FinalizedHeaderEvent.
482-
func (bc *BlockChain) SubscribeFinalizedHeaderEvent(ch chan<- FinalizedHeaderEvent) event.Subscription {
483-
return bc.scope.Track(bc.finalizedHeaderFeed.Subscribe(ch))
484-
}

core/events.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@ type NewMinedBlockEvent struct{ Block *types.Block }
3232
// RemovedLogsEvent is posted when a reorg happens
3333
type RemovedLogsEvent struct{ Logs []*types.Log }
3434

35-
// FinalizedHeaderEvent is posted when a finalized header is reached.
36-
type FinalizedHeaderEvent struct{ Header *types.Header }
37-
3835
type ChainEvent struct {
3936
Header *types.Header
4037
}

core/headerchain.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -99,17 +99,6 @@ func NewHeaderChain(chainDb ethdb.Database, config *params.ChainConfig, engine c
9999
return hc, nil
100100
}
101101

102-
// GetFinalizedNumber returns the highest finalized number before the specific block.
103-
func (hc *HeaderChain) GetFinalizedNumber(header *types.Header) uint64 {
104-
if p, ok := hc.engine.(consensus.PoS); ok {
105-
if finalizedHeader := p.GetFinalizedHeader(hc, header); finalizedHeader != nil {
106-
return finalizedHeader.Number.Uint64()
107-
}
108-
}
109-
110-
return 0
111-
}
112-
113102
// GetBlockNumber retrieves the block number belonging to the given hash
114103
// from the cache or database
115104
func (hc *HeaderChain) GetBlockNumber(hash common.Hash) *uint64 {

core/txpool/blobpool/blobpool.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -840,7 +840,9 @@ func (p *BlobPool) Reset(oldHead, newHead *types.Header) {
840840
}
841841
// Flush out any blobs from limbo that are older than the latest finality
842842
if p.chain.Config().IsCancun(p.head.Number, p.head.Time) {
843-
p.limbo.finalize(p.chain.CurrentFinalBlock())
843+
if h := p.chain.CurrentFinalBlock(); h != nil {
844+
p.limbo.finalize(h)
845+
}
844846
}
845847
// Reset the price heap for the new set of basefee/blobfee pairs
846848
var (

eth/api_backend.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -312,10 +312,6 @@ func (b *EthAPIBackend) SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) e
312312
return b.eth.BlockChain().SubscribeChainHeadEvent(ch)
313313
}
314314

315-
func (b *EthAPIBackend) SubscribeFinalizedHeaderEvent(ch chan<- core.FinalizedHeaderEvent) event.Subscription {
316-
return b.eth.BlockChain().SubscribeFinalizedHeaderEvent(ch)
317-
}
318-
319315
func (b *EthAPIBackend) SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription {
320316
return b.eth.BlockChain().SubscribeLogsEvent(ch)
321317
}

eth/ethconfig/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ type Config struct {
197197
// Clique is allowed for now to live standalone, but ethash is forbidden and can
198198
// only exist on already merged networks.
199199
func CreateConsensusEngine(config *params.ChainConfig, db ethdb.Database, statisticsCfg dbft.StatisticsConfig) (consensus.Engine, error) {
200+
// TODO: config TTD correctly and move this check back after Clique
200201
if config.DBFT != nil {
201202
bft, err := dbft.New(config, db, statisticsCfg)
202203
if err != nil {

eth/filters/api.go

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -273,65 +273,6 @@ func (api *FilterAPI) NewHeads(ctx context.Context) (*rpc.Subscription, error) {
273273
return rpcSub, nil
274274
}
275275

276-
// NewFinalizedHeaderFilter creates a filter that fetches finalized headers that are reached.
277-
func (api *FilterAPI) NewFinalizedHeaderFilter() rpc.ID {
278-
var (
279-
headers = make(chan *types.Header)
280-
headerSub = api.events.SubscribeNewFinalizedHeaders(headers)
281-
)
282-
283-
api.filtersMu.Lock()
284-
api.filters[headerSub.ID] = &filter{typ: FinalizedHeadersSubscription, deadline: time.NewTimer(api.timeout), hashes: make([]common.Hash, 0), s: headerSub}
285-
api.filtersMu.Unlock()
286-
287-
go func() {
288-
for {
289-
select {
290-
case h := <-headers:
291-
api.filtersMu.Lock()
292-
if f, found := api.filters[headerSub.ID]; found {
293-
f.hashes = append(f.hashes, h.Hash())
294-
}
295-
api.filtersMu.Unlock()
296-
case <-headerSub.Err():
297-
api.filtersMu.Lock()
298-
delete(api.filters, headerSub.ID)
299-
api.filtersMu.Unlock()
300-
return
301-
}
302-
}
303-
}()
304-
305-
return headerSub.ID
306-
}
307-
308-
// NewFinalizedHeaders send a notification each time a new finalized header is reached.
309-
func (api *FilterAPI) NewFinalizedHeaders(ctx context.Context) (*rpc.Subscription, error) {
310-
notifier, supported := rpc.NotifierFromContext(ctx)
311-
if !supported {
312-
return &rpc.Subscription{}, rpc.ErrNotificationsUnsupported
313-
}
314-
315-
rpcSub := notifier.CreateSubscription()
316-
317-
go func() {
318-
headers := make(chan *types.Header)
319-
headersSub := api.events.SubscribeNewFinalizedHeaders(headers)
320-
defer headersSub.Unsubscribe()
321-
322-
for {
323-
select {
324-
case h := <-headers:
325-
notifier.Notify(rpcSub.ID, h)
326-
case <-rpcSub.Err():
327-
return
328-
}
329-
}
330-
}()
331-
332-
return rpcSub, nil
333-
}
334-
335276
// Logs creates a subscription that fires for all new log that match the given filter criteria.
336277
func (api *FilterAPI) Logs(ctx context.Context, crit FilterCriteria) (*rpc.Subscription, error) {
337278
notifier, supported := rpc.NotifierFromContext(ctx)

0 commit comments

Comments
 (0)