From 0ce971d9d396521697b56efeb60e28fe40bd0015 Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Wed, 1 Apr 2015 15:27:14 +0200 Subject: Add new formatting regex --- rpc/responses_test.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) (limited to 'rpc') diff --git a/rpc/responses_test.go b/rpc/responses_test.go index 43924151a..20bc8e97d 100644 --- a/rpc/responses_test.go +++ b/rpc/responses_test.go @@ -13,12 +13,13 @@ import ( ) const ( - reHash = `"0x[0-9a-f]{64}"` // 32 bytes - reHashOpt = `"(0x[0-9a-f]{64})"|null` // 32 bytes or null - reAddress = `"0x[0-9a-f]{40}"` // 20 bytes - reAddressOpt = `"0x[0-9a-f]{40}"|null` // 20 bytes or null - reNum = `"0x([1-9a-f][0-9a-f]{1,15})|0"` // must not have left-padded zeros - reData = `"0x[0-9a-f]*"` // can be "empty" + reHash = `"0x[0-9a-f]{64}"` // 32 bytes + reHashOpt = `"(0x[0-9a-f]{64})"|null` // 32 bytes or null + reAddress = `"0x[0-9a-f]{40}"` // 20 bytes + reAddressOpt = `"0x[0-9a-f]{40}"|null` // 20 bytes or null + reNum = `"0x([1-9a-f][0-9a-f]{0,15})|0"` // must not have left-padded zeros + reNumOpt = `"0x([1-9a-f][0-9a-f]{0,15})|0"|null` // must not have left-padded zeros or null + reData = `"0x[0-9a-f]*"` // can be "empty" ) func TestNewBlockRes(t *testing.T) { -- cgit From 93f832a1a76430ebb18276aaf2a3f18d000edae2 Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Wed, 1 Apr 2015 15:27:37 +0200 Subject: Make block context optional nulls --- rpc/responses_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'rpc') diff --git a/rpc/responses_test.go b/rpc/responses_test.go index 20bc8e97d..702fc5c22 100644 --- a/rpc/responses_test.go +++ b/rpc/responses_test.go @@ -72,9 +72,9 @@ func TestNewTransactionRes(t *testing.T) { tests := map[string]string{ "hash": reHash, "nonce": reNum, - "blockHash": reHash, - "blockNum": reNum, - "transactionIndex": reNum, + "blockHash": reHashOpt, + "blockNum": reNumOpt, + "transactionIndex": reNumOpt, "from": reAddress, "to": reAddressOpt, "value": reNum, -- cgit From 6e8ff578f18473d7c4fe013a88d2bece1ecf65d1 Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Wed, 1 Apr 2015 15:28:06 +0200 Subject: Block nonce as data --- rpc/responses.go | 4 ++-- rpc/responses_test.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'rpc') diff --git a/rpc/responses.go b/rpc/responses.go index 45a2fa18b..0f67299fc 100644 --- a/rpc/responses.go +++ b/rpc/responses.go @@ -11,7 +11,7 @@ type BlockRes struct { BlockNumber *hexnum `json:"number"` BlockHash *hexdata `json:"hash"` ParentHash *hexdata `json:"parentHash"` - Nonce *hexnum `json:"nonce"` + Nonce *hexdata `json:"nonce"` Sha3Uncles *hexdata `json:"sha3Uncles"` LogsBloom *hexdata `json:"logsBloom"` TransactionRoot *hexdata `json:"transactionRoot"` @@ -41,7 +41,7 @@ func NewBlockRes(block *types.Block, fullTx bool) *BlockRes { res.BlockNumber = newHexNum(block.Number()) res.BlockHash = newHexData(block.Hash()) res.ParentHash = newHexData(block.ParentHash()) - res.Nonce = newHexNum(block.Header().Nonce) + res.Nonce = newHexData(block.Header().Nonce) res.Sha3Uncles = newHexData(block.Header().UncleHash) res.LogsBloom = newHexData(block.Bloom()) res.TransactionRoot = newHexData(block.Header().TxHash) diff --git a/rpc/responses_test.go b/rpc/responses_test.go index 702fc5c22..949e1794b 100644 --- a/rpc/responses_test.go +++ b/rpc/responses_test.go @@ -34,7 +34,7 @@ func TestNewBlockRes(t *testing.T) { "number": reNum, "hash": reHash, "parentHash": reHash, - "nonce": reNum, + "nonce": reData, "sha3Uncles": reHash, "logsBloom": reData, "transactionRoot": reHash, -- cgit From ac03ff6f055ef2bd9404a30a0c257fdd2722eafe Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Wed, 1 Apr 2015 15:44:09 +0200 Subject: Fix block size output #613 --- rpc/responses.go | 2 +- rpc/responses_test.go | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) (limited to 'rpc') diff --git a/rpc/responses.go b/rpc/responses.go index 0f67299fc..41776a0be 100644 --- a/rpc/responses.go +++ b/rpc/responses.go @@ -49,7 +49,7 @@ func NewBlockRes(block *types.Block, fullTx bool) *BlockRes { res.Miner = newHexData(block.Header().Coinbase) res.Difficulty = newHexNum(block.Difficulty()) res.TotalDifficulty = newHexNum(block.Td) - res.Size = newHexNum(block.Size()) + res.Size = newHexNum(block.Size().Int64()) res.ExtraData = newHexData(block.Header().Extra) res.GasLimit = newHexNum(block.GasLimit()) // res.MinGasPrice = diff --git a/rpc/responses_test.go b/rpc/responses_test.go index 949e1794b..704a38186 100644 --- a/rpc/responses_test.go +++ b/rpc/responses_test.go @@ -18,6 +18,7 @@ const ( reAddress = `"0x[0-9a-f]{40}"` // 20 bytes reAddressOpt = `"0x[0-9a-f]{40}"|null` // 20 bytes or null reNum = `"0x([1-9a-f][0-9a-f]{0,15})|0"` // must not have left-padded zeros + reNumNonZero = `"0x([1-9a-f][0-9a-f]{0,15})"` // non-zero required must not have left-padded zeros reNumOpt = `"0x([1-9a-f][0-9a-f]{0,15})|0"|null` // must not have left-padded zeros or null reData = `"0x[0-9a-f]*"` // can be "empty" ) @@ -42,7 +43,7 @@ func TestNewBlockRes(t *testing.T) { "miner": reAddress, "difficulty": `"0x1"`, "totalDifficulty": reNum, - "size": reNum, + "size": reNumNonZero, "extraData": reData, "gasLimit": reNum, // "minGasPrice": "0x", -- cgit From bea3879d6f5862886948b9045b6c6111b41f8480 Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Wed, 1 Apr 2015 15:45:56 +0200 Subject: If nil, type doesn't matter --- rpc/messages.go | 3 +++ 1 file changed, 3 insertions(+) (limited to 'rpc') diff --git a/rpc/messages.go b/rpc/messages.go index 3c011cd93..549268cce 100644 --- a/rpc/messages.go +++ b/rpc/messages.go @@ -45,6 +45,9 @@ func (d *hexdata) UnmarshalJSON(b []byte) (err error) { func newHexData(input interface{}) *hexdata { d := new(hexdata) + if input == nil { + d.data = nil + } switch input := input.(type) { case []byte: d.data = input -- cgit From 55b9689950dbb3f47e2d4720d04d2539243429bd Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Wed, 1 Apr 2015 15:53:48 +0200 Subject: rename messages to types --- rpc/messages.go | 220 --------------------------------------------------- rpc/messages_test.go | 50 ------------ rpc/types.go | 220 +++++++++++++++++++++++++++++++++++++++++++++++++++ rpc/types_test.go | 50 ++++++++++++ 4 files changed, 270 insertions(+), 270 deletions(-) delete mode 100644 rpc/messages.go delete mode 100644 rpc/messages_test.go create mode 100644 rpc/types.go create mode 100644 rpc/types_test.go (limited to 'rpc') diff --git a/rpc/messages.go b/rpc/messages.go deleted file mode 100644 index 549268cce..000000000 --- a/rpc/messages.go +++ /dev/null @@ -1,220 +0,0 @@ -/* - This file is part of go-ethereum - - go-ethereum is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - go-ethereum is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with go-ethereum. If not, see . -*/ -package rpc - -import ( - "encoding/json" - "fmt" - "math/big" - "strings" - - "github.com/ethereum/go-ethereum/common" -) - -type hexdata struct { - data []byte -} - -func (d *hexdata) String() string { - return "0x" + common.Bytes2Hex(d.data) -} - -func (d *hexdata) MarshalJSON() ([]byte, error) { - return json.Marshal(d.String()) -} - -func (d *hexdata) UnmarshalJSON(b []byte) (err error) { - d.data = common.FromHex(string(b)) - return nil -} - -func newHexData(input interface{}) *hexdata { - d := new(hexdata) - - if input == nil { - d.data = nil - } - switch input := input.(type) { - case []byte: - d.data = input - case common.Hash: - d.data = input.Bytes() - case *common.Hash: - d.data = input.Bytes() - case common.Address: - d.data = input.Bytes() - case *common.Address: - d.data = input.Bytes() - case *big.Int: - d.data = input.Bytes() - case int64: - d.data = big.NewInt(input).Bytes() - case uint64: - d.data = big.NewInt(int64(input)).Bytes() - case int: - d.data = big.NewInt(int64(input)).Bytes() - case uint: - d.data = big.NewInt(int64(input)).Bytes() - case string: // hexstring - d.data = common.Big(input).Bytes() - default: - d.data = nil - } - - return d -} - -type hexnum struct { - data []byte -} - -func (d *hexnum) String() string { - // Get hex string from bytes - out := common.Bytes2Hex(d.data) - // Trim leading 0s - out = strings.Trim(out, "0") - // Output "0x0" when value is 0 - if len(out) == 0 { - out = "0" - } - return "0x" + out -} - -func (d *hexnum) MarshalJSON() ([]byte, error) { - return json.Marshal(d.String()) -} - -func (d *hexnum) UnmarshalJSON(b []byte) (err error) { - d.data = common.FromHex(string(b)) - return nil -} - -func newHexNum(input interface{}) *hexnum { - d := new(hexnum) - - d.data = newHexData(input).data - - return d -} - -type RpcConfig struct { - ListenAddress string - ListenPort uint - CorsDomain string -} - -type InvalidTypeError struct { - method string - msg string -} - -func (e *InvalidTypeError) Error() string { - return fmt.Sprintf("invalid type on field %s: %s", e.method, e.msg) -} - -func NewInvalidTypeError(method, msg string) *InvalidTypeError { - return &InvalidTypeError{ - method: method, - msg: msg, - } -} - -type InsufficientParamsError struct { - have int - want int -} - -func (e *InsufficientParamsError) Error() string { - return fmt.Sprintf("insufficient params, want %d have %d", e.want, e.have) -} - -func NewInsufficientParamsError(have int, want int) *InsufficientParamsError { - return &InsufficientParamsError{ - have: have, - want: want, - } -} - -type NotImplementedError struct { - Method string -} - -func (e *NotImplementedError) Error() string { - return fmt.Sprintf("%s method not implemented", e.Method) -} - -func NewNotImplementedError(method string) *NotImplementedError { - return &NotImplementedError{ - Method: method, - } -} - -type DecodeParamError struct { - err string -} - -func (e *DecodeParamError) Error() string { - return fmt.Sprintf("could not decode, %s", e.err) - -} - -func NewDecodeParamError(errstr string) error { - return &DecodeParamError{ - err: errstr, - } -} - -type ValidationError struct { - ParamName string - msg string -} - -func (e *ValidationError) Error() string { - return fmt.Sprintf("%s not valid, %s", e.ParamName, e.msg) -} - -func NewValidationError(param string, msg string) error { - return &ValidationError{ - ParamName: param, - msg: msg, - } -} - -type RpcRequest struct { - Id interface{} `json:"id"` - Jsonrpc string `json:"jsonrpc"` - Method string `json:"method"` - Params json.RawMessage `json:"params"` -} - -type RpcSuccessResponse struct { - Id interface{} `json:"id"` - Jsonrpc string `json:"jsonrpc"` - Result interface{} `json:"result"` -} - -type RpcErrorResponse struct { - Id interface{} `json:"id"` - Jsonrpc string `json:"jsonrpc"` - Error *RpcErrorObject `json:"error"` -} - -type RpcErrorObject struct { - Code int `json:"code"` - Message string `json:"message"` - // Data interface{} `json:"data"` -} diff --git a/rpc/messages_test.go b/rpc/messages_test.go deleted file mode 100644 index 91f0152dc..000000000 --- a/rpc/messages_test.go +++ /dev/null @@ -1,50 +0,0 @@ -package rpc - -import ( - "testing" -) - -func TestInvalidTypeError(t *testing.T) { - err := NewInvalidTypeError("testField", "not string") - expected := "invalid type on field testField: not string" - - if err.Error() != expected { - t.Error(err.Error()) - } -} - -func TestInsufficientParamsError(t *testing.T) { - err := NewInsufficientParamsError(0, 1) - expected := "insufficient params, want 1 have 0" - - if err.Error() != expected { - t.Error(err.Error()) - } -} - -func TestNotImplementedError(t *testing.T) { - err := NewNotImplementedError("foo") - expected := "foo method not implemented" - - if err.Error() != expected { - t.Error(err.Error()) - } -} - -func TestDecodeParamError(t *testing.T) { - err := NewDecodeParamError("foo") - expected := "could not decode, foo" - - if err.Error() != expected { - t.Error(err.Error()) - } -} - -func TestValidationError(t *testing.T) { - err := NewValidationError("foo", "should be `bar`") - expected := "foo not valid, should be `bar`" - - if err.Error() != expected { - t.Error(err.Error()) - } -} diff --git a/rpc/types.go b/rpc/types.go new file mode 100644 index 000000000..549268cce --- /dev/null +++ b/rpc/types.go @@ -0,0 +1,220 @@ +/* + This file is part of go-ethereum + + go-ethereum is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + go-ethereum is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with go-ethereum. If not, see . +*/ +package rpc + +import ( + "encoding/json" + "fmt" + "math/big" + "strings" + + "github.com/ethereum/go-ethereum/common" +) + +type hexdata struct { + data []byte +} + +func (d *hexdata) String() string { + return "0x" + common.Bytes2Hex(d.data) +} + +func (d *hexdata) MarshalJSON() ([]byte, error) { + return json.Marshal(d.String()) +} + +func (d *hexdata) UnmarshalJSON(b []byte) (err error) { + d.data = common.FromHex(string(b)) + return nil +} + +func newHexData(input interface{}) *hexdata { + d := new(hexdata) + + if input == nil { + d.data = nil + } + switch input := input.(type) { + case []byte: + d.data = input + case common.Hash: + d.data = input.Bytes() + case *common.Hash: + d.data = input.Bytes() + case common.Address: + d.data = input.Bytes() + case *common.Address: + d.data = input.Bytes() + case *big.Int: + d.data = input.Bytes() + case int64: + d.data = big.NewInt(input).Bytes() + case uint64: + d.data = big.NewInt(int64(input)).Bytes() + case int: + d.data = big.NewInt(int64(input)).Bytes() + case uint: + d.data = big.NewInt(int64(input)).Bytes() + case string: // hexstring + d.data = common.Big(input).Bytes() + default: + d.data = nil + } + + return d +} + +type hexnum struct { + data []byte +} + +func (d *hexnum) String() string { + // Get hex string from bytes + out := common.Bytes2Hex(d.data) + // Trim leading 0s + out = strings.Trim(out, "0") + // Output "0x0" when value is 0 + if len(out) == 0 { + out = "0" + } + return "0x" + out +} + +func (d *hexnum) MarshalJSON() ([]byte, error) { + return json.Marshal(d.String()) +} + +func (d *hexnum) UnmarshalJSON(b []byte) (err error) { + d.data = common.FromHex(string(b)) + return nil +} + +func newHexNum(input interface{}) *hexnum { + d := new(hexnum) + + d.data = newHexData(input).data + + return d +} + +type RpcConfig struct { + ListenAddress string + ListenPort uint + CorsDomain string +} + +type InvalidTypeError struct { + method string + msg string +} + +func (e *InvalidTypeError) Error() string { + return fmt.Sprintf("invalid type on field %s: %s", e.method, e.msg) +} + +func NewInvalidTypeError(method, msg string) *InvalidTypeError { + return &InvalidTypeError{ + method: method, + msg: msg, + } +} + +type InsufficientParamsError struct { + have int + want int +} + +func (e *InsufficientParamsError) Error() string { + return fmt.Sprintf("insufficient params, want %d have %d", e.want, e.have) +} + +func NewInsufficientParamsError(have int, want int) *InsufficientParamsError { + return &InsufficientParamsError{ + have: have, + want: want, + } +} + +type NotImplementedError struct { + Method string +} + +func (e *NotImplementedError) Error() string { + return fmt.Sprintf("%s method not implemented", e.Method) +} + +func NewNotImplementedError(method string) *NotImplementedError { + return &NotImplementedError{ + Method: method, + } +} + +type DecodeParamError struct { + err string +} + +func (e *DecodeParamError) Error() string { + return fmt.Sprintf("could not decode, %s", e.err) + +} + +func NewDecodeParamError(errstr string) error { + return &DecodeParamError{ + err: errstr, + } +} + +type ValidationError struct { + ParamName string + msg string +} + +func (e *ValidationError) Error() string { + return fmt.Sprintf("%s not valid, %s", e.ParamName, e.msg) +} + +func NewValidationError(param string, msg string) error { + return &ValidationError{ + ParamName: param, + msg: msg, + } +} + +type RpcRequest struct { + Id interface{} `json:"id"` + Jsonrpc string `json:"jsonrpc"` + Method string `json:"method"` + Params json.RawMessage `json:"params"` +} + +type RpcSuccessResponse struct { + Id interface{} `json:"id"` + Jsonrpc string `json:"jsonrpc"` + Result interface{} `json:"result"` +} + +type RpcErrorResponse struct { + Id interface{} `json:"id"` + Jsonrpc string `json:"jsonrpc"` + Error *RpcErrorObject `json:"error"` +} + +type RpcErrorObject struct { + Code int `json:"code"` + Message string `json:"message"` + // Data interface{} `json:"data"` +} diff --git a/rpc/types_test.go b/rpc/types_test.go new file mode 100644 index 000000000..91f0152dc --- /dev/null +++ b/rpc/types_test.go @@ -0,0 +1,50 @@ +package rpc + +import ( + "testing" +) + +func TestInvalidTypeError(t *testing.T) { + err := NewInvalidTypeError("testField", "not string") + expected := "invalid type on field testField: not string" + + if err.Error() != expected { + t.Error(err.Error()) + } +} + +func TestInsufficientParamsError(t *testing.T) { + err := NewInsufficientParamsError(0, 1) + expected := "insufficient params, want 1 have 0" + + if err.Error() != expected { + t.Error(err.Error()) + } +} + +func TestNotImplementedError(t *testing.T) { + err := NewNotImplementedError("foo") + expected := "foo method not implemented" + + if err.Error() != expected { + t.Error(err.Error()) + } +} + +func TestDecodeParamError(t *testing.T) { + err := NewDecodeParamError("foo") + expected := "could not decode, foo" + + if err.Error() != expected { + t.Error(err.Error()) + } +} + +func TestValidationError(t *testing.T) { + err := NewValidationError("foo", "should be `bar`") + expected := "foo not valid, should be `bar`" + + if err.Error() != expected { + t.Error(err.Error()) + } +} -- cgit From b6f0b400375dfe3ae1ada954f14fa15155163f1e Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Wed, 1 Apr 2015 16:49:07 +0200 Subject: Respect fullTx option #614 --- rpc/responses.go | 95 +++++++++++++++++++++++++++++++++++++++++++++++++++ rpc/responses_test.go | 63 ++++++++++++++++++++++++++++++++++ 2 files changed, 158 insertions(+) (limited to 'rpc') diff --git a/rpc/responses.go b/rpc/responses.go index 41776a0be..172a0902f 100644 --- a/rpc/responses.go +++ b/rpc/responses.go @@ -1,6 +1,8 @@ package rpc import ( + "encoding/json" + "github.com/ethereum/go-ethereum/core/state" "github.com/ethereum/go-ethereum/core/types" ) @@ -29,6 +31,99 @@ type BlockRes struct { Uncles []*hexdata `json:"uncles"` } +func (b *BlockRes) MarshalJSON() ([]byte, error) { + if b.fullTx { + var ext struct { + BlockNumber *hexnum `json:"number"` + BlockHash *hexdata `json:"hash"` + ParentHash *hexdata `json:"parentHash"` + Nonce *hexdata `json:"nonce"` + Sha3Uncles *hexdata `json:"sha3Uncles"` + LogsBloom *hexdata `json:"logsBloom"` + TransactionRoot *hexdata `json:"transactionRoot"` + StateRoot *hexdata `json:"stateRoot"` + Miner *hexdata `json:"miner"` + Difficulty *hexnum `json:"difficulty"` + TotalDifficulty *hexnum `json:"totalDifficulty"` + Size *hexnum `json:"size"` + ExtraData *hexdata `json:"extraData"` + GasLimit *hexnum `json:"gasLimit"` + MinGasPrice *hexnum `json:"minGasPrice"` + GasUsed *hexnum `json:"gasUsed"` + UnixTimestamp *hexnum `json:"timestamp"` + Transactions []*TransactionRes `json:"transactions"` + Uncles []*hexdata `json:"uncles"` + } + + ext.BlockNumber = b.BlockNumber + ext.BlockHash = b.BlockHash + ext.ParentHash = b.ParentHash + ext.Nonce = b.Nonce + ext.Sha3Uncles = b.Sha3Uncles + ext.LogsBloom = b.LogsBloom + ext.TransactionRoot = b.TransactionRoot + ext.StateRoot = b.StateRoot + ext.Miner = b.Miner + ext.Difficulty = b.Difficulty + ext.TotalDifficulty = b.TotalDifficulty + ext.Size = b.Size + ext.ExtraData = b.ExtraData + ext.GasLimit = b.GasLimit + ext.MinGasPrice = b.MinGasPrice + ext.GasUsed = b.GasUsed + ext.UnixTimestamp = b.UnixTimestamp + ext.Transactions = b.Transactions + ext.Uncles = b.Uncles + return json.Marshal(ext) + } else { + var ext struct { + BlockNumber *hexnum `json:"number"` + BlockHash *hexdata `json:"hash"` + ParentHash *hexdata `json:"parentHash"` + Nonce *hexdata `json:"nonce"` + Sha3Uncles *hexdata `json:"sha3Uncles"` + LogsBloom *hexdata `json:"logsBloom"` + TransactionRoot *hexdata `json:"transactionRoot"` + StateRoot *hexdata `json:"stateRoot"` + Miner *hexdata `json:"miner"` + Difficulty *hexnum `json:"difficulty"` + TotalDifficulty *hexnum `json:"totalDifficulty"` + Size *hexnum `json:"size"` + ExtraData *hexdata `json:"extraData"` + GasLimit *hexnum `json:"gasLimit"` + MinGasPrice *hexnum `json:"minGasPrice"` + GasUsed *hexnum `json:"gasUsed"` + UnixTimestamp *hexnum `json:"timestamp"` + Transactions []*hexdata `json:"transactions"` + Uncles []*hexdata `json:"uncles"` + } + + ext.BlockNumber = b.BlockNumber + ext.BlockHash = b.BlockHash + ext.ParentHash = b.ParentHash + ext.Nonce = b.Nonce + ext.Sha3Uncles = b.Sha3Uncles + ext.LogsBloom = b.LogsBloom + ext.TransactionRoot = b.TransactionRoot + ext.StateRoot = b.StateRoot + ext.Miner = b.Miner + ext.Difficulty = b.Difficulty + ext.TotalDifficulty = b.TotalDifficulty + ext.Size = b.Size + ext.ExtraData = b.ExtraData + ext.GasLimit = b.GasLimit + ext.MinGasPrice = b.MinGasPrice + ext.GasUsed = b.GasUsed + ext.UnixTimestamp = b.UnixTimestamp + ext.Transactions = make([]*hexdata, len(b.Transactions)) + for i, tx := range b.Transactions { + ext.Transactions[i] = tx.Hash + } + ext.Uncles = b.Uncles + return json.Marshal(ext) + } +} + func NewBlockRes(block *types.Block, fullTx bool) *BlockRes { // TODO respect fullTx flag diff --git a/rpc/responses_test.go b/rpc/responses_test.go index 704a38186..0f911c886 100644 --- a/rpc/responses_test.go +++ b/rpc/responses_test.go @@ -21,6 +21,8 @@ const ( reNumNonZero = `"0x([1-9a-f][0-9a-f]{0,15})"` // non-zero required must not have left-padded zeros reNumOpt = `"0x([1-9a-f][0-9a-f]{0,15})|0"|null` // must not have left-padded zeros or null reData = `"0x[0-9a-f]*"` // can be "empty" + // reListHash = `[("\w":"0x[0-9a-f]{64}",?)*]` + // reListObj = `[("\w":(".+"|null),?)*]` ) func TestNewBlockRes(t *testing.T) { @@ -49,9 +51,70 @@ func TestNewBlockRes(t *testing.T) { // "minGasPrice": "0x", "gasUsed": reNum, "timestamp": reNum, + // "transactions": reListHash, + // "uncles": reListHash, } + to := common.HexToAddress("0x02") + amount := big.NewInt(1) + gasAmount := big.NewInt(1) + gasPrice := big.NewInt(1) + data := []byte{1, 2, 3} + tx := types.NewTransactionMessage(to, amount, gasAmount, gasPrice, data) + v := NewBlockRes(block, false) + v.Transactions = make([]*TransactionRes, 1) + v.Transactions[0] = NewTransactionRes(tx) + j, _ := json.Marshal(v) + + for k, re := range tests { + match, _ := regexp.MatchString(fmt.Sprintf(`{.*"%s":%s.*}`, k, re), string(j)) + if !match { + t.Error(fmt.Sprintf("%s output json does not match format %s. Got %s", k, re, j)) + } + } +} + +func TestNewBlockResWithTrans(t *testing.T) { + parentHash := common.HexToHash("0x01") + coinbase := common.HexToAddress("0x01") + root := common.HexToHash("0x01") + difficulty := common.Big1 + nonce := uint64(1) + extra := "" + block := types.NewBlock(parentHash, coinbase, root, difficulty, nonce, extra) + tests := map[string]string{ + "number": reNum, + "hash": reHash, + "parentHash": reHash, + "nonce": reData, + "sha3Uncles": reHash, + "logsBloom": reData, + "transactionRoot": reHash, + "stateRoot": reHash, + "miner": reAddress, + "difficulty": `"0x1"`, + "totalDifficulty": reNum, + "size": reNumNonZero, + "extraData": reData, + "gasLimit": reNum, + // "minGasPrice": "0x", + "gasUsed": reNum, + "timestamp": reNum, + // "transactions": `[{.*}]`, + // "uncles": reListHash, + } + + to := common.HexToAddress("0x02") + amount := big.NewInt(1) + gasAmount := big.NewInt(1) + gasPrice := big.NewInt(1) + data := []byte{1, 2, 3} + tx := types.NewTransactionMessage(to, amount, gasAmount, gasPrice, data) + + v := NewBlockRes(block, true) + v.Transactions = make([]*TransactionRes, 1) + v.Transactions[0] = NewTransactionRes(tx) j, _ := json.Marshal(v) for k, re := range tests { -- cgit From 1045015a3c8df048d80c0f66598a8a8fa09b2f71 Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Wed, 1 Apr 2015 17:48:53 +0200 Subject: Update nonce field --- rpc/responses.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'rpc') diff --git a/rpc/responses.go b/rpc/responses.go index 172a0902f..bf4f518aa 100644 --- a/rpc/responses.go +++ b/rpc/responses.go @@ -136,7 +136,7 @@ func NewBlockRes(block *types.Block, fullTx bool) *BlockRes { res.BlockNumber = newHexNum(block.Number()) res.BlockHash = newHexData(block.Hash()) res.ParentHash = newHexData(block.ParentHash()) - res.Nonce = newHexData(block.Header().Nonce) + res.Nonce = newHexData(block.Nonce()) res.Sha3Uncles = newHexData(block.Header().UncleHash) res.LogsBloom = newHexData(block.Bloom()) res.TransactionRoot = newHexData(block.Header().TxHash) -- cgit From eac4d582d7bfe427d4bf5c738e38f1f9861cd30a Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Wed, 1 Apr 2015 17:49:22 +0200 Subject: Patches --- rpc/types.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'rpc') diff --git a/rpc/types.go b/rpc/types.go index 549268cce..2d0cf53be 100644 --- a/rpc/types.go +++ b/rpc/types.go @@ -47,6 +47,7 @@ func newHexData(input interface{}) *hexdata { if input == nil { d.data = nil + return d } switch input := input.(type) { case []byte: @@ -57,8 +58,8 @@ func newHexData(input interface{}) *hexdata { d.data = input.Bytes() case common.Address: d.data = input.Bytes() - case *common.Address: - d.data = input.Bytes() + // case *common.Address: + // d.data = input.Bytes() case *big.Int: d.data = input.Bytes() case int64: -- cgit From 5d8be9c30d1f2334ecac0ddb92d82a878b35c51c Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Wed, 1 Apr 2015 23:08:00 +0200 Subject: Fixed decoding for uint64 into bytes --- rpc/types.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'rpc') diff --git a/rpc/types.go b/rpc/types.go index 2d0cf53be..75c4ba85f 100644 --- a/rpc/types.go +++ b/rpc/types.go @@ -17,6 +17,7 @@ package rpc import ( + "encoding/binary" "encoding/json" "fmt" "math/big" @@ -65,7 +66,9 @@ func newHexData(input interface{}) *hexdata { case int64: d.data = big.NewInt(input).Bytes() case uint64: - d.data = big.NewInt(int64(input)).Bytes() + buff := make([]byte, 8) + binary.BigEndian.PutUint64(buff, input) + d.data = buff case int: d.data = big.NewInt(int64(input)).Bytes() case uint: -- cgit From c71ca1a089cc963848c5eee7391a53168adf188c Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Thu, 2 Apr 2015 11:32:50 +0200 Subject: Better nil handling --- rpc/types.go | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) (limited to 'rpc') diff --git a/rpc/types.go b/rpc/types.go index 75c4ba85f..53a2a0806 100644 --- a/rpc/types.go +++ b/rpc/types.go @@ -24,10 +24,12 @@ import ( "strings" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/types" ) type hexdata struct { - data []byte + data []byte + isNil bool } func (d *hexdata) String() string { @@ -35,6 +37,9 @@ func (d *hexdata) String() string { } func (d *hexdata) MarshalJSON() ([]byte, error) { + if d.isNil { + return json.Marshal(nil) + } return json.Marshal(d.String()) } @@ -56,11 +61,19 @@ func newHexData(input interface{}) *hexdata { case common.Hash: d.data = input.Bytes() case *common.Hash: - d.data = input.Bytes() + if input == nil { + d.isNil = true + } else { + d.data = input.Bytes() + } case common.Address: d.data = input.Bytes() - // case *common.Address: - // d.data = input.Bytes() + case *common.Address: + if input == nil { + d.isNil = true + } else { + d.data = input.Bytes() + } case *big.Int: d.data = input.Bytes() case int64: @@ -83,7 +96,8 @@ func newHexData(input interface{}) *hexdata { } type hexnum struct { - data []byte + data []byte + isNil bool } func (d *hexnum) String() string { @@ -99,6 +113,9 @@ func (d *hexnum) String() string { } func (d *hexnum) MarshalJSON() ([]byte, error) { + if d.isNil { + return json.Marshal(nil) + } return json.Marshal(d.String()) } -- cgit 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') 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 From f89baa73e5cfc0e93f32e36f5c3838cd808e6f46 Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Thu, 2 Apr 2015 12:30:22 +0200 Subject: transactionRoot -> transactionsRoot --- rpc/responses.go | 6 +++--- rpc/responses_test.go | 56 +++++++++++++++++++++++++-------------------------- 2 files changed, 31 insertions(+), 31 deletions(-) (limited to 'rpc') diff --git a/rpc/responses.go b/rpc/responses.go index bf4f518aa..f1dc344db 100644 --- a/rpc/responses.go +++ b/rpc/responses.go @@ -16,7 +16,7 @@ type BlockRes struct { Nonce *hexdata `json:"nonce"` Sha3Uncles *hexdata `json:"sha3Uncles"` LogsBloom *hexdata `json:"logsBloom"` - TransactionRoot *hexdata `json:"transactionRoot"` + TransactionRoot *hexdata `json:"transactionsRoot"` StateRoot *hexdata `json:"stateRoot"` Miner *hexdata `json:"miner"` Difficulty *hexnum `json:"difficulty"` @@ -40,7 +40,7 @@ func (b *BlockRes) MarshalJSON() ([]byte, error) { Nonce *hexdata `json:"nonce"` Sha3Uncles *hexdata `json:"sha3Uncles"` LogsBloom *hexdata `json:"logsBloom"` - TransactionRoot *hexdata `json:"transactionRoot"` + TransactionRoot *hexdata `json:"transactionsRoot"` StateRoot *hexdata `json:"stateRoot"` Miner *hexdata `json:"miner"` Difficulty *hexnum `json:"difficulty"` @@ -83,7 +83,7 @@ func (b *BlockRes) MarshalJSON() ([]byte, error) { Nonce *hexdata `json:"nonce"` Sha3Uncles *hexdata `json:"sha3Uncles"` LogsBloom *hexdata `json:"logsBloom"` - TransactionRoot *hexdata `json:"transactionRoot"` + TransactionRoot *hexdata `json:"transactionsRoot"` StateRoot *hexdata `json:"stateRoot"` Miner *hexdata `json:"miner"` Difficulty *hexnum `json:"difficulty"` diff --git a/rpc/responses_test.go b/rpc/responses_test.go index 0f911c886..2ec6d9d15 100644 --- a/rpc/responses_test.go +++ b/rpc/responses_test.go @@ -34,20 +34,20 @@ func TestNewBlockRes(t *testing.T) { extra := "" block := types.NewBlock(parentHash, coinbase, root, difficulty, nonce, extra) tests := map[string]string{ - "number": reNum, - "hash": reHash, - "parentHash": reHash, - "nonce": reData, - "sha3Uncles": reHash, - "logsBloom": reData, - "transactionRoot": reHash, - "stateRoot": reHash, - "miner": reAddress, - "difficulty": `"0x1"`, - "totalDifficulty": reNum, - "size": reNumNonZero, - "extraData": reData, - "gasLimit": reNum, + "number": reNum, + "hash": reHash, + "parentHash": reHash, + "nonce": reData, + "sha3Uncles": reHash, + "logsBloom": reData, + "transactionsRoot": reHash, + "stateRoot": reHash, + "miner": reAddress, + "difficulty": `"0x1"`, + "totalDifficulty": reNum, + "size": reNumNonZero, + "extraData": reData, + "gasLimit": reNum, // "minGasPrice": "0x", "gasUsed": reNum, "timestamp": reNum, @@ -84,20 +84,20 @@ func TestNewBlockResWithTrans(t *testing.T) { extra := "" block := types.NewBlock(parentHash, coinbase, root, difficulty, nonce, extra) tests := map[string]string{ - "number": reNum, - "hash": reHash, - "parentHash": reHash, - "nonce": reData, - "sha3Uncles": reHash, - "logsBloom": reData, - "transactionRoot": reHash, - "stateRoot": reHash, - "miner": reAddress, - "difficulty": `"0x1"`, - "totalDifficulty": reNum, - "size": reNumNonZero, - "extraData": reData, - "gasLimit": reNum, + "number": reNum, + "hash": reHash, + "parentHash": reHash, + "nonce": reData, + "sha3Uncles": reHash, + "logsBloom": reData, + "transactionsRoot": reHash, + "stateRoot": reHash, + "miner": reAddress, + "difficulty": `"0x1"`, + "totalDifficulty": reNum, + "size": reNumNonZero, + "extraData": reData, + "gasLimit": reNum, // "minGasPrice": "0x", "gasUsed": reNum, "timestamp": reNum, -- cgit From cc45b4d8b536a53b59696389e3609c2f633d07a7 Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Thu, 2 Apr 2015 12:30:42 +0200 Subject: Trim left only, not right --- rpc/types.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'rpc') diff --git a/rpc/types.go b/rpc/types.go index 53a2a0806..83542c83d 100644 --- a/rpc/types.go +++ b/rpc/types.go @@ -104,7 +104,7 @@ func (d *hexnum) String() string { // Get hex string from bytes out := common.Bytes2Hex(d.data) // Trim leading 0s - out = strings.Trim(out, "0") + out = strings.TrimLeft(out, "0") // Output "0x0" when value is 0 if len(out) == 0 { out = "0" -- cgit From b10e33c04011c9b9f52841ed09de0b8d4cb5bc6a Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Thu, 2 Apr 2015 12:31:10 +0200 Subject: More types supported --- rpc/types.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'rpc') diff --git a/rpc/types.go b/rpc/types.go index 83542c83d..205b58b0a 100644 --- a/rpc/types.go +++ b/rpc/types.go @@ -74,6 +74,14 @@ func newHexData(input interface{}) *hexdata { } else { d.data = input.Bytes() } + case types.Bloom: + d.data = input.Bytes() + case *types.Bloom: + if input == nil { + d.isNil = true + } else { + d.data = input.Bytes() + } case *big.Int: d.data = input.Bytes() case int64: @@ -86,6 +94,18 @@ func newHexData(input interface{}) *hexdata { d.data = big.NewInt(int64(input)).Bytes() case uint: d.data = big.NewInt(int64(input)).Bytes() + case int8: + d.data = big.NewInt(int64(input)).Bytes() + case uint8: + d.data = big.NewInt(int64(input)).Bytes() + case int16: + d.data = big.NewInt(int64(input)).Bytes() + case uint16: + d.data = big.NewInt(int64(input)).Bytes() + case int32: + d.data = big.NewInt(int64(input)).Bytes() + case uint32: + d.data = big.NewInt(int64(input)).Bytes() case string: // hexstring d.data = common.Big(input).Bytes() default: -- cgit From edfd2757d9d958dca936fe38efe350cf60b2c3ce Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Thu, 2 Apr 2015 12:52:25 +0200 Subject: Better decoding of uint* --- rpc/types.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'rpc') diff --git a/rpc/types.go b/rpc/types.go index 205b58b0a..0789a9901 100644 --- a/rpc/types.go +++ b/rpc/types.go @@ -101,11 +101,15 @@ func newHexData(input interface{}) *hexdata { case int16: d.data = big.NewInt(int64(input)).Bytes() case uint16: - d.data = big.NewInt(int64(input)).Bytes() + buff := make([]byte, 8) + binary.BigEndian.PutUint16(buff, input) + d.data = buff case int32: d.data = big.NewInt(int64(input)).Bytes() case uint32: - d.data = big.NewInt(int64(input)).Bytes() + buff := make([]byte, 8) + binary.BigEndian.PutUint32(buff, input) + d.data = buff case string: // hexstring d.data = common.Big(input).Bytes() default: -- cgit From 585aec127cb231d91998b33564b2294659f56ac1 Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Thu, 2 Apr 2015 12:56:36 +0200 Subject: Don't ignore user-specified "include tx" param --- rpc/api.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'rpc') diff --git a/rpc/api.go b/rpc/api.go index 660bb3251..940b80758 100644 --- a/rpc/api.go +++ b/rpc/api.go @@ -179,7 +179,7 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err } block := api.xeth().EthBlockByHash(args.BlockHash) - br := NewBlockRes(block, true) + br := NewBlockRes(block, args.IncludeTxs) *reply = br case "eth_getBlockByNumber": @@ -189,7 +189,7 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err } block := api.xeth().EthBlockByNumber(args.BlockNumber) - br := NewBlockRes(block, true) + br := NewBlockRes(block, args.IncludeTxs) *reply = br case "eth_getTransactionByHash": -- cgit From 2efb89d5440a9e43bec75b5f59032a0ff0cdc3cc Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Thu, 2 Apr 2015 13:04:58 +0200 Subject: Guard for nil *big.Int --- rpc/types.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'rpc') diff --git a/rpc/types.go b/rpc/types.go index 0789a9901..806c9c8a4 100644 --- a/rpc/types.go +++ b/rpc/types.go @@ -83,7 +83,11 @@ func newHexData(input interface{}) *hexdata { d.data = input.Bytes() } case *big.Int: - d.data = input.Bytes() + if input == nil { + d.isNil = true + } else { + d.data = input.Bytes() + } case int64: d.data = big.NewInt(input).Bytes() case uint64: -- cgit From 14c14fd61f72bfa8c1494fa799a4f2e2eb14f4c1 Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Thu, 2 Apr 2015 13:05:13 +0200 Subject: Output empty block as nil --- rpc/responses.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'rpc') diff --git a/rpc/responses.go b/rpc/responses.go index f1dc344db..3d1687cb6 100644 --- a/rpc/responses.go +++ b/rpc/responses.go @@ -128,7 +128,7 @@ func NewBlockRes(block *types.Block, fullTx bool) *BlockRes { // TODO respect fullTx flag if block == nil { - return &BlockRes{} + return nil } res := new(BlockRes) -- cgit 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/api.go | 12 ++++---- rpc/args.go | 88 +++++++++++++++++++++++++++++++++++++++----------------- rpc/args_test.go | 12 +++++++- 3 files changed, 79 insertions(+), 33 deletions(-) (limited to 'rpc') diff --git a/rpc/api.go b/rpc/api.go index 940b80758..478ca8752 100644 --- a/rpc/api.go +++ b/rpc/api.go @@ -108,15 +108,15 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err count := api.xethAtStateNum(args.BlockNumber).TxCountAt(args.Address) *reply = common.ToHex(big.NewInt(int64(count)).Bytes()) case "eth_getBlockTransactionCountByHash": - args := new(GetBlockByHashArgs) + args := new(HashArgs) if err := json.Unmarshal(req.Params, &args); err != nil { return err } - block := NewBlockRes(api.xeth().EthBlockByHash(args.BlockHash), false) + block := NewBlockRes(api.xeth().EthBlockByHash(args.Hash), false) *reply = common.ToHex(big.NewInt(int64(len(block.Transactions))).Bytes()) case "eth_getBlockTransactionCountByNumber": - args := new(GetBlockByNumberArgs) + args := new(BlockNumArg) if err := json.Unmarshal(req.Params, &args); err != nil { return err } @@ -124,16 +124,16 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err block := NewBlockRes(api.xeth().EthBlockByNumber(args.BlockNumber), false) *reply = common.ToHex(big.NewInt(int64(len(block.Transactions))).Bytes()) case "eth_getUncleCountByBlockHash": - args := new(GetBlockByHashArgs) + args := new(HashArgs) if err := json.Unmarshal(req.Params, &args); err != nil { return err } - block := api.xeth().EthBlockByHash(args.BlockHash) + block := api.xeth().EthBlockByHash(args.Hash) br := NewBlockRes(block, false) *reply = common.ToHex(big.NewInt(int64(len(br.Uncles))).Bytes()) case "eth_getUncleCountByBlockNumber": - args := new(GetBlockByNumberArgs) + args := new(BlockNumArg) if err := json.Unmarshal(req.Params, &args); err != nil { return err } 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 } diff --git a/rpc/args_test.go b/rpc/args_test.go index 3635882c0..f00899b79 100644 --- a/rpc/args_test.go +++ b/rpc/args_test.go @@ -225,7 +225,7 @@ func TestGetBlockByHashArgsHashInt(t *testing.T) { input := `[8]` args := new(GetBlockByHashArgs) - str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args)) + str := ExpectInsufficientParamsError(json.Unmarshal([]byte(input), &args)) if len(str) > 0 { t.Error(str) } @@ -281,6 +281,16 @@ func TestGetBlockByNumberEmpty(t *testing.T) { } } +func TestGetBlockByNumberShort(t *testing.T) { + input := `["0xbbb"]` + + args := new(GetBlockByNumberArgs) + str := ExpectInsufficientParamsError(json.Unmarshal([]byte(input), &args)) + if len(str) > 0 { + t.Error(str) + } +} + func TestGetBlockByNumberBool(t *testing.T) { input := `[true, true]` -- cgit From b86450aaae361ef183928b2de53e3724d3e174a8 Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Thu, 2 Apr 2015 13:27:58 +0200 Subject: Guard from nil pointers --- rpc/api.go | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'rpc') diff --git a/rpc/api.go b/rpc/api.go index 478ca8752..20299bef1 100644 --- a/rpc/api.go +++ b/rpc/api.go @@ -238,6 +238,10 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err } br := NewBlockRes(api.xeth().EthBlockByHash(args.Hash), false) + if br == nil { + *reply = nil + return nil + } if args.Index >= int64(len(br.Uncles)) || args.Index < 0 { return NewValidationError("Index", "does not exist") @@ -256,6 +260,11 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err block := api.xeth().EthBlockByNumber(args.BlockNumber) v := NewBlockRes(block, true) + if v == nil { + *reply = nil + return nil + } + if args.Index >= int64(len(v.Uncles)) || args.Index < 0 { return NewValidationError("Index", "does not exist") } -- cgit From 81de8ed0912f767b2e3104aaad5c7f31920aaf2a Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Thu, 2 Apr 2015 13:38:12 +0200 Subject: Format code as hexdata --- rpc/api.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'rpc') diff --git a/rpc/api.go b/rpc/api.go index 20299bef1..b554dc16b 100644 --- a/rpc/api.go +++ b/rpc/api.go @@ -146,7 +146,8 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err if err := json.Unmarshal(req.Params, &args); err != nil { return err } - *reply = api.xethAtStateNum(args.BlockNumber).CodeAt(args.Address) + v := api.xethAtStateNum(args.BlockNumber).CodeAtBytes(args.Address) + *reply = newHexData(v) case "eth_sendTransaction", "eth_transact": args := new(NewTxArgs) if err := json.Unmarshal(req.Params, &args); err != nil { -- cgit 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 +- rpc/args_test.go | 12 +++++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) (limited to 'rpc') 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 diff --git a/rpc/args_test.go b/rpc/args_test.go index f00899b79..8c1e8c478 100644 --- a/rpc/args_test.go +++ b/rpc/args_test.go @@ -500,12 +500,18 @@ func TestNewTxArgsValueMissing(t *testing.T) { "gasPrice": "0x9184e72a000", "data": "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675" }]` + expected := new(NewTxArgs) + expected.Value = big.NewInt(0) args := new(NewTxArgs) - str := ExpectValidationError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) + if err := json.Unmarshal([]byte(input), &args); err != nil { + t.Error(err) } + + if bytes.Compare(expected.Value.Bytes(), args.Value.Bytes()) != 0 { + t.Errorf("Value shoud be %v but is %v", expected.Value, args.Value) + } + } func TestNewTxArgsEmpty(t *testing.T) { -- cgit 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 ++-- rpc/args_test.go | 23 +++++++++++++++++------ 2 files changed, 19 insertions(+), 8 deletions(-) (limited to 'rpc') 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 diff --git a/rpc/args_test.go b/rpc/args_test.go index 8c1e8c478..902f8013e 100644 --- a/rpc/args_test.go +++ b/rpc/args_test.go @@ -468,11 +468,16 @@ func TestNewTxArgsGasMissing(t *testing.T) { "value": "0x9184e72a000", "data": "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675" }]` + expected := new(NewTxArgs) + expected.Gas = big.NewInt(0) args := new(NewTxArgs) - str := ExpectValidationError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) + if err := json.Unmarshal([]byte(input), &args); err != nil { + t.Error(err) + } + + if bytes.Compare(expected.Gas.Bytes(), args.Gas.Bytes()) != 0 { + t.Errorf("Gas shoud be %v but is %v", expected.Gas, args.Gas) } } @@ -484,12 +489,18 @@ func TestNewTxArgsBlockGaspriceMissing(t *testing.T) { "value": "0x9184e72a000", "data": "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675" }]` + expected := new(NewTxArgs) + expected.GasPrice = big.NewInt(0) args := new(NewTxArgs) - str := ExpectValidationError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) + if err := json.Unmarshal([]byte(input), &args); err != nil { + t.Error(err) } + + if bytes.Compare(expected.GasPrice.Bytes(), args.GasPrice.Bytes()) != 0 { + t.Errorf("GasPrice shoud be %v but is %v", expected.GasPrice, args.GasPrice) + } + } func TestNewTxArgsValueMissing(t *testing.T) { -- cgit