@ -8,10 +8,18 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common"
)
)
func blockNumber ( raw json . RawMessage , number * int64 ) ( err error ) {
func blockAge ( raw interface { } , number * int64 ) ( err error ) {
var str string
// Parse as integer
if err = json . Unmarshal ( raw , & str ) ; err != nil {
num , ok := raw . ( float64 )
return NewDecodeParamError ( err . Error ( ) )
if ok {
* number = int64 ( num )
return nil
}
// Parse as string/hexstring
str , ok := raw . ( string )
if ! ok {
return NewDecodeParamError ( "BlockNumber is not a string" )
}
}
switch str {
switch str {
@ -22,6 +30,7 @@ func blockNumber(raw json.RawMessage, number *int64) (err error) {
default :
default :
* number = common . String2Big ( str ) . Int64 ( )
* number = common . String2Big ( str ) . Int64 ( )
}
}
return nil
return nil
}
}
@ -95,18 +104,51 @@ type NewTxArgs struct {
}
}
func ( args * NewTxArgs ) UnmarshalJSON ( b [ ] byte ) ( err error ) {
func ( args * NewTxArgs ) UnmarshalJSON ( b [ ] byte ) ( err error ) {
var obj struct { From , To , Value , Gas , GasPrice , Data string }
var obj [ ] json . RawMessage
if err = UnmarshalRawMessages ( b , & obj , & args . BlockNumber ) ; err != nil {
var ext struct { From , To , Value , Gas , GasPrice , Data string }
// Decode byte slice to array of RawMessages
if err := json . Unmarshal ( b , & obj ) ; err != nil {
return NewDecodeParamError ( err . Error ( ) )
}
// Check for sufficient params
if len ( obj ) < 1 {
return NewInsufficientParamsError ( len ( obj ) , 1 )
}
// Decode 0th RawMessage to temporary struct
if err := json . Unmarshal ( obj [ 0 ] , & ext ) ; err != nil {
return NewDecodeParamError ( err . Error ( ) )
}
// var ok bool
args . From = ext . From
args . To = ext . To
args . Value = common . String2Big ( ext . Value )
args . Gas = common . String2Big ( ext . Gas )
args . GasPrice = common . String2Big ( ext . GasPrice )
args . Data = ext . Data
// Check for optional BlockNumber param
if len ( obj ) > 1 {
var raw interface { }
if err = json . Unmarshal ( obj [ 1 ] , & raw ) ; err != nil {
return NewDecodeParamError ( err . Error ( ) )
}
if err := blockAge ( raw , & args . BlockNumber ) ; err != nil {
return err
return err
}
}
}
args . From = obj . From
return nil
args . To = obj . To
}
args . Value = common . Big ( obj . Value )
args . Gas = common . Big ( obj . Gas )
args . GasPrice = common . Big ( obj . GasPrice )
args . Data = obj . Data
func ( args * NewTxArgs ) requirements ( ) error {
if len ( args . From ) == 0 {
return NewValidationError ( "From" , "Is required" )
}
return nil
return nil
}
}
@ -116,10 +158,27 @@ type GetStorageArgs struct {
}
}
func ( args * GetStorageArgs ) UnmarshalJSON ( b [ ] byte ) ( err error ) {
func ( args * GetStorageArgs ) UnmarshalJSON ( b [ ] byte ) ( err error ) {
if err = UnmarshalRawMessages ( b , & args . Address , & args . BlockNumber ) ; err != nil {
var obj [ ] interface { }
if err := json . Unmarshal ( b , & obj ) ; err != nil {
return NewDecodeParamError ( err . Error ( ) )
return NewDecodeParamError ( err . Error ( ) )
}
}
if len ( obj ) < 1 {
return NewInsufficientParamsError ( len ( obj ) , 1 )
}
addstr , ok := obj [ 0 ] . ( string )
if ! ok {
return NewDecodeParamError ( "Address is not a string" )
}
args . Address = addstr
if len ( obj ) > 1 {
if err := blockAge ( obj [ 1 ] , & args . BlockNumber ) ; err != nil {
return err
}
}
return nil
return nil
}
}
@ -137,16 +196,32 @@ type GetStorageAtArgs struct {
}
}
func ( args * GetStorageAtArgs ) UnmarshalJSON ( b [ ] byte ) ( err error ) {
func ( args * GetStorageAtArgs ) UnmarshalJSON ( b [ ] byte ) ( err error ) {
var obj [ ] string
var obj [ ] interface { }
if err = UnmarshalRawMessages ( b , & obj , & args . BlockNumber ) ; err != nil {
if err := json . Unmarshal ( b , & obj ) ; err != nil {
return NewDecodeParamError ( err . Error ( ) )
return NewDecodeParamError ( err . Error ( ) )
}
}
if len ( obj ) < 2 {
if len ( obj ) < 2 {
return NewInsufficientParamsError ( len ( obj ) , 2 )
return NewInsufficientParamsError ( len ( obj ) , 2 )
}
}
args . Address = obj [ 0 ]
addstr , ok := obj [ 0 ] . ( string )
args . Key = obj [ 1 ]
if ! ok {
return NewDecodeParamError ( "Address is not a string" )
}
args . Address = addstr
keystr , ok := obj [ 1 ] . ( string )
if ! ok {
return NewDecodeParamError ( "Key is not a string" )
}
args . Key = keystr
if len ( obj ) > 2 {
if err := blockAge ( obj [ 2 ] , & args . BlockNumber ) ; err != nil {
return err
}
}
return nil
return nil
}
}
@ -168,10 +243,27 @@ type GetTxCountArgs struct {
}
}
func ( args * GetTxCountArgs ) UnmarshalJSON ( b [ ] byte ) ( err error ) {
func ( args * GetTxCountArgs ) UnmarshalJSON ( b [ ] byte ) ( err error ) {
if err = UnmarshalRawMessages ( b , & args . Address , & args . BlockNumber ) ; err != nil {
var obj [ ] interface { }
if err := json . Unmarshal ( b , & obj ) ; err != nil {
return NewDecodeParamError ( err . Error ( ) )
return NewDecodeParamError ( err . Error ( ) )
}
}
if len ( obj ) < 1 {
return NewInsufficientParamsError ( len ( obj ) , 1 )
}
addstr , ok := obj [ 0 ] . ( string )
if ! ok {
return NewDecodeParamError ( "Address is not a string" )
}
args . Address = addstr
if len ( obj ) > 1 {
if err := blockAge ( obj [ 1 ] , & args . BlockNumber ) ; err != nil {
return err
}
}
return nil
return nil
}
}
@ -189,8 +281,7 @@ type GetBalanceArgs struct {
func ( args * GetBalanceArgs ) UnmarshalJSON ( b [ ] byte ) ( err error ) {
func ( args * GetBalanceArgs ) UnmarshalJSON ( b [ ] byte ) ( err error ) {
var obj [ ] interface { }
var obj [ ] interface { }
r := bytes . NewReader ( b )
if err := json . Unmarshal ( b , & obj ) ; err != nil {
if err := json . NewDecoder ( r ) . Decode ( & obj ) ; err != nil {
return NewDecodeParamError ( err . Error ( ) )
return NewDecodeParamError ( err . Error ( ) )
}
}
@ -205,17 +296,11 @@ func (args *GetBalanceArgs) UnmarshalJSON(b []byte) (err error) {
args . Address = addstr
args . Address = addstr
if len ( obj ) > 1 {
if len ( obj ) > 1 {
if obj [ 1 ] . ( string ) == "latest" {
if err := blockAge ( obj [ 1 ] , & args . BlockNumber ) ; err != nil {
args . BlockNumber = - 1
return err
} else {
args . BlockNumber = common . Big ( obj [ 1 ] . ( string ) ) . Int64 ( )
}
}
}
}
// if err = UnmarshalRawMessages(b, &args.Address, &args.BlockNumber); err != nil {
// return NewDecodeParamError(err.Error())
// }
return nil
return nil
}
}
@ -232,10 +317,27 @@ type GetDataArgs struct {
}
}
func ( args * GetDataArgs ) UnmarshalJSON ( b [ ] byte ) ( err error ) {
func ( args * GetDataArgs ) UnmarshalJSON ( b [ ] byte ) ( err error ) {
if err = UnmarshalRawMessages ( b , & args . Address , & args . BlockNumber ) ; err != nil {
var obj [ ] interface { }
if err := json . Unmarshal ( b , & obj ) ; err != nil {
return NewDecodeParamError ( err . Error ( ) )
return NewDecodeParamError ( err . Error ( ) )
}
}
if len ( obj ) < 1 {
return NewInsufficientParamsError ( len ( obj ) , 1 )
}
addstr , ok := obj [ 0 ] . ( string )
if ! ok {
return NewDecodeParamError ( "Address is not a string" )
}
args . Address = addstr
if len ( obj ) > 1 {
if err := blockAge ( obj [ 1 ] , & args . BlockNumber ) ; err != nil {
return err
}
}
return nil
return nil
}
}
@ -392,10 +494,6 @@ func (args *FilterOptions) UnmarshalJSON(b []byte) (err error) {
return nil
return nil
}
}
// type FilterChangedArgs struct {
// n int
// }
type DbArgs struct {
type DbArgs struct {
Database string
Database string
Key string
Key string
@ -578,31 +676,3 @@ func (args *WhisperFilterArgs) UnmarshalJSON(b []byte) (err error) {
return nil
return nil
}
}
// func (req *RpcRequest) ToRegisterArgs() (string, error) {
// if len(req.Params) < 1 {
// return "", errArguments
// }
// var args string
// err := json.Unmarshal(req.Params, &args)
// if err != nil {
// return "", err
// }
// return args, nil
// }
// func (req *RpcRequest) ToWatchTxArgs() (string, error) {
// if len(req.Params) < 1 {
// return "", errArguments
// }
// var args string
// err := json.Unmarshal(req.Params, &args)
// if err != nil {
// return "", err
// }
// return args, nil
// }