-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathstatus_data_provider.go
163 lines (140 loc) · 4.46 KB
/
status_data_provider.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Copyright 2024 The Erigon Authors
// This file is part of Erigon.
//
// Erigon is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Erigon is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with Erigon. If not, see <http://www.gnu.org/licenses/>.
package sentry
import (
"context"
"errors"
"fmt"
"math/big"
"github.com/holiman/uint256"
"github.com/erigontech/erigon-lib/log/v3"
"github.com/erigontech/erigon-lib/chain"
libcommon "github.com/erigontech/erigon-lib/common"
"github.com/erigontech/erigon-lib/gointerfaces"
proto_sentry "github.com/erigontech/erigon-lib/gointerfaces/sentryproto"
"github.com/erigontech/erigon-lib/kv"
"github.com/erigontech/erigon/core/forkid"
"github.com/erigontech/erigon/core/rawdb"
"github.com/erigontech/erigon/core/types"
)
var ErrNoHead = errors.New("ReadChainHead: ReadCurrentHeader error")
type ChainHead struct {
HeadHeight uint64
HeadTime uint64
HeadHash libcommon.Hash
HeadTd *uint256.Int
}
type StatusDataProvider struct {
db kv.RoDB
networkId uint64
genesisHash libcommon.Hash
genesisHead ChainHead
heightForks []uint64
timeForks []uint64
logger log.Logger
}
func NewStatusDataProvider(
db kv.RoDB,
chainConfig *chain.Config,
genesis *types.Block,
networkId uint64,
logger log.Logger,
) *StatusDataProvider {
s := &StatusDataProvider{
db: db,
networkId: networkId,
genesisHash: genesis.Hash(),
genesisHead: makeGenesisChainHead(genesis),
logger: logger,
}
s.heightForks, s.timeForks = forkid.GatherForks(chainConfig, genesis.Time())
return s
}
func uint256FromBigInt(num *big.Int) (*uint256.Int, error) {
if num == nil {
num = new(big.Int)
}
num256 := new(uint256.Int)
overflow := num256.SetFromBig(num)
if overflow {
return nil, errors.New("uint256FromBigInt: big.Int greater than 2^256-1")
}
return num256, nil
}
func makeGenesisChainHead(genesis *types.Block) ChainHead {
genesisDifficulty, err := uint256FromBigInt(genesis.Difficulty())
if err != nil {
panic(fmt.Errorf("makeGenesisChainHead: difficulty conversion error: %w", err))
}
return ChainHead{
HeadHeight: genesis.NumberU64(),
HeadTime: genesis.Time(),
HeadHash: genesis.Hash(),
HeadTd: genesisDifficulty,
}
}
func (s *StatusDataProvider) makeStatusData(head ChainHead) *proto_sentry.StatusData {
return &proto_sentry.StatusData{
NetworkId: s.networkId,
TotalDifficulty: gointerfaces.ConvertUint256IntToH256(head.HeadTd),
BestHash: gointerfaces.ConvertHashToH256(head.HeadHash),
MaxBlockHeight: head.HeadHeight,
MaxBlockTime: head.HeadTime,
ForkData: &proto_sentry.Forks{
Genesis: gointerfaces.ConvertHashToH256(s.genesisHash),
HeightForks: s.heightForks,
TimeForks: s.timeForks,
},
}
}
func (s *StatusDataProvider) GetStatusData(ctx context.Context) (*proto_sentry.StatusData, error) {
chainHead, err := ReadChainHead(ctx, s.db)
if err != nil {
if errors.Is(err, ErrNoHead) {
s.logger.Warn("sentry.StatusDataProvider: The canonical chain current header not found in the database. Check the database consistency. Using genesis as a fallback.")
return s.makeStatusData(s.genesisHead), nil
}
return nil, err
}
return s.makeStatusData(chainHead), err
}
func ReadChainHeadWithTx(tx kv.Tx) (ChainHead, error) {
header := rawdb.ReadCurrentHeaderHavingBody(tx)
if header == nil {
return ChainHead{}, ErrNoHead
}
height := header.Number.Uint64()
hash := header.Hash()
time := header.Time
td, err := rawdb.ReadTd(tx, hash, height)
if err != nil {
return ChainHead{}, fmt.Errorf("ReadChainHead: ReadTd error at height %d and hash %s: %w", height, hash, err)
}
td256, err := uint256FromBigInt(td)
if err != nil {
return ChainHead{}, fmt.Errorf("ReadChainHead: total difficulty conversion error: %w", err)
}
return ChainHead{height, time, hash, td256}, nil
}
func ReadChainHead(ctx context.Context, db kv.RoDB) (ChainHead, error) {
var head ChainHead
var err error
err = db.View(ctx, func(tx kv.Tx) error {
head, err = ReadChainHeadWithTx(tx)
return err
})
return head, err
}