2015-03-28 21:22:39 -07:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
2015-04-01 17:30:16 -07:00
|
|
|
. "github.com/tendermint/tendermint/common"
|
2015-04-02 18:11:37 -07:00
|
|
|
"github.com/tendermint/tendermint/config"
|
2015-04-01 17:30:16 -07:00
|
|
|
"github.com/tendermint/tendermint/state"
|
|
|
|
"github.com/tendermint/tendermint/types"
|
2015-03-28 21:22:39 -07:00
|
|
|
"testing"
|
2015-03-31 04:53:34 -07:00
|
|
|
"time"
|
2015-03-28 21:22:39 -07:00
|
|
|
)
|
|
|
|
|
2015-04-02 18:11:37 -07:00
|
|
|
func testStatus(t *testing.T, typ string) {
|
|
|
|
client := clients[typ]
|
2015-04-02 03:06:46 -07:00
|
|
|
resp, err := client.Status()
|
2015-03-28 21:22:39 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-04-02 03:06:46 -07:00
|
|
|
fmt.Println(">>>", resp)
|
2015-04-02 18:11:37 -07:00
|
|
|
if resp.Network != config.App().GetString("Network") {
|
|
|
|
t.Fatal(fmt.Errorf("Network mismatch: got %s expected %s",
|
|
|
|
resp.Network, config.App().Get("Network")))
|
|
|
|
}
|
2015-03-28 21:22:39 -07:00
|
|
|
}
|
|
|
|
|
2015-04-02 18:11:37 -07:00
|
|
|
func testGenPriv(t *testing.T, typ string) {
|
|
|
|
client := clients[typ]
|
2015-04-02 03:06:46 -07:00
|
|
|
resp, err := client.GenPrivAccount()
|
2015-03-28 21:22:39 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-04-02 03:06:46 -07:00
|
|
|
fmt.Println(">>>", resp)
|
2015-04-02 18:11:37 -07:00
|
|
|
if len(resp.PrivAccount.Address) == 0 {
|
|
|
|
t.Fatal("Failed to generate an address")
|
|
|
|
}
|
2015-03-28 21:22:39 -07:00
|
|
|
}
|
|
|
|
|
2015-04-02 18:11:37 -07:00
|
|
|
func testGetAccount(t *testing.T, typ string) {
|
2015-03-28 21:22:39 -07:00
|
|
|
byteAddr, _ := hex.DecodeString(userAddr)
|
2015-04-02 18:11:37 -07:00
|
|
|
acc := getAccount(t, typ, byteAddr)
|
2015-03-30 15:14:33 -07:00
|
|
|
if acc == nil {
|
|
|
|
t.Fatalf("Account was nil")
|
|
|
|
}
|
2015-03-28 21:22:39 -07:00
|
|
|
if bytes.Compare(acc.Address, byteAddr) != 0 {
|
|
|
|
t.Fatalf("Failed to get correct account. Got %x, expected %x", acc.Address, byteAddr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-02 18:11:37 -07:00
|
|
|
func testSignedTx(t *testing.T, typ string) {
|
2015-03-28 21:22:39 -07:00
|
|
|
byteAddr, _ := hex.DecodeString(userAddr)
|
|
|
|
var byteKey [64]byte
|
|
|
|
oh, _ := hex.DecodeString(userPriv)
|
|
|
|
copy(byteKey[:], oh)
|
|
|
|
|
|
|
|
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}
|
2015-04-02 18:11:37 -07:00
|
|
|
tx, priv := signTx(t, typ, byteAddr, toAddr, nil, byteKey, amt, 0, 0)
|
2015-03-31 04:53:34 -07:00
|
|
|
checkTx(t, byteAddr, priv, tx.(*types.SendTx))
|
2015-03-28 21:22:39 -07:00
|
|
|
|
|
|
|
toAddr = []byte{20, 143, 24, 63, 16, 17, 83, 29, 90, 91, 52, 2, 0, 41, 190, 121, 122, 34, 86, 54}
|
2015-04-02 18:11:37 -07:00
|
|
|
tx, priv = signTx(t, typ, byteAddr, toAddr, nil, byteKey, amt, 0, 0)
|
2015-03-31 04:53:34 -07:00
|
|
|
checkTx(t, byteAddr, priv, tx.(*types.SendTx))
|
2015-03-28 21:22:39 -07:00
|
|
|
|
|
|
|
toAddr = []byte{0, 0, 4, 0, 0, 4, 0, 0, 4, 91, 52, 2, 0, 41, 190, 121, 122, 34, 86, 54}
|
2015-04-02 18:11:37 -07:00
|
|
|
tx, priv = signTx(t, typ, byteAddr, toAddr, nil, byteKey, amt, 0, 0)
|
2015-03-31 04:53:34 -07:00
|
|
|
checkTx(t, byteAddr, priv, tx.(*types.SendTx))
|
2015-03-28 21:22:39 -07:00
|
|
|
}
|
|
|
|
|
2015-04-02 18:11:37 -07:00
|
|
|
func testBroadcastTx(t *testing.T, typ string) {
|
2015-03-28 21:22:39 -07:00
|
|
|
byteAddr, _ := hex.DecodeString(userAddr)
|
|
|
|
var byteKey [64]byte
|
|
|
|
oh, _ := hex.DecodeString(userPriv)
|
|
|
|
copy(byteKey[:], oh)
|
|
|
|
|
|
|
|
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}
|
2015-04-02 18:11:37 -07:00
|
|
|
tx, receipt := broadcastTx(t, typ, byteAddr, toAddr, nil, byteKey, amt, 0, 0)
|
2015-03-28 21:22:39 -07:00
|
|
|
if receipt.CreatesContract > 0 {
|
|
|
|
t.Fatal("This tx does not create a contract")
|
|
|
|
}
|
|
|
|
if len(receipt.TxHash) == 0 {
|
|
|
|
t.Fatal("Failed to compute tx hash")
|
|
|
|
}
|
|
|
|
pool := node.MempoolReactor().Mempool
|
|
|
|
txs := pool.GetProposalTxs()
|
2015-03-28 21:38:29 -07:00
|
|
|
if len(txs) != mempoolCount+1 {
|
|
|
|
t.Fatalf("The mem pool has %d txs. Expected %d", len(txs), mempoolCount+1)
|
2015-03-28 21:22:39 -07:00
|
|
|
}
|
2015-03-28 21:38:29 -07:00
|
|
|
tx2 := txs[mempoolCount].(*types.SendTx)
|
|
|
|
mempoolCount += 1
|
2015-04-02 18:11:37 -07:00
|
|
|
n, err := new(int64), new(error)
|
|
|
|
buf1, buf2 := new(bytes.Buffer), new(bytes.Buffer)
|
|
|
|
tx.WriteSignBytes(buf1, n, err)
|
|
|
|
tx2.WriteSignBytes(buf2, n, err)
|
|
|
|
if bytes.Compare(buf1.Bytes(), buf2.Bytes()) != 0 {
|
2015-03-28 21:22:39 -07:00
|
|
|
t.Fatal("inconsistent hashes for mempool tx and sent tx")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-02 18:11:37 -07:00
|
|
|
func testGetStorage(t *testing.T, typ string) {
|
2015-03-31 04:53:34 -07:00
|
|
|
priv := state.LoadPrivValidator(".tendermint/priv_validator.json")
|
|
|
|
_ = priv
|
|
|
|
//core.SetPrivValidator(priv)
|
|
|
|
|
|
|
|
byteAddr, _ := hex.DecodeString(userAddr)
|
|
|
|
var byteKey [64]byte
|
|
|
|
oh, _ := hex.DecodeString(userPriv)
|
|
|
|
copy(byteKey[:], oh)
|
|
|
|
|
|
|
|
amt := uint64(1100)
|
|
|
|
code := []byte{0x60, 0x5, 0x60, 0x1, 0x55}
|
2015-04-02 18:11:37 -07:00
|
|
|
_, receipt := broadcastTx(t, typ, byteAddr, nil, code, byteKey, amt, 1000, 1000)
|
2015-03-31 04:53:34 -07:00
|
|
|
if receipt.CreatesContract == 0 {
|
|
|
|
t.Fatal("This tx creates a contract")
|
|
|
|
}
|
|
|
|
if len(receipt.TxHash) == 0 {
|
|
|
|
t.Fatal("Failed to compute tx hash")
|
|
|
|
}
|
|
|
|
contractAddr := receipt.ContractAddr
|
|
|
|
if len(contractAddr) == 0 {
|
|
|
|
t.Fatal("Creates contract but resulting address is empty")
|
|
|
|
}
|
|
|
|
time.Sleep(time.Second * 20)
|
2015-03-31 15:39:42 -07:00
|
|
|
mempoolCount -= 1
|
2015-03-31 04:53:34 -07:00
|
|
|
|
2015-04-02 18:11:37 -07:00
|
|
|
v := getStorage(t, typ, contractAddr, []byte{0x1})
|
2015-03-31 04:53:34 -07:00
|
|
|
got := RightPadWord256(v)
|
|
|
|
expected := RightPadWord256([]byte{0x5})
|
|
|
|
if got.Compare(expected) != 0 {
|
|
|
|
t.Fatalf("Wrong storage value. Got %x, expected %x", got.Bytes(), expected.Bytes())
|
|
|
|
}
|
|
|
|
}
|