Merge pull request #766 from tgerring/issue762

Treat hexnums as big.Int instead of int64. Closes #762
This commit is contained in:
Jeffrey Wilcke 2015-04-22 02:42:16 -07:00
commit 15550dc8c5

View File

@ -53,22 +53,23 @@ func blockHeight(raw interface{}, number *int64) error {
return nil return nil
} }
func numString(raw interface{}, number *int64) error { func numString(raw interface{}) (*big.Int, error) {
var number *big.Int
// Parse as integer // Parse as integer
num, ok := raw.(float64) num, ok := raw.(float64)
if ok { if ok {
*number = int64(num) number = big.NewInt(int64(num))
return nil return number, nil
} }
// Parse as string/hexstring // Parse as string/hexstring
str, ok := raw.(string) str, ok := raw.(string)
if !ok { if ok {
return NewInvalidTypeError("", "not a number or string") number = common.String2Big(str)
return number, nil
} }
*number = common.String2Big(str).Int64()
return nil return nil, NewInvalidTypeError("", "not a number or string")
} }
// func toNumber(v interface{}) (int64, error) { // func toNumber(v interface{}) (int64, error) {
@ -202,33 +203,36 @@ func (args *NewTxArgs) UnmarshalJSON(b []byte) (err error) {
args.To = ext.To args.To = ext.To
args.Data = ext.Data args.Data = ext.Data
var num int64 var num *big.Int
if ext.Value == nil { if ext.Value == nil {
num = 0 num = big.NewInt(0)
} else { } else {
if err := numString(ext.Value, &num); err != nil { num, err = numString(ext.Value)
if err != nil {
return err return err
} }
} }
args.Value = big.NewInt(num) args.Value = num
num = nil
if ext.Gas == nil { if ext.Gas == nil {
num = 0 num = big.NewInt(0)
} else { } else {
if err := numString(ext.Gas, &num); err != nil { if num, err = numString(ext.Gas); err != nil {
return err return err
} }
} }
args.Gas = big.NewInt(num) args.Gas = num
num = nil
if ext.GasPrice == nil { if ext.GasPrice == nil {
num = 0 num = big.NewInt(0)
} else { } else {
if err := numString(ext.GasPrice, &num); err != nil { if num, err = numString(ext.GasPrice); err != nil {
return err return err
} }
} }
args.GasPrice = big.NewInt(num) args.GasPrice = num
// Check for optional BlockNumber param // Check for optional BlockNumber param
if len(obj) > 1 { if len(obj) > 1 {
@ -286,33 +290,33 @@ func (args *CallArgs) UnmarshalJSON(b []byte) (err error) {
} }
args.To = ext.To args.To = ext.To
var num int64 var num *big.Int
if ext.Value == nil { if ext.Value == nil {
num = int64(0) num = big.NewInt(0)
} else { } else {
if err := numString(ext.Value, &num); err != nil { if num, err = numString(ext.Value); err != nil {
return err return err
} }
} }
args.Value = big.NewInt(num) args.Value = num
if ext.Gas == nil { if ext.Gas == nil {
num = int64(0) num = big.NewInt(0)
} else { } else {
if err := numString(ext.Gas, &num); err != nil { if num, err = numString(ext.Gas); err != nil {
return err return err
} }
} }
args.Gas = big.NewInt(num) args.Gas = num
if ext.GasPrice == nil { if ext.GasPrice == nil {
num = int64(0) num = big.NewInt(0)
} else { } else {
if err := numString(ext.GasPrice, &num); err != nil { if num, err = numString(ext.GasPrice); err != nil {
return err return err
} }
} }
args.GasPrice = big.NewInt(num) args.GasPrice = num
args.Data = ext.Data args.Data = ext.Data
@ -655,6 +659,7 @@ func (args *BlockFilterArgs) UnmarshalJSON(b []byte) (err error) {
// return NewDecodeParamError(fmt.Sprintf("ToBlock %v", err)) // return NewDecodeParamError(fmt.Sprintf("ToBlock %v", err))
var num int64 var num int64
var numBig *big.Int
// if blank then latest // if blank then latest
if obj[0].FromBlock == nil { if obj[0].FromBlock == nil {
@ -682,22 +687,22 @@ func (args *BlockFilterArgs) UnmarshalJSON(b []byte) (err error) {
args.Latest = num args.Latest = num
if obj[0].Limit == nil { if obj[0].Limit == nil {
num = defaultLogLimit numBig = big.NewInt(defaultLogLimit)
} else { } else {
if err := numString(obj[0].Limit, &num); err != nil { if numBig, err = numString(obj[0].Limit); err != nil {
return err return err
} }
} }
args.Max = int(num) args.Max = int(numBig.Int64())
if obj[0].Offset == nil { if obj[0].Offset == nil {
num = defaultLogOffset numBig = big.NewInt(defaultLogOffset)
} else { } else {
if err := numString(obj[0].Offset, &num); err != nil { if numBig, err = numString(obj[0].Offset); err != nil {
return err return err
} }
} }
args.Skip = int(num) args.Skip = int(numBig.Int64())
if obj[0].Address != nil { if obj[0].Address != nil {
marg, ok := obj[0].Address.([]interface{}) marg, ok := obj[0].Address.([]interface{})
@ -894,16 +899,16 @@ func (args *WhisperMessageArgs) UnmarshalJSON(b []byte) (err error) {
args.From = obj[0].From args.From = obj[0].From
args.Topics = obj[0].Topics args.Topics = obj[0].Topics
var num int64 var num *big.Int
if err := numString(obj[0].Priority, &num); err != nil { if num, err = numString(obj[0].Priority); err != nil {
return err return err
} }
args.Priority = uint32(num) args.Priority = uint32(num.Int64())
if err := numString(obj[0].Ttl, &num); err != nil { if num, err = numString(obj[0].Ttl); err != nil {
return err return err
} }
args.Ttl = uint32(num) args.Ttl = uint32(num.Int64())
return nil return nil
} }
@ -973,11 +978,11 @@ func (args *FilterIdArgs) UnmarshalJSON(b []byte) (err error) {
return NewInsufficientParamsError(len(obj), 1) return NewInsufficientParamsError(len(obj), 1)
} }
var num int64 var num *big.Int
if err := numString(obj[0], &num); err != nil { if num, err = numString(obj[0]); err != nil {
return err return err
} }
args.Id = int(num) args.Id = int(num.Int64())
return nil return nil
} }