Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RPC cleanup #379

Merged
merged 4 commits into from
Feb 24, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 38 additions & 40 deletions rpc/packages.go → rpc/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ For each request type, define the following:
package rpc

import (
"fmt"
"math/big"
"strings"
"sync"
Expand All @@ -26,8 +25,9 @@ import (
)

var (
defaultGasPrice = big.NewInt(10000000000000)
defaultGas = big.NewInt(10000)
defaultGasPrice = big.NewInt(10000000000000)
defaultGas = big.NewInt(10000)
filterTickerTime = 15 * time.Second
)

type EthereumApi struct {
Expand Down Expand Up @@ -63,6 +63,39 @@ func NewEthereumApi(eth *xeth.XEth) *EthereumApi {
return api
}

func (self *EthereumApi) start() {
timer := time.NewTicker(filterTickerTime)
done:
for {
select {
case <-timer.C:
self.logMut.Lock()
self.messagesMut.Lock()
for id, filter := range self.logs {
if time.Since(filter.timeout) > 20*time.Second {
self.filterManager.UninstallFilter(id)
delete(self.logs, id)
}
}

for id, filter := range self.messages {
if time.Since(filter.timeout) > 20*time.Second {
self.xeth.Whisper().Unwatch(id)
delete(self.messages, id)
}
}
self.logMut.Unlock()
self.messagesMut.Unlock()
case <-self.quit:
break done
}
}
}

func (self *EthereumApi) stop() {
close(self.quit)
}

func (self *EthereumApi) Register(args string, reply *interface{}) error {
self.regmut.Lock()
defer self.regmut.Unlock()
Expand Down Expand Up @@ -410,7 +443,7 @@ func (p *EthereumApi) WhisperMessages(id int, reply *interface{}) error {
}

func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error {
// Spec at https://github.com/ethereum/wiki/wiki/Generic-ON-RPC
// Spec at https://github.com/ethereum/wiki/wiki/Generic-JSON-RPC
rpclogger.DebugDetailf("%T %s", req.Params, req.Params)
switch req.Method {
case "eth_coinbase":
Expand Down Expand Up @@ -595,44 +628,9 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error
}
return p.WhisperMessages(args, reply)
default:
return NewErrorResponse(fmt.Sprintf("%v %s", ErrorNotImplemented, req.Method))
return NewErrorWithMessage(errNotImplemented, req.Method)
}

rpclogger.DebugDetailf("Reply: %T %s", reply, reply)
return nil
}

var filterTickerTime = 15 * time.Second

func (self *EthereumApi) start() {
timer := time.NewTicker(filterTickerTime)
done:
for {
select {
case <-timer.C:
self.logMut.Lock()
self.messagesMut.Lock()
for id, filter := range self.logs {
if time.Since(filter.timeout) > 20*time.Second {
self.filterManager.UninstallFilter(id)
delete(self.logs, id)
}
}

for id, filter := range self.messages {
if time.Since(filter.timeout) > 20*time.Second {
self.xeth.Whisper().Unwatch(id)
delete(self.messages, id)
}
}
self.logMut.Unlock()
self.messagesMut.Unlock()
case <-self.quit:
break done
}
}
}

func (self *EthereumApi) stop() {
close(self.quit)
}
File renamed without changes.
66 changes: 18 additions & 48 deletions rpc/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func (obj *GetBlockArgs) UnmarshalJSON(b []byte) (err error) {
obj.Hash = argstr
return
}
return NewErrorResponse(ErrorDecodeArgs)
return errDecodeArgs
}

type NewTxArgs struct {
Expand Down Expand Up @@ -57,7 +57,7 @@ func (obj *NewTxArgs) UnmarshalJSON(b []byte) (err error) {
return
}

return NewErrorResponse(ErrorDecodeArgs)
return errDecodeArgs
}

type PushTxArgs struct {
Expand All @@ -70,12 +70,12 @@ func (obj *PushTxArgs) UnmarshalJSON(b []byte) (err error) {
obj.Tx = arg0
return
}
return NewErrorResponse(ErrorDecodeArgs)
return errDecodeArgs
}

func (a *PushTxArgs) requirementsPushTx() error {
if a.Tx == "" {
return NewErrorResponse("PushTx requires a 'tx' as argument")
return NewErrorWithMessage(errArguments, "PushTx requires a 'tx' as argument")
}
return nil
}
Expand All @@ -86,14 +86,14 @@ type GetStorageArgs struct {

func (obj *GetStorageArgs) UnmarshalJSON(b []byte) (err error) {
if err = json.Unmarshal(b, &obj.Address); err != nil {
return NewErrorResponse(ErrorDecodeArgs)
return errDecodeArgs
}
return
}

func (a *GetStorageArgs) requirements() error {
if len(a.Address) == 0 {
return NewErrorResponse("GetStorageAt requires an 'address' value as argument")
return NewErrorWithMessage(errArguments, "GetStorageAt requires an 'address' value as argument")
}
return nil
}
Expand All @@ -109,64 +109,39 @@ func (obj *GetStateArgs) UnmarshalJSON(b []byte) (err error) {
obj.Address = arg0
return
}
return NewErrorResponse(ErrorDecodeArgs)
return errDecodeArgs
}

func (a *GetStateArgs) requirements() error {
if a.Address == "" {
return NewErrorResponse("GetStorageAt requires an 'address' value as argument")
return NewErrorWithMessage(errArguments, "GetStorageAt requires an 'address' value as argument")
}
if a.Key == "" {
return NewErrorResponse("GetStorageAt requires an 'key' value as argument")
return NewErrorWithMessage(errArguments, "GetStorageAt requires an 'key' value as argument")
}
return nil
}

type GetStorageAtRes struct {
Key string `json:"key"`
Value string `json:"value"`
}

type GetTxCountArgs struct {
Address string `json:"address"`
}

// type GetTxCountRes struct {
// Nonce int `json:"nonce"`
// }

func (obj *GetTxCountArgs) UnmarshalJSON(b []byte) (err error) {
arg0 := ""
if err = json.Unmarshal(b, &arg0); err == nil {
obj.Address = arg0
return
}
return NewErrorResponse("Could not determine JSON parameters")
return errDecodeArgs
}

func (a *GetTxCountArgs) requirements() error {
if a.Address == "" {
return NewErrorResponse("GetTxCountAt requires an 'address' value as argument")
return NewErrorWithMessage(errArguments, "GetTxCountAt requires an 'address' value as argument")
}
return nil
}

// type GetPeerCountRes struct {
// PeerCount int `json:"peerCount"`
// }

// type GetListeningRes struct {
// IsListening bool `json:"isListening"`
// }

// type GetCoinbaseRes struct {
// Coinbase string `json:"coinbase"`
// }

// type GetMiningRes struct {
// IsMining bool `json:"isMining"`
// }

type GetBalanceArgs struct {
Address string
}
Expand All @@ -177,21 +152,16 @@ func (obj *GetBalanceArgs) UnmarshalJSON(b []byte) (err error) {
obj.Address = arg0
return
}
return NewErrorResponse("Could not determine JSON parameters")
return errDecodeArgs
}

func (a *GetBalanceArgs) requirements() error {
if a.Address == "" {
return NewErrorResponse("GetBalanceAt requires an 'address' value as argument")
return NewErrorWithMessage(errArguments, "GetBalanceAt requires an 'address' value as argument")
}
return nil
}

type BalanceRes struct {
Balance string `json:"balance"`
Address string `json:"address"`
}

type GetCodeAtArgs struct {
Address string
}
Expand All @@ -202,12 +172,12 @@ func (obj *GetCodeAtArgs) UnmarshalJSON(b []byte) (err error) {
obj.Address = arg0
return
}
return NewErrorResponse(ErrorDecodeArgs)
return errDecodeArgs
}

func (a *GetCodeAtArgs) requirements() error {
if a.Address == "" {
return NewErrorResponse("GetCodeAt requires an 'address' value as argument")
return NewErrorWithMessage(errArguments, "GetCodeAt requires an 'address' value as argument")
}
return nil
}
Expand All @@ -218,7 +188,7 @@ type Sha3Args struct {

func (obj *Sha3Args) UnmarshalJSON(b []byte) (err error) {
if err = json.Unmarshal(b, &obj.Data); err != nil {
return NewErrorResponse(ErrorDecodeArgs)
return errDecodeArgs
}
return
}
Expand Down Expand Up @@ -270,10 +240,10 @@ type DbArgs struct {

func (a *DbArgs) requirements() error {
if len(a.Database) == 0 {
return NewErrorResponse("DbPutArgs requires an 'Database' value as argument")
return NewErrorWithMessage(errArguments, "DbPutArgs requires an 'Database' value as argument")
}
if len(a.Key) == 0 {
return NewErrorResponse("DbPutArgs requires an 'Key' value as argument")
return NewErrorWithMessage(errArguments, "DbPutArgs requires an 'Key' value as argument")
}
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion rpc/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func (s *RpcHttpServer) apiHandler(api *rpc.EthereumApi) http.Handler {

reqParsed, reqerr := JSON.ParseRequestBody(req)
if reqerr != nil {
jsonerr := &rpc.RpcErrorObject{-32700, rpc.ErrorParseRequest}
jsonerr := &rpc.RpcErrorObject{-32700, "Error: Could not parse request"}
JSON.Send(w, &rpc.RpcErrorResponse{JsonRpc: jsonrpcver, ID: nil, Error: jsonerr})
return
}
Expand Down
Loading