-
Notifications
You must be signed in to change notification settings - Fork 128
/
collector.go
468 lines (414 loc) · 13.7 KB
/
collector.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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
// Copyright (c) 2018-2019, The Decred developers
// Copyright (c) 2017, Jonathan Chappelow
// See LICENSE for details.
package mempool
import (
"fmt"
"sort"
"sync"
"time"
"github.com/decred/dcrd/blockchain/stake/v2"
"github.com/decred/dcrd/blockchain/standalone"
"github.com/decred/dcrd/chaincfg/chainhash"
"github.com/decred/dcrd/chaincfg/v2"
"github.com/decred/dcrd/dcrutil/v2"
chainjson "github.com/decred/dcrd/rpc/jsonrpc/types"
"github.com/decred/dcrd/rpcclient/v4"
apitypes "github.com/decred/dcrdata/api/types/v4"
exptypes "github.com/decred/dcrdata/explorer/types/v2"
"github.com/decred/dcrdata/rpcutils/v2"
"github.com/decred/dcrdata/txhelpers/v3"
humanize "github.com/dustin/go-humanize"
)
// MempoolDataCollector is used for retrieving and processing data from a chain
// server's mempool.
type MempoolDataCollector struct {
// Mutex is used to prevent multiple concurrent calls to Collect.
mtx sync.Mutex
dcrdChainSvr *rpcclient.Client
activeChain *chaincfg.Params
}
// NewMempoolDataCollector creates a new MempoolDataCollector.
func NewMempoolDataCollector(dcrdChainSvr *rpcclient.Client, params *chaincfg.Params) *MempoolDataCollector {
return &MempoolDataCollector{
dcrdChainSvr: dcrdChainSvr,
activeChain: params,
}
}
// mempoolTxns retrieves all transactions and returns them as a
// []exptypes.MempoolTx. See also ParseTxns, which may process this slice. A
// fresh MempoolAddressStore and TxnsStore are also generated.
func (t *MempoolDataCollector) mempoolTxns() ([]exptypes.MempoolTx, txhelpers.MempoolAddressStore, txhelpers.TxnsStore, error) {
mempooltxs, err := t.dcrdChainSvr.GetRawMempoolVerbose(chainjson.GRMAll)
if err != nil {
return nil, nil, nil, fmt.Errorf("GetRawMempoolVerbose failed: %v", err)
}
blockHash, _, err := t.dcrdChainSvr.GetBestBlock()
if err != nil {
return nil, nil, nil, err
}
blockhash := blockHash.String()
txs := make([]exptypes.MempoolTx, 0, len(mempooltxs))
addrMap := make(txhelpers.MempoolAddressStore)
txnsStore := make(txhelpers.TxnsStore)
for hashStr, tx := range mempooltxs {
hash, err := chainhash.NewHashFromStr(hashStr)
if err != nil {
log.Warn(err)
continue
}
rawtx, err := rpcutils.GetTransactionVerboseByID(t.dcrdChainSvr, hash)
if err != nil {
log.Warn(err)
continue
}
if rawtx == nil {
log.Errorf("Failed to get mempool transaction %s.", hash)
continue
}
msgTx, err := txhelpers.MsgTxFromHex(rawtx.Hex)
if err != nil {
log.Errorf("Failed to decode transaction hex: %v", err)
continue
}
// Set Outpoints in the addrMap.
txhelpers.TxOutpointsByAddr(addrMap, msgTx, t.activeChain)
// Set PrevOuts in the addrMap, and related txns data in txnsStore.
txhelpers.TxPrevOutsByAddr(addrMap, txnsStore, msgTx, t.dcrdChainSvr, t.activeChain)
// Store the current mempool transaction with MemPoolTime from GRM, and
// block info zeroed.
txnsStore[msgTx.TxHash()] = &txhelpers.TxWithBlockData{
Tx: msgTx,
MemPoolTime: tx.Time,
}
totalOut := 0.0
for _, v := range rawtx.Vout {
totalOut += v.Value
}
var voteInfo *exptypes.VoteInfo
if ok := stake.IsSSGen(msgTx); ok {
validation, version, bits, choices, err := txhelpers.SSGenVoteChoices(msgTx, t.activeChain)
if err != nil {
log.Debugf("Cannot get vote choices for %s", hash)
} else {
voteInfo = &exptypes.VoteInfo{
Validation: exptypes.BlockValidation{
Hash: validation.Hash.String(),
Height: validation.Height,
Validity: validation.Validity,
},
Version: version,
Bits: bits,
Choices: choices,
TicketSpent: msgTx.TxIn[1].PreviousOutPoint.Hash.String(),
}
voteInfo.ForLastBlock = voteInfo.VotesOnBlock(blockhash)
}
}
// Note that the fee computed from msgTx will be a large negative fee
// for coinbase transactions.
//fee, _ := txhelpers.TxFeeRate(msgTx)
// log.Tracef("tx fee: GRM result = %f, msgTx = %f", tx.Fee, fee.ToCoin())
txs = append(txs, exptypes.MempoolTx{
TxID: msgTx.TxHash().String(),
Fees: tx.Fee,
VinCount: len(msgTx.TxIn),
VoutCount: len(msgTx.TxOut),
Vin: exptypes.MsgTxMempoolInputs(msgTx),
Coinbase: standalone.IsCoinBaseTx(msgTx),
Hash: hashStr,
Time: tx.Time,
Size: tx.Size,
TotalOut: totalOut,
Type: txhelpers.DetermineTxTypeString(msgTx),
VoteInfo: voteInfo,
})
}
return txs, addrMap, txnsStore, nil
}
// Collect is the main handler for collecting mempool data. Data collection is
// focused on stake-related information, including vote and ticket transactions,
// and fee info. Transactions of all types in mempool are returned as a
// []exptypes.MempoolTx, corresponding to the same data provided by the
// unexported mempoolTxns method.
func (t *MempoolDataCollector) Collect() (*StakeData, []exptypes.MempoolTx, txhelpers.MempoolAddressStore, txhelpers.TxnsStore, 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("MempoolDataCollector.Collect() completed in %v",
time.Since(start))
}(time.Now())
// client
c := t.dcrdChainSvr
// Get a map of ticket hashes to getrawmempool results
// mempoolTickets[ticketHashes[0].String()].Fee
mempoolTickets, err := c.GetRawMempoolVerbose(chainjson.GRMTickets)
if err != nil {
return nil, nil, nil, nil, err
}
mempoolVotes, err := c.GetRawMempoolVerbose(chainjson.GRMVotes)
if err != nil {
return nil, nil, nil, nil, err
}
numVotes := len(mempoolVotes)
// Grab the current stake difficulty (ticket price).
stakeDiff, err := c.GetStakeDifficulty()
if err != nil {
return nil, nil, nil, nil, err
}
hash, height, err := c.GetBestBlock()
if err != nil {
return nil, nil, nil, nil, err
}
header, err := c.GetBlockHeaderVerbose(hash)
if err != nil {
return nil, nil, nil, nil, err
}
blockTime := header.Time
// Fee info
var numFeeWindows, numFeeBlocks uint32 = 0, 0
feeInfo, err := c.TicketFeeInfo(&numFeeBlocks, &numFeeWindows)
if err != nil {
return nil, nil, nil, nil, err
}
// All transactions in mempool.
allTxns, addrMap, txnsStore, err := t.mempoolTxns()
if err != nil {
return nil, nil, nil, nil, err
}
now := time.Now()
// Make slice of TicketDetails
N := len(mempoolTickets)
allTicketsDetails := make(TicketsDetails, 0, N)
for hash, ticket := range mempoolTickets {
//ageSec := time.Since(time.Unix(ticket.Time, 0)).Seconds()
// Compute fee in DCR / kB
feeRate := ticket.Fee / float64(ticket.Size) * 1000
allTicketsDetails = append(allTicketsDetails, &apitypes.TicketDetails{
Hash: hash,
Fee: ticket.Fee,
FeeRate: feeRate,
Size: ticket.Size,
Height: ticket.Height,
})
}
// Verify we get the correct median result
//medianFee := MedianCoin(allFeeRates)
//log.Infof("Median fee computed: %v (%v)", medianFee, N)
sort.Sort(ByAbsoluteFee{allTicketsDetails})
allFees := make([]float64, 0, N)
for _, td := range allTicketsDetails {
allFees = append(allFees, td.Fee)
}
sort.Sort(ByFeeRate{allTicketsDetails})
allFeeRates := make([]float64, 0, N)
for _, td := range allTicketsDetails {
allFeeRates = append(allFeeRates, td.FeeRate)
}
// 20 tickets purchases may be mined per block
Nmax := int(t.activeChain.MaxFreshStakePerBlock)
//sort.Float64s(allFeeRates)
var lowestMineableFee float64
// If no tickets, no valid index
var lowestMineableIdx = -1
if N >= Nmax {
lowestMineableIdx = N - Nmax
lowestMineableFee = allFeeRates[lowestMineableIdx]
} else if N != 0 {
lowestMineableIdx = 0
lowestMineableFee = allFeeRates[0]
}
// Extract the fees for a window about the mileability threshold
var targetFeeWindow []float64
if N > 0 {
// Summary output has it's own radius, but here we hard-code
const feeRad int = 5
lowEnd := lowestMineableIdx - feeRad
if lowEnd < 0 {
lowEnd = 0
}
// highEnd is the exclusive end of the half-open range (+1)
highEnd := lowestMineableIdx + feeRad + 1
if highEnd > N {
highEnd = N
}
targetFeeWindow = allFeeRates[lowEnd:highEnd]
}
mineables := &MinableFeeInfo{
allFees,
allFeeRates,
lowestMineableIdx,
lowestMineableFee,
targetFeeWindow,
}
mpoolData := &StakeData{
LatestBlock: BlockID{
Hash: *hash,
Height: height,
Time: blockTime,
},
Time: now,
NumTickets: feeInfo.FeeInfoMempool.Number,
NumVotes: uint32(numVotes),
// NewTickets set by CollectAndStore
Ticketfees: feeInfo,
MinableFees: mineables,
AllTicketsDetails: allTicketsDetails,
StakeDiff: stakeDiff.CurrentStakeDifficulty,
}
return mpoolData, allTxns, addrMap, txnsStore, err
}
// NumLatestMempoolTxns is the maximum number of mempool transactions that will
// be stored in the LatestTransactions field of the MempoolInfo generated by
// ParseTxns.
const NumLatestMempoolTxns = 5
// ParseTxns analyzes the mempool transactions in the txs slice, and generates a
// MempoolInfo summary with categorized transactions.
func ParseTxns(txs []exptypes.MempoolTx, params *chaincfg.Params, lastBlock *BlockID) *exptypes.MempoolInfo {
// The txs slice needs to be sorted by time, but we do not want to modify
// the slice outside of this function and we do not want to waste time
// copying it if it is already sorted. So, make a copy and sort only if it
// is not already sorted.
if !sort.SliceIsSorted(txs, func(i, j int) bool {
return txs[i].Time > txs[j].Time
}) {
log.Debug("The transactions slice was not sorted by time. Sorting it now.")
// Copy the input slice to avoid side effects.
txs0 := txs
txs = make([]exptypes.MempoolTx, len(txs0))
copy(txs, txs0)
sort.Sort(exptypes.MPTxsByTime(txs))
}
// Get the NumLatestMempoolTxns latest transactions in mempool.
var latest []exptypes.MempoolTx
if len(txs) > NumLatestMempoolTxns {
latest = txs[:NumLatestMempoolTxns]
} else {
latest = txs
}
// Initialize with make to ensure they marshal to JSON as [] if empty.
tickets := make([]exptypes.MempoolTx, 0)
votes := make([]exptypes.MempoolTx, 0)
revs := make([]exptypes.MempoolTx, 0)
regular := make([]exptypes.MempoolTx, 0)
// Transaction inventory.
invRegular := make(map[string]struct{})
invStake := make(map[string]struct{})
blockhash := lastBlock.Hash.String()
votingInfo := exptypes.NewVotingInfo(params.TicketsPerBlock)
// Reduction variables.
var latestTime int64
var totalOut, regularTotal, ticketTotal, voteTotal, revTotal dcrutil.Amount
var likelyMineable bool
var likelyTotal dcrutil.Amount
var totalSize, likelySize int32
var numLikely int
// Initialize the BlockValidatorIndex, a map.
ticketSpendInds := make(exptypes.BlockValidatorIndex)
for _, tx := range txs {
likelyMineable = true
out, _ := dcrutil.NewAmount(tx.TotalOut) // 0 for invalid amounts
switch tx.Type {
case "Ticket":
if _, found := invStake[tx.Hash]; found {
continue
}
ticketTotal += out
invStake[tx.Hash] = struct{}{}
tickets = append(tickets, tx)
case "Vote":
if _, found := invStake[tx.Hash]; found {
continue
}
invStake[tx.Hash] = struct{}{}
votes = append(votes, tx)
if tx.VoteInfo == nil {
log.Errorf("Missing vote information for %v!", tx)
continue
}
// Assign an index to this vote that is unique to the spent ticket +
// validated block.
tx.VoteInfo.SetTicketIndex(ticketSpendInds)
// Determine if this vote is (in)validating the previous block.
tx.VoteInfo.ForLastBlock = tx.VoteInfo.VotesOnBlock(blockhash)
// Update tally if this is for the previous block, the ticket has not
// yet been spent. Do not attempt to decide block validity.
if tx.VoteInfo.ForLastBlock && !votingInfo.VotedTickets[tx.VoteInfo.TicketSpent] {
votingInfo.VotedTickets[tx.VoteInfo.TicketSpent] = true
votingInfo.TicketsVoted++
voteTotal += out
votingInfo.Tally(tx.VoteInfo)
} else {
likelyMineable = false
}
case "Revocation":
if _, found := invStake[tx.Hash]; found {
continue
}
revTotal += out
invStake[tx.Hash] = struct{}{}
revs = append(revs, tx)
default:
if _, found := invRegular[tx.Hash]; found {
continue
}
regularTotal += out
invRegular[tx.Hash] = struct{}{}
regular = append(regular, tx)
}
// Update mempool totals
if likelyMineable {
likelyTotal += out
likelySize += tx.Size
numLikely++
}
totalOut += out
totalSize += tx.Size
if latestTime < tx.Time {
latestTime = tx.Time
}
}
sort.Sort(exptypes.MPTxsByHeight(votes))
formattedSize := humanize.Bytes(uint64(totalSize))
// Store mempool data for template rendering
mpInfo := exptypes.MempoolInfo{
MempoolShort: exptypes.MempoolShort{
LastBlockHeight: lastBlock.Height,
LastBlockHash: blockhash,
LastBlockTime: lastBlock.Time,
FormattedBlockTime: (exptypes.TimeDef{T: time.Unix(lastBlock.Time, 0)}).String(),
Time: latestTime,
TotalOut: totalOut.ToCoin(),
TotalSize: totalSize,
NumTickets: len(tickets),
NumVotes: len(votes),
NumRegular: len(regular),
NumRevokes: len(revs),
NumAll: len(txs),
LikelyMineable: exptypes.LikelyMineable{
Total: likelyTotal.ToCoin(),
Size: likelySize,
FormattedSize: humanize.Bytes(uint64(likelySize)),
RegularTotal: regularTotal.ToCoin(),
TicketTotal: ticketTotal.ToCoin(),
VoteTotal: voteTotal.ToCoin(),
RevokeTotal: revTotal.ToCoin(),
Count: numLikely,
},
LatestTransactions: latest,
FormattedTotalSize: formattedSize,
TicketIndexes: ticketSpendInds,
VotingInfo: votingInfo,
InvRegular: invRegular,
InvStake: invStake,
},
Transactions: regular,
Tickets: tickets,
Votes: votes,
Revocations: revs,
}
return &mpInfo
}