github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/app/rpc/tests/utils.go (about) 1 package tests 2 3 import ( 4 "bytes" 5 "context" 6 "encoding/json" 7 "fmt" 8 "net/http" 9 "reflect" 10 "testing" 11 "time" 12 13 "github.com/ethereum/go-ethereum/common" 14 ethcmn "github.com/ethereum/go-ethereum/common" 15 "github.com/ethereum/go-ethereum/common/hexutil" 16 gorpc "github.com/ethereum/go-ethereum/rpc" 17 "github.com/stretchr/testify/require" 18 ) 19 20 var ( 21 contextType = reflect.TypeOf((*context.Context)(nil)).Elem() 22 errorType = reflect.TypeOf((*error)(nil)).Elem() 23 subscriptionType = reflect.TypeOf(gorpc.Subscription{}) 24 stringType = reflect.TypeOf("") 25 ) 26 27 type Request struct { 28 Version string `json:"jsonrpc"` 29 Method string `json:"method"` 30 Params interface{} `json:"params"` 31 ID int `json:"id"` 32 } 33 34 type RPCError struct { 35 Code int `json:"code"` 36 Message string `json:"message"` 37 Data interface{} `json:"data,omitempty"` 38 } 39 40 type Response struct { 41 Error *RPCError `json:"error"` 42 ID int `json:"id"` 43 Result json.RawMessage `json:"result,omitempty"` 44 } 45 46 func GetAddress(netAddr string) ([]byte, error) { 47 rpcRes, err := CallWithError(netAddr, "eth_accounts", []string{}) 48 if err != nil { 49 return nil, err 50 } 51 52 var res []hexutil.Bytes 53 err = json.Unmarshal(rpcRes.Result, &res) 54 if err != nil { 55 return nil, err 56 } 57 58 return res[0], nil 59 } 60 61 func CreateRequest(method string, params interface{}) Request { 62 return Request{ 63 Version: "2.0", 64 Method: method, 65 Params: params, 66 ID: 1, 67 } 68 } 69 70 type callback struct { 71 fn reflect.Value // the function 72 rcvr reflect.Value // receiver object of method, set if fn is method 73 argTypes []reflect.Type // input argument types 74 hasCtx bool // method's first argument is a context (not included in argTypes) 75 errPos int // err return idx, of -1 when method cannot return error 76 isSubscribe bool // true if this is a subscription callback 77 } 78 79 func Call(t *testing.T, addr string, method string, params interface{}) *Response { 80 req, err := json.Marshal(CreateRequest(method, params)) 81 require.NoError(t, err) 82 83 var rpcRes *Response 84 time.Sleep(1 * time.Second) 85 /* #nosec */ 86 87 if addr == "" { 88 addr = "http://localhost:8030" 89 } 90 res, err := http.Post(addr, "application/json", bytes.NewBuffer(req)) //nolint:gosec 91 require.NoError(t, err) 92 93 decoder := json.NewDecoder(res.Body) 94 rpcRes = new(Response) 95 err = decoder.Decode(&rpcRes) 96 require.NoError(t, err) 97 98 err = res.Body.Close() 99 require.NoError(t, err) 100 require.Nil(t, rpcRes.Error) 101 102 return rpcRes 103 } 104 105 func CallWithError(netAddr string, method string, params interface{}) (*Response, error) { 106 req, err := json.Marshal(CreateRequest(method, params)) 107 if err != nil { 108 return nil, err 109 } 110 111 var rpcRes *Response 112 //time.Sleep(1 * time.Second) 113 /* #nosec */ 114 115 if netAddr == "" { 116 netAddr = "http://localhost:8030" 117 } 118 res, err := http.Post(netAddr, "application/json", bytes.NewBuffer(req)) //nolint:gosec 119 if err != nil { 120 return nil, err 121 } 122 123 decoder := json.NewDecoder(res.Body) 124 rpcRes = new(Response) 125 err = decoder.Decode(&rpcRes) 126 if err != nil { 127 return nil, err 128 } 129 130 err = res.Body.Close() 131 if err != nil { 132 return nil, err 133 } 134 135 if rpcRes.Error != nil { 136 return nil, fmt.Errorf(rpcRes.Error.Message) 137 } 138 139 return rpcRes, nil 140 } 141 142 //nolint 143 func GetTransactionReceipt(t *testing.T, addr string, hash ethcmn.Hash) map[string]interface{} { 144 param := []string{hash.Hex()} 145 rpcRes := Call(t, addr, "eth_getTransactionReceipt", param) 146 147 receipt := make(map[string]interface{}) 148 err := json.Unmarshal(rpcRes.Result, &receipt) 149 require.NoError(t, err) 150 151 return receipt 152 } 153 154 func WaitForReceipt(t *testing.T, netAddr string, hash ethcmn.Hash) map[string]interface{} { 155 for i := 0; i < 12; i++ { 156 receipt := GetTransactionReceipt(t, netAddr, hash) 157 if receipt != nil { 158 return receipt 159 } 160 161 time.Sleep(time.Second) 162 } 163 164 return nil 165 } 166 167 func GetNonce(t *testing.T, netAddr string, block string, addr string) hexutil.Uint64 { 168 rpcRes := Call(t, netAddr, "eth_getTransactionCount", []interface{}{addr, block}) 169 170 var nonce hexutil.Uint64 171 require.NoError(t, json.Unmarshal(rpcRes.Result, &nonce)) 172 return nonce 173 } 174 175 func UnlockAllAccounts(t *testing.T, netAddr string) { 176 var accts []common.Address 177 rpcRes := Call(t, netAddr, "eth_accounts", []map[string]string{}) 178 err := json.Unmarshal(rpcRes.Result, &accts) 179 require.NoError(t, err) 180 181 for _, acct := range accts { 182 t.Logf("account: %v", acct) 183 rpcRes = Call(t, netAddr, "personal_unlockAccount", []interface{}{acct, ""}) 184 var unlocked bool 185 err = json.Unmarshal(rpcRes.Result, &unlocked) 186 require.NoError(t, err) 187 require.True(t, unlocked) 188 } 189 }