forked from cerc-io/laconicd-deprecated
update fork
This commit is contained in:
@@ -1,7 +1,5 @@
|
||||
// This is a test utility for Ethermint's Web3 JSON-RPC services.
|
||||
//
|
||||
// To run these tests please first ensure you have the ethermintd running
|
||||
//
|
||||
// You can configure the desired HOST and MODE as well in integration-test-all.sh
|
||||
package rpc
|
||||
|
||||
@@ -10,12 +8,13 @@ import (
|
||||
"fmt"
|
||||
"math/big"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/common/hexutil"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
rpctypes "github.com/cerc-io/laconicd/rpc/ethereum/types"
|
||||
rpctypes "github.com/cerc-io/laconicd/rpc/types"
|
||||
)
|
||||
|
||||
// func TestMain(m *testing.M) {
|
||||
@@ -269,8 +268,9 @@ func TestEth_Pending_GetTransactionByBlockNumberAndIndex(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestEth_Pending_GetTransactionByHash(t *testing.T) {
|
||||
sleep := 0 * time.Second
|
||||
// negative case, check that it returns empty.
|
||||
rpcRes := Call(t, "eth_getTransactionByHash", []interface{}{"0xec5fa15e1368d6ac314f9f64118c5794f076f63c02e66f97ea5fe1de761a8973"})
|
||||
rpcRes := CallWithSleep(t, "eth_getTransactionByHash", []interface{}{"0xec5fa15e1368d6ac314f9f64118c5794f076f63c02e66f97ea5fe1de761a8973"}, sleep)
|
||||
var tx map[string]interface{}
|
||||
err := json.Unmarshal(rpcRes.Result, &tx)
|
||||
require.NoError(t, err)
|
||||
@@ -281,17 +281,17 @@ func TestEth_Pending_GetTransactionByHash(t *testing.T) {
|
||||
param := makePendingTxParams(t)
|
||||
param[0]["data"] = data
|
||||
|
||||
txRes := Call(t, "eth_sendTransaction", param)
|
||||
txRes := CallWithSleep(t, "eth_sendTransaction", param, sleep)
|
||||
var txHash common.Hash
|
||||
err = txHash.UnmarshalJSON(txRes.Result)
|
||||
require.NoError(t, err)
|
||||
|
||||
rpcRes = Call(t, "eth_getTransactionByHash", []interface{}{txHash})
|
||||
rpcRes = CallWithSleep(t, "eth_getTransactionByHash", []interface{}{txHash}, sleep)
|
||||
var pendingTx map[string]interface{}
|
||||
err = json.Unmarshal(rpcRes.Result, &pendingTx)
|
||||
require.NoError(t, err)
|
||||
|
||||
txsRes := Call(t, "eth_getPendingTransactions", []interface{}{})
|
||||
txsRes := CallWithSleep(t, "eth_getPendingTransactions", []interface{}{}, sleep)
|
||||
var pendingTxs []map[string]interface{}
|
||||
err = json.Unmarshal(txsRes.Result, &pendingTxs)
|
||||
require.NoError(t, err)
|
||||
|
||||
+31
-2
@@ -17,7 +17,7 @@ import (
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
rpctypes "github.com/cerc-io/laconicd/rpc/ethereum/types"
|
||||
rpctypes "github.com/cerc-io/laconicd/rpc/types"
|
||||
ethermint "github.com/cerc-io/laconicd/types"
|
||||
evmtypes "github.com/cerc-io/laconicd/x/evm/types"
|
||||
|
||||
@@ -301,6 +301,17 @@ func deployTestContract(t *testing.T) (hexutil.Bytes, map[string]interface{}) {
|
||||
return hash, receipt
|
||||
}
|
||||
|
||||
func getTransactionReceipt(t *testing.T, hash hexutil.Bytes) map[string]interface{} {
|
||||
param := []string{hash.String()}
|
||||
rpcRes := call(t, "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, hash hexutil.Bytes) map[string]interface{} {
|
||||
timeout := time.After(12 * time.Second)
|
||||
ticker := time.Tick(500 * time.Millisecond)
|
||||
@@ -310,7 +321,7 @@ func waitForReceipt(t *testing.T, hash hexutil.Bytes) map[string]interface{} {
|
||||
case <-timeout:
|
||||
return nil
|
||||
case <-ticker:
|
||||
receipt := GetTransactionReceipt(t, hash)
|
||||
receipt := getTransactionReceipt(t, hash)
|
||||
if receipt != nil {
|
||||
return receipt
|
||||
}
|
||||
@@ -318,6 +329,24 @@ func waitForReceipt(t *testing.T, hash hexutil.Bytes) map[string]interface{} {
|
||||
}
|
||||
}
|
||||
|
||||
func TestEth_IncompleteSendTransaction(t *testing.T) {
|
||||
// get gasprice
|
||||
gasPrice := GetGasPrice(t)
|
||||
|
||||
// make tx params without from address
|
||||
param := make([]map[string]string, 1)
|
||||
param[0] = make(map[string]string)
|
||||
param[0]["from"] = ""
|
||||
param[0]["to"] = "0x1122334455667788990011223344556677889900"
|
||||
param[0]["value"] = "0x1"
|
||||
param[0]["gasPrice"] = gasPrice
|
||||
_, err := callWithError("eth_sendTransaction", param)
|
||||
|
||||
// require well-formatted error (should not be "method handler crashed")
|
||||
require.Error(t, err)
|
||||
require.NotEqual(t, err.Error(), "method handler crashed", "no from field dealt with incorrectly")
|
||||
}
|
||||
|
||||
func TestEth_GetFilterChanges_NoTopics(t *testing.T) {
|
||||
rpcRes := call(t, "eth_blockNumber", []string{})
|
||||
|
||||
|
||||
+10
-4
@@ -63,12 +63,14 @@ func CreateRequest(method string, params interface{}) Request {
|
||||
}
|
||||
}
|
||||
|
||||
func Call(t *testing.T, method string, params interface{}) *Response {
|
||||
func CallWithSleep(t *testing.T, method string, params interface{}, sleep time.Duration) *Response {
|
||||
req, err := json.Marshal(CreateRequest(method, params))
|
||||
require.NoError(t, err)
|
||||
|
||||
var rpcRes *Response
|
||||
time.Sleep(1 * time.Second)
|
||||
if sleep > 0 {
|
||||
time.Sleep(sleep)
|
||||
}
|
||||
|
||||
httpReq, err := http.NewRequestWithContext(context.Background(), "POST", HOST, bytes.NewBuffer(req))
|
||||
if err != nil {
|
||||
@@ -94,6 +96,10 @@ func Call(t *testing.T, method string, params interface{}) *Response {
|
||||
return rpcRes
|
||||
}
|
||||
|
||||
func Call(t *testing.T, method string, params interface{}) *Response {
|
||||
return CallWithSleep(t, method, params, time.Second)
|
||||
}
|
||||
|
||||
func CallWithError(method string, params interface{}) (*Response, error) {
|
||||
req, err := json.Marshal(CreateRequest(method, params))
|
||||
if err != nil {
|
||||
@@ -167,7 +173,7 @@ func DeployTestContract(t *testing.T, addr []byte) (hexutil.Bytes, map[string]in
|
||||
param := make([]map[string]string, 1)
|
||||
param[0] = make(map[string]string)
|
||||
param[0]["from"] = "0x" + fmt.Sprintf("%x", addr)
|
||||
param[0]["data"] = "0x6080604052348015600f57600080fd5b5060117f775a94827b8fd9b519d36cd827093c664f93347070a554f65e4a6f56cd73889860405160405180910390a2603580604b6000396000f3fe6080604052600080fdfea165627a7a723058206cab665f0f557620554bb45adf266708d2bd349b8a4314bdff205ee8440e3c240029"
|
||||
param[0]["data"] = "0x6080604052348015600f57600080fd5b5060117f775a94827b8fd9b519d36cd827093c664f93347070a554f65e4a6f56cd73889860405160405180910390a2603580604b6000396000f3fe6080604052600080fdfea165627a7a723058206cab665f0f557620554bb45adf266708d2bd349b8a4314bdff205ee8440e3c240029" //nolint:lll
|
||||
param[0]["gas"] = "0x200000"
|
||||
|
||||
rpcRes := Call(t, "personal_unlockAccount", []interface{}{param[0]["from"], ""})
|
||||
@@ -206,7 +212,7 @@ func DeployTestContractWithFunction(t *testing.T, addr []byte) hexutil.Bytes {
|
||||
// }
|
||||
// }
|
||||
|
||||
bytecode := "0x608060405234801561001057600080fd5b5060117f775a94827b8fd9b519d36cd827093c664f93347070a554f65e4a6f56cd73889860405160405180910390a260d08061004d6000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c8063eb8ac92114602d575b600080fd5b606060048036036040811015604157600080fd5b8101908080359060200190929190803590602001909291905050506062565b005b8160008190555080827ff3ca124a697ba07e8c5e80bebcfcc48991fc16a63170e8a9206e30508960d00360405160405180910390a3505056fea265627a7a723158201d94d2187aaf3a6790527b615fcc40970febf0385fa6d72a2344848ebd0df3e964736f6c63430005110032"
|
||||
bytecode := "0x608060405234801561001057600080fd5b5060117f775a94827b8fd9b519d36cd827093c664f93347070a554f65e4a6f56cd73889860405160405180910390a260d08061004d6000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c8063eb8ac92114602d575b600080fd5b606060048036036040811015604157600080fd5b8101908080359060200190929190803590602001909291905050506062565b005b8160008190555080827ff3ca124a697ba07e8c5e80bebcfcc48991fc16a63170e8a9206e30508960d00360405160405180910390a3505056fea265627a7a723158201d94d2187aaf3a6790527b615fcc40970febf0385fa6d72a2344848ebd0df3e964736f6c63430005110032" //nolint:lll
|
||||
|
||||
param := make([]map[string]string, 1)
|
||||
param[0] = make(map[string]string)
|
||||
|
||||
@@ -0,0 +1,174 @@
|
||||
package rpc
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/gorilla/websocket"
|
||||
"github.com/stretchr/testify/require"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
HOST_WS = os.Getenv("HOST_WS")
|
||||
wsUrl string
|
||||
)
|
||||
|
||||
func init() {
|
||||
if HOST_WS == "" {
|
||||
HOST_WS = "localhost:8542"
|
||||
}
|
||||
|
||||
u := url.URL{Scheme: "ws", Host: HOST_WS, Path: ""}
|
||||
wsUrl = u.String()
|
||||
}
|
||||
|
||||
func TestWsSingleRequest(t *testing.T) {
|
||||
t.Log("test simple rpc request net_version with Websocket")
|
||||
|
||||
wc, _, err := websocket.DefaultDialer.Dial(wsUrl, nil)
|
||||
require.NoError(t, err)
|
||||
defer wc.Close()
|
||||
|
||||
wsWriteMessage(t, wc, `{"jsonrpc":"2.0","method":"net_version","params":[],"id":1}`)
|
||||
|
||||
time.Sleep(1 * time.Second)
|
||||
mb := readMessage(t, wc)
|
||||
msg := jsonUnmarshal(t, mb)
|
||||
result, ok := msg["result"].(string)
|
||||
require.True(t, ok)
|
||||
require.Equal(t, "9000", result)
|
||||
}
|
||||
|
||||
func TestWsBatchRequest(t *testing.T) {
|
||||
t.Log("test batch request with Websocket")
|
||||
|
||||
wc, _, err := websocket.DefaultDialer.Dial(wsUrl, nil)
|
||||
require.NoError(t, err)
|
||||
defer wc.Close()
|
||||
|
||||
wsWriteMessage(t, wc, `[{"jsonrpc":"2.0","method":"net_version","params":[],"id":1},{"jsonrpc":"2.0","method":"eth_protocolVersion","params":[],"id":2}]`)
|
||||
|
||||
time.Sleep(1 * time.Second)
|
||||
mb := readMessage(t, wc)
|
||||
|
||||
var msg []map[string]interface{}
|
||||
err = json.Unmarshal(mb, &msg)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, 2, len(msg))
|
||||
|
||||
// net_version
|
||||
resNetVersion := msg[0]
|
||||
result, ok := resNetVersion["result"].(string)
|
||||
require.True(t, ok)
|
||||
require.Equal(t, "9000", result)
|
||||
id, ok := resNetVersion["id"].(float64)
|
||||
require.True(t, ok)
|
||||
require.Equal(t, 1, int(id))
|
||||
|
||||
// eth_protocolVersion
|
||||
resEthProtocolVersion := msg[1]
|
||||
result, ok = resEthProtocolVersion["result"].(string)
|
||||
require.True(t, ok)
|
||||
require.Equal(t, "0x41", result)
|
||||
id, ok = resEthProtocolVersion["id"].(float64)
|
||||
require.True(t, ok)
|
||||
require.Equal(t, 2, int(id))
|
||||
}
|
||||
|
||||
func TestWsEth_subscribe_newHeads(t *testing.T) {
|
||||
t.Log("test eth_subscribe newHeads with Websocket")
|
||||
|
||||
wc, _, err := websocket.DefaultDialer.Dial(wsUrl, nil)
|
||||
require.NoError(t, err)
|
||||
defer wc.Close()
|
||||
|
||||
wsWriteMessage(t, wc, `{"id":1,"method":"eth_subscribe","params":["newHeads",{}]}`)
|
||||
|
||||
time.Sleep(1 * time.Second)
|
||||
mb := readMessage(t, wc)
|
||||
msg := jsonUnmarshal(t, mb)
|
||||
subscribeId, ok := msg["result"].(string)
|
||||
require.True(t, ok)
|
||||
require.True(t, strings.HasPrefix(subscribeId, "0x"))
|
||||
|
||||
time.Sleep(3 * time.Second)
|
||||
mb = readMessage(t, wc)
|
||||
msg = jsonUnmarshal(t, mb)
|
||||
method, ok := msg["method"].(string)
|
||||
require.Equal(t, "eth_subscription", method)
|
||||
|
||||
// id should not exist with eth_subscription event
|
||||
_, ok = msg["id"].(float64)
|
||||
require.False(t, ok)
|
||||
|
||||
wsUnsubscribe(t, wc, subscribeId) // eth_unsubscribe
|
||||
}
|
||||
|
||||
func TestWsEth_subscribe_log(t *testing.T) {
|
||||
t.Log("test eth_subscribe log with websocket")
|
||||
|
||||
wc, _, err := websocket.DefaultDialer.Dial(wsUrl, nil)
|
||||
require.NoError(t, err)
|
||||
defer wc.Close()
|
||||
|
||||
wsWriteMessage(t, wc, fmt.Sprintf(`{"jsonrpc":"2.0","id":1,"method":"eth_subscribe","params":["logs",{"topics":["%s", "%s"]}]}`, helloTopic, worldTopic))
|
||||
|
||||
time.Sleep(1 * time.Second)
|
||||
mb := readMessage(t, wc)
|
||||
msg := jsonUnmarshal(t, mb)
|
||||
subscribeId, ok := msg["result"].(string)
|
||||
require.True(t, ok)
|
||||
require.True(t, strings.HasPrefix(subscribeId, "0x"))
|
||||
|
||||
// do something here to receive subscription messages
|
||||
deployTestContractWithFunction(t)
|
||||
|
||||
time.Sleep(3 * time.Second)
|
||||
mb = readMessage(t, wc)
|
||||
msg = jsonUnmarshal(t, mb)
|
||||
method, ok := msg["method"].(string)
|
||||
require.Equal(t, "eth_subscription", method)
|
||||
|
||||
// id should not exist with eth_subscription event
|
||||
_, ok = msg["id"].(float64)
|
||||
require.False(t, ok)
|
||||
|
||||
wsUnsubscribe(t, wc, subscribeId) // eth_unsubscribe
|
||||
}
|
||||
|
||||
func wsWriteMessage(t *testing.T, wc *websocket.Conn, jsonStr string) {
|
||||
t.Logf("send: %s", jsonStr)
|
||||
err := wc.WriteMessage(websocket.TextMessage, []byte(jsonStr))
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
func wsUnsubscribe(t *testing.T, wc *websocket.Conn, subscribeId string) {
|
||||
t.Logf("eth_unsubscribe %s", subscribeId)
|
||||
wsWriteMessage(t, wc, fmt.Sprintf(`{"id":1,"method":"eth_unsubscribe","params":["%s"]}`, subscribeId))
|
||||
|
||||
time.Sleep(1 * time.Second)
|
||||
mb := readMessage(t, wc)
|
||||
msg := jsonUnmarshal(t, mb)
|
||||
|
||||
result, ok := msg["result"].(bool)
|
||||
require.True(t, ok)
|
||||
require.True(t, result)
|
||||
}
|
||||
|
||||
func readMessage(t *testing.T, wc *websocket.Conn) []byte {
|
||||
_, mb, err := wc.ReadMessage()
|
||||
require.NoError(t, err)
|
||||
t.Logf("recv: %s", mb)
|
||||
return mb
|
||||
}
|
||||
|
||||
func jsonUnmarshal(t *testing.T, mb []byte) map[string]interface{} {
|
||||
var msg map[string]interface{}
|
||||
err := json.Unmarshal(mb, &msg)
|
||||
require.NoError(t, err)
|
||||
return msg
|
||||
}
|
||||
Reference in New Issue
Block a user