/
utils.go
189 lines (155 loc) · 4.53 KB
/
utils.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
package tests
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"reflect"
"testing"
"time"
"github.com/ethereum/go-ethereum/common"
ethcmn "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
gorpc "github.com/ethereum/go-ethereum/rpc"
"github.com/stretchr/testify/require"
)
var (
contextType = reflect.TypeOf((*context.Context)(nil)).Elem()
errorType = reflect.TypeOf((*error)(nil)).Elem()
subscriptionType = reflect.TypeOf(gorpc.Subscription{})
stringType = reflect.TypeOf("")
)
type Request struct {
Version string `json:"jsonrpc"`
Method string `json:"method"`
Params interface{} `json:"params"`
ID int `json:"id"`
}
type RPCError struct {
Code int `json:"code"`
Message string `json:"message"`
Data interface{} `json:"data,omitempty"`
}
type Response struct {
Error *RPCError `json:"error"`
ID int `json:"id"`
Result json.RawMessage `json:"result,omitempty"`
}
func GetAddress(netAddr string) ([]byte, error) {
rpcRes, err := CallWithError(netAddr, "eth_accounts", []string{})
if err != nil {
return nil, err
}
var res []hexutil.Bytes
err = json.Unmarshal(rpcRes.Result, &res)
if err != nil {
return nil, err
}
return res[0], nil
}
func CreateRequest(method string, params interface{}) Request {
return Request{
Version: "2.0",
Method: method,
Params: params,
ID: 1,
}
}
type callback struct {
fn reflect.Value // the function
rcvr reflect.Value // receiver object of method, set if fn is method
argTypes []reflect.Type // input argument types
hasCtx bool // method's first argument is a context (not included in argTypes)
errPos int // err return idx, of -1 when method cannot return error
isSubscribe bool // true if this is a subscription callback
}
func Call(t *testing.T, addr string, method string, params interface{}) *Response {
req, err := json.Marshal(CreateRequest(method, params))
require.NoError(t, err)
var rpcRes *Response
time.Sleep(1 * time.Second)
/* #nosec */
if addr == "" {
addr = "http://localhost:8030"
}
res, err := http.Post(addr, "application/json", bytes.NewBuffer(req)) //nolint:gosec
require.NoError(t, err)
decoder := json.NewDecoder(res.Body)
rpcRes = new(Response)
err = decoder.Decode(&rpcRes)
require.NoError(t, err)
err = res.Body.Close()
require.NoError(t, err)
require.Nil(t, rpcRes.Error)
return rpcRes
}
func CallWithError(netAddr string, method string, params interface{}) (*Response, error) {
req, err := json.Marshal(CreateRequest(method, params))
if err != nil {
return nil, err
}
var rpcRes *Response
//time.Sleep(1 * time.Second)
/* #nosec */
if netAddr == "" {
netAddr = "http://localhost:8030"
}
res, err := http.Post(netAddr, "application/json", bytes.NewBuffer(req)) //nolint:gosec
if err != nil {
return nil, err
}
decoder := json.NewDecoder(res.Body)
rpcRes = new(Response)
err = decoder.Decode(&rpcRes)
if err != nil {
return nil, err
}
err = res.Body.Close()
if err != nil {
return nil, err
}
if rpcRes.Error != nil {
return nil, fmt.Errorf(rpcRes.Error.Message)
}
return rpcRes, nil
}
//nolint
func GetTransactionReceipt(t *testing.T, addr string, hash ethcmn.Hash) map[string]interface{} {
param := []string{hash.Hex()}
rpcRes := Call(t, addr, "eth_getTransactionReceipt", param)
receipt := make(map[string]interface{})
err := json.Unmarshal(rpcRes.Result, &receipt)
require.NoError(t, err)
return receipt
}
func WaitForReceipt(t *testing.T, netAddr string, hash ethcmn.Hash) map[string]interface{} {
for i := 0; i < 12; i++ {
receipt := GetTransactionReceipt(t, netAddr, hash)
if receipt != nil {
return receipt
}
time.Sleep(time.Second)
}
return nil
}
func GetNonce(t *testing.T, netAddr string, block string, addr string) hexutil.Uint64 {
rpcRes := Call(t, netAddr, "eth_getTransactionCount", []interface{}{addr, block})
var nonce hexutil.Uint64
require.NoError(t, json.Unmarshal(rpcRes.Result, &nonce))
return nonce
}
func UnlockAllAccounts(t *testing.T, netAddr string) {
var accts []common.Address
rpcRes := Call(t, netAddr, "eth_accounts", []map[string]string{})
err := json.Unmarshal(rpcRes.Result, &accts)
require.NoError(t, err)
for _, acct := range accts {
t.Logf("account: %v", acct)
rpcRes = Call(t, netAddr, "personal_unlockAccount", []interface{}{acct, ""})
var unlocked bool
err = json.Unmarshal(rpcRes.Result, &unlocked)
require.NoError(t, err)
require.True(t, unlocked)
}
}