core, eth: improve delivery speed on header requests (#23105)
This PR reduces the amount of work we do when answering header queries, e.g. when a peer is syncing from us. For some items, e.g block bodies, when we read the rlp-data from database, we plug it directly into the response package. We didn't do that for headers, but instead read headers-rlp, decode to types.Header, and re-encode to rlp. This PR changes that to keep it in RLP-form as much as possible. When a node is syncing from us, it typically requests 192 contiguous headers. On master it has the following effect: - For headers not in ancient: 2 db lookups. One for translating hash->number (even though the request is by number), and another for reading by hash (this latter one is sometimes cached). - For headers in ancient: 1 file lookup/syscall for translating hash->number (even though the request is by number), and another for reading the header itself. After this, it also performes a hashing of the header, to ensure that the hash is what it expected. In this PR, I instead move the logic for "give me a sequence of blocks" into the lower layers, where the database can determine how and what to read from leveldb and/or ancients. There are basically four types of requests; three of them are improved this way. The fourth, by hash going backwards, is more tricky to optimize. However, since we know that the gap is 0, we can look up by the parentHash, and stlil shave off all the number->hash lookups. The gapped collection can be optimized similarly, as a follow-up, at least in three out of four cases. Co-authored-by: Felix Lange <fjl@twurst.com>
This commit is contained in:
committed by
GitHub
parent
7f7877a023
commit
db03faa10d
@ -883,3 +883,67 @@ func BenchmarkDecodeRLPLogs(b *testing.B) {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestHeadersRLPStorage(t *testing.T) {
|
||||
// Have N headers in the freezer
|
||||
frdir, err := ioutil.TempDir("", "")
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create temp freezer dir: %v", err)
|
||||
}
|
||||
defer os.Remove(frdir)
|
||||
|
||||
db, err := NewDatabaseWithFreezer(NewMemoryDatabase(), frdir, "", false)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create database with ancient backend")
|
||||
}
|
||||
defer db.Close()
|
||||
// Create blocks
|
||||
var chain []*types.Block
|
||||
var pHash common.Hash
|
||||
for i := 0; i < 100; i++ {
|
||||
block := types.NewBlockWithHeader(&types.Header{
|
||||
Number: big.NewInt(int64(i)),
|
||||
Extra: []byte("test block"),
|
||||
UncleHash: types.EmptyUncleHash,
|
||||
TxHash: types.EmptyRootHash,
|
||||
ReceiptHash: types.EmptyRootHash,
|
||||
ParentHash: pHash,
|
||||
})
|
||||
chain = append(chain, block)
|
||||
pHash = block.Hash()
|
||||
}
|
||||
var receipts []types.Receipts = make([]types.Receipts, 100)
|
||||
// Write first half to ancients
|
||||
WriteAncientBlocks(db, chain[:50], receipts[:50], big.NewInt(100))
|
||||
// Write second half to db
|
||||
for i := 50; i < 100; i++ {
|
||||
WriteCanonicalHash(db, chain[i].Hash(), chain[i].NumberU64())
|
||||
WriteBlock(db, chain[i])
|
||||
}
|
||||
checkSequence := func(from, amount int) {
|
||||
headersRlp := ReadHeaderRange(db, uint64(from), uint64(amount))
|
||||
if have, want := len(headersRlp), amount; have != want {
|
||||
t.Fatalf("have %d headers, want %d", have, want)
|
||||
}
|
||||
for i, headerRlp := range headersRlp {
|
||||
var header types.Header
|
||||
if err := rlp.DecodeBytes(headerRlp, &header); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if have, want := header.Number.Uint64(), uint64(from-i); have != want {
|
||||
t.Fatalf("wrong number, have %d want %d", have, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
checkSequence(99, 20) // Latest block and 19 parents
|
||||
checkSequence(99, 50) // Latest block -> all db blocks
|
||||
checkSequence(99, 51) // Latest block -> one from ancients
|
||||
checkSequence(99, 52) // Latest blocks -> two from ancients
|
||||
checkSequence(50, 2) // One from db, one from ancients
|
||||
checkSequence(49, 1) // One from ancients
|
||||
checkSequence(49, 50) // All ancient ones
|
||||
checkSequence(99, 100) // All blocks
|
||||
checkSequence(0, 1) // Only genesis
|
||||
checkSequence(1, 1) // Only block 1
|
||||
checkSequence(1, 2) // Genesis + block 1
|
||||
}
|
||||
|
Reference in New Issue
Block a user