From 5da3d911cf80e032ceb79a17609e41973923fc1a Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Fri, 6 Mar 2015 00:03:22 -0600 Subject: [PATCH] Initial args tests --- rpc/args.go | 26 +++++----- rpc/args_test.go | 125 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 138 insertions(+), 13 deletions(-) create mode 100644 rpc/args_test.go diff --git a/rpc/args.go b/rpc/args.go index f560512422..ba2ef731aa 100644 --- a/rpc/args.go +++ b/rpc/args.go @@ -47,7 +47,7 @@ func (args *GetBlockByNumberArgs) UnmarshalJSON(b []byte) (err error) { if len(obj) < 1 { return errArguments } - args.BlockNumber = ethutil.BytesToNumber(fromHex(obj[0].(string))) + args.BlockNumber = uint64(ethutil.Big(obj[0].(string)).Int64()) if len(obj) > 1 { args.Transactions = obj[1].(bool) @@ -110,7 +110,7 @@ func (args *GetStorageArgs) UnmarshalJSON(b []byte) (err error) { args.Address = obj[0].(string) if len(obj) > 1 { - args.BlockNumber = ethutil.BytesToNumber(fromHex(obj[1].(string))) + args.BlockNumber = uint64(ethutil.Big(obj[1].(string)).Int64()) } return nil @@ -179,7 +179,7 @@ func (args *GetTxCountArgs) UnmarshalJSON(b []byte) (err error) { args.Address = obj[0].(string) if len(obj) > 1 { - args.BlockNumber = ethutil.BytesToNumber(fromHex(obj[1].(string))) + args.BlockNumber = uint64(ethutil.Big(obj[1].(string)).Int64()) } return nil @@ -210,7 +210,7 @@ func (args *GetBalanceArgs) UnmarshalJSON(b []byte) (err error) { args.Address = obj[0].(string) if len(obj) > 1 { - args.BlockNumber = ethutil.BytesToNumber(fromHex(obj[1].(string))) + args.BlockNumber = uint64(ethutil.Big(obj[1].(string)).Int64()) } return nil @@ -241,7 +241,7 @@ func (args *GetDataArgs) UnmarshalJSON(b []byte) (err error) { args.Address = obj[0].(string) if len(obj) > 1 { - args.BlockNumber = ethutil.BytesToNumber(fromHex(obj[1].(string))) + args.BlockNumber = uint64(ethutil.Big(obj[1].(string)).Int64()) } return nil @@ -299,10 +299,10 @@ func (args *FilterArgs) UnmarshalJSON(b []byte) (err error) { if len(obj) < 1 { return errArguments } - args.FromBlock = ethutil.BytesToNumber(fromHex(obj[0].FromBlock)) - args.ToBlock = ethutil.BytesToNumber(fromHex(obj[0].ToBlock)) - args.Limit = ethutil.BytesToNumber(fromHex(obj[0].Limit)) - args.Offset = ethutil.BytesToNumber(fromHex(obj[0].Offset)) + args.FromBlock = uint64(ethutil.Big(obj[0].FromBlock).Int64()) + args.ToBlock = uint64(ethutil.Big(obj[0].ToBlock).Int64()) + args.Limit = uint64(ethutil.Big(obj[0].Limit).Int64()) + args.Offset = uint64(ethutil.Big(obj[0].Offset).Int64()) args.Address = obj[0].Address args.Topics = obj[0].Topics @@ -335,10 +335,10 @@ func (args *FilterOptions) UnmarshalJSON(b []byte) (err error) { if len(obj) < 1 { return errArguments } - args.Earliest = int64(ethutil.BytesToNumber(fromHex(obj[0].FromBlock))) - args.Latest = int64(ethutil.BytesToNumber(fromHex(obj[0].ToBlock))) - args.Max = int(ethutil.BytesToNumber(fromHex(obj[0].Limit))) - args.Skip = int(ethutil.BytesToNumber(fromHex(obj[0].Offset))) + args.Earliest = int64(ethutil.Big(obj[0].FromBlock).Int64()) + args.Latest = int64(ethutil.Big(obj[0].ToBlock).Int64()) + args.Max = int(ethutil.Big(obj[0].Limit).Int64()) + args.Skip = int(ethutil.Big(obj[0].Offset).Int64()) args.Address = obj[0].Address args.Topic = obj[0].Topics diff --git a/rpc/args_test.go b/rpc/args_test.go new file mode 100644 index 0000000000..380ed176b3 --- /dev/null +++ b/rpc/args_test.go @@ -0,0 +1,125 @@ +package rpc + +import ( + "bytes" + "encoding/json" + "math/big" + "testing" +) + +func TestSha3(t *testing.T) { + input := `["0x68656c6c6f20776f726c64"]` + expected := "0x68656c6c6f20776f726c64" + + args := new(Sha3Args) + json.Unmarshal([]byte(input), &args) + + if args.Data != expected { + t.Error("got %s expected %s", input, expected) + } +} + +func TestGetBalanceArgs(t *testing.T) { + input := `["0x407d73d8a49eeb85d32cf465507dd71d507100c1", "0x1f"]` + expected := new(GetBalanceArgs) + expected.Address = "0x407d73d8a49eeb85d32cf465507dd71d507100c1" + expected.BlockNumber = 31 + + 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 TestGetBlockByHashArgs(t *testing.T) { + input := `["0xe670ec64341771606e55d6b4ca35a1a6b75ee3d5145a99d05921026d1527331", true]` + expected := new(GetBlockByHashArgs) + expected.BlockHash = "0xe670ec64341771606e55d6b4ca35a1a6b75ee3d5145a99d05921026d1527331" + expected.Transactions = true + + args := new(GetBlockByHashArgs) + if err := json.Unmarshal([]byte(input), &args); err != nil { + t.Error(err) + } + + if args.BlockHash != expected.BlockHash { + t.Errorf("BlockHash should be %v but is %v", expected.BlockHash, args.BlockHash) + } + + if args.Transactions != expected.Transactions { + t.Errorf("Transactions should be %v but is %v", expected.Transactions, args.Transactions) + } +} + +func TestGetBlockByNumberArgs(t *testing.T) { + input := `["0x1b4", false]` + expected := new(GetBlockByNumberArgs) + expected.BlockNumber = 436 + expected.Transactions = false + + args := new(GetBlockByNumberArgs) + if err := json.Unmarshal([]byte(input), &args); err != nil { + t.Error(err) + } + + if args.BlockNumber != expected.BlockNumber { + t.Errorf("BlockHash should be %v but is %v", expected.BlockNumber, args.BlockNumber) + } + + if args.Transactions != expected.Transactions { + t.Errorf("Transactions should be %v but is %v", expected.Transactions, args.Transactions) + } +} + +func TestNewTxArgs(t *testing.T) { + input := `[{"from": "0xb60e8dd61c5d32be8058bb8eb970870f07233155", + "to": "0xd46e8dd67c5d32be8058bb8eb970870f072445675", + "gas": "0x76c0", + "gasPrice": "0x9184e72a000", + "value": "0x9184e72a000", + "data": "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675"}]` + expected := new(NewTxArgs) + expected.From = "0xb60e8dd61c5d32be8058bb8eb970870f07233155" + expected.To = "0xd46e8dd67c5d32be8058bb8eb970870f072445675" + expected.Gas = big.NewInt(30400) + expected.GasPrice = big.NewInt(10000000000000) + expected.Value = big.NewInt(10000000000000) + expected.Data = "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675" + + args := new(NewTxArgs) + if err := json.Unmarshal([]byte(input), &args); err != nil { + t.Error(err) + } + + if expected.From != args.From { + t.Errorf("From shoud be %#v but is %#v", expected.From, args.From) + } + + if expected.To != args.To { + t.Errorf("To shoud be %#v but is %#v", expected.To, args.To) + } + + if bytes.Compare(expected.Gas.Bytes(), args.Gas.Bytes()) != 0 { + t.Errorf("Gas shoud be %#v but is %#v", expected.Gas.Bytes(), args.Gas.Bytes()) + } + + if bytes.Compare(expected.GasPrice.Bytes(), args.GasPrice.Bytes()) != 0 { + t.Errorf("GasPrice shoud be %#v but is %#v", expected.GasPrice, args.GasPrice) + } + + if bytes.Compare(expected.Value.Bytes(), args.Value.Bytes()) != 0 { + t.Errorf("Value shoud be %#v but is %#v", expected.Value, args.Value) + } + + if expected.Data != args.Data { + t.Errorf("Data shoud be %#v but is %#v", expected.Data, args.Data) + } +}