Skip to content

Commit 386d78f

Browse files
committed
core/rawdb: add test
1 parent 44b1553 commit 386d78f

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

core/rawdb/accessors_chain.go

+3
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,9 @@ func writeAncientBlock(op ethdb.AncientWriteOp, block *types.Block, header *type
729729
return nil
730730
}
731731

732+
// WriteAncientHeaderChain writes the supplied headers along with nil block
733+
// bodies and receipts into the ancient store. It's supposed to be used for
734+
// storing chain segment before the chain cutoff.
732735
func WriteAncientHeaderChain(db ethdb.AncientWriter, headers []*types.Header) (int64, error) {
733736
return db.ModifyAncients(func(op ethdb.AncientWriteOp) error {
734737
for _, header := range headers {

core/rawdb/accessors_chain_test.go

+42
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,48 @@ func TestAncientStorage(t *testing.T) {
464464
}
465465
}
466466

467+
func TestWriteAncientHeaderChain(t *testing.T) {
468+
db, err := NewDatabaseWithFreezer(NewMemoryDatabase(), t.TempDir(), "", false)
469+
if err != nil {
470+
t.Fatalf("failed to create database with ancient backend")
471+
}
472+
defer db.Close()
473+
474+
// Create a test block
475+
var headers []*types.Header
476+
headers = append(headers, &types.Header{
477+
Number: big.NewInt(0),
478+
Extra: []byte("test block"),
479+
UncleHash: types.EmptyUncleHash,
480+
TxHash: types.EmptyTxsHash,
481+
ReceiptHash: types.EmptyReceiptsHash,
482+
})
483+
headers = append(headers, &types.Header{
484+
Number: big.NewInt(1),
485+
Extra: []byte("test block"),
486+
UncleHash: types.EmptyUncleHash,
487+
TxHash: types.EmptyTxsHash,
488+
ReceiptHash: types.EmptyReceiptsHash,
489+
})
490+
// Write and verify the header in the database
491+
WriteAncientHeaderChain(db, headers)
492+
493+
for _, header := range headers {
494+
if blob := ReadHeaderRLP(db, header.Hash(), header.Number.Uint64()); len(blob) == 0 {
495+
t.Fatalf("no header returned")
496+
}
497+
if h := ReadCanonicalHash(db, header.Number.Uint64()); h != header.Hash() {
498+
t.Fatalf("no canonical hash returned")
499+
}
500+
if blob := ReadBodyRLP(db, header.Hash(), header.Number.Uint64()); len(blob) != 0 {
501+
t.Fatalf("unexpected body returned")
502+
}
503+
if blob := ReadReceiptsRLP(db, header.Hash(), header.Number.Uint64()); len(blob) != 0 {
504+
t.Fatalf("unexpected body returned")
505+
}
506+
}
507+
}
508+
467509
func TestCanonicalHashIteration(t *testing.T) {
468510
var cases = []struct {
469511
from, to uint64

eth/downloader/beaconsync.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package downloader
1818

1919
import (
20-
"errors"
2120
"fmt"
2221
"sync"
2322
"time"
@@ -308,7 +307,7 @@ func (d *Downloader) fetchHeaders(from uint64) error {
308307
}
309308
}
310309
if h == nil {
311-
return errors.New("header at chain cutoff is not available")
310+
return fmt.Errorf("header at chain cutoff is not available, cutoff: %d", d.chainCutoffNumber)
312311
}
313312
if h.Hash() != d.chainCutoffHash {
314313
return fmt.Errorf("header at chain cutoff mismatched, want: %v, got: %v", d.chainCutoffHash, h.Hash())

0 commit comments

Comments
 (0)