Skip to content

Commit

Permalink
fix: jsonrpc to support both positional parameters and named paramete…
Browse files Browse the repository at this point in the history
…rs (#16)

#15
  • Loading branch information
baabeetaa authored Apr 6, 2023
1 parent 069a296 commit eb4617f
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 17 deletions.
2 changes: 1 addition & 1 deletion config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ upstream:
func TestGetBackendNodeType(t *testing.T) {
b := Backend{
Rpc: "http://pruned",
Blocks: []int{10},
Blocks: []int64{10},
}

assert.Equal(t, GetBackendNodeType(&b), BackendNodeTypePruned)
Expand Down
60 changes: 46 additions & 14 deletions rpc_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,13 @@ func StartRpcServer() {

m0 := j0.(map[string]interface{})
method := m0["method"].(string)
params := m0["params"].([]interface{})
//params := m0["params"].([]interface{})

fmt.Printf("method=%s, params=%+v\n", method, params)
fmt.Printf("method=%s, params=%+v\n", method, m0["params"])

// note that params could be positional parameters or named parameters
// eg., "params": [ "9045128", "1", "30" ]
// or "params": { "height": "9045128", "page": "1", "per_page": "30" }

if method == "abci_info" ||
strings.HasPrefix(method, "broadcast_") ||
Expand All @@ -105,14 +109,28 @@ func StartRpcServer() {
method == "consensus_params" ||
method == "validators" {

// height is 1st param
if len(params) < 1 {
SendError(w)
height := int64(0)

if positionalParams, ok := m0["params"].([]interface{}); ok { // positional parameters
// height is 1st param
if len(positionalParams) < 1 {
SendError(w)
}

heightParam := positionalParams[0].(string)
height, err = strconv.ParseInt(heightParam, 10, 64)
if err != nil {
SendError(w)
}
} else if namedParams, ok := m0["params"].(map[string]interface{}); ok { // named parameters
heightParam := namedParams["height"].(string)
height, err = strconv.ParseInt(heightParam, 10, 64)
if err != nil {
SendError(w)
}
}

heightParam := params[0].(string)
height, err := strconv.ParseInt(heightParam, 10, 64)
if err != nil {
if height == 0 {
SendError(w)
}

Expand All @@ -123,14 +141,28 @@ func StartRpcServer() {

selectedHost = node.Backend.Rpc
} else if method == "abci_query" {
// height is 3rd param
if len(params) < 3 {
SendError(w)
height := int64(0)

if positionalParams, ok := m0["params"].([]interface{}); ok { // positional parameters
// height is 3rd param
if len(positionalParams) < 3 {
SendError(w)
}

heightParam := positionalParams[2].(string)
height, err = strconv.ParseInt(heightParam, 10, 64)
if err != nil {
SendError(w)
}
} else if namedParams, ok := m0["params"].(map[string]interface{}); ok { // named parameters
heightParam := namedParams["height"].(string)
height, err = strconv.ParseInt(heightParam, 10, 64)
if err != nil {
SendError(w)
}
}

heightParam := params[2].(string)
height, err := strconv.ParseInt(heightParam, 10, 64)
if err != nil {
if height == 0 {
SendError(w)
}

Expand Down
26 changes: 26 additions & 0 deletions rpc_server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package main

import (
"encoding/json"
"fmt"
"github.com/stretchr/testify/assert"
"reflect"
"testing"
)

func TestJsonRpcParams(t *testing.T) {
jsonText := []byte(`{"jsonrpc": "2.0", "id": "0", "method": "validators", "params": { "height": "9045128", "page": "1", "per_page": "30" }}`)
//jsonText := []byte(`{"jsonrpc": "2.0", "id": "0", "method": "validators", "params": ["9045128", "1", "30"]}`)

var j0 interface{}
err := json.Unmarshal(jsonText, &j0)
assert.NoError(t, err)

m0 := j0.(map[string]interface{})
//method := m0["method"].(string)
//params := m0["params"].([]interface{})
//
//fmt.Printf("method=%s, params=%+v\n", method, params)

fmt.Println(reflect.TypeOf(m0["params"]))
}
4 changes: 2 additions & 2 deletions state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (
func TestSelectPrunedNode(t *testing.T) {
pruned := Backend{
Rpc: "http://pruned",
Blocks: []int{10},
Blocks: []int64{10},
}
archive := Backend{
Rpc: "http://archive",
Blocks: []int{},
Blocks: []int64{},
}

cfg = &Config{
Expand Down

0 comments on commit eb4617f

Please sign in to comment.