This repository has been archived by the owner on Apr 4, 2023. It is now read-only.
/
client.go
260 lines (230 loc) · 6.27 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
// Copyright (C) 2019-2021, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
// Package client implements "blobvm" client SDK.
package client
import (
"context"
"fmt"
"time"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils/rpc"
"github.com/ethereum/go-ethereum/common"
"github.com/fatih/color"
"github.com/ava-labs/blobvm/chain"
"github.com/ava-labs/blobvm/tdata"
"github.com/ava-labs/blobvm/vm"
)
// Client defines blobvm client operations.
type Client interface {
// Pings the VM.
Ping(ctx context.Context) (bool, error)
// Network information about this instance of the VM
Network(ctx context.Context) (uint32, ids.ID, ids.ID, error)
// Returns the VM genesis.
Genesis(ctx context.Context) (*chain.Genesis, error)
// Accepted fetches the ID of the last accepted block.
Accepted(ctx context.Context) (ids.ID, error)
// Balance returns the balance of an account
Balance(ctx context.Context, addr common.Address) (bal uint64, err error)
// Resolve returns the value associated with a path
Resolve(ctx context.Context, key common.Hash) (exists bool, value []byte, valueMeta *chain.ValueMeta, err error)
// Requests the suggested price and cost from VM.
SuggestedRawFee(ctx context.Context) (uint64, uint64, error)
// Issues the transaction and returns the transaction ID.
IssueRawTx(ctx context.Context, d []byte) (ids.ID, error)
// Requests the suggested price and cost from VM, returns the input as
// TypedData.
SuggestedFee(ctx context.Context, i *chain.Input) (*tdata.TypedData, uint64, error)
// Issues a human-readable transaction and returns the transaction ID.
IssueTx(ctx context.Context, td *tdata.TypedData, sig []byte) (ids.ID, error)
// Checks the status of the transaction, and returns "true" if confirmed.
HasTx(ctx context.Context, id ids.ID) (bool, error)
// Polls the transactions until its status is confirmed.
PollTx(ctx context.Context, txID ids.ID) (confirmed bool, err error)
// Recent actions on the network (sorted from recent to oldest)
RecentActivity(ctx context.Context) ([]*chain.Activity, error)
}
// New creates a new client object.
func New(uri string, reqTimeout time.Duration) Client {
req := rpc.NewEndpointRequester(
fmt.Sprintf("%s%s", uri, vm.PublicEndpoint),
)
return &client{req: req}
}
type client struct {
req rpc.EndpointRequester
}
func (cli *client) Ping(ctx context.Context) (bool, error) {
resp := new(vm.PingReply)
err := cli.req.SendRequest(ctx,
"blobvm.ping",
nil,
resp,
)
if err != nil {
return false, err
}
return resp.Success, nil
}
func (cli *client) Network(ctx context.Context) (uint32, ids.ID, ids.ID, error) {
resp := new(vm.NetworkReply)
err := cli.req.SendRequest(
ctx,
"blobvm.network",
nil,
resp,
)
if err != nil {
return 0, ids.Empty, ids.Empty, err
}
return resp.NetworkID, resp.SubnetID, resp.ChainID, nil
}
func (cli *client) Genesis(ctx context.Context) (*chain.Genesis, error) {
resp := new(vm.GenesisReply)
err := cli.req.SendRequest(
ctx,
"blobvm.genesis",
nil,
resp,
)
return resp.Genesis, err
}
func (cli *client) Accepted(ctx context.Context) (ids.ID, error) {
resp := new(vm.LastAcceptedReply)
if err := cli.req.SendRequest(
ctx,
"blobvm.lastAccepted",
nil,
resp,
); err != nil {
color.Red("failed to get curr block %v", err)
return ids.ID{}, err
}
return resp.BlockID, nil
}
func (cli *client) SuggestedRawFee(ctx context.Context) (uint64, uint64, error) {
resp := new(vm.SuggestedRawFeeReply)
if err := cli.req.SendRequest(
ctx,
"blobvm.suggestedRawFee",
nil,
resp,
); err != nil {
return 0, 0, err
}
return resp.Price, resp.Cost, nil
}
func (cli *client) IssueRawTx(ctx context.Context, d []byte) (ids.ID, error) {
resp := new(vm.IssueRawTxReply)
if err := cli.req.SendRequest(
ctx,
"blobvm.issueRawTx",
&vm.IssueRawTxArgs{Tx: d},
resp,
); err != nil {
return ids.Empty, err
}
return resp.TxID, nil
}
func (cli *client) HasTx(ctx context.Context, txID ids.ID) (bool, error) {
resp := new(vm.HasTxReply)
if err := cli.req.SendRequest(
ctx,
"blobvm.hasTx",
&vm.HasTxArgs{TxID: txID},
resp,
); err != nil {
return false, err
}
return resp.Accepted, nil
}
func (cli *client) SuggestedFee(ctx context.Context, i *chain.Input) (*tdata.TypedData, uint64, error) {
resp := new(vm.SuggestedFeeReply)
if err := cli.req.SendRequest(
ctx,
"blobvm.suggestedFee",
&vm.SuggestedFeeArgs{Input: i},
resp,
); err != nil {
return nil, 0, err
}
return resp.TypedData, resp.TotalCost, nil
}
func (cli *client) IssueTx(ctx context.Context, td *tdata.TypedData, sig []byte) (ids.ID, error) {
resp := new(vm.IssueTxReply)
if err := cli.req.SendRequest(
ctx,
"blobvm.issueTx",
&vm.IssueTxArgs{TypedData: td, Signature: sig},
resp,
); err != nil {
return ids.Empty, err
}
return resp.TxID, nil
}
func (cli *client) PollTx(ctx context.Context, txID ids.ID) (confirmed bool, err error) {
done:
for ctx.Err() == nil {
select {
case <-time.After(time.Second):
case <-ctx.Done():
break done
}
confirmed, err := cli.HasTx(ctx, txID)
if err != nil {
color.Red("polling transaction failed %v", err)
continue
}
if confirmed {
color.Green("confirmed transaction %v", txID)
return true, nil
}
}
return false, ctx.Err()
}
func (cli *client) Resolve(ctx context.Context, key common.Hash) (bool, []byte, *chain.ValueMeta, error) {
resp := new(vm.ResolveReply)
if err := cli.req.SendRequest(
ctx,
"blobvm.resolve",
&vm.ResolveArgs{
Key: key,
},
resp,
); err != nil {
return false, nil, nil, err
}
if !resp.Exists {
return false, nil, nil, nil
}
if key != chain.ValueHash(resp.Value) {
return false, nil, nil, ErrIntegrityFailure
}
return true, resp.Value, resp.ValueMeta, nil
}
func (cli *client) Balance(ctx context.Context, addr common.Address) (bal uint64, err error) {
resp := new(vm.BalanceReply)
if err = cli.req.SendRequest(
ctx,
"blobvm.balance",
&vm.BalanceArgs{
Address: addr,
},
resp,
); err != nil {
return 0, err
}
return resp.Balance, nil
}
func (cli *client) RecentActivity(ctx context.Context) (activity []*chain.Activity, err error) {
resp := new(vm.RecentActivityReply)
if err = cli.req.SendRequest(
ctx,
"blobvm.recentActivity",
nil,
resp,
); err != nil {
return nil, err
}
return resp.Activity, nil
}