This commit is contained in:
Federico Kunze
2021-04-18 19:23:26 +02:00
parent cb2ab3d95d
commit 6f7470c2e0
29 changed files with 650 additions and 1549 deletions
+345 -121
View File
@@ -1,12 +1,14 @@
// This is a test utility for Ethermint's Web3 JSON-RPC services.
//
// To run these tests please first ensure you have the ethermintd running
// To run these tests please first ensure you have the injectived running
// and have started the RPC service with `injectived rest-server`.
//
// You can configure the desired HOST and MODE as well in integration-test-all.sh
// You can configure the desired HOST and MODE as well
package tests
import (
"bytes"
"encoding/hex"
"encoding/json"
"fmt"
"math/big"
@@ -17,12 +19,11 @@ import (
"github.com/stretchr/testify/require"
rpctypes "github.com/cosmos/ethermint/ethereum/rpc/types"
evmtypes "github.com/cosmos/ethermint/x/evm/types"
ethcmn "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
ethtypes "github.com/ethereum/go-ethereum/core/types"
rpctypes "github.com/cosmos/ethermint/rpc/types"
ethermint "github.com/cosmos/ethermint/types"
)
const (
@@ -31,9 +32,10 @@ const (
)
var (
MODE = os.Getenv("MODE")
from = []byte{}
MODE = os.Getenv("MODE")
zeroString = "0x0"
from = []byte{}
)
func TestMain(m *testing.M) {
@@ -42,8 +44,12 @@ func TestMain(m *testing.M) {
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)
@@ -54,48 +60,115 @@ func TestMain(m *testing.M) {
os.Exit(code)
}
func TestEth_GetTransactionCount(t *testing.T) {
// TODO: this test passes on when run on its own, but fails when run with the other tests
if testing.Short() {
t.Skip("skipping TestEth_GetTransactionCount")
func getAddress() ([]byte, error) {
rpcRes, err := callWithError("eth_accounts", []string{})
if err != nil {
return nil, err
}
prev := GetNonce(t, "latest")
SendTestTransaction(t, from)
time.Sleep(20 * time.Second)
post := GetNonce(t, "latest")
require.Equal(t, prev, post-1)
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)
number := receipt["blockNumber"].(string)
param := []interface{}{number, false}
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))
require.NotEqual(t, big.NewInt(0), lb)
require.Equal(t, hash.String(), block["transactions"].([]interface{})[0])
}
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)
require.NotNil(t, rpcRes)
require.Nil(t, rpcRes.Error)
var logs []*ethtypes.Log
err := json.Unmarshal(rpcRes.Result, &logs)
require.NoError(t, err)
require.Empty(t, logs) // begin, end are -1, crit.Addresses, crit.Topics are empty array, so no log should be returned(run this test before anyone else so that log will be empty)
}
func TestBlockBloom(t *testing.T) {
hash := DeployTestContractWithFunction(t, from)
receipt := WaitForReceipt(t, hash)
number := receipt["blockNumber"].(string)
param := []interface{}{number, false}
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))
require.NotEqual(t, big.NewInt(0), lb)
require.Equal(t, hash.String(), block["transactions"].([]interface{})[0])
call(t, "eth_getLogs", param)
}
func TestEth_GetLogs_Topics_AB(t *testing.T) {
@@ -104,7 +177,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)
@@ -115,10 +188,10 @@ func TestEth_GetLogs_Topics_AB(t *testing.T) {
param[0]["topics"] = []string{helloTopic, worldTopic}
param[0]["fromBlock"] = res.String()
hash := DeployTestContractWithFunction(t, from)
WaitForReceipt(t, hash)
hash := deployTestContractWithFunction(t)
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)
@@ -127,16 +200,28 @@ func TestEth_GetLogs_Topics_AB(t *testing.T) {
require.Equal(t, 1, len(logs))
}
func TestEth_GetTransactionCount(t *testing.T) {
// TODO: this test passes on when run on its own, but fails when run with the other tests
if testing.Short() {
t.Skip("skipping TestEth_GetTransactionCount")
}
prev := getNonce(t)
sendTestTransaction(t)
post := getNonce(t)
require.Equal(t, prev, post-1)
}
func TestEth_GetTransactionLogs(t *testing.T) {
// TODO: this test passes on when run on its own, but fails when run with the other tests
if testing.Short() {
t.Skip("skipping TestEth_GetTransactionLogs")
}
hash, _ := DeployTestContract(t, from)
hash, _ := deployTestContract(t)
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)
@@ -145,9 +230,9 @@ func TestEth_GetTransactionLogs(t *testing.T) {
}
func TestEth_protocolVersion(t *testing.T) {
expectedRes := hexutil.Uint(ethermint.ProtocolVersion)
expectedRes := hexutil.Uint(evmtypes.ProtocolVersion)
rpcRes := Call(t, "eth_protocolVersion", []string{})
rpcRes := call(t, "eth_protocolVersion", []string{})
var res hexutil.Uint
err := res.UnmarshalJSON(rpcRes.Result)
@@ -158,7 +243,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)
@@ -167,7 +252,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)
@@ -178,7 +263,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)
@@ -189,7 +274,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)
@@ -205,8 +290,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}
testKey := 1 // using 0 as key here will create an ethereum empty hash which is considered as invalid hash
rpcRes := Call(t, "eth_getStorageAt", []string{addrA, fmt.Sprint(testKey), "0x3"})
rpcRes := call(t, "eth_getStorageAt", []string{addrA, fmt.Sprint(addrAStoreKey), zeroString})
var storage hexutil.Bytes
err := storage.UnmarshalJSON(rpcRes.Result)
@@ -221,9 +305,8 @@ func TestEth_GetProof(t *testing.T) {
params := make([]interface{}, 3)
params[0] = addrA
params[1] = []string{fmt.Sprint(addrAStoreKey)}
params[2] = "0x3" // queries at height <= 2 are not supported
rpcRes := Call(t, "eth_getProof", params)
params[2] = "latest"
rpcRes := call(t, "eth_getProof", params)
require.NotNil(t, rpcRes)
var accRes rpctypes.AccountResult
@@ -237,7 +320,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)
@@ -257,16 +340,13 @@ func TestEth_SendTransaction_Transfer(t *testing.T) {
param[0]["gasLimit"] = "0x5208"
param[0]["gasPrice"] = "0x55ae82600"
rpcRes := Call(t, "personal_unlockAccount", []interface{}{param[0]["from"], ""})
require.Nil(t, rpcRes.Error)
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))
}
@@ -277,10 +357,7 @@ func TestEth_SendTransaction_ContractDeploy(t *testing.T) {
param[0]["from"] = "0x" + fmt.Sprintf("%x", from)
param[0]["data"] = "0x6080604052348015600f57600080fd5b5060117f775a94827b8fd9b519d36cd827093c664f93347070a554f65e4a6f56cd73889860405160405180910390a2603580604b6000396000f3fe6080604052600080fdfea165627a7a723058206cab665f0f557620554bb45adf266708d2bd349b8a4314bdff205ee8440e3c240029"
rpcRes := Call(t, "personal_unlockAccount", []interface{}{param[0]["from"], ""})
require.Nil(t, rpcRes.Error)
rpcRes = Call(t, "eth_sendTransaction", param)
rpcRes := call(t, "eth_sendTransaction", param)
var hash hexutil.Bytes
err := json.Unmarshal(rpcRes.Result, &hash)
@@ -291,7 +368,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)
@@ -299,7 +376,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)
@@ -307,7 +384,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)
@@ -315,7 +392,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)
@@ -326,13 +403,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)
@@ -340,7 +417,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
@@ -359,13 +436,28 @@ 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, from)
hash := sendTestTransaction(t)
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{})
@@ -376,16 +468,34 @@ func TestEth_GetTransactionReceipt(t *testing.T) {
require.Equal(t, []interface{}{}, receipt["logs"].([]interface{}))
}
func TestEth_GetTransactionReceipt_ContractDeployment(t *testing.T) {
rpcRes := Call(t, "personal_unlockAccount", []interface{}{hexutil.Bytes(from), ""})
require.Nil(t, rpcRes.Error)
// 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"
hash, _ := DeployTestContract(t, from)
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)
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)
@@ -397,8 +507,32 @@ 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)
@@ -410,18 +544,22 @@ 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, from)
deployTestContract(t)
// get filter changes
changesRes := Call(t, "eth_getFilterChanges", []string{ID})
require.Equal(t, 2, len(changesRes.Result))
changesRes := call(t, "eth_getFilterChanges", []string{ID})
var logs []*ethtypes.Log
err = json.Unmarshal(changesRes.Result, &logs)
require.NoError(t, err)
require.Equal(t, 1, len(logs))
}
func TestEth_GetFilterChanges_Addresses(t *testing.T) {
@@ -440,11 +578,51 @@ 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)
@@ -456,20 +634,25 @@ 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, from)
deployTestContractWithFunction(t)
// get filter changes
changesRes := Call(t, "eth_getFilterChanges", []string{ID})
require.Equal(t, 2, len(changesRes.Result))
changesRes := call(t, "eth_getFilterChanges", []string{ID})
var logs []*ethtypes.Log
err = json.Unmarshal(changesRes.Result, &logs)
require.NoError(t, err)
require.Equal(t, 1, len(logs))
}
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)
@@ -481,16 +664,21 @@ 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, from)
deployTestContractWithFunction(t)
// get filter changes
changesRes := Call(t, "eth_getFilterChanges", []string{ID})
require.Equal(t, 2, len(changesRes.Result))
changesRes := call(t, "eth_getFilterChanges", []string{ID})
var logs []*ethtypes.Log
err = json.Unmarshal(changesRes.Result, &logs)
require.NoError(t, err)
require.Equal(t, 1, len(logs))
}
func TestEth_GetFilterChanges_Topics_XXC(t *testing.T) {
@@ -499,20 +687,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, from)
deployTestContractWithFunction(t)
}
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
@@ -522,18 +710,23 @@ func TestEth_PendingTransactionFilter(t *testing.T) {
require.True(t, len(txs) >= 2, "could not get any txs", "changesRes.Result", string(changesRes.Result))
}
func TestEth_EstimateGas(t *testing.T) {
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, "personal_unlockAccount", []interface{}{param[0]["from"], ""})
require.NotNil(t, rpcRes)
rpcRes = Call(t, "eth_estimateGas", param)
rpcRes := call(t, "eth_estimateGas", param)
require.NotNil(t, rpcRes)
require.NotEmpty(t, rpcRes.Result)
@@ -541,28 +734,18 @@ func TestEth_EstimateGas(t *testing.T) {
err := json.Unmarshal(rpcRes.Result, &gas)
require.NoError(t, err, string(rpcRes.Result))
require.Equal(t, "0xfab9", gas)
require.Equal(t, "0xf552", gas)
}
func TestEth_EstimateGas_ContractDeployment(t *testing.T) {
bytecode := "0x608060405234801561001057600080fd5b5060117f775a94827b8fd9b519d36cd827093c664f93347070a554f65e4a6f56cd73889860405160405180910390a260d08061004d6000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c8063eb8ac92114602d575b600080fd5b606060048036036040811015604157600080fd5b8101908080359060200190929190803590602001909291905050506062565b005b8160008190555080827ff3ca124a697ba07e8c5e80bebcfcc48991fc16a63170e8a9206e30508960d00360405160405180910390a3505056fea265627a7a723158201d94d2187aaf3a6790527b615fcc40970febf0385fa6d72a2344848ebd0df3e964736f6c63430005110032"
//deploy contract befor call estimateGas make account nonce changed
hash, _ := DeployTestContract(t, from)
time.Sleep(5 * time.Second)
paramdeploy := []string{hash.String()}
rpcRes := Call(t, "eth_getTransactionReceipt", paramdeploy)
require.Nil(t, rpcRes.Error)
param := make([]map[string]string, 1)
param[0] = make(map[string]string)
param[0]["from"] = "0x" + fmt.Sprintf("%x", from)
param[0]["data"] = bytecode
rpcRes := Call(t, "personal_unlockAccount", []interface{}{param[0]["from"], ""})
require.NotNil(t, rpcRes)
rpcRes = Call(t, "eth_estimateGas", param)
rpcRes := call(t, "eth_estimateGas", param)
require.NotNil(t, rpcRes)
require.NotEmpty(t, rpcRes.Result)
@@ -570,12 +753,53 @@ func TestEth_EstimateGas_ContractDeployment(t *testing.T) {
err := json.Unmarshal(rpcRes.Result, &gas)
require.NoError(t, err, string(rpcRes.Result))
require.Equal(t, "0x1ab8d", gas.String())
require.Equal(t, "0x1c2c4", gas.String())
}
func TestEth_ExportAccount_WithStorage(t *testing.T) {
hash := deployTestContractWithFunction(t)
receipt := waitForReceipt(t, hash)
addr := receipt["contractAddress"].(string)
// call function to set storage
calldata := "0xeb8ac92100000000000000000000000000000000000000000000000000000000000000630000000000000000000000000000000000000000000000000000000000000000"
param := make([]map[string]string, 1)
param[0] = make(map[string]string)
param[0]["from"] = "0x" + fmt.Sprintf("%x", from)
param[0]["to"] = addr
param[0]["data"] = calldata
rpcRes := call(t, "eth_sendTransaction", param)
var txhash hexutil.Bytes
err := json.Unmarshal(rpcRes.Result, &txhash)
require.NoError(t, err)
waitForReceipt(t, txhash)
// get exported account
eap := []string{}
eap = append(eap, addr)
eap = append(eap, "latest")
rpcRes = call(t, "eth_exportAccount", eap)
var res string
err = json.Unmarshal(rpcRes.Result, &res)
require.NoError(t, err)
var account evmtypes.GenesisAccount
err = json.Unmarshal([]byte(res), &account)
require.NoError(t, err)
// deployed bytecode
bytecode := "0x6080604052348015600f57600080fd5b506004361060285760003560e01c8063eb8ac92114602d575b600080fd5b606060048036036040811015604157600080fd5b8101908080359060200190929190803590602001909291905050506062565b005b8160008190555080827ff3ca124a697ba07e8c5e80bebcfcc48991fc16a63170e8a9206e30508960d00360405160405180910390a3505056fea265627a7a723158201d94d2187aaf3a6790527b615fcc40970febf0385fa6d72a2344848ebd0df3e964736f6c63430005110032"
require.Equal(t, addr, account.Address)
require.Equal(t, bytecode, account.Code)
require.NotEqual(t, evmtypes.Storage(nil), account.Storage)
}
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)
+1 -20
View File
@@ -16,7 +16,7 @@ import (
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/stretchr/testify/require"
rpctypes "github.com/cosmos/ethermint/rpc/types"
rpctypes "github.com/cosmos/ethermint/ethereum/rpc/types"
util "github.com/cosmos/ethermint/tests"
)
@@ -30,25 +30,6 @@ var (
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")