/
blockdata.go
323 lines (287 loc) · 10 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
// Copyright (c) 2017, Jonathan Chappelow
// See LICENSE for details.
package blockdata
import (
"errors"
"sync"
"time"
"github.com/decred/dcrd/chaincfg"
"github.com/decred/dcrd/chaincfg/chainhash"
"github.com/decred/dcrd/dcrjson"
"github.com/decred/dcrd/dcrutil"
"github.com/decred/dcrd/rpcclient"
"github.com/decred/dcrd/wire"
apitypes "github.com/decred/dcrdata/api/types"
"github.com/decred/dcrdata/stakedb"
"github.com/decred/dcrdata/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 dcrjson.GetBlockHeaderVerboseResult
Connections int32
FeeInfo dcrjson.FeeInfoBlock
CurrentStakeDiff dcrjson.GetStakeDifficultyResult
EstStakeDiff dcrjson.EstimateStakeDiffResult
PoolInfo apitypes.TicketPoolInfo
ExtraInfo apitypes.BlockExplorerExtraInfo
PriceWindowNum int
IdxBlockInWindow int
WinningTickets []string
}
// ToStakeInfoExtended returns an apitypes.StakeInfoExtended object from the
// blockdata
func (b *BlockData) ToStakeInfoExtended() apitypes.StakeInfoExtended {
return apitypes.StakeInfoExtended{
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{
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 {
return apitypes.BlockDataBasic{
Height: b.Header.Height,
Size: b.Header.Size,
Hash: b.Header.Hash,
Difficulty: b.Header.Difficulty,
StakeDiff: b.Header.SBits,
Time: b.Header.Time,
PoolInfo: b.PoolInfo,
}
}
// ToBlockExplorerSummary returns a BlockExplorerBasic
func (b *BlockData) ToBlockExplorerSummary() apitypes.BlockExplorerBasic {
t := time.Unix(b.Header.Time, 0)
ftime := t.Format("2006-01-02 15:04:05")
extra := b.ExtraInfo
extra.FormattedTime = ftime
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: b.Header.Time,
}
}
// 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{
mtx: sync.Mutex{},
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{
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,
*dcrjson.FeeInfoBlock, *dcrjson.GetBlockHeaderVerboseResult,
*apitypes.BlockExplorerExtraInfo, *wire.MsgBlock, error) {
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())
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)
}
// Ticket pool info (value, size, avg)
var ticketPoolInfo *apitypes.TicketPoolInfo
var found bool
if ticketPoolInfo, found = t.stakeDB.PoolInfo(*hash); !found {
log.Infof("Unable to find block (%s) in pool info cache, trying best block.", hash.String())
ticketPoolInfo = t.stakeDB.PoolInfoBest()
if ticketPoolInfo.Height != height {
log.Warnf("Collected block height %d != stake db height %d. Pool "+
"info will not match the rest of this block's data.",
height, ticketPoolInfo.Height)
}
}
// Fee info
feeInfoBlock := txhelpers.FeeRateInfoBlock(block)
if feeInfoBlock == nil {
log.Error("FeeInfoBlock failed")
}
// Work/Stake difficulty
header := block.MsgBlock().Header
diff := txhelpers.GetDifficultyRatio(header.Bits, t.netParams)
sdiff := dcrutil.Amount(header.SBits).ToCoin()
blockHeaderResults, err := t.dcrdChainSvr.GetBlockHeaderVerbose(hash)
if err != nil {
return nil, nil, nil, nil, nil, err
}
// Output
blockdata := &apitypes.BlockDataBasic{
Height: height,
Size: uint32(block.MsgBlock().SerializeSize()),
Hash: hash.String(),
Difficulty: diff,
StakeDiff: sdiff,
Time: header.Timestamp.Unix(),
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)
}
// Output
height := int64(blockDataBasic.Height)
winSize := t.netParams.StakeDiffWindowSize
blockdata := &BlockData{
Header: *blockHeaderVerbose,
Connections: int32(numConn),
FeeInfo: *feeInfoBlock,
CurrentStakeDiff: dcrjson.GetStakeDifficultyResult{CurrentStakeDifficulty: blockDataBasic.StakeDiff},
EstStakeDiff: dcrjson.EstimateStakeDiffResult{},
PoolInfo: blockDataBasic.PoolInfo,
ExtraInfo: *extra,
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 bbhRes struct {
err error
hash *chainhash.Hash
}
toch := make(chan bbhRes)
// Pull and store relevant data about the blockchain.
go func() {
bestBlockHash, err := t.dcrdChainSvr.GetBestBlockHash()
toch <- bbhRes{err, bestBlockHash}
}()
var bbs bbhRes
select {
case bbs = <-toch:
case <-time.After(time.Second * 10):
log.Errorf("Timeout waiting for dcrd.")
return nil, nil, errors.New("Timeout")
}
// 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 = &dcrjson.EstimateStakeDiffResult{}
}
// Info specific to the block hash
blockDataBasic, feeInfoBlock, blockHeaderVerbose, extra, msgBlock, err := t.CollectBlockInfo(bbs.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,
PoolInfo: blockDataBasic.PoolInfo,
PriceWindowNum: int(height / winSize),
IdxBlockInWindow: int(height%winSize) + 1,
}
return blockdata, msgBlock, err
}