forked from erigontech/erigon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stage_blockhashes.go
51 lines (45 loc) · 1.33 KB
/
stage_blockhashes.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package stagedsync
import (
"encoding/binary"
"github.com/ledgerwatch/turbo-geth/common"
"github.com/ledgerwatch/turbo-geth/common/dbutils"
"github.com/ledgerwatch/turbo-geth/common/etl"
"github.com/ledgerwatch/turbo-geth/core/rawdb"
"github.com/ledgerwatch/turbo-geth/ethdb"
)
func extractHeaders(k []byte, v []byte, next etl.ExtractNextFunc) error {
// We only want to extract entries composed by Block Number + Header Hash
if len(k) != 40 {
return nil
}
return next(k, common.CopyBytes(k[8:]), common.CopyBytes(k[:8]))
}
func SpawnBlockHashStage(s *StageState, stateDB ethdb.Database, tmpdir string, quit <-chan struct{}) error {
headHash := rawdb.ReadHeadHeaderHash(stateDB)
headNumber := rawdb.ReadHeaderNumber(stateDB, headHash)
if s.BlockNumber == *headNumber {
s.Done()
return nil
}
startKey := make([]byte, 8)
binary.BigEndian.PutUint64(startKey, s.BlockNumber)
endKey := dbutils.HeaderKey(*headNumber, headHash) // Make sure we stop at head
logPrefix := s.state.LogPrefix()
if err := etl.Transform(
logPrefix,
stateDB,
dbutils.HeaderPrefix,
dbutils.HeaderNumberPrefix,
tmpdir,
extractHeaders,
etl.IdentityLoadFunc,
etl.TransformArgs{
ExtractStartKey: startKey,
ExtractEndKey: endKey,
Quit: quit,
},
); err != nil {
return err
}
return s.DoneAndUpdate(stateDB, *headNumber)
}