forked from erigontech/erigon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchain_reader.go
98 lines (82 loc) · 3.14 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package stagedsync
import (
"context"
"math/big"
"github.com/ledgerwatch/log/v3"
"github.com/ledgerwatch/erigon-lib/chain"
libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon/core/rawdb"
"github.com/ledgerwatch/erigon/core/types"
"github.com/ledgerwatch/erigon/rlp"
"github.com/ledgerwatch/erigon/turbo/services"
)
// ChainReader implements consensus.ChainReader
type ChainReader struct {
Cfg chain.Config
Db kv.Getter
BlockReader services.FullBlockReader
Logger log.Logger
}
// Config retrieves the blockchain's chain configuration.
func (cr ChainReader) Config() *chain.Config {
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)
h, _ := cr.BlockReader.Header(context.Background(), cr.Db, hash, *number)
return h
}
// GetHeader retrieves a block header from the database by hash and number.
func (cr ChainReader) GetHeader(hash libcommon.Hash, number uint64) *types.Header {
h, _ := cr.BlockReader.Header(context.Background(), cr.Db, hash, number)
return h
}
// GetHeaderByNumber retrieves a block header from the database by number.
func (cr ChainReader) GetHeaderByNumber(number uint64) *types.Header {
h, _ := cr.BlockReader.HeaderByNumber(context.Background(), cr.Db, number)
return h
}
// GetHeaderByHash retrieves a block header from the database by its hash.
func (cr ChainReader) GetHeaderByHash(hash libcommon.Hash) *types.Header {
number := rawdb.ReadHeaderNumber(cr.Db, hash)
h, _ := cr.BlockReader.Header(context.Background(), cr.Db, hash, *number)
return h
}
// GetBlock retrieves a block from the database by hash and number.
func (cr ChainReader) GetBlock(hash libcommon.Hash, number uint64) *types.Block {
b, _, _ := cr.BlockReader.BlockWithSenders(context.Background(), cr.Db, hash, number)
return b
}
// HasBlock retrieves a block from the database by hash and number.
func (cr ChainReader) HasBlock(hash libcommon.Hash, number uint64) bool {
b, _ := cr.BlockReader.BodyRlp(context.Background(), cr.Db, hash, number)
return b != nil
}
// GetTd retrieves the total difficulty from the database by hash and number.
func (cr ChainReader) GetTd(hash libcommon.Hash, number uint64) *big.Int {
td, err := rawdb.ReadTd(cr.Db, hash, number)
if err != nil {
cr.Logger.Error("ReadTd failed", "err", err)
return nil
}
return td
}
func (cr ChainReader) FrozenBlocks() uint64 { return cr.BlockReader.FrozenBlocks() }
func (cr ChainReader) FrozenBorBlocks() uint64 { return cr.BlockReader.FrozenBorBlocks() }
func (cr ChainReader) BorStartEventID(_ libcommon.Hash, _ uint64) uint64 {
panic("bor events by block not implemented")
}
func (cr ChainReader) BorEventsByBlock(_ libcommon.Hash, _ uint64) []rlp.RawValue {
panic("bor events by block not implemented")
}
func (cr ChainReader) BorSpan(spanId uint64) []byte {
span, err := cr.BlockReader.Span(context.Background(), cr.Db, spanId)
if err != nil {
cr.Logger.Error("BorSpan failed", "err", err)
return nil
}
return span
}