forked from goat-systems/go-tezos
/
chains.go
380 lines (312 loc) · 8.55 KB
/
chains.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
package gotezos
import (
"encoding/json"
"fmt"
"strconv"
"time"
"github.com/pkg/errors"
)
/*
Checkpoint represents a Tezos checkpoint.
RPC:
/chains/<chain_id>/checkpoint (GET)
Link:
https://tezos.gitlab.io/api/rpc.html#get-chains-chain-id-checkpoint
*/
type Checkpoint struct {
Block struct {
Level int `json:"level"`
Proto int `json:"proto"`
Predecessor string `json:"predecessor"`
Timestamp time.Time `json:"timestamp"`
ValidationPass int `json:"validation_pass"`
OperationsHash string `json:"operations_hash"`
Fitness []string `json:"fitness"`
Context string `json:"context"`
ProtocolData string `json:"protocol_data"`
} `json:"block"`
SavePoint int `json:"save_point"`
Caboose int `json:"caboose"`
HistoryMode string `json:"history_mode"`
}
/*
InvalidBlock represents Tezos invalid blocks.
RPC:
/chains/<chain_id>/invalid_blocks (GET)
Link:
https://tezos.gitlab.io/api/rpc.html#get-chains-chain-id-invalid-blocks
*/
type InvalidBlock struct {
Block string `json:"block"`
Level int `json:"level"`
Errors RPCErrors `json:"errors"`
}
/*
BlocksInput is the input for the goTezos.Blocks function.
Function:
func (t *GoTezos) EndorsingRights(input *EndorsingRightsInput) (*EndorsingRights, error) {}
*/
type BlocksInput struct {
//length is the requested number of predecessors to returns (per requested head).
Length int
//An empty argument requests blocks from the current heads. A non empty list allow to request specific fragment of the chain.
Head string
// When `min_date` is provided, heads with a timestamp before `min_date` are filtered out
MinDate *time.Time
}
/*
OperationsAlt represents a JSON array containing an opHash at index 0, and
an Operation object at index 1. The RPC does not properly objectify Refused,
BranchRefused, BranchDelayed, and Unprocessed sections of the mempool,
so we must parse them manually.
Code hints used from github.com/blockwatch-cc/tzindex/rpc/mempool.go
*/
type OperationsAlt Operations
func (o *OperationsAlt) UnmarshalJSON(buf []byte) error {
return unmarshalNamedJSONArray(buf, &o.Hash, (*Operations)(o))
}
func unmarshalNamedJSONArray(data []byte, v ...interface{}) error {
var raw []json.RawMessage
if err := json.Unmarshal(data, &raw); err != nil {
return err
}
if len(raw) < len(v) {
return fmt.Errorf("JSON array is too short, expected %d, got %d", len(v), len(raw))
}
for i, vv := range v {
if err := json.Unmarshal(raw[i], vv); err != nil {
return err
}
}
return nil
}
/*
MempoolInput is the input for the goTezos.Mempool function.
Function:
func (t *GoTezos) Mempool(input *MempoolInput) (Mempool, error) {}
*/
type MempoolInput struct {
// Specify the ChainID.
ChainID string `validate:"required"`
// Mempool filters
Applied bool
BranchDelayed bool
Refused bool
BranchRefused bool
}
/*
Mempool represents the contents of the Tezos mempool.
RPC:
/chains/<chain_id>/mempool/pending_operations (GET)
*/
type Mempool struct {
Applied []Operations `json:"applied"`
Refused []OperationsAlt `json:"refused"`
BranchRefused []OperationsAlt `json:"branch_refused"`
BranchDelayed []OperationsAlt `json:"branch_delayed"`
Unprocessed []OperationsAlt `json:"unprocessed"`
}
/*
Mempool fetches the current contents of main the chain mempool.
Path:
/chains/<chain_id>/mempool/pending_operations (GET)
Parameters:
None
*/
func (t *GoTezos) Mempool(input MempoolInput) (Mempool, error) {
var mempool Mempool
resp, err := t.get(fmt.Sprintf("/chains/%s/mempool/pending_operations", input.ChainID), input.contructRPCOptions()...)
if err != nil {
return mempool, errors.Wrap(err, "failed to fetch mempool contents")
}
err = json.Unmarshal(resp, &mempool)
if err != nil {
return mempool, errors.Wrap(err, "failed to unmarshal mempool contents")
}
return mempool, nil
}
func (m *MempoolInput) contructRPCOptions() []rpcOptions {
var opts []rpcOptions
if m.Applied {
opts = append(opts, rpcOptions{
"applied",
"true",
})
} else {
opts = append(opts, rpcOptions{
"applied",
"false",
})
}
if m.BranchDelayed {
opts = append(opts, rpcOptions{
"branch_delayed",
"true",
})
} else {
opts = append(opts, rpcOptions{
"branch_delayed",
"false",
})
}
if m.Refused {
opts = append(opts, rpcOptions{
"refused",
"true",
})
} else {
opts = append(opts, rpcOptions{
"refused",
"false",
})
}
if m.BranchRefused {
opts = append(opts, rpcOptions{
"branch_refused",
"true",
})
} else {
opts = append(opts, rpcOptions{
"branch_refused",
"false",
})
}
return opts
}
/*
Blocks lists known heads of the blockchain sorted with decreasing fitness.
Optional arguments allows to returns the list of predecessors for known heads or
the list of predecessors for a given list of blocks.
Path:
/chains/<chain_id>/blocks (GET)
Link:
https://tezos.gitlab.io/api/rpc.html#get-chains-chain-id-blocks
Parameters:
input:
Modifies the Blocks RPC query by passing optional URL parameters.
*/
func (t *GoTezos) Blocks(input BlocksInput) ([][]string, error) {
resp, err := t.get("/chains/main/blocks", input.contructRPCOptions()...)
if err != nil {
return [][]string{}, errors.Wrap(err, "failed to get blocks")
}
var blocks [][]string
err = json.Unmarshal(resp, &blocks)
if err != nil {
return [][]string{}, errors.Wrap(err, "failed to unmarshal blocks")
}
return blocks, nil
}
func (b *BlocksInput) contructRPCOptions() []rpcOptions {
var opts []rpcOptions
if b.Length > 0 {
opts = append(opts, rpcOptions{
"length",
strconv.Itoa(b.Length),
})
}
if b.Head != "" {
opts = append(opts, rpcOptions{
"head",
b.Head,
})
}
if b.MinDate != nil {
opts = append(opts, rpcOptions{
"min_date",
strconv.Itoa(int(b.MinDate.Unix())),
})
}
return opts
}
/*
ChainID gets the chain unique identifier.
Path:
/chains/<chain_id>/chain_id (GET)
Link:
https://tezos.gitlab.io/api/rpc.html#get-chains-chain-id-chain-id
*/
func (t *GoTezos) ChainID() (string, error) {
resp, err := t.get("/chains/main/chain_id")
if err != nil {
return "", errors.Wrapf(err, "failed to get chain id")
}
var chainID string
err = json.Unmarshal(resp, &chainID)
if err != nil {
return "", errors.Wrapf(err, "failed to unmarshal chain id")
}
return chainID, nil
}
/*
Checkpoint gets the current checkpoint for this chain.
Path:
/chains/<chain_id>/checkpoint (GET)RPC
Link:
https://tezos.gitlab.io/api/rpc.html#get-chains-chain-id-checkpoint
*/
func (t *GoTezos) Checkpoint() (Checkpoint, error) {
resp, err := t.get("/chains/main/checkpoint")
if err != nil {
return Checkpoint{}, errors.Wrap(err, "failed to get checkpoint")
}
var c Checkpoint
err = json.Unmarshal(resp, &c)
if err != nil {
return c, errors.Wrap(err, "failed to unmarshal checkpoint")
}
return c, nil
}
/*
InvalidBlocks lists blocks that have been declared invalid
along with the errors that led to them being declared invalid.
Path:
/chains/<chain_id>/invalid_blocks (GET)
Link:
https://tezos.gitlab.io/api/rpc.html#get-chains-chain-id-invalid-blocks
*/
func (t *GoTezos) InvalidBlocks() ([]InvalidBlock, error) {
resp, err := t.get("/chains/main/invalid_blocks")
if err != nil {
return []InvalidBlock{}, errors.Wrap(err, "failed to get invalid blocks")
}
var blocks []InvalidBlock
err = json.Unmarshal(resp, &blocks)
if err != nil {
return []InvalidBlock{}, errors.Wrap(err, "failed to unmarshal invalid blocks")
}
return blocks, nil
}
/*
InvalidBlock gets the errors that appears during the block (in)validation.
Path:
/chains/<chain_id>/invalid_blocks/<block_hash> (GET)
Link:
https://tezos.gitlab.io/api/rpc.html#get-chains-chain-id-invalid-blocks-block-hash
*/
func (t *GoTezos) InvalidBlock(blockHash string) (InvalidBlock, error) {
resp, err := t.get(fmt.Sprintf("/chains/main/invalid_blocks/%s", blockHash))
if err != nil {
return InvalidBlock{}, errors.Wrap(err, "failed to get invalid blocks")
}
var block InvalidBlock
err = json.Unmarshal(resp, &block)
if err != nil {
return InvalidBlock{}, errors.Wrap(err, "failed to unmarshal invalid blocks")
}
return block, nil
}
/*
DeleteInvalidBlock remove an invalid block for the tezos storage.
Path:
/chains/<chain_id>/invalid_blocks/<block_hash> (DELETE)
Link:
https://tezos.gitlab.io/api/rpc.html#delete-chains-chain-id-invalid-blocks-block-hash
*/
func (t *GoTezos) DeleteInvalidBlock(blockHash string) error {
_, err := t.delete(fmt.Sprintf("/chains/main/invalid_blocks/%s", blockHash))
if err != nil {
return errors.Wrap(err, "failed to delete invalid blocks")
}
return nil
}