mirror of
https://github.com/fluencelabs/tendermint
synced 2025-05-29 06:01:21 +00:00
Merge pull request #75 from tendermint/refactor_rpctests
cleanup rpc tests, use tx_utils
This commit is contained in:
commit
ca8fa5678f
@ -157,7 +157,7 @@ func argsToURLValues(argNames []string, args ...interface{}) (url.Values, error)
|
|||||||
|
|
||||||
/*rpc-gen:imports:
|
/*rpc-gen:imports:
|
||||||
github.com/tendermint/tendermint/binary
|
github.com/tendermint/tendermint/binary
|
||||||
github.com/tendermint/tendermint/rpc/types
|
rpctypes github.com/tendermint/tendermint/rpc/types
|
||||||
net/http
|
net/http
|
||||||
io/ioutil
|
io/ioutil
|
||||||
fmt
|
fmt
|
||||||
|
@ -7,7 +7,7 @@ import (
|
|||||||
"github.com/tendermint/tendermint/account"
|
"github.com/tendermint/tendermint/account"
|
||||||
"github.com/tendermint/tendermint/binary"
|
"github.com/tendermint/tendermint/binary"
|
||||||
ctypes "github.com/tendermint/tendermint/rpc/core/types"
|
ctypes "github.com/tendermint/tendermint/rpc/core/types"
|
||||||
"github.com/tendermint/tendermint/rpc/types"
|
rpctypes "github.com/tendermint/tendermint/rpc/types"
|
||||||
"github.com/tendermint/tendermint/types"
|
"github.com/tendermint/tendermint/types"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
@ -9,6 +9,8 @@ import (
|
|||||||
"github.com/tendermint/tendermint/types"
|
"github.com/tendermint/tendermint/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var wsTyp = "JSONRPC"
|
||||||
|
|
||||||
//--------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------
|
||||||
// Test the websocket service
|
// Test the websocket service
|
||||||
|
|
||||||
@ -52,7 +54,7 @@ func TestWSSend(t *testing.T) {
|
|||||||
amt := uint64(100)
|
amt := uint64(100)
|
||||||
|
|
||||||
con := newWSCon(t)
|
con := newWSCon(t)
|
||||||
eidInput := types.EventStringAccInput(userByteAddr)
|
eidInput := types.EventStringAccInput(user[0].Address)
|
||||||
eidOutput := types.EventStringAccOutput(toAddr)
|
eidOutput := types.EventStringAccOutput(toAddr)
|
||||||
subscribe(t, con, eidInput)
|
subscribe(t, con, eidInput)
|
||||||
subscribe(t, con, eidOutput)
|
subscribe(t, con, eidOutput)
|
||||||
@ -62,7 +64,8 @@ func TestWSSend(t *testing.T) {
|
|||||||
con.Close()
|
con.Close()
|
||||||
}()
|
}()
|
||||||
waitForEvent(t, con, eidInput, true, func() {
|
waitForEvent(t, con, eidInput, true, func() {
|
||||||
broadcastTx(t, "JSONRPC", userByteAddr, toAddr, nil, userBytePriv, amt, 0, 0)
|
tx := makeDefaultSendTxSigned(t, wsTyp, toAddr, amt)
|
||||||
|
broadcastTx(t, wsTyp, tx)
|
||||||
}, unmarshalValidateSend(amt, toAddr))
|
}, unmarshalValidateSend(amt, toAddr))
|
||||||
waitForEvent(t, con, eidOutput, true, func() {}, unmarshalValidateSend(amt, toAddr))
|
waitForEvent(t, con, eidOutput, true, func() {}, unmarshalValidateSend(amt, toAddr))
|
||||||
}
|
}
|
||||||
@ -70,7 +73,7 @@ func TestWSSend(t *testing.T) {
|
|||||||
// ensure events are only fired once for a given transaction
|
// ensure events are only fired once for a given transaction
|
||||||
func TestWSDoubleFire(t *testing.T) {
|
func TestWSDoubleFire(t *testing.T) {
|
||||||
con := newWSCon(t)
|
con := newWSCon(t)
|
||||||
eid := types.EventStringAccInput(userByteAddr)
|
eid := types.EventStringAccInput(user[0].Address)
|
||||||
subscribe(t, con, eid)
|
subscribe(t, con, eid)
|
||||||
defer func() {
|
defer func() {
|
||||||
unsubscribe(t, con, eid)
|
unsubscribe(t, con, eid)
|
||||||
@ -80,7 +83,8 @@ func TestWSDoubleFire(t *testing.T) {
|
|||||||
toAddr := []byte{20, 143, 25, 63, 16, 177, 83, 29, 91, 91, 54, 23, 233, 46, 190, 121, 122, 34, 86, 54}
|
toAddr := []byte{20, 143, 25, 63, 16, 177, 83, 29, 91, 91, 54, 23, 233, 46, 190, 121, 122, 34, 86, 54}
|
||||||
// broadcast the transaction, wait to hear about it
|
// broadcast the transaction, wait to hear about it
|
||||||
waitForEvent(t, con, eid, true, func() {
|
waitForEvent(t, con, eid, true, func() {
|
||||||
broadcastTx(t, "JSONRPC", userByteAddr, toAddr, nil, userBytePriv, amt, 0, 0)
|
tx := makeDefaultSendTxSigned(t, wsTyp, toAddr, amt)
|
||||||
|
broadcastTx(t, wsTyp, tx)
|
||||||
}, func(eid string, b []byte) error {
|
}, func(eid string, b []byte) error {
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
@ -94,18 +98,19 @@ func TestWSDoubleFire(t *testing.T) {
|
|||||||
// create a contract, wait for the event, and send it a msg, validate the return
|
// create a contract, wait for the event, and send it a msg, validate the return
|
||||||
func TestWSCallWait(t *testing.T) {
|
func TestWSCallWait(t *testing.T) {
|
||||||
con := newWSCon(t)
|
con := newWSCon(t)
|
||||||
eid1 := types.EventStringAccInput(userByteAddr)
|
eid1 := types.EventStringAccInput(user[0].Address)
|
||||||
subscribe(t, con, eid1)
|
subscribe(t, con, eid1)
|
||||||
defer func() {
|
defer func() {
|
||||||
unsubscribe(t, con, eid1)
|
unsubscribe(t, con, eid1)
|
||||||
con.Close()
|
con.Close()
|
||||||
}()
|
}()
|
||||||
amt := uint64(10000)
|
amt, gasLim, fee := uint64(10000), uint64(1000), uint64(1000)
|
||||||
code, returnCode, returnVal := simpleContract()
|
code, returnCode, returnVal := simpleContract()
|
||||||
var contractAddr []byte
|
var contractAddr []byte
|
||||||
// wait for the contract to be created
|
// wait for the contract to be created
|
||||||
waitForEvent(t, con, eid1, true, func() {
|
waitForEvent(t, con, eid1, true, func() {
|
||||||
_, receipt := broadcastTx(t, "JSONRPC", userByteAddr, nil, code, userBytePriv, amt, 1000, 1000)
|
tx := makeDefaultCallTx(t, wsTyp, nil, code, amt, gasLim, fee)
|
||||||
|
receipt := broadcastTx(t, wsTyp, tx)
|
||||||
contractAddr = receipt.ContractAddr
|
contractAddr = receipt.ContractAddr
|
||||||
}, unmarshalValidateCall(amt, returnCode))
|
}, unmarshalValidateCall(amt, returnCode))
|
||||||
|
|
||||||
@ -117,9 +122,11 @@ func TestWSCallWait(t *testing.T) {
|
|||||||
unsubscribe(t, con, eid2)
|
unsubscribe(t, con, eid2)
|
||||||
}()
|
}()
|
||||||
// get the return value from a call
|
// get the return value from a call
|
||||||
data := []byte{0x1} // just needs to be non empty for this to be a CallTx
|
data := []byte{0x1}
|
||||||
waitForEvent(t, con, eid2, true, func() {
|
waitForEvent(t, con, eid2, true, func() {
|
||||||
broadcastTx(t, "JSONRPC", userByteAddr, contractAddr, data, userBytePriv, amt, 1000, 1000)
|
tx := makeDefaultCallTx(t, wsTyp, contractAddr, data, amt, gasLim, fee)
|
||||||
|
receipt := broadcastTx(t, wsTyp, tx)
|
||||||
|
contractAddr = receipt.ContractAddr
|
||||||
}, unmarshalValidateCall(amt, returnVal))
|
}, unmarshalValidateCall(amt, returnVal))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -127,10 +134,11 @@ func TestWSCallWait(t *testing.T) {
|
|||||||
// and validate return
|
// and validate return
|
||||||
func TestWSCallNoWait(t *testing.T) {
|
func TestWSCallNoWait(t *testing.T) {
|
||||||
con := newWSCon(t)
|
con := newWSCon(t)
|
||||||
amt := uint64(10000)
|
amt, gasLim, fee := uint64(10000), uint64(1000), uint64(1000)
|
||||||
code, _, returnVal := simpleContract()
|
code, _, returnVal := simpleContract()
|
||||||
|
|
||||||
_, receipt := broadcastTx(t, "JSONRPC", userByteAddr, nil, code, userBytePriv, amt, 1000, 1000)
|
tx := makeDefaultCallTx(t, wsTyp, nil, code, amt, gasLim, fee)
|
||||||
|
receipt := broadcastTx(t, wsTyp, tx)
|
||||||
contractAddr := receipt.ContractAddr
|
contractAddr := receipt.ContractAddr
|
||||||
|
|
||||||
// susbscribe to the new contract
|
// susbscribe to the new contract
|
||||||
@ -142,24 +150,28 @@ func TestWSCallNoWait(t *testing.T) {
|
|||||||
con.Close()
|
con.Close()
|
||||||
}()
|
}()
|
||||||
// get the return value from a call
|
// get the return value from a call
|
||||||
data := []byte{0x1} // just needs to be non empty for this to be a CallTx
|
data := []byte{0x1}
|
||||||
waitForEvent(t, con, eid, true, func() {
|
waitForEvent(t, con, eid, true, func() {
|
||||||
broadcastTx(t, "JSONRPC", userByteAddr, contractAddr, data, userBytePriv, amt, 1000, 1000)
|
tx := makeDefaultCallTx(t, wsTyp, contractAddr, data, amt, gasLim, fee)
|
||||||
|
broadcastTx(t, wsTyp, tx)
|
||||||
}, unmarshalValidateCall(amt, returnVal))
|
}, unmarshalValidateCall(amt, returnVal))
|
||||||
}
|
}
|
||||||
|
|
||||||
// create two contracts, one of which calls the other
|
// create two contracts, one of which calls the other
|
||||||
func TestWSCallCall(t *testing.T) {
|
func TestWSCallCall(t *testing.T) {
|
||||||
con := newWSCon(t)
|
con := newWSCon(t)
|
||||||
amt := uint64(10000)
|
amt, gasLim, fee := uint64(10000), uint64(1000), uint64(1000)
|
||||||
code, _, returnVal := simpleContract()
|
code, _, returnVal := simpleContract()
|
||||||
txid := new([]byte)
|
txid := new([]byte)
|
||||||
|
|
||||||
// deploy the two contracts
|
// deploy the two contracts
|
||||||
_, receipt := broadcastTx(t, "JSONRPC", userByteAddr, nil, code, userBytePriv, amt, 1000, 1000)
|
tx := makeDefaultCallTx(t, wsTyp, nil, code, amt, gasLim, fee)
|
||||||
|
receipt := broadcastTx(t, wsTyp, tx)
|
||||||
contractAddr1 := receipt.ContractAddr
|
contractAddr1 := receipt.ContractAddr
|
||||||
|
|
||||||
code, _, _ = simpleCallContract(contractAddr1)
|
code, _, _ = simpleCallContract(contractAddr1)
|
||||||
_, receipt = broadcastTx(t, "JSONRPC", userByteAddr, nil, code, userBytePriv, amt, 1000, 1000)
|
tx = makeDefaultCallTx(t, wsTyp, nil, code, amt, gasLim, fee)
|
||||||
|
receipt = broadcastTx(t, wsTyp, tx)
|
||||||
contractAddr2 := receipt.ContractAddr
|
contractAddr2 := receipt.ContractAddr
|
||||||
|
|
||||||
// susbscribe to the new contracts
|
// susbscribe to the new contracts
|
||||||
@ -171,7 +183,6 @@ func TestWSCallCall(t *testing.T) {
|
|||||||
con.Close()
|
con.Close()
|
||||||
}()
|
}()
|
||||||
// call contract2, which should call contract1, and wait for ev1
|
// call contract2, which should call contract1, and wait for ev1
|
||||||
data := []byte{0x1} // just needs to be non empty for this to be a CallTx
|
|
||||||
|
|
||||||
// let the contract get created first
|
// let the contract get created first
|
||||||
waitForEvent(t, con, eid1, true, func() {
|
waitForEvent(t, con, eid1, true, func() {
|
||||||
@ -180,7 +191,8 @@ func TestWSCallCall(t *testing.T) {
|
|||||||
})
|
})
|
||||||
// call it
|
// call it
|
||||||
waitForEvent(t, con, eid1, true, func() {
|
waitForEvent(t, con, eid1, true, func() {
|
||||||
tx, _ := broadcastTx(t, "JSONRPC", userByteAddr, contractAddr2, data, userBytePriv, amt, 1000, 1000)
|
tx := makeDefaultCallTx(t, wsTyp, contractAddr2, nil, amt, gasLim, fee)
|
||||||
|
broadcastTx(t, wsTyp, tx)
|
||||||
*txid = account.HashSignBytes(tx)
|
*txid = account.HashSignBytes(tx)
|
||||||
}, unmarshalValidateCallCall(userByteAddr, returnVal, txid))
|
}, unmarshalValidateCallCall(user[0].Address, returnVal, txid))
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,10 @@ package rpctest
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
|
"strconv"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/tendermint/tendermint/account"
|
"github.com/tendermint/tendermint/account"
|
||||||
. "github.com/tendermint/tendermint/common"
|
. "github.com/tendermint/tendermint/common"
|
||||||
"github.com/tendermint/tendermint/consensus"
|
"github.com/tendermint/tendermint/consensus"
|
||||||
@ -12,8 +16,6 @@ import (
|
|||||||
cclient "github.com/tendermint/tendermint/rpc/core_client"
|
cclient "github.com/tendermint/tendermint/rpc/core_client"
|
||||||
"github.com/tendermint/tendermint/state"
|
"github.com/tendermint/tendermint/state"
|
||||||
"github.com/tendermint/tendermint/types"
|
"github.com/tendermint/tendermint/types"
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// global variables for use across all tests
|
// global variables for use across all tests
|
||||||
@ -26,10 +28,9 @@ var (
|
|||||||
|
|
||||||
mempoolCount = 0
|
mempoolCount = 0
|
||||||
|
|
||||||
userAddr = "1D7A91CB32F758A02EBB9BE1FB6F8DEE56F90D42"
|
// make keys
|
||||||
userPriv = "C453604BD6480D5538B4C6FD2E3E314B5BCE518D75ADE4DA3DA85AB8ADFD819606FBAC4E285285D1D91FCBC7E91C780ADA11516F67462340B3980CE2B94940E8"
|
userPriv = "C453604BD6480D5538B4C6FD2E3E314B5BCE518D75ADE4DA3DA85AB8ADFD819606FBAC4E285285D1D91FCBC7E91C780ADA11516F67462340B3980CE2B94940E8"
|
||||||
userPub = "06FBAC4E285285D1D91FCBC7E91C780ADA11516F67462340B3980CE2B94940E8"
|
user = makeUsers(2)
|
||||||
userByteAddr, userBytePriv = initUserBytes()
|
|
||||||
|
|
||||||
clients = map[string]cclient.Client{
|
clients = map[string]cclient.Client{
|
||||||
"JSONRPC": cclient.NewClient(requestAddr, "JSONRPC"),
|
"JSONRPC": cclient.NewClient(requestAddr, "JSONRPC"),
|
||||||
@ -37,22 +38,21 @@ var (
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
// returns byte versions of address and private key
|
func makeUsers(n int) []*account.PrivAccount {
|
||||||
// type [64]byte needed by account.GenPrivAccountFromKey
|
accounts := []*account.PrivAccount{}
|
||||||
func initUserBytes() ([]byte, [64]byte) {
|
for i := 0; i < n; i++ {
|
||||||
byteAddr, _ := hex.DecodeString(userAddr)
|
secret := []byte("mysecret" + strconv.Itoa(i))
|
||||||
|
user := account.GenPrivAccountFromSecret(secret)
|
||||||
|
accounts = append(accounts, user)
|
||||||
|
}
|
||||||
|
|
||||||
|
// include our validator
|
||||||
var byteKey [64]byte
|
var byteKey [64]byte
|
||||||
userPrivByteSlice, _ := hex.DecodeString(userPriv)
|
userPrivByteSlice, _ := hex.DecodeString(userPriv)
|
||||||
copy(byteKey[:], userPrivByteSlice)
|
copy(byteKey[:], userPrivByteSlice)
|
||||||
return byteAddr, byteKey
|
privAcc := account.GenPrivAccountFromKey(byteKey)
|
||||||
}
|
accounts[0] = privAcc
|
||||||
|
return accounts
|
||||||
func decodeHex(hexStr string) []byte {
|
|
||||||
bytes, err := hex.DecodeString(hexStr)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
return bytes
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// create a new node and sleep forever
|
// create a new node and sleep forever
|
||||||
@ -76,9 +76,9 @@ func newNode(ready chan struct{}) {
|
|||||||
func init() {
|
func init() {
|
||||||
// Save new priv_validator file.
|
// Save new priv_validator file.
|
||||||
priv := &state.PrivValidator{
|
priv := &state.PrivValidator{
|
||||||
Address: decodeHex(userAddr),
|
Address: user[0].Address,
|
||||||
PubKey: account.PubKeyEd25519(decodeHex(userPub)),
|
PubKey: account.PubKeyEd25519(user[0].PubKey.(account.PubKeyEd25519)),
|
||||||
PrivKey: account.PrivKeyEd25519(decodeHex(userPriv)),
|
PrivKey: account.PrivKeyEd25519(user[0].PrivKey.(account.PrivKeyEd25519)),
|
||||||
}
|
}
|
||||||
priv.SetFile(config.GetString("priv_validator_file"))
|
priv.SetFile(config.GetString("priv_validator_file"))
|
||||||
priv.Save()
|
priv.Save()
|
||||||
@ -93,70 +93,44 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//-------------------------------------------------------------------------------
|
//-------------------------------------------------------------------------------
|
||||||
// make transactions
|
// some default transaction functions
|
||||||
|
|
||||||
// make a send tx (uses get account to figure out the nonce)
|
func makeDefaultSendTx(t *testing.T, typ string, addr []byte, amt uint64) *types.SendTx {
|
||||||
func makeSendTx(t *testing.T, typ string, from, to []byte, amt uint64) *types.SendTx {
|
nonce := getNonce(t, typ, user[0].Address)
|
||||||
acc := getAccount(t, typ, from)
|
tx := types.NewSendTx()
|
||||||
nonce := 0
|
tx.AddInputWithNonce(user[0].PubKey, amt, nonce)
|
||||||
if acc != nil {
|
tx.AddOutput(addr, amt)
|
||||||
nonce = int(acc.Sequence) + 1
|
|
||||||
}
|
|
||||||
bytePub, err := hex.DecodeString(userPub)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
tx := &types.SendTx{
|
|
||||||
Inputs: []*types.TxInput{
|
|
||||||
&types.TxInput{
|
|
||||||
Address: from,
|
|
||||||
Amount: amt,
|
|
||||||
Sequence: uint(nonce),
|
|
||||||
Signature: account.SignatureEd25519{},
|
|
||||||
PubKey: account.PubKeyEd25519(bytePub),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
Outputs: []*types.TxOutput{
|
|
||||||
&types.TxOutput{
|
|
||||||
Address: to,
|
|
||||||
Amount: amt,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
return tx
|
return tx
|
||||||
}
|
}
|
||||||
|
|
||||||
// make a call tx (uses get account to figure out the nonce)
|
func makeDefaultSendTxSigned(t *testing.T, typ string, addr []byte, amt uint64) *types.SendTx {
|
||||||
func makeCallTx(t *testing.T, typ string, from, to, data []byte, amt, gaslim, fee uint64) *types.CallTx {
|
tx := makeDefaultSendTx(t, typ, addr, amt)
|
||||||
acc := getAccount(t, typ, from)
|
tx.SignInput(0, user[0])
|
||||||
nonce := 0
|
return tx
|
||||||
if acc != nil {
|
}
|
||||||
nonce = int(acc.Sequence) + 1
|
|
||||||
}
|
func makeDefaultCallTx(t *testing.T, typ string, addr, code []byte, amt, gasLim, fee uint64) *types.CallTx {
|
||||||
|
nonce := getNonce(t, typ, user[0].Address)
|
||||||
bytePub, err := hex.DecodeString(userPub)
|
tx := types.NewCallTxWithNonce(user[0].PubKey, addr, code, amt, gasLim, fee, nonce)
|
||||||
if err != nil {
|
tx.Sign(user[0])
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
tx := &types.CallTx{
|
|
||||||
Input: &types.TxInput{
|
|
||||||
Address: from,
|
|
||||||
Amount: amt,
|
|
||||||
Sequence: uint(nonce),
|
|
||||||
Signature: account.SignatureEd25519{},
|
|
||||||
PubKey: account.PubKeyEd25519(bytePub),
|
|
||||||
},
|
|
||||||
Address: to,
|
|
||||||
GasLimit: gaslim,
|
|
||||||
Fee: fee,
|
|
||||||
Data: data,
|
|
||||||
}
|
|
||||||
return tx
|
return tx
|
||||||
}
|
}
|
||||||
|
|
||||||
// make transactions
|
|
||||||
//-------------------------------------------------------------------------------
|
//-------------------------------------------------------------------------------
|
||||||
// rpc call wrappers
|
// rpc call wrappers (fail on err)
|
||||||
|
|
||||||
|
// get an account's nonce
|
||||||
|
func getNonce(t *testing.T, typ string, addr []byte) uint64 {
|
||||||
|
client := clients[typ]
|
||||||
|
ac, err := client.GetAccount(addr)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if ac.Account == nil {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
return uint64(ac.Account.Sequence)
|
||||||
|
}
|
||||||
|
|
||||||
// get the account
|
// get the account
|
||||||
func getAccount(t *testing.T, typ string, addr []byte) *account.Account {
|
func getAccount(t *testing.T, typ string, addr []byte) *account.Account {
|
||||||
@ -168,38 +142,25 @@ func getAccount(t *testing.T, typ string, addr []byte) *account.Account {
|
|||||||
return ac.Account
|
return ac.Account
|
||||||
}
|
}
|
||||||
|
|
||||||
// make and sign transaction
|
// sign transaction
|
||||||
func signTx(t *testing.T, typ string, fromAddr, toAddr, data []byte, key [64]byte, amt, gaslim, fee uint64) (types.Tx, *account.PrivAccount) {
|
func signTx(t *testing.T, typ string, tx types.Tx, privAcc *account.PrivAccount) types.Tx {
|
||||||
var tx types.Tx
|
|
||||||
if data == nil {
|
|
||||||
tx = makeSendTx(t, typ, fromAddr, toAddr, amt)
|
|
||||||
} else {
|
|
||||||
tx = makeCallTx(t, typ, fromAddr, toAddr, data, amt, gaslim, fee)
|
|
||||||
}
|
|
||||||
|
|
||||||
privAcc := account.GenPrivAccountFromKey(key)
|
|
||||||
if bytes.Compare(privAcc.PubKey.Address(), fromAddr) != 0 {
|
|
||||||
t.Fatal("Failed to generate correct priv acc")
|
|
||||||
}
|
|
||||||
|
|
||||||
client := clients[typ]
|
client := clients[typ]
|
||||||
resp, err := client.SignTx(tx, []*account.PrivAccount{privAcc})
|
resp, err := client.SignTx(tx, []*account.PrivAccount{privAcc})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
return resp.Tx, privAcc
|
return resp.Tx
|
||||||
}
|
}
|
||||||
|
|
||||||
// create, sign, and broadcast a transaction
|
// broadcast transaction
|
||||||
func broadcastTx(t *testing.T, typ string, fromAddr, toAddr, data []byte, key [64]byte, amt, gaslim, fee uint64) (types.Tx, ctypes.Receipt) {
|
func broadcastTx(t *testing.T, typ string, tx types.Tx) ctypes.Receipt {
|
||||||
tx, _ := signTx(t, typ, fromAddr, toAddr, data, key, amt, gaslim, fee)
|
|
||||||
client := clients[typ]
|
client := clients[typ]
|
||||||
resp, err := client.BroadcastTx(tx)
|
resp, err := client.BroadcastTx(tx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
mempoolCount += 1
|
mempoolCount += 1
|
||||||
return tx, resp.Receipt
|
return resp.Receipt
|
||||||
}
|
}
|
||||||
|
|
||||||
// dump all storage for an account. currently unused
|
// dump all storage for an account. currently unused
|
||||||
|
@ -3,6 +3,7 @@ package rpctest
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/tendermint/tendermint/account"
|
||||||
. "github.com/tendermint/tendermint/common"
|
. "github.com/tendermint/tendermint/common"
|
||||||
"github.com/tendermint/tendermint/types"
|
"github.com/tendermint/tendermint/types"
|
||||||
"testing"
|
"testing"
|
||||||
@ -32,34 +33,47 @@ func testGenPriv(t *testing.T, typ string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func testGetAccount(t *testing.T, typ string) {
|
func testGetAccount(t *testing.T, typ string) {
|
||||||
acc := getAccount(t, typ, userByteAddr)
|
acc := getAccount(t, typ, user[0].Address)
|
||||||
if acc == nil {
|
if acc == nil {
|
||||||
t.Fatalf("Account was nil")
|
t.Fatalf("Account was nil")
|
||||||
}
|
}
|
||||||
if bytes.Compare(acc.Address, userByteAddr) != 0 {
|
if bytes.Compare(acc.Address, user[0].Address) != 0 {
|
||||||
t.Fatalf("Failed to get correct account. Got %x, expected %x", acc.Address, userByteAddr)
|
t.Fatalf("Failed to get correct account. Got %x, expected %x", acc.Address, user[0].Address)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func testSignedTx(t *testing.T, typ string) {
|
func testSignedTx(t *testing.T, typ string) {
|
||||||
amt := uint64(100)
|
amt := uint64(100)
|
||||||
toAddr := []byte{20, 143, 25, 63, 16, 177, 83, 29, 91, 91, 54, 23, 233, 46, 190, 121, 122, 34, 86, 54}
|
toAddr := []byte{20, 143, 25, 63, 16, 177, 83, 29, 91, 91, 54, 23, 233, 46, 190, 121, 122, 34, 86, 54}
|
||||||
tx, priv := signTx(t, typ, userByteAddr, toAddr, nil, userBytePriv, amt, 0, 0)
|
testOneSignTx(t, typ, toAddr, amt)
|
||||||
checkTx(t, userByteAddr, priv, tx.(*types.SendTx))
|
|
||||||
|
|
||||||
toAddr = []byte{20, 143, 24, 63, 16, 17, 83, 29, 90, 91, 52, 2, 0, 41, 190, 121, 122, 34, 86, 54}
|
toAddr = []byte{20, 143, 24, 63, 16, 17, 83, 29, 90, 91, 52, 2, 0, 41, 190, 121, 122, 34, 86, 54}
|
||||||
tx, priv = signTx(t, typ, userByteAddr, toAddr, nil, userBytePriv, amt, 0, 0)
|
testOneSignTx(t, typ, toAddr, amt)
|
||||||
checkTx(t, userByteAddr, priv, tx.(*types.SendTx))
|
|
||||||
|
|
||||||
toAddr = []byte{0, 0, 4, 0, 0, 4, 0, 0, 4, 91, 52, 2, 0, 41, 190, 121, 122, 34, 86, 54}
|
toAddr = []byte{0, 0, 4, 0, 0, 4, 0, 0, 4, 91, 52, 2, 0, 41, 190, 121, 122, 34, 86, 54}
|
||||||
tx, priv = signTx(t, typ, userByteAddr, toAddr, nil, userBytePriv, amt, 0, 0)
|
testOneSignTx(t, typ, toAddr, amt)
|
||||||
checkTx(t, userByteAddr, priv, tx.(*types.SendTx))
|
}
|
||||||
|
|
||||||
|
func testOneSignTx(t *testing.T, typ string, addr []byte, amt uint64) {
|
||||||
|
tx := makeDefaultSendTx(t, typ, addr, amt)
|
||||||
|
tx2 := signTx(t, typ, tx, user[0])
|
||||||
|
tx2hash := account.HashSignBytes(tx2)
|
||||||
|
tx.SignInput(0, user[0])
|
||||||
|
txhash := account.HashSignBytes(tx)
|
||||||
|
if bytes.Compare(txhash, tx2hash) != 0 {
|
||||||
|
t.Fatal("Got different signatures for signing via rpc vs tx_utils")
|
||||||
|
}
|
||||||
|
|
||||||
|
tx_ := signTx(t, typ, tx, user[0])
|
||||||
|
tx = tx_.(*types.SendTx)
|
||||||
|
checkTx(t, user[0].Address, user[0], tx)
|
||||||
}
|
}
|
||||||
|
|
||||||
func testBroadcastTx(t *testing.T, typ string) {
|
func testBroadcastTx(t *testing.T, typ string) {
|
||||||
amt := uint64(100)
|
amt := uint64(100)
|
||||||
toAddr := []byte{20, 143, 25, 63, 16, 177, 83, 29, 91, 91, 54, 23, 233, 46, 190, 121, 122, 34, 86, 54}
|
toAddr := []byte{20, 143, 25, 63, 16, 177, 83, 29, 91, 91, 54, 23, 233, 46, 190, 121, 122, 34, 86, 54}
|
||||||
tx, receipt := broadcastTx(t, typ, userByteAddr, toAddr, nil, userBytePriv, amt, 0, 0)
|
tx := makeDefaultSendTxSigned(t, typ, toAddr, amt)
|
||||||
|
receipt := broadcastTx(t, typ, tx)
|
||||||
if receipt.CreatesContract > 0 {
|
if receipt.CreatesContract > 0 {
|
||||||
t.Fatal("This tx does not create a contract")
|
t.Fatal("This tx does not create a contract")
|
||||||
}
|
}
|
||||||
@ -90,9 +104,10 @@ func testGetStorage(t *testing.T, typ string) {
|
|||||||
con.Close()
|
con.Close()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
amt := uint64(1100)
|
amt, gasLim, fee := uint64(1100), uint64(1000), uint64(1000)
|
||||||
code := []byte{0x60, 0x5, 0x60, 0x1, 0x55}
|
code := []byte{0x60, 0x5, 0x60, 0x1, 0x55}
|
||||||
_, receipt := broadcastTx(t, typ, userByteAddr, nil, code, userBytePriv, amt, 1000, 1000)
|
tx := makeDefaultCallTx(t, typ, nil, code, amt, gasLim, fee)
|
||||||
|
receipt := broadcastTx(t, typ, tx)
|
||||||
if receipt.CreatesContract == 0 {
|
if receipt.CreatesContract == 0 {
|
||||||
t.Fatal("This tx creates a contract")
|
t.Fatal("This tx creates a contract")
|
||||||
}
|
}
|
||||||
@ -147,9 +162,11 @@ func testCall(t *testing.T, typ string) {
|
|||||||
client := clients[typ]
|
client := clients[typ]
|
||||||
|
|
||||||
// create the contract
|
// create the contract
|
||||||
amt := uint64(6969)
|
amt, gasLim, fee := uint64(6969), uint64(1000), uint64(1000)
|
||||||
code, _, _ := simpleContract()
|
code, _, _ := simpleContract()
|
||||||
_, receipt := broadcastTx(t, typ, userByteAddr, nil, code, userBytePriv, amt, 1000, 1000)
|
tx := makeDefaultCallTx(t, typ, nil, code, amt, gasLim, fee)
|
||||||
|
receipt := broadcastTx(t, typ, tx)
|
||||||
|
|
||||||
if receipt.CreatesContract == 0 {
|
if receipt.CreatesContract == 0 {
|
||||||
t.Fatal("This tx creates a contract")
|
t.Fatal("This tx creates a contract")
|
||||||
}
|
}
|
||||||
|
@ -10,8 +10,8 @@ import (
|
|||||||
|
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
"github.com/tendermint/tendermint/binary"
|
"github.com/tendermint/tendermint/binary"
|
||||||
"github.com/tendermint/tendermint/rpc/types"
|
|
||||||
_ "github.com/tendermint/tendermint/config/tendermint_test"
|
_ "github.com/tendermint/tendermint/config/tendermint_test"
|
||||||
|
"github.com/tendermint/tendermint/rpc/types"
|
||||||
"github.com/tendermint/tendermint/types"
|
"github.com/tendermint/tendermint/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -173,14 +173,14 @@ func unmarshalValidateSend(amt uint64, toAddr []byte) func(string, []byte) error
|
|||||||
return fmt.Errorf("Eventid is not correct. Got %s, expected %s", response.Event, eid)
|
return fmt.Errorf("Eventid is not correct. Got %s, expected %s", response.Event, eid)
|
||||||
}
|
}
|
||||||
tx := response.Data
|
tx := response.Data
|
||||||
if bytes.Compare(tx.Inputs[0].Address, userByteAddr) != 0 {
|
if bytes.Compare(tx.Inputs[0].Address, user[0].Address) != 0 {
|
||||||
return fmt.Errorf("Senders do not match up! Got %x, expected %x", tx.Inputs[0].Address, userByteAddr)
|
return fmt.Errorf("Senders do not match up! Got %x, expected %x", tx.Inputs[0].Address, user[0].Address)
|
||||||
}
|
}
|
||||||
if tx.Inputs[0].Amount != amt {
|
if tx.Inputs[0].Amount != amt {
|
||||||
return fmt.Errorf("Amt does not match up! Got %d, expected %d", tx.Inputs[0].Amount, amt)
|
return fmt.Errorf("Amt does not match up! Got %d, expected %d", tx.Inputs[0].Amount, amt)
|
||||||
}
|
}
|
||||||
if bytes.Compare(tx.Outputs[0].Address, toAddr) != 0 {
|
if bytes.Compare(tx.Outputs[0].Address, toAddr) != 0 {
|
||||||
return fmt.Errorf("Receivers do not match up! Got %x, expected %x", tx.Outputs[0].Address, userByteAddr)
|
return fmt.Errorf("Receivers do not match up! Got %x, expected %x", tx.Outputs[0].Address, user[0].Address)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -210,8 +210,8 @@ func unmarshalValidateCall(amt uint64, returnCode []byte) func(string, []byte) e
|
|||||||
return fmt.Errorf(response.Data.Exception)
|
return fmt.Errorf(response.Data.Exception)
|
||||||
}
|
}
|
||||||
tx := response.Data.Tx
|
tx := response.Data.Tx
|
||||||
if bytes.Compare(tx.Input.Address, userByteAddr) != 0 {
|
if bytes.Compare(tx.Input.Address, user[0].Address) != 0 {
|
||||||
return fmt.Errorf("Senders do not match up! Got %x, expected %x", tx.Input.Address, userByteAddr)
|
return fmt.Errorf("Senders do not match up! Got %x, expected %x", tx.Input.Address, user[0].Address)
|
||||||
}
|
}
|
||||||
if tx.Input.Amount != amt {
|
if tx.Input.Amount != amt {
|
||||||
return fmt.Errorf("Amt does not match up! Got %d, expected %d", tx.Input.Amount, amt)
|
return fmt.Errorf("Amt does not match up! Got %d, expected %d", tx.Input.Amount, amt)
|
||||||
|
@ -36,6 +36,18 @@ func (tx *SendTx) AddInput(st AccountGetter, pubkey account.PubKey, amt uint64)
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (tx *SendTx) AddInputWithNonce(pubkey account.PubKey, amt, nonce uint64) error {
|
||||||
|
addr := pubkey.Address()
|
||||||
|
tx.Inputs = append(tx.Inputs, &TxInput{
|
||||||
|
Address: addr,
|
||||||
|
Amount: amt,
|
||||||
|
Sequence: uint(nonce) + 1,
|
||||||
|
Signature: account.SignatureEd25519{},
|
||||||
|
PubKey: pubkey,
|
||||||
|
})
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (tx *SendTx) AddOutput(addr []byte, amt uint64) error {
|
func (tx *SendTx) AddOutput(addr []byte, amt uint64) error {
|
||||||
tx.Outputs = append(tx.Outputs, &TxOutput{
|
tx.Outputs = append(tx.Outputs, &TxOutput{
|
||||||
Address: addr,
|
Address: addr,
|
||||||
@ -63,10 +75,16 @@ func NewCallTx(st AccountGetter, from account.PubKey, to, data []byte, amt, gasL
|
|||||||
return nil, fmt.Errorf("Invalid address %X from pubkey %X", addr, from)
|
return nil, fmt.Errorf("Invalid address %X from pubkey %X", addr, from)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
nonce := uint64(acc.Sequence)
|
||||||
|
return NewCallTxWithNonce(from, to, data, amt, gasLimit, fee, nonce), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewCallTxWithNonce(from account.PubKey, to, data []byte, amt, gasLimit, fee, nonce uint64) *CallTx {
|
||||||
|
addr := from.Address()
|
||||||
input := &TxInput{
|
input := &TxInput{
|
||||||
Address: addr,
|
Address: addr,
|
||||||
Amount: amt,
|
Amount: amt,
|
||||||
Sequence: uint(acc.Sequence) + 1,
|
Sequence: uint(nonce) + 1,
|
||||||
Signature: account.SignatureEd25519{},
|
Signature: account.SignatureEd25519{},
|
||||||
PubKey: from,
|
PubKey: from,
|
||||||
}
|
}
|
||||||
@ -77,7 +95,7 @@ func NewCallTx(st AccountGetter, from account.PubKey, to, data []byte, amt, gasL
|
|||||||
GasLimit: gasLimit,
|
GasLimit: gasLimit,
|
||||||
Fee: fee,
|
Fee: fee,
|
||||||
Data: data,
|
Data: data,
|
||||||
}, nil
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tx *CallTx) Sign(privAccount *account.PrivAccount) {
|
func (tx *CallTx) Sign(privAccount *account.PrivAccount) {
|
||||||
@ -117,6 +135,18 @@ func (tx *BondTx) AddInput(st AccountGetter, pubkey account.PubKey, amt uint64)
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (tx *BondTx) AddInputWithNonce(pubkey account.PubKey, amt, nonce uint64) error {
|
||||||
|
addr := pubkey.Address()
|
||||||
|
tx.Inputs = append(tx.Inputs, &TxInput{
|
||||||
|
Address: addr,
|
||||||
|
Amount: amt,
|
||||||
|
Sequence: uint(nonce) + 1,
|
||||||
|
Signature: account.SignatureEd25519{},
|
||||||
|
PubKey: pubkey,
|
||||||
|
})
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (tx *BondTx) AddOutput(addr []byte, amt uint64) error {
|
func (tx *BondTx) AddOutput(addr []byte, amt uint64) error {
|
||||||
tx.UnbondTo = append(tx.UnbondTo, &TxOutput{
|
tx.UnbondTo = append(tx.UnbondTo, &TxOutput{
|
||||||
Address: addr,
|
Address: addr,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user