-
Notifications
You must be signed in to change notification settings - Fork 23
/
api.go
246 lines (205 loc) · 5.49 KB
/
api.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
package database
import (
"errors"
"strconv"
"time"
"github.com/0xb10c/memo/api/types"
"github.com/gomodule/redigo/redis"
)
// GetMempool gets the current mempool from the database
func (p *RedisPool) GetMempool() (timestamp time.Time, feerateMapJSON string, megabyteMarkersJSON string, mempoolSize int, err error) {
c := p.Get()
defer c.Close()
prefix := "currentMempool"
response, err := redis.Strings(c.Do("MGET", prefix+":utcTimestamp", prefix+":feerateMap", prefix+":megabyteMarkers", prefix+":mempoolSizeInByte"))
if err != nil {
return
}
if n, err := strconv.Atoi(response[0]); err == nil {
timestamp = time.Unix(int64(n), 0)
} else {
timestamp = time.Unix(0, 0)
}
feerateMapJSON = response[1]
megabyteMarkersJSON = response[2]
if n, err := strconv.Atoi(response[3]); err == nil {
mempoolSize = n
} else {
mempoolSize = 0
}
return
}
// GetRecentBlocks returns the 10 most recent blocks.
func (p *RedisPool) GetRecentBlocks() (blocks []types.RecentBlock, err error) {
c := p.Get()
defer c.Close()
reJSON, err := redis.Strings(c.Do("LRANGE", "recentBlocks", 0, 9))
if err != nil {
return
}
blocks = make([]types.RecentBlock, 0)
for index := range reJSON {
block := types.RecentBlock{}
err = json.Unmarshal([]byte(reJSON[index]), &block)
if err != nil {
return
}
blocks = append(blocks, block)
}
return
}
// GetBlockEntries returns the 20 most recent blocks with short TXIDs.
func (p *RedisPool) GetBlockEntries() (blocks []types.BlockEntry, err error) {
c := p.Get()
defer c.Close()
beJSON, err := redis.Strings(c.Do("LRANGE", "blockEntries", 0, 20))
if err != nil {
return
}
blocks = make([]types.BlockEntry, 0)
for index := range beJSON {
block := types.BlockEntry{}
err = json.Unmarshal([]byte(beJSON[index]), &block)
if err != nil {
return
}
blocks = append(blocks, block)
}
return
}
type MempoolState struct {
time time.Time
Timestamp int64 `json:"timestamp"`
dataInBucketsJSON string
DataInBuckets []float64 `json:"dataInBuckets"`
}
func (p *RedisPool) GetHistorical(timeframe int, by string) (hmds []types.HistoricalMempoolData, err error) {
c := p.Get()
defer c.Close()
var timeSelector string
switch timeframe {
case 1:
timeSelector = "historicalMempool1"
case 2:
timeSelector = "historicalMempool2"
case 3:
timeSelector = "historicalMempool3"
case 4:
timeSelector = "historicalMempool4"
case 5:
timeSelector = "historicalMempool5"
case 6:
timeSelector = "historicalMempool6"
default:
return hmds, errors.New("Invalid input")
}
var bySelector string
switch by {
case "byCount":
bySelector = "countInBuckets"
case "byFee":
bySelector = "feeInBuckets"
case "bySize":
bySelector = "sizeInBuckets"
default:
return hmds, errors.New("Invalid input")
}
hmdsJSON, err := redis.Strings(c.Do("LRANGE", timeSelector+":"+bySelector, 0, 29))
if err != nil {
return
}
hmds = make([]types.HistoricalMempoolData, 0)
for index := range hmdsJSON {
hmd := types.HistoricalMempoolData{}
err = json.Unmarshal([]byte(hmdsJSON[index]), &hmd)
if err != nil {
return
}
hmds = append(hmds, hmd)
}
return
}
// GetTransactionStats gets the Transaction Stats data from the database
func (p *RedisPool) GetTransactionStats() (tss []types.TransactionStat, err error) {
c := p.Get()
defer c.Close()
tssJSON, err := redis.Strings(c.Do("LRANGE", "transactionStats", 0, 180))
if err != nil {
return
}
type transactionStat struct {
SegwitCount int `json:"segwitCount"`
RbfCount int `json:"rbfCount"`
TxCount int `json:"txCount"`
Timestamp int64 `json:"timestamp"`
}
tss = make([]types.TransactionStat, 0)
for index := range tssJSON {
ts := types.TransactionStat{}
err = json.Unmarshal([]byte(tssJSON[index]), &ts)
if err != nil {
return
}
tss = append(tss, ts)
}
return
}
// GetMempoolEntries gets the last x mempool Entries from the database
func (p *RedisPool) GetMempoolEntries() (mes []types.MempoolEntry, err error) {
c := p.Get()
defer c.Close()
// gets recent entries from 0 to 19999 (20k)
mesJSON, err := redis.Strings(c.Do("ZREVRANGE", "mempoolEntries", 0, 29999))
if err != nil {
return
}
mes = make([]types.MempoolEntry, 0)
for index := range mesJSON {
me := types.MempoolEntry{}
err = json.Unmarshal([]byte(mesJSON[index]), &me)
if err != nil {
return
}
mes = append(mes, me)
}
return
}
// SetMempoolEntriesCache SETs the response of a recent GetMempoolEntries() as a cache
func (p *RedisPool) SetMempoolEntriesCache(mesJSON string) (err error) {
c := p.Get()
defer c.Close()
_, err = c.Do("SET", "cache:mempoolEntries", mesJSON)
if err != nil {
return err
}
return nil
}
// GetMempoolEntriesCache GET the cached response of a recent GetMempoolEntries() call
func (p *RedisPool) GetMempoolEntriesCache() (mesJSON string, err error) {
c := p.Get()
defer c.Close()
mesJSON, err = redis.String(c.Do("GET", "cache:mempoolEntries"))
if err != nil {
return
}
return
}
// GetRecentFeerateAPIEntries returns the recent feeRate API entries from Redis
func (p *RedisPool) GetRecentFeerateAPIEntries() (entries []types.FeeRateAPIEntry, err error) {
c := p.Get()
defer c.Close()
entriesJSON, err := redis.Strings(c.Do("LRANGE", "feerateAPIEntries", 0, 400))
if err != nil {
return
}
entries = make([]types.FeeRateAPIEntry, 0)
for index := range entriesJSON {
entry := types.FeeRateAPIEntry{}
err = json.Unmarshal([]byte(entriesJSON[index]), &entry)
if err != nil {
return
}
entries = append(entries, entry)
}
return
}