-
Notifications
You must be signed in to change notification settings - Fork 3
/
blockdata.go
367 lines (326 loc) · 11.6 KB
/
blockdata.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
// Copyright (c) 2017, Jonathan Chappelow
// See LICENSE for details.
package blockdata
import (
"errors"
"fmt"
"sync"
"time"
"github.com/Decred-Next/dcrnd/chaincfg/chainhash/v8"
"github.com/Decred-Next/dcrnd/chaincfg/v8"
"github.com/Decred-Next/dcrnd/dcrutil/version2/v8"
chainjson "github.com/Decred-Next/dcrnd/rpc/jsonrpc/types/version2/v8"
"github.com/Decred-Next/dcrnd/rpcclient/version5/v8"
"github.com/Decred-Next/dcrnd/wire/v8"
apitypes "github.com/Decred-Next/dcrndata/v8/api/types"
"github.com/Decred-Next/dcrndata/v8/db/dbtypes"
"github.com/Decred-Next/dcrndata/v8/stakedb"
"github.com/Decred-Next/dcrndata/v8/txhelpers"
)
// BlockData contains all the data collected by a Collector and stored
// by a BlockDataSaver. TODO: consider if pointers are desirable here.
type BlockData struct {
Header chainjson.GetBlockHeaderVerboseResult
Connections int32
FeeInfo chainjson.FeeInfoBlock
CurrentStakeDiff chainjson.GetStakeDifficultyResult
EstStakeDiff chainjson.EstimateStakeDiffResult
PoolInfo *apitypes.TicketPoolInfo
ExtraInfo apitypes.BlockExplorerExtraInfo
BlockchainInfo *chainjson.GetBlockChainInfoResult
PriceWindowNum int
IdxBlockInWindow int
WinningTickets []string
}
// ToStakeInfoExtended returns an apitypes.StakeInfoExtended object from the
// blockdata
func (b *BlockData) ToStakeInfoExtended() apitypes.StakeInfoExtended {
return apitypes.StakeInfoExtended{
Hash: b.Header.Hash,
Feeinfo: b.FeeInfo,
StakeDiff: b.CurrentStakeDiff.CurrentStakeDifficulty,
PriceWindowNum: b.PriceWindowNum,
IdxBlockInWindow: b.IdxBlockInWindow,
PoolInfo: b.PoolInfo,
}
}
// ToStakeInfoExtendedEstimates returns an apitypes.StakeInfoExtendedEstimates
// object from the blockdata
func (b *BlockData) ToStakeInfoExtendedEstimates() apitypes.StakeInfoExtendedEstimates {
return apitypes.StakeInfoExtendedEstimates{
Hash: b.Header.Hash,
Feeinfo: b.FeeInfo,
StakeDiff: apitypes.StakeDiff{
GetStakeDifficultyResult: b.CurrentStakeDiff,
Estimates: b.EstStakeDiff,
IdxBlockInWindow: b.IdxBlockInWindow,
PriceWindowNum: b.PriceWindowNum,
},
// PriceWindowNum and Idx... are repeated here since this is a kludge
PriceWindowNum: b.PriceWindowNum,
IdxBlockInWindow: b.IdxBlockInWindow,
PoolInfo: b.PoolInfo,
}
}
// ToBlockSummary returns an apitypes.BlockDataBasic object from the blockdata
func (b *BlockData) ToBlockSummary() apitypes.BlockDataBasic {
t := dbtypes.NewTimeDefFromUNIX(b.Header.Time)
return apitypes.BlockDataBasic{
Height: b.Header.Height,
Size: b.Header.Size,
Hash: b.Header.Hash,
Difficulty: b.Header.Difficulty,
StakeDiff: b.Header.SBits,
Time: apitypes.TimeAPI{S: t},
PoolInfo: b.PoolInfo,
}
}
// ToBlockExplorerSummary returns a BlockExplorerBasic
func (b *BlockData) ToBlockExplorerSummary() apitypes.BlockExplorerBasic {
extra := b.ExtraInfo
t := dbtypes.NewTimeDefFromUNIX(b.Header.Time)
return apitypes.BlockExplorerBasic{
Height: b.Header.Height,
Size: b.Header.Size,
Voters: b.Header.Voters,
Revocations: b.Header.Revocations,
FreshStake: b.Header.FreshStake,
StakeDiff: b.Header.SBits,
BlockExplorerExtraInfo: extra,
Time: t,
}
}
// Collector models a structure for the source of the blockdata
type Collector struct {
mtx sync.Mutex
dcrdChainSvr *rpcclient.Client
netParams *chaincfg.Params
stakeDB *stakedb.StakeDatabase
}
// NewCollector creates a new Collector.
func NewCollector(dcrdChainSvr *rpcclient.Client, params *chaincfg.Params,
stakeDB *stakedb.StakeDatabase) *Collector {
return &Collector{
dcrdChainSvr: dcrdChainSvr,
netParams: params,
stakeDB: stakeDB,
}
}
// CollectAPITypes uses CollectBlockInfo to collect block data, then organizes
// it into the BlockDataBasic and StakeInfoExtended and dcrdataapi types.
func (t *Collector) CollectAPITypes(hash *chainhash.Hash) (*apitypes.BlockDataBasic, *apitypes.StakeInfoExtended) {
blockDataBasic, feeInfoBlock, _, _, _, err := t.CollectBlockInfo(hash)
if err != nil {
return nil, nil
}
height := int64(blockDataBasic.Height)
winSize := t.netParams.StakeDiffWindowSize
stakeInfoExtended := &apitypes.StakeInfoExtended{
Hash: blockDataBasic.Hash,
Feeinfo: *feeInfoBlock,
StakeDiff: blockDataBasic.StakeDiff,
PriceWindowNum: int(height / winSize),
IdxBlockInWindow: int(height%winSize) + 1,
PoolInfo: blockDataBasic.PoolInfo,
}
return blockDataBasic, stakeInfoExtended
}
// CollectBlockInfo uses the chain server and the stake DB to collect most of
// the block data required by Collect() that is specific to the block with the
// given hash.
func (t *Collector) CollectBlockInfo(hash *chainhash.Hash) (*apitypes.BlockDataBasic,
*chainjson.FeeInfoBlock, *chainjson.GetBlockHeaderVerboseResult,
*apitypes.BlockExplorerExtraInfo, *wire.MsgBlock, error) {
// Retrieve block from dcrd.
msgBlock, err := t.dcrdChainSvr.GetBlock(hash)
if err != nil {
return nil, nil, nil, nil, nil, err
}
height := msgBlock.Header.Height
block := dcrutil.NewBlock(msgBlock)
txLen := len(block.Transactions())
// Coin supply and block subsidy. If either RPC fails, do not immediately
// return. Attempt acquisition of other data for this block.
coinSupply, err := t.dcrdChainSvr.GetCoinSupply()
if err != nil {
log.Error("GetCoinSupply failed: ", err)
}
nbSubsidy, err := t.dcrdChainSvr.GetBlockSubsidy(int64(msgBlock.Header.Height)+1, 5)
if err != nil {
log.Errorf("GetBlockSubsidy for %d failed: %v", msgBlock.Header.Height, err)
}
// Block header
blockHeaderResults, err := t.dcrdChainSvr.GetBlockHeaderVerbose(hash)
if err != nil {
return nil, nil, nil, nil, nil, err
}
isSideChain := blockHeaderResults.Confirmations == -1
// Ticket pool info (value, size, avg)
var ticketPoolInfo *apitypes.TicketPoolInfo
var found bool
if ticketPoolInfo, found = t.stakeDB.PoolInfo(*hash); !found {
// If unable to get ticket pool info for this block, stakedb does
// not have it. This is expected for side chain blocks, so do not
// log in that case.
if !isSideChain {
log.Infof("Unable to find block (%v) in pool info cache, trying best block.", hash)
}
ticketPoolInfo = t.stakeDB.PoolInfoBest()
if ticketPoolInfo.Height != height {
if !isSideChain {
log.Warnf("Ticket pool info not available for block %v.", hash)
}
ticketPoolInfo = nil
}
}
// Fee info
feeInfoBlock := txhelpers.FeeRateInfoBlock(block)
if feeInfoBlock == nil {
log.Error("FeeInfoBlock failed")
}
// Work/Stake difficulty
header := msgBlock.Header
diff := txhelpers.GetDifficultyRatio(header.Bits, t.netParams)
sdiff := dcrutil.Amount(header.SBits).ToCoin()
// Output
blockdata := &apitypes.BlockDataBasic{
Height: height,
Size: uint32(block.MsgBlock().SerializeSize()),
Hash: hash.String(),
Difficulty: diff,
StakeDiff: sdiff,
Time: apitypes.TimeAPI{S: dbtypes.NewTimeDef(header.Timestamp)},
PoolInfo: ticketPoolInfo,
}
extrainfo := &apitypes.BlockExplorerExtraInfo{
TxLen: txLen,
CoinSupply: int64(coinSupply),
NextBlockSubsidy: nbSubsidy,
}
return blockdata, feeInfoBlock, blockHeaderResults, extrainfo, msgBlock, err
}
// CollectHash collects chain data at the block with the specified hash.
func (t *Collector) CollectHash(hash *chainhash.Hash) (*BlockData, *wire.MsgBlock, error) {
// In case of a very fast block, make sure previous call to collect is not
// still running, or dcrd may be mad.
t.mtx.Lock()
defer t.mtx.Unlock()
// Time this function
defer func(start time.Time) {
log.Debugf("Collector.CollectHash() completed in %v", time.Since(start))
}(time.Now())
// Info specific to the block hash
blockDataBasic, feeInfoBlock, blockHeaderVerbose, extra, msgBlock, err :=
t.CollectBlockInfo(hash)
if err != nil {
return nil, nil, err
}
// Number of peer connection to chain server
numConn, err := t.dcrdChainSvr.GetConnectionCount()
if err != nil {
log.Warn("Unable to get connection count: ", err)
}
// Blockchain info (e.g. syncheight, verificationprogress, chainwork,
// bestblockhash, initialblockdownload, maxblocksize, deployments, etc.).
chainInfo, err := t.dcrdChainSvr.GetBlockChainInfo()
if err != nil {
log.Warn("Unable to get blockchain info: ", err)
}
// GetBlockChainInfo is only valid for for chain tip.
if chainInfo.BestBlockHash != hash.String() {
chainInfo = nil
}
// Output
height := int64(blockDataBasic.Height)
winSize := t.netParams.StakeDiffWindowSize
blockdata := &BlockData{
Header: *blockHeaderVerbose,
Connections: int32(numConn),
FeeInfo: *feeInfoBlock,
CurrentStakeDiff: chainjson.GetStakeDifficultyResult{CurrentStakeDifficulty: blockDataBasic.StakeDiff},
EstStakeDiff: chainjson.EstimateStakeDiffResult{},
PoolInfo: blockDataBasic.PoolInfo,
ExtraInfo: *extra,
BlockchainInfo: chainInfo,
PriceWindowNum: int(height / winSize),
IdxBlockInWindow: int(height%winSize) + 1,
}
return blockdata, msgBlock, err
}
// Collect collects chain data at the current best block.
func (t *Collector) Collect() (*BlockData, *wire.MsgBlock, error) {
// In case of a very fast block, make sure previous call to collect is not
// still running, or dcrd may be mad.
t.mtx.Lock()
defer t.mtx.Unlock()
// Time this function
defer func(start time.Time) {
log.Debugf("Collector.Collect() completed in %v", time.Since(start))
}(time.Now())
// Run first client call with a timeout.
type bciRes struct {
err error
blockchainInfo *chainjson.GetBlockChainInfoResult
}
toch := make(chan bciRes)
// Pull and store relevant data about the blockchain (e.g. syncheight,
// verificationprogress, chainwork, bestblockhash, initialblockdownload,
// maxblocksize, deployments, etc.).
go func() {
blockchainInfo, err := t.dcrdChainSvr.GetBlockChainInfo()
toch <- bciRes{err, blockchainInfo}
}()
var bci bciRes
select {
case bci = <-toch:
case <-time.After(time.Second * 10):
log.Errorf("Timeout waiting for dcrd.")
return nil, nil, errors.New("Timeout")
}
if bci.err != nil {
return nil, nil, fmt.Errorf("unable to get blockchain info: %v", bci.err)
}
hash, err := chainhash.NewHashFromStr(bci.blockchainInfo.BestBlockHash)
if err != nil {
return nil, nil,
fmt.Errorf("invalid best block hash from getblockchaininfo: %v", err)
}
// Stake difficulty
stakeDiff, err := t.dcrdChainSvr.GetStakeDifficulty()
if err != nil {
return nil, nil, err
}
// estimatestakediff
estStakeDiff, err := t.dcrdChainSvr.EstimateStakeDiff(nil)
if err != nil {
log.Warn("estimatestakediff is broken: ", err)
estStakeDiff = &chainjson.EstimateStakeDiffResult{}
}
// Info specific to the block hash
blockDataBasic, feeInfoBlock, blockHeaderVerbose, extra, msgBlock, err :=
t.CollectBlockInfo(hash)
if err != nil {
return nil, nil, err
}
// Number of peer connection to chain server
numConn, err := t.dcrdChainSvr.GetConnectionCount()
if err != nil {
log.Warn("Unable to get connection count: ", err)
}
// Output
height := int64(blockDataBasic.Height)
winSize := t.netParams.StakeDiffWindowSize
blockdata := &BlockData{
Header: *blockHeaderVerbose,
Connections: int32(numConn),
FeeInfo: *feeInfoBlock,
CurrentStakeDiff: *stakeDiff,
EstStakeDiff: *estStakeDiff,
ExtraInfo: *extra,
BlockchainInfo: bci.blockchainInfo,
PoolInfo: blockDataBasic.PoolInfo,
PriceWindowNum: int(height / winSize),
IdxBlockInWindow: int(height%winSize) + 1,
}
return blockdata, msgBlock, err
}