From 05af306a8d0ec3037c3688d6195afb5caef5232b Mon Sep 17 00:00:00 2001 From: Ethan Buchman Date: Thu, 3 Mar 2016 06:18:11 +0000 Subject: [PATCH] rpc: unsafe_set_config --- rpc/core/mempool.go | 5 --- rpc/core/routes.go | 14 +++++---- rpc/core/status.go | 28 +++++++++++++++++ rpc/core/types/responses.go | 6 ++-- rpc/test/client_test.go | 61 +++++++++++++++++++++++++++++++++++++ 5 files changed, 100 insertions(+), 14 deletions(-) diff --git a/rpc/core/mempool.go b/rpc/core/mempool.go index 8eca1883e..61bf08a56 100644 --- a/rpc/core/mempool.go +++ b/rpc/core/mempool.go @@ -39,8 +39,3 @@ func UnconfirmedTxs() (*ctypes.ResultUnconfirmedTxs, error) { txs := mempoolReactor.Mempool.Reap() return &ctypes.ResultUnconfirmedTxs{len(txs), txs}, nil } - -func TestStartMempool() (*ctypes.ResultTestStartMempool, error) { - config.Set("mempool_reap", true) - return &ctypes.ResultTestStartMempool{}, nil -} diff --git a/rpc/core/routes.go b/rpc/core/routes.go index 1d6eaac17..53604c0fc 100644 --- a/rpc/core/routes.go +++ b/rpc/core/routes.go @@ -7,8 +7,10 @@ import ( ) var Routes = map[string]*rpc.RPCFunc{ - "subscribe": rpc.NewWSRPCFunc(SubscribeResult, "event"), - "unsubscribe": rpc.NewWSRPCFunc(UnsubscribeResult, "event"), + // subscribe/unsubscribe are reserved for websocket events. + "subscribe": rpc.NewWSRPCFunc(SubscribeResult, "event"), + "unsubscribe": rpc.NewWSRPCFunc(UnsubscribeResult, "event"), + "status": rpc.NewRPCFunc(StatusResult, ""), "net_info": rpc.NewRPCFunc(NetInfoResult, ""), "dial_seeds": rpc.NewRPCFunc(DialSeedsResult, "seeds"), @@ -20,8 +22,8 @@ var Routes = map[string]*rpc.RPCFunc{ "broadcast_tx_sync": rpc.NewRPCFunc(BroadcastTxSyncResult, "tx"), "broadcast_tx_async": rpc.NewRPCFunc(BroadcastTxAsyncResult, "tx"), "unconfirmed_txs": rpc.NewRPCFunc(UnconfirmedTxsResult, ""), - "test_start_mempool": rpc.NewRPCFunc(TestStartMempoolResult, ""), // move to test server ? - // subscribe/unsubscribe are reserved for websocket events. + + "unsafe_set_config": rpc.NewRPCFunc(UnsafeSetConfigResult, "type,key,value"), } func SubscribeResult(wsCtx rpctypes.WSRPCContext, event string) (ctypes.TMResult, error) { @@ -128,8 +130,8 @@ func BroadcastTxAsyncResult(tx []byte) (ctypes.TMResult, error) { } } -func TestStartMempoolResult() (ctypes.TMResult, error) { - if r, err := TestStartMempool(); err != nil { +func UnsafeSetConfigResult(typ, key, value string) (ctypes.TMResult, error) { + if r, err := UnsafeSetConfig(typ, key, value); err != nil { return nil, err } else { return r, nil diff --git a/rpc/core/status.go b/rpc/core/status.go index bf3d69ffe..8f056fd4e 100644 --- a/rpc/core/status.go +++ b/rpc/core/status.go @@ -1,6 +1,9 @@ package core import ( + "fmt" + "strconv" + ctypes "github.com/tendermint/tendermint/rpc/core/types" "github.com/tendermint/tendermint/types" ) @@ -28,3 +31,28 @@ func Status() (*ctypes.ResultStatus, error) { LatestBlockHeight: latestHeight, LatestBlockTime: latestBlockTime}, nil } + +func UnsafeSetConfig(typ, key, value string) (*ctypes.ResultUnsafeSetConfig, error) { + switch typ { + case "string": + config.Set(key, value) + case "int": + val, err := strconv.Atoi(value) + if err != nil { + return nil, fmt.Errorf("non-integer value found. key:%s; value:%s; err:%v", key, value, err) + } + config.Set(key, val) + case "bool": + switch value { + case "true": + config.Set(key, true) + case "false": + config.Set(key, false) + default: + return nil, fmt.Errorf("bool value must be true or false. got %s", value) + } + default: + return nil, fmt.Errorf("Unknown type %s", typ) + } + return &ctypes.ResultUnsafeSetConfig{}, nil +} diff --git a/rpc/core/types/responses.go b/rpc/core/types/responses.go index 78e722e38..660c6e0c1 100644 --- a/rpc/core/types/responses.go +++ b/rpc/core/types/responses.go @@ -68,7 +68,7 @@ type ResultUnconfirmedTxs struct { Txs []types.Tx `json:"txs"` } -type ResultTestStartMempool struct{} +type ResultUnsafeSetConfig struct{} type ResultSubscribe struct { } @@ -109,7 +109,7 @@ const ( ResultTypeEvent = byte(0x82) // 0xa bytes for testing - ResultTypeTestStartMempool = byte(0xa0) + ResultTypeUnsafeSetConfig = byte(0xa0) ) type TMResult interface { @@ -132,5 +132,5 @@ var _ = wire.RegisterInterface( wire.ConcreteType{&ResultSubscribe{}, ResultTypeSubscribe}, wire.ConcreteType{&ResultUnsubscribe{}, ResultTypeUnsubscribe}, wire.ConcreteType{&ResultEvent{}, ResultTypeEvent}, - wire.ConcreteType{&ResultTestStartMempool{}, ResultTypeTestStartMempool}, + wire.ConcreteType{&ResultUnsafeSetConfig{}, ResultTypeUnsafeSetConfig}, ) diff --git a/rpc/test/client_test.go b/rpc/test/client_test.go index 64f557586..4b5b66db4 100644 --- a/rpc/test/client_test.go +++ b/rpc/test/client_test.go @@ -11,6 +11,10 @@ import ( //-------------------------------------------------------------------------------- // Test the HTTP client +//-------------------------------------------------------------------------------- + +//-------------------------------------------------------------------------------- +// status func TestURIStatus(t *testing.T) { tmResult := new(ctypes.TMResult) @@ -39,6 +43,63 @@ func testStatus(t *testing.T, statusI interface{}) { } } +//-------------------------------------------------------------------------------- +// unsafe_set_config + +var stringVal = "my string" +var intVal = 987654321 +var boolVal = true + +// don't change these +var testCasesUnsafeSetConfig = [][]string{ + []string{"string", "key1", stringVal}, + []string{"int", "key2", fmt.Sprintf("%v", intVal)}, + []string{"bool", "key3", fmt.Sprintf("%v", boolVal)}, +} + +func TestURIUnsafeSetConfig(t *testing.T) { + for _, testCase := range testCasesUnsafeSetConfig { + tmResult := new(ctypes.TMResult) + _, err := clientURI.Call("unsafe_set_config", map[string]interface{}{ + "type": testCase[0], + "key": testCase[1], + "value": testCase[2], + }, tmResult) + if err != nil { + t.Fatal(err) + } + } + testUnsafeSetConfig(t) +} + +func TestJSONUnsafeSetConfig(t *testing.T) { + for _, testCase := range testCasesUnsafeSetConfig { + tmResult := new(ctypes.TMResult) + _, err := clientJSON.Call("unsafe_set_config", []interface{}{testCase[0], testCase[1], testCase[2]}, tmResult) + if err != nil { + t.Fatal(err) + } + } + testUnsafeSetConfig(t) +} + +func testUnsafeSetConfig(t *testing.T) { + s := config.GetString("key1") + if s != stringVal { + t.Fatalf("got %v, expected %v", s, stringVal) + } + + i := config.GetInt("key2") + if i != intVal { + t.Fatalf("got %v, expected %v", i, intVal) + } + + b := config.GetBool("key3") + if b != boolVal { + t.Fatalf("got %v, expected %v", b, boolVal) + } +} + /*func TestURIBroadcastTx(t *testing.T) { testBroadcastTx(t, "HTTP") }*/