/
mempoolcache.go
153 lines (128 loc) · 4.01 KB
/
mempoolcache.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
// Copyright (c) 2017, Jonathan Chappelow
// See LICENSE for details.
package mempool
import (
"sync"
"time"
"github.com/decred/dcrd/dcrjson"
apitypes "github.com/decred/dcrdata/v3/api/types"
)
// MempoolDataCache models the basic data for the mempool cache
type MempoolDataCache struct {
sync.RWMutex
height uint32
timestamp time.Time
numTickets uint32
ticketFeeInfo dcrjson.FeeInfoMempool
allFees []float64
allFeeRates []float64
lowestMineableByFeeRate float64
allTicketsDetails TicketsDetails
}
// StoreMPData stores info from data in the mempool cache
func (c *MempoolDataCache) StoreMPData(data *MempoolData, timestamp time.Time) error {
c.Lock()
defer c.Unlock()
c.height = data.Height
c.timestamp = timestamp
c.numTickets = data.NumTickets
c.ticketFeeInfo = data.Ticketfees.FeeInfoMempool
c.allFees = data.MinableFees.allFees
c.allFeeRates = data.MinableFees.allFeeRates
c.lowestMineableByFeeRate = data.MinableFees.lowestMineableFee
c.allTicketsDetails = data.AllTicketsDetails
return nil
}
// GetHeight returns the mempool height
func (c *MempoolDataCache) GetHeight() uint32 {
return c.height
}
// GetNumTickets returns the mempool height and number of tickets
func (c *MempoolDataCache) GetNumTickets() (uint32, uint32) {
c.RLock()
defer c.RUnlock()
return c.height, c.numTickets
}
// GetFeeInfo returns the mempool height and basic fee info
func (c *MempoolDataCache) GetFeeInfo() (uint32, dcrjson.FeeInfoMempool) {
c.RLock()
defer c.RUnlock()
return c.height, c.ticketFeeInfo
}
// GetFeeInfoExtra returns the mempool height and detailed fee info
func (c *MempoolDataCache) GetFeeInfoExtra() (uint32, *apitypes.MempoolTicketFeeInfo) {
c.RLock()
defer c.RUnlock()
feeInfo := apitypes.MempoolTicketFeeInfo{
Height: c.height,
Time: c.timestamp.Unix(),
FeeInfoMempool: c.ticketFeeInfo,
LowestMineable: c.lowestMineableByFeeRate,
}
return c.height, &feeInfo
}
// GetFees returns the mempool height number of fees and an array of the fields
func (c *MempoolDataCache) GetFees(N int) (uint32, int, []float64) {
c.RLock()
defer c.RUnlock()
numFees := len(c.allFees)
//var fees []float64
fees := []float64{} // for consistency
if N == 0 {
return c.height, numFees, fees
}
if N < 0 || N >= numFees {
fees = make([]float64, numFees)
copy(fees, c.allFees)
} else if N < numFees {
// fees are in ascending order, take from end of slice
smallestFeeInd := numFees - N
fees = make([]float64, N)
copy(fees, c.allFees[smallestFeeInd:])
}
return c.height, numFees, fees
}
// GetFeeRates returns the mempool height, time, number of fees and an array of
// fee rates
func (c *MempoolDataCache) GetFeeRates(N int) (uint32, int64, int, []float64) {
c.RLock()
defer c.RUnlock()
numFees := len(c.allFeeRates)
//var fees []float64
fees := []float64{}
if N == 0 {
return c.height, c.timestamp.Unix(), numFees, fees
}
if N < 0 || N >= numFees {
fees = make([]float64, numFees)
copy(fees, c.allFeeRates)
} else if N < numFees {
// fees are in ascending order, take from end of slice
smallestFeeInd := numFees - N
fees = make([]float64, N)
copy(fees, c.allFeeRates[smallestFeeInd:])
}
return c.height, c.timestamp.Unix(), numFees, fees
}
// GetTicketsDetails returns the mempool height, time, number of tickets and the
// ticket details
func (c *MempoolDataCache) GetTicketsDetails(N int) (uint32, int64, int, TicketsDetails) {
c.RLock()
defer c.RUnlock()
numSSTx := len(c.allTicketsDetails)
//var details TicketsDetails
details := TicketsDetails{}
if N == 0 {
return c.height, c.timestamp.Unix(), numSSTx, details
}
if N < 0 || N >= numSSTx {
details = make(TicketsDetails, numSSTx)
copy(details, c.allTicketsDetails)
} else if N < numSSTx {
// fees are in ascending order, take from end of slice
smallestFeeInd := numSSTx - N
details = make(TicketsDetails, N)
copy(details, c.allTicketsDetails[smallestFeeInd:])
}
return c.height, c.timestamp.Unix(), numSSTx, details
}