From f1a4a6e563ea72affe365d89513e5c83a35e4c28 Mon Sep 17 00:00:00 2001 From: Bas van Kervel Date: Wed, 24 Jun 2015 13:53:37 +0200 Subject: [PATCH 01/13] added eth.pendingTransactions --- rpc/api/eth.go | 40 +++++++++++++++++++++++++++++++++++----- rpc/api/eth_args.go | 34 ++++++++++++++++++++++++++++++++++ rpc/api/eth_js.go | 8 ++++++++ rpc/api/utils.go | 3 ++- 4 files changed, 79 insertions(+), 6 deletions(-) diff --git a/rpc/api/eth.go b/rpc/api/eth.go index 0dff138c6..77c710fb0 100644 --- a/rpc/api/eth.go +++ b/rpc/api/eth.go @@ -6,9 +6,11 @@ import ( "math/big" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/eth" "github.com/ethereum/go-ethereum/rpc/codec" "github.com/ethereum/go-ethereum/rpc/shared" "github.com/ethereum/go-ethereum/xeth" + "gopkg.in/fatih/set.v0" ) const ( @@ -18,9 +20,10 @@ const ( // eth api provider // See https://github.com/ethereum/wiki/wiki/JSON-RPC type ethApi struct { - xeth *xeth.XEth - methods map[string]ethhandler - codec codec.ApiCoder + xeth *xeth.XEth + ethereum *eth.Ethereum + methods map[string]ethhandler + codec codec.ApiCoder } // eth callback handler @@ -71,12 +74,13 @@ var ( "eth_hashrate": (*ethApi).Hashrate, "eth_getWork": (*ethApi).GetWork, "eth_submitWork": (*ethApi).SubmitWork, + "eth_pendingTransactions": (*ethApi).PendingTransactions, } ) // create new ethApi instance -func NewEthApi(xeth *xeth.XEth, codec codec.Codec) *ethApi { - return ðApi{xeth, ethMapping, codec.New(nil)} +func NewEthApi(xeth *xeth.XEth, eth *eth.Ethereum, codec codec.Codec) *ethApi { + return ðApi{xeth, eth, ethMapping, codec.New(nil)} } // collection with supported methods @@ -556,3 +560,29 @@ func (self *ethApi) SubmitWork(req *shared.Request) (interface{}, error) { } return self.xeth.RemoteMining().SubmitWork(args.Nonce, common.HexToHash(args.Digest), common.HexToHash(args.Header)), nil } + +func (self *ethApi) PendingTransactions(req *shared.Request) (interface{}, error) { + txs := self.ethereum.TxPool().GetTransactions() + + // grab the accounts from the account manager. This will help with determening which + // transactions should be returned. + accounts, err := self.ethereum.AccountManager().Accounts() + if err != nil { + return nil, err + } + + // Add the accouns to a new set + accountSet := set.New() + for _, account := range accounts { + accountSet.Add(account.Address) + } + + var ltxs []*tx + for _, tx := range txs { + if from, _ := tx.From(); accountSet.Has(from) { + ltxs = append(ltxs, newTx(tx)) + } + } + + return ltxs, nil +} diff --git a/rpc/api/eth_args.go b/rpc/api/eth_args.go index bf8ffead6..39c003f66 100644 --- a/rpc/api/eth_args.go +++ b/rpc/api/eth_args.go @@ -5,8 +5,11 @@ import ( "fmt" "math/big" + "strconv" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/state" + "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/rpc/shared" ) @@ -858,3 +861,34 @@ func (args *SubmitWorkArgs) UnmarshalJSON(b []byte) (err error) { return nil } + +type tx struct { + tx *types.Transaction + + To string + From string + Nonce string + Value string + Data string + GasLimit string + GasPrice string +} + +func newTx(t *types.Transaction) *tx { + from, _ := t.From() + var to string + if t := t.To(); t != nil { + to = t.Hex() + } + + return &tx{ + tx: t, + To: to, + From: from.Hex(), + Value: t.Amount.String(), + Nonce: strconv.Itoa(int(t.Nonce())), + Data: "0x" + common.Bytes2Hex(t.Data()), + GasLimit: t.GasLimit.String(), + GasPrice: t.GasPrice().String(), + } +} diff --git a/rpc/api/eth_js.go b/rpc/api/eth_js.go index e1268eb76..8d0fe8f0a 100644 --- a/rpc/api/eth_js.go +++ b/rpc/api/eth_js.go @@ -15,6 +15,14 @@ web3._extend({ inputFormatter: [web3._extend.formatters.formatInputString,web3._extend.formatters.formatInputString], outputFormatter: web3._extend.formatters.formatOutputString }) + ], + properties: + [ + new web3._extend.Property({ + name: 'pendingTransactions', + getter: 'eth_pendingTransactions', + outputFormatter: function(obj) { return obj; } + }) ] }); ` diff --git a/rpc/api/utils.go b/rpc/api/utils.go index 6e4835de6..3d46f78d3 100644 --- a/rpc/api/utils.go +++ b/rpc/api/utils.go @@ -84,6 +84,7 @@ var ( "hashrate", "getWork", "submitWork", + "pendingTransactions", }, "miner": []string{ "hashrate", @@ -149,7 +150,7 @@ func ParseApiString(apistr string, codec codec.Codec, xeth *xeth.XEth, eth *eth. case shared.DbApiName: apis[i] = NewDbApi(xeth, eth, codec) case shared.EthApiName: - apis[i] = NewEthApi(xeth, codec) + apis[i] = NewEthApi(xeth, eth, codec) case shared.MinerApiName: apis[i] = NewMinerApi(eth, codec) case shared.NetApiName: From dc58568a25e54ea601aefb8e97f427cae0814612 Mon Sep 17 00:00:00 2001 From: Bas van Kervel Date: Wed, 24 Jun 2015 14:56:53 +0200 Subject: [PATCH 02/13] added eth.resend --- rpc/api/eth.go | 18 ++++++++++++++++ rpc/api/eth_args.go | 50 +++++++++++++++++++++++++++++++++++++++++++++ rpc/api/eth_js.go | 7 +++++++ rpc/api/utils.go | 1 + 4 files changed, 76 insertions(+) diff --git a/rpc/api/eth.go b/rpc/api/eth.go index 77c710fb0..2bd7e4cdb 100644 --- a/rpc/api/eth.go +++ b/rpc/api/eth.go @@ -6,6 +6,7 @@ import ( "math/big" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/eth" "github.com/ethereum/go-ethereum/rpc/codec" "github.com/ethereum/go-ethereum/rpc/shared" @@ -74,6 +75,7 @@ var ( "eth_hashrate": (*ethApi).Hashrate, "eth_getWork": (*ethApi).GetWork, "eth_submitWork": (*ethApi).SubmitWork, + "eth_resend": (*ethApi).Resend, "eth_pendingTransactions": (*ethApi).PendingTransactions, } ) @@ -561,6 +563,22 @@ func (self *ethApi) SubmitWork(req *shared.Request) (interface{}, error) { return self.xeth.RemoteMining().SubmitWork(args.Nonce, common.HexToHash(args.Digest), common.HexToHash(args.Header)), nil } +func (self *ethApi) Resend(req *shared.Request) (interface{}, error) { + args := new(ResendArgs) + if err := self.codec.Decode(req.Params, &args); err != nil { + return nil, shared.NewDecodeParamError(err.Error()) + } + + ret, err := self.xeth.Transact(args.Tx.From, args.Tx.To, args.Tx.Nonce, args.Tx.Value, args.GasLimit, args.GasPrice, args.Tx.Data) + if err != nil { + return nil, err + } + + self.ethereum.TxPool().RemoveTransactions(types.Transactions{args.Tx.tx}) + + return ret, nil +} + func (self *ethApi) PendingTransactions(req *shared.Request) (interface{}, error) { txs := self.ethereum.TxPool().GetTransactions() diff --git a/rpc/api/eth_args.go b/rpc/api/eth_args.go index 39c003f66..a75fdbdee 100644 --- a/rpc/api/eth_args.go +++ b/rpc/api/eth_args.go @@ -892,3 +892,53 @@ func newTx(t *types.Transaction) *tx { GasPrice: t.GasPrice().String(), } } + +type ResendArgs struct { + Tx *tx + GasPrice string + GasLimit string +} + +func (args *ResendArgs) UnmarshalJSON(b []byte) (err error) { + var obj []interface{} + if err = json.Unmarshal(b, &obj); err != nil { + return shared.NewDecodeParamError(err.Error()) + } + + if len(obj) < 1 { + return shared.NewInsufficientParamsError(len(obj), 1) + } + + data, err := json.Marshal(obj[0]) + if err != nil { + return shared.NewDecodeParamError("Unable to parse transaction object") + } + + trans := new(tx) + err = json.Unmarshal(data, trans) + if err != nil { + return shared.NewDecodeParamError("Unable to parse transaction object.") + } + + gasLimit, gasPrice := trans.GasLimit, trans.GasPrice + + if len(obj) > 1 && obj[1] != nil { + if gp, ok := obj[1].(string); ok { + gasPrice = gp + } else { + return shared.NewInvalidTypeError("gasPrice", "not a string") + } + } + if len(obj) > 2 && obj[2] != nil { + if gl, ok := obj[2].(string); ok { + gasLimit = gl + } else { + return shared.NewInvalidTypeError("gasLimit", "not a string") + } + } + args.Tx = trans + args.GasPrice = gasPrice + args.GasLimit = gasLimit + + return nil +} diff --git a/rpc/api/eth_js.go b/rpc/api/eth_js.go index 8d0fe8f0a..4512cc147 100644 --- a/rpc/api/eth_js.go +++ b/rpc/api/eth_js.go @@ -14,6 +14,13 @@ web3._extend({ params: 2, inputFormatter: [web3._extend.formatters.formatInputString,web3._extend.formatters.formatInputString], outputFormatter: web3._extend.formatters.formatOutputString + }), + new web3._extend.Method({ + name: 'resend', + call: 'eth_resend', + params: 3, + inputFormatter: [function(obj) { return obj; },web3._extend.formatters.formatInputString,web3._extend.formatters.formatInputString], + outputFormatter: web3._extend.formatters.formatOutputString }) ], properties: diff --git a/rpc/api/utils.go b/rpc/api/utils.go index 3d46f78d3..e6a01d3d6 100644 --- a/rpc/api/utils.go +++ b/rpc/api/utils.go @@ -85,6 +85,7 @@ var ( "getWork", "submitWork", "pendingTransactions", + "resend", }, "miner": []string{ "hashrate", From ee73f09727004e94a04a396b99151ab79fd187f4 Mon Sep 17 00:00:00 2001 From: Bas van Kervel Date: Wed, 24 Jun 2015 16:07:44 +0200 Subject: [PATCH 03/13] fixed unittest compilation issue --- rpc/api/api_test.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/rpc/api/api_test.go b/rpc/api/api_test.go index 7e273ef28..2ac8bcd45 100644 --- a/rpc/api/api_test.go +++ b/rpc/api/api_test.go @@ -76,8 +76,9 @@ func TestCompileSolidity(t *testing.T) { expLanguageVersion := "0" expSource := source - xeth := xeth.NewTest(ð.Ethereum{}, nil) - api := NewEthApi(xeth, codec.JSON) + eth := ð.Ethereum{} + xeth := xeth.NewTest(eth, nil) + api := NewEthApi(xeth, eth, codec.JSON) var rpcRequest shared.Request json.Unmarshal([]byte(jsonstr), &rpcRequest) From a355777ff8531ba91fbdfb093532e7314c15710b Mon Sep 17 00:00:00 2001 From: Bas van Kervel Date: Mon, 29 Jun 2015 11:13:28 +0200 Subject: [PATCH 04/13] improved error handling in parsing request --- rpc/api/eth.go | 3 ++- rpc/api/eth_args.go | 7 ++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/rpc/api/eth.go b/rpc/api/eth.go index 2bd7e4cdb..0735754b5 100644 --- a/rpc/api/eth.go +++ b/rpc/api/eth.go @@ -12,6 +12,7 @@ import ( "github.com/ethereum/go-ethereum/rpc/shared" "github.com/ethereum/go-ethereum/xeth" "gopkg.in/fatih/set.v0" + "fmt" ) const ( @@ -582,7 +583,7 @@ func (self *ethApi) Resend(req *shared.Request) (interface{}, error) { func (self *ethApi) PendingTransactions(req *shared.Request) (interface{}, error) { txs := self.ethereum.TxPool().GetTransactions() - // grab the accounts from the account manager. This will help with determening which + // grab the accounts from the account manager. This will help with determining which // transactions should be returned. accounts, err := self.ethereum.AccountManager().Accounts() if err != nil { diff --git a/rpc/api/eth_args.go b/rpc/api/eth_args.go index a75fdbdee..88fc00a6c 100644 --- a/rpc/api/eth_args.go +++ b/rpc/api/eth_args.go @@ -917,7 +917,11 @@ func (args *ResendArgs) UnmarshalJSON(b []byte) (err error) { trans := new(tx) err = json.Unmarshal(data, trans) if err != nil { - return shared.NewDecodeParamError("Unable to parse transaction object.") + return shared.NewDecodeParamError("Unable to parse transaction object") + } + + if trans == nil || trans.tx == nil { + return shared.NewDecodeParamError("Unable to parse transaction object") } gasLimit, gasPrice := trans.GasLimit, trans.GasPrice @@ -936,6 +940,7 @@ func (args *ResendArgs) UnmarshalJSON(b []byte) (err error) { return shared.NewInvalidTypeError("gasLimit", "not a string") } } + args.Tx = trans args.GasPrice = gasPrice args.GasLimit = gasLimit From f9264e87ec4c612f647a05ea11d5640709577a7f Mon Sep 17 00:00:00 2001 From: Bas van Kervel Date: Mon, 29 Jun 2015 12:32:01 +0200 Subject: [PATCH 05/13] add json parsing method for resend transaction --- rpc/api/eth.go | 1 - rpc/api/eth_args.go | 77 ++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 76 insertions(+), 2 deletions(-) diff --git a/rpc/api/eth.go b/rpc/api/eth.go index 0735754b5..6c071569c 100644 --- a/rpc/api/eth.go +++ b/rpc/api/eth.go @@ -12,7 +12,6 @@ import ( "github.com/ethereum/go-ethereum/rpc/shared" "github.com/ethereum/go-ethereum/xeth" "gopkg.in/fatih/set.v0" - "fmt" ) const ( diff --git a/rpc/api/eth_args.go b/rpc/api/eth_args.go index 88fc00a6c..203171d58 100644 --- a/rpc/api/eth_args.go +++ b/rpc/api/eth_args.go @@ -4,8 +4,8 @@ import ( "encoding/json" "fmt" "math/big" - "strconv" + "strings" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/state" @@ -899,6 +899,81 @@ type ResendArgs struct { GasLimit string } +func (tx *tx) UnmarshalJSON(b []byte) (err error) { + var fields map[string]interface{} + if err := json.Unmarshal(b, &fields); err != nil { + return shared.NewDecodeParamError(err.Error()) + } + + trans := new(types.Transaction) + + if val, found := fields["To"]; found { + if strVal, ok := val.(string); ok && len(strVal) > 0 { + tx.To = strVal + to := common.StringToAddress(strVal) + trans.Recipient = &to + } + } + + if val, found := fields["From"]; found { + if strVal, ok := val.(string); ok { + tx.From = strVal + } + } + + if val, found := fields["Nonce"]; found { + if strVal, ok := val.(string); ok { + tx.Nonce = strVal + if trans.AccountNonce, err = strconv.ParseUint(strVal, 10, 64); err != nil { + return shared.NewDecodeParamError(fmt.Sprintf("Unable to decode tx.Nonce - %v", err)) + } + } + } + + var parseOk bool + if val, found := fields["Value"]; found { + if strVal, ok := val.(string); ok { + tx.Value = strVal + if trans.Amount, parseOk = new(big.Int).SetString(strVal, 0); !parseOk { + return shared.NewDecodeParamError(fmt.Sprintf("Unable to decode tx.Amount - %v", err)) + } + } + } + + if val, found := fields["Data"]; found { + if strVal, ok := val.(string); ok { + tx.Data = strVal + if strings.HasPrefix(strVal, "0x") { + trans.Payload = common.Hex2Bytes(strVal[2:]) + } else { + trans.Payload = common.Hex2Bytes(strVal) + } + } + } + + if val, found := fields["GasLimit"]; found { + if strVal, ok := val.(string); ok { + tx.GasLimit = strVal + if trans.GasLimit, parseOk = new(big.Int).SetString(strVal, 0); !parseOk { + return shared.NewDecodeParamError(fmt.Sprintf("Unable to decode tx.GasLimit - %v", err)) + } + } + } + + if val, found := fields["GasPrice"]; found { + if strVal, ok := val.(string); ok { + tx.GasPrice = strVal + if trans.Price, parseOk = new(big.Int).SetString(strVal, 0); !parseOk { + return shared.NewDecodeParamError(fmt.Sprintf("Unable to decode tx.GasPrice - %v", err)) + } + } + } + + tx.tx = trans + + return nil +} + func (args *ResendArgs) UnmarshalJSON(b []byte) (err error) { var obj []interface{} if err = json.Unmarshal(b, &obj); err != nil { From 61ccc39b569b0d9c1c0c5a51975723621dd312d0 Mon Sep 17 00:00:00 2001 From: Bas van Kervel Date: Mon, 29 Jun 2015 12:42:47 +0200 Subject: [PATCH 06/13] initialize fields to prevent nil pointer exception --- rpc/api/eth_args.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/rpc/api/eth_args.go b/rpc/api/eth_args.go index 203171d58..b5507832d 100644 --- a/rpc/api/eth_args.go +++ b/rpc/api/eth_args.go @@ -906,6 +906,9 @@ func (tx *tx) UnmarshalJSON(b []byte) (err error) { } trans := new(types.Transaction) + trans.Amount = new(big.Int) + trans.GasLimit = new(big.Int) + trans.Price = new(big.Int) if val, found := fields["To"]; found { if strVal, ok := val.(string); ok && len(strVal) > 0 { @@ -928,13 +931,15 @@ func (tx *tx) UnmarshalJSON(b []byte) (err error) { return shared.NewDecodeParamError(fmt.Sprintf("Unable to decode tx.Nonce - %v", err)) } } + } else { + return shared.NewDecodeParamError("tx.Nonce not found") } var parseOk bool if val, found := fields["Value"]; found { if strVal, ok := val.(string); ok { tx.Value = strVal - if trans.Amount, parseOk = new(big.Int).SetString(strVal, 0); !parseOk { + if _, parseOk = trans.Amount.SetString(strVal, 0); !parseOk { return shared.NewDecodeParamError(fmt.Sprintf("Unable to decode tx.Amount - %v", err)) } } @@ -954,7 +959,7 @@ func (tx *tx) UnmarshalJSON(b []byte) (err error) { if val, found := fields["GasLimit"]; found { if strVal, ok := val.(string); ok { tx.GasLimit = strVal - if trans.GasLimit, parseOk = new(big.Int).SetString(strVal, 0); !parseOk { + if _, parseOk = trans.GasLimit.SetString(strVal, 0); !parseOk { return shared.NewDecodeParamError(fmt.Sprintf("Unable to decode tx.GasLimit - %v", err)) } } @@ -963,7 +968,7 @@ func (tx *tx) UnmarshalJSON(b []byte) (err error) { if val, found := fields["GasPrice"]; found { if strVal, ok := val.(string); ok { tx.GasPrice = strVal - if trans.Price, parseOk = new(big.Int).SetString(strVal, 0); !parseOk { + if _, parseOk = trans.Price.SetString(strVal, 0); !parseOk { return shared.NewDecodeParamError(fmt.Sprintf("Unable to decode tx.GasPrice - %v", err)) } } From 056e9dd393eeb7ddb4f6bf3e508228e1874bc94e Mon Sep 17 00:00:00 2001 From: Bas van Kervel Date: Wed, 24 Jun 2015 13:53:37 +0200 Subject: [PATCH 07/13] added eth.pendingTransactions --- rpc/api/eth.go | 40 +++++++++++++++++++++++++++++++++++----- rpc/api/eth_args.go | 34 ++++++++++++++++++++++++++++++++++ rpc/api/eth_js.go | 8 ++++++++ rpc/api/utils.go | 3 ++- 4 files changed, 79 insertions(+), 6 deletions(-) diff --git a/rpc/api/eth.go b/rpc/api/eth.go index 962c8d0f9..d77bf9803 100644 --- a/rpc/api/eth.go +++ b/rpc/api/eth.go @@ -6,9 +6,11 @@ import ( "math/big" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/eth" "github.com/ethereum/go-ethereum/rpc/codec" "github.com/ethereum/go-ethereum/rpc/shared" "github.com/ethereum/go-ethereum/xeth" + "gopkg.in/fatih/set.v0" ) const ( @@ -18,9 +20,10 @@ const ( // eth api provider // See https://github.com/ethereum/wiki/wiki/JSON-RPC type ethApi struct { - xeth *xeth.XEth - methods map[string]ethhandler - codec codec.ApiCoder + xeth *xeth.XEth + ethereum *eth.Ethereum + methods map[string]ethhandler + codec codec.ApiCoder } // eth callback handler @@ -71,12 +74,13 @@ var ( "eth_hashrate": (*ethApi).Hashrate, "eth_getWork": (*ethApi).GetWork, "eth_submitWork": (*ethApi).SubmitWork, + "eth_pendingTransactions": (*ethApi).PendingTransactions, } ) // create new ethApi instance -func NewEthApi(xeth *xeth.XEth, codec codec.Codec) *ethApi { - return ðApi{xeth, ethMapping, codec.New(nil)} +func NewEthApi(xeth *xeth.XEth, eth *eth.Ethereum, codec codec.Codec) *ethApi { + return ðApi{xeth, eth, ethMapping, codec.New(nil)} } // collection with supported methods @@ -548,3 +552,29 @@ func (self *ethApi) SubmitWork(req *shared.Request) (interface{}, error) { } return self.xeth.RemoteMining().SubmitWork(args.Nonce, common.HexToHash(args.Digest), common.HexToHash(args.Header)), nil } + +func (self *ethApi) PendingTransactions(req *shared.Request) (interface{}, error) { + txs := self.ethereum.TxPool().GetTransactions() + + // grab the accounts from the account manager. This will help with determening which + // transactions should be returned. + accounts, err := self.ethereum.AccountManager().Accounts() + if err != nil { + return nil, err + } + + // Add the accouns to a new set + accountSet := set.New() + for _, account := range accounts { + accountSet.Add(account.Address) + } + + var ltxs []*tx + for _, tx := range txs { + if from, _ := tx.From(); accountSet.Has(from) { + ltxs = append(ltxs, newTx(tx)) + } + } + + return ltxs, nil +} diff --git a/rpc/api/eth_args.go b/rpc/api/eth_args.go index bf8ffead6..39c003f66 100644 --- a/rpc/api/eth_args.go +++ b/rpc/api/eth_args.go @@ -5,8 +5,11 @@ import ( "fmt" "math/big" + "strconv" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/state" + "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/rpc/shared" ) @@ -858,3 +861,34 @@ func (args *SubmitWorkArgs) UnmarshalJSON(b []byte) (err error) { return nil } + +type tx struct { + tx *types.Transaction + + To string + From string + Nonce string + Value string + Data string + GasLimit string + GasPrice string +} + +func newTx(t *types.Transaction) *tx { + from, _ := t.From() + var to string + if t := t.To(); t != nil { + to = t.Hex() + } + + return &tx{ + tx: t, + To: to, + From: from.Hex(), + Value: t.Amount.String(), + Nonce: strconv.Itoa(int(t.Nonce())), + Data: "0x" + common.Bytes2Hex(t.Data()), + GasLimit: t.GasLimit.String(), + GasPrice: t.GasPrice().String(), + } +} diff --git a/rpc/api/eth_js.go b/rpc/api/eth_js.go index e1268eb76..8d0fe8f0a 100644 --- a/rpc/api/eth_js.go +++ b/rpc/api/eth_js.go @@ -15,6 +15,14 @@ web3._extend({ inputFormatter: [web3._extend.formatters.formatInputString,web3._extend.formatters.formatInputString], outputFormatter: web3._extend.formatters.formatOutputString }) + ], + properties: + [ + new web3._extend.Property({ + name: 'pendingTransactions', + getter: 'eth_pendingTransactions', + outputFormatter: function(obj) { return obj; } + }) ] }); ` diff --git a/rpc/api/utils.go b/rpc/api/utils.go index 6e4835de6..3d46f78d3 100644 --- a/rpc/api/utils.go +++ b/rpc/api/utils.go @@ -84,6 +84,7 @@ var ( "hashrate", "getWork", "submitWork", + "pendingTransactions", }, "miner": []string{ "hashrate", @@ -149,7 +150,7 @@ func ParseApiString(apistr string, codec codec.Codec, xeth *xeth.XEth, eth *eth. case shared.DbApiName: apis[i] = NewDbApi(xeth, eth, codec) case shared.EthApiName: - apis[i] = NewEthApi(xeth, codec) + apis[i] = NewEthApi(xeth, eth, codec) case shared.MinerApiName: apis[i] = NewMinerApi(eth, codec) case shared.NetApiName: From ec866b066ace5d80c3c6a69349dbb1fac4503b46 Mon Sep 17 00:00:00 2001 From: Bas van Kervel Date: Wed, 24 Jun 2015 14:56:53 +0200 Subject: [PATCH 08/13] added eth.resend --- rpc/api/eth.go | 18 ++++++++++++++++ rpc/api/eth_args.go | 50 +++++++++++++++++++++++++++++++++++++++++++++ rpc/api/eth_js.go | 7 +++++++ rpc/api/utils.go | 1 + 4 files changed, 76 insertions(+) diff --git a/rpc/api/eth.go b/rpc/api/eth.go index d77bf9803..1883e363c 100644 --- a/rpc/api/eth.go +++ b/rpc/api/eth.go @@ -6,6 +6,7 @@ import ( "math/big" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/eth" "github.com/ethereum/go-ethereum/rpc/codec" "github.com/ethereum/go-ethereum/rpc/shared" @@ -74,6 +75,7 @@ var ( "eth_hashrate": (*ethApi).Hashrate, "eth_getWork": (*ethApi).GetWork, "eth_submitWork": (*ethApi).SubmitWork, + "eth_resend": (*ethApi).Resend, "eth_pendingTransactions": (*ethApi).PendingTransactions, } ) @@ -553,6 +555,22 @@ func (self *ethApi) SubmitWork(req *shared.Request) (interface{}, error) { return self.xeth.RemoteMining().SubmitWork(args.Nonce, common.HexToHash(args.Digest), common.HexToHash(args.Header)), nil } +func (self *ethApi) Resend(req *shared.Request) (interface{}, error) { + args := new(ResendArgs) + if err := self.codec.Decode(req.Params, &args); err != nil { + return nil, shared.NewDecodeParamError(err.Error()) + } + + ret, err := self.xeth.Transact(args.Tx.From, args.Tx.To, args.Tx.Nonce, args.Tx.Value, args.GasLimit, args.GasPrice, args.Tx.Data) + if err != nil { + return nil, err + } + + self.ethereum.TxPool().RemoveTransactions(types.Transactions{args.Tx.tx}) + + return ret, nil +} + func (self *ethApi) PendingTransactions(req *shared.Request) (interface{}, error) { txs := self.ethereum.TxPool().GetTransactions() diff --git a/rpc/api/eth_args.go b/rpc/api/eth_args.go index 39c003f66..a75fdbdee 100644 --- a/rpc/api/eth_args.go +++ b/rpc/api/eth_args.go @@ -892,3 +892,53 @@ func newTx(t *types.Transaction) *tx { GasPrice: t.GasPrice().String(), } } + +type ResendArgs struct { + Tx *tx + GasPrice string + GasLimit string +} + +func (args *ResendArgs) UnmarshalJSON(b []byte) (err error) { + var obj []interface{} + if err = json.Unmarshal(b, &obj); err != nil { + return shared.NewDecodeParamError(err.Error()) + } + + if len(obj) < 1 { + return shared.NewInsufficientParamsError(len(obj), 1) + } + + data, err := json.Marshal(obj[0]) + if err != nil { + return shared.NewDecodeParamError("Unable to parse transaction object") + } + + trans := new(tx) + err = json.Unmarshal(data, trans) + if err != nil { + return shared.NewDecodeParamError("Unable to parse transaction object.") + } + + gasLimit, gasPrice := trans.GasLimit, trans.GasPrice + + if len(obj) > 1 && obj[1] != nil { + if gp, ok := obj[1].(string); ok { + gasPrice = gp + } else { + return shared.NewInvalidTypeError("gasPrice", "not a string") + } + } + if len(obj) > 2 && obj[2] != nil { + if gl, ok := obj[2].(string); ok { + gasLimit = gl + } else { + return shared.NewInvalidTypeError("gasLimit", "not a string") + } + } + args.Tx = trans + args.GasPrice = gasPrice + args.GasLimit = gasLimit + + return nil +} diff --git a/rpc/api/eth_js.go b/rpc/api/eth_js.go index 8d0fe8f0a..4512cc147 100644 --- a/rpc/api/eth_js.go +++ b/rpc/api/eth_js.go @@ -14,6 +14,13 @@ web3._extend({ params: 2, inputFormatter: [web3._extend.formatters.formatInputString,web3._extend.formatters.formatInputString], outputFormatter: web3._extend.formatters.formatOutputString + }), + new web3._extend.Method({ + name: 'resend', + call: 'eth_resend', + params: 3, + inputFormatter: [function(obj) { return obj; },web3._extend.formatters.formatInputString,web3._extend.formatters.formatInputString], + outputFormatter: web3._extend.formatters.formatOutputString }) ], properties: diff --git a/rpc/api/utils.go b/rpc/api/utils.go index 3d46f78d3..e6a01d3d6 100644 --- a/rpc/api/utils.go +++ b/rpc/api/utils.go @@ -85,6 +85,7 @@ var ( "getWork", "submitWork", "pendingTransactions", + "resend", }, "miner": []string{ "hashrate", From 02c6af66bfd04f5eb2e2d48a85615e93ea9c9ddc Mon Sep 17 00:00:00 2001 From: Bas van Kervel Date: Wed, 24 Jun 2015 16:07:44 +0200 Subject: [PATCH 09/13] fixed unittest compilation issue --- rpc/api/api_test.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/rpc/api/api_test.go b/rpc/api/api_test.go index 7e273ef28..2ac8bcd45 100644 --- a/rpc/api/api_test.go +++ b/rpc/api/api_test.go @@ -76,8 +76,9 @@ func TestCompileSolidity(t *testing.T) { expLanguageVersion := "0" expSource := source - xeth := xeth.NewTest(ð.Ethereum{}, nil) - api := NewEthApi(xeth, codec.JSON) + eth := ð.Ethereum{} + xeth := xeth.NewTest(eth, nil) + api := NewEthApi(xeth, eth, codec.JSON) var rpcRequest shared.Request json.Unmarshal([]byte(jsonstr), &rpcRequest) From 6fdddc5ac940b6241596e0a2622461148e8a57a0 Mon Sep 17 00:00:00 2001 From: Bas van Kervel Date: Mon, 29 Jun 2015 11:13:28 +0200 Subject: [PATCH 10/13] improved error handling in parsing request --- rpc/api/eth.go | 3 ++- rpc/api/eth_args.go | 7 ++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/rpc/api/eth.go b/rpc/api/eth.go index 1883e363c..8e9647861 100644 --- a/rpc/api/eth.go +++ b/rpc/api/eth.go @@ -12,6 +12,7 @@ import ( "github.com/ethereum/go-ethereum/rpc/shared" "github.com/ethereum/go-ethereum/xeth" "gopkg.in/fatih/set.v0" + "fmt" ) const ( @@ -574,7 +575,7 @@ func (self *ethApi) Resend(req *shared.Request) (interface{}, error) { func (self *ethApi) PendingTransactions(req *shared.Request) (interface{}, error) { txs := self.ethereum.TxPool().GetTransactions() - // grab the accounts from the account manager. This will help with determening which + // grab the accounts from the account manager. This will help with determining which // transactions should be returned. accounts, err := self.ethereum.AccountManager().Accounts() if err != nil { diff --git a/rpc/api/eth_args.go b/rpc/api/eth_args.go index a75fdbdee..88fc00a6c 100644 --- a/rpc/api/eth_args.go +++ b/rpc/api/eth_args.go @@ -917,7 +917,11 @@ func (args *ResendArgs) UnmarshalJSON(b []byte) (err error) { trans := new(tx) err = json.Unmarshal(data, trans) if err != nil { - return shared.NewDecodeParamError("Unable to parse transaction object.") + return shared.NewDecodeParamError("Unable to parse transaction object") + } + + if trans == nil || trans.tx == nil { + return shared.NewDecodeParamError("Unable to parse transaction object") } gasLimit, gasPrice := trans.GasLimit, trans.GasPrice @@ -936,6 +940,7 @@ func (args *ResendArgs) UnmarshalJSON(b []byte) (err error) { return shared.NewInvalidTypeError("gasLimit", "not a string") } } + args.Tx = trans args.GasPrice = gasPrice args.GasLimit = gasLimit From 7ffabf1d399989618470794600e25764bdd9954b Mon Sep 17 00:00:00 2001 From: Bas van Kervel Date: Mon, 29 Jun 2015 12:32:01 +0200 Subject: [PATCH 11/13] add json parsing method for resend transaction --- rpc/api/eth.go | 1 - rpc/api/eth_args.go | 77 ++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 76 insertions(+), 2 deletions(-) diff --git a/rpc/api/eth.go b/rpc/api/eth.go index 8e9647861..db0b4b024 100644 --- a/rpc/api/eth.go +++ b/rpc/api/eth.go @@ -12,7 +12,6 @@ import ( "github.com/ethereum/go-ethereum/rpc/shared" "github.com/ethereum/go-ethereum/xeth" "gopkg.in/fatih/set.v0" - "fmt" ) const ( diff --git a/rpc/api/eth_args.go b/rpc/api/eth_args.go index 88fc00a6c..203171d58 100644 --- a/rpc/api/eth_args.go +++ b/rpc/api/eth_args.go @@ -4,8 +4,8 @@ import ( "encoding/json" "fmt" "math/big" - "strconv" + "strings" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/state" @@ -899,6 +899,81 @@ type ResendArgs struct { GasLimit string } +func (tx *tx) UnmarshalJSON(b []byte) (err error) { + var fields map[string]interface{} + if err := json.Unmarshal(b, &fields); err != nil { + return shared.NewDecodeParamError(err.Error()) + } + + trans := new(types.Transaction) + + if val, found := fields["To"]; found { + if strVal, ok := val.(string); ok && len(strVal) > 0 { + tx.To = strVal + to := common.StringToAddress(strVal) + trans.Recipient = &to + } + } + + if val, found := fields["From"]; found { + if strVal, ok := val.(string); ok { + tx.From = strVal + } + } + + if val, found := fields["Nonce"]; found { + if strVal, ok := val.(string); ok { + tx.Nonce = strVal + if trans.AccountNonce, err = strconv.ParseUint(strVal, 10, 64); err != nil { + return shared.NewDecodeParamError(fmt.Sprintf("Unable to decode tx.Nonce - %v", err)) + } + } + } + + var parseOk bool + if val, found := fields["Value"]; found { + if strVal, ok := val.(string); ok { + tx.Value = strVal + if trans.Amount, parseOk = new(big.Int).SetString(strVal, 0); !parseOk { + return shared.NewDecodeParamError(fmt.Sprintf("Unable to decode tx.Amount - %v", err)) + } + } + } + + if val, found := fields["Data"]; found { + if strVal, ok := val.(string); ok { + tx.Data = strVal + if strings.HasPrefix(strVal, "0x") { + trans.Payload = common.Hex2Bytes(strVal[2:]) + } else { + trans.Payload = common.Hex2Bytes(strVal) + } + } + } + + if val, found := fields["GasLimit"]; found { + if strVal, ok := val.(string); ok { + tx.GasLimit = strVal + if trans.GasLimit, parseOk = new(big.Int).SetString(strVal, 0); !parseOk { + return shared.NewDecodeParamError(fmt.Sprintf("Unable to decode tx.GasLimit - %v", err)) + } + } + } + + if val, found := fields["GasPrice"]; found { + if strVal, ok := val.(string); ok { + tx.GasPrice = strVal + if trans.Price, parseOk = new(big.Int).SetString(strVal, 0); !parseOk { + return shared.NewDecodeParamError(fmt.Sprintf("Unable to decode tx.GasPrice - %v", err)) + } + } + } + + tx.tx = trans + + return nil +} + func (args *ResendArgs) UnmarshalJSON(b []byte) (err error) { var obj []interface{} if err = json.Unmarshal(b, &obj); err != nil { From 57dff6f1d7e8402d2849205cb44daaffcc40cc23 Mon Sep 17 00:00:00 2001 From: Bas van Kervel Date: Mon, 29 Jun 2015 12:42:47 +0200 Subject: [PATCH 12/13] initialize fields to prevent nil pointer exception --- rpc/api/eth_args.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/rpc/api/eth_args.go b/rpc/api/eth_args.go index 203171d58..b5507832d 100644 --- a/rpc/api/eth_args.go +++ b/rpc/api/eth_args.go @@ -906,6 +906,9 @@ func (tx *tx) UnmarshalJSON(b []byte) (err error) { } trans := new(types.Transaction) + trans.Amount = new(big.Int) + trans.GasLimit = new(big.Int) + trans.Price = new(big.Int) if val, found := fields["To"]; found { if strVal, ok := val.(string); ok && len(strVal) > 0 { @@ -928,13 +931,15 @@ func (tx *tx) UnmarshalJSON(b []byte) (err error) { return shared.NewDecodeParamError(fmt.Sprintf("Unable to decode tx.Nonce - %v", err)) } } + } else { + return shared.NewDecodeParamError("tx.Nonce not found") } var parseOk bool if val, found := fields["Value"]; found { if strVal, ok := val.(string); ok { tx.Value = strVal - if trans.Amount, parseOk = new(big.Int).SetString(strVal, 0); !parseOk { + if _, parseOk = trans.Amount.SetString(strVal, 0); !parseOk { return shared.NewDecodeParamError(fmt.Sprintf("Unable to decode tx.Amount - %v", err)) } } @@ -954,7 +959,7 @@ func (tx *tx) UnmarshalJSON(b []byte) (err error) { if val, found := fields["GasLimit"]; found { if strVal, ok := val.(string); ok { tx.GasLimit = strVal - if trans.GasLimit, parseOk = new(big.Int).SetString(strVal, 0); !parseOk { + if _, parseOk = trans.GasLimit.SetString(strVal, 0); !parseOk { return shared.NewDecodeParamError(fmt.Sprintf("Unable to decode tx.GasLimit - %v", err)) } } @@ -963,7 +968,7 @@ func (tx *tx) UnmarshalJSON(b []byte) (err error) { if val, found := fields["GasPrice"]; found { if strVal, ok := val.(string); ok { tx.GasPrice = strVal - if trans.Price, parseOk = new(big.Int).SetString(strVal, 0); !parseOk { + if _, parseOk = trans.Price.SetString(strVal, 0); !parseOk { return shared.NewDecodeParamError(fmt.Sprintf("Unable to decode tx.GasPrice - %v", err)) } } From a5d5387dee984b0d3712379998c200d2c6fe89e5 Mon Sep 17 00:00:00 2001 From: Bas van Kervel Date: Tue, 30 Jun 2015 15:27:27 +0200 Subject: [PATCH 13/13] rebase with develop --- rpc/api/eth.go | 9 +++++++++ rpc/api/eth_args.go | 39 ++++++++++++++++++++++++--------------- 2 files changed, 33 insertions(+), 15 deletions(-) diff --git a/rpc/api/eth.go b/rpc/api/eth.go index db0b4b024..784cd0f48 100644 --- a/rpc/api/eth.go +++ b/rpc/api/eth.go @@ -7,7 +7,9 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/crypto/sha3" "github.com/ethereum/go-ethereum/eth" + "github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rpc/codec" "github.com/ethereum/go-ethereum/rpc/shared" "github.com/ethereum/go-ethereum/xeth" @@ -555,6 +557,13 @@ func (self *ethApi) SubmitWork(req *shared.Request) (interface{}, error) { return self.xeth.RemoteMining().SubmitWork(args.Nonce, common.HexToHash(args.Digest), common.HexToHash(args.Header)), nil } +func rlpHash(x interface{}) (h common.Hash) { + hw := sha3.NewKeccak256() + rlp.Encode(hw, x) + hw.Sum(h[:0]) + return h +} + func (self *ethApi) Resend(req *shared.Request) (interface{}, error) { args := new(ResendArgs) if err := self.codec.Decode(req.Params, &args); err != nil { diff --git a/rpc/api/eth_args.go b/rpc/api/eth_args.go index b5507832d..8f64280d3 100644 --- a/rpc/api/eth_args.go +++ b/rpc/api/eth_args.go @@ -885,10 +885,10 @@ func newTx(t *types.Transaction) *tx { tx: t, To: to, From: from.Hex(), - Value: t.Amount.String(), + Value: t.Value().String(), Nonce: strconv.Itoa(int(t.Nonce())), Data: "0x" + common.Bytes2Hex(t.Data()), - GasLimit: t.GasLimit.String(), + GasLimit: t.Gas().String(), GasPrice: t.GasPrice().String(), } } @@ -905,16 +905,21 @@ func (tx *tx) UnmarshalJSON(b []byte) (err error) { return shared.NewDecodeParamError(err.Error()) } - trans := new(types.Transaction) - trans.Amount = new(big.Int) - trans.GasLimit = new(big.Int) - trans.Price = new(big.Int) + var ( + nonce uint64 + to common.Address + amount = new(big.Int).Set(common.Big0) + gasLimit = new(big.Int).Set(common.Big0) + gasPrice = new(big.Int).Set(common.Big0) + data []byte + contractCreation = true + ) if val, found := fields["To"]; found { if strVal, ok := val.(string); ok && len(strVal) > 0 { tx.To = strVal - to := common.StringToAddress(strVal) - trans.Recipient = &to + to = common.HexToAddress(strVal) + contractCreation = false } } @@ -927,7 +932,7 @@ func (tx *tx) UnmarshalJSON(b []byte) (err error) { if val, found := fields["Nonce"]; found { if strVal, ok := val.(string); ok { tx.Nonce = strVal - if trans.AccountNonce, err = strconv.ParseUint(strVal, 10, 64); err != nil { + if nonce, err = strconv.ParseUint(strVal, 10, 64); err != nil { return shared.NewDecodeParamError(fmt.Sprintf("Unable to decode tx.Nonce - %v", err)) } } @@ -939,7 +944,7 @@ func (tx *tx) UnmarshalJSON(b []byte) (err error) { if val, found := fields["Value"]; found { if strVal, ok := val.(string); ok { tx.Value = strVal - if _, parseOk = trans.Amount.SetString(strVal, 0); !parseOk { + if _, parseOk = amount.SetString(strVal, 0); !parseOk { return shared.NewDecodeParamError(fmt.Sprintf("Unable to decode tx.Amount - %v", err)) } } @@ -949,9 +954,9 @@ func (tx *tx) UnmarshalJSON(b []byte) (err error) { if strVal, ok := val.(string); ok { tx.Data = strVal if strings.HasPrefix(strVal, "0x") { - trans.Payload = common.Hex2Bytes(strVal[2:]) + data = common.Hex2Bytes(strVal[2:]) } else { - trans.Payload = common.Hex2Bytes(strVal) + data = common.Hex2Bytes(strVal) } } } @@ -959,7 +964,7 @@ func (tx *tx) UnmarshalJSON(b []byte) (err error) { if val, found := fields["GasLimit"]; found { if strVal, ok := val.(string); ok { tx.GasLimit = strVal - if _, parseOk = trans.GasLimit.SetString(strVal, 0); !parseOk { + if _, parseOk = gasLimit.SetString(strVal, 0); !parseOk { return shared.NewDecodeParamError(fmt.Sprintf("Unable to decode tx.GasLimit - %v", err)) } } @@ -968,13 +973,17 @@ func (tx *tx) UnmarshalJSON(b []byte) (err error) { if val, found := fields["GasPrice"]; found { if strVal, ok := val.(string); ok { tx.GasPrice = strVal - if _, parseOk = trans.Price.SetString(strVal, 0); !parseOk { + if _, parseOk = gasPrice.SetString(strVal, 0); !parseOk { return shared.NewDecodeParamError(fmt.Sprintf("Unable to decode tx.GasPrice - %v", err)) } } } - tx.tx = trans + if contractCreation { + tx.tx = types.NewContractCreation(nonce, amount, gasLimit, gasPrice, data) + } else { + tx.tx = types.NewTransaction(nonce, to, amount, gasLimit, gasPrice, data) + } return nil }