-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathagent_test.go
342 lines (303 loc) · 9.4 KB
/
agent_test.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
package agent_test
import (
"encoding/hex"
"encoding/json"
"fmt"
"testing"
"github.com/aviate-labs/agent-go"
"github.com/aviate-labs/agent-go/candid/idl"
"github.com/aviate-labs/agent-go/certification/hashtree"
"github.com/aviate-labs/agent-go/identity"
"github.com/aviate-labs/agent-go/principal"
)
var (
LEDGER_PRINCIPAL = principal.MustDecode("ryjl3-tyaaa-aaaaa-aaaba-cai")
REGISTRY_PRINCIPAL = principal.MustDecode("rwlgt-iiaaa-aaaaa-aaaaa-cai")
)
var _ = new(testLogger)
func Example_anonymous_call() {
a, _ := agent.New(agent.DefaultConfig)
var balance struct {
E8S uint64 `ic:"e8s"`
}
accountID, _ := hex.DecodeString("9523dc824aa062dcd9c91b98f4594ff9c6af661ac96747daef2090b7fe87037d")
err := a.Call(LEDGER_PRINCIPAL, "account_balance", []any{
struct {
Account []byte `ic:"account"`
}{Account: accountID},
}, []any{&balance})
fmt.Println(balance.E8S, err)
// Output:
// 0 <nil>
}
func Example_anonymous_query() {
a, _ := agent.New(agent.DefaultConfig)
var balance struct {
E8S uint64 `ic:"e8s"`
}
accountID, _ := hex.DecodeString("9523dc824aa062dcd9c91b98f4594ff9c6af661ac96747daef2090b7fe87037d")
err := a.Query(LEDGER_PRINCIPAL, "account_balance", []any{
struct {
Account []byte `ic:"account"`
}{Account: accountID},
}, []any{&balance})
fmt.Println(balance.E8S, err)
// Output:
// 0 <nil>
}
func Example_json() {
raw := `{"e8s":1}`
var balance struct {
// Tags can be combined with json tags.
E8S uint64 `ic:"e8s" json:"e8s"`
}
_ = json.Unmarshal([]byte(raw), &balance)
fmt.Println(balance.E8S)
a, _ := agent.New(agent.DefaultConfig)
accountID, _ := hex.DecodeString("9523dc824aa062dcd9c91b98f4594ff9c6af661ac96747daef2090b7fe87037d")
if err := a.Query(LEDGER_PRINCIPAL, "account_balance", []any{struct {
Account []byte `json:"account"`
}{
Account: accountID,
}}, []any{&balance}); err != nil {
fmt.Println(err)
}
rawJSON, _ := json.Marshal(balance)
fmt.Println(string(rawJSON))
// Output:
// 1
// {"e8s":0}
}
func Example_query_ed25519() {
id, _ := identity.NewRandomEd25519Identity()
ledgerID := principal.MustDecode("ryjl3-tyaaa-aaaaa-aaaba-cai")
a, _ := agent.New(agent.Config{Identity: id})
var balance struct {
E8S uint64 `ic:"e8s"`
}
accountID, _ := hex.DecodeString("9523dc824aa062dcd9c91b98f4594ff9c6af661ac96747daef2090b7fe87037d")
_ = a.Query(ledgerID, "account_balance", []any{map[string]any{
"account": accountID,
}}, []any{&balance})
fmt.Println(balance.E8S)
// Output:
// 0
}
func Example_query_prime256v1() {
id, _ := identity.NewRandomPrime256v1Identity()
a, _ := agent.New(agent.Config{Identity: id})
var balance struct {
E8S uint64 `ic:"e8s"`
}
accountID, _ := hex.DecodeString("9523dc824aa062dcd9c91b98f4594ff9c6af661ac96747daef2090b7fe87037d")
_ = a.Query(LEDGER_PRINCIPAL, "account_balance", []any{map[string]any{
"account": accountID,
}}, []any{&balance})
fmt.Println(balance.E8S)
// Output:
// 0
}
func Example_query_secp256k1() {
id, _ := identity.NewRandomSecp256k1Identity()
a, _ := agent.New(agent.Config{Identity: id})
var balance struct {
E8S uint64 `ic:"e8s"`
}
accountID, _ := hex.DecodeString("9523dc824aa062dcd9c91b98f4594ff9c6af661ac96747daef2090b7fe87037d")
_ = a.Query(LEDGER_PRINCIPAL, "account_balance", []any{map[string]any{
"account": accountID,
}}, []any{&balance})
fmt.Println(balance.E8S)
// Output:
// 0
}
func TestAgent_Call(t *testing.T) {
a, err := agent.New(agent.DefaultConfig)
if err != nil {
t.Fatal(err)
}
n, err := a.ReadStateCertificate(REGISTRY_PRINCIPAL, [][]hashtree.Label{{hashtree.Label("subnet")}})
if err != nil {
t.Fatal(err)
}
for _, path := range hashtree.ListPaths(n, nil) {
if len(path) == 3 && string(path[0]) == "subnet" && string(path[2]) == "public_key" {
subnetID := principal.Principal{Raw: []byte(path[1])}
_ = subnetID
}
}
}
func TestAgent_Query_Ed25519(t *testing.T) {
id, err := identity.NewRandomEd25519Identity()
if err != nil {
t.Fatal(err)
}
a, _ := agent.New(agent.Config{
Identity: id,
})
var balance struct {
E8S uint64 `ic:"e8s"`
}
accountID, _ := hex.DecodeString("9523dc824aa062dcd9c91b98f4594ff9c6af661ac96747daef2090b7fe87037d")
if err := a.Query(LEDGER_PRINCIPAL, "account_balance", []any{
struct {
Account []byte `ic:"account"`
}{Account: accountID},
}, []any{&balance}); err != nil {
t.Fatal(err)
}
}
func TestAgent_Query_Secp256k1(t *testing.T) {
id, err := identity.NewRandomSecp256k1Identity()
if err != nil {
t.Fatal(err)
}
a, _ := agent.New(agent.Config{
Identity: id,
})
var balance struct {
E8S uint64 `ic:"e8s"`
}
accountID, _ := hex.DecodeString("9523dc824aa062dcd9c91b98f4594ff9c6af661ac96747daef2090b7fe87037d")
if err := a.Query(LEDGER_PRINCIPAL, "account_balance", []any{
struct {
Account []byte `ic:"account"`
}{Account: accountID},
}, []any{&balance}); err != nil {
t.Fatal(err)
}
}
func TestAgent_Query_callback(t *testing.T) {
a, err := agent.New(agent.DefaultConfig)
if err != nil {
t.Fatal(err)
}
type GetBlocksArgs struct {
Start uint64 `ic:"start" json:"start"`
Length uint64 `ic:"length" json:"length"`
}
type ArchivedBlocksRange struct {
Start uint64 `ic:"start" json:"start"`
Length uint64 `ic:"length" json:"length"`
Callback idl.Function `ic:"callback" json:"callback"`
}
type QueryBlocksResponse struct {
ChainLength uint64 `ic:"chain_length" json:"chain_length"`
Certificate *[]byte `ic:"certificate,omitempty" json:"certificate,omitempty"`
Blocks []any `ic:"blocks" json:"blocks"`
FirstBlockIndex uint64 `ic:"first_block_index" json:"first_block_index"`
ArchivedBlocks []ArchivedBlocksRange `ic:"archived_blocks" json:"archived_blocks"`
}
args := GetBlocksArgs{
Start: 123,
Length: 1,
}
req, err := a.CreateCandidAPIRequest(
agent.RequestTypeQuery,
LEDGER_PRINCIPAL,
"query_blocks",
args,
)
if err != nil {
t.Fatal(err)
}
var out QueryBlocksResponse
if err := req.Query([]any{&out}, false); err != nil {
t.Fatal(err)
}
archive := out.ArchivedBlocks[0]
if archive.Start != 123 || archive.Length != 1 {
t.Error(archive)
}
if !archive.Callback.Method.Principal.Equal(principal.MustDecode("qjdve-lqaaa-aaaaa-aaaeq-cai")) {
t.Error(archive.Callback.Method.Principal)
}
if archive.Callback.Method.Method != "get_blocks" {
t.Error(archive.Callback.Method.Method)
}
type Timestamp struct {
TimestampNanos uint64 `ic:"timestamp_nanos" json:"timestamp_nanos"`
}
type Tokens struct {
E8s uint64 `ic:"e8s" json:"e8s"`
}
type Operation struct {
Mint *struct {
To []byte `ic:"to" json:"to"`
Amount Tokens `ic:"amount" json:"amount"`
} `ic:"Mint,variant"`
Burn *struct {
From []byte `ic:"from" json:"from"`
Spender *[]byte `ic:"spender,omitempty" json:"spender,omitempty"`
Amount Tokens `ic:"amount" json:"amount"`
} `ic:"Burn,variant"`
Transfer *struct {
From []byte `ic:"from" json:"from"`
To []byte `ic:"to" json:"to"`
Amount Tokens `ic:"amount" json:"amount"`
Fee Tokens `ic:"fee" json:"fee"`
Spender *[]uint8 `ic:"spender,omitempty" json:"spender,omitempty"`
} `ic:"Transfer,variant"`
Approve *struct {
From []byte `ic:"from" json:"from"`
Spender []byte `ic:"spender" json:"spender"`
AllowanceE8s idl.Int `ic:"allowance_e8s" json:"allowance_e8s"`
Allowance Tokens `ic:"allowance" json:"allowance"`
Fee Tokens `ic:"fee" json:"fee"`
ExpiresAt *Timestamp `ic:"expires_at,omitempty" json:"expires_at,omitempty"`
ExpectedAllowance *Tokens `ic:"expected_allowance,omitempty" json:"expected_allowance,omitempty"`
} `ic:"Approve,variant"`
}
type Transaction struct {
Memo uint64 `ic:"memo" json:"memo"`
Icrc1Memo *[]byte `ic:"icrc1_memo,omitempty" json:"icrc1_memo,omitempty"`
Operation *Operation `ic:"operation,omitempty" json:"operation,omitempty"`
CreatedAtTime Timestamp `ic:"created_at_time" json:"created_at_time"`
}
type Block struct {
ParentHash *[]byte `ic:"parent_hash,omitempty" json:"parent_hash,omitempty"`
Transaction Transaction `ic:"transaction" json:"transaction"`
Timestamp Timestamp `ic:"timestamp" json:"timestamp"`
}
type BlockRange struct {
Blocks []Block `ic:"blocks" json:"blocks"`
}
type GetBlocksError struct {
BadFirstBlockIndex *struct {
RequestedIndex uint64 `ic:"requested_index" json:"requested_index"`
FirstValidIndex uint64 `ic:"first_valid_index" json:"first_valid_index"`
} `ic:"BadFirstBlockIndex,variant"`
Other *struct {
ErrorCode uint64 `ic:"error_code" json:"error_code"`
ErrorMessage string `ic:"error_message" json:"error_message"`
} `ic:"Other,variant"`
}
type GetBlocksResult struct {
Ok *BlockRange `ic:"Ok,variant"`
Err *GetBlocksError `ic:"Err,variant"`
}
var blocks GetBlocksResult
if err := a.Query(
archive.Callback.Method.Principal,
archive.Callback.Method.Method,
[]any{args},
[]any{&blocks},
); err != nil {
t.Error(err)
}
if len(blocks.Ok.Blocks) != 1 {
t.Error(blocks)
}
}
func TestCall_invalid(t *testing.T) {
a, _ := agent.New(agent.DefaultConfig)
qErr := a.Query(LEDGER_PRINCIPAL, "account_balance", []any{}, []any{})
cErr := a.Call(LEDGER_PRINCIPAL, "account_balance", []any{}, []any{})
if qErr != cErr {
t.Error(qErr, cErr)
}
}
type testLogger struct{}
func (t testLogger) Printf(format string, v ...any) {
fmt.Printf("[TEST]"+format+"\n", v...)
}