-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathrpc.go
559 lines (448 loc) · 11.3 KB
/
rpc.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
// Package rpc provides an RPC client to the snarkOS Aleo client.
package rpc
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
)
// Config holds the configuration for the RPC client.
type Config struct {
User string
Password string
Host string
Port string
}
// Client maintains a connection to the Aleo client.
type Client struct {
cfg *Config
httpClient *http.Client
}
// NewClient returns a new RPC client.
func NewClient(cfg *Config) (*Client, error) {
httpClient := &http.Client{}
return &Client{
httpClient: httpClient,
cfg: cfg,
}, nil
}
// newRequest creates a serialized request.
func newRequest(client *Client, body []byte) (*Result, error) {
url := fmt.Sprintf("http://%s:%s", client.cfg.Host, client.cfg.Port)
req, err := http.NewRequest("POST", url, bytes.NewBuffer(body))
if err != nil {
return nil, err
}
req.Header.Add("content-type", "application/json")
req.SetBasicAuth(client.cfg.User, client.cfg.Password)
resp, err := client.httpClient.Do(req)
if err != nil {
return nil, err
}
buf, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var result Result
if err := json.Unmarshal(buf, &result); err != nil {
return nil, err
}
if result.Error != nil {
return nil, fmt.Errorf("%v", result.Error)
}
return &result, nil
}
func newRequestBody(rpcVersion int, id string, method string, params []json.RawMessage) ([]byte, error) {
req := &Request{
Method: method,
Params: params,
ID: id,
RPCVersion: "2.0",
}
return json.Marshal(req)
}
// GetBlockHeight the block height for the given the block hash.
func (c *Client) GetBlockHeight(blockHash string) (int64, error) {
param, err := json.Marshal(blockHash)
if err != nil {
return 0, err
}
req, err := newRequestBody(2, "", getBlockHeightMethod, []json.RawMessage{param})
if err != nil {
return 0, err
}
resp, err := newRequest(c, req)
if err != nil {
return 0, err
}
var count int64
if err := json.Unmarshal(resp.Result, &count); err != nil {
return 0, err
}
return count, nil
}
// GetBestBlockHash returns the block hash of the head of the best valid chain.
func (c *Client) GetBestBlockHash() (string, error) {
req, err := newRequestBody(2, "", getBestBlockHashMethod, nil)
if err != nil {
return "", err
}
resp, err := newRequest(c, req)
if err != nil {
return "", err
}
var res string
if err := json.Unmarshal(resp.Result, &res); err != nil {
return "", err
}
return res, nil
}
// GetBlock returns information about a block from a block height.
func (c *Client) GetBlock(blockNumber int64) (*Block, error) {
param, err := json.Marshal(blockNumber)
if err != nil {
return nil, err
}
req, err := newRequestBody(2, "", getBlockMethod, []json.RawMessage{param})
if err != nil {
return nil, err
}
resp, err := newRequest(c, req)
if err != nil {
return nil, err
}
var res Block
if err := json.Unmarshal(resp.Result, &res); err != nil {
return nil, err
}
return &res, nil
}
// GetBlockHash returns the block hash of a block at the given block height in the best valid chain.
func (c *Client) GetBlockHash(height int64) (string, error) {
param, err := json.Marshal(height)
if err != nil {
return "", err
}
req, err := newRequestBody(2, "", getBlockHashMethod, []json.RawMessage{param})
if err != nil {
return "", err
}
resp, err := newRequest(c, req)
if err != nil {
return "", err
}
var res string
if err := json.Unmarshal(resp.Result, &res); err != nil {
return "", err
}
return res, nil
}
// GetTransaction returns a transaction with metadata given the transaction ID.
func (c *Client) GetTransaction(txID string) (*GetTransactionResponse, error) {
param, err := json.Marshal(txID)
if err != nil {
return nil, err
}
req, err := newRequestBody(2, "", getTransactionMethod, []json.RawMessage{param})
if err != nil {
return nil, err
}
resp, err := newRequest(c, req)
if err != nil {
return nil, err
}
var res GetTransactionResponse
if err := json.Unmarshal(resp.Result, &res); err != nil {
return nil, err
}
return &res, nil
}
// GetTransition returns a transition given an id.
func (c *Client) GetTransition(transitionID string) (*Transition, error) {
param, err := json.Marshal(transitionID)
if err != nil {
return nil, err
}
req, err := newRequestBody(2, "", getTransitionMethod, []json.RawMessage{param})
if err != nil {
return nil, err
}
resp, err := newRequest(c, req)
if err != nil {
return nil, err
}
var res Transition
if err := json.Unmarshal(resp.Result, &res); err != nil {
return nil, err
}
return &res, nil
}
// SendTransaction sends raw transaction bytes to this node to be added into the mempool.
// If valid, the transaction will be stored and propagated to all peers.
func (c *Client) SendTransaction(txHex string) (string, error) {
param, err := json.Marshal(txHex)
if err != nil {
return "", err
}
req, err := newRequestBody(2, "", sendTransactionMethod, []json.RawMessage{param})
if err != nil {
return "", err
}
resp, err := newRequest(c, req)
if err != nil {
return "", err
}
var res string
if err := json.Unmarshal(resp.Result, &res); err != nil {
return "", err
}
return res, nil
}
// LatestLedgerRoot returns the latest ledger root.
func (c *Client) LatestLedgerRoot() (string, error) {
req, err := newRequestBody(2, "", latestLedgerRootMethod, nil)
if err != nil {
return "", err
}
resp, err := newRequest(c, req)
if err != nil {
return "", err
}
var res string
if err := json.Unmarshal(resp.Result, &res); err != nil {
return "", err
}
return res, nil
}
// GetLedgerProof returns the ledger proof of a given record commitment.
func (c *Client) GetLedgerProof(recordCommitment string) (string, error) {
param, err := json.Marshal(recordCommitment)
if err != nil {
return "", err
}
req, err := newRequestBody(2, "", getLedgerProofMethod, []json.RawMessage{param})
if err != nil {
return "", err
}
resp, err := newRequest(c, req)
if err != nil {
return "", err
}
var res string
if err := json.Unmarshal(resp.Result, &res); err != nil {
return "", err
}
return res, nil
}
// GetCiphertext returns the ciphertext using a given id.
func (c *Client) GetCiphertext(id string) (string, error) {
param, err := json.Marshal(id)
if err != nil {
return "", err
}
req, err := newRequestBody(2, "", getCiphertextMethod, []json.RawMessage{param})
if err != nil {
return "", err
}
resp, err := newRequest(c, req)
if err != nil {
return "", err
}
var res string
if err := json.Unmarshal(resp.Result, &res); err != nil {
return "", err
}
return res, nil
}
// GetBlockHashes returns blockhashes for a given range of block heights.
func (c *Client) GetBlockHashes(start, end int64) ([]string, error) {
if start > end {
return nil, errors.New("start > end")
}
startParam, err := json.Marshal(start)
if err != nil {
return nil, err
}
endParam, err := json.Marshal(end)
if err != nil {
return nil, err
}
req, err := newRequestBody(2, "", getBlockHashesMethod, []json.RawMessage{startParam, endParam})
if err != nil {
return nil, err
}
resp, err := newRequest(c, req)
if err != nil {
return nil, err
}
var res []string
if err := json.Unmarshal(resp.Result, &res); err != nil {
return nil, err
}
return res, nil
}
// GetBlockHeader returns the blockheader of a given block height.
func (c *Client) GetBlockHeader(height int64) (*BlockHeader, error) {
param, err := json.Marshal(height)
if err != nil {
return nil, err
}
req, err := newRequestBody(2, "", getBlockHeaderMethod, []json.RawMessage{param})
if err != nil {
return nil, err
}
resp, err := newRequest(c, req)
if err != nil {
return nil, err
}
var res BlockHeader
if err := json.Unmarshal(resp.Result, &res); err != nil {
return nil, err
}
return &res, nil
}
// GetBlocks returns a range of blocks with the given block heights.
func (c *Client) GetBlocks(start, end int64) ([]Block, error) {
if start > end {
return nil, errors.New("start > end")
}
startParam, err := json.Marshal(start)
if err != nil {
return nil, err
}
endParam, err := json.Marshal(end)
if err != nil {
return nil, err
}
req, err := newRequestBody(2, "", getBlocksMethod, []json.RawMessage{startParam, endParam})
if err != nil {
return nil, err
}
resp, err := newRequest(c, req)
if err != nil {
return nil, err
}
var res []Block
if err := json.Unmarshal(resp.Result, &res); err != nil {
return nil, err
}
return res, nil
}
// GetBlockTransactions returns transactions in a block at a given height.
func (c *Client) GetBlockTransactions(height int64) (*Transactions, error) {
param, err := json.Marshal(height)
if err != nil {
return nil, err
}
req, err := newRequestBody(2, "", getBlockTransactionsMethod, []json.RawMessage{param})
if err != nil {
return nil, err
}
resp, err := newRequest(c, req)
if err != nil {
return nil, err
}
var res Transactions
if err := json.Unmarshal(resp.Result, &res); err != nil {
return nil, err
}
return &res, nil
}
// LatestBlock returns the latest block.
func (c *Client) LatestBlock() (*Block, error) {
req, err := newRequestBody(2, "", latestBlockMethod, nil)
if err != nil {
return nil, err
}
resp, err := newRequest(c, req)
if err != nil {
return nil, err
}
var res Block
if err := json.Unmarshal(resp.Result, &res); err != nil {
return nil, err
}
return &res, nil
}
// LatestBlockHash returns the latest blockhash.
func (c *Client) LatestBlockHash() (string, error) {
req, err := newRequestBody(2, "", latestBlockHashMethod, nil)
if err != nil {
return "", err
}
resp, err := newRequest(c, req)
if err != nil {
return "", err
}
var res string
if err := json.Unmarshal(resp.Result, &res); err != nil {
return "", err
}
return res, nil
}
// LatestBlockHeader returns the latest blcok header.
func (c *Client) LatestBlockHeader() (*BlockHeader, error) {
req, err := newRequestBody(2, "", latestBlockHeaderMethod, nil)
if err != nil {
return nil, err
}
resp, err := newRequest(c, req)
if err != nil {
return nil, err
}
var res BlockHeader
if err := json.Unmarshal(resp.Result, &res); err != nil {
return nil, err
}
return &res, nil
}
// LatestBlockHeight returns the latest blockheight.
func (c *Client) LatestBlockHeight() (int64, error) {
req, err := newRequestBody(2, "", latestBlockHeightMethod, nil)
if err != nil {
return 0, err
}
resp, err := newRequest(c, req)
if err != nil {
return 0, err
}
var res int64
if err := json.Unmarshal(resp.Result, &res); err != nil {
return 0, err
}
return res, nil
}
// LatestBlockTransactions returns a list of transactions for the latest block.
func (c *Client) LatestBlockTransactions() (*Transactions, error) {
req, err := newRequestBody(2, "", latestBlockTransactionsMethod, nil)
if err != nil {
return nil, err
}
resp, err := newRequest(c, req)
if err != nil {
return nil, err
}
var res Transactions
if err := json.Unmarshal(resp.Result, &res); err != nil {
return nil, err
}
return &res, nil
}
// GetConnectedPeers returns a list of connected peers.
func (c *Client) GetConnectedPeers() ([]string, error) {
req, err := newRequestBody(2, "", getConnectedPeersMethod, nil)
if err != nil {
return nil, err
}
resp, err := newRequest(c, req)
if err != nil {
return nil, err
}
var res []string
if err := json.Unmarshal(resp.Result, &res); err != nil {
return nil, err
}
return res, nil
}