Skip to content

Commit c10a0a6

Browse files
authored
eth: request id dispatcher and direct req/reply APIs (#23576)
* eth: request ID based message dispatcher * eth: fix dispatcher cancellation, rework fetchers idleness tracker * eth/downloader: drop peers who refuse to serve advertised chains
1 parent 3038e48 commit c10a0a6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+3195
-3382
lines changed

cmd/utils/flags.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ var (
215215
defaultSyncMode = ethconfig.Defaults.SyncMode
216216
SyncModeFlag = TextMarshalerFlag{
217217
Name: "syncmode",
218-
Usage: `Blockchain sync mode ("fast", "full", "snap" or "light")`,
218+
Usage: `Blockchain sync mode ("snap", "full" or "light")`,
219219
Value: &defaultSyncMode,
220220
}
221221
GCModeFlag = cli.StringFlag{

core/blockchain.go

+9-15
Original file line numberDiff line numberDiff line change
@@ -629,9 +629,9 @@ func (bc *BlockChain) setHeadBeyondRoot(head uint64, root common.Hash, repair bo
629629
return rootNumber, bc.loadLastState()
630630
}
631631

632-
// FastSyncCommitHead sets the current head block to the one defined by the hash
632+
// SnapSyncCommitHead sets the current head block to the one defined by the hash
633633
// irrelevant what the chain contents were prior.
634-
func (bc *BlockChain) FastSyncCommitHead(hash common.Hash) error {
634+
func (bc *BlockChain) SnapSyncCommitHead(hash common.Hash) error {
635635
// Make sure that both the block as well at its state trie exists
636636
block := bc.GetBlockByHash(hash)
637637
if block == nil {
@@ -736,30 +736,24 @@ func (bc *BlockChain) ExportN(w io.Writer, first uint64, last uint64) error {
736736
//
737737
// Note, this function assumes that the `mu` mutex is held!
738738
func (bc *BlockChain) writeHeadBlock(block *types.Block) {
739-
// If the block is on a side chain or an unknown one, force other heads onto it too
740-
updateHeads := rawdb.ReadCanonicalHash(bc.db, block.NumberU64()) != block.Hash()
741-
742739
// Add the block to the canonical chain number scheme and mark as the head
743740
batch := bc.db.NewBatch()
741+
rawdb.WriteHeadHeaderHash(batch, block.Hash())
742+
rawdb.WriteHeadFastBlockHash(batch, block.Hash())
744743
rawdb.WriteCanonicalHash(batch, block.Hash(), block.NumberU64())
745744
rawdb.WriteTxLookupEntriesByBlock(batch, block)
746745
rawdb.WriteHeadBlockHash(batch, block.Hash())
747746

748-
// If the block is better than our head or is on a different chain, force update heads
749-
if updateHeads {
750-
rawdb.WriteHeadHeaderHash(batch, block.Hash())
751-
rawdb.WriteHeadFastBlockHash(batch, block.Hash())
752-
}
753747
// Flush the whole batch into the disk, exit the node if failed
754748
if err := batch.Write(); err != nil {
755749
log.Crit("Failed to update chain indexes and markers", "err", err)
756750
}
757751
// Update all in-memory chain markers in the last step
758-
if updateHeads {
759-
bc.hc.SetCurrentHeader(block.Header())
760-
bc.currentFastBlock.Store(block)
761-
headFastBlockGauge.Update(int64(block.NumberU64()))
762-
}
752+
bc.hc.SetCurrentHeader(block.Header())
753+
754+
bc.currentFastBlock.Store(block)
755+
headFastBlockGauge.Update(int64(block.NumberU64()))
756+
763757
bc.currentBlock.Store(block)
764758
headBlockGauge.Update(int64(block.NumberU64()))
765759
}

core/blockchain_repair_test.go

+112-112
Large diffs are not rendered by default.

core/blockchain_sethead_test.go

+120-120
Large diffs are not rendered by default.

core/blockchain_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -2637,7 +2637,7 @@ func TestTransactionIndices(t *testing.T) {
26372637
}
26382638
}
26392639

2640-
func TestSkipStaleTxIndicesInFastSync(t *testing.T) {
2640+
func TestSkipStaleTxIndicesInSnapSync(t *testing.T) {
26412641
// Configure and generate a sample block chain
26422642
var (
26432643
gendb = rawdb.NewMemoryDatabase()

core/chain_makers.go

+22
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,28 @@ func (b *BlockGen) TxNonce(addr common.Address) uint64 {
155155

156156
// AddUncle adds an uncle header to the generated block.
157157
func (b *BlockGen) AddUncle(h *types.Header) {
158+
// The uncle will have the same timestamp and auto-generated difficulty
159+
h.Time = b.header.Time
160+
161+
var parent *types.Header
162+
for i := b.i - 1; i >= 0; i-- {
163+
if b.chain[i].Hash() == h.ParentHash {
164+
parent = b.chain[i].Header()
165+
break
166+
}
167+
}
168+
chainreader := &fakeChainReader{config: b.config}
169+
h.Difficulty = b.engine.CalcDifficulty(chainreader, b.header.Time, parent)
170+
171+
// The gas limit and price should be derived from the parent
172+
h.GasLimit = parent.GasLimit
173+
if b.config.IsLondon(h.Number) {
174+
h.BaseFee = misc.CalcBaseFee(b.config, parent)
175+
if !b.config.IsLondon(parent.Number) {
176+
parentGasLimit := parent.GasLimit * params.ElasticityMultiplier
177+
h.GasLimit = CalcGasLimit(parentGasLimit, parentGasLimit)
178+
}
179+
}
158180
b.uncles = append(b.uncles, h)
159181
}
160182

core/rawdb/accessors_chain.go

-18
Original file line numberDiff line numberDiff line change
@@ -242,24 +242,6 @@ func WriteLastPivotNumber(db ethdb.KeyValueWriter, pivot uint64) {
242242
}
243243
}
244244

245-
// ReadFastTrieProgress retrieves the number of tries nodes fast synced to allow
246-
// reporting correct numbers across restarts.
247-
func ReadFastTrieProgress(db ethdb.KeyValueReader) uint64 {
248-
data, _ := db.Get(fastTrieProgressKey)
249-
if len(data) == 0 {
250-
return 0
251-
}
252-
return new(big.Int).SetBytes(data).Uint64()
253-
}
254-
255-
// WriteFastTrieProgress stores the fast sync trie process counter to support
256-
// retrieving it across restarts.
257-
func WriteFastTrieProgress(db ethdb.KeyValueWriter, count uint64) {
258-
if err := db.Put(fastTrieProgressKey, new(big.Int).SetUint64(count).Bytes()); err != nil {
259-
log.Crit("Failed to store fast sync trie progress", "err", err)
260-
}
261-
}
262-
263245
// ReadTxIndexTail retrieves the number of oldest indexed block
264246
// whose transaction indices has been indexed. If the corresponding entry
265247
// is non-existent in database it means the indexing has been finished.

core/rawdb/accessors_snapshot.go

-8
Original file line numberDiff line numberDiff line change
@@ -208,11 +208,3 @@ func WriteSnapshotSyncStatus(db ethdb.KeyValueWriter, status []byte) {
208208
log.Crit("Failed to store snapshot sync status", "err", err)
209209
}
210210
}
211-
212-
// DeleteSnapshotSyncStatus deletes the serialized sync status saved at the last
213-
// shutdown
214-
func DeleteSnapshotSyncStatus(db ethdb.KeyValueWriter) {
215-
if err := db.Delete(snapshotSyncStatusKey); err != nil {
216-
log.Crit("Failed to remove snapshot sync status", "err", err)
217-
}
218-
}

0 commit comments

Comments
 (0)