rpc: generalized rpc using reflection on funcs and params

This commit is contained in:
Ethan Buchman
2015-03-27 02:25:41 -07:00
parent 1fb1163721
commit 9aeafffd9b
14 changed files with 436 additions and 405 deletions

View File

@ -0,0 +1,24 @@
{
"Accounts": [
{
"Address": "d7dff9806078899c8da3fe3633cc0bf3c6c2b1bb",
"Amount": 200000000
},
{
"Address": "AC89A6DDF4C309A89A2C4078CE409A5A7B282270",
"Amount": 200000000
}
],
"Validators": [
{
"PubKey": [1, "2239c21c81ea7173a6c489145490c015e05d4b97448933b708a7ec5b7b4921e3"],
"Amount": 1000000,
"UnbondTo": [
{
"Address": "d7dff9806078899c8da3fe3633cc0bf3c6c2b1bb",
"Amount": 100000
}
]
}
]
}

View File

@ -0,0 +1 @@
{"Address":"D7DFF9806078899C8DA3FE3633CC0BF3C6C2B1BB","PubKey":[1,"2239C21C81EA7173A6C489145490C015E05D4B97448933B708A7EC5B7B4921E3"],"PrivKey":[1,"FDE3BD94CB327D19464027BA668194C5EFA46AE83E8419D7542CFF41F00C81972239C21C81EA7173A6C489145490C015E05D4B97448933B708A7EC5B7B4921E3"],"LastHeight":3,"LastRound":0,"LastStep":2}

View File

@ -1,16 +1,19 @@
package rpc
import (
"bytes"
"encoding/hex"
"encoding/json"
"fmt"
"github.com/tendermint/tendermint/binary"
"github.com/tendermint/tendermint/config"
"github.com/tendermint/tendermint/daemon"
"github.com/tendermint/tendermint/p2p"
"github.com/tendermint/tendermint/rpc"
"io/ioutil"
"net/http"
"net/url"
"testing"
"time"
)
var (
@ -18,9 +21,10 @@ var (
requestAddr = "http://" + rpcAddr + "/"
chainId string
node *daemon.Node
userAddr = "D7DFF9806078899C8DA3FE3633CC0BF3C6C2B1BB"
)
func newNode() {
func newNode(ready chan struct{}) {
// Create & start node
node = daemon.NewNode()
l := p2p.NewDefaultListener("tcp", config.App().GetString("ListenAddr"), false)
@ -29,6 +33,7 @@ func newNode() {
// Run the RPC server.
node.StartRpc()
ready <- struct{}{}
// Sleep forever
ch := make(chan struct{})
@ -36,14 +41,19 @@ func newNode() {
}
func init() {
rootDir := ".tendermint"
config.Init(rootDir)
app := config.App()
app.Set("SeedNode", "")
app.Set("DB.Backend", "memdb")
app.Set("RPC.HTTP.ListenAddr", rpcAddr)
app.Set("GenesisFile", rootDir+"/genesis.json")
app.Set("PrivValidatorFile", rootDir+"/priv_validator.json")
config.SetApp(app)
// start a node
go newNode()
time.Sleep(2 * time.Second)
ready := make(chan struct{})
go newNode(ready)
<-ready
}
func TestSayHello(t *testing.T) {
@ -64,7 +74,100 @@ func TestSayHello(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if status.Data.ChainId != node.Switch().GetChainId() {
t.Fatal(fmt.Errorf("ChainId mismatch: got %s expected %s", status.Data.ChainId, node.Switch().GetChainId()))
if status.Data.Network != config.App().GetString("Network") {
t.Fatal(fmt.Errorf("Network mismatch: got %s expected %s", status.Data.Network, config.App().Get("Network")))
}
}
func TestGenPriv(t *testing.T) {
resp, err := http.Get(requestAddr + "unsafe/gen_priv_account")
if err != nil {
t.Fatal(err)
}
if resp.StatusCode != 200 {
t.Fatal(resp)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
var status struct {
Status string
Data rpc.ResponseGenPrivAccount
}
binary.ReadJSON(&status, body, &err)
if err != nil {
t.Fatal(err)
}
if len(status.Data.PrivAccount.Address) == 0 {
t.Fatal("Failed to generate an address")
}
}
func TestGetAccount(t *testing.T) {
byteAddr, _ := hex.DecodeString(userAddr)
resp, err := http.PostForm(requestAddr+"get_account",
url.Values{"address": {string(byteAddr)}})
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
var status struct {
Status string
Data rpc.ResponseGetAccount
}
fmt.Println(string(body))
binary.ReadJSON(&status, body, &err)
if err != nil {
t.Fatal(err)
}
if bytes.Compare(status.Data.Account.Address, byteAddr) != 0 {
t.Fatalf("Failed to get correct account. Got %x, expected %x", status.Data.Account.Address, byteAddr)
}
}
func TestSignedTx(t *testing.T) {
}
/*
acc := mint.MempoolReactor.Mempool.GetState().GetAccount(mint.priv.Address)
nonce := 0
if acc != nil {
nonce = int(acc.Sequence) + 1
}
amtInt, err := strconv.Atoi(amt)
if err != nil {
return "", err
}
amtUint64 := uint64(amtInt)
tx := &blk.SendTx{
Inputs: []*blk.TxInput{
&blk.TxInput{
Address: mint.priv.Address,
Amount: amtUint64,
Sequence: uint(nonce),
Signature: account.SignatureEd25519{},
PubKey: mint.priv.PubKey,
},
},
Outputs: []*blk.TxOutput{
&blk.TxOutput{
Address: addrB,
Amount: amtUint64,
},
},
}
tx.Inputs[0].Signature = mint.priv.PrivKey.Sign(account.SignBytes(tx))
err = mint.MempoolReactor.BroadcastTx(tx)
return hex.EncodeToString(merkle.HashFromBinary(tx)), err
*/