-
Notifications
You must be signed in to change notification settings - Fork 644
/
client.go
547 lines (502 loc) · 19.2 KB
/
client.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
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package platformvm
import (
"context"
"time"
"github.com/ava-labs/avalanchego/api"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow/validators"
"github.com/ava-labs/avalanchego/utils/constants"
"github.com/ava-labs/avalanchego/utils/crypto/secp256k1"
"github.com/ava-labs/avalanchego/utils/formatting"
"github.com/ava-labs/avalanchego/utils/formatting/address"
"github.com/ava-labs/avalanchego/utils/json"
"github.com/ava-labs/avalanchego/utils/rpc"
"github.com/ava-labs/avalanchego/vms/platformvm/status"
)
var _ Client = (*client)(nil)
// Client interface for interacting with the P Chain endpoint
type Client interface {
// GetHeight returns the current block height of the P Chain
GetHeight(ctx context.Context, options ...rpc.Option) (uint64, error)
// ExportKey returns the private key corresponding to [address] from [user]'s account
//
// Deprecated: Keys should no longer be stored on the node.
ExportKey(ctx context.Context, user api.UserPass, address ids.ShortID, options ...rpc.Option) (*secp256k1.PrivateKey, error)
// GetBalance returns the balance of [addrs] on the P Chain
//
// Deprecated: GetUTXOs should be used instead.
GetBalance(ctx context.Context, addrs []ids.ShortID, options ...rpc.Option) (*GetBalanceResponse, error)
// ListAddresses returns an array of platform addresses controlled by [user]
//
// Deprecated: Keys should no longer be stored on the node.
ListAddresses(ctx context.Context, user api.UserPass, options ...rpc.Option) ([]ids.ShortID, error)
// GetUTXOs returns the byte representation of the UTXOs controlled by [addrs]
GetUTXOs(
ctx context.Context,
addrs []ids.ShortID,
limit uint32,
startAddress ids.ShortID,
startUTXOID ids.ID,
options ...rpc.Option,
) ([][]byte, ids.ShortID, ids.ID, error)
// GetAtomicUTXOs returns the byte representation of the atomic UTXOs controlled by [addrs]
// from [sourceChain]
GetAtomicUTXOs(
ctx context.Context,
addrs []ids.ShortID,
sourceChain string,
limit uint32,
startAddress ids.ShortID,
startUTXOID ids.ID,
options ...rpc.Option,
) ([][]byte, ids.ShortID, ids.ID, error)
// GetSubnet returns information about the specified subnet
GetSubnet(ctx context.Context, subnetID ids.ID, options ...rpc.Option) (GetSubnetClientResponse, error)
// GetSubnets returns information about the specified subnets
//
// Deprecated: Subnets should be fetched from a dedicated indexer.
GetSubnets(ctx context.Context, subnetIDs []ids.ID, options ...rpc.Option) ([]ClientSubnet, error)
// GetStakingAssetID returns the assetID of the asset used for staking on
// subnet corresponding to [subnetID]
GetStakingAssetID(ctx context.Context, subnetID ids.ID, options ...rpc.Option) (ids.ID, error)
// GetCurrentValidators returns the list of current validators for subnet with ID [subnetID]
GetCurrentValidators(ctx context.Context, subnetID ids.ID, nodeIDs []ids.NodeID, options ...rpc.Option) ([]ClientPermissionlessValidator, error)
// GetCurrentSupply returns an upper bound on the supply of AVAX in the system along with the P-chain height
GetCurrentSupply(ctx context.Context, subnetID ids.ID, options ...rpc.Option) (uint64, uint64, error)
// SampleValidators returns the nodeIDs of a sample of [sampleSize] validators from the current validator set for subnet with ID [subnetID]
SampleValidators(ctx context.Context, subnetID ids.ID, sampleSize uint16, options ...rpc.Option) ([]ids.NodeID, error)
// GetBlockchainStatus returns the current status of blockchain with ID: [blockchainID]
GetBlockchainStatus(ctx context.Context, blockchainID string, options ...rpc.Option) (status.BlockchainStatus, error)
// ValidatedBy returns the ID of the Subnet that validates [blockchainID]
ValidatedBy(ctx context.Context, blockchainID ids.ID, options ...rpc.Option) (ids.ID, error)
// Validates returns the list of blockchains that are validated by the subnet with ID [subnetID]
Validates(ctx context.Context, subnetID ids.ID, options ...rpc.Option) ([]ids.ID, error)
// GetBlockchains returns the list of blockchains on the platform
//
// Deprecated: Blockchains should be fetched from a dedicated indexer.
GetBlockchains(ctx context.Context, options ...rpc.Option) ([]APIBlockchain, error)
// IssueTx issues the transaction and returns its txID
IssueTx(ctx context.Context, tx []byte, options ...rpc.Option) (ids.ID, error)
// GetTx returns the byte representation of the transaction corresponding to [txID]
GetTx(ctx context.Context, txID ids.ID, options ...rpc.Option) ([]byte, error)
// GetTxStatus returns the status of the transaction corresponding to [txID]
GetTxStatus(ctx context.Context, txID ids.ID, options ...rpc.Option) (*GetTxStatusResponse, error)
// AwaitTxDecided polls [GetTxStatus] until a status is returned that
// implies the tx may be decided.
// TODO: Move this function off of the Client interface into a utility
// function.
AwaitTxDecided(
ctx context.Context,
txID ids.ID,
freq time.Duration,
options ...rpc.Option,
) (*GetTxStatusResponse, error)
// GetStake returns the amount of nAVAX that [addrs] have cumulatively
// staked on the Primary Network.
//
// Deprecated: Stake should be calculated using GetTx and GetCurrentValidators.
GetStake(
ctx context.Context,
addrs []ids.ShortID,
validatorsOnly bool,
options ...rpc.Option,
) (map[ids.ID]uint64, [][]byte, error)
// GetMinStake returns the minimum staking amount in nAVAX for validators
// and delegators respectively
GetMinStake(ctx context.Context, subnetID ids.ID, options ...rpc.Option) (uint64, uint64, error)
// GetTotalStake returns the total amount (in nAVAX) staked on the network
GetTotalStake(ctx context.Context, subnetID ids.ID, options ...rpc.Option) (uint64, error)
// GetRewardUTXOs returns the reward UTXOs for a transaction
//
// Deprecated: GetRewardUTXOs should be fetched from a dedicated indexer.
GetRewardUTXOs(context.Context, *api.GetTxArgs, ...rpc.Option) ([][]byte, error)
// GetTimestamp returns the current chain timestamp
GetTimestamp(ctx context.Context, options ...rpc.Option) (time.Time, error)
// GetValidatorsAt returns the weights of the validator set of a provided
// subnet at the specified height.
GetValidatorsAt(
ctx context.Context,
subnetID ids.ID,
height uint64,
options ...rpc.Option,
) (map[ids.NodeID]*validators.GetValidatorOutput, error)
// GetBlock returns the block with the given id.
GetBlock(ctx context.Context, blockID ids.ID, options ...rpc.Option) ([]byte, error)
// GetBlockByHeight returns the block at the given [height].
GetBlockByHeight(ctx context.Context, height uint64, options ...rpc.Option) ([]byte, error)
}
// Client implementation for interacting with the P Chain endpoint
type client struct {
requester rpc.EndpointRequester
}
// NewClient returns a Client for interacting with the P Chain endpoint
func NewClient(uri string) Client {
return &client{requester: rpc.NewEndpointRequester(
uri + "/ext/P",
)}
}
func (c *client) GetHeight(ctx context.Context, options ...rpc.Option) (uint64, error) {
res := &api.GetHeightResponse{}
err := c.requester.SendRequest(ctx, "platform.getHeight", struct{}{}, res, options...)
return uint64(res.Height), err
}
func (c *client) ExportKey(ctx context.Context, user api.UserPass, address ids.ShortID, options ...rpc.Option) (*secp256k1.PrivateKey, error) {
res := &ExportKeyReply{}
err := c.requester.SendRequest(ctx, "platform.exportKey", &ExportKeyArgs{
UserPass: user,
Address: address.String(),
}, res, options...)
return res.PrivateKey, err
}
func (c *client) GetBalance(ctx context.Context, addrs []ids.ShortID, options ...rpc.Option) (*GetBalanceResponse, error) {
res := &GetBalanceResponse{}
err := c.requester.SendRequest(ctx, "platform.getBalance", &GetBalanceRequest{
Addresses: ids.ShortIDsToStrings(addrs),
}, res, options...)
return res, err
}
func (c *client) ListAddresses(ctx context.Context, user api.UserPass, options ...rpc.Option) ([]ids.ShortID, error) {
res := &api.JSONAddresses{}
err := c.requester.SendRequest(ctx, "platform.listAddresses", &user, res, options...)
if err != nil {
return nil, err
}
return address.ParseToIDs(res.Addresses)
}
func (c *client) GetUTXOs(
ctx context.Context,
addrs []ids.ShortID,
limit uint32,
startAddress ids.ShortID,
startUTXOID ids.ID,
options ...rpc.Option,
) ([][]byte, ids.ShortID, ids.ID, error) {
return c.GetAtomicUTXOs(ctx, addrs, "", limit, startAddress, startUTXOID, options...)
}
func (c *client) GetAtomicUTXOs(
ctx context.Context,
addrs []ids.ShortID,
sourceChain string,
limit uint32,
startAddress ids.ShortID,
startUTXOID ids.ID,
options ...rpc.Option,
) ([][]byte, ids.ShortID, ids.ID, error) {
res := &api.GetUTXOsReply{}
err := c.requester.SendRequest(ctx, "platform.getUTXOs", &api.GetUTXOsArgs{
Addresses: ids.ShortIDsToStrings(addrs),
SourceChain: sourceChain,
Limit: json.Uint32(limit),
StartIndex: api.Index{
Address: startAddress.String(),
UTXO: startUTXOID.String(),
},
Encoding: formatting.Hex,
}, res, options...)
if err != nil {
return nil, ids.ShortID{}, ids.Empty, err
}
utxos := make([][]byte, len(res.UTXOs))
for i, utxo := range res.UTXOs {
utxoBytes, err := formatting.Decode(res.Encoding, utxo)
if err != nil {
return nil, ids.ShortID{}, ids.Empty, err
}
utxos[i] = utxoBytes
}
endAddr, err := address.ParseToID(res.EndIndex.Address)
if err != nil {
return nil, ids.ShortID{}, ids.Empty, err
}
endUTXOID, err := ids.FromString(res.EndIndex.UTXO)
return utxos, endAddr, endUTXOID, err
}
// GetSubnetClientResponse is the response from calling GetSubnet on the client
type GetSubnetClientResponse struct {
// whether it is permissioned or not
IsPermissioned bool
// subnet auth information for a permissioned subnet
ControlKeys []ids.ShortID
Threshold uint32
Locktime uint64
// subnet transformation tx ID for a permissionless subnet
SubnetTransformationTxID ids.ID
}
func (c *client) GetSubnet(ctx context.Context, subnetID ids.ID, options ...rpc.Option) (GetSubnetClientResponse, error) {
res := &GetSubnetResponse{}
err := c.requester.SendRequest(ctx, "platform.getSubnet", &GetSubnetArgs{
SubnetID: subnetID,
}, res, options...)
if err != nil {
return GetSubnetClientResponse{}, err
}
controlKeys, err := address.ParseToIDs(res.ControlKeys)
if err != nil {
return GetSubnetClientResponse{}, err
}
return GetSubnetClientResponse{
IsPermissioned: res.IsPermissioned,
ControlKeys: controlKeys,
Threshold: uint32(res.Threshold),
Locktime: uint64(res.Locktime),
SubnetTransformationTxID: res.SubnetTransformationTxID,
}, nil
}
// ClientSubnet is a representation of a subnet used in client methods
type ClientSubnet struct {
// ID of the subnet
ID ids.ID
// Each element of [ControlKeys] the address of a public key.
// A transaction to add a validator to this subnet requires
// signatures from [Threshold] of these keys to be valid.
ControlKeys []ids.ShortID
Threshold uint32
}
func (c *client) GetSubnets(ctx context.Context, ids []ids.ID, options ...rpc.Option) ([]ClientSubnet, error) {
res := &GetSubnetsResponse{}
err := c.requester.SendRequest(ctx, "platform.getSubnets", &GetSubnetsArgs{
IDs: ids,
}, res, options...)
if err != nil {
return nil, err
}
subnets := make([]ClientSubnet, len(res.Subnets))
for i, apiSubnet := range res.Subnets {
controlKeys, err := address.ParseToIDs(apiSubnet.ControlKeys)
if err != nil {
return nil, err
}
subnets[i] = ClientSubnet{
ID: apiSubnet.ID,
ControlKeys: controlKeys,
Threshold: uint32(apiSubnet.Threshold),
}
}
return subnets, nil
}
func (c *client) GetStakingAssetID(ctx context.Context, subnetID ids.ID, options ...rpc.Option) (ids.ID, error) {
res := &GetStakingAssetIDResponse{}
err := c.requester.SendRequest(ctx, "platform.getStakingAssetID", &GetStakingAssetIDArgs{
SubnetID: subnetID,
}, res, options...)
return res.AssetID, err
}
func (c *client) GetCurrentValidators(
ctx context.Context,
subnetID ids.ID,
nodeIDs []ids.NodeID,
options ...rpc.Option,
) ([]ClientPermissionlessValidator, error) {
res := &GetCurrentValidatorsReply{}
err := c.requester.SendRequest(ctx, "platform.getCurrentValidators", &GetCurrentValidatorsArgs{
SubnetID: subnetID,
NodeIDs: nodeIDs,
}, res, options...)
if err != nil {
return nil, err
}
return getClientPermissionlessValidators(res.Validators)
}
func (c *client) GetCurrentSupply(ctx context.Context, subnetID ids.ID, options ...rpc.Option) (uint64, uint64, error) {
res := &GetCurrentSupplyReply{}
err := c.requester.SendRequest(ctx, "platform.getCurrentSupply", &GetCurrentSupplyArgs{
SubnetID: subnetID,
}, res, options...)
return uint64(res.Supply), uint64(res.Height), err
}
func (c *client) SampleValidators(ctx context.Context, subnetID ids.ID, sampleSize uint16, options ...rpc.Option) ([]ids.NodeID, error) {
res := &SampleValidatorsReply{}
err := c.requester.SendRequest(ctx, "platform.sampleValidators", &SampleValidatorsArgs{
SubnetID: subnetID,
Size: json.Uint16(sampleSize),
}, res, options...)
return res.Validators, err
}
func (c *client) GetBlockchainStatus(ctx context.Context, blockchainID string, options ...rpc.Option) (status.BlockchainStatus, error) {
res := &GetBlockchainStatusReply{}
err := c.requester.SendRequest(ctx, "platform.getBlockchainStatus", &GetBlockchainStatusArgs{
BlockchainID: blockchainID,
}, res, options...)
return res.Status, err
}
func (c *client) ValidatedBy(ctx context.Context, blockchainID ids.ID, options ...rpc.Option) (ids.ID, error) {
res := &ValidatedByResponse{}
err := c.requester.SendRequest(ctx, "platform.validatedBy", &ValidatedByArgs{
BlockchainID: blockchainID,
}, res, options...)
return res.SubnetID, err
}
func (c *client) Validates(ctx context.Context, subnetID ids.ID, options ...rpc.Option) ([]ids.ID, error) {
res := &ValidatesResponse{}
err := c.requester.SendRequest(ctx, "platform.validates", &ValidatesArgs{
SubnetID: subnetID,
}, res, options...)
return res.BlockchainIDs, err
}
func (c *client) GetBlockchains(ctx context.Context, options ...rpc.Option) ([]APIBlockchain, error) {
res := &GetBlockchainsResponse{}
err := c.requester.SendRequest(ctx, "platform.getBlockchains", struct{}{}, res, options...)
return res.Blockchains, err
}
func (c *client) IssueTx(ctx context.Context, txBytes []byte, options ...rpc.Option) (ids.ID, error) {
txStr, err := formatting.Encode(formatting.Hex, txBytes)
if err != nil {
return ids.ID{}, err
}
res := &api.JSONTxID{}
err = c.requester.SendRequest(ctx, "platform.issueTx", &api.FormattedTx{
Tx: txStr,
Encoding: formatting.Hex,
}, res, options...)
return res.TxID, err
}
func (c *client) GetTx(ctx context.Context, txID ids.ID, options ...rpc.Option) ([]byte, error) {
res := &api.FormattedTx{}
err := c.requester.SendRequest(ctx, "platform.getTx", &api.GetTxArgs{
TxID: txID,
Encoding: formatting.Hex,
}, res, options...)
if err != nil {
return nil, err
}
return formatting.Decode(res.Encoding, res.Tx)
}
func (c *client) GetTxStatus(ctx context.Context, txID ids.ID, options ...rpc.Option) (*GetTxStatusResponse, error) {
res := &GetTxStatusResponse{}
err := c.requester.SendRequest(
ctx,
"platform.getTxStatus",
&GetTxStatusArgs{
TxID: txID,
},
res,
options...,
)
return res, err
}
func (c *client) AwaitTxDecided(ctx context.Context, txID ids.ID, freq time.Duration, options ...rpc.Option) (*GetTxStatusResponse, error) {
ticker := time.NewTicker(freq)
defer ticker.Stop()
for {
res, err := c.GetTxStatus(ctx, txID, options...)
if err == nil {
switch res.Status {
case status.Committed, status.Aborted, status.Dropped:
return res, nil
}
}
select {
case <-ticker.C:
case <-ctx.Done():
return nil, ctx.Err()
}
}
}
func (c *client) GetStake(
ctx context.Context,
addrs []ids.ShortID,
validatorsOnly bool,
options ...rpc.Option,
) (map[ids.ID]uint64, [][]byte, error) {
res := &GetStakeReply{}
err := c.requester.SendRequest(ctx, "platform.getStake", &GetStakeArgs{
JSONAddresses: api.JSONAddresses{
Addresses: ids.ShortIDsToStrings(addrs),
},
ValidatorsOnly: validatorsOnly,
Encoding: formatting.Hex,
}, res, options...)
if err != nil {
return nil, nil, err
}
staked := make(map[ids.ID]uint64, len(res.Stakeds))
for assetID, amount := range res.Stakeds {
staked[assetID] = uint64(amount)
}
outputs := make([][]byte, len(res.Outputs))
for i, outputStr := range res.Outputs {
output, err := formatting.Decode(res.Encoding, outputStr)
if err != nil {
return nil, nil, err
}
outputs[i] = output
}
return staked, outputs, err
}
func (c *client) GetMinStake(ctx context.Context, subnetID ids.ID, options ...rpc.Option) (uint64, uint64, error) {
res := &GetMinStakeReply{}
err := c.requester.SendRequest(ctx, "platform.getMinStake", &GetMinStakeArgs{
SubnetID: subnetID,
}, res, options...)
return uint64(res.MinValidatorStake), uint64(res.MinDelegatorStake), err
}
func (c *client) GetTotalStake(ctx context.Context, subnetID ids.ID, options ...rpc.Option) (uint64, error) {
res := &GetTotalStakeReply{}
err := c.requester.SendRequest(ctx, "platform.getTotalStake", &GetTotalStakeArgs{
SubnetID: subnetID,
}, res, options...)
var amount json.Uint64
if subnetID == constants.PrimaryNetworkID {
amount = res.Stake
} else {
amount = res.Weight
}
return uint64(amount), err
}
func (c *client) GetRewardUTXOs(ctx context.Context, args *api.GetTxArgs, options ...rpc.Option) ([][]byte, error) {
res := &GetRewardUTXOsReply{}
err := c.requester.SendRequest(ctx, "platform.getRewardUTXOs", args, res, options...)
if err != nil {
return nil, err
}
utxos := make([][]byte, len(res.UTXOs))
for i, utxoStr := range res.UTXOs {
utxoBytes, err := formatting.Decode(res.Encoding, utxoStr)
if err != nil {
return nil, err
}
utxos[i] = utxoBytes
}
return utxos, err
}
func (c *client) GetTimestamp(ctx context.Context, options ...rpc.Option) (time.Time, error) {
res := &GetTimestampReply{}
err := c.requester.SendRequest(ctx, "platform.getTimestamp", struct{}{}, res, options...)
return res.Timestamp, err
}
func (c *client) GetValidatorsAt(
ctx context.Context,
subnetID ids.ID,
height uint64,
options ...rpc.Option,
) (map[ids.NodeID]*validators.GetValidatorOutput, error) {
res := &GetValidatorsAtReply{}
err := c.requester.SendRequest(ctx, "platform.getValidatorsAt", &GetValidatorsAtArgs{
SubnetID: subnetID,
Height: json.Uint64(height),
}, res, options...)
return res.Validators, err
}
func (c *client) GetBlock(ctx context.Context, blockID ids.ID, options ...rpc.Option) ([]byte, error) {
res := &api.FormattedBlock{}
if err := c.requester.SendRequest(ctx, "platform.getBlock", &api.GetBlockArgs{
BlockID: blockID,
Encoding: formatting.Hex,
}, res, options...); err != nil {
return nil, err
}
return formatting.Decode(res.Encoding, res.Block)
}
func (c *client) GetBlockByHeight(ctx context.Context, height uint64, options ...rpc.Option) ([]byte, error) {
res := &api.FormattedBlock{}
err := c.requester.SendRequest(ctx, "platform.getBlockByHeight", &api.GetBlockByHeightArgs{
Height: json.Uint64(height),
Encoding: formatting.HexNC,
}, res, options...)
if err != nil {
return nil, err
}
return formatting.Decode(res.Encoding, res.Block)
}