forked from erigontech/erigon
-
Notifications
You must be signed in to change notification settings - Fork 1
/
chain_reader.go
72 lines (60 loc) · 2.17 KB
/
chain_reader.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package stagedsync
import (
"math/big"
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon/common"
"github.com/ledgerwatch/erigon/core/rawdb"
"github.com/ledgerwatch/erigon/core/types"
"github.com/ledgerwatch/erigon/params"
"github.com/ledgerwatch/log/v3"
)
// Implements consensus.ChainReader
type ChainReader struct {
Cfg params.ChainConfig
Db kv.Getter
}
// Config retrieves the blockchain's chain configuration.
func (cr ChainReader) Config() *params.ChainConfig {
return &cr.Cfg
}
// CurrentHeader retrieves the current header from the local chain.
func (cr ChainReader) CurrentHeader() *types.Header {
hash := rawdb.ReadHeadHeaderHash(cr.Db)
number := rawdb.ReadHeaderNumber(cr.Db, hash)
return rawdb.ReadHeader(cr.Db, hash, *number)
}
// GetHeader retrieves a block header from the database by hash and number.
func (cr ChainReader) GetHeader(hash common.Hash, number uint64) *types.Header {
return rawdb.ReadHeader(cr.Db, hash, number)
}
// GetHeaderByNumber retrieves a block header from the database by number.
func (cr ChainReader) GetHeaderByNumber(number uint64) *types.Header {
hash, err := rawdb.ReadCanonicalHash(cr.Db, number)
if err != nil {
log.Error("ReadCanonicalHash failed", "err", err)
return nil
}
return rawdb.ReadHeader(cr.Db, hash, number)
}
// GetHeaderByHash retrieves a block header from the database by its hash.
func (cr ChainReader) GetHeaderByHash(hash common.Hash) *types.Header {
number := rawdb.ReadHeaderNumber(cr.Db, hash)
return rawdb.ReadHeader(cr.Db, hash, *number)
}
// GetBlock retrieves a block from the database by hash and number.
func (cr ChainReader) GetBlock(hash common.Hash, number uint64) *types.Block {
return rawdb.ReadBlock(cr.Db, hash, number)
}
// HasBlock retrieves a block from the database by hash and number.
func (cr ChainReader) HasBlock(hash common.Hash, number uint64) bool {
return rawdb.HasBlock(cr.Db, hash, number)
}
// GetTd retrieves the total difficulty from the database by hash and number.
func (cr ChainReader) GetTd(hash common.Hash, number uint64) *big.Int {
td, err := rawdb.ReadTd(cr.Db, hash, number)
if err != nil {
log.Error("ReadTd failed", "err", err)
return nil
}
return td
}