* add PendingBlockNumber -1

* increase block times

* update bn

* get pending balance

* additional logic to check for pending state

* add multiple balance query

* pending state for getTransactionCount

* fix lint

* add getBlockTransactionCountByNumber code - commented

* cleanup test

* GetBlockTransactionCountByNumber

* cleanup

* getBlockByNumber

* GetTransactionByBlockNumberAndIndex

* conform to namespace changes

* exportable FormatBlock method

* eth_getTransactionByHash

* eth_getTransactionByBlockNumberAndIndex

* pending nonce

* set nonce for pending and check for invalid

* WIP: doCall

* add pending tx test

* cleanup + refactor

* push first tests and init pending

* pending changes (#600)

* cleanup

* updates

* more fixes

* lint

* update call and send

* comments and minor changes

* add pending tests into sep package

* fix latest case for eth_GetBlockTransactionCountByNumber

* fix repeating null transactions in queue

* remove repeated structs

* latestblock case

* revert init script back

* fix to exportable method

* automate pending tests; add make cmd

* move and comment out pending call test

* fix some golint

* fix unlock issue

* wip: linter stringer fix?

* stringer lint

* set arr instead of append

* instantiate with length

* sep if statement

* edit pendingblocknumber note

* switch statement

* fix and update tests

* move tests-pending into tests dir

* remove commented test

* revert to appending pendingtx

* Update tests/utils.go

Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com>

* Update tests/utils.go

Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com>

* Update tests/utils.go

Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com>

* require no err

* rename var

* check result for eth_sendTransaction

* update changelog

* update

* Update tests/utils.go

Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com>

* Update tests/tests-pending/rpc_pending_test.go

Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com>

* changelog

* remove redundant check

Co-authored-by: noot <elizabethjbinks@gmail.com>
Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com>
Co-authored-by: Federico Kunze <federico.kunze94@gmail.com>
This commit is contained in:
Daniel Choi
2020-12-15 19:52:09 +00:00
committed by GitHub
co-authored by Federico Kunze noot Federico Kunze
parent 602e61adea
commit cb96bc4ea3
12 changed files with 1027 additions and 358 deletions
+15 -15
View File
@@ -12,7 +12,7 @@ import (
)
func TestPersonal_ListAccounts(t *testing.T) {
rpcRes := call(t, "personal_listAccounts", []string{})
rpcRes := Call(t, "personal_listAccounts", []string{})
var res []hexutil.Bytes
err := json.Unmarshal(rpcRes.Result, &res)
@@ -21,12 +21,12 @@ func TestPersonal_ListAccounts(t *testing.T) {
}
func TestPersonal_NewAccount(t *testing.T) {
rpcRes := call(t, "personal_newAccount", []string{"password"})
rpcRes := Call(t, "personal_newAccount", []string{"password"})
var addr common.Address
err := json.Unmarshal(rpcRes.Result, &addr)
require.NoError(t, err)
rpcRes = call(t, "personal_listAccounts", []string{})
rpcRes = Call(t, "personal_listAccounts", []string{})
var res []hexutil.Bytes
err = json.Unmarshal(rpcRes.Result, &res)
require.NoError(t, err)
@@ -34,7 +34,7 @@ func TestPersonal_NewAccount(t *testing.T) {
}
func TestPersonal_Sign(t *testing.T) {
rpcRes := call(t, "personal_sign", []interface{}{hexutil.Bytes{0x88}, hexutil.Bytes(from), ""})
rpcRes := Call(t, "personal_sign", []interface{}{hexutil.Bytes{0x88}, hexutil.Bytes(from), ""})
var res hexutil.Bytes
err := json.Unmarshal(rpcRes.Result, &res)
@@ -49,7 +49,7 @@ func TestPersonal_ImportRawKey(t *testing.T) {
// parse priv key to hex
hexPriv := common.Bytes2Hex(ethcrypto.FromECDSA(privkey))
rpcRes := call(t, "personal_importRawKey", []string{hexPriv, "password"})
rpcRes := Call(t, "personal_importRawKey", []string{hexPriv, "password"})
var res hexutil.Bytes
err = json.Unmarshal(rpcRes.Result, &res)
@@ -63,14 +63,14 @@ func TestPersonal_ImportRawKey(t *testing.T) {
func TestPersonal_EcRecover(t *testing.T) {
data := hexutil.Bytes{0x88}
rpcRes := call(t, "personal_sign", []interface{}{data, hexutil.Bytes(from), ""})
rpcRes := Call(t, "personal_sign", []interface{}{data, hexutil.Bytes(from), ""})
var res hexutil.Bytes
err := json.Unmarshal(rpcRes.Result, &res)
require.NoError(t, err)
require.Equal(t, 65, len(res))
rpcRes = call(t, "personal_ecRecover", []interface{}{data, res})
rpcRes = Call(t, "personal_ecRecover", []interface{}{data, res})
var ecrecoverRes common.Address
err = json.Unmarshal(rpcRes.Result, &ecrecoverRes)
require.NoError(t, err)
@@ -79,23 +79,23 @@ func TestPersonal_EcRecover(t *testing.T) {
func TestPersonal_UnlockAccount(t *testing.T) {
pswd := "nootwashere"
rpcRes := call(t, "personal_newAccount", []string{pswd})
rpcRes := Call(t, "personal_newAccount", []string{pswd})
var addr common.Address
err := json.Unmarshal(rpcRes.Result, &addr)
require.NoError(t, err)
// try to sign, should be locked
_, err = callWithError("personal_sign", []interface{}{hexutil.Bytes{0x88}, addr, ""})
_, err = CallWithError("personal_sign", []interface{}{hexutil.Bytes{0x88}, addr, ""})
require.Error(t, err)
rpcRes = call(t, "personal_unlockAccount", []interface{}{addr, ""})
rpcRes = Call(t, "personal_unlockAccount", []interface{}{addr, ""})
var unlocked bool
err = json.Unmarshal(rpcRes.Result, &unlocked)
require.NoError(t, err)
require.True(t, unlocked)
// try to sign, should work now
rpcRes = call(t, "personal_sign", []interface{}{hexutil.Bytes{0x88}, addr, pswd})
rpcRes = Call(t, "personal_sign", []interface{}{hexutil.Bytes{0x88}, addr, pswd})
var res hexutil.Bytes
err = json.Unmarshal(rpcRes.Result, &res)
require.NoError(t, err)
@@ -104,24 +104,24 @@ func TestPersonal_UnlockAccount(t *testing.T) {
func TestPersonal_LockAccount(t *testing.T) {
pswd := "nootwashere"
rpcRes := call(t, "personal_newAccount", []string{pswd})
rpcRes := Call(t, "personal_newAccount", []string{pswd})
var addr common.Address
err := json.Unmarshal(rpcRes.Result, &addr)
require.NoError(t, err)
rpcRes = call(t, "personal_unlockAccount", []interface{}{addr, ""})
rpcRes = Call(t, "personal_unlockAccount", []interface{}{addr, ""})
var unlocked bool
err = json.Unmarshal(rpcRes.Result, &unlocked)
require.NoError(t, err)
require.True(t, unlocked)
rpcRes = call(t, "personal_lockAccount", []interface{}{addr})
rpcRes = Call(t, "personal_lockAccount", []interface{}{addr})
var locked bool
err = json.Unmarshal(rpcRes.Result, &locked)
require.NoError(t, err)
require.True(t, locked)
// try to sign, should be locked
_, err = callWithError("personal_sign", []interface{}{hexutil.Bytes{0x88}, addr, ""})
_, err = CallWithError("personal_sign", []interface{}{hexutil.Bytes{0x88}, addr, ""})
require.Error(t, err)
}
+57 -280
View File
@@ -8,7 +8,6 @@ package tests
import (
"bytes"
"encoding/hex"
"encoding/json"
"fmt"
"math/big"
@@ -33,44 +32,19 @@ const (
)
var (
MODE = os.Getenv("MODE")
HOST = os.Getenv("HOST")
zeroString = "0x0"
MODE = os.Getenv("MODE")
from = []byte{}
zeroString = "0x0"
)
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 TestMain(m *testing.M) {
if MODE != "rpc" {
_, _ = fmt.Fprintln(os.Stdout, "Skipping RPC test")
return
}
if HOST == "" {
HOST = "http://localhost:8545"
}
var err error
from, err = getAddress()
from, err = GetAddress()
if err != nil {
fmt.Printf("failed to get account: %s\n", err)
os.Exit(1)
@@ -81,106 +55,19 @@ func TestMain(m *testing.M) {
os.Exit(code)
}
func getAddress() ([]byte, error) {
rpcRes, err := callWithError("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,
}
}
func call(t *testing.T, 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 */
res, err := http.Post(HOST, "application/json", bytes.NewBuffer(req))
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(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 */
res, err := http.Post(HOST, "application/json", bytes.NewBuffer(req))
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
}
// turns a 0x prefixed hex string to a big.Int
func hexToBigInt(t *testing.T, in string) *big.Int {
s := in[2:]
b, err := hex.DecodeString(s)
require.NoError(t, err)
return big.NewInt(0).SetBytes(b)
}
func TestBlockBloom(t *testing.T) {
hash := deployTestContractWithFunction(t)
receipt := waitForReceipt(t, hash)
hash := DeployTestContractWithFunction(t, from)
receipt := WaitForReceipt(t, hash)
number := receipt["blockNumber"].(string)
param := []interface{}{number, false}
rpcRes := call(t, "eth_getBlockByNumber", param)
rpcRes := Call(t, "eth_getBlockByNumber", param)
block := make(map[string]interface{})
err := json.Unmarshal(rpcRes.Result, &block)
require.NoError(t, err)
lb := hexToBigInt(t, block["logsBloom"].(string))
lb := HexToBigInt(t, block["logsBloom"].(string))
require.NotEqual(t, big.NewInt(0), lb)
require.Equal(t, hash.String(), block["transactions"].([]interface{})[0])
}
@@ -189,7 +76,7 @@ func TestEth_GetLogs_NoLogs(t *testing.T) {
param := make([]map[string][]string, 1)
param[0] = make(map[string][]string)
param[0]["topics"] = []string{}
rpcRes := call(t, "eth_getLogs", param)
rpcRes := Call(t, "eth_getLogs", param)
require.NotNil(t, rpcRes)
require.Nil(t, rpcRes.Error)
@@ -205,7 +92,7 @@ func TestEth_GetLogs_Topics_AB(t *testing.T) {
t.Skip("skipping TestEth_GetLogs_Topics_AB")
}
rpcRes := call(t, "eth_blockNumber", []string{})
rpcRes := Call(t, "eth_blockNumber", []string{})
var res hexutil.Uint64
err := res.UnmarshalJSON(rpcRes.Result)
@@ -216,10 +103,10 @@ func TestEth_GetLogs_Topics_AB(t *testing.T) {
param[0]["topics"] = []string{helloTopic, worldTopic}
param[0]["fromBlock"] = res.String()
hash := deployTestContractWithFunction(t)
waitForReceipt(t, hash)
hash := DeployTestContractWithFunction(t, from)
WaitForReceipt(t, hash)
rpcRes = call(t, "eth_getLogs", param)
rpcRes = Call(t, "eth_getLogs", param)
var logs []*ethtypes.Log
err = json.Unmarshal(rpcRes.Result, &logs)
@@ -234,9 +121,9 @@ func TestEth_GetTransactionCount(t *testing.T) {
t.Skip("skipping TestEth_GetTransactionCount")
}
prev := getNonce(t)
sendTestTransaction(t)
post := getNonce(t)
prev := GetNonce(t, "latest")
SendTestTransaction(t, from)
post := GetNonce(t, "latest")
require.Equal(t, prev, post-1)
}
@@ -246,10 +133,10 @@ func TestEth_GetTransactionLogs(t *testing.T) {
t.Skip("skipping TestEth_GetTransactionLogs")
}
hash, _ := deployTestContract(t)
hash, _ := DeployTestContract(t, from)
param := []string{hash.String()}
rpcRes := call(t, "eth_getTransactionLogs", param)
rpcRes := Call(t, "eth_getTransactionLogs", param)
logs := new([]*ethtypes.Log)
err := json.Unmarshal(rpcRes.Result, logs)
@@ -260,7 +147,7 @@ func TestEth_GetTransactionLogs(t *testing.T) {
func TestEth_protocolVersion(t *testing.T) {
expectedRes := hexutil.Uint(ethermint.ProtocolVersion)
rpcRes := call(t, "eth_protocolVersion", []string{})
rpcRes := Call(t, "eth_protocolVersion", []string{})
var res hexutil.Uint
err := res.UnmarshalJSON(rpcRes.Result)
@@ -271,7 +158,7 @@ func TestEth_protocolVersion(t *testing.T) {
}
func TestEth_chainId(t *testing.T) {
rpcRes := call(t, "eth_chainId", []string{})
rpcRes := Call(t, "eth_chainId", []string{})
var res hexutil.Uint
err := res.UnmarshalJSON(rpcRes.Result)
@@ -280,7 +167,7 @@ func TestEth_chainId(t *testing.T) {
}
func TestEth_blockNumber(t *testing.T) {
rpcRes := call(t, "eth_blockNumber", []string{})
rpcRes := Call(t, "eth_blockNumber", []string{})
var res hexutil.Uint64
err := res.UnmarshalJSON(rpcRes.Result)
@@ -291,7 +178,7 @@ func TestEth_blockNumber(t *testing.T) {
func TestEth_coinbase(t *testing.T) {
zeroAddress := hexutil.Bytes(ethcmn.Address{}.Bytes())
rpcRes := call(t, "eth_coinbase", []string{})
rpcRes := Call(t, "eth_coinbase", []string{})
var res hexutil.Bytes
err := res.UnmarshalJSON(rpcRes.Result)
@@ -302,7 +189,7 @@ func TestEth_coinbase(t *testing.T) {
}
func TestEth_GetBalance(t *testing.T) {
rpcRes := call(t, "eth_getBalance", []string{addrA, zeroString})
rpcRes := Call(t, "eth_getBalance", []string{addrA, zeroString})
var res hexutil.Big
err := res.UnmarshalJSON(rpcRes.Result)
@@ -318,7 +205,7 @@ func TestEth_GetBalance(t *testing.T) {
func TestEth_GetStorageAt(t *testing.T) {
expectedRes := hexutil.Bytes{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
rpcRes := call(t, "eth_getStorageAt", []string{addrA, fmt.Sprint(addrAStoreKey), zeroString})
rpcRes := Call(t, "eth_getStorageAt", []string{addrA, fmt.Sprint(addrAStoreKey), zeroString})
var storage hexutil.Bytes
err := storage.UnmarshalJSON(rpcRes.Result)
@@ -334,7 +221,7 @@ func TestEth_GetProof(t *testing.T) {
params[0] = addrA
params[1] = []string{fmt.Sprint(addrAStoreKey)}
params[2] = "latest"
rpcRes := call(t, "eth_getProof", params)
rpcRes := Call(t, "eth_getProof", params)
require.NotNil(t, rpcRes)
var accRes rpctypes.AccountResult
@@ -348,7 +235,7 @@ func TestEth_GetProof(t *testing.T) {
func TestEth_GetCode(t *testing.T) {
expectedRes := hexutil.Bytes{}
rpcRes := call(t, "eth_getCode", []string{addrA, zeroString})
rpcRes := Call(t, "eth_getCode", []string{addrA, zeroString})
var code hexutil.Bytes
err := code.UnmarshalJSON(rpcRes.Result)
@@ -368,13 +255,13 @@ func TestEth_SendTransaction_Transfer(t *testing.T) {
param[0]["gasLimit"] = "0x5208"
param[0]["gasPrice"] = "0x55ae82600"
rpcRes := call(t, "eth_sendTransaction", param)
rpcRes := Call(t, "eth_sendTransaction", param)
var hash hexutil.Bytes
err := json.Unmarshal(rpcRes.Result, &hash)
require.NoError(t, err)
receipt := waitForReceipt(t, hash)
receipt := WaitForReceipt(t, hash)
require.NotNil(t, receipt)
require.Equal(t, "0x1", receipt["status"].(string))
}
@@ -385,7 +272,7 @@ func TestEth_SendTransaction_ContractDeploy(t *testing.T) {
param[0]["from"] = "0x" + fmt.Sprintf("%x", from)
param[0]["data"] = "0x6080604052348015600f57600080fd5b5060117f775a94827b8fd9b519d36cd827093c664f93347070a554f65e4a6f56cd73889860405160405180910390a2603580604b6000396000f3fe6080604052600080fdfea165627a7a723058206cab665f0f557620554bb45adf266708d2bd349b8a4314bdff205ee8440e3c240029"
rpcRes := call(t, "eth_sendTransaction", param)
rpcRes := Call(t, "eth_sendTransaction", param)
var hash hexutil.Bytes
err := json.Unmarshal(rpcRes.Result, &hash)
@@ -396,7 +283,7 @@ func TestEth_NewFilter(t *testing.T) {
param := make([]map[string][]string, 1)
param[0] = make(map[string][]string)
param[0]["topics"] = []string{"0x0000000000000000000000000000000000000000000000000000000012341234"}
rpcRes := call(t, "eth_newFilter", param)
rpcRes := Call(t, "eth_newFilter", param)
var ID string
err := json.Unmarshal(rpcRes.Result, &ID)
@@ -404,7 +291,7 @@ func TestEth_NewFilter(t *testing.T) {
}
func TestEth_NewBlockFilter(t *testing.T) {
rpcRes := call(t, "eth_newBlockFilter", []string{})
rpcRes := Call(t, "eth_newBlockFilter", []string{})
var ID string
err := json.Unmarshal(rpcRes.Result, &ID)
@@ -412,7 +299,7 @@ func TestEth_NewBlockFilter(t *testing.T) {
}
func TestEth_GetFilterChanges_BlockFilter(t *testing.T) {
rpcRes := call(t, "eth_newBlockFilter", []string{})
rpcRes := Call(t, "eth_newBlockFilter", []string{})
var ID string
err := json.Unmarshal(rpcRes.Result, &ID)
@@ -420,7 +307,7 @@ func TestEth_GetFilterChanges_BlockFilter(t *testing.T) {
time.Sleep(5 * time.Second)
changesRes := call(t, "eth_getFilterChanges", []string{ID})
changesRes := Call(t, "eth_getFilterChanges", []string{ID})
var hashes []ethcmn.Hash
err = json.Unmarshal(changesRes.Result, &hashes)
require.NoError(t, err)
@@ -431,13 +318,13 @@ func TestEth_GetFilterChanges_NoLogs(t *testing.T) {
param := make([]map[string][]string, 1)
param[0] = make(map[string][]string)
param[0]["topics"] = []string{}
rpcRes := call(t, "eth_newFilter", param)
rpcRes := Call(t, "eth_newFilter", param)
var ID string
err := json.Unmarshal(rpcRes.Result, &ID)
require.NoError(t, err)
changesRes := call(t, "eth_getFilterChanges", []string{ID})
changesRes := Call(t, "eth_getFilterChanges", []string{ID})
var logs []*ethtypes.Log
err = json.Unmarshal(changesRes.Result, &logs)
@@ -445,7 +332,7 @@ func TestEth_GetFilterChanges_NoLogs(t *testing.T) {
}
func TestEth_GetFilterChanges_WrongID(t *testing.T) {
req, err := json.Marshal(createRequest("eth_getFilterChanges", []string{"0x1122334400000077"}))
req, err := json.Marshal(CreateRequest("eth_getFilterChanges", []string{"0x1122334400000077"}))
require.NoError(t, err)
var rpcRes *Response
@@ -464,28 +351,13 @@ func TestEth_GetFilterChanges_WrongID(t *testing.T) {
require.NotNil(t, "invalid filter ID", rpcRes.Error.Message)
}
// sendTestTransaction sends a dummy transaction
func sendTestTransaction(t *testing.T) hexutil.Bytes {
param := make([]map[string]string, 1)
param[0] = make(map[string]string)
param[0]["from"] = "0x" + fmt.Sprintf("%x", from)
param[0]["to"] = "0x1122334455667788990011223344556677889900"
param[0]["value"] = "0x1"
rpcRes := call(t, "eth_sendTransaction", param)
var hash hexutil.Bytes
err := json.Unmarshal(rpcRes.Result, &hash)
require.NoError(t, err)
return hash
}
func TestEth_GetTransactionReceipt(t *testing.T) {
hash := sendTestTransaction(t)
hash := SendTestTransaction(t, from)
time.Sleep(time.Second * 5)
param := []string{hash.String()}
rpcRes := call(t, "eth_getTransactionReceipt", param)
rpcRes := Call(t, "eth_getTransactionReceipt", param)
require.Nil(t, rpcRes.Error)
receipt := make(map[string]interface{})
@@ -496,34 +368,13 @@ func TestEth_GetTransactionReceipt(t *testing.T) {
require.Equal(t, []interface{}{}, receipt["logs"].([]interface{}))
}
// deployTestContract deploys a contract that emits an event in the constructor
func deployTestContract(t *testing.T) (hexutil.Bytes, map[string]interface{}) {
param := make([]map[string]string, 1)
param[0] = make(map[string]string)
param[0]["from"] = "0x" + fmt.Sprintf("%x", from)
param[0]["data"] = "0x6080604052348015600f57600080fd5b5060117f775a94827b8fd9b519d36cd827093c664f93347070a554f65e4a6f56cd73889860405160405180910390a2603580604b6000396000f3fe6080604052600080fdfea165627a7a723058206cab665f0f557620554bb45adf266708d2bd349b8a4314bdff205ee8440e3c240029"
param[0]["gas"] = "0x200000"
rpcRes := call(t, "eth_sendTransaction", param)
var hash hexutil.Bytes
err := json.Unmarshal(rpcRes.Result, &hash)
require.NoError(t, err)
receipt := waitForReceipt(t, hash)
require.NotNil(t, receipt, "transaction failed")
require.Equal(t, "0x1", receipt["status"].(string))
return hash, receipt
}
func TestEth_GetTransactionReceipt_ContractDeployment(t *testing.T) {
hash, _ := deployTestContract(t)
hash, _ := DeployTestContract(t, from)
time.Sleep(time.Second * 5)
param := []string{hash.String()}
rpcRes := call(t, "eth_getTransactionReceipt", param)
rpcRes := Call(t, "eth_getTransactionReceipt", param)
receipt := make(map[string]interface{})
err := json.Unmarshal(rpcRes.Result, &receipt)
@@ -535,32 +386,8 @@ func TestEth_GetTransactionReceipt_ContractDeployment(t *testing.T) {
}
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{} {
for i := 0; i < 12; i++ {
receipt := getTransactionReceipt(t, hash)
if receipt != nil {
return receipt
}
time.Sleep(time.Second)
}
return nil
}
func TestEth_GetFilterChanges_NoTopics(t *testing.T) {
rpcRes := call(t, "eth_blockNumber", []string{})
rpcRes := Call(t, "eth_blockNumber", []string{})
var res hexutil.Uint64
err := res.UnmarshalJSON(rpcRes.Result)
@@ -572,17 +399,17 @@ func TestEth_GetFilterChanges_NoTopics(t *testing.T) {
param[0]["fromBlock"] = res.String()
// instantiate new filter
rpcRes = call(t, "eth_newFilter", param)
rpcRes = Call(t, "eth_newFilter", param)
require.Nil(t, rpcRes.Error)
var ID string
err = json.Unmarshal(rpcRes.Result, &ID)
require.NoError(t, err)
// deploy contract, emitting some event
deployTestContract(t)
DeployTestContract(t, from)
// get filter changes
changesRes := call(t, "eth_getFilterChanges", []string{ID})
changesRes := Call(t, "eth_getFilterChanges", []string{ID})
var logs []*ethtypes.Log
err = json.Unmarshal(changesRes.Result, &logs)
@@ -606,51 +433,11 @@ var helloTopic = "0x775a94827b8fd9b519d36cd827093c664f93347070a554f65e4a6f56cd73
// world parameter in Hello event
var worldTopic = "0x0000000000000000000000000000000000000000000000000000000000000011"
func deployTestContractWithFunction(t *testing.T) hexutil.Bytes {
// pragma solidity ^0.5.1;
// contract Test {
// event Hello(uint256 indexed world);
// event TestEvent(uint256 indexed a, uint256 indexed b);
// uint256 myStorage;
// constructor() public {
// emit Hello(17);
// }
// function test(uint256 a, uint256 b) public {
// myStorage = a;
// emit TestEvent(a, b);
// }
// }
bytecode := "0x608060405234801561001057600080fd5b5060117f775a94827b8fd9b519d36cd827093c664f93347070a554f65e4a6f56cd73889860405160405180910390a260d08061004d6000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c8063eb8ac92114602d575b600080fd5b606060048036036040811015604157600080fd5b8101908080359060200190929190803590602001909291905050506062565b005b8160008190555080827ff3ca124a697ba07e8c5e80bebcfcc48991fc16a63170e8a9206e30508960d00360405160405180910390a3505056fea265627a7a723158201d94d2187aaf3a6790527b615fcc40970febf0385fa6d72a2344848ebd0df3e964736f6c63430005110032"
param := make([]map[string]string, 1)
param[0] = make(map[string]string)
param[0]["from"] = "0x" + fmt.Sprintf("%x", from)
param[0]["data"] = bytecode
param[0]["gas"] = "0x200000"
rpcRes := call(t, "eth_sendTransaction", param)
var hash hexutil.Bytes
err := json.Unmarshal(rpcRes.Result, &hash)
require.NoError(t, err)
receipt := waitForReceipt(t, hash)
require.NotNil(t, receipt, "transaction failed")
require.Equal(t, "0x1", receipt["status"].(string))
return hash
}
// Tests topics case where there are topics in first two positions
func TestEth_GetFilterChanges_Topics_AB(t *testing.T) {
time.Sleep(time.Second)
rpcRes := call(t, "eth_blockNumber", []string{})
rpcRes := Call(t, "eth_blockNumber", []string{})
var res hexutil.Uint64
err := res.UnmarshalJSON(rpcRes.Result)
@@ -662,15 +449,15 @@ func TestEth_GetFilterChanges_Topics_AB(t *testing.T) {
param[0]["fromBlock"] = res.String()
// instantiate new filter
rpcRes = call(t, "eth_newFilter", param)
rpcRes = Call(t, "eth_newFilter", param)
var ID string
err = json.Unmarshal(rpcRes.Result, &ID)
require.NoError(t, err, string(rpcRes.Result))
deployTestContractWithFunction(t)
DeployTestContractWithFunction(t, from)
// get filter changes
changesRes := call(t, "eth_getFilterChanges", []string{ID})
changesRes := Call(t, "eth_getFilterChanges", []string{ID})
var logs []*ethtypes.Log
err = json.Unmarshal(changesRes.Result, &logs)
@@ -680,7 +467,7 @@ func TestEth_GetFilterChanges_Topics_AB(t *testing.T) {
}
func TestEth_GetFilterChanges_Topics_XB(t *testing.T) {
rpcRes := call(t, "eth_blockNumber", []string{})
rpcRes := Call(t, "eth_blockNumber", []string{})
var res hexutil.Uint64
err := res.UnmarshalJSON(rpcRes.Result)
@@ -692,15 +479,15 @@ func TestEth_GetFilterChanges_Topics_XB(t *testing.T) {
param[0]["fromBlock"] = res.String()
// instantiate new filter
rpcRes = call(t, "eth_newFilter", param)
rpcRes = Call(t, "eth_newFilter", param)
var ID string
err = json.Unmarshal(rpcRes.Result, &ID)
require.NoError(t, err)
deployTestContractWithFunction(t)
DeployTestContractWithFunction(t, from)
// get filter changes
changesRes := call(t, "eth_getFilterChanges", []string{ID})
changesRes := Call(t, "eth_getFilterChanges", []string{ID})
var logs []*ethtypes.Log
err = json.Unmarshal(changesRes.Result, &logs)
@@ -715,20 +502,20 @@ func TestEth_GetFilterChanges_Topics_XXC(t *testing.T) {
}
func TestEth_PendingTransactionFilter(t *testing.T) {
rpcRes := call(t, "eth_newPendingTransactionFilter", []string{})
rpcRes := Call(t, "eth_newPendingTransactionFilter", []string{})
var ID string
err := json.Unmarshal(rpcRes.Result, &ID)
require.NoError(t, err)
for i := 0; i < 5; i++ {
deployTestContractWithFunction(t)
DeployTestContractWithFunction(t, from)
}
time.Sleep(10 * time.Second)
// get filter changes
changesRes := call(t, "eth_getFilterChanges", []string{ID})
changesRes := Call(t, "eth_getFilterChanges", []string{ID})
require.NotNil(t, changesRes)
var txs []*hexutil.Bytes
@@ -738,23 +525,13 @@ func TestEth_PendingTransactionFilter(t *testing.T) {
require.True(t, len(txs) >= 2, "could not get any txs", "changesRes.Result", string(changesRes.Result))
}
func getNonce(t *testing.T) hexutil.Uint64 {
param := []interface{}{hexutil.Bytes(from), "latest"}
rpcRes := call(t, "eth_getTransactionCount", param)
var nonce hexutil.Uint64
err := json.Unmarshal(rpcRes.Result, &nonce)
require.NoError(t, err)
return nonce
}
func TestEth_EstimateGas(t *testing.T) {
param := make([]map[string]string, 1)
param[0] = make(map[string]string)
param[0]["from"] = "0x" + fmt.Sprintf("%x", from)
param[0]["to"] = "0x1122334455667788990011223344556677889900"
param[0]["value"] = "0x1"
rpcRes := call(t, "eth_estimateGas", param)
rpcRes := Call(t, "eth_estimateGas", param)
require.NotNil(t, rpcRes)
require.NotEmpty(t, rpcRes.Result)
@@ -773,7 +550,7 @@ func TestEth_EstimateGas_ContractDeployment(t *testing.T) {
param[0]["from"] = "0x" + fmt.Sprintf("%x", from)
param[0]["data"] = bytecode
rpcRes := call(t, "eth_estimateGas", param)
rpcRes := Call(t, "eth_estimateGas", param)
require.NotNil(t, rpcRes)
require.NotEmpty(t, rpcRes.Result)
@@ -786,7 +563,7 @@ func TestEth_EstimateGas_ContractDeployment(t *testing.T) {
func TestEth_GetBlockByNumber(t *testing.T) {
param := []interface{}{"0x1", false}
rpcRes := call(t, "eth_getBlockByNumber", param)
rpcRes := Call(t, "eth_getBlockByNumber", param)
block := make(map[string]interface{})
err := json.Unmarshal(rpcRes.Result, &block)
+323
View File
@@ -0,0 +1,323 @@
// This is a test utility for Ethermint's Web3 JSON-RPC services.
//
// To run these tests please first ensure you have the ethermintd running
// and have started the RPC service with `ethermintcli rest-server`.
//
// You can configure the desired HOST and MODE as well
package pending
import (
"encoding/json"
"fmt"
"math/big"
"os"
"testing"
"github.com/stretchr/testify/require"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
rpctypes "github.com/cosmos/ethermint/rpc/types"
util "github.com/cosmos/ethermint/tests"
)
const (
addrA = "0xc94770007dda54cF92009BFF0dE90c06F603a09f"
addrAStoreKey = 0
)
var (
MODE = os.Getenv("MODE")
from = []byte{}
)
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 TestMain(m *testing.M) {
if MODE != "pending" {
_, _ = fmt.Fprintln(os.Stdout, "Skipping pending RPC test")
return
}
var err error
from, err = util.GetAddress()
if err != nil {
fmt.Printf("failed to get account: %s\n", err)
os.Exit(1)
}
// Start all tests
code := m.Run()
os.Exit(code)
}
func TestEth_Pending_GetBalance(t *testing.T) {
var res hexutil.Big
rpcRes := util.Call(t, "eth_getBalance", []string{addrA, "latest"})
err := res.UnmarshalJSON(rpcRes.Result)
require.NoError(t, err)
preTxLatestBalance := res.ToInt()
rpcRes = util.Call(t, "eth_getBalance", []string{addrA, "pending"})
err = res.UnmarshalJSON(rpcRes.Result)
require.NoError(t, err)
preTxPendingBalance := res.ToInt()
t.Logf("Got pending balance %s for %s pre tx\n", preTxPendingBalance, addrA)
t.Logf("Got latest balance %s for %s pre tx\n", preTxLatestBalance, addrA)
param := make([]map[string]string, 1)
param[0] = make(map[string]string)
param[0]["from"] = "0x" + fmt.Sprintf("%x", from)
param[0]["to"] = addrA
param[0]["value"] = "0xA"
param[0]["gasLimit"] = "0x5208"
param[0]["gasPrice"] = "0x1"
rpcRes = util.Call(t, "eth_sendTransaction", param)
require.Nil(t, rpcRes.Error)
rpcRes = util.Call(t, "eth_getBalance", []string{addrA, "pending"})
err = res.UnmarshalJSON(rpcRes.Result)
require.NoError(t, err)
postTxPendingBalance := res.ToInt()
t.Logf("Got pending balance %s for %s post tx\n", postTxPendingBalance, addrA)
require.Equal(t, preTxPendingBalance.Add(preTxPendingBalance, big.NewInt(10)), postTxPendingBalance)
rpcRes = util.Call(t, "eth_getBalance", []string{addrA, "latest"})
err = res.UnmarshalJSON(rpcRes.Result)
require.NoError(t, err)
postTxLatestBalance := res.ToInt()
t.Logf("Got latest balance %s for %s post tx\n", postTxLatestBalance, addrA)
require.Equal(t, preTxLatestBalance, postTxLatestBalance)
}
func TestEth_Pending_GetTransactionCount(t *testing.T) {
prePendingNonce := util.GetNonce(t, "pending")
t.Logf("Pending nonce before tx is %d", prePendingNonce)
currentNonce := util.GetNonce(t, "latest")
t.Logf("Current nonce is %d", currentNonce)
param := make([]map[string]string, 1)
param[0] = make(map[string]string)
param[0]["from"] = "0x" + fmt.Sprintf("%x", from)
param[0]["to"] = addrA
param[0]["value"] = "0xA"
param[0]["gasLimit"] = "0x5208"
param[0]["gasPrice"] = "0x1"
txRes := util.Call(t, "eth_sendTransaction", param)
require.Nil(t, txRes.Error)
pendingNonce := util.GetNonce(t, "pending")
latestNonce := util.GetNonce(t, "latest")
t.Logf("Latest nonce is %d", latestNonce)
require.Equal(t, currentNonce, latestNonce)
t.Logf("Pending nonce is %d", pendingNonce)
require.NotEqual(t, latestNonce, pendingNonce)
require.Greater(t, uint64(pendingNonce), uint64(latestNonce))
require.Equal(t, uint64(prePendingNonce)+uint64(1), uint64(pendingNonce))
}
func TestEth_Pending_GetBlockTransactionCountByNumber(t *testing.T) {
rpcRes := util.Call(t, "eth_getBlockTransactionCountByNumber", []interface{}{"pending"})
var preTxPendingTxCount hexutil.Uint
err := json.Unmarshal(rpcRes.Result, &preTxPendingTxCount)
require.NoError(t, err)
t.Logf("Pre tx pending nonce is %d", preTxPendingTxCount)
rpcRes = util.Call(t, "eth_getBlockTransactionCountByNumber", []interface{}{"latest"})
var preTxLatestTxCount hexutil.Uint
err = json.Unmarshal(rpcRes.Result, &preTxLatestTxCount)
require.NoError(t, err)
t.Logf("Pre tx latest nonce is %d", preTxLatestTxCount)
param := make([]map[string]string, 1)
param[0] = make(map[string]string)
param[0]["from"] = "0x" + fmt.Sprintf("%x", from)
param[0]["to"] = addrA
param[0]["value"] = "0xA"
param[0]["gasLimit"] = "0x5208"
param[0]["gasPrice"] = "0x1"
txRes := util.Call(t, "eth_sendTransaction", param)
require.Nil(t, txRes.Error)
rpcRes = util.Call(t, "eth_getBlockTransactionCountByNumber", []interface{}{"pending"})
var postTxPendingTxCount hexutil.Uint
err = json.Unmarshal(rpcRes.Result, &postTxPendingTxCount)
require.NoError(t, err)
t.Logf("Post tx pending nonce is %d", postTxPendingTxCount)
rpcRes = util.Call(t, "eth_getBlockTransactionCountByNumber", []interface{}{"latest"})
var postTxLatestTxCount hexutil.Uint
err = json.Unmarshal(rpcRes.Result, &postTxLatestTxCount)
require.NoError(t, err)
t.Logf("Post tx latest nonce is %d", postTxLatestTxCount)
require.Equal(t, uint64(preTxPendingTxCount)+uint64(1), uint64(postTxPendingTxCount))
require.NotEqual(t, uint64(postTxPendingTxCount)-uint64(preTxPendingTxCount), uint64(postTxLatestTxCount)-uint64(preTxLatestTxCount))
}
func TestEth_Pending_GetBlockByNumber(t *testing.T) {
rpcRes := util.Call(t, "eth_getBlockByNumber", []interface{}{"latest", true})
var preTxLatestBlock map[string]interface{}
err := json.Unmarshal(rpcRes.Result, &preTxLatestBlock)
require.NoError(t, err)
preTxLatestTxs := len(preTxLatestBlock["transactions"].([]interface{}))
rpcRes = util.Call(t, "eth_getBlockByNumber", []interface{}{"pending", true})
var preTxPendingBlock map[string]interface{}
err = json.Unmarshal(rpcRes.Result, &preTxPendingBlock)
require.NoError(t, err)
preTxPendingTxs := len(preTxPendingBlock["transactions"].([]interface{}))
param := make([]map[string]string, 1)
param[0] = make(map[string]string)
param[0]["from"] = "0x" + fmt.Sprintf("%x", from)
param[0]["to"] = addrA
param[0]["value"] = "0xA"
param[0]["gasLimit"] = "0x5208"
param[0]["gasPrice"] = "0x1"
txRes := util.Call(t, "eth_sendTransaction", param)
require.Nil(t, txRes.Error)
rpcRes = util.Call(t, "eth_getBlockByNumber", []interface{}{"pending", true})
var postTxPendingBlock map[string]interface{}
err = json.Unmarshal(rpcRes.Result, &postTxPendingBlock)
require.NoError(t, err)
postTxPendingTxs := len(postTxPendingBlock["transactions"].([]interface{}))
require.Greater(t, postTxPendingTxs, preTxPendingTxs)
rpcRes = util.Call(t, "eth_getBlockByNumber", []interface{}{"latest", true})
var postTxLatestBlock map[string]interface{}
err = json.Unmarshal(rpcRes.Result, &postTxLatestBlock)
require.NoError(t, err)
postTxLatestTxs := len(postTxLatestBlock["transactions"].([]interface{}))
require.Equal(t, preTxLatestTxs, postTxLatestTxs)
require.Greater(t, postTxPendingTxs, preTxPendingTxs)
}
func TestEth_Pending_GetTransactionByBlockNumberAndIndex(t *testing.T) {
var pendingTx []*rpctypes.Transaction
resPendingTxs := util.Call(t, "eth_pendingTransactions", []string{})
err := json.Unmarshal(resPendingTxs.Result, &pendingTx)
require.NoError(t, err)
pendingTxCount := len(pendingTx)
data := "0x608060405234801561001057600080fd5b5061011e806100206000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c8063bc9c707d14602d575b600080fd5b603360ab565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101560715780820151818401526020810190506058565b50505050905090810190601f168015609d5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b60606040518060400160405280600681526020017f617261736b61000000000000000000000000000000000000000000000000000081525090509056fea2646970667358221220a31fa4c1ce0b3651fbf5401c511b483c43570c7de4735b5c3b0ad0db30d2573164736f6c63430007050033"
param := make([]map[string]string, 1)
param[0] = make(map[string]string)
param[0]["from"] = "0x" + fmt.Sprintf("%x", from)
param[0]["to"] = addrA
param[0]["value"] = "0xA"
param[0]["gasLimit"] = "0x5208"
param[0]["gasPrice"] = "0x1"
param[0]["data"] = data
txRes := util.Call(t, "eth_sendTransaction", param)
require.Nil(t, txRes.Error)
rpcRes := util.Call(t, "eth_getTransactionByBlockNumberAndIndex", []interface{}{"pending", "0x" + fmt.Sprintf("%X", pendingTxCount)})
var pendingBlockTx map[string]interface{}
err = json.Unmarshal(rpcRes.Result, &pendingBlockTx)
require.NoError(t, err)
// verify the pending tx has all the correct fields from the tx sent.
require.NotEmpty(t, pendingBlockTx["hash"])
require.Equal(t, pendingBlockTx["value"], "0xa")
require.Equal(t, data, pendingBlockTx["input"])
rpcRes = util.Call(t, "eth_getTransactionByBlockNumberAndIndex", []interface{}{"latest", "0x" + fmt.Sprintf("%X", pendingTxCount)})
var latestBlock map[string]interface{}
err = json.Unmarshal(rpcRes.Result, &latestBlock)
require.NoError(t, err)
// verify the pending trasnaction does not exist in the latest block info.
require.Empty(t, latestBlock)
}
func TestEth_Pending_GetTransactionByHash(t *testing.T) {
data := "0x608060405234801561001057600080fd5b5061011e806100206000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c806302eb691b14602d575b600080fd5b603360ab565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101560715780820151818401526020810190506058565b50505050905090810190601f168015609d5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b60606040518060400160405280600d81526020017f617261736b61776173686572650000000000000000000000000000000000000081525090509056fea264697066735822122060917c5c2fab8c058a17afa6d3c1d23a7883b918ea3c7157131ea5b396e1aa7564736f6c63430007050033"
param := make([]map[string]string, 1)
param[0] = make(map[string]string)
param[0]["from"] = "0x" + fmt.Sprintf("%x", from)
param[0]["to"] = addrA
param[0]["value"] = "0xA"
param[0]["gasLimit"] = "0x5208"
param[0]["gasPrice"] = "0x1"
param[0]["data"] = data
txRes := util.Call(t, "eth_sendTransaction", param)
var txHash common.Hash
err := txHash.UnmarshalJSON(txRes.Result)
require.NoError(t, err)
rpcRes := util.Call(t, "eth_getTransactionByHash", []interface{}{txHash})
var pendingBlockTx map[string]interface{}
err = json.Unmarshal(rpcRes.Result, &pendingBlockTx)
require.NoError(t, err)
// verify the pending tx has all the correct fields from the tx sent.
require.NotEmpty(t, pendingBlockTx)
require.NotEmpty(t, pendingBlockTx["hash"])
require.Equal(t, pendingBlockTx["value"], "0xa")
require.Equal(t, pendingBlockTx["input"], data)
}
func TestEth_Pending_SendTransaction_PendingNonce(t *testing.T) {
currNonce := util.GetNonce(t, "latest")
param := make([]map[string]string, 1)
param[0] = make(map[string]string)
param[0]["from"] = "0x" + fmt.Sprintf("%x", from)
param[0]["to"] = addrA
param[0]["value"] = "0xA"
param[0]["gasLimit"] = "0x5208"
param[0]["gasPrice"] = "0x1"
// first transaction
txRes1 := util.Call(t, "eth_sendTransaction", param)
require.Nil(t, txRes1.Error)
pendingNonce1 := util.GetNonce(t, "pending")
require.Greater(t, uint64(pendingNonce1), uint64(currNonce))
// second transaction
param[0]["to"] = "0x7f0f463c4d57b1bd3e3b79051e6c5ab703e803d9"
txRes2 := util.Call(t, "eth_sendTransaction", param)
require.Nil(t, txRes2.Error)
pendingNonce2 := util.GetNonce(t, "pending")
require.Greater(t, uint64(pendingNonce2), uint64(currNonce))
require.Greater(t, uint64(pendingNonce2), uint64(pendingNonce1))
// third transaction
param[0]["to"] = "0x7fb24493808b3f10527e3e0870afeb8a953052d2"
txRes3 := util.Call(t, "eth_sendTransaction", param)
require.Nil(t, txRes3.Error)
pendingNonce3 := util.GetNonce(t, "pending")
require.Greater(t, uint64(pendingNonce3), uint64(currNonce))
require.Greater(t, uint64(pendingNonce3), uint64(pendingNonce2))
}
+265
View File
@@ -0,0 +1,265 @@
package tests
import (
"bytes"
"encoding/hex"
"encoding/json"
"fmt"
"math/big"
"net/http"
"os"
"testing"
"time"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/stretchr/testify/require"
)
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"`
}
var (
HOST = os.Getenv("HOST")
)
func GetAddress() ([]byte, error) {
rpcRes, err := CallWithError("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,
}
}
func Call(t *testing.T, 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 HOST == "" {
HOST = "http://localhost:8545"
}
res, err := http.Post(HOST, "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(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 HOST == "" {
HOST = "http://localhost:8545"
}
res, err := http.Post(HOST, "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
}
// turns a 0x prefixed hex string to a big.Int
func HexToBigInt(t *testing.T, in string) *big.Int {
s := in[2:]
b, err := hex.DecodeString(s)
require.NoError(t, err)
return big.NewInt(0).SetBytes(b)
}
// sendTestTransaction sends a dummy transaction
func SendTestTransaction(t *testing.T, addr []byte) hexutil.Bytes {
param := make([]map[string]string, 1)
param[0] = make(map[string]string)
param[0]["from"] = "0x" + fmt.Sprintf("%x", addr)
param[0]["to"] = "0x1122334455667788990011223344556677889900"
param[0]["value"] = "0x1"
rpcRes := Call(t, "eth_sendTransaction", param)
var hash hexutil.Bytes
err := json.Unmarshal(rpcRes.Result, &hash)
require.NoError(t, err)
return hash
}
// deployTestContract deploys a contract that emits an event in the constructor
func DeployTestContract(t *testing.T, addr []byte) (hexutil.Bytes, map[string]interface{}) {
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]["gas"] = "0x200000"
rpcRes := Call(t, "eth_sendTransaction", param)
var hash hexutil.Bytes
err := json.Unmarshal(rpcRes.Result, &hash)
require.NoError(t, err)
receipt := WaitForReceipt(t, hash)
require.NotNil(t, receipt, "transaction failed")
require.Equal(t, "0x1", receipt["status"].(string))
return hash, receipt
}
func DeployTestContractWithFunction(t *testing.T, addr []byte) hexutil.Bytes {
// pragma solidity ^0.5.1;
// contract Test {
// event Hello(uint256 indexed world);
// event TestEvent(uint256 indexed a, uint256 indexed b);
// uint256 myStorage;
// constructor() public {
// emit Hello(17);
// }
// function test(uint256 a, uint256 b) public {
// myStorage = a;
// emit TestEvent(a, b);
// }
// }
bytecode := "0x608060405234801561001057600080fd5b5060117f775a94827b8fd9b519d36cd827093c664f93347070a554f65e4a6f56cd73889860405160405180910390a260d08061004d6000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c8063eb8ac92114602d575b600080fd5b606060048036036040811015604157600080fd5b8101908080359060200190929190803590602001909291905050506062565b005b8160008190555080827ff3ca124a697ba07e8c5e80bebcfcc48991fc16a63170e8a9206e30508960d00360405160405180910390a3505056fea265627a7a723158201d94d2187aaf3a6790527b615fcc40970febf0385fa6d72a2344848ebd0df3e964736f6c63430005110032"
param := make([]map[string]string, 1)
param[0] = make(map[string]string)
param[0]["from"] = "0x" + fmt.Sprintf("%x", addr)
param[0]["data"] = bytecode
param[0]["gas"] = "0x200000"
rpcRes := Call(t, "eth_sendTransaction", param)
var hash hexutil.Bytes
err := json.Unmarshal(rpcRes.Result, &hash)
require.NoError(t, err)
receipt := WaitForReceipt(t, hash)
require.NotNil(t, receipt, "transaction failed")
require.Equal(t, "0x1", receipt["status"].(string))
return hash
}
//nolint
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{} {
for i := 0; i < 12; i++ {
receipt := GetTransactionReceipt(t, hash)
if receipt != nil {
return receipt
}
time.Sleep(time.Second)
}
return nil
}
func GetNonce(t *testing.T, block string) hexutil.Uint64 {
from, err := GetAddress()
require.NoError(t, err)
param := []interface{}{hexutil.Bytes(from), block}
rpcRes := Call(t, "eth_getTransactionCount", param)
var nonce hexutil.Uint64
err = json.Unmarshal(rpcRes.Result, &nonce)
require.NoError(t, err)
return nonce
}
func UnlockAllAccounts(t *testing.T) {
var accts []common.Address
rpcRes := Call(t, "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, "personal_unlockAccount", []interface{}{acct, ""})
var unlocked bool
err = json.Unmarshal(rpcRes.Result, &unlocked)
require.NoError(t, err)
require.True(t, unlocked)
}
}