diff options
author | Taylor Gerring <taylor.gerring@gmail.com> | 2015-04-02 19:17:55 +0800 |
---|---|---|
committer | Taylor Gerring <taylor.gerring@gmail.com> | 2015-04-02 19:17:55 +0800 |
commit | e402e1dc2e72df2a433b984caeaba771085b2b66 (patch) | |
tree | a9083825ab58d3ff37a1a8137b10d68865d6d565 /rpc/args.go | |
parent | 14c14fd61f72bfa8c1494fa799a4f2e2eb14f4c1 (diff) | |
download | go-tangerine-e402e1dc2e72df2a433b984caeaba771085b2b66.tar go-tangerine-e402e1dc2e72df2a433b984caeaba771085b2b66.tar.gz go-tangerine-e402e1dc2e72df2a433b984caeaba771085b2b66.tar.bz2 go-tangerine-e402e1dc2e72df2a433b984caeaba771085b2b66.tar.lz go-tangerine-e402e1dc2e72df2a433b984caeaba771085b2b66.tar.xz go-tangerine-e402e1dc2e72df2a433b984caeaba771085b2b66.tar.zst go-tangerine-e402e1dc2e72df2a433b984caeaba771085b2b66.zip |
New args types with stricter checking
Diffstat (limited to 'rpc/args.go')
-rw-r--r-- | rpc/args.go | 88 |
1 files changed, 62 insertions, 26 deletions
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 } |