/
common.go
442 lines (369 loc) · 11.7 KB
/
common.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
package eostest
import (
"context"
"encoding/hex"
"fmt"
"math/rand"
"os"
"os/exec"
"strings"
"testing"
"time"
"github.com/dfuse-io/logging"
"github.com/k0kubun/go-ansi"
"github.com/schollz/progressbar/v3"
"github.com/sebastianmontero/eos-go"
"github.com/sebastianmontero/eos-go/ecc"
"github.com/sebastianmontero/eos-go/system"
"go.uber.org/zap"
)
const charset = "abcdefghijklmnopqrstuvwxyz" + "12345"
const creator = "eosio"
const retries = 10
const retrySleep = 2
type ProgressBarInterface interface {
Add(int) error
Clear() error
RenderBlank() error
Reset()
Finish() error
Set(int) error
IsFinished() bool
}
var cmd *exec.Cmd = nil
var zlog *zap.Logger
func init() {
logging.Register("github.com/digital-scarcity/eos-go-test", &zlog)
}
type FakeProgressBar struct {
}
func (r *FakeProgressBar) Add(int) error {
return nil
}
func (r *FakeProgressBar) Clear() error {
return nil
}
func (r *FakeProgressBar) RenderBlank() error {
return nil
}
func (r *FakeProgressBar) Reset() {
}
func (r *FakeProgressBar) Finish() error {
return nil
}
func (r *FakeProgressBar) Set(int) error {
return nil
}
func (r *FakeProgressBar) IsFinished() bool {
return true
}
func (r *FakeProgressBar) render() error {
return nil
}
var seededRand *rand.Rand = rand.New(
rand.NewSource(time.Now().UnixNano()))
func IsInteractive() bool {
return os.Getenv("INTERACTIVE_MODE") != "false"
}
func stringWithCharset(length int, charset string) string {
b := make([]byte, length)
for i := range b {
b[i] = charset[seededRand.Intn(len(charset))]
}
return string(b)
}
func RandAccountName() string {
return stringWithCharset(12, charset)
}
// DefaultKey returns the default EOSIO key
func DefaultKey() string {
return "5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3"
}
func ExecWithRetry(ctx context.Context, api *eos.API, actions []*eos.Action) (string, error) {
return Trx(ctx, api, actions, retries)
}
func isRetryableError(err error) bool {
errMsg := err.Error()
// fmt.Println("Error: ", errMsg)
return strings.Contains(errMsg, "deadline") ||
strings.Contains(errMsg, "connection reset by peer") ||
strings.Contains(errMsg, "Transaction took too long") ||
strings.Contains(errMsg, "exceeded the current CPU usage limit") ||
strings.Contains(errMsg, "ABI serialization time has exceeded")
}
// ExecTrx executes a list of actions
func ExecTrx(ctx context.Context, api *eos.API, actions []*eos.Action) (string, error) {
txOpts := &eos.TxOptions{}
if err := txOpts.FillFromChain(ctx, api); err != nil {
return "error", fmt.Errorf("error filling tx opts: %s", err)
}
tx := eos.NewTransaction(actions, txOpts)
_, packedTx, err := api.SignTransaction(ctx, tx, txOpts.ChainID, eos.CompressionNone)
if err != nil {
return "error", fmt.Errorf("error signing transaction: %s", err)
}
response, err := api.PushTransaction(ctx, packedTx)
if err != nil {
return "error", fmt.Errorf("error pushing transaction: %s", err)
}
trxID := hex.EncodeToString(response.Processed.ID)
return trxID, nil
}
func Trx(ctx context.Context, api *eos.API, actions []*eos.Action, retries int) (string, error) {
response, err := ExecTrx(ctx, api, actions)
if err != nil {
if retries > 0 {
// fmt.Println("Attempt: ", retries)
if isRetryableError(err) {
time.Sleep(time.Duration(retrySleep) * time.Second)
return Trx(ctx, api, actions, retries-1)
}
}
return "", err
}
return response, nil
}
// CreateAccount creates an account and sets the eosio.code permission on active
func CreateAccount(ctx context.Context, api *eos.API, accountName string, publicKey ecc.PublicKey) (eos.AccountName, error) {
acct := eos.AN(accountName)
actions := []*eos.Action{system.NewNewAccount(creator, acct, publicKey)}
_, err := ExecTrx(ctx, api, actions)
if err != nil {
return eos.AccountName(""), fmt.Errorf("error filling tx opts: %s", err)
}
codePermissionActions := []*eos.Action{system.NewUpdateAuth(acct,
"active",
"owner",
eos.Authority{
Threshold: 1,
Keys: []eos.KeyWeight{{
PublicKey: publicKey,
Weight: 1,
}},
Accounts: []eos.PermissionLevelWeight{{
Permission: eos.PermissionLevel{
Actor: acct,
Permission: "eosio.code",
},
Weight: 1,
}},
Waits: []eos.WaitWeight{},
}, "owner")}
_, err = ExecTrx(ctx, api, codePermissionActions)
if err != nil {
return "", fmt.Errorf("error filling tx opts: %s", err)
}
return acct, nil
}
// CreateAccountWithRandomKey specifies a name and uses a random key to create account (adds to Keybag)
func CreateAccountWithRandomKey(ctx context.Context, api *eos.API, accountName string) (ecc.PublicKey, eos.AccountName, error) {
key, _ := ecc.NewRandomPrivateKey()
err := api.Signer.ImportPrivateKey(ctx, key.String())
if err != nil {
return ecc.PublicKey{}, "", fmt.Errorf("error importing key: %s", err)
}
acct, err := CreateAccount(ctx, api, accountName, key.PublicKey())
return key.PublicKey(), acct, err
}
// CreateAccountWithRandomName ...
func CreateAccountWithRandomName(ctx context.Context, api *eos.API, key ecc.PublicKey) (eos.AccountName, error) {
return CreateAccount(ctx, api, RandAccountName(), key)
}
// CreateAccountFromString creates an specific account from a string name
func CreateAccountFromString(ctx context.Context, api *eos.API, accountName, privateKey string) (eos.AccountName, error) {
key, err := ImportKey(ctx, api, privateKey)
if err != nil {
return "", err
}
return CreateAccount(ctx, api, accountName, key.PublicKey())
}
func ImportKey(ctx context.Context, api *eos.API, privateKey string) (*ecc.PrivateKey, error) {
key, err := ecc.NewPrivateKey(privateKey)
if err != nil {
return nil, fmt.Errorf("privateKey parameter is not a valid format: %s", err)
}
keys, err := api.Signer.AvailableKeys(ctx)
if err != nil {
return nil, fmt.Errorf("error getting available key: %s", err)
}
for _, k := range keys {
if k.String() == key.PublicKey().String() {
return key, nil
}
}
err = api.Signer.ImportPrivateKey(ctx, privateKey)
if err != nil {
return nil, fmt.Errorf("error importing key: %s", err)
}
return key, nil
}
// CreateAccountWithRandomNameAndKey ...
func CreateAccountWithRandomNameAndKey(ctx context.Context, api *eos.API) (ecc.PublicKey, eos.AccountName, error) {
return CreateAccountWithRandomKey(ctx, api, RandAccountName())
}
// CreateRandoms returns a list of accounts with eosio.code permission attached to active
func CreateRandomAccountsDefaultKey(ctx context.Context, api *eos.API, length int) ([]eos.AccountName, error) {
accounts := make([]eos.AccountName, length)
i := 0
for i < length {
account, err := CreateAccountFromString(ctx, api, RandAccountName(), DefaultKey())
if err != nil {
return []eos.AccountName{}, fmt.Errorf("cannot create accounts: %s", err)
}
accounts[i] = account
i++
}
return accounts, nil
}
// CreateRandoms returns a list of accounts with eosio.code permission attached to active
func CreateRandoms(ctx context.Context, api *eos.API, length int) ([]eos.AccountName, error) {
accounts := make([]eos.AccountName, length)
i := 0
for i < length {
_, account, err := CreateAccountWithRandomNameAndKey(ctx, api)
if err != nil {
return []eos.AccountName{}, fmt.Errorf("error importing key: %s", err)
}
accounts[i] = account
i++
}
return accounts, nil
}
// SetContract sets the wasm and abi files to the account
func SetContract(ctx context.Context, api *eos.API, accountName eos.AccountName, wasmFile, abiFile string) (string, error) {
setCodeAction, err := system.NewSetCode(accountName, wasmFile)
if err != nil {
return "", fmt.Errorf("unable construct set_code action: %v", err)
}
_, err = Trx(ctx, api, []*eos.Action{setCodeAction}, 3)
if err != nil {
errMsg := err.Error()
// fmt.Println("Error: ", errMsg)
if !strings.Contains(errMsg, "Contract is already running this version of code") {
return "", err
}
}
setAbiAction, err := system.NewSetABI(accountName, abiFile)
if err != nil {
return "", fmt.Errorf("unable construct set_abi action: %v", err)
}
resp, err := Trx(ctx, api, []*eos.Action{setAbiAction}, 3)
if err != nil {
errMsg := err.Error()
// fmt.Println("Error: ", errMsg)
if !strings.Contains(errMsg, "Contract is already running this version of code") {
return "", err
}
}
return resp, nil
}
type tokenCreate struct {
Issuer eos.AccountName
MaxSupply eos.Asset
}
// DeployAndCreateToken deploys the standard token contract and creates the specified token max supply
func DeployAndCreateToken(ctx context.Context, t *testing.T, api *eos.API, tokenHome string,
contract, issuer eos.AccountName, maxSupply eos.Asset) (string, error) {
// TODO: how to save wasm and abi to distribute with package
_, err := SetContract(ctx, api, contract, tokenHome+"/token/token.wasm", tokenHome+"/token/token.abi")
if err != nil {
return "", fmt.Errorf("cannot set contract: %s", err)
}
actions := []*eos.Action{{
Account: contract,
Name: eos.ActN("create"),
Authorization: []eos.PermissionLevel{
{Actor: contract, Permission: eos.PN("active")},
},
ActionData: eos.NewActionData(tokenCreate{
Issuer: issuer,
MaxSupply: maxSupply,
}),
}}
t.Log("Created Token : ", contract, " : ", maxSupply.String())
return ExecTrx(ctx, api, actions)
}
// Pause will pause execution and print a head
func Pause(seconds time.Duration, headline, prefix string) {
if headline != "" {
zlog.Info("Pausing for", zap.Duration("duration", seconds), zap.String("headline", headline))
}
bar := DefaultProgressBar(prefix, 100)
chunk := seconds / 100
for i := 0; i < 100; i++ {
err := bar.Add(1)
if err != nil {
zlog.Error("Cannot increment progress bar", zap.Error(err))
}
time.Sleep(chunk)
}
}
func DefaultProgressBar(prefix string, counter int) ProgressBarInterface {
if !IsInteractive() {
return &FakeProgressBar{}
}
return progressbar.NewOptions(counter,
progressbar.OptionSetWriter(ansi.NewAnsiStdout()),
progressbar.OptionEnableColorCodes(true),
progressbar.OptionSetWidth(90),
// progressbar.OptionShowIts(),
progressbar.OptionSetDescription("[cyan]"+fmt.Sprintf("%20v", prefix)),
progressbar.OptionSetTheme(progressbar.Theme{
Saucer: "[green]=[reset]",
SaucerHead: "[green]>[reset]",
SaucerPadding: " ",
BarStart: "[",
BarEnd: "]",
}))
}
func RestartNodeos(useDefault bool, arg ...string) (*exec.Cmd, error) {
if cmd != nil {
err := cmd.Process.Signal(os.Interrupt)
if err != nil {
fmt.Println("error killing process, err:", err)
}
procState, err := cmd.Process.Wait()
if err != nil {
fmt.Println("error waiting process, err:", err)
}
if !procState.Exited() {
// panic(fmt.Sprintf("Nodeos process, failed to exit correctly, exit code: %v", procState.ExitCode()))
panic("Nodeos process did not exit")
}
// time.Sleep(time.Second * 2)
} else {
// cancel nodeos if it is running
_, err := exec.Command("sh", "-c", "pkill -SIGINT nodeos").Output()
if err == nil {
Pause(time.Second, "Killing nodeos ...", "")
}
}
// start nodeos with some reasonable defaults
if useDefault {
cmd = exec.Command("nodeos", "-e", "-p", "eosio",
"--plugin", "eosio::producer_plugin",
"--plugin", "eosio::producer_api_plugin",
"--plugin", "eosio::chain_api_plugin",
"--plugin", "eosio::http_plugin",
"--access-control-allow-origin", "*",
"--contracts-console",
"--http-validate-host", "false",
"--verbose-http-errors",
"--delete-all-blocks")
} else {
cmd = exec.Command("nodeos", arg...)
}
// outfile, err := os.Create("./nodeos-go.log")
// if err != nil {
// return nil, fmt.Errorf("unable to create nodeos-go.log file: %v", err)
// }
// defer outfile.Close()
// cmd.Stdout = outfile
// cmd.Stderr = outfile
err := cmd.Start()
if err != nil {
return nil, err
}
Pause(time.Second, "", "")
return cmd, nil
}