2022-12-16 09:48:38 +00:00
|
|
|
// Copyright 2021 Evmos Foundation
|
|
|
|
// This file is part of Evmos' Ethermint library.
|
|
|
|
//
|
|
|
|
// The Ethermint library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The Ethermint library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the Ethermint library. If not, see https://github.com/evmos/ethermint/blob/main/LICENSE
|
2021-05-11 11:54:55 +00:00
|
|
|
package rpc
|
2020-12-15 19:52:09 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2021-09-05 11:03:06 +00:00
|
|
|
"context"
|
2020-12-15 19:52:09 +00:00
|
|
|
"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"
|
2021-10-07 16:41:27 +00:00
|
|
|
"github.com/pkg/errors"
|
2020-12-15 19:52:09 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Request struct {
|
|
|
|
Version string `json:"jsonrpc"`
|
|
|
|
Method string `json:"method"`
|
|
|
|
Params interface{} `json:"params"`
|
|
|
|
ID int `json:"id"`
|
|
|
|
}
|
|
|
|
|
2021-06-08 07:07:11 +00:00
|
|
|
type Error struct {
|
2020-12-15 19:52:09 +00:00
|
|
|
Code int `json:"code"`
|
|
|
|
Message string `json:"message"`
|
|
|
|
Data interface{} `json:"data,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Response struct {
|
2021-06-08 07:07:11 +00:00
|
|
|
Error *Error `json:"error"`
|
2020-12-15 19:52:09 +00:00
|
|
|
ID int `json:"id"`
|
|
|
|
Result json.RawMessage `json:"result,omitempty"`
|
|
|
|
}
|
|
|
|
|
2021-09-05 11:03:06 +00:00
|
|
|
var HOST = os.Getenv("HOST")
|
2020-12-15 19:52:09 +00:00
|
|
|
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-31 05:04:14 +00:00
|
|
|
func CallWithSleep(t *testing.T, method string, params interface{}, sleep time.Duration) *Response {
|
2020-12-15 19:52:09 +00:00
|
|
|
req, err := json.Marshal(CreateRequest(method, params))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var rpcRes *Response
|
2022-05-31 05:04:14 +00:00
|
|
|
if sleep > 0 {
|
|
|
|
time.Sleep(sleep)
|
|
|
|
}
|
2020-12-15 19:52:09 +00:00
|
|
|
|
2021-10-07 16:41:27 +00:00
|
|
|
httpReq, err := http.NewRequestWithContext(context.Background(), "POST", HOST, bytes.NewBuffer(req))
|
|
|
|
if err != nil {
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
httpReq.Header.Set("Content-Type", "application/json")
|
|
|
|
client := &http.Client{}
|
|
|
|
res, err := client.Do(httpReq)
|
|
|
|
if err != nil {
|
|
|
|
require.NoError(t, errors.Wrap(err, "Could not perform request"))
|
2020-12-15 19:52:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-05-31 05:04:14 +00:00
|
|
|
func Call(t *testing.T, method string, params interface{}) *Response {
|
|
|
|
return CallWithSleep(t, method, params, time.Second)
|
|
|
|
}
|
|
|
|
|
2020-12-15 19:52:09 +00:00
|
|
|
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)
|
|
|
|
|
2021-10-07 16:41:27 +00:00
|
|
|
httpReq, err := http.NewRequestWithContext(context.Background(), "POST", HOST, bytes.NewBuffer(req))
|
2020-12-15 19:52:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-10-07 16:41:27 +00:00
|
|
|
httpReq.Header.Set("Content-Type", "application/json")
|
|
|
|
client := &http.Client{}
|
|
|
|
res, err := client.Do(httpReq)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "Could not perform request")
|
|
|
|
}
|
|
|
|
|
2020-12-15 19:52:09 +00:00
|
|
|
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"
|
2021-04-17 10:00:07 +00:00
|
|
|
|
|
|
|
rpcRes := Call(t, "personal_unlockAccount", []interface{}{param[0]["from"], ""})
|
|
|
|
require.Nil(t, rpcRes.Error)
|
|
|
|
|
|
|
|
rpcRes = Call(t, "eth_sendTransaction", param)
|
|
|
|
require.Nil(t, rpcRes.Error)
|
2020-12-15 19:52:09 +00:00
|
|
|
|
|
|
|
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)
|
2022-08-26 10:30:55 +00:00
|
|
|
param[0]["data"] = "0x6080604052348015600f57600080fd5b5060117f775a94827b8fd9b519d36cd827093c664f93347070a554f65e4a6f56cd73889860405160405180910390a2603580604b6000396000f3fe6080604052600080fdfea165627a7a723058206cab665f0f557620554bb45adf266708d2bd349b8a4314bdff205ee8440e3c240029" //nolint:lll
|
2020-12-15 19:52:09 +00:00
|
|
|
param[0]["gas"] = "0x200000"
|
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
rpcRes := Call(t, "personal_unlockAccount", []interface{}{param[0]["from"], ""})
|
|
|
|
require.Nil(t, rpcRes.Error)
|
|
|
|
|
|
|
|
rpcRes = Call(t, "eth_sendTransaction", param)
|
|
|
|
require.Nil(t, rpcRes.Error)
|
2020-12-15 19:52:09 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
2022-08-26 10:30:55 +00:00
|
|
|
bytecode := "0x608060405234801561001057600080fd5b5060117f775a94827b8fd9b519d36cd827093c664f93347070a554f65e4a6f56cd73889860405160405180910390a260d08061004d6000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c8063eb8ac92114602d575b600080fd5b606060048036036040811015604157600080fd5b8101908080359060200190929190803590602001909291905050506062565b005b8160008190555080827ff3ca124a697ba07e8c5e80bebcfcc48991fc16a63170e8a9206e30508960d00360405160405180910390a3505056fea265627a7a723158201d94d2187aaf3a6790527b615fcc40970febf0385fa6d72a2344848ebd0df3e964736f6c63430005110032" //nolint:lll
|
2020-12-15 19:52:09 +00:00
|
|
|
|
|
|
|
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"
|
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
rpcRes := Call(t, "personal_unlockAccount", []interface{}{param[0]["from"], ""})
|
|
|
|
require.Nil(t, rpcRes.Error)
|
|
|
|
|
|
|
|
rpcRes = Call(t, "eth_sendTransaction", param)
|
2020-12-15 19:52:09 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-11-23 15:00:25 +00:00
|
|
|
func GetGasPrice(t *testing.T) string {
|
|
|
|
gasRes := Call(t, "eth_gasPrice", []interface{}{})
|
|
|
|
|
|
|
|
var gasPrice string
|
|
|
|
err := json.Unmarshal(gasRes.Result, &gasPrice)
|
|
|
|
require.NoError(t, err)
|
|
|
|
return gasPrice
|
|
|
|
}
|
|
|
|
|
2020-12-15 19:52:09 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|