From f1a4a6e563ea72affe365d89513e5c83a35e4c28 Mon Sep 17 00:00:00 2001 From: Bas van Kervel Date: Wed, 24 Jun 2015 13:53:37 +0200 Subject: 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(-) (limited to 'rpc') 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: -- cgit v1.2.3 From dc58568a25e54ea601aefb8e97f427cae0814612 Mon Sep 17 00:00:00 2001 From: Bas van Kervel Date: Wed, 24 Jun 2015 14:56:53 +0200 Subject: 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(+) (limited to 'rpc') 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", -- cgit v1.2.3 From ee73f09727004e94a04a396b99151ab79fd187f4 Mon Sep 17 00:00:00 2001 From: Bas van Kervel Date: Wed, 24 Jun 2015 16:07:44 +0200 Subject: fixed unittest compilation issue --- rpc/api/api_test.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'rpc') 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) -- cgit v1.2.3 From a355777ff8531ba91fbdfb093532e7314c15710b Mon Sep 17 00:00:00 2001 From: Bas van Kervel Date: Mon, 29 Jun 2015 11:13:28 +0200 Subject: improved error handling in parsing request --- rpc/api/eth.go | 3 ++- rpc/api/eth_args.go | 7 ++++++- 2 files changed, 8 insertions(+), 2 deletions(-) (limited to 'rpc') 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 -- cgit v1.2.3 From f9264e87ec4c612f647a05ea11d5640709577a7f Mon Sep 17 00:00:00 2001 From: Bas van Kervel Date: Mon, 29 Jun 2015 12:32:01 +0200 Subject: 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(-) (limited to 'rpc') 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 { -- cgit v1.2.3 From 61ccc39b569b0d9c1c0c5a51975723621dd312d0 Mon Sep 17 00:00:00 2001 From: Bas van Kervel Date: Mon, 29 Jun 2015 12:42:47 +0200 Subject: initialize fields to prevent nil pointer exception --- rpc/api/eth_args.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'rpc') 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)) } } -- cgit v1.2.3