forked from cerc-io/laconicd-deprecated
tests: refactor solidity test cases (#249)
* Refactor * add script to run all tests * Spawn ethermintd in node script * Update README * kill process when test finished * add new test case * add yarn.lock inside tests to be tracked
This commit is contained in:
@@ -1,158 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common/hexutil"
|
||||
)
|
||||
|
||||
var (
|
||||
HOST = os.Getenv("HOST")
|
||||
HOME = os.Getenv("PWD")
|
||||
)
|
||||
|
||||
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 createRequest(method string, params interface{}) Request {
|
||||
return Request{
|
||||
Version: "2.0",
|
||||
Method: method,
|
||||
Params: params,
|
||||
ID: 1,
|
||||
}
|
||||
}
|
||||
|
||||
func getTransactionReceipt(hash hexutil.Bytes) (map[string]interface{}, error) {
|
||||
param := []string{hash.String()}
|
||||
|
||||
rpcRes, err := call("eth_getTransactionReceipt", param)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
receipt := make(map[string]interface{})
|
||||
|
||||
err = json.Unmarshal(rpcRes.Result, &receipt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return receipt, nil
|
||||
}
|
||||
|
||||
func waitForReceipt(hash hexutil.Bytes) (map[string]interface{}, error) {
|
||||
for i := 0; i < 10; i++ {
|
||||
receipt, err := getTransactionReceipt(hash)
|
||||
if receipt != nil {
|
||||
return receipt, err
|
||||
} else if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
|
||||
return nil, errors.New("cound not find transaction on chain")
|
||||
}
|
||||
|
||||
func call(method string, params interface{}) (*Response, error) {
|
||||
var rpcRes *Response
|
||||
|
||||
if HOST == "" {
|
||||
HOST = "http://localhost:8545"
|
||||
}
|
||||
|
||||
req, err := json.Marshal(createRequest(method, params))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
time.Sleep(1000000 * time.Nanosecond)
|
||||
/* #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)
|
||||
|
||||
if err := decoder.Decode(&rpcRes); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := res.Body.Close(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return rpcRes, nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
var hash hexutil.Bytes
|
||||
|
||||
dat, err := ioutil.ReadFile(HOME + "/counter/counter_sol.bin")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
param := make([]map[string]string, 1)
|
||||
param[0] = make(map[string]string)
|
||||
param[0]["from"] = os.Args[1]
|
||||
param[0]["data"] = "0x" + string(dat)
|
||||
|
||||
txRPCRes, err := call("eth_sendTransaction", param)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(txRPCRes.Result, &hash); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
fmt.Println("Contract TX hash: ", hash)
|
||||
|
||||
receipt, err := waitForReceipt(hash)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
/*
|
||||
//test for bad hash
|
||||
testhash, err := hexutil.Decode("0xe146d95c74a48e730bf825c2a3dcbce8122b8a463bc15bcbb38b9c195402f0a5")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
receipt, err := waitForReceipt(testhash)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
*/
|
||||
|
||||
fmt.Println("receipt: ", receipt)
|
||||
}
|
||||
@@ -9,9 +9,7 @@
|
||||
},
|
||||
"devDependencies": {
|
||||
"truffle": "^5.1.42",
|
||||
"truffle-assertions": "^0.9.2",
|
||||
"web3": "^1.2.11"
|
||||
},
|
||||
"dependencies": {
|
||||
"truffle": "^5.1.43"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,25 @@
|
||||
const Counter = artifacts.require("Counter")
|
||||
const truffleAssert = require('truffle-assertions');
|
||||
|
||||
contract('Counter', ([one, two, three]) => {
|
||||
console.log(one, two, three)
|
||||
async function expectRevert(promise) {
|
||||
try {
|
||||
await promise;
|
||||
} catch (error) {
|
||||
if (error.message.indexOf('revert') === -1) {
|
||||
expect('revert').to.equal(error.message, 'Wrong kind of exception received');
|
||||
}
|
||||
return;
|
||||
}
|
||||
expect.fail('Expected an exception but none was received');
|
||||
}
|
||||
|
||||
contract('Counter', (accounts) => {
|
||||
console.log(`Using Accounts (${accounts.length}): \n${accounts.join('\n')}`);
|
||||
console.log('==========================\n');
|
||||
const [one, two, three] = accounts;
|
||||
let counter
|
||||
|
||||
beforeEach(async() => {
|
||||
beforeEach(async () => {
|
||||
counter = await Counter.new()
|
||||
console.log('Counter:', counter.address)
|
||||
|
||||
@@ -15,7 +30,7 @@ contract('Counter', ([one, two, three]) => {
|
||||
console.log('')
|
||||
})
|
||||
|
||||
it('should add', async() => {
|
||||
it('should add', async () => {
|
||||
const balanceOne = await web3.eth.getBalance(one)
|
||||
const balanceTwo = await web3.eth.getBalance(two)
|
||||
const balanceThree = await web3.eth.getBalance(three)
|
||||
@@ -41,7 +56,7 @@ contract('Counter', ([one, two, three]) => {
|
||||
assert.notEqual(balanceThree, await web3.eth.getBalance(three), `${three}'s balance should be different`)
|
||||
})
|
||||
|
||||
it('should subtract', async() => {
|
||||
it('should subtract', async () => {
|
||||
let count
|
||||
|
||||
await counter.add()
|
||||
@@ -68,13 +83,7 @@ contract('Counter', ([one, two, three]) => {
|
||||
assert.equal(allEvents.length, 3)
|
||||
assert.equal(changedEvents.length, 2)
|
||||
|
||||
try {
|
||||
await counter.subtract()
|
||||
assert.fail('Subtracting past 0 should have reverted')
|
||||
} catch (err) {
|
||||
console.log()
|
||||
console.log('Passed -- was expecting error')
|
||||
console.log('Error:', err)
|
||||
}
|
||||
await expectRevert(counter.subtract());
|
||||
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user