-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils_test.go
82 lines (69 loc) · 1.98 KB
/
utils_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
package godence
import (
"context"
"fmt"
"testing"
"time"
"github.com/onflow/flow-go-sdk"
flowGrpc "github.com/onflow/flow-go-sdk/access/grpc"
"github.com/onflow/flow-go-sdk/crypto"
"github.com/stretchr/testify/assert"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
// Helper for unsupport type test.
type unsupportType string
var flowCli *flowGrpc.Client
func initFlowClient() {
client, err := flowGrpc.NewClient(
"localhost:3569",
grpc.WithTransportCredentials(insecure.NewCredentials()),
)
if err != nil {
panic(err)
}
err = client.Ping(context.Background())
if err != nil {
panic(err)
}
flowCli = client
fmt.Println("Flow client init success.")
}
func TestMain(m *testing.M) {
// start: Init a flow client.
initFlowClient()
m.Run()
}
// waitForTransactionSealed. Only for test
func waitForTransactionSealed(tx *flow.Transaction, a *assert.Assertions) *flow.TransactionResult {
for {
result, err := flowCli.GetTransactionResult(context.Background(), tx.ID())
a.NoError(err)
if result.Error != nil {
return result
}
if result.Status == flow.TransactionStatusSealed {
return result
}
// sleep for 200ms
time.Sleep(200 * time.Millisecond)
}
}
func buildSimpleTx(script []byte, a *assert.Assertions) *flow.Transaction {
latestBlock, err := flowCli.GetLatestBlock(context.Background(), true)
a.NoError(err)
account, err := flowCli.GetAccount(context.Background(), flow.HexToAddress("0xf8d6e0586b0a20c7"))
a.NoError(err)
tx := flow.NewTransaction().
SetScript([]byte(script)).
SetReferenceBlockID(latestBlock.ID).
SetProposalKey(account.Address, 0, account.Keys[0].SequenceNumber).
SetPayer(account.Address).
SetGasLimit(999)
privateKey, err := crypto.DecodePrivateKeyHex(crypto.ECDSA_P256, "c47db93881bc34a6155192c2bec0d124731e08ff105672afdb09892e3dc9ccae")
a.NoError(err)
signer, err := crypto.NewInMemorySigner(privateKey, crypto.SHA3_256)
a.NoError(err)
tx.SignEnvelope(account.Address, 0, signer)
return tx
}