-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathblock.go
221 lines (187 loc) · 5.51 KB
/
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
// SPDX-License-Identifier: Apache-2.0
//
// Copyright © 2022 The Cardano Community Authors
package koios
import (
"context"
"encoding/json"
"fmt"
"io"
"github.com/shopspring/decimal"
)
type (
// Block defines model for block.
Block struct {
// Hash block hash
Hash BlockHash `json:"hash"`
// EpochNo number.
EpochNo EpochNo `json:"epoch_no"`
// AbsSlot is overall slot number (slots from genesis block of chain).
AbsSlot Slot `json:"abs_slot"`
// EpochSlot slot number within epoch.
EpochSlot Slot `json:"epoch_slot"`
// Height is block number on chain where transaction was included.
Height int `json:"block_height"`
// Size of block.
Size int `json:"block_size"`
// Time of the block.
Time Timestamp `json:"block_time"`
// TxCount transactions count in block.
TxCount int `json:"tx_count"`
// VrfKey is pool VRF key.
VrfKey string `json:"vrf_key"`
// OpCert latest ool operational certificate hash
OpCert string `json:"op_cert,omitempty"`
// Pool ID.
Pool string `json:"pool"`
// OpCertCounter is pool latest operational certificate counter value.
OpCertCounter int `json:"op_cert_counter"`
// ParentHash parent block hash
ParentHash BlockHash `json:"parent_hash,omitempty"`
// ChildHash child block hash
ChildHash BlockHash `json:"child_hash,omitempty"`
// ProtoMajor is protocol major version
ProtoMajor int `json:"proto_major,omitempty"`
// ProtoMinor is protocol minor version
ProtoMinor int `json:"proto_minor"`
// TotalOutput output of the block (in lovelace)
TotalOutput decimal.Decimal `json:"total_output,omitempty"`
// TotalOutput Total fees of the block (in lovelace)
TotalFees decimal.Decimal `json:"total_fees,omitempty"`
// Confirmations is number of confirmations for the block
Confirmations int `json:"num_confirmations,omitempty"`
}
// BlocksResponse represents response from `/blocks` endpoint.
BlocksResponse struct {
Response
Data []Block `json:"data"`
}
// BlockInfoResponse represents response from `/block_info` endpoint.
BlockInfoResponse struct {
Response
Data Block `json:"data"`
}
// BlockInfoResponse represents response from `/block_info` endpoint.
BlocksInfoResponse struct {
Response
Data []Block `json:"data"`
}
// BlockTxsHashesResponse represents response from `/block_txs` endpoint.
BlockTxs struct {
BlockHash BlockHash `json:"block_hash"`
TxHashes TxHash `json:"tx_hash"`
EpochNo EpochNo `json:"epoch_no"`
BlockHeight uint `json:"block_height"`
BlockTime Timestamp `json:"block_time"`
}
BlocksTxsResponse struct {
Response
Data []BlockTxs `json:"data"`
}
BlockTxsResponse struct {
Response
Data BlockTxs `json:"data"`
}
)
// GetBlocks returns summarised details about all blocks (paginated - latest first).
func (c *Client) GetBlocks(
ctx context.Context,
opts *RequestOptions,
) (res *BlocksResponse, err error) {
res = &BlocksResponse{}
rsp, err := c.request(ctx, &res.Response, "GET", "/blocks", nil, opts)
if err != nil {
return
}
err = ReadAndUnmarshalResponse(rsp, &res.Response, &res.Data)
return
}
// GetBlockInfo returns detailed information about a specific block.
func (c *Client) GetBlockInfo(
ctx context.Context,
hash BlockHash,
opts *RequestOptions,
) (res *BlockInfoResponse, err error) {
res = &BlockInfoResponse{}
rsp, err := c.GetBlockInfos(ctx, []BlockHash{hash}, opts)
res.Response = rsp.Response
if len(rsp.Data) > 0 {
res.Data = rsp.Data[0]
} else {
err = fmt.Errorf("%w: block_info response was empty", ErrResponse)
}
return
}
// GetBlocksInfo returns detailed information about a set of blocks.
func (c *Client) GetBlockInfos(
ctx context.Context,
hashes []BlockHash,
opts *RequestOptions,
) (res *BlocksInfoResponse, err error) {
res = &BlocksInfoResponse{}
rsp, err := c.request(ctx, &res.Response, "POST", "/block_info", blockHashesPL(hashes), opts)
if err != nil {
return
}
err = ReadAndUnmarshalResponse(rsp, &res.Response, &res.Data)
return
}
// GetBlocksTxs returns a list of all transactions included in a blocks.
func (c *Client) GetBlockTxs(
ctx context.Context,
hash BlockHash,
opts *RequestOptions,
) (res *BlockTxsResponse, err error) {
res = &BlockTxsResponse{}
rsp, err := c.GetBlocksTxs(ctx, []BlockHash{hash}, opts)
res.Response = rsp.Response
if len(rsp.Data) > 0 {
res.Data = rsp.Data[0]
} else {
err = fmt.Errorf("%w: block %s had no transactions", ErrNoData, hash)
}
return
}
// GetBlocksTxs returns a list of all transactions included in a blocks.
func (c *Client) GetBlocksTxs(
ctx context.Context,
hashes []BlockHash,
opts *RequestOptions,
) (res *BlocksTxsResponse, err error) {
res = &BlocksTxsResponse{}
rsp, err := c.request(ctx, &res.Response, "POST", "/block_txs", blockHashesPL(hashes), opts)
if err != nil {
return
}
err = ReadAndUnmarshalResponse(rsp, &res.Response, &res.Data)
return
}
func blockHashesPL(bhash []BlockHash) io.Reader {
var payload = struct {
BlockHashes []BlockHash `json:"_block_hashes"`
}{bhash}
rpipe, w := io.Pipe()
go func() {
_ = json.NewEncoder(w).Encode(payload)
defer w.Close()
}()
return rpipe
}
// handle api json tags Block.epoch and Block.epoch_no.
// SEE: https://github.com/cardano-community/koios-artifacts/issues/102
func (block *Block) UnmarshalJSON(b []byte) error {
type B Block
if err := json.Unmarshal(b, (*B)(block)); err != nil {
return err
}
if block.EpochNo == 0 {
var fix = struct {
EpochNo EpochNo `json:"epoch"`
}{}
if err := json.Unmarshal(b, &fix); err != nil {
return err
}
block.EpochNo = fix.EpochNo
}
return nil
}