diff options
Diffstat (limited to 'rpc/args.go')
-rw-r--r-- | rpc/args.go | 103 |
1 files changed, 84 insertions, 19 deletions
diff --git a/rpc/args.go b/rpc/args.go index 504e67c07..5b655024c 100644 --- a/rpc/args.go +++ b/rpc/args.go @@ -8,7 +8,7 @@ import ( "github.com/ethereum/go-ethereum/common" ) -func blockAge(raw interface{}, number *int64) (err error) { +func blockHeight(raw interface{}, number *int64) (err error) { // Parse as integer num, ok := raw.(float64) if ok { @@ -26,7 +26,7 @@ func blockAge(raw interface{}, number *int64) (err error) { case "latest": *number = -1 case "pending": - *number = 0 + *number = -2 default: *number = common.String2Big(str).Int64() } @@ -137,7 +137,7 @@ func (args *NewTxArgs) UnmarshalJSON(b []byte) (err error) { return NewDecodeParamError(err.Error()) } - if err := blockAge(raw, &args.BlockNumber); err != nil { + if err := blockHeight(raw, &args.BlockNumber); err != nil { return err } } @@ -174,7 +174,7 @@ func (args *GetStorageArgs) UnmarshalJSON(b []byte) (err error) { args.Address = addstr if len(obj) > 1 { - if err := blockAge(obj[1], &args.BlockNumber); err != nil { + if err := blockHeight(obj[1], &args.BlockNumber); err != nil { return err } } @@ -218,7 +218,7 @@ func (args *GetStorageAtArgs) UnmarshalJSON(b []byte) (err error) { args.Key = keystr if len(obj) > 2 { - if err := blockAge(obj[2], &args.BlockNumber); err != nil { + if err := blockHeight(obj[2], &args.BlockNumber); err != nil { return err } } @@ -259,7 +259,7 @@ func (args *GetTxCountArgs) UnmarshalJSON(b []byte) (err error) { args.Address = addstr if len(obj) > 1 { - if err := blockAge(obj[1], &args.BlockNumber); err != nil { + if err := blockHeight(obj[1], &args.BlockNumber); err != nil { return err } } @@ -296,7 +296,7 @@ func (args *GetBalanceArgs) UnmarshalJSON(b []byte) (err error) { args.Address = addstr if len(obj) > 1 { - if err := blockAge(obj[1], &args.BlockNumber); err != nil { + if err := blockHeight(obj[1], &args.BlockNumber); err != nil { return err } } @@ -333,7 +333,7 @@ func (args *GetDataArgs) UnmarshalJSON(b []byte) (err error) { args.Address = addstr if len(obj) > 1 { - if err := blockAge(obj[1], &args.BlockNumber); err != nil { + if err := blockHeight(obj[1], &args.BlockNumber); err != nil { return err } } @@ -467,7 +467,7 @@ func (args *BlockFilterArgs) UnmarshalJSON(b []byte) (err error) { switch fromstr { case "latest": - args.Earliest = 0 + args.Earliest = -1 default: args.Earliest = int64(common.Big(obj[0].FromBlock.(string)).Int64()) } @@ -479,9 +479,9 @@ func (args *BlockFilterArgs) UnmarshalJSON(b []byte) (err error) { switch tostr { case "latest": - args.Latest = 0 - case "pending": args.Latest = -1 + case "pending": + args.Latest = -2 default: args.Latest = int64(common.Big(obj[0].ToBlock.(string)).Int64()) } @@ -497,24 +497,39 @@ func (args *BlockFilterArgs) UnmarshalJSON(b []byte) (err error) { type DbArgs struct { Database string Key string - Value string + Value []byte } func (args *DbArgs) UnmarshalJSON(b []byte) (err error) { var obj []interface{} - r := bytes.NewReader(b) - if err := json.NewDecoder(r).Decode(&obj); err != nil { + if err := json.Unmarshal(b, &obj); err != nil { return NewDecodeParamError(err.Error()) } if len(obj) < 2 { return NewInsufficientParamsError(len(obj), 2) } - args.Database = obj[0].(string) - args.Key = obj[1].(string) + + var objstr string + var ok bool + + if objstr, ok = obj[0].(string); !ok { + return NewDecodeParamError("Database is not a string") + } + args.Database = objstr + + if objstr, ok = obj[1].(string); !ok { + return NewDecodeParamError("Key is not a string") + } + args.Key = objstr if len(obj) > 2 { - args.Value = obj[2].(string) + objstr, ok = obj[2].(string) + if !ok { + return NewDecodeParamError("Value is not a string") + } + + args.Value = []byte(objstr) } return nil @@ -530,6 +545,57 @@ func (a *DbArgs) requirements() error { return nil } +type DbHexArgs struct { + Database string + Key string + Value []byte +} + +func (args *DbHexArgs) UnmarshalJSON(b []byte) (err error) { + var obj []interface{} + if err := json.Unmarshal(b, &obj); err != nil { + return NewDecodeParamError(err.Error()) + } + + if len(obj) < 2 { + return NewInsufficientParamsError(len(obj), 2) + } + + var objstr string + var ok bool + + if objstr, ok = obj[0].(string); !ok { + return NewDecodeParamError("Database is not a string") + } + args.Database = objstr + + if objstr, ok = obj[1].(string); !ok { + return NewDecodeParamError("Key is not a string") + } + args.Key = objstr + + if len(obj) > 2 { + objstr, ok = obj[2].(string) + if !ok { + return NewDecodeParamError("Value is not a string") + } + + args.Value = common.FromHex(objstr) + } + + return nil +} + +func (a *DbHexArgs) requirements() error { + if len(a.Database) == 0 { + return NewValidationError("Database", "cannot be blank") + } + if len(a.Key) == 0 { + return NewValidationError("Key", "cannot be blank") + } + return nil +} + type WhisperMessageArgs struct { Payload string To string @@ -709,8 +775,7 @@ func (args *SubmitWorkArgs) UnmarshalJSON(b []byte) (err error) { return NewDecodeParamError("Nonce is not a string") } - args.Nonce = common.BytesToNumber(common.Hex2Bytes(objstr)) - + args.Nonce = common.String2Big(objstr).Uint64() if objstr, ok = obj[1].(string); !ok { return NewDecodeParamError("Header is not a string") } |