good morning!!!!

Skip to content
Snippets Groups Projects
Commit 3063aad7 authored by Jeffrey Wilcke's avatar Jeffrey Wilcke
Browse files

merge conflict

parents 50aa1f17 d0c3f127
Branches
Tags
No related merge requests found
...@@ -53,28 +53,21 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err ...@@ -53,28 +53,21 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err
case "net_listening": case "net_listening":
*reply = api.xeth().IsListening() *reply = api.xeth().IsListening()
case "net_peerCount": case "net_peerCount":
v := api.xeth().PeerCount() *reply = newHexNum(api.xeth().PeerCount())
*reply = common.ToHex(big.NewInt(int64(v)).Bytes())
case "eth_protocolVersion": case "eth_protocolVersion":
*reply = api.xeth().EthVersion() *reply = api.xeth().EthVersion()
case "eth_coinbase": case "eth_coinbase":
// TODO handling of empty coinbase due to lack of accounts *reply = newHexData(api.xeth().Coinbase())
res := api.xeth().Coinbase()
if res == "0x" || res == "0x0" {
*reply = nil
} else {
*reply = res
}
case "eth_mining": case "eth_mining":
*reply = api.xeth().IsMining() *reply = api.xeth().IsMining()
case "eth_gasPrice": case "eth_gasPrice":
v := xeth.DefaultGas() v := xeth.DefaultGas()
*reply = common.ToHex(v.Bytes()) *reply = newHexData(v.Bytes())
case "eth_accounts": case "eth_accounts":
*reply = api.xeth().Accounts() *reply = api.xeth().Accounts()
case "eth_blockNumber": case "eth_blockNumber":
v := api.xeth().CurrentBlock().Number() v := api.xeth().CurrentBlock().Number()
*reply = common.ToHex(v.Bytes()) *reply = newHexNum(v.Bytes())
case "eth_getBalance": case "eth_getBalance":
args := new(GetBalanceArgs) args := new(GetBalanceArgs)
if err := json.Unmarshal(req.Params, &args); err != nil { if err := json.Unmarshal(req.Params, &args); err != nil {
...@@ -105,7 +98,7 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err ...@@ -105,7 +98,7 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err
} }
count := api.xethAtStateNum(args.BlockNumber).TxCountAt(args.Address) 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": case "eth_getBlockTransactionCountByHash":
args := new(HashArgs) args := new(HashArgs)
if err := json.Unmarshal(req.Params, &args); err != nil { if err := json.Unmarshal(req.Params, &args); err != nil {
...@@ -116,7 +109,7 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err ...@@ -116,7 +109,7 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err
if block == nil { if block == nil {
*reply = nil *reply = nil
} else { } else {
*reply = common.ToHex(big.NewInt(int64(len(block.Transactions))).Bytes()) *reply = newHexNum(big.NewInt(int64(len(block.Transactions))).Bytes())
} }
case "eth_getBlockTransactionCountByNumber": case "eth_getBlockTransactionCountByNumber":
args := new(BlockNumArg) args := new(BlockNumArg)
...@@ -125,7 +118,12 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err ...@@ -125,7 +118,12 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err
} }
block := NewBlockRes(api.xeth().EthBlockByNumber(args.BlockNumber), false) 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": case "eth_getUncleCountByBlockHash":
args := new(HashArgs) args := new(HashArgs)
if err := json.Unmarshal(req.Params, &args); err != nil { if err := json.Unmarshal(req.Params, &args); err != nil {
...@@ -134,7 +132,12 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err ...@@ -134,7 +132,12 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err
block := api.xeth().EthBlockByHash(args.Hash) block := api.xeth().EthBlockByHash(args.Hash)
br := NewBlockRes(block, false) 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": case "eth_getUncleCountByBlockNumber":
args := new(BlockNumArg) args := new(BlockNumArg)
if err := json.Unmarshal(req.Params, &args); err != nil { if err := json.Unmarshal(req.Params, &args); err != nil {
...@@ -143,7 +146,12 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err ...@@ -143,7 +146,12 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err
block := api.xeth().EthBlockByNumber(args.BlockNumber) block := api.xeth().EthBlockByNumber(args.BlockNumber)
br := NewBlockRes(block, false) 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": case "eth_getData", "eth_getCode":
args := new(GetDataArgs) args := new(GetDataArgs)
if err := json.Unmarshal(req.Params, &args); err != nil { if err := json.Unmarshal(req.Params, &args); err != nil {
...@@ -219,6 +227,7 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err ...@@ -219,6 +227,7 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err
br := NewBlockRes(block, true) br := NewBlockRes(block, true)
if br == nil { if br == nil {
*reply = nil *reply = nil
break
} }
if args.Index >= int64(len(br.Transactions)) || args.Index < 0 { if args.Index >= int64(len(br.Transactions)) || args.Index < 0 {
...@@ -237,6 +246,7 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err ...@@ -237,6 +246,7 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err
v := NewBlockRes(block, true) v := NewBlockRes(block, true)
if v == nil { if v == nil {
*reply = nil *reply = nil
break
} }
if args.Index >= int64(len(v.Transactions)) || args.Index < 0 { if args.Index >= int64(len(v.Transactions)) || args.Index < 0 {
...@@ -295,14 +305,13 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err ...@@ -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) 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": case "eth_newBlockFilter":
args := new(FilterStringArgs) args := new(FilterStringArgs)
if err := json.Unmarshal(req.Params, &args); err != nil { if err := json.Unmarshal(req.Params, &args); err != nil {
return err return err
} }
id := api.xeth().NewFilterString(args.Word) *reply = newHexNum(api.xeth().NewFilterString(args.Word))
*reply = common.ToHex(big.NewInt(int64(id)).Bytes())
case "eth_uninstallFilter": case "eth_uninstallFilter":
args := new(FilterIdArgs) args := new(FilterIdArgs)
if err := json.Unmarshal(req.Params, &args); err != nil { if err := json.Unmarshal(req.Params, &args); err != nil {
...@@ -384,7 +393,7 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err ...@@ -384,7 +393,7 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err
} }
res, _ := api.xeth().DbGet([]byte(args.Database + args.Key)) res, _ := api.xeth().DbGet([]byte(args.Database + args.Key))
*reply = common.ToHex(res) *reply = newHexData(res)
case "shh_version": case "shh_version":
*reply = api.xeth().WhisperVersion() *reply = api.xeth().WhisperVersion()
case "shh_post": case "shh_post":
...@@ -425,7 +434,7 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err ...@@ -425,7 +434,7 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err
opts.To = args.To opts.To = args.To
opts.Topics = args.Topics opts.Topics = args.Topics
id := api.xeth().NewWhisperFilter(opts) id := api.xeth().NewWhisperFilter(opts)
*reply = common.ToHex(big.NewInt(int64(id)).Bytes()) *reply = newHexNum(big.NewInt(int64(id)).Bytes())
case "shh_uninstallFilter": case "shh_uninstallFilter":
args := new(FilterIdArgs) args := new(FilterIdArgs)
if err := json.Unmarshal(req.Params, &args); err != nil { if err := json.Unmarshal(req.Params, &args); err != nil {
......
...@@ -36,6 +36,8 @@ func blockHeight(raw interface{}, number *int64) error { ...@@ -36,6 +36,8 @@ func blockHeight(raw interface{}, number *int64) error {
} }
switch str { switch str {
case "earliest":
*number = 0
case "latest": case "latest":
*number = -1 *number = -1
case "pending": case "pending":
......
...@@ -8,6 +8,61 @@ import ( ...@@ -8,6 +8,61 @@ import (
"testing" "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 { func ExpectValidationError(err error) string {
var str string var str string
switch err.(type) { switch err.(type) {
...@@ -121,6 +176,26 @@ func TestGetBalanceArgs(t *testing.T) { ...@@ -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) { func TestGetBalanceArgsLatest(t *testing.T) {
input := `["0x407d73d8a49eeb85d32cf465507dd71d507100c1", "latest"]` input := `["0x407d73d8a49eeb85d32cf465507dd71d507100c1", "latest"]`
expected := new(GetBalanceArgs) expected := new(GetBalanceArgs)
...@@ -231,6 +306,16 @@ func TestGetBlockByHashArgsHashInt(t *testing.T) { ...@@ -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) { func TestGetBlockByNumberArgsBlockNum(t *testing.T) {
input := `[436, false]` input := `[436, false]`
expected := new(GetBlockByNumberArgs) expected := new(GetBlockByNumberArgs)
...@@ -850,6 +935,26 @@ func TestGetStorageArgs(t *testing.T) { ...@@ -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) { func TestGetStorageInvalidArgs(t *testing.T) {
input := `{}` input := `{}`
...@@ -915,6 +1020,31 @@ func TestGetStorageAtArgs(t *testing.T) { ...@@ -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) { func TestGetStorageAtEmptyArgs(t *testing.T) {
input := `[]` input := `[]`
...@@ -1015,6 +1145,26 @@ func TestGetTxCountAddressNotString(t *testing.T) { ...@@ -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) { func TestGetTxCountBlockheightInvalid(t *testing.T) {
input := `["0x407d73d8a49eeb85d32cf465507dd71d507100c1", {}]` input := `["0x407d73d8a49eeb85d32cf465507dd71d507100c1", {}]`
...@@ -1045,6 +1195,26 @@ func TestGetDataArgs(t *testing.T) { ...@@ -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) { func TestGetDataArgsEmpty(t *testing.T) {
input := `[]` input := `[]`
...@@ -1995,6 +2165,50 @@ func TestWhisperIdentityArgsInt(t *testing.T) { ...@@ -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) { func TestBlockNumIndexArgs(t *testing.T) {
input := `["0x29a", "0x0"]` input := `["0x29a", "0x0"]`
expected := new(BlockNumIndexArgs) expected := new(BlockNumIndexArgs)
......
...@@ -26,12 +26,6 @@ const ( ...@@ -26,12 +26,6 @@ const (
) )
func TestNewBlockRes(t *testing.T) { 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{ tests := map[string]string{
"number": reNum, "number": reNum,
"hash": reHash, "hash": reHash,
...@@ -54,16 +48,8 @@ func TestNewBlockRes(t *testing.T) { ...@@ -54,16 +48,8 @@ func TestNewBlockRes(t *testing.T) {
// "uncles": reListHash, // "uncles": reListHash,
} }
to := common.HexToAddress("0x02") block := makeBlock()
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)
v := NewBlockRes(block, false) v := NewBlockRes(block, false)
v.Transactions = make([]*TransactionRes, 1)
v.Transactions[0] = NewTransactionRes(tx)
j, _ := json.Marshal(v) j, _ := json.Marshal(v)
for k, re := range tests { for k, re := range tests {
...@@ -74,13 +60,7 @@ func TestNewBlockRes(t *testing.T) { ...@@ -74,13 +60,7 @@ func TestNewBlockRes(t *testing.T) {
} }
} }
func TestNewBlockResWithTrans(t *testing.T) { func TestNewBlockResTxFull(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{ tests := map[string]string{
"number": reNum, "number": reNum,
"hash": reHash, "hash": reHash,
...@@ -99,20 +79,12 @@ func TestNewBlockResWithTrans(t *testing.T) { ...@@ -99,20 +79,12 @@ func TestNewBlockResWithTrans(t *testing.T) {
// "minGasPrice": "0x", // "minGasPrice": "0x",
"gasUsed": reNum, "gasUsed": reNum,
"timestamp": reNum, "timestamp": reNum,
// "transactions": `[{.*}]`, // "transactions": reListHash,
// "uncles": reListHash, // "uncles": reListHash,
} }
to := common.HexToAddress("0x02") block := makeBlock()
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)
v := NewBlockRes(block, true) v := NewBlockRes(block, true)
v.Transactions = make([]*TransactionRes, 1)
v.Transactions[0] = NewTransactionRes(tx)
j, _ := json.Marshal(v) j, _ := json.Marshal(v)
for k, re := range tests { for k, re := range tests {
...@@ -123,6 +95,16 @@ func TestNewBlockResWithTrans(t *testing.T) { ...@@ -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) { func TestNewTransactionRes(t *testing.T) {
to := common.HexToAddress("0x02") to := common.HexToAddress("0x02")
amount := big.NewInt(1) amount := big.NewInt(1)
...@@ -159,6 +141,55 @@ func TestNewTransactionRes(t *testing.T) { ...@@ -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) { func TestNewLogRes(t *testing.T) {
log := makeStateLog(0) log := makeStateLog(0)
tests := map[string]string{ tests := map[string]string{
...@@ -215,3 +246,50 @@ func makeStateLog(num int) state.Log { ...@@ -215,3 +246,50 @@ func makeStateLog(num int) state.Log {
log := state.NewLog(address, topics, data, number) log := state.NewLog(address, topics, data, number)
return log 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
}
...@@ -43,16 +43,11 @@ func (d *hexdata) MarshalJSON() ([]byte, error) { ...@@ -43,16 +43,11 @@ func (d *hexdata) MarshalJSON() ([]byte, error) {
return json.Marshal(d.String()) 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 { func newHexData(input interface{}) *hexdata {
d := new(hexdata) d := new(hexdata)
if input == nil { if input == nil {
d.data = nil d.isNil = true
return d return d
} }
switch input := input.(type) { switch input := input.(type) {
...@@ -105,19 +100,19 @@ func newHexData(input interface{}) *hexdata { ...@@ -105,19 +100,19 @@ func newHexData(input interface{}) *hexdata {
case int16: case int16:
d.data = big.NewInt(int64(input)).Bytes() d.data = big.NewInt(int64(input)).Bytes()
case uint16: case uint16:
buff := make([]byte, 8) buff := make([]byte, 2)
binary.BigEndian.PutUint16(buff, input) binary.BigEndian.PutUint16(buff, input)
d.data = buff d.data = buff
case int32: case int32:
d.data = big.NewInt(int64(input)).Bytes() d.data = big.NewInt(int64(input)).Bytes()
case uint32: case uint32:
buff := make([]byte, 8) buff := make([]byte, 4)
binary.BigEndian.PutUint32(buff, input) binary.BigEndian.PutUint32(buff, input)
d.data = buff d.data = buff
case string: // hexstring case string: // hexstring
d.data = common.Big(input).Bytes() d.data = common.Big(input).Bytes()
default: default:
d.data = nil d.isNil = true
} }
return d return d
...@@ -147,11 +142,6 @@ func (d *hexnum) MarshalJSON() ([]byte, error) { ...@@ -147,11 +142,6 @@ func (d *hexnum) MarshalJSON() ([]byte, error) {
return json.Marshal(d.String()) 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 { func newHexNum(input interface{}) *hexnum {
d := new(hexnum) d := new(hexnum)
......
package rpc package rpc
import ( import (
"bytes"
"encoding/json"
"math/big"
"testing" "testing"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
) )
func TestInvalidTypeError(t *testing.T) { func TestInvalidTypeError(t *testing.T) {
...@@ -48,3 +54,151 @@ func TestValidationError(t *testing.T) { ...@@ -48,3 +54,151 @@ func TestValidationError(t *testing.T) {
t.Error(err.Error()) 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)
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment