-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.go
604 lines (520 loc) · 15.5 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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
package chaintester
import (
"context"
"encoding/binary"
"encoding/hex"
"encoding/json"
"fmt"
"os"
"reflect"
"github.com/uuosio/chaintester/interfaces"
"github.com/apache/thrift/lib/go/thrift"
)
type ActionArguments interfaces.ActionArguments
type IPCClient struct {
seqId int32
iprot, oprot thrift.TProtocol
}
// IPCClient implements TClient, and uses the standard message format for Thrift.
// It is not safe for concurrent use.
func NewIPCClient(inputProtocol, outputProtocol thrift.TProtocol) *IPCClient {
return &IPCClient{
iprot: inputProtocol,
oprot: outputProtocol,
}
}
func (p *IPCClient) Send(ctx context.Context, oprot thrift.TProtocol, seqId int32, method string, args thrift.TStruct) error {
// Set headers from context object on THeaderProtocol
if headerProt, ok := oprot.(*thrift.THeaderProtocol); ok {
headerProt.ClearWriteHeaders()
for _, key := range thrift.GetWriteHeaderList(ctx) {
if value, ok := thrift.GetHeader(ctx, key); ok {
headerProt.SetWriteHeader(key, value)
}
}
}
if err := oprot.WriteMessageBegin(ctx, method, thrift.CALL, seqId); err != nil {
return err
}
if err := args.Write(ctx, oprot); err != nil {
return err
}
if err := oprot.WriteMessageEnd(ctx); err != nil {
return err
}
return oprot.Flush(ctx)
}
func (p *IPCClient) Recv(ctx context.Context, iprot thrift.TProtocol, seqId int32, method string, result thrift.TStruct) error {
rMethod, rTypeId, rSeqId, err := iprot.ReadMessageBegin(ctx)
if err != nil {
return err
}
if method != rMethod {
return thrift.NewTApplicationException(thrift.WRONG_METHOD_NAME, fmt.Sprintf("%s: wrong method name", method))
} else if seqId != rSeqId {
return thrift.NewTApplicationException(thrift.BAD_SEQUENCE_ID, fmt.Sprintf("%s: out of order sequence response", method))
} else if rTypeId == thrift.EXCEPTION {
exception := thrift.NewTApplicationException(0, "")
if err := exception.Read(ctx, iprot); err != nil {
return err
}
if err := iprot.ReadMessageEnd(ctx); err != nil {
return err
}
return exception
} else if rTypeId != thrift.REPLY {
return thrift.NewTApplicationException(thrift.INVALID_MESSAGE_TYPE_EXCEPTION, fmt.Sprintf("%s: invalid message type", method))
}
if err := result.Read(ctx, iprot); err != nil {
return err
}
return iprot.ReadMessageEnd(ctx)
}
func (p *IPCClient) Call(ctx context.Context, method string, args, result thrift.TStruct) (thrift.ResponseMeta, error) {
p.seqId++
seqId := p.seqId
if err := p.Send(ctx, p.oprot, seqId, method, args); err != nil {
return thrift.ResponseMeta{}, err
}
// method is oneway
if result == nil {
return thrift.ResponseMeta{}, nil
}
err := p.Recv(ctx, p.iprot, seqId, method, result)
var headers thrift.THeaderMap
if hp, ok := p.iprot.(*thrift.THeaderProtocol); ok {
transport := reflect.ValueOf(hp).Elem().FieldByName("transport")
readHeaders := reflect.ValueOf(transport).Elem().FieldByName("readHeaders")
headers = readHeaders.Interface().(thrift.THeaderMap)
}
return thrift.ResponseMeta{
Headers: headers,
}, err
}
var defaultCtx = context.Background()
type ChainTester struct {
interfaces.IPCChainTesterClient
client *IPCClient
id int32
}
var g_ApplyRequestServer *ApplyRequestServer
func GetApplyRequestServer() *ApplyRequestServer {
if g_ApplyRequestServer == nil {
g_ApplyRequestServer = NewApplyRequestServer()
g_ApplyRequestServer.AcceptOnce()
}
return g_ApplyRequestServer
}
var g_IPCClient *IPCClient = nil
type DebuggerConfig struct {
DebuggerServerAddress string
DebuggerServerPort string
VMAPIServerAddress string
VMAPIServerPort string
ApplyRequestServerAddress string
ApplyRequestServerPort string
}
var g_DebuggerConfig = DebuggerConfig{
DebuggerServerAddress: "127.0.0.1",
DebuggerServerPort: "9090",
VMAPIServerAddress: "127.0.0.1",
VMAPIServerPort: "9092",
ApplyRequestServerAddress: "127.0.0.1",
ApplyRequestServerPort: "9091",
}
func GetDebuggerConfig() *DebuggerConfig {
return &g_DebuggerConfig
}
func SetDebuggerServerAddress(addr string) {
g_DebuggerConfig.DebuggerServerAddress = addr
}
func SetDebuggerServerPort(port string) {
g_DebuggerConfig.DebuggerServerPort = port
}
func SetApplyRequestServerAddress(addr string) {
g_DebuggerConfig.ApplyRequestServerAddress = addr
}
func SetApplyRequestServerPort(port string) {
g_DebuggerConfig.ApplyRequestServerPort = port
}
func SetVMAPIServerAddress(addr string) {
g_DebuggerConfig.VMAPIServerAddress = addr
}
func SetVMAPIServerPort(port string) {
g_DebuggerConfig.VMAPIServerPort = port
}
func GetIPCClient() *IPCClient {
if g_IPCClient == nil {
addr := fmt.Sprintf("%s:%s", g_DebuggerConfig.DebuggerServerAddress, g_DebuggerConfig.DebuggerServerPort)
iprot, oprot, err := NewProtocol(addr)
if err != nil {
panic(err)
}
g_IPCClient = NewIPCClient(iprot, oprot)
tester := interfaces.NewIPCChainTesterClient(g_IPCClient)
tester.InitVMAPI(defaultCtx)
InitVMAPI() //init vm api client
tester.InitApplyRequest(defaultCtx)
GetApplyRequestServer() // init apply request server
}
return g_IPCClient
}
// cannot use c (variable of type *IPCClient) as thrift.TClient value in argument to interfaces.NewIPCChainTesterClient: wrong type for method Call (have
// func(ctx context.Context, method string, args github.com/apache/thrift/lib/go/thrift.TStruct, result github.com/apache/thrift/lib/go/thrift.TStruct) (chaintester.ResponseMeta, error), want
// func(ctx context.Context, method string, args github.com/apache/thrift/lib/go/thrift.TStruct, result github.com/apache/thrift/lib/go/thrift.TStruct) (github.com/apache/thrift/lib/go/thrift.ResponseMeta, error))compilerInvalidIfaceAssign
func NewChainTester() *ChainTester {
c := GetIPCClient()
tester := &ChainTester{
IPCChainTesterClient: *interfaces.NewIPCChainTesterClient(c),
client: c,
}
var err error
tester.id, err = tester.NewChain_(defaultCtx, true)
if err != nil {
panic(err)
}
return tester
}
func (p *ChainTester) SetNativeApply(contract string, apply func(uint64, uint64, uint64)) {
if apply == nil {
p.EnableDebugContract(contract, false)
if applyMap, ok := g_ChainTesterApplyMap[p.id]; ok {
if _, ok := applyMap[contract]; ok {
delete(applyMap, contract)
}
}
} else {
if _, ok := g_ChainTesterApplyMap[p.id]; !ok {
g_ChainTesterApplyMap[p.id] = make(map[string]func(uint64, uint64, uint64))
}
p.EnableDebugContract(contract, true)
g_ChainTesterApplyMap[p.id][contract] = apply
}
}
func (p *ChainTester) Call(ctx context.Context, method string, args, result thrift.TStruct) (thrift.ResponseMeta, error) {
p.client.seqId++
seqId := p.client.seqId
if err := p.client.Send(ctx, p.client.oprot, seqId, method, args); err != nil {
return thrift.ResponseMeta{}, err
}
// method is oneway
if result == nil {
return thrift.ResponseMeta{}, nil
}
//runApplyRequestServer
//start apply request server
if "push_action" == method || "push_actions" == method || "produce_block" == method {
GetApplyRequestServer().Serve()
}
err := p.client.Recv(ctx, p.client.iprot, seqId, method, result)
var headers thrift.THeaderMap
if hp, ok := p.client.iprot.(*thrift.THeaderProtocol); ok {
transport := reflect.ValueOf(hp).Elem().FieldByName("transport")
readHeaders := reflect.ValueOf(transport).Elem().FieldByName("readHeaders")
headers = readHeaders.Interface().(thrift.THeaderMap)
}
return thrift.ResponseMeta{
Headers: headers,
}, err
}
func (p *ChainTester) pushAction(account string, action string, arguments *interfaces.ActionArguments, permissions string) (*JsonValue, error) {
var _args20 interfaces.IPCChainTesterPushActionArgs
_args20.ID = p.id
_args20.Account = account
_args20.Action = action
_args20.Arguments = arguments
_args20.Permissions = permissions
var _result22 interfaces.IPCChainTesterPushActionResult
var _meta21 thrift.ResponseMeta
var _err error
_meta21, _err = p.Call(defaultCtx, "push_action", &_args20, &_result22)
if _err != nil {
panic(_err)
}
p.IPCChainTesterClient.SetLastResponseMeta_(_meta21)
ret := _result22.GetSuccess()
value := &JsonValue{}
// fmt.Printf("++++++push_action return: %v", string(ret))
err := json.Unmarshal(ret, value)
if err != nil {
return nil, err
}
_, err = value.Get("except")
if err == nil {
return nil, NewTransactionError(ret)
} else {
return value, nil
}
}
func (p *ChainTester) PushAction(account string, action string, arguments interface{}, permissions string) (*JsonValue, error) {
_arguments := interfaces.NewActionArguments()
switch args := arguments.(type) {
case string:
_arguments.JSONArgs_ = &args
case []byte:
_arguments.RawArgs_ = args
default:
panic("Invalid arguments type")
}
return p.pushAction(account, action, _arguments, permissions)
}
func (p *ChainTester) PushActions(actions []*interfaces.Action) (*JsonValue, error) {
var _args29 interfaces.IPCChainTesterPushActionsArgs
_args29.ID = p.id
_args29.Actions = actions
var _result31 interfaces.IPCChainTesterPushActionsResult
var _meta30 thrift.ResponseMeta
var _err error
_meta30, _err = p.Call(defaultCtx, "push_actions", &_args29, &_result31)
p.SetLastResponseMeta_(_meta30)
if _err != nil {
return nil, _err
}
ret := _result31.GetSuccess()
value := &JsonValue{}
// fmt.Printf("++++++push_action return: %v", string(ret))
err := json.Unmarshal(ret, value)
if err != nil {
return nil, err
}
_, err = value.Get("except")
if err == nil {
return nil, NewTransactionError(ret)
} else {
return value, nil
}
}
func (p *ChainTester) DeployContract(account string, wasmFile string, abiFile string) (err error) {
wasm, err := os.ReadFile(wasmFile)
if err != nil {
return err
}
hexWasm := make([]byte, len(wasm)*2)
hex.Encode(hexWasm, wasm)
jsonArgs := fmt.Sprintf(`
{
"account": "%s",
"vmtype": 0,
"vmversion": 0,
"code": "%s"
}
`,
account,
string(hexWasm))
permissions := fmt.Sprintf(`
{
"%s": "active"
}
`, account)
setCodeArgs := interfaces.NewActionArguments()
setCodeArgs.JSONArgs_ = &jsonArgs
setCodeAction := &interfaces.Action{
Account: "eosio",
Action: "setcode",
Arguments: setCodeArgs,
Permissions: permissions,
}
actions := make([]*interfaces.Action, 0, 2)
actions = append(actions, setCodeAction)
if abiFile != "" {
abi, err := os.ReadFile(abiFile)
if err != nil {
return err
}
rawAbi, _ := p.PackAbi(string(abi))
hexRawAbi := make([]byte, len(rawAbi)*2)
hex.Encode(hexRawAbi, rawAbi)
jsonArgs := fmt.Sprintf(
`
{
"account": "%s",
"abi": "%s"
}
`,
account,
string(hexRawAbi),
)
setAbiArgs := interfaces.NewActionArguments()
setAbiArgs.JSONArgs_ = &jsonArgs
setAbiAction := &interfaces.Action{
Account: "eosio",
Action: "setabi",
Arguments: setAbiArgs,
Permissions: permissions,
}
actions = append(actions, setAbiAction)
}
_, err = p.PushActions(actions)
if err != nil {
return err
}
return nil
}
func (p *ChainTester) produceBlock(next_block_skip_seconds int64) error {
var _args41 interfaces.IPCChainTesterProduceBlockArgs
_args41.ID = p.id
_args41.NextBlockSkipSeconds = next_block_skip_seconds
var _result43 interfaces.IPCChainTesterProduceBlockResult
var _meta42 thrift.ResponseMeta
var _err error
_meta42, _err = p.Call(defaultCtx, "produce_block", &_args41, &_result43)
p.IPCChainTesterClient.SetLastResponseMeta_(_meta42)
if _err != nil {
return _err
}
return nil
}
func (p *ChainTester) ProduceBlock(next_block_skip_time ...int64) error {
if len(next_block_skip_time) == 0 {
return p.produceBlock(0)
}
if len(next_block_skip_time) != 1 {
panic("invalid arguments")
}
return p.produceBlock(next_block_skip_time[0])
}
func (p *ChainTester) GetInfo() (*JsonValue, error) {
ret, err := p.IPCChainTesterClient.GetInfo(defaultCtx, p.id)
value := &JsonValue{}
err = json.Unmarshal([]byte(ret), value)
if err != nil {
return nil, fmt.Errorf("%v", string(ret))
}
return value, nil
}
func (p *ChainTester) CreateKey(keyType ...string) (*JsonValue, error) {
value := &JsonValue{}
if len(keyType) == 0 {
ret, err := p.IPCChainTesterClient.CreateKey(defaultCtx, "K1")
err = json.Unmarshal([]byte(ret), value)
if err != nil {
return nil, fmt.Errorf("%v", string(ret))
}
} else {
if len(keyType) != 1 {
panic("Invalid keyType")
}
ret, err := p.IPCChainTesterClient.CreateKey(defaultCtx, keyType[0])
err = json.Unmarshal([]byte(ret), value)
if err != nil {
return nil, fmt.Errorf("%v", string(ret))
}
}
return value, nil
}
func (p *ChainTester) GetAccount(account string) (*JsonValue, error) {
ret, err := p.IPCChainTesterClient.GetAccount(defaultCtx, p.id, account)
value := &JsonValue{}
err = json.Unmarshal([]byte(ret), value)
if err != nil {
return nil, fmt.Errorf("%v", string(ret))
}
return value, nil
}
func (p *ChainTester) CreateAccount(creator string, account string, owner_key string, active_key string, ram_bytes int64, res ...int64) (*JsonValue, error) {
stake_net := int64(0)
stake_cpu := int64(0)
if len(res) != 0 {
if len(res) != 2 {
panic("invalid argugments")
}
stake_net = res[0]
stake_cpu = res[1]
}
ret, err := p.IPCChainTesterClient.CreateAccount(defaultCtx, p.id, creator, account, owner_key, active_key, ram_bytes, stake_net, stake_cpu)
value := &JsonValue{}
err = json.Unmarshal([]byte(ret), value)
if err != nil {
return nil, fmt.Errorf("%v", string(ret))
}
return value, nil
}
func (p *ChainTester) GetTableRows(_json bool, code string, scope string, table string, lower_bound string, upper_bound string, limit int64) (*JsonValue, error) {
return p.GetTableRowsEx(
_json,
code,
scope,
table,
lower_bound,
upper_bound,
limit,
"",
"",
"",
false,
false)
}
func (p *ChainTester) GetTableRowsEx(_json bool, code string, scope string, table string, lower_bound string, upper_bound string, limit int64, key_type string, index_position string, encode_type string, reverse bool, show_payer bool) (*JsonValue, error) {
ret, err := p.IPCChainTesterClient.GetTableRows(defaultCtx,
p.id,
_json,
code,
scope,
table,
lower_bound,
upper_bound,
limit,
key_type,
index_position,
encode_type,
reverse,
show_payer)
value := &JsonValue{}
err = json.Unmarshal([]byte(ret), value)
if err != nil {
return nil, fmt.Errorf("%v", string(ret))
}
return value, nil
}
func (tester *ChainTester) GetBalance(account string, extras ...string) uint64 {
tokenAccount := "eosio.token"
symbol := "EOS"
if len(extras) == 1 {
tokenAccount = extras[0]
}
if len(extras) == 2 {
symbol = extras[1]
}
rows, err := tester.GetTableRows(false, tokenAccount, account, "accounts", symbol, "", 1)
if err != nil {
panic(err)
}
hexString, err := rows.GetString("rows", 0)
if err != nil {
panic(err)
}
rawBalance, err := hex.DecodeString(hexString)
if err != nil {
panic(err)
}
return binary.LittleEndian.Uint64(rawBalance[:8])
}
func (p *ChainTester) EnableDebugContract(contract string, enable bool) error {
err := p.IPCChainTesterClient.EnableDebugContract(defaultCtx, p.id, contract, enable)
return err
}
func (p *ChainTester) PackAbi(abi string) ([]byte, error) {
return p.IPCChainTesterClient.PackAbi(defaultCtx, abi)
}
func (p *ChainTester) FreeChain() (int32, error) {
delete(g_ChainTesterApplyMap, p.id)
return p.IPCChainTesterClient.FreeChain(defaultCtx, p.id)
}
func handleClient(client *ChainTester) (err error) {
args := `
{
"name": "go"
}
`
permissions := `
{
"hello": "active"
}
`
// id, err := client.NewChain_(defaultCtx)
// if err != nil {
// return err
// }
client.PushAction("hello", "sayhello", args, permissions)
return nil
}