-
Notifications
You must be signed in to change notification settings - Fork 20.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
eth: core: implement final block #24282
Changes from 3 commits
e068edc
38b368b
eb285f2
5d58d48
2c43cc0
a4a0182
665cee2
03241f0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,9 +47,10 @@ import ( | |
) | ||
|
||
var ( | ||
headBlockGauge = metrics.NewRegisteredGauge("chain/head/block", nil) | ||
headHeaderGauge = metrics.NewRegisteredGauge("chain/head/header", nil) | ||
headFastBlockGauge = metrics.NewRegisteredGauge("chain/head/receipt", nil) | ||
headBlockGauge = metrics.NewRegisteredGauge("chain/head/block", nil) | ||
headHeaderGauge = metrics.NewRegisteredGauge("chain/head/header", nil) | ||
headFastBlockGauge = metrics.NewRegisteredGauge("chain/head/receipt", nil) | ||
headFinalizedBlockGauge = metrics.NewRegisteredGauge("chain/head/finalized", nil) | ||
|
||
accountReadTimer = metrics.NewRegisteredTimer("chain/account/reads", nil) | ||
accountHashTimer = metrics.NewRegisteredTimer("chain/account/hashes", nil) | ||
|
@@ -187,8 +188,9 @@ type BlockChain struct { | |
// Readers don't need to take it, they can just read the database. | ||
chainmu *syncx.ClosableMutex | ||
|
||
currentBlock atomic.Value // Current head of the block chain | ||
currentFastBlock atomic.Value // Current head of the fast-sync chain (may be above the block chain!) | ||
currentBlock atomic.Value // Current head of the block chain | ||
currentFastBlock atomic.Value // Current head of the fast-sync chain (may be above the block chain!) | ||
currentFinalizedBlock atomic.Value // Current finalized head | ||
|
||
stateCache state.Database // State database to reuse between imports (contains state cache) | ||
bodyCache *lru.Cache // Cache for the most recent block bodies | ||
|
@@ -264,6 +266,7 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, chainConfig *par | |
var nilBlock *types.Block | ||
bc.currentBlock.Store(nilBlock) | ||
bc.currentFastBlock.Store(nilBlock) | ||
bc.currentFinalizedBlock.Store(nilBlock) | ||
|
||
// Initialize the chain with ancient data if it isn't empty. | ||
var txIndexBlock uint64 | ||
|
@@ -460,8 +463,17 @@ func (bc *BlockChain) loadLastState() error { | |
headFastBlockGauge.Update(int64(block.NumberU64())) | ||
} | ||
} | ||
|
||
// Restore the last known finalized block | ||
if head := rawdb.ReadFinalizedBlockHash(bc.db); head != (common.Hash{}) { | ||
if block := bc.GetBlockByHash(head); block != nil { | ||
bc.currentFinalizedBlock.Store(block) | ||
headFinalizedBlockGauge.Update(int64(block.NumberU64())) | ||
} | ||
} | ||
// Issue a status log for the user | ||
currentFastBlock := bc.CurrentFastBlock() | ||
currentFinalizedBlock := bc.CurrentFinalizedBlock() | ||
|
||
headerTd := bc.GetTd(currentHeader.Hash(), currentHeader.Number.Uint64()) | ||
blockTd := bc.GetTd(currentBlock.Hash(), currentBlock.NumberU64()) | ||
|
@@ -470,6 +482,11 @@ func (bc *BlockChain) loadLastState() error { | |
log.Info("Loaded most recent local header", "number", currentHeader.Number, "hash", currentHeader.Hash(), "td", headerTd, "age", common.PrettyAge(time.Unix(int64(currentHeader.Time), 0))) | ||
log.Info("Loaded most recent local full block", "number", currentBlock.Number(), "hash", currentBlock.Hash(), "td", blockTd, "age", common.PrettyAge(time.Unix(int64(currentBlock.Time()), 0))) | ||
log.Info("Loaded most recent local fast block", "number", currentFastBlock.Number(), "hash", currentFastBlock.Hash(), "td", fastTd, "age", common.PrettyAge(time.Unix(int64(currentFastBlock.Time()), 0))) | ||
|
||
if currentFinalizedBlock != nil { | ||
finalTd := bc.GetTd(currentFinalizedBlock.Hash(), currentFinalizedBlock.NumberU64()) | ||
log.Info("Loaded most recent local finalized block", "number", currentFinalizedBlock.Number(), "hash", currentFinalizedBlock.Hash(), "td", finalTd, "age", common.PrettyAge(time.Unix(int64(currentFinalizedBlock.Time()), 0))) | ||
} | ||
if pivot := rawdb.ReadLastPivotNumber(bc.db); pivot != nil { | ||
log.Info("Loaded last fast-sync pivot marker", "number", *pivot) | ||
} | ||
|
@@ -484,6 +501,13 @@ func (bc *BlockChain) SetHead(head uint64) error { | |
return err | ||
} | ||
|
||
// SetFinalized sets the finalized block. | ||
func (bc *BlockChain) SetFinalized(block *types.Block) { | ||
bc.currentFinalizedBlock.Store(block) | ||
rawdb.WriteFinalizedBlockHash(bc.db, block.Hash()) | ||
headFinalizedBlockGauge.Update(int64(block.NumberU64())) | ||
Comment on lines
+506
to
+508
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe it doesn't matter (?)...
So there may be a mismatch between in-memory marker and disk-marker. But as I said, this may not be an issue at all (we seem to rely on the disk only for bootstrap, and use in-memory marker during normal operations) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm it means you could roll back finalization a couple of blocks for the user if a node restarts. |
||
} | ||
|
||
// setHeadBeyondRoot rewinds the local chain to a new head with the extra condition | ||
// that the rewind must pass the specified state root. This method is meant to be | ||
// used when rewinding with snapshots enabled to ensure that we go back further than | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wondering if it makes sense to add the TD, or rather to remove it from all other logs once PoS switchover hits.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm maybe just remove all of them in a big swoop after the merge?