aboutsummaryrefslogtreecommitdiffstats
path: root/rpc
diff options
context:
space:
mode:
authorobscuren <geffobscura@gmail.com>2015-04-07 21:04:29 +0800
committerobscuren <geffobscura@gmail.com>2015-04-07 21:04:29 +0800
commit3063aad7dbe5b20d8af51a907b1d673f36331354 (patch)
treec2755b55e9a7e68c4fcd900b4d418fefbd7662ce /rpc
parent50aa1f178c71a4ff6c59888fcb9e78ff8a0abacd (diff)
parentd0c3f127eee44274f72377085535d4c25e8b0f55 (diff)
downloadgo-tangerine-3063aad7dbe5b20d8af51a907b1d673f36331354.tar.gz
go-tangerine-3063aad7dbe5b20d8af51a907b1d673f36331354.tar.zst
go-tangerine-3063aad7dbe5b20d8af51a907b1d673f36331354.zip
merge conflict
Diffstat (limited to 'rpc')
-rw-r--r--rpc/api.go51
-rw-r--r--rpc/args.go2
-rw-r--r--rpc/args_test.go214
-rw-r--r--rpc/responses_test.go142
-rw-r--r--rpc/types.go18
-rw-r--r--rpc/types_test.go154
6 files changed, 514 insertions, 67 deletions
diff --git a/rpc/api.go b/rpc/api.go
index 30ba1ddc1..6e2ae2e93 100644
--- a/rpc/api.go
+++ b/rpc/api.go
@@ -53,28 +53,21 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err
case "net_listening":
*reply = api.xeth().IsListening()
case "net_peerCount":
- v := api.xeth().PeerCount()
- *reply = common.ToHex(big.NewInt(int64(v)).Bytes())
+ *reply = newHexNum(api.xeth().PeerCount())
case "eth_protocolVersion":
*reply = api.xeth().EthVersion()
case "eth_coinbase":
- // TODO handling of empty coinbase due to lack of accounts
- res := api.xeth().Coinbase()
- if res == "0x" || res == "0x0" {
- *reply = nil
- } else {
- *reply = res
- }
+ *reply = newHexData(api.xeth().Coinbase())
case "eth_mining":
*reply = api.xeth().IsMining()
case "eth_gasPrice":
v := xeth.DefaultGas()
- *reply = common.ToHex(v.Bytes())
+ *reply = newHexData(v.Bytes())
case "eth_accounts":
*reply = api.xeth().Accounts()
case "eth_blockNumber":
v := api.xeth().CurrentBlock().Number()
- *reply = common.ToHex(v.Bytes())
+ *reply = newHexNum(v.Bytes())
case "eth_getBalance":
args := new(GetBalanceArgs)
if err := json.Unmarshal(req.Params, &args); err != nil {
@@ -105,7 +98,7 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err
}
count := api.xethAtStateNum(args.BlockNumber).TxCountAt(args.Address)
- *reply = common.ToHex(big.NewInt(int64(count)).Bytes())
+ *reply = newHexNum(big.NewInt(int64(count)).Bytes())
case "eth_getBlockTransactionCountByHash":
args := new(HashArgs)
if err := json.Unmarshal(req.Params, &args); err != nil {
@@ -116,7 +109,7 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err
if block == nil {
*reply = nil
} else {
- *reply = common.ToHex(big.NewInt(int64(len(block.Transactions))).Bytes())
+ *reply = newHexNum(big.NewInt(int64(len(block.Transactions))).Bytes())
}
case "eth_getBlockTransactionCountByNumber":
args := new(BlockNumArg)
@@ -125,7 +118,12 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err
}
block := NewBlockRes(api.xeth().EthBlockByNumber(args.BlockNumber), false)
- *reply = common.ToHex(big.NewInt(int64(len(block.Transactions))).Bytes())
+ if block == nil {
+ *reply = nil
+ break
+ }
+
+ *reply = newHexNum(big.NewInt(int64(len(block.Transactions))).Bytes())
case "eth_getUncleCountByBlockHash":
args := new(HashArgs)
if err := json.Unmarshal(req.Params, &args); err != nil {
@@ -134,7 +132,12 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err
block := api.xeth().EthBlockByHash(args.Hash)
br := NewBlockRes(block, false)
- *reply = common.ToHex(big.NewInt(int64(len(br.Uncles))).Bytes())
+ if br == nil {
+ *reply = nil
+ break
+ }
+
+ *reply = newHexNum(big.NewInt(int64(len(br.Uncles))).Bytes())
case "eth_getUncleCountByBlockNumber":
args := new(BlockNumArg)
if err := json.Unmarshal(req.Params, &args); err != nil {
@@ -143,7 +146,12 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err
block := api.xeth().EthBlockByNumber(args.BlockNumber)
br := NewBlockRes(block, false)
- *reply = common.ToHex(big.NewInt(int64(len(br.Uncles))).Bytes())
+ if br == nil {
+ *reply = nil
+ break
+ }
+
+ *reply = newHexNum(big.NewInt(int64(len(br.Uncles))).Bytes())
case "eth_getData", "eth_getCode":
args := new(GetDataArgs)
if err := json.Unmarshal(req.Params, &args); err != nil {
@@ -219,6 +227,7 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err
br := NewBlockRes(block, true)
if br == nil {
*reply = nil
+ break
}
if args.Index >= int64(len(br.Transactions)) || args.Index < 0 {
@@ -237,6 +246,7 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err
v := NewBlockRes(block, true)
if v == nil {
*reply = nil
+ break
}
if args.Index >= int64(len(v.Transactions)) || args.Index < 0 {
@@ -295,14 +305,13 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err
}
id := api.xeth().RegisterFilter(args.Earliest, args.Latest, args.Skip, args.Max, args.Address, args.Topics)
- *reply = common.ToHex(big.NewInt(int64(id)).Bytes())
+ *reply = newHexNum(big.NewInt(int64(id)).Bytes())
case "eth_newBlockFilter":
args := new(FilterStringArgs)
if err := json.Unmarshal(req.Params, &args); err != nil {
return err
}
- id := api.xeth().NewFilterString(args.Word)
- *reply = common.ToHex(big.NewInt(int64(id)).Bytes())
+ *reply = newHexNum(api.xeth().NewFilterString(args.Word))
case "eth_uninstallFilter":
args := new(FilterIdArgs)
if err := json.Unmarshal(req.Params, &args); err != nil {
@@ -384,7 +393,7 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err
}
res, _ := api.xeth().DbGet([]byte(args.Database + args.Key))
- *reply = common.ToHex(res)
+ *reply = newHexData(res)
case "shh_version":
*reply = api.xeth().WhisperVersion()
case "shh_post":
@@ -425,7 +434,7 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err
opts.To = args.To
opts.Topics = args.Topics
id := api.xeth().NewWhisperFilter(opts)
- *reply = common.ToHex(big.NewInt(int64(id)).Bytes())
+ *reply = newHexNum(big.NewInt(int64(id)).Bytes())
case "shh_uninstallFilter":
args := new(FilterIdArgs)
if err := json.Unmarshal(req.Params, &args); err != nil {
diff --git a/rpc/args.go b/rpc/args.go
index cebabf4ba..5b1c271cc 100644
--- a/rpc/args.go
+++ b/rpc/args.go
@@ -36,6 +36,8 @@ func blockHeight(raw interface{}, number *int64) error {
}
switch str {
+ case "earliest":
+ *number = 0
case "latest":
*number = -1
case "pending":
diff --git a/rpc/args_test.go b/rpc/args_test.go
index b88bab280..050f8e472 100644
--- a/rpc/args_test.go
+++ b/rpc/args_test.go
@@ -8,6 +8,61 @@ import (
"testing"
)
+func TestBlockheightInvalidString(t *testing.T) {
+ v := "foo"
+ var num int64
+
+ str := ExpectInvalidTypeError(blockHeight(v, &num))
+ if len(str) > 0 {
+ t.Error(str)
+ }
+}
+
+func TestBlockheightEarliest(t *testing.T) {
+ v := "earliest"
+ e := int64(0)
+ var num int64
+
+ err := blockHeight(v, &num)
+ if err != nil {
+ t.Error(err)
+ }
+
+ if num != e {
+ t.Errorf("Expected %s but got %s", e, num)
+ }
+}
+
+func TestBlockheightLatest(t *testing.T) {
+ v := "latest"
+ e := int64(-1)
+ var num int64
+
+ err := blockHeight(v, &num)
+ if err != nil {
+ t.Error(err)
+ }
+
+ if num != e {
+ t.Errorf("Expected %s but got %s", e, num)
+ }
+}
+
+func TestBlockheightPending(t *testing.T) {
+ v := "pending"
+ e := int64(-2)
+ var num int64
+
+ err := blockHeight(v, &num)
+ if err != nil {
+ t.Error(err)
+ }
+
+ if num != e {
+ t.Errorf("Expected %s but got %s", e, num)
+ }
+}
+
func ExpectValidationError(err error) string {
var str string
switch err.(type) {
@@ -121,6 +176,26 @@ func TestGetBalanceArgs(t *testing.T) {
}
}
+func TestGetBalanceArgsBlocknumMissing(t *testing.T) {
+ input := `["0x407d73d8a49eeb85d32cf465507dd71d507100c1"]`
+ expected := new(GetBalanceArgs)
+ expected.Address = "0x407d73d8a49eeb85d32cf465507dd71d507100c1"
+ expected.BlockNumber = -1
+
+ args := new(GetBalanceArgs)
+ if err := json.Unmarshal([]byte(input), &args); err != nil {
+ t.Error(err)
+ }
+
+ if args.Address != expected.Address {
+ t.Errorf("Address should be %v but is %v", expected.Address, args.Address)
+ }
+
+ if args.BlockNumber != expected.BlockNumber {
+ t.Errorf("BlockNumber should be %v but is %v", expected.BlockNumber, args.BlockNumber)
+ }
+}
+
func TestGetBalanceArgsLatest(t *testing.T) {
input := `["0x407d73d8a49eeb85d32cf465507dd71d507100c1", "latest"]`
expected := new(GetBalanceArgs)
@@ -231,6 +306,16 @@ func TestGetBlockByHashArgsHashInt(t *testing.T) {
}
}
+func TestGetBlockByHashArgsHashBool(t *testing.T) {
+ input := `[false, true]`
+
+ args := new(GetBlockByHashArgs)
+ str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args))
+ if len(str) > 0 {
+ t.Error(str)
+ }
+}
+
func TestGetBlockByNumberArgsBlockNum(t *testing.T) {
input := `[436, false]`
expected := new(GetBlockByNumberArgs)
@@ -850,6 +935,26 @@ func TestGetStorageArgs(t *testing.T) {
}
}
+func TestGetStorageArgsMissingBlocknum(t *testing.T) {
+ input := `["0x407d73d8a49eeb85d32cf465507dd71d507100c1"]`
+ expected := new(GetStorageArgs)
+ expected.Address = "0x407d73d8a49eeb85d32cf465507dd71d507100c1"
+ expected.BlockNumber = -1
+
+ args := new(GetStorageArgs)
+ if err := json.Unmarshal([]byte(input), &args); err != nil {
+ t.Error(err)
+ }
+
+ if expected.Address != args.Address {
+ t.Errorf("Address shoud be %#v but is %#v", expected.Address, args.Address)
+ }
+
+ if expected.BlockNumber != args.BlockNumber {
+ t.Errorf("BlockNumber shoud be %#v but is %#v", expected.BlockNumber, args.BlockNumber)
+ }
+}
+
func TestGetStorageInvalidArgs(t *testing.T) {
input := `{}`
@@ -915,6 +1020,31 @@ func TestGetStorageAtArgs(t *testing.T) {
}
}
+func TestGetStorageAtArgsMissingBlocknum(t *testing.T) {
+ input := `["0x407d73d8a49eeb85d32cf465507dd71d507100c1", "0x0"]`
+ expected := new(GetStorageAtArgs)
+ expected.Address = "0x407d73d8a49eeb85d32cf465507dd71d507100c1"
+ expected.Key = "0x0"
+ expected.BlockNumber = -1
+
+ args := new(GetStorageAtArgs)
+ if err := json.Unmarshal([]byte(input), &args); err != nil {
+ t.Error(err)
+ }
+
+ if expected.Address != args.Address {
+ t.Errorf("Address shoud be %#v but is %#v", expected.Address, args.Address)
+ }
+
+ if expected.Key != args.Key {
+ t.Errorf("Address shoud be %#v but is %#v", expected.Address, args.Address)
+ }
+
+ if expected.BlockNumber != args.BlockNumber {
+ t.Errorf("BlockNumber shoud be %#v but is %#v", expected.BlockNumber, args.BlockNumber)
+ }
+}
+
func TestGetStorageAtEmptyArgs(t *testing.T) {
input := `[]`
@@ -1015,6 +1145,26 @@ func TestGetTxCountAddressNotString(t *testing.T) {
}
}
+func TestGetTxCountBlockheightMissing(t *testing.T) {
+ input := `["0x407d73d8a49eeb85d32cf465507dd71d507100c1"]`
+ expected := new(GetTxCountArgs)
+ expected.Address = "0x407d73d8a49eeb85d32cf465507dd71d507100c1"
+ expected.BlockNumber = -1
+
+ args := new(GetTxCountArgs)
+ if err := json.Unmarshal([]byte(input), &args); err != nil {
+ t.Error(err)
+ }
+
+ if expected.Address != args.Address {
+ t.Errorf("Address shoud be %#v but is %#v", expected.Address, args.Address)
+ }
+
+ if expected.BlockNumber != args.BlockNumber {
+ t.Errorf("BlockNumber shoud be %#v but is %#v", expected.BlockNumber, args.BlockNumber)
+ }
+}
+
func TestGetTxCountBlockheightInvalid(t *testing.T) {
input := `["0x407d73d8a49eeb85d32cf465507dd71d507100c1", {}]`
@@ -1045,6 +1195,26 @@ func TestGetDataArgs(t *testing.T) {
}
}
+func TestGetDataArgsBlocknumMissing(t *testing.T) {
+ input := `["0xd5677cf67b5aa051bb40496e68ad359eb97cfbf8"]`
+ expected := new(GetDataArgs)
+ expected.Address = "0xd5677cf67b5aa051bb40496e68ad359eb97cfbf8"
+ expected.BlockNumber = -1
+
+ args := new(GetDataArgs)
+ if err := json.Unmarshal([]byte(input), &args); err != nil {
+ t.Error(err)
+ }
+
+ if expected.Address != args.Address {
+ t.Errorf("Address shoud be %#v but is %#v", expected.Address, args.Address)
+ }
+
+ if expected.BlockNumber != args.BlockNumber {
+ t.Errorf("BlockNumber shoud be %#v but is %#v", expected.BlockNumber, args.BlockNumber)
+ }
+}
+
func TestGetDataArgsEmpty(t *testing.T) {
input := `[]`
@@ -1995,6 +2165,50 @@ func TestWhisperIdentityArgsInt(t *testing.T) {
}
}
+func TestBlockNumArgs(t *testing.T) {
+ input := `["0x29a"]`
+ expected := new(BlockNumIndexArgs)
+ expected.BlockNumber = 666
+
+ args := new(BlockNumArg)
+ if err := json.Unmarshal([]byte(input), &args); err != nil {
+ t.Error(err)
+ }
+
+ if expected.BlockNumber != args.BlockNumber {
+ t.Errorf("BlockNumber shoud be %#v but is %#v", expected.BlockNumber, args.BlockNumber)
+ }
+}
+
+func TestBlockNumArgsInvalid(t *testing.T) {
+ input := `{}`
+
+ args := new(BlockNumArg)
+ str := ExpectDecodeParamError(json.Unmarshal([]byte(input), &args))
+ if len(str) > 0 {
+ t.Error(str)
+ }
+}
+
+func TestBlockNumArgsEmpty(t *testing.T) {
+ input := `[]`
+
+ args := new(BlockNumArg)
+ str := ExpectInsufficientParamsError(json.Unmarshal([]byte(input), &args))
+ if len(str) > 0 {
+ t.Error(str)
+ }
+}
+func TestBlockNumArgsBool(t *testing.T) {
+ input := `[true]`
+
+ args := new(BlockNumArg)
+ str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args))
+ if len(str) > 0 {
+ t.Error(str)
+ }
+}
+
func TestBlockNumIndexArgs(t *testing.T) {
input := `["0x29a", "0x0"]`
expected := new(BlockNumIndexArgs)
diff --git a/rpc/responses_test.go b/rpc/responses_test.go
index 72d4a91ee..e04a064ce 100644
--- a/rpc/responses_test.go
+++ b/rpc/responses_test.go
@@ -26,12 +26,6 @@ const (
)
func TestNewBlockRes(t *testing.T) {
- parentHash := common.HexToHash("0x01")
- coinbase := common.HexToAddress("0x01")
- root := common.HexToHash("0x01")
- difficulty := common.Big1
- nonce := uint64(1)
- block := types.NewBlock(parentHash, coinbase, root, difficulty, nonce, nil)
tests := map[string]string{
"number": reNum,
"hash": reHash,
@@ -54,16 +48,8 @@ func TestNewBlockRes(t *testing.T) {
// "uncles": reListHash,
}
- to := common.HexToAddress("0x02")
- amount := big.NewInt(1)
- gasAmount := big.NewInt(1)
- gasPrice := big.NewInt(1)
- data := []byte{1, 2, 3}
- tx := types.NewTransactionMessage(to, amount, gasAmount, gasPrice, data)
-
+ block := makeBlock()
v := NewBlockRes(block, false)
- v.Transactions = make([]*TransactionRes, 1)
- v.Transactions[0] = NewTransactionRes(tx)
j, _ := json.Marshal(v)
for k, re := range tests {
@@ -74,13 +60,7 @@ func TestNewBlockRes(t *testing.T) {
}
}
-func TestNewBlockResWithTrans(t *testing.T) {
- parentHash := common.HexToHash("0x01")
- coinbase := common.HexToAddress("0x01")
- root := common.HexToHash("0x01")
- difficulty := common.Big1
- nonce := uint64(1)
- block := types.NewBlock(parentHash, coinbase, root, difficulty, nonce, nil)
+func TestNewBlockResTxFull(t *testing.T) {
tests := map[string]string{
"number": reNum,
"hash": reHash,
@@ -99,20 +79,12 @@ func TestNewBlockResWithTrans(t *testing.T) {
// "minGasPrice": "0x",
"gasUsed": reNum,
"timestamp": reNum,
- // "transactions": `[{.*}]`,
+ // "transactions": reListHash,
// "uncles": reListHash,
}
- to := common.HexToAddress("0x02")
- amount := big.NewInt(1)
- gasAmount := big.NewInt(1)
- gasPrice := big.NewInt(1)
- data := []byte{1, 2, 3}
- tx := types.NewTransactionMessage(to, amount, gasAmount, gasPrice, data)
-
+ block := makeBlock()
v := NewBlockRes(block, true)
- v.Transactions = make([]*TransactionRes, 1)
- v.Transactions[0] = NewTransactionRes(tx)
j, _ := json.Marshal(v)
for k, re := range tests {
@@ -123,6 +95,16 @@ func TestNewBlockResWithTrans(t *testing.T) {
}
}
+func TestBlockNil(t *testing.T) {
+ var block *types.Block
+ block = nil
+ u := NewBlockRes(block, false)
+ j, _ := json.Marshal(u)
+ if string(j) != "null" {
+ t.Errorf("Expected null but got %v", string(j))
+ }
+}
+
func TestNewTransactionRes(t *testing.T) {
to := common.HexToAddress("0x02")
amount := big.NewInt(1)
@@ -159,6 +141,55 @@ func TestNewTransactionRes(t *testing.T) {
}
+func TestTransactionNil(t *testing.T) {
+ var tx *types.Transaction
+ tx = nil
+ u := NewTransactionRes(tx)
+ j, _ := json.Marshal(u)
+ if string(j) != "null" {
+ t.Errorf("Expected null but got %v", string(j))
+ }
+}
+
+func TestNewUncleRes(t *testing.T) {
+ header := makeHeader()
+ u := NewUncleRes(header)
+ tests := map[string]string{
+ "number": reNum,
+ "hash": reHash,
+ "parentHash": reHash,
+ "nonce": reData,
+ "sha3Uncles": reHash,
+ "receiptHash": reHash,
+ "transactionsRoot": reHash,
+ "stateRoot": reHash,
+ "miner": reAddress,
+ "difficulty": reNum,
+ "extraData": reData,
+ "gasLimit": reNum,
+ "gasUsed": reNum,
+ "timestamp": reNum,
+ }
+
+ j, _ := json.Marshal(u)
+ 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 TestUncleNil(t *testing.T) {
+ var header *types.Header
+ header = nil
+ u := NewUncleRes(header)
+ j, _ := json.Marshal(u)
+ if string(j) != "null" {
+ t.Errorf("Expected null but got %v", string(j))
+ }
+}
+
func TestNewLogRes(t *testing.T) {
log := makeStateLog(0)
tests := map[string]string{
@@ -215,3 +246,50 @@ func makeStateLog(num int) state.Log {
log := state.NewLog(address, topics, data, number)
return log
}
+
+func makeHeader() *types.Header {
+ header := &types.Header{
+ ParentHash: common.StringToHash("0x00"),
+ UncleHash: common.StringToHash("0x00"),
+ Coinbase: common.StringToAddress("0x00"),
+ Root: common.StringToHash("0x00"),
+ TxHash: common.StringToHash("0x00"),
+ ReceiptHash: common.StringToHash("0x00"),
+ // Bloom:
+ Difficulty: big.NewInt(88888888),
+ Number: big.NewInt(16),
+ GasLimit: big.NewInt(70000),
+ GasUsed: big.NewInt(25000),
+ Time: 124356789,
+ Extra: nil,
+ MixDigest: common.StringToHash("0x00"),
+ Nonce: [8]byte{0, 1, 2, 3, 4, 5, 6, 7},
+ }
+ return header
+}
+
+func makeBlock() *types.Block {
+ parentHash := common.HexToHash("0x01")
+ coinbase := common.HexToAddress("0x01")
+ root := common.HexToHash("0x01")
+ difficulty := common.Big1
+ nonce := uint64(1)
+ block := types.NewBlock(parentHash, coinbase, root, difficulty, nonce, nil)
+
+ txto := common.HexToAddress("0x02")
+ txamount := big.NewInt(1)
+ txgasAmount := big.NewInt(1)
+ txgasPrice := big.NewInt(1)
+ txdata := []byte{1, 2, 3}
+
+ tx := types.NewTransactionMessage(txto, txamount, txgasAmount, txgasPrice, txdata)
+ txs := make([]*types.Transaction, 1)
+ txs[0] = tx
+ block.SetTransactions(txs)
+
+ uncles := make([]*types.Header, 1)
+ uncles[0] = makeHeader()
+ block.SetUncles(uncles)
+
+ return block
+}
diff --git a/rpc/types.go b/rpc/types.go
index 806c9c8a4..bc9a46ed5 100644
--- a/rpc/types.go
+++ b/rpc/types.go
@@ -43,16 +43,11 @@ func (d *hexdata) MarshalJSON() ([]byte, error) {
return json.Marshal(d.String())
}
-func (d *hexdata) UnmarshalJSON(b []byte) (err error) {
- d.data = common.FromHex(string(b))
- return nil
-}
-
func newHexData(input interface{}) *hexdata {
d := new(hexdata)
if input == nil {
- d.data = nil
+ d.isNil = true
return d
}
switch input := input.(type) {
@@ -105,19 +100,19 @@ func newHexData(input interface{}) *hexdata {
case int16:
d.data = big.NewInt(int64(input)).Bytes()
case uint16:
- buff := make([]byte, 8)
+ buff := make([]byte, 2)
binary.BigEndian.PutUint16(buff, input)
d.data = buff
case int32:
d.data = big.NewInt(int64(input)).Bytes()
case uint32:
- buff := make([]byte, 8)
+ buff := make([]byte, 4)
binary.BigEndian.PutUint32(buff, input)
d.data = buff
case string: // hexstring
d.data = common.Big(input).Bytes()
default:
- d.data = nil
+ d.isNil = true
}
return d
@@ -147,11 +142,6 @@ func (d *hexnum) MarshalJSON() ([]byte, error) {
return json.Marshal(d.String())
}
-func (d *hexnum) UnmarshalJSON(b []byte) (err error) {
- d.data = common.FromHex(string(b))
- return nil
-}
-
func newHexNum(input interface{}) *hexnum {
d := new(hexnum)
diff --git a/rpc/types_test.go b/rpc/types_test.go
index 91f0152dc..9ef7b8d38 100644
--- a/rpc/types_test.go
+++ b/rpc/types_test.go
@@ -1,7 +1,13 @@
package rpc
import (
+ "bytes"
+ "encoding/json"
+ "math/big"
"testing"
+
+ "github.com/ethereum/go-ethereum/common"
+ "github.com/ethereum/go-ethereum/core/types"
)
func TestInvalidTypeError(t *testing.T) {
@@ -48,3 +54,151 @@ func TestValidationError(t *testing.T) {
t.Error(err.Error())
}
}
+
+func TestHexdataMarshalNil(t *testing.T) {
+ hd := newHexData([]byte{})
+ hd.isNil = true
+ v, _ := json.Marshal(hd)
+ if string(v) != "null" {
+ t.Errorf("Expected null, got %s", v)
+ }
+}
+
+func TestHexnumMarshalNil(t *testing.T) {
+ hn := newHexNum([]byte{})
+ hn.isNil = true
+ v, _ := json.Marshal(hn)
+ if string(v) != "null" {
+ t.Errorf("Expected null, got %s", v)
+ }
+}
+
+func TestHexdataNil(t *testing.T) {
+ v := newHexData(nil)
+ if v.isNil != true {
+ t.Errorf("Expected isNil to be true, but is %v", v.isNil)
+ }
+}
+
+func TestHexdataPtrHash(t *testing.T) {
+ in := common.Hash{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}
+ v := newHexData(&in)
+ if bytes.Compare(in.Bytes(), v.data) != 0 {
+ t.Errorf("Got % x expected % x", in, v.data)
+ }
+}
+
+func TestHexdataPtrHashNil(t *testing.T) {
+ var in *common.Hash
+ in = nil
+ v := newHexData(in)
+ if !v.isNil {
+ t.Errorf("Expect isNil to be true, but is %v", v.isNil)
+ }
+}
+
+func TestHexdataPtrAddress(t *testing.T) {
+ in := common.Address{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19}
+ v := newHexData(&in)
+ if bytes.Compare(in.Bytes(), v.data) != 0 {
+ t.Errorf("Got % x expected % x", in, v.data)
+ }
+}
+
+func TestHexdataPtrAddressNil(t *testing.T) {
+ var in *common.Address
+ in = nil
+ v := newHexData(in)
+ if !v.isNil {
+ t.Errorf("Expect isNil to be true, but is %v", v.isNil)
+ }
+}
+
+func TestHexdataPtrBloom(t *testing.T) {
+ in := types.Bloom{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19}
+ v := newHexData(&in)
+ if bytes.Compare(in.Bytes(), v.data) != 0 {
+ t.Errorf("Got % x expected % x", in, v.data)
+ }
+}
+
+func TestHexdataPtrBloomNil(t *testing.T) {
+ var in *types.Bloom
+ in = nil
+ v := newHexData(in)
+ if !v.isNil {
+ t.Errorf("Expect isNil to be true, but is %v", v.isNil)
+ }
+}
+
+func TestHexdataBigintNil(t *testing.T) {
+ var in *big.Int
+ in = nil
+ v := newHexData(in)
+ if !v.isNil {
+ t.Errorf("Expect isNil to be true, but is %v", v.isNil)
+ }
+}
+
+func TestHexdataUint(t *testing.T) {
+ var in = uint(16)
+ var expected = []byte{0x10}
+ v := newHexData(in)
+ if bytes.Compare(expected, v.data) != 0 {
+ t.Errorf("Expected % x got % x", expected, v.data)
+ }
+}
+
+func TestHexdataInt8(t *testing.T) {
+ var in = int8(16)
+ var expected = []byte{0x10}
+ v := newHexData(in)
+ if bytes.Compare(expected, v.data) != 0 {
+ t.Errorf("Expected % x got % x", expected, v.data)
+ }
+}
+
+func TestHexdataUint8(t *testing.T) {
+ var in = uint8(16)
+ var expected = []byte{0x10}
+ v := newHexData(in)
+ if bytes.Compare(expected, v.data) != 0 {
+ t.Errorf("Expected % x got % x", expected, v.data)
+ }
+}
+
+func TestHexdataInt16(t *testing.T) {
+ var in = int16(16)
+ var expected = []byte{0x10}
+ v := newHexData(in)
+ if bytes.Compare(expected, v.data) != 0 {
+ t.Errorf("Expected % x got % x", expected, v.data)
+ }
+}
+
+func TestHexdataUint16(t *testing.T) {
+ var in = uint16(16)
+ var expected = []byte{0x0, 0x10}
+ v := newHexData(in)
+ if bytes.Compare(expected, v.data) != 0 {
+ t.Errorf("Expected % x got % x", expected, v.data)
+ }
+}
+
+func TestHexdataInt32(t *testing.T) {
+ var in = int32(16)
+ var expected = []byte{0x10}
+ v := newHexData(in)
+ if bytes.Compare(expected, v.data) != 0 {
+ t.Errorf("Expected % x got % x", expected, v.data)
+ }
+}
+
+func TestHexdataUint32(t *testing.T) {
+ var in = uint32(16)
+ var expected = []byte{0x0, 0x0, 0x0, 0x10}
+ v := newHexData(in)
+ if bytes.Compare(expected, v.data) != 0 {
+ t.Errorf("Expected % x got % x", expected, v.data)
+ }
+}