From 85ebbc9aa5e4c8902b94847dbeb78f26255eb0aa Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Thu, 2 Apr 2015 11:57:26 +0200 Subject: Default BlockNumber to -1 when missing --- rpc/args.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'rpc/args.go') diff --git a/rpc/args.go b/rpc/args.go index dd013147d..220daf960 100644 --- a/rpc/args.go +++ b/rpc/args.go @@ -233,6 +233,8 @@ func (args *NewTxArgs) UnmarshalJSON(b []byte) (err error) { if err := blockHeightFromJson(obj[1], &args.BlockNumber); err != nil { return err } + } else { + args.BlockNumber = -1 } return nil @@ -320,6 +322,8 @@ func (args *CallArgs) UnmarshalJSON(b []byte) (err error) { if err := blockHeightFromJson(obj[1], &args.BlockNumber); err != nil { return err } + } else { + args.BlockNumber = -1 } return nil @@ -350,6 +354,8 @@ func (args *GetStorageArgs) UnmarshalJSON(b []byte) (err error) { if err := blockHeight(obj[1], &args.BlockNumber); err != nil { return err } + } else { + args.BlockNumber = -1 } return nil @@ -387,6 +393,8 @@ func (args *GetStorageAtArgs) UnmarshalJSON(b []byte) (err error) { if err := blockHeight(obj[2], &args.BlockNumber); err != nil { return err } + } else { + args.BlockNumber = -1 } return nil @@ -417,6 +425,8 @@ func (args *GetTxCountArgs) UnmarshalJSON(b []byte) (err error) { if err := blockHeight(obj[1], &args.BlockNumber); err != nil { return err } + } else { + args.BlockNumber = -1 } return nil @@ -447,6 +457,8 @@ func (args *GetBalanceArgs) UnmarshalJSON(b []byte) (err error) { if err := blockHeight(obj[1], &args.BlockNumber); err != nil { return err } + } else { + args.BlockNumber = -1 } return nil @@ -477,6 +489,8 @@ func (args *GetDataArgs) UnmarshalJSON(b []byte) (err error) { if err := blockHeight(obj[1], &args.BlockNumber); err != nil { return err } + } else { + args.BlockNumber = -1 } return nil -- cgit v1.2.3 From e402e1dc2e72df2a433b984caeaba771085b2b66 Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Thu, 2 Apr 2015 13:17:55 +0200 Subject: New args types with stricter checking --- rpc/args.go | 88 +++++++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 62 insertions(+), 26 deletions(-) (limited to 'rpc/args.go') diff --git a/rpc/args.go b/rpc/args.go index 220daf960..b43c465c0 100644 --- a/rpc/args.go +++ b/rpc/args.go @@ -108,8 +108,8 @@ func (args *GetBlockByHashArgs) UnmarshalJSON(b []byte) (err error) { return NewDecodeParamError(err.Error()) } - if len(obj) < 1 { - return NewInsufficientParamsError(len(obj), 1) + if len(obj) < 2 { + return NewInsufficientParamsError(len(obj), 2) } argstr, ok := obj[0].(string) @@ -118,9 +118,7 @@ func (args *GetBlockByHashArgs) UnmarshalJSON(b []byte) (err error) { } args.BlockHash = argstr - if len(obj) > 1 { - args.IncludeTxs = obj[1].(bool) - } + args.IncludeTxs = obj[1].(bool) return nil } @@ -136,8 +134,8 @@ func (args *GetBlockByNumberArgs) UnmarshalJSON(b []byte) (err error) { return NewDecodeParamError(err.Error()) } - if len(obj) < 1 { - return NewInsufficientParamsError(len(obj), 1) + if len(obj) < 2 { + return NewInsufficientParamsError(len(obj), 2) } if v, ok := obj[0].(float64); ok { @@ -148,9 +146,7 @@ func (args *GetBlockByNumberArgs) UnmarshalJSON(b []byte) (err error) { return NewInvalidTypeError("blockNumber", "not a number or string") } - if len(obj) > 1 { - args.IncludeTxs = obj[1].(bool) - } + args.IncludeTxs = obj[1].(bool) return nil } @@ -496,6 +492,27 @@ func (args *GetDataArgs) UnmarshalJSON(b []byte) (err error) { return nil } +type BlockNumArg struct { + BlockNumber int64 +} + +func (args *BlockNumArg) UnmarshalJSON(b []byte) (err error) { + var obj []interface{} + if err := json.Unmarshal(b, &obj); err != nil { + return NewDecodeParamError(err.Error()) + } + + if len(obj) < 1 { + return NewInsufficientParamsError(len(obj), 1) + } + + if err := blockHeight(obj[0], &args.BlockNumber); err != nil { + return err + } + + return nil +} + type BlockNumIndexArgs struct { BlockNumber int64 Index int64 @@ -507,21 +524,42 @@ func (args *BlockNumIndexArgs) UnmarshalJSON(b []byte) (err error) { return NewDecodeParamError(err.Error()) } - if len(obj) < 1 { - return NewInsufficientParamsError(len(obj), 1) + if len(obj) < 2 { + return NewInsufficientParamsError(len(obj), 2) } if err := blockHeight(obj[0], &args.BlockNumber); err != nil { return err } - if len(obj) > 1 { - arg1, ok := obj[1].(string) - if !ok { - return NewInvalidTypeError("index", "not a string") - } - args.Index = common.Big(arg1).Int64() + arg1, ok := obj[1].(string) + if !ok { + return NewInvalidTypeError("index", "not a string") } + args.Index = common.Big(arg1).Int64() + + return nil +} + +type HashArgs struct { + Hash string +} + +func (args *HashArgs) UnmarshalJSON(b []byte) (err error) { + var obj []interface{} + if err := json.Unmarshal(b, &obj); err != nil { + return NewDecodeParamError(err.Error()) + } + + if len(obj) < 1 { + return NewInsufficientParamsError(len(obj), 1) + } + + arg0, ok := obj[0].(string) + if !ok { + return NewInvalidTypeError("hash", "not a string") + } + args.Hash = arg0 return nil } @@ -537,8 +575,8 @@ func (args *HashIndexArgs) UnmarshalJSON(b []byte) (err error) { return NewDecodeParamError(err.Error()) } - if len(obj) < 1 { - return NewInsufficientParamsError(len(obj), 1) + if len(obj) < 2 { + return NewInsufficientParamsError(len(obj), 2) } arg0, ok := obj[0].(string) @@ -547,13 +585,11 @@ func (args *HashIndexArgs) UnmarshalJSON(b []byte) (err error) { } args.Hash = arg0 - if len(obj) > 1 { - arg1, ok := obj[1].(string) - if !ok { - return NewInvalidTypeError("index", "not a string") - } - args.Index = common.Big(arg1).Int64() + arg1, ok := obj[1].(string) + if !ok { + return NewInvalidTypeError("index", "not a string") } + args.Index = common.Big(arg1).Int64() return nil } -- cgit v1.2.3 From 015453f5b3f5b78ac8f432aed2247c839a6e5049 Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Thu, 2 Apr 2015 13:55:53 +0200 Subject: Default Value to 0 for NewTxArgs --- rpc/args.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'rpc/args.go') diff --git a/rpc/args.go b/rpc/args.go index b43c465c0..df57e69c4 100644 --- a/rpc/args.go +++ b/rpc/args.go @@ -198,7 +198,7 @@ func (args *NewTxArgs) UnmarshalJSON(b []byte) (err error) { var num int64 if ext.Value == nil { - return NewValidationError("value", "is required") + num = 0 } else { if err := numString(ext.Value, &num); err != nil { return err -- cgit v1.2.3 From b4eef59b6f9631d22fdf62a2b1a40fe05209fccd Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Thu, 2 Apr 2015 14:05:14 +0200 Subject: No longer require gas & gas price in transactions Defaults to 0, which is then set to default values in XEth --- rpc/args.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'rpc/args.go') diff --git a/rpc/args.go b/rpc/args.go index df57e69c4..70618a01a 100644 --- a/rpc/args.go +++ b/rpc/args.go @@ -207,7 +207,7 @@ func (args *NewTxArgs) UnmarshalJSON(b []byte) (err error) { args.Value = big.NewInt(num) if ext.Gas == nil { - return NewValidationError("gas", "is required") + num = 0 } else { if err := numString(ext.Gas, &num); err != nil { return err @@ -216,7 +216,7 @@ func (args *NewTxArgs) UnmarshalJSON(b []byte) (err error) { args.Gas = big.NewInt(num) if ext.GasPrice == nil { - return NewValidationError("gasprice", "is required") + num = 0 } else { if err := numString(ext.GasPrice, &num); err != nil { return err -- cgit v1.2.3