-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathapi_block.go
345 lines (288 loc) · 8.43 KB
/
api_block.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
package blockfrost
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"sync"
)
const (
resourceBlocksAffectedAddresses = "addresses"
)
// Block defines content of a block
type Block struct {
// Block creation time in UNIX time
Time int `json:"time"`
// Block number
Height int `json:"height"`
// Hash of the block
Hash string `json:"hash"`
// Slot number
Slot int `json:"slot"`
// Epoch number
Epoch int `json:"epoch"`
// Slot within the epoch
EpochSlot int `json:"epoch_slot"`
// Bech32 ID of the slot leader or specific block description in case there is no slot leader
SlotLeader string `json:"slot_leader"`
// Block size in Bytes
Size int `json:"size"`
// Number of transactions in the block
TxCount int `json:"tx_count"`
// Total output within the block in Lovelaces
Output *string `json:"output"`
// Total fees within the block in Lovelaces
Fees *string `json:"fees"`
// VRF key of the block
BlockVRF *string `json:"block_vrf"`
// The hash of the operational certificate of the block producer
OPCert *string `json:"op_cert,omitempty"` // omitempty due to webhook test fixtures
// The value of the counter used to produce the operational certificate
OPCertCounter *string `json:"op_cert_counter,omitempty"` // omitempty due to webhook test fixtures
// Hash of the previous block
PreviousBlock string `json:"previous_block"`
// Hash of the next block
NextBlock *string `json:"next_block"`
// Number of block confirmations
Confirmations int `json:"confirmations"`
}
type BlockAffectedAddresses struct {
Address string `json:"address"`
Transactions []struct {
TxHash string `json:"tx_hash"`
} `json:"transactions"`
}
type BlockAffectedAddressesResult struct {
Res []BlockAffectedAddresses
Err error
}
// BlocksLatest Return the latest block available to the backends, also known as the
// tip of the blockchain.
func (c *apiClient) BlockLatest(ctx context.Context) (b Block, err error) {
requestUrl, err := url.Parse((fmt.Sprintf("%s/%s", c.server, resourceBlocksLatest)))
if err != nil {
return
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, requestUrl.String(), nil)
if err != nil {
return
}
res, err := c.handleRequest(req)
if err != nil {
return
}
defer res.Body.Close()
err = json.NewDecoder(res.Body).Decode(&b)
if err != nil {
return
}
return b, nil
}
// Block returns the content of a requested block by the hash or block number
func (c *apiClient) Block(ctx context.Context, hashOrNumber string) (bl Block, err error) {
requestUrl, err := url.Parse(fmt.Sprintf("%s/%s/%s", c.server, resourceBlock, hashOrNumber))
if err != nil {
return
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, requestUrl.String(), nil)
if err != nil {
return
}
res, err := c.handleRequest(req)
if err != nil {
return
}
defer res.Body.Close()
if err = json.NewDecoder(res.Body).Decode(&bl); err != nil {
return
}
return bl, nil
}
// BlocksNext returns the list of blocks following a specific block
// denoted by the hash or block number
func (c *apiClient) BlocksNext(ctx context.Context, hashorNumber string) (bls []Block, err error) {
requestUrl, err := url.Parse(fmt.Sprintf("%s/%s/%s/%s", c.server, resourceBlock, hashorNumber, "next"))
if err != nil {
return
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, requestUrl.String(), nil)
if err != nil {
return
}
res, err := c.handleRequest(req)
if err != nil {
return
}
defer res.Body.Close()
if err = json.NewDecoder(res.Body).Decode(&bls); err != nil {
return
}
return bls, nil
}
// BlocksPrevious returns the list of blocks preceding a specific block
// specified by a hash or block number
func (c *apiClient) BlocksPrevious(ctx context.Context, hashorNumber string) (bls []Block, err error) {
requestUrl, err := url.Parse(fmt.Sprintf("%s/%s/%s/%s", c.server, resourceBlock, hashorNumber, "previous"))
if err != nil {
return
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, requestUrl.String(), nil)
if err != nil {
return
}
res, err := c.handleRequest(req)
if err != nil {
return
}
defer res.Body.Close()
if err = json.NewDecoder(res.Body).Decode(&bls); err != nil {
return
}
return bls, nil
}
// BlocksTransactions returns slice of Transaction within the block specified
// by a hash or block number
func (c *apiClient) BlockTransactions(ctx context.Context, hashOrNumber string) (txs []Transaction, err error) {
requestUrl, err := url.Parse(fmt.Sprintf("%s/%s/%s/%s", c.server, resourceBlock, hashOrNumber, "txs"))
if err != nil {
return
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, requestUrl.String(), nil)
if err != nil {
return
}
res, err := c.handleRequest(req)
if err != nil {
return
}
defer res.Body.Close()
err = json.NewDecoder(res.Body).Decode(&txs)
if err != nil {
return
}
return txs, nil
}
// BlockLatestTransactions returns the transactions within the latest block.
func (c *apiClient) BlockLatestTransactions(ctx context.Context) (txs []Transaction, err error) {
requestUrl, err := url.Parse(fmt.Sprintf("%s/%s", c.server, resourceBlocksLatestTransactions))
if err != nil {
return
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, requestUrl.String(), nil)
if err != nil {
return
}
res, err := c.handleRequest(req)
if err != nil {
return
}
defer res.Body.Close()
if err = json.NewDecoder(res.Body).Decode(&txs); err != nil {
return
}
return txs, nil
}
// BlocksBySlot returns the content of a requested block for a specific slot.
func (c *apiClient) BlockBySlot(ctx context.Context, slotNumber int) (bl Block, err error) {
requestUrl, err := url.Parse(fmt.Sprintf("%s/%s/%d", c.server, resourceBlocksSlot, slotNumber))
if err != nil {
return
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, requestUrl.String(), nil)
if err != nil {
return
}
res, err := c.handleRequest(req)
if err != nil {
return
}
defer res.Body.Close()
err = json.NewDecoder(res.Body).Decode(&bl)
if err != nil {
return
}
return bl, nil
}
// BlocksBySlotAndEpoch returns a Block for a specific slot and epoch
func (c *apiClient) BlocksBySlotAndEpoch(ctx context.Context, slotNumber int, epochNumber int) (bl Block, err error) {
requestUrl, err := url.Parse(fmt.Sprintf("%s/%s/%s/%d/%s/%d", c.server, "blocks", "epoch", epochNumber, "slot", slotNumber))
if err != nil {
return
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, requestUrl.String(), nil)
if err != nil {
return
}
res, err := c.handleRequest(req)
if err != nil {
return
}
defer res.Body.Close()
err = json.NewDecoder(res.Body).Decode(&bl)
if err != nil {
return
}
return bl, nil
}
// BlocksAddresses returns list of addresses affected in the specified block with additional information
func (c *apiClient) BlocksAddresses(ctx context.Context, hashOrNumber string, query APIQueryParams) (txs []BlockAffectedAddresses, err error) {
requestUrl, err := url.Parse(fmt.Sprintf("%s/%s/%s/%s", c.server, resourceBlock, hashOrNumber, resourceBlocksAffectedAddresses))
if err != nil {
return
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, requestUrl.String(), nil)
if err != nil {
return
}
v := req.URL.Query()
v = formatParams(v, query)
req.URL.RawQuery = v.Encode()
res, err := c.handleRequest(req)
if err != nil {
return
}
defer res.Body.Close()
if err = json.NewDecoder(res.Body).Decode(&txs); err != nil {
return
}
return txs, nil
}
func (c *apiClient) BlocksAddressesAll(ctx context.Context, hashOrNumber string) <-chan BlockAffectedAddressesResult {
ch := make(chan BlockAffectedAddressesResult, c.routines)
jobs := make(chan methodOptions, c.routines)
quit := make(chan bool, 1)
wg := sync.WaitGroup{}
for i := 0; i < c.routines; i++ {
wg.Add(1)
go func(jobs chan methodOptions, ch chan BlockAffectedAddressesResult, wg *sync.WaitGroup) {
defer wg.Done()
for j := range jobs {
affectedAddresses, err := c.BlocksAddresses(j.ctx, hashOrNumber, j.query)
if len(affectedAddresses) != j.query.Count || err != nil {
select {
case quit <- true:
default:
}
}
res := BlockAffectedAddressesResult{Res: affectedAddresses, Err: err}
ch <- res
}
}(jobs, ch, &wg)
}
go func() {
defer close(ch)
fetchNextPage := true
for i := 1; fetchNextPage; i++ {
select {
case <-quit:
fetchNextPage = false
default:
jobs <- methodOptions{ctx: ctx, query: APIQueryParams{Count: 100, Page: i}}
}
}
close(jobs)
wg.Wait()
}()
return ch
}