aboutsummaryrefslogtreecommitdiffstats
path: root/rpc
diff options
context:
space:
mode:
Diffstat (limited to 'rpc')
-rw-r--r--rpc/messages.go6
-rw-r--r--rpc/responses_test.go128
2 files changed, 117 insertions, 17 deletions
diff --git a/rpc/messages.go b/rpc/messages.go
index 1ad41654b..42af53c58 100644
--- a/rpc/messages.go
+++ b/rpc/messages.go
@@ -50,8 +50,12 @@ func newHexData(input interface{}) *hexdata {
d.data = input.([]byte)
case common.Hash:
d.data = input.(common.Hash).Bytes()
+ case *common.Hash:
+ d.data = input.(*common.Hash).Bytes()
case common.Address:
d.data = input.(common.Address).Bytes()
+ case *common.Address:
+ d.data = input.(*common.Address).Bytes()
case *big.Int:
d.data = input.(*big.Int).Bytes()
case int64:
@@ -62,7 +66,7 @@ func newHexData(input interface{}) *hexdata {
d.data = big.NewInt(int64(input.(int))).Bytes()
case uint:
d.data = big.NewInt(int64(input.(uint))).Bytes()
- case string:
+ case string: // hexstring
d.data = common.Big(input.(string)).Bytes()
default:
d.data = nil
diff --git a/rpc/responses_test.go b/rpc/responses_test.go
index 8c1e2873f..5c6c6a895 100644
--- a/rpc/responses_test.go
+++ b/rpc/responses_test.go
@@ -1,7 +1,10 @@
package rpc
import (
+ "encoding/json"
+ "fmt"
"math/big"
+ "regexp"
"testing"
"github.com/ethereum/go-ethereum/common"
@@ -9,6 +12,15 @@ import (
"github.com/ethereum/go-ethereum/core/types"
)
+const (
+ reHash = `"0x[0-9a-f]{64}"` // 32 bytes
+ reHashOpt = `"(0x[0-9a-f]{64})"|null` // 32 bytes or null
+ reAddress = `"0x[0-9a-f]{40}"` // 20 bytes
+ reAddressOpt = `"0x[0-9a-f]{40}"|null` // 20 bytes or null
+ reNum = `"0x([1-9a-f][0-9a-f]{1,15})|0"` // must not have left-padded zeros
+ reData = `"0x[0-9a-f]*"` // can be "empty"
+)
+
func TestNewBlockRes(t *testing.T) {
parentHash := common.HexToHash("0x01")
coinbase := common.HexToAddress("0x01")
@@ -17,8 +29,35 @@ func TestNewBlockRes(t *testing.T) {
nonce := uint64(1)
extra := ""
block := types.NewBlock(parentHash, coinbase, root, difficulty, nonce, extra)
+ tests := map[string]string{
+ "number": reNum,
+ "hash": reHash,
+ "parentHash": reHash,
+ "nonce": reNum,
+ "sha3Uncles": reHash,
+ "logsBloom": reData,
+ "transactionRoot": reHash,
+ "stateRoot": reHash,
+ "miner": reAddress,
+ "difficulty": `"0x1"`,
+ "totalDifficulty": reNum,
+ "size": reNum,
+ "extraData": reData,
+ "gasLimit": reNum,
+ // "minGasPrice": "0x",
+ "gasUsed": reNum,
+ "timestamp": reNum,
+ }
- _ = NewBlockRes(block)
+ v := NewBlockRes(block)
+ j, _ := json.Marshal(v)
+
+ for k, re := range tests {
+ match, _ := regexp.MatchString(fmt.Sprintf(`{.*"%s":%s.*}`, k, re), string(j))
+ if !match {
+ t.Error(fmt.Sprintf("%s output json does not match format %s. Got %s", k, re, j))
+ }
+ }
}
func TestNewTransactionRes(t *testing.T) {
@@ -29,10 +68,80 @@ func TestNewTransactionRes(t *testing.T) {
data := []byte{1, 2, 3}
tx := types.NewTransactionMessage(to, amount, gasAmount, gasPrice, data)
- _ = NewTransactionRes(tx)
+ tests := map[string]string{
+ "hash": reHash,
+ "nonce": reNum,
+ "blockHash": reHash,
+ "blockNum": reNum,
+ "transactionIndex": reNum,
+ "from": reAddress,
+ "to": reAddressOpt,
+ "value": reNum,
+ "gas": reNum,
+ "gasPrice": reNum,
+ "input": reData,
+ }
+
+ v := NewTransactionRes(tx)
+ v.BlockHash = newHexData(common.HexToHash("0x030201"))
+ v.BlockNumber = newHexNum(5)
+ v.TxIndex = newHexNum(0)
+ j, _ := json.Marshal(v)
+ for k, re := range tests {
+ match, _ := regexp.MatchString(fmt.Sprintf(`{.*"%s":%s.*}`, k, re), string(j))
+ if !match {
+ t.Error(fmt.Sprintf("`%s` output json does not match format %s. Source %s", k, re, j))
+ }
+ }
+
}
-func MakeStateLog(num int) state.Log {
+func TestNewLogRes(t *testing.T) {
+ log := makeStateLog(0)
+ tests := map[string]string{
+ "address": reAddress,
+ // "topics": "[.*]"
+ "data": reData,
+ "blockNumber": reNum,
+ // "hash": reHash,
+ // "logIndex": reNum,
+ // "blockHash": reHash,
+ // "transactionHash": reHash,
+ "transactionIndex": reNum,
+ }
+
+ v := NewLogRes(log)
+ j, _ := json.Marshal(v)
+
+ for k, re := range tests {
+ match, _ := regexp.MatchString(fmt.Sprintf(`{.*"%s":%s.*}`, k, re), string(j))
+ if !match {
+ t.Error(fmt.Sprintf("`%s` output json does not match format %s. Got %s", k, re, j))
+ }
+ }
+
+}
+
+func TestNewLogsRes(t *testing.T) {
+ logs := make([]state.Log, 3)
+ logs[0] = makeStateLog(1)
+ logs[1] = makeStateLog(2)
+ logs[2] = makeStateLog(3)
+ tests := map[string]string{}
+
+ v := NewLogsRes(logs)
+ j, _ := json.Marshal(v)
+
+ for k, re := range tests {
+ match, _ := regexp.MatchString(fmt.Sprintf(`[{.*"%s":%s.*}]`, k, re), string(j))
+ if !match {
+ t.Error(fmt.Sprintf("%s output json does not match format %s. Got %s", k, re, j))
+ }
+ }
+
+}
+
+func makeStateLog(num int) state.Log {
address := common.HexToAddress("0x0")
data := []byte{1, 2, 3}
number := uint64(num)
@@ -43,16 +152,3 @@ func MakeStateLog(num int) state.Log {
log := state.NewLog(address, topics, data, number)
return log
}
-
-func TestNewLogRes(t *testing.T) {
- log := MakeStateLog(0)
- _ = NewLogRes(log)
-}
-
-func TestNewLogsRes(t *testing.T) {
- logs := make([]state.Log, 3)
- logs[0] = MakeStateLog(1)
- logs[1] = MakeStateLog(2)
- logs[2] = MakeStateLog(3)
- _ = NewLogsRes(logs)
-}