From 19b2640e89465c1c57f1bbea0274d52d97151f60 Mon Sep 17 00:00:00 2001 From: Bas van Kervel Date: Wed, 16 Dec 2015 10:58:01 +0100 Subject: rpc: migrated the RPC insterface to a new reflection based RPC layer --- rpc/api/admin.go | 465 ------- rpc/api/admin_args.go | 468 ------- rpc/api/admin_js.go | 143 -- rpc/api/api.go | 26 - rpc/api/api_test.go | 170 --- rpc/api/args.go | 74 -- rpc/api/args_test.go | 2649 -------------------------------------- rpc/api/db.go | 144 --- rpc/api/db_args.go | 126 -- rpc/api/db_js.go | 29 - rpc/api/debug.go | 303 ----- rpc/api/debug_args.go | 87 -- rpc/api/debug_js.go | 83 -- rpc/api/eth.go | 721 ----------- rpc/api/eth_args.go | 1104 ---------------- rpc/api/eth_js.go | 66 - rpc/api/mergedapi.go | 88 -- rpc/api/miner.go | 177 --- rpc/api/miner_args.go | 142 -- rpc/api/miner_js.go | 83 -- rpc/api/net.go | 99 -- rpc/api/net_js.go | 39 - rpc/api/parsing.go | 522 -------- rpc/api/personal.go | 139 -- rpc/api/personal_args.go | 85 -- rpc/api/personal_js.go | 51 - rpc/api/shh.go | 196 --- rpc/api/shh_args.go | 174 --- rpc/api/shh_js.go | 34 - rpc/api/txpool.go | 92 -- rpc/api/txpool_js.go | 33 - rpc/api/utils.go | 226 ---- rpc/api/web3.go | 99 -- rpc/api/web3_args.go | 45 - rpc/codec/codec.go | 65 - rpc/codec/json.go | 149 --- rpc/codec/json_test.go | 157 --- rpc/comms/comms.go | 150 --- rpc/comms/http.go | 345 ----- rpc/comms/inproc.go | 82 -- rpc/comms/ipc.go | 158 --- rpc/comms/ipc_unix.go | 82 -- rpc/comms/ipc_windows.go | 697 ---------- rpc/doc.go | 228 ++++ rpc/errors.go | 97 ++ rpc/http.go | 368 ++++++ rpc/ipc.go | 84 ++ rpc/ipc_unix.go | 45 + rpc/ipc_windows.go | 655 ++++++++++ rpc/javascript.go | 414 ++++++ rpc/jeth.go | 324 ----- rpc/json.go | 343 +++++ rpc/json_test.go | 73 ++ rpc/server.go | 438 +++++++ rpc/server_test.go | 271 ++++ rpc/shared/errors.go | 126 -- rpc/shared/types.go | 108 -- rpc/shared/utils.go | 43 - rpc/types.go | 368 ++++++ rpc/types_test.go | 73 ++ rpc/useragent/agent.go | 24 - rpc/useragent/remote_frontend.go | 166 --- rpc/utils.go | 246 ++++ rpc/v2/doc.go | 102 -- rpc/v2/errors.go | 85 -- rpc/v2/json.go | 343 ----- rpc/v2/json_test.go | 73 -- rpc/v2/server.go | 385 ------ rpc/v2/server_test.go | 255 ---- rpc/v2/types.go | 352 ----- rpc/v2/types_test.go | 57 - rpc/v2/utils.go | 214 --- rpc/websocket.go | 235 ++++ rpc/xeth.go | 77 -- 74 files changed, 3938 insertions(+), 13601 deletions(-) delete mode 100644 rpc/api/admin.go delete mode 100644 rpc/api/admin_args.go delete mode 100644 rpc/api/admin_js.go delete mode 100644 rpc/api/api.go delete mode 100644 rpc/api/api_test.go delete mode 100644 rpc/api/args.go delete mode 100644 rpc/api/args_test.go delete mode 100644 rpc/api/db.go delete mode 100644 rpc/api/db_args.go delete mode 100644 rpc/api/db_js.go delete mode 100644 rpc/api/debug.go delete mode 100644 rpc/api/debug_args.go delete mode 100644 rpc/api/debug_js.go delete mode 100644 rpc/api/eth.go delete mode 100644 rpc/api/eth_args.go delete mode 100644 rpc/api/eth_js.go delete mode 100644 rpc/api/mergedapi.go delete mode 100644 rpc/api/miner.go delete mode 100644 rpc/api/miner_args.go delete mode 100644 rpc/api/miner_js.go delete mode 100644 rpc/api/net.go delete mode 100644 rpc/api/net_js.go delete mode 100644 rpc/api/parsing.go delete mode 100644 rpc/api/personal.go delete mode 100644 rpc/api/personal_args.go delete mode 100644 rpc/api/personal_js.go delete mode 100644 rpc/api/shh.go delete mode 100644 rpc/api/shh_args.go delete mode 100644 rpc/api/shh_js.go delete mode 100644 rpc/api/txpool.go delete mode 100644 rpc/api/txpool_js.go delete mode 100644 rpc/api/utils.go delete mode 100644 rpc/api/web3.go delete mode 100644 rpc/api/web3_args.go delete mode 100644 rpc/codec/codec.go delete mode 100644 rpc/codec/json.go delete mode 100644 rpc/codec/json_test.go delete mode 100644 rpc/comms/comms.go delete mode 100644 rpc/comms/http.go delete mode 100644 rpc/comms/inproc.go delete mode 100644 rpc/comms/ipc.go delete mode 100644 rpc/comms/ipc_unix.go delete mode 100644 rpc/comms/ipc_windows.go create mode 100644 rpc/doc.go create mode 100644 rpc/errors.go create mode 100644 rpc/http.go create mode 100644 rpc/ipc.go create mode 100644 rpc/ipc_unix.go create mode 100644 rpc/ipc_windows.go create mode 100644 rpc/javascript.go delete mode 100644 rpc/jeth.go create mode 100644 rpc/json.go create mode 100644 rpc/json_test.go create mode 100644 rpc/server.go create mode 100644 rpc/server_test.go delete mode 100644 rpc/shared/errors.go delete mode 100644 rpc/shared/types.go delete mode 100644 rpc/shared/utils.go create mode 100644 rpc/types.go create mode 100644 rpc/types_test.go delete mode 100644 rpc/useragent/agent.go delete mode 100644 rpc/useragent/remote_frontend.go create mode 100644 rpc/utils.go delete mode 100644 rpc/v2/doc.go delete mode 100644 rpc/v2/errors.go delete mode 100644 rpc/v2/json.go delete mode 100644 rpc/v2/json_test.go delete mode 100644 rpc/v2/server.go delete mode 100644 rpc/v2/server_test.go delete mode 100644 rpc/v2/types.go delete mode 100644 rpc/v2/types_test.go delete mode 100644 rpc/v2/utils.go create mode 100644 rpc/websocket.go delete mode 100644 rpc/xeth.go (limited to 'rpc') diff --git a/rpc/api/admin.go b/rpc/api/admin.go deleted file mode 100644 index daf2f31b4..000000000 --- a/rpc/api/admin.go +++ /dev/null @@ -1,465 +0,0 @@ -// Copyright 2015 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library 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 Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see . - -package api - -import ( - "fmt" - "io" - "math/big" - "os" - "time" - - "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/common/compiler" - "github.com/ethereum/go-ethereum/common/natspec" - "github.com/ethereum/go-ethereum/common/registrar" - "github.com/ethereum/go-ethereum/core" - "github.com/ethereum/go-ethereum/core/types" - "github.com/ethereum/go-ethereum/crypto" - "github.com/ethereum/go-ethereum/eth" - "github.com/ethereum/go-ethereum/node" - "github.com/ethereum/go-ethereum/p2p/discover" - "github.com/ethereum/go-ethereum/rlp" - "github.com/ethereum/go-ethereum/rpc/codec" - "github.com/ethereum/go-ethereum/rpc/comms" - "github.com/ethereum/go-ethereum/rpc/shared" - "github.com/ethereum/go-ethereum/rpc/useragent" - "github.com/ethereum/go-ethereum/xeth" -) - -const ( - AdminApiversion = "1.0" - importBatchSize = 2500 -) - -var ( - // mapping between methods and handlers - AdminMapping = map[string]adminhandler{ - "admin_addPeer": (*adminApi).AddPeer, - "admin_peers": (*adminApi).Peers, - "admin_nodeInfo": (*adminApi).NodeInfo, - "admin_exportChain": (*adminApi).ExportChain, - "admin_importChain": (*adminApi).ImportChain, - "admin_setSolc": (*adminApi).SetSolc, - "admin_datadir": (*adminApi).DataDir, - "admin_startRPC": (*adminApi).StartRPC, - "admin_stopRPC": (*adminApi).StopRPC, - "admin_setGlobalRegistrar": (*adminApi).SetGlobalRegistrar, - "admin_setHashReg": (*adminApi).SetHashReg, - "admin_setUrlHint": (*adminApi).SetUrlHint, - "admin_saveInfo": (*adminApi).SaveInfo, - "admin_register": (*adminApi).Register, - "admin_registerUrl": (*adminApi).RegisterUrl, - "admin_startNatSpec": (*adminApi).StartNatSpec, - "admin_stopNatSpec": (*adminApi).StopNatSpec, - "admin_getContractInfo": (*adminApi).GetContractInfo, - "admin_httpGet": (*adminApi).HttpGet, - "admin_sleepBlocks": (*adminApi).SleepBlocks, - "admin_sleep": (*adminApi).Sleep, - "admin_enableUserAgent": (*adminApi).EnableUserAgent, - } -) - -// admin callback handler -type adminhandler func(*adminApi, *shared.Request) (interface{}, error) - -// admin api provider -type adminApi struct { - xeth *xeth.XEth - stack *node.Node - ethereum *eth.Ethereum - codec codec.Codec - coder codec.ApiCoder -} - -// create a new admin api instance -func NewAdminApi(xeth *xeth.XEth, stack *node.Node, codec codec.Codec) *adminApi { - api := &adminApi{ - xeth: xeth, - stack: stack, - codec: codec, - coder: codec.New(nil), - } - if stack != nil { - stack.Service(&api.ethereum) - } - return api -} - -// collection with supported methods -func (self *adminApi) Methods() []string { - methods := make([]string, len(AdminMapping)) - i := 0 - for k := range AdminMapping { - methods[i] = k - i++ - } - return methods -} - -// Execute given request -func (self *adminApi) Execute(req *shared.Request) (interface{}, error) { - if callback, ok := AdminMapping[req.Method]; ok { - return callback(self, req) - } - - return nil, &shared.NotImplementedError{req.Method} -} - -func (self *adminApi) Name() string { - return shared.AdminApiName -} - -func (self *adminApi) ApiVersion() string { - return AdminApiversion -} - -func (self *adminApi) AddPeer(req *shared.Request) (interface{}, error) { - args := new(AddPeerArgs) - if err := self.coder.Decode(req.Params, &args); err != nil { - return nil, shared.NewDecodeParamError(err.Error()) - } - node, err := discover.ParseNode(args.Url) - if err != nil { - return nil, fmt.Errorf("invalid node URL: %v", err) - } - self.stack.Server().AddPeer(node) - return true, nil -} - -func (self *adminApi) Peers(req *shared.Request) (interface{}, error) { - return self.stack.Server().PeersInfo(), nil -} - -func (self *adminApi) NodeInfo(req *shared.Request) (interface{}, error) { - return self.stack.Server().NodeInfo(), nil -} - -func (self *adminApi) DataDir(req *shared.Request) (interface{}, error) { - return self.stack.DataDir(), nil -} - -func hasAllBlocks(chain *core.BlockChain, bs []*types.Block) bool { - for _, b := range bs { - if !chain.HasBlock(b.Hash()) { - return false - } - } - return true -} - -func (self *adminApi) ImportChain(req *shared.Request) (interface{}, error) { - args := new(ImportExportChainArgs) - if err := self.coder.Decode(req.Params, &args); err != nil { - return nil, shared.NewDecodeParamError(err.Error()) - } - - fh, err := os.Open(args.Filename) - if err != nil { - return false, err - } - defer fh.Close() - stream := rlp.NewStream(fh, 0) - - // Run actual the import. - blocks := make(types.Blocks, importBatchSize) - n := 0 - for batch := 0; ; batch++ { - - i := 0 - for ; i < importBatchSize; i++ { - var b types.Block - if err := stream.Decode(&b); err == io.EOF { - break - } else if err != nil { - return false, fmt.Errorf("at block %d: %v", n, err) - } - blocks[i] = &b - n++ - } - if i == 0 { - break - } - // Import the batch. - if hasAllBlocks(self.ethereum.BlockChain(), blocks[:i]) { - continue - } - if _, err := self.ethereum.BlockChain().InsertChain(blocks[:i]); err != nil { - return false, fmt.Errorf("invalid block %d: %v", n, err) - } - } - return true, nil -} - -func (self *adminApi) ExportChain(req *shared.Request) (interface{}, error) { - args := new(ImportExportChainArgs) - if err := self.coder.Decode(req.Params, &args); err != nil { - return nil, shared.NewDecodeParamError(err.Error()) - } - - fh, err := os.OpenFile(args.Filename, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, os.ModePerm) - if err != nil { - return false, err - } - defer fh.Close() - if err := self.ethereum.BlockChain().Export(fh); err != nil { - return false, err - } - - return true, nil -} - -func (self *adminApi) SetSolc(req *shared.Request) (interface{}, error) { - args := new(SetSolcArgs) - if err := self.coder.Decode(req.Params, &args); err != nil { - return nil, shared.NewDecodeParamError(err.Error()) - } - - solc, err := self.xeth.SetSolc(args.Path) - if err != nil { - return nil, err - } - return solc.Info(), nil -} - -func (self *adminApi) StartRPC(req *shared.Request) (interface{}, error) { - args := new(StartRPCArgs) - if err := self.coder.Decode(req.Params, &args); err != nil { - return nil, shared.NewDecodeParamError(err.Error()) - } - - cfg := comms.HttpConfig{ - ListenAddress: args.ListenAddress, - ListenPort: args.ListenPort, - CorsDomain: args.CorsDomain, - } - - apis, err := ParseApiString(args.Apis, self.codec, self.xeth, self.stack) - if err != nil { - return false, err - } - - err = comms.StartHttp(cfg, self.codec, Merge(apis...)) - if err == nil { - return true, nil - } - return false, err -} - -func (self *adminApi) StopRPC(req *shared.Request) (interface{}, error) { - comms.StopHttp() - return true, nil -} - -func (self *adminApi) SleepBlocks(req *shared.Request) (interface{}, error) { - args := new(SleepBlocksArgs) - if err := self.coder.Decode(req.Params, &args); err != nil { - return nil, shared.NewDecodeParamError(err.Error()) - } - var timer <-chan time.Time - var height *big.Int - var err error - if args.Timeout > 0 { - timer = time.NewTimer(time.Duration(args.Timeout) * time.Second).C - } - - height = new(big.Int).Add(self.xeth.CurrentBlock().Number(), big.NewInt(args.N)) - height, err = sleepBlocks(self.xeth.UpdateState(), height, timer) - if err != nil { - return nil, err - } - return height.Uint64(), nil -} - -func sleepBlocks(wait chan *big.Int, height *big.Int, timer <-chan time.Time) (newHeight *big.Int, err error) { - wait <- height - select { - case <-timer: - // if times out make sure the xeth loop does not block - go func() { - select { - case wait <- nil: - case <-wait: - } - }() - return nil, fmt.Errorf("timeout") - case newHeight = <-wait: - } - return -} - -func (self *adminApi) Sleep(req *shared.Request) (interface{}, error) { - args := new(SleepArgs) - if err := self.coder.Decode(req.Params, &args); err != nil { - return nil, shared.NewDecodeParamError(err.Error()) - } - time.Sleep(time.Duration(args.S) * time.Second) - return nil, nil -} - -func (self *adminApi) SetGlobalRegistrar(req *shared.Request) (interface{}, error) { - args := new(SetGlobalRegistrarArgs) - if err := self.coder.Decode(req.Params, &args); err != nil { - return nil, shared.NewDecodeParamError(err.Error()) - } - - sender := common.HexToAddress(args.ContractAddress) - - reg := registrar.New(self.xeth) - txhash, err := reg.SetGlobalRegistrar(args.NameReg, sender) - if err != nil { - return false, err - } - - return txhash, nil -} - -func (self *adminApi) SetHashReg(req *shared.Request) (interface{}, error) { - args := new(SetHashRegArgs) - if err := self.coder.Decode(req.Params, &args); err != nil { - return nil, shared.NewDecodeParamError(err.Error()) - } - - reg := registrar.New(self.xeth) - sender := common.HexToAddress(args.Sender) - txhash, err := reg.SetHashReg(args.HashReg, sender) - if err != nil { - return false, err - } - - return txhash, nil -} - -func (self *adminApi) SetUrlHint(req *shared.Request) (interface{}, error) { - args := new(SetUrlHintArgs) - if err := self.coder.Decode(req.Params, &args); err != nil { - return nil, shared.NewDecodeParamError(err.Error()) - } - - urlHint := args.UrlHint - sender := common.HexToAddress(args.Sender) - - reg := registrar.New(self.xeth) - txhash, err := reg.SetUrlHint(urlHint, sender) - if err != nil { - return nil, err - } - - return txhash, nil -} - -func (self *adminApi) SaveInfo(req *shared.Request) (interface{}, error) { - args := new(SaveInfoArgs) - if err := self.coder.Decode(req.Params, &args); err != nil { - return nil, shared.NewDecodeParamError(err.Error()) - } - - contenthash, err := compiler.SaveInfo(&args.ContractInfo, args.Filename) - if err != nil { - return nil, err - } - - return contenthash.Hex(), nil -} - -func (self *adminApi) Register(req *shared.Request) (interface{}, error) { - args := new(RegisterArgs) - if err := self.coder.Decode(req.Params, &args); err != nil { - return nil, shared.NewDecodeParamError(err.Error()) - } - - sender := common.HexToAddress(args.Sender) - // sender and contract address are passed as hex strings - codeb := self.xeth.CodeAtBytes(args.Address) - codeHash := common.BytesToHash(crypto.Sha3(codeb)) - contentHash := common.HexToHash(args.ContentHashHex) - registry := registrar.New(self.xeth) - - _, err := registry.SetHashToHash(sender, codeHash, contentHash) - if err != nil { - return false, err - } - - return true, nil -} - -func (self *adminApi) RegisterUrl(req *shared.Request) (interface{}, error) { - args := new(RegisterUrlArgs) - if err := self.coder.Decode(req.Params, &args); err != nil { - return nil, shared.NewDecodeParamError(err.Error()) - } - - sender := common.HexToAddress(args.Sender) - registry := registrar.New(self.xeth) - _, err := registry.SetUrlToHash(sender, common.HexToHash(args.ContentHash), args.Url) - if err != nil { - return false, err - } - - return true, nil -} - -func (self *adminApi) StartNatSpec(req *shared.Request) (interface{}, error) { - self.ethereum.NatSpec = true - return true, nil -} - -func (self *adminApi) StopNatSpec(req *shared.Request) (interface{}, error) { - self.ethereum.NatSpec = false - return true, nil -} - -func (self *adminApi) GetContractInfo(req *shared.Request) (interface{}, error) { - args := new(GetContractInfoArgs) - if err := self.coder.Decode(req.Params, &args); err != nil { - return nil, shared.NewDecodeParamError(err.Error()) - } - - infoDoc, err := natspec.FetchDocsForContract(args.Contract, self.xeth, self.ethereum.HTTPClient()) - if err != nil { - return nil, err - } - - var info interface{} - err = self.coder.Decode(infoDoc, &info) - if err != nil { - return nil, err - } - - return info, nil -} - -func (self *adminApi) HttpGet(req *shared.Request) (interface{}, error) { - args := new(HttpGetArgs) - if err := self.coder.Decode(req.Params, &args); err != nil { - return nil, shared.NewDecodeParamError(err.Error()) - } - - resp, err := self.ethereum.HTTPClient().Get(args.Uri, args.Path) - if err != nil { - return nil, err - } - - return string(resp), nil -} - -func (self *adminApi) EnableUserAgent(req *shared.Request) (interface{}, error) { - if fe, ok := self.xeth.Frontend().(*useragent.RemoteFrontend); ok { - fe.Enable() - } - return true, nil -} diff --git a/rpc/api/admin_args.go b/rpc/api/admin_args.go deleted file mode 100644 index e09597ad4..000000000 --- a/rpc/api/admin_args.go +++ /dev/null @@ -1,468 +0,0 @@ -// Copyright 2015 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library 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 Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see . - -package api - -import ( - "encoding/json" - - "github.com/ethereum/go-ethereum/common/compiler" - "github.com/ethereum/go-ethereum/rpc/shared" -) - -type AddPeerArgs struct { - Url string -} - -func (args *AddPeerArgs) 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.NewDecodeParamError("Expected enode as argument") - } - - urlstr, ok := obj[0].(string) - if !ok { - return shared.NewInvalidTypeError("url", "not a string") - } - args.Url = urlstr - - return nil -} - -type ImportExportChainArgs struct { - Filename string -} - -func (args *ImportExportChainArgs) 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.NewDecodeParamError("Expected filename as argument") - } - - filename, ok := obj[0].(string) - if !ok { - return shared.NewInvalidTypeError("filename", "not a string") - } - args.Filename = filename - - return nil -} - -type SetSolcArgs struct { - Path string -} - -func (args *SetSolcArgs) 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.NewDecodeParamError("Expected path as argument") - } - - if pathstr, ok := obj[0].(string); ok { - args.Path = pathstr - return nil - } - - return shared.NewInvalidTypeError("path", "not a string") -} - -type StartRPCArgs struct { - ListenAddress string - ListenPort uint - CorsDomain string - Apis string -} - -func (args *StartRPCArgs) UnmarshalJSON(b []byte) (err error) { - var obj []interface{} - if err := json.Unmarshal(b, &obj); err != nil { - return shared.NewDecodeParamError(err.Error()) - } - - args.ListenAddress = "127.0.0.1" - args.ListenPort = 8545 - args.Apis = "net,eth,web3" - - if len(obj) >= 1 && obj[0] != nil { - if addr, ok := obj[0].(string); ok { - args.ListenAddress = addr - } else { - return shared.NewInvalidTypeError("listenAddress", "not a string") - } - } - - if len(obj) >= 2 && obj[1] != nil { - if port, ok := obj[1].(float64); ok && port >= 0 && port <= 64*1024 { - args.ListenPort = uint(port) - } else { - return shared.NewInvalidTypeError("listenPort", "not a valid port number") - } - } - - if len(obj) >= 3 && obj[2] != nil { - if corsDomain, ok := obj[2].(string); ok { - args.CorsDomain = corsDomain - } else { - return shared.NewInvalidTypeError("corsDomain", "not a string") - } - } - - if len(obj) >= 4 && obj[3] != nil { - if apis, ok := obj[3].(string); ok { - args.Apis = apis - } else { - return shared.NewInvalidTypeError("apis", "not a string") - } - } - - return nil -} - -type SleepArgs struct { - S int -} - -func (args *SleepArgs) 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 { - if obj[0] != nil { - if n, err := numString(obj[0]); err == nil { - args.S = int(n.Int64()) - } else { - return shared.NewInvalidTypeError("N", "not an integer: "+err.Error()) - } - } else { - return shared.NewInsufficientParamsError(0, 1) - } - } - return nil -} - -type SleepBlocksArgs struct { - N int64 - Timeout int64 -} - -func (args *SleepBlocksArgs) UnmarshalJSON(b []byte) (err error) { - - var obj []interface{} - if err := json.Unmarshal(b, &obj); err != nil { - return shared.NewDecodeParamError(err.Error()) - } - - args.N = 1 - args.Timeout = 0 - if len(obj) >= 1 && obj[0] != nil { - if n, err := numString(obj[0]); err == nil { - args.N = n.Int64() - } else { - return shared.NewInvalidTypeError("N", "not an integer: "+err.Error()) - } - } - - if len(obj) >= 2 && obj[1] != nil { - if n, err := numString(obj[1]); err == nil { - args.Timeout = n.Int64() - } else { - return shared.NewInvalidTypeError("Timeout", "not an integer: "+err.Error()) - } - } - - return nil -} - -type SetGlobalRegistrarArgs struct { - NameReg string - ContractAddress string -} - -func (args *SetGlobalRegistrarArgs) UnmarshalJSON(b []byte) (err error) { - var obj []interface{} - if err := json.Unmarshal(b, &obj); err != nil { - return shared.NewDecodeParamError(err.Error()) - } - - if len(obj) == 0 { - return shared.NewDecodeParamError("Expected namereg address") - } - - if len(obj) >= 1 { - if namereg, ok := obj[0].(string); ok { - args.NameReg = namereg - } else { - return shared.NewInvalidTypeError("NameReg", "not a string") - } - } - - if len(obj) >= 2 && obj[1] != nil { - if addr, ok := obj[1].(string); ok { - args.ContractAddress = addr - } else { - return shared.NewInvalidTypeError("ContractAddress", "not a string") - } - } - - return nil -} - -type SetHashRegArgs struct { - HashReg string - Sender string -} - -func (args *SetHashRegArgs) 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 && obj[0] != nil { - if hashreg, ok := obj[0].(string); ok { - args.HashReg = hashreg - } else { - return shared.NewInvalidTypeError("HashReg", "not a string") - } - } - - if len(obj) >= 2 && obj[1] != nil { - if sender, ok := obj[1].(string); ok { - args.Sender = sender - } else { - return shared.NewInvalidTypeError("Sender", "not a string") - } - } - - return nil -} - -type SetUrlHintArgs struct { - UrlHint string - Sender string -} - -func (args *SetUrlHintArgs) 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 && obj[0] != nil { - if urlhint, ok := obj[0].(string); ok { - args.UrlHint = urlhint - } else { - return shared.NewInvalidTypeError("UrlHint", "not a string") - } - } - - if len(obj) >= 2 && obj[1] != nil { - if sender, ok := obj[1].(string); ok { - args.Sender = sender - } else { - return shared.NewInvalidTypeError("Sender", "not a string") - } - } - - return nil -} - -type SaveInfoArgs struct { - ContractInfo compiler.ContractInfo - Filename string -} - -func (args *SaveInfoArgs) UnmarshalJSON(b []byte) (err error) { - var obj []interface{} - if err := json.Unmarshal(b, &obj); err != nil { - return shared.NewDecodeParamError(err.Error()) - } - - if len(obj) < 2 { - return shared.NewInsufficientParamsError(len(obj), 2) - } - - if jsonraw, err := json.Marshal(obj[0]); err == nil { - if err = json.Unmarshal(jsonraw, &args.ContractInfo); err != nil { - return err - } - } else { - return err - } - - if filename, ok := obj[1].(string); ok { - args.Filename = filename - } else { - return shared.NewInvalidTypeError("Filename", "not a string") - } - - return nil -} - -type RegisterArgs struct { - Sender string - Address string - ContentHashHex string -} - -func (args *RegisterArgs) UnmarshalJSON(b []byte) (err error) { - var obj []interface{} - if err := json.Unmarshal(b, &obj); err != nil { - return shared.NewDecodeParamError(err.Error()) - } - - if len(obj) < 3 { - return shared.NewInsufficientParamsError(len(obj), 3) - } - - if len(obj) >= 1 { - if sender, ok := obj[0].(string); ok { - args.Sender = sender - } else { - return shared.NewInvalidTypeError("Sender", "not a string") - } - } - - if len(obj) >= 2 { - if address, ok := obj[1].(string); ok { - args.Address = address - } else { - return shared.NewInvalidTypeError("Address", "not a string") - } - } - - if len(obj) >= 3 { - if hex, ok := obj[2].(string); ok { - args.ContentHashHex = hex - } else { - return shared.NewInvalidTypeError("ContentHashHex", "not a string") - } - } - - return nil -} - -type RegisterUrlArgs struct { - Sender string - ContentHash string - Url string -} - -func (args *RegisterUrlArgs) 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 { - if sender, ok := obj[0].(string); ok { - args.Sender = sender - } else { - return shared.NewInvalidTypeError("Sender", "not a string") - } - } - - if len(obj) >= 2 { - if sender, ok := obj[1].(string); ok { - args.ContentHash = sender - } else { - return shared.NewInvalidTypeError("ContentHash", "not a string") - } - } - - if len(obj) >= 3 { - if sender, ok := obj[2].(string); ok { - args.Url = sender - } else { - return shared.NewInvalidTypeError("Url", "not a string") - } - } - - return nil -} - -type GetContractInfoArgs struct { - Contract string -} - -func (args *GetContractInfoArgs) 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) - } - - if len(obj) >= 1 { - if contract, ok := obj[0].(string); ok { - args.Contract = contract - } else { - return shared.NewInvalidTypeError("Contract", "not a string") - } - } - - return nil -} - -type HttpGetArgs struct { - Uri string - Path string -} - -func (args *HttpGetArgs) 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) - } - - if len(obj) >= 1 { - if uri, ok := obj[0].(string); ok { - args.Uri = uri - } else { - return shared.NewInvalidTypeError("Uri", "not a string") - } - } - - if len(obj) >= 2 && obj[1] != nil { - if path, ok := obj[1].(string); ok { - args.Path = path - } else { - return shared.NewInvalidTypeError("Path", "not a string") - } - } - - return nil -} diff --git a/rpc/api/admin_js.go b/rpc/api/admin_js.go deleted file mode 100644 index e6171cc74..000000000 --- a/rpc/api/admin_js.go +++ /dev/null @@ -1,143 +0,0 @@ -// Copyright 2015 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library 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 Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see . - -package api - -const Admin_JS = ` -web3._extend({ - property: 'admin', - methods: - [ - new web3._extend.Method({ - name: 'addPeer', - call: 'admin_addPeer', - params: 1, - inputFormatter: [null] - }), - new web3._extend.Method({ - name: 'exportChain', - call: 'admin_exportChain', - params: 1, - inputFormatter: [null] - }), - new web3._extend.Method({ - name: 'importChain', - call: 'admin_importChain', - params: 1, - inputFormatter: [null] - }), - new web3._extend.Method({ - name: 'sleepBlocks', - call: 'admin_sleepBlocks', - params: 2, - inputFormatter: [null, null] - }), - new web3._extend.Method({ - name: 'setSolc', - call: 'admin_setSolc', - params: 1, - inputFormatter: [null] - }), - new web3._extend.Method({ - name: 'startRPC', - call: 'admin_startRPC', - params: 4, - inputFormatter: [null, null, null, null] - }), - new web3._extend.Method({ - name: 'stopRPC', - call: 'admin_stopRPC', - params: 0, - inputFormatter: [] - }), - new web3._extend.Method({ - name: 'setGlobalRegistrar', - call: 'admin_setGlobalRegistrar', - params: 2, - inputFormatter: [null,null] - }), - new web3._extend.Method({ - name: 'setHashReg', - call: 'admin_setHashReg', - params: 2, - inputFormatter: [null,null] - }), - new web3._extend.Method({ - name: 'setUrlHint', - call: 'admin_setUrlHint', - params: 2, - inputFormatter: [null,null] - }), - new web3._extend.Method({ - name: 'saveInfo', - call: 'admin_saveInfo', - params: 2, - inputFormatter: [null,null] - }), - new web3._extend.Method({ - name: 'register', - call: 'admin_register', - params: 3, - inputFormatter: [null,null,null] - }), - new web3._extend.Method({ - name: 'registerUrl', - call: 'admin_registerUrl', - params: 3, - inputFormatter: [null,null,null] - }), - new web3._extend.Method({ - name: 'startNatSpec', - call: 'admin_startNatSpec', - params: 0, - inputFormatter: [] - }), - new web3._extend.Method({ - name: 'stopNatSpec', - call: 'admin_stopNatSpec', - params: 0, - inputFormatter: [] - }), - new web3._extend.Method({ - name: 'getContractInfo', - call: 'admin_getContractInfo', - params: 1, - inputFormatter: [null], - }), - new web3._extend.Method({ - name: 'httpGet', - call: 'admin_httpGet', - params: 2, - inputFormatter: [null, null] - }) - ], - properties: - [ - new web3._extend.Property({ - name: 'nodeInfo', - getter: 'admin_nodeInfo' - }), - new web3._extend.Property({ - name: 'peers', - getter: 'admin_peers' - }), - new web3._extend.Property({ - name: 'datadir', - getter: 'admin_datadir' - }) - ] -}); -` diff --git a/rpc/api/api.go b/rpc/api/api.go deleted file mode 100644 index e03250ec6..000000000 --- a/rpc/api/api.go +++ /dev/null @@ -1,26 +0,0 @@ -// Copyright 2015 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library 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 Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see . - -package api - -import ( - "github.com/ethereum/go-ethereum/rpc/shared" -) - -// Merge multiple API's to a single API instance -func Merge(apis ...shared.EthereumApi) shared.EthereumApi { - return newMergedApi(apis...) -} diff --git a/rpc/api/api_test.go b/rpc/api/api_test.go deleted file mode 100644 index eb63e8151..000000000 --- a/rpc/api/api_test.go +++ /dev/null @@ -1,170 +0,0 @@ -// Copyright 2015 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library 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 Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see . - -package api - -import ( - "testing" - - "encoding/json" - "strconv" - - "github.com/ethereum/go-ethereum/common/compiler" - "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" -) - -func TestParseApiString(t *testing.T) { - apis, err := ParseApiString("", codec.JSON, nil, nil) - if err == nil { - t.Errorf("Expected an err from parsing empty API string but got nil") - } - - if len(apis) != 0 { - t.Errorf("Expected 0 apis from empty API string") - } - - apis, err = ParseApiString("eth", codec.JSON, nil, nil) - if err != nil { - t.Errorf("Expected nil err from parsing empty API string but got %v", err) - } - - if len(apis) != 1 { - t.Errorf("Expected 1 apis but got %d - %v", apis, apis) - } - - apis, err = ParseApiString("eth,eth", codec.JSON, nil, nil) - if err != nil { - t.Errorf("Expected nil err from parsing empty API string but got \"%v\"", err) - } - - if len(apis) != 2 { - t.Errorf("Expected 2 apis but got %d - %v", apis, apis) - } - - apis, err = ParseApiString("eth,invalid", codec.JSON, nil, nil) - if err == nil { - t.Errorf("Expected an err but got no err") - } - -} - -const solcVersion = "0.9.23" - -func TestCompileSolidity(t *testing.T) { - - solc, err := compiler.New("") - if solc == nil { - t.Skip("no solc found: skip") - } else if solc.Version() != solcVersion { - t.Skip("WARNING: skipping test because of solc different version (%v, test written for %v, may need to update)", solc.Version(), solcVersion) - } - source := `contract test {\n` + - " /// @notice Will multiply `a` by 7." + `\n` + - ` function multiply(uint a) returns(uint d) {\n` + - ` return a * 7;\n` + - ` }\n` + - `}\n` - - jsonstr := `{"jsonrpc":"2.0","method":"eth_compileSolidity","params":["` + source + `"],"id":64}` - - expCode := "0x605880600c6000396000f3006000357c010000000000000000000000000000000000000000000000000000000090048063c6888fa114602e57005b603d6004803590602001506047565b8060005260206000f35b60006007820290506053565b91905056" - expAbiDefinition := `[{"constant":false,"inputs":[{"name":"a","type":"uint256"}],"name":"multiply","outputs":[{"name":"d","type":"uint256"}],"type":"function"}]` - expUserDoc := `{"methods":{"multiply(uint256)":{"notice":"Will multiply ` + "`a`" + ` by 7."}}}` - expDeveloperDoc := `{"methods":{}}` - expCompilerVersion := solc.Version() - expLanguage := "Solidity" - expLanguageVersion := "0" - expSource := source - - eth := ð.Ethereum{} - xeth := xeth.NewTest(nil, nil) - api := NewEthApi(xeth, eth, codec.JSON) - - var rpcRequest shared.Request - json.Unmarshal([]byte(jsonstr), &rpcRequest) - - response, err := api.CompileSolidity(&rpcRequest) - if err != nil { - t.Errorf("Execution failed, %v", err) - } - - respjson, err := json.Marshal(response) - if err != nil { - t.Errorf("expected no error, got %v", err) - } - - var contracts = make(map[string]*compiler.Contract) - err = json.Unmarshal(respjson, &contracts) - if err != nil { - t.Errorf("expected no error, got %v", err) - } - - if len(contracts) != 1 { - t.Errorf("expected one contract, got %v", len(contracts)) - } - - contract := contracts["test"] - - if contract.Code != expCode { - t.Errorf("Expected \n%s got \n%s", expCode, contract.Code) - } - - if strconv.Quote(contract.Info.Source) != `"`+expSource+`"` { - t.Errorf("Expected \n'%s' got \n'%s'", expSource, strconv.Quote(contract.Info.Source)) - } - - if contract.Info.Language != expLanguage { - t.Errorf("Expected %s got %s", expLanguage, contract.Info.Language) - } - - if contract.Info.LanguageVersion != expLanguageVersion { - t.Errorf("Expected %s got %s", expLanguageVersion, contract.Info.LanguageVersion) - } - - if contract.Info.CompilerVersion != expCompilerVersion { - t.Errorf("Expected %s got %s", expCompilerVersion, contract.Info.CompilerVersion) - } - - userdoc, err := json.Marshal(contract.Info.UserDoc) - if err != nil { - t.Errorf("expected no error, got %v", err) - } - - devdoc, err := json.Marshal(contract.Info.DeveloperDoc) - if err != nil { - t.Errorf("expected no error, got %v", err) - } - - abidef, err := json.Marshal(contract.Info.AbiDefinition) - if err != nil { - t.Errorf("expected no error, got %v", err) - } - - if string(abidef) != expAbiDefinition { - t.Errorf("Expected \n'%s' got \n'%s'", expAbiDefinition, string(abidef)) - } - - if string(userdoc) != expUserDoc { - t.Errorf("Expected \n'%s' got \n'%s'", expUserDoc, string(userdoc)) - } - - if string(devdoc) != expDeveloperDoc { - t.Errorf("Expected %s got %s", expDeveloperDoc, string(devdoc)) - } -} diff --git a/rpc/api/args.go b/rpc/api/args.go deleted file mode 100644 index 20f073b67..000000000 --- a/rpc/api/args.go +++ /dev/null @@ -1,74 +0,0 @@ -// Copyright 2015 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library 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 Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see . - -package api - -import ( - "encoding/json" - - "github.com/ethereum/go-ethereum/rpc/shared" -) - -type CompileArgs struct { - Source string -} - -func (args *CompileArgs) 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) - } - argstr, ok := obj[0].(string) - if !ok { - return shared.NewInvalidTypeError("arg0", "is not a string") - } - args.Source = argstr - - return nil -} - -type FilterStringArgs struct { - Word string -} - -func (args *FilterStringArgs) 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) - } - - var argstr string - argstr, ok := obj[0].(string) - if !ok { - return shared.NewInvalidTypeError("filter", "not a string") - } - switch argstr { - case "latest", "pending": - break - default: - return shared.NewValidationError("Word", "Must be `latest` or `pending`") - } - args.Word = argstr - return nil -} diff --git a/rpc/api/args_test.go b/rpc/api/args_test.go deleted file mode 100644 index 130315bd9..000000000 --- a/rpc/api/args_test.go +++ /dev/null @@ -1,2649 +0,0 @@ -// Copyright 2015 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library 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 Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see . - -package api - -import ( - "bytes" - "encoding/json" - "fmt" - "math/big" - "testing" - - "github.com/ethereum/go-ethereum/rpc/shared" -) - -func TestBlockheightInvalidString(t *testing.T) { - v := "foo" - var num int64 - - str := ExpectInvalidTypeError(blockHeight(v, &num)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestBlockheightEarliest(t *testing.T) { - v := "earliest" - e := int64(0) - var num int64 - - err := blockHeight(v, &num) - if err != nil { - t.Error(err) - } - - if num != e { - t.Errorf("Expected %s but got %s", e, num) - } -} - -func TestBlockheightLatest(t *testing.T) { - v := "latest" - e := int64(-1) - var num int64 - - err := blockHeight(v, &num) - if err != nil { - t.Error(err) - } - - if num != e { - t.Errorf("Expected %s but got %s", e, num) - } -} - -func TestBlockheightPending(t *testing.T) { - v := "pending" - e := int64(-2) - var num int64 - - err := blockHeight(v, &num) - if err != nil { - t.Error(err) - } - - if num != e { - t.Errorf("Expected %s but got %s", e, num) - } -} - -func ExpectValidationError(err error) string { - var str string - switch err.(type) { - case nil: - str = "Expected error but didn't get one" - case *shared.ValidationError: - break - default: - str = fmt.Sprintf("Expected *rpc.ValidationError but got %T with message `%s`", err, err.Error()) - } - return str -} - -func ExpectInvalidTypeError(err error) string { - var str string - switch err.(type) { - case nil: - str = "Expected error but didn't get one" - case *shared.InvalidTypeError: - break - default: - str = fmt.Sprintf("Expected *rpc.InvalidTypeError but got %T with message `%s`", err, err.Error()) - } - return str -} - -func ExpectInsufficientParamsError(err error) string { - var str string - switch err.(type) { - case nil: - str = "Expected error but didn't get one" - case *shared.InsufficientParamsError: - break - default: - str = fmt.Sprintf("Expected *rpc.InsufficientParamsError but got %T with message %s", err, err.Error()) - } - return str -} - -func ExpectDecodeParamError(err error) string { - var str string - switch err.(type) { - case nil: - str = "Expected error but didn't get one" - case *shared.DecodeParamError: - break - default: - str = fmt.Sprintf("Expected *rpc.DecodeParamError but got %T with message `%s`", err, err.Error()) - } - return str -} - -func TestSha3(t *testing.T) { - input := `["0x68656c6c6f20776f726c64"]` - expected := "0x68656c6c6f20776f726c64" - - args := new(Sha3Args) - json.Unmarshal([]byte(input), &args) - - if args.Data != expected { - t.Error("got %s expected %s", input, expected) - } -} - -func TestSha3ArgsInvalid(t *testing.T) { - input := `{}` - - args := new(Sha3Args) - str := ExpectDecodeParamError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestSha3ArgsEmpty(t *testing.T) { - input := `[]` - - args := new(Sha3Args) - str := ExpectInsufficientParamsError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) - } -} -func TestSha3ArgsDataInvalid(t *testing.T) { - input := `[4]` - - args := new(Sha3Args) - str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestGetBalanceArgs(t *testing.T) { - input := `["0x407d73d8a49eeb85d32cf465507dd71d507100c1", "0x1f"]` - expected := new(GetBalanceArgs) - expected.Address = "0x407d73d8a49eeb85d32cf465507dd71d507100c1" - expected.BlockNumber = 31 - - args := new(GetBalanceArgs) - if err := json.Unmarshal([]byte(input), &args); err != nil { - t.Error(err) - } - - if args.Address != expected.Address { - t.Errorf("Address should be %v but is %v", expected.Address, args.Address) - } - - if args.BlockNumber != expected.BlockNumber { - t.Errorf("BlockNumber should be %v but is %v", expected.BlockNumber, args.BlockNumber) - } -} - -func TestGetBalanceArgsBlocknumMissing(t *testing.T) { - input := `["0x407d73d8a49eeb85d32cf465507dd71d507100c1"]` - expected := new(GetBalanceArgs) - expected.Address = "0x407d73d8a49eeb85d32cf465507dd71d507100c1" - expected.BlockNumber = -1 - - args := new(GetBalanceArgs) - if err := json.Unmarshal([]byte(input), &args); err != nil { - t.Error(err) - } - - if args.Address != expected.Address { - t.Errorf("Address should be %v but is %v", expected.Address, args.Address) - } - - if args.BlockNumber != expected.BlockNumber { - t.Errorf("BlockNumber should be %v but is %v", expected.BlockNumber, args.BlockNumber) - } -} - -func TestGetBalanceArgsLatest(t *testing.T) { - input := `["0x407d73d8a49eeb85d32cf465507dd71d507100c1", "latest"]` - expected := new(GetBalanceArgs) - expected.Address = "0x407d73d8a49eeb85d32cf465507dd71d507100c1" - expected.BlockNumber = -1 - - args := new(GetBalanceArgs) - if err := json.Unmarshal([]byte(input), &args); err != nil { - t.Error(err) - } - - if args.Address != expected.Address { - t.Errorf("Address should be %v but is %v", expected.Address, args.Address) - } - - if args.BlockNumber != expected.BlockNumber { - t.Errorf("BlockNumber should be %v but is %v", expected.BlockNumber, args.BlockNumber) - } -} - -func TestGetBalanceArgsEmpty(t *testing.T) { - input := `[]` - - args := new(GetBalanceArgs) - str := ExpectInsufficientParamsError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestGetBalanceArgsInvalid(t *testing.T) { - input := `6` - - args := new(GetBalanceArgs) - str := ExpectDecodeParamError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestGetBalanceArgsBlockInvalid(t *testing.T) { - input := `["0x407d73d8a49eeb85d32cf465507dd71d507100c1", false]` - - args := new(GetBalanceArgs) - str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestGetBalanceArgsAddressInvalid(t *testing.T) { - input := `[-9, "latest"]` - - args := new(GetBalanceArgs) - str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestGetBlockByHashArgs(t *testing.T) { - input := `["0xe670ec64341771606e55d6b4ca35a1a6b75ee3d5145a99d05921026d1527331", true]` - expected := new(GetBlockByHashArgs) - expected.BlockHash = "0xe670ec64341771606e55d6b4ca35a1a6b75ee3d5145a99d05921026d1527331" - expected.IncludeTxs = true - - args := new(GetBlockByHashArgs) - if err := json.Unmarshal([]byte(input), &args); err != nil { - t.Error(err) - } - - if args.BlockHash != expected.BlockHash { - t.Errorf("BlockHash should be %v but is %v", expected.BlockHash, args.BlockHash) - } - - if args.IncludeTxs != expected.IncludeTxs { - t.Errorf("IncludeTxs should be %v but is %v", expected.IncludeTxs, args.IncludeTxs) - } -} - -func TestGetBlockByHashArgsEmpty(t *testing.T) { - input := `[]` - - args := new(GetBlockByHashArgs) - str := ExpectInsufficientParamsError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestGetBlockByHashArgsInvalid(t *testing.T) { - input := `{}` - - args := new(GetBlockByHashArgs) - str := ExpectDecodeParamError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestGetBlockByHashArgsHashInt(t *testing.T) { - input := `[8]` - - args := new(GetBlockByHashArgs) - str := ExpectInsufficientParamsError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestGetBlockByHashArgsHashBool(t *testing.T) { - input := `[false, true]` - - args := new(GetBlockByHashArgs) - str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestGetBlockByNumberArgsBlockNum(t *testing.T) { - input := `[436, false]` - expected := new(GetBlockByNumberArgs) - expected.BlockNumber = 436 - expected.IncludeTxs = false - - args := new(GetBlockByNumberArgs) - if err := json.Unmarshal([]byte(input), &args); err != nil { - t.Error(err) - } - - if args.BlockNumber != expected.BlockNumber { - t.Errorf("BlockNumber should be %v but is %v", expected.BlockNumber, args.BlockNumber) - } - - if args.IncludeTxs != expected.IncludeTxs { - t.Errorf("IncludeTxs should be %v but is %v", expected.IncludeTxs, args.IncludeTxs) - } -} - -func TestGetBlockByNumberArgsBlockHex(t *testing.T) { - input := `["0x1b4", false]` - expected := new(GetBlockByNumberArgs) - expected.BlockNumber = 436 - expected.IncludeTxs = false - - args := new(GetBlockByNumberArgs) - if err := json.Unmarshal([]byte(input), &args); err != nil { - t.Error(err) - } - - if args.BlockNumber != expected.BlockNumber { - t.Errorf("BlockNumber should be %v but is %v", expected.BlockNumber, args.BlockNumber) - } - - if args.IncludeTxs != expected.IncludeTxs { - t.Errorf("IncludeTxs should be %v but is %v", expected.IncludeTxs, args.IncludeTxs) - } -} -func TestGetBlockByNumberArgsWords(t *testing.T) { - input := `["earliest", true]` - expected := new(GetBlockByNumberArgs) - expected.BlockNumber = 0 - expected.IncludeTxs = true - - args := new(GetBlockByNumberArgs) - if err := json.Unmarshal([]byte(input), &args); err != nil { - t.Error(err) - } - - if args.BlockNumber != expected.BlockNumber { - t.Errorf("BlockNumber should be %v but is %v", expected.BlockNumber, args.BlockNumber) - } - - if args.IncludeTxs != expected.IncludeTxs { - t.Errorf("IncludeTxs should be %v but is %v", expected.IncludeTxs, args.IncludeTxs) - } -} - -func TestGetBlockByNumberEmpty(t *testing.T) { - input := `[]` - - args := new(GetBlockByNumberArgs) - str := ExpectInsufficientParamsError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) - } -} - -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]` - - args := new(GetBlockByNumberArgs) - str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) - } -} -func TestGetBlockByNumberBlockObject(t *testing.T) { - input := `{}` - - args := new(GetBlockByNumberArgs) - str := ExpectDecodeParamError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestNewTxArgs(t *testing.T) { - input := `[{"from": "0xb60e8dd61c5d32be8058bb8eb970870f07233155", - "to": "0xd46e8dd67c5d32be8058bb8eb970870f072445675", - "gas": "0x76c0", - "gasPrice": "0x9184e72a000", - "value": "0x9184e72a000", - "data": "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675"}, - "0x10"]` - expected := new(NewTxArgs) - expected.From = "0xb60e8dd61c5d32be8058bb8eb970870f07233155" - expected.To = "0xd46e8dd67c5d32be8058bb8eb970870f072445675" - expected.Gas = big.NewInt(30400) - expected.GasPrice = big.NewInt(10000000000000) - expected.Value = big.NewInt(10000000000000) - expected.Data = "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675" - expected.BlockNumber = big.NewInt(16).Int64() - - args := new(NewTxArgs) - if err := json.Unmarshal([]byte(input), &args); err != nil { - t.Error(err) - } - - if expected.From != args.From { - t.Errorf("From shoud be %#v but is %#v", expected.From, args.From) - } - - if expected.To != args.To { - t.Errorf("To shoud be %#v but is %#v", expected.To, args.To) - } - - if bytes.Compare(expected.Gas.Bytes(), args.Gas.Bytes()) != 0 { - t.Errorf("Gas shoud be %#v but is %#v", expected.Gas.Bytes(), args.Gas.Bytes()) - } - - if bytes.Compare(expected.GasPrice.Bytes(), args.GasPrice.Bytes()) != 0 { - t.Errorf("GasPrice shoud be %#v but is %#v", expected.GasPrice, args.GasPrice) - } - - if bytes.Compare(expected.Value.Bytes(), args.Value.Bytes()) != 0 { - t.Errorf("Value shoud be %#v but is %#v", expected.Value, args.Value) - } - - if expected.Data != args.Data { - t.Errorf("Data shoud be %#v but is %#v", expected.Data, args.Data) - } - - if expected.BlockNumber != args.BlockNumber { - t.Errorf("BlockNumber shoud be %#v but is %#v", expected.BlockNumber, args.BlockNumber) - } -} - -func TestNewTxArgsInt(t *testing.T) { - input := `[{"from": "0xb60e8dd61c5d32be8058bb8eb970870f07233155", - "to": "0xd46e8dd67c5d32be8058bb8eb970870f072445675", - "gas": 100, - "gasPrice": 50, - "value": 8765456789, - "data": "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675"}, - 5]` - expected := new(NewTxArgs) - expected.Gas = big.NewInt(100) - expected.GasPrice = big.NewInt(50) - expected.Value = big.NewInt(8765456789) - expected.BlockNumber = int64(5) - - args := new(NewTxArgs) - 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) - } - - if bytes.Compare(expected.GasPrice.Bytes(), args.GasPrice.Bytes()) != 0 { - t.Errorf("GasPrice shoud be %v but is %v", expected.GasPrice, args.GasPrice) - } - - if bytes.Compare(expected.Value.Bytes(), args.Value.Bytes()) != 0 { - t.Errorf("Value shoud be %v but is %v", expected.Value, args.Value) - } - - if expected.BlockNumber != args.BlockNumber { - t.Errorf("BlockNumber shoud be %v but is %v", expected.BlockNumber, args.BlockNumber) - } -} - -func TestNewTxArgsBlockBool(t *testing.T) { - input := `[{"from": "0xb60e8dd61c5d32be8058bb8eb970870f07233155", - "to": "0xd46e8dd67c5d32be8058bb8eb970870f072445675", - "gas": "0x76c0", - "gasPrice": "0x9184e72a000", - "value": "0x9184e72a000", - "data": "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675"}, - false]` - - args := new(NewTxArgs) - str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestNewTxArgsGasInvalid(t *testing.T) { - input := `[{"from": "0xb60e8dd61c5d32be8058bb8eb970870f07233155", - "to": "0xd46e8dd67c5d32be8058bb8eb970870f072445675", - "gas": false, - "gasPrice": "0x9184e72a000", - "value": "0x9184e72a000", - "data": "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675" - }]` - - args := new(NewTxArgs) - str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestNewTxArgsGaspriceInvalid(t *testing.T) { - input := `[{"from": "0xb60e8dd61c5d32be8058bb8eb970870f07233155", - "to": "0xd46e8dd67c5d32be8058bb8eb970870f072445675", - "gas": "0x76c0", - "gasPrice": false, - "value": "0x9184e72a000", - "data": "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675" - }]` - - args := new(NewTxArgs) - str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestNewTxArgsValueInvalid(t *testing.T) { - input := `[{"from": "0xb60e8dd61c5d32be8058bb8eb970870f07233155", - "to": "0xd46e8dd67c5d32be8058bb8eb970870f072445675", - "gas": "0x76c0", - "gasPrice": "0x9184e72a000", - "value": false, - "data": "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675" - }]` - - args := new(NewTxArgs) - str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestNewTxArgsGasMissing(t *testing.T) { - input := `[{"from": "0xb60e8dd61c5d32be8058bb8eb970870f07233155", - "to": "0xd46e8dd67c5d32be8058bb8eb970870f072445675", - "gasPrice": "0x9184e72a000", - "value": "0x9184e72a000", - "data": "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675" - }]` - expected := new(NewTxArgs) - expected.Gas = nil - - args := new(NewTxArgs) - if err := json.Unmarshal([]byte(input), &args); err != nil { - t.Error(err) - } - - if args.Gas != expected.Gas { - // if bytes.Compare(expected.Gas.Bytes(), args.Gas.Bytes()) != 0 { - t.Errorf("Gas shoud be %v but is %v", expected.Gas, args.Gas) - } -} - -func TestNewTxArgsBlockGaspriceMissing(t *testing.T) { - input := `[{ - "from": "0xb60e8dd61c5d32be8058bb8eb970870f07233155", - "to": "0xd46e8dd67c5d32be8058bb8eb970870f072445675", - "gas": "0x76c0", - "value": "0x9184e72a000", - "data": "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675" - }]` - expected := new(NewTxArgs) - expected.GasPrice = nil - - args := new(NewTxArgs) - if err := json.Unmarshal([]byte(input), &args); err != nil { - t.Error(err) - } - - if args.GasPrice != expected.GasPrice { - // 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) { - input := `[{ - "from": "0xb60e8dd61c5d32be8058bb8eb970870f07233155", - "to": "0xd46e8dd67c5d32be8058bb8eb970870f072445675", - "gas": "0x76c0", - "gasPrice": "0x9184e72a000", - "data": "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675" - }]` - expected := new(NewTxArgs) - expected.Value = big.NewInt(0) - - args := new(NewTxArgs) - 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) { - input := `[]` - - args := new(NewTxArgs) - str := ExpectInsufficientParamsError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestNewTxArgsInvalid(t *testing.T) { - input := `{}` - - args := new(NewTxArgs) - str := ExpectDecodeParamError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) - } -} -func TestNewTxArgsNotStrings(t *testing.T) { - input := `[{"from":6}]` - - args := new(NewTxArgs) - str := ExpectDecodeParamError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestNewTxArgsFromEmpty(t *testing.T) { - input := `[{"to": "0xb60e8dd61c5d32be8058bb8eb970870f07233155"}]` - - args := new(NewTxArgs) - str := ExpectValidationError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestCallArgs(t *testing.T) { - input := `[{"from": "0xb60e8dd61c5d32be8058bb8eb970870f07233155", - "to": "0xd46e8dd67c5d32be8058bb8eb970870f072445675", - "gas": "0x76c0", - "gasPrice": "0x9184e72a000", - "value": "0x9184e72a000", - "data": "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675"}, - "0x10"]` - expected := new(CallArgs) - expected.From = "0xb60e8dd61c5d32be8058bb8eb970870f07233155" - expected.To = "0xd46e8dd67c5d32be8058bb8eb970870f072445675" - expected.Gas = big.NewInt(30400) - expected.GasPrice = big.NewInt(10000000000000) - expected.Value = big.NewInt(10000000000000) - expected.Data = "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675" - expected.BlockNumber = big.NewInt(16).Int64() - - args := new(CallArgs) - if err := json.Unmarshal([]byte(input), &args); err != nil { - t.Error(err) - } - - if expected.To != args.To { - t.Errorf("To shoud be %#v but is %#v", expected.To, args.To) - } - - if bytes.Compare(expected.Gas.Bytes(), args.Gas.Bytes()) != 0 { - t.Errorf("Gas shoud be %#v but is %#v", expected.Gas.Bytes(), args.Gas.Bytes()) - } - - if bytes.Compare(expected.GasPrice.Bytes(), args.GasPrice.Bytes()) != 0 { - t.Errorf("GasPrice shoud be %#v but is %#v", expected.GasPrice, args.GasPrice) - } - - if bytes.Compare(expected.Value.Bytes(), args.Value.Bytes()) != 0 { - t.Errorf("Value shoud be %#v but is %#v", expected.Value, args.Value) - } - - if expected.Data != args.Data { - t.Errorf("Data shoud be %#v but is %#v", expected.Data, args.Data) - } - - if expected.BlockNumber != args.BlockNumber { - t.Errorf("BlockNumber shoud be %#v but is %#v", expected.BlockNumber, args.BlockNumber) - } -} - -func TestCallArgsInt(t *testing.T) { - input := `[{"from": "0xb60e8dd61c5d32be8058bb8eb970870f07233155", - "to": "0xd46e8dd67c5d32be8058bb8eb970870f072445675", - "gas": 100, - "gasPrice": 50, - "value": 8765456789, - "data": "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675"}, - 5]` - expected := new(CallArgs) - expected.Gas = big.NewInt(100) - expected.GasPrice = big.NewInt(50) - expected.Value = big.NewInt(8765456789) - expected.BlockNumber = int64(5) - - args := new(CallArgs) - 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) - } - - if bytes.Compare(expected.GasPrice.Bytes(), args.GasPrice.Bytes()) != 0 { - t.Errorf("GasPrice shoud be %v but is %v", expected.GasPrice, args.GasPrice) - } - - if bytes.Compare(expected.Value.Bytes(), args.Value.Bytes()) != 0 { - t.Errorf("Value shoud be %v but is %v", expected.Value, args.Value) - } - - if expected.BlockNumber != args.BlockNumber { - t.Errorf("BlockNumber shoud be %v but is %v", expected.BlockNumber, args.BlockNumber) - } -} - -func TestCallArgsBlockBool(t *testing.T) { - input := `[{"from": "0xb60e8dd61c5d32be8058bb8eb970870f07233155", - "to": "0xd46e8dd67c5d32be8058bb8eb970870f072445675", - "gas": "0x76c0", - "gasPrice": "0x9184e72a000", - "value": "0x9184e72a000", - "data": "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675"}, - false]` - - args := new(CallArgs) - str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestCallArgsGasInvalid(t *testing.T) { - input := `[{"from": "0xb60e8dd61c5d32be8058bb8eb970870f07233155", - "to": "0xd46e8dd67c5d32be8058bb8eb970870f072445675", - "gas": false, - "gasPrice": "0x9184e72a000", - "value": "0x9184e72a000", - "data": "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675" - }]` - - args := new(CallArgs) - str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestCallArgsGaspriceInvalid(t *testing.T) { - input := `[{"from": "0xb60e8dd61c5d32be8058bb8eb970870f07233155", - "to": "0xd46e8dd67c5d32be8058bb8eb970870f072445675", - "gas": "0x76c0", - "gasPrice": false, - "value": "0x9184e72a000", - "data": "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675" - }]` - - args := new(CallArgs) - str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestCallArgsValueInvalid(t *testing.T) { - input := `[{"from": "0xb60e8dd61c5d32be8058bb8eb970870f07233155", - "to": "0xd46e8dd67c5d32be8058bb8eb970870f072445675", - "gas": "0x76c0", - "gasPrice": "0x9184e72a000", - "value": false, - "data": "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675" - }]` - - args := new(CallArgs) - str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestCallArgsGasMissing(t *testing.T) { - input := `[{"from": "0xb60e8dd61c5d32be8058bb8eb970870f07233155", - "to": "0xd46e8dd67c5d32be8058bb8eb970870f072445675", - "gasPrice": "0x9184e72a000", - "value": "0x9184e72a000", - "data": "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675" - }]` - - args := new(CallArgs) - if err := json.Unmarshal([]byte(input), &args); err != nil { - t.Error(err) - } - - expected := new(CallArgs) - expected.Gas = nil - - if args.Gas != expected.Gas { - // if bytes.Compare(expected.Gas.Bytes(), args.Gas.Bytes()) != 0 { - t.Errorf("Gas shoud be %v but is %v", expected.Gas, args.Gas) - } - -} - -func TestCallArgsBlockGaspriceMissing(t *testing.T) { - input := `[{ - "from": "0xb60e8dd61c5d32be8058bb8eb970870f07233155", - "to": "0xd46e8dd67c5d32be8058bb8eb970870f072445675", - "gas": "0x76c0", - "value": "0x9184e72a000", - "data": "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675" - }]` - - args := new(CallArgs) - if err := json.Unmarshal([]byte(input), &args); err != nil { - t.Error(err) - } - - expected := new(CallArgs) - expected.GasPrice = nil - - if args.GasPrice != expected.GasPrice { - // if bytes.Compare(expected.GasPrice.Bytes(), args.GasPrice.Bytes()) != 0 { - t.Errorf("GasPrice shoud be %v but is %v", expected.GasPrice, args.GasPrice) - } -} - -func TestCallArgsValueMissing(t *testing.T) { - input := `[{ - "from": "0xb60e8dd61c5d32be8058bb8eb970870f07233155", - "to": "0xd46e8dd67c5d32be8058bb8eb970870f072445675", - "gas": "0x76c0", - "gasPrice": "0x9184e72a000", - "data": "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675" - }]` - - args := new(CallArgs) - if err := json.Unmarshal([]byte(input), &args); err != nil { - t.Error(err) - } - - expected := new(CallArgs) - expected.Value = big.NewInt(int64(0)) - - if bytes.Compare(expected.Value.Bytes(), args.Value.Bytes()) != 0 { - t.Errorf("GasPrice shoud be %v but is %v", expected.Value, args.Value) - } -} - -func TestCallArgsEmpty(t *testing.T) { - input := `[]` - - args := new(CallArgs) - str := ExpectInsufficientParamsError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestCallArgsInvalid(t *testing.T) { - input := `{}` - - args := new(CallArgs) - str := ExpectDecodeParamError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) - } -} -func TestCallArgsNotStrings(t *testing.T) { - input := `[{"from":6}]` - - args := new(CallArgs) - str := ExpectDecodeParamError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestCallArgsToEmpty(t *testing.T) { - input := `[{"from": "0xb60e8dd61c5d32be8058bb8eb970870f07233155"}]` - args := new(CallArgs) - err := json.Unmarshal([]byte(input), &args) - if err != nil { - t.Error("Did not expect error. Got", err) - } -} - -func TestGetStorageArgs(t *testing.T) { - input := `["0x407d73d8a49eeb85d32cf465507dd71d507100c1", "latest"]` - expected := new(GetStorageArgs) - expected.Address = "0x407d73d8a49eeb85d32cf465507dd71d507100c1" - expected.BlockNumber = -1 - - args := new(GetStorageArgs) - if err := json.Unmarshal([]byte(input), &args); err != nil { - t.Error(err) - } - - if expected.Address != args.Address { - t.Errorf("Address shoud be %#v but is %#v", expected.Address, args.Address) - } - - if expected.BlockNumber != args.BlockNumber { - t.Errorf("BlockNumber shoud be %#v but is %#v", expected.BlockNumber, args.BlockNumber) - } -} - -func TestGetStorageArgsMissingBlocknum(t *testing.T) { - input := `["0x407d73d8a49eeb85d32cf465507dd71d507100c1"]` - expected := new(GetStorageArgs) - expected.Address = "0x407d73d8a49eeb85d32cf465507dd71d507100c1" - expected.BlockNumber = -1 - - args := new(GetStorageArgs) - if err := json.Unmarshal([]byte(input), &args); err != nil { - t.Error(err) - } - - if expected.Address != args.Address { - t.Errorf("Address shoud be %#v but is %#v", expected.Address, args.Address) - } - - if expected.BlockNumber != args.BlockNumber { - t.Errorf("BlockNumber shoud be %#v but is %#v", expected.BlockNumber, args.BlockNumber) - } -} - -func TestGetStorageInvalidArgs(t *testing.T) { - input := `{}` - - args := new(GetStorageArgs) - str := ExpectDecodeParamError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestGetStorageInvalidBlockheight(t *testing.T) { - input := `["0x407d73d8a49eeb85d32cf465507dd71d507100c1", {}]` - - args := new(GetStorageArgs) - str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestGetStorageEmptyArgs(t *testing.T) { - input := `[]` - - args := new(GetStorageArgs) - str := ExpectInsufficientParamsError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestGetStorageAddressInt(t *testing.T) { - input := `[32456785432456, "latest"]` - - args := new(GetStorageArgs) - str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestGetStorageAtArgs(t *testing.T) { - input := `["0x407d73d8a49eeb85d32cf465507dd71d507100c1", "0x0", "0x2"]` - expected := new(GetStorageAtArgs) - expected.Address = "0x407d73d8a49eeb85d32cf465507dd71d507100c1" - expected.Key = "0x0" - expected.BlockNumber = 2 - - args := new(GetStorageAtArgs) - if err := json.Unmarshal([]byte(input), &args); err != nil { - t.Error(err) - } - - if expected.Address != args.Address { - t.Errorf("Address shoud be %#v but is %#v", expected.Address, args.Address) - } - - if expected.Key != args.Key { - t.Errorf("Address shoud be %#v but is %#v", expected.Address, args.Address) - } - - if expected.BlockNumber != args.BlockNumber { - t.Errorf("BlockNumber shoud be %#v but is %#v", expected.BlockNumber, args.BlockNumber) - } -} - -func TestGetStorageAtArgsMissingBlocknum(t *testing.T) { - input := `["0x407d73d8a49eeb85d32cf465507dd71d507100c1", "0x0"]` - expected := new(GetStorageAtArgs) - expected.Address = "0x407d73d8a49eeb85d32cf465507dd71d507100c1" - expected.Key = "0x0" - expected.BlockNumber = -1 - - args := new(GetStorageAtArgs) - if err := json.Unmarshal([]byte(input), &args); err != nil { - t.Error(err) - } - - if expected.Address != args.Address { - t.Errorf("Address shoud be %#v but is %#v", expected.Address, args.Address) - } - - if expected.Key != args.Key { - t.Errorf("Address shoud be %#v but is %#v", expected.Address, args.Address) - } - - if expected.BlockNumber != args.BlockNumber { - t.Errorf("BlockNumber shoud be %#v but is %#v", expected.BlockNumber, args.BlockNumber) - } -} - -func TestGetStorageAtEmptyArgs(t *testing.T) { - input := `[]` - - args := new(GetStorageAtArgs) - str := ExpectInsufficientParamsError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestGetStorageAtArgsInvalid(t *testing.T) { - input := `{}` - - args := new(GetStorageAtArgs) - str := ExpectDecodeParamError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestGetStorageAtArgsAddressNotString(t *testing.T) { - input := `[true, "0x0", "0x2"]` - - args := new(GetStorageAtArgs) - str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestGetStorageAtArgsKeyNotString(t *testing.T) { - input := `["0x407d73d8a49eeb85d32cf465507dd71d507100c1", true, "0x2"]` - - args := new(GetStorageAtArgs) - str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestGetStorageAtArgsValueNotString(t *testing.T) { - input := `["0x407d73d8a49eeb85d32cf465507dd71d507100c1", "0x1", true]` - - args := new(GetStorageAtArgs) - str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestGetTxCountArgs(t *testing.T) { - input := `["0x407d73d8a49eeb85d32cf465507dd71d507100c1", "pending"]` - expected := new(GetTxCountArgs) - expected.Address = "0x407d73d8a49eeb85d32cf465507dd71d507100c1" - expected.BlockNumber = -2 - - args := new(GetTxCountArgs) - if err := json.Unmarshal([]byte(input), &args); err != nil { - t.Error(err) - } - - if expected.Address != args.Address { - t.Errorf("Address shoud be %#v but is %#v", expected.Address, args.Address) - } - - if expected.BlockNumber != args.BlockNumber { - t.Errorf("BlockNumber shoud be %#v but is %#v", expected.BlockNumber, args.BlockNumber) - } -} - -func TestGetTxCountEmptyArgs(t *testing.T) { - input := `[]` - - args := new(GetTxCountArgs) - str := ExpectInsufficientParamsError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestGetTxCountEmptyArgsInvalid(t *testing.T) { - input := `false` - - args := new(GetTxCountArgs) - str := ExpectDecodeParamError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestGetTxCountAddressNotString(t *testing.T) { - input := `[false, "pending"]` - - args := new(GetTxCountArgs) - str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestGetTxCountBlockheightMissing(t *testing.T) { - input := `["0x407d73d8a49eeb85d32cf465507dd71d507100c1"]` - expected := new(GetTxCountArgs) - expected.Address = "0x407d73d8a49eeb85d32cf465507dd71d507100c1" - expected.BlockNumber = -1 - - args := new(GetTxCountArgs) - if err := json.Unmarshal([]byte(input), &args); err != nil { - t.Error(err) - } - - if expected.Address != args.Address { - t.Errorf("Address shoud be %#v but is %#v", expected.Address, args.Address) - } - - if expected.BlockNumber != args.BlockNumber { - t.Errorf("BlockNumber shoud be %#v but is %#v", expected.BlockNumber, args.BlockNumber) - } -} - -func TestGetTxCountBlockheightInvalid(t *testing.T) { - input := `["0x407d73d8a49eeb85d32cf465507dd71d507100c1", {}]` - - args := new(GetTxCountArgs) - str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestGetDataArgs(t *testing.T) { - input := `["0xd5677cf67b5aa051bb40496e68ad359eb97cfbf8", "latest"]` - expected := new(GetDataArgs) - expected.Address = "0xd5677cf67b5aa051bb40496e68ad359eb97cfbf8" - expected.BlockNumber = -1 - - args := new(GetDataArgs) - if err := json.Unmarshal([]byte(input), &args); err != nil { - t.Error(err) - } - - if expected.Address != args.Address { - t.Errorf("Address shoud be %#v but is %#v", expected.Address, args.Address) - } - - if expected.BlockNumber != args.BlockNumber { - t.Errorf("BlockNumber shoud be %#v but is %#v", expected.BlockNumber, args.BlockNumber) - } -} - -func TestGetDataArgsBlocknumMissing(t *testing.T) { - input := `["0xd5677cf67b5aa051bb40496e68ad359eb97cfbf8"]` - expected := new(GetDataArgs) - expected.Address = "0xd5677cf67b5aa051bb40496e68ad359eb97cfbf8" - expected.BlockNumber = -1 - - args := new(GetDataArgs) - if err := json.Unmarshal([]byte(input), &args); err != nil { - t.Error(err) - } - - if expected.Address != args.Address { - t.Errorf("Address shoud be %#v but is %#v", expected.Address, args.Address) - } - - if expected.BlockNumber != args.BlockNumber { - t.Errorf("BlockNumber shoud be %#v but is %#v", expected.BlockNumber, args.BlockNumber) - } -} - -func TestGetDataArgsEmpty(t *testing.T) { - input := `[]` - - args := new(GetDataArgs) - str := ExpectInsufficientParamsError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestGetDataArgsInvalid(t *testing.T) { - input := `{}` - - args := new(GetDataArgs) - str := ExpectDecodeParamError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestGetDataArgsAddressNotString(t *testing.T) { - input := `[12, "latest"]` - - args := new(GetDataArgs) - str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestGetDataArgsBlocknumberNotString(t *testing.T) { - input := `["0xd5677cf67b5aa051bb40496e68ad359eb97cfbf8", false]` - - args := new(GetDataArgs) - str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestBlockFilterArgs(t *testing.T) { - input := `[{ - "fromBlock": "0x1", - "toBlock": "0x2", - "limit": "0x3", - "offset": "0x0", - "address": "0xd5677cf67b5aa051bb40496e68ad359eb97cfbf8", - "topics": - [ - ["0xAA", "0xBB"], - ["0xCC", "0xDD"] - ] - }]` - - expected := new(BlockFilterArgs) - expected.Earliest = 1 - expected.Latest = 2 - expected.Max = 3 - expected.Skip = 0 - expected.Address = []string{"0xd5677cf67b5aa051bb40496e68ad359eb97cfbf8"} - expected.Topics = [][]string{ - []string{"0xAA", "0xBB"}, - []string{"0xCC", "0xDD"}, - } - - args := new(BlockFilterArgs) - if err := json.Unmarshal([]byte(input), &args); err != nil { - t.Error(err) - } - - if expected.Earliest != args.Earliest { - t.Errorf("Earliest shoud be %#v but is %#v", expected.Earliest, args.Earliest) - } - - if expected.Latest != args.Latest { - t.Errorf("Latest shoud be %#v but is %#v", expected.Latest, args.Latest) - } - - if expected.Max != args.Max { - t.Errorf("Max shoud be %#v but is %#v", expected.Max, args.Max) - } - - if expected.Skip != args.Skip { - t.Errorf("Skip shoud be %#v but is %#v", expected.Skip, args.Skip) - } - - if expected.Address[0] != args.Address[0] { - t.Errorf("Address shoud be %#v but is %#v", expected.Address, args.Address) - } - - if expected.Topics[0][0] != args.Topics[0][0] { - t.Errorf("Topics shoud be %#v but is %#v", expected.Topics, args.Topics) - } - if expected.Topics[0][1] != args.Topics[0][1] { - t.Errorf("Topics shoud be %#v but is %#v", expected.Topics, args.Topics) - } - if expected.Topics[1][0] != args.Topics[1][0] { - t.Errorf("Topics shoud be %#v but is %#v", expected.Topics, args.Topics) - } - if expected.Topics[1][1] != args.Topics[1][1] { - t.Errorf("Topics shoud be %#v but is %#v", expected.Topics, args.Topics) - } - -} - -func TestBlockFilterArgsDefaults(t *testing.T) { - input := `[{ - "address": ["0xd5677cf67b5aa051bb40496e68ad359eb97cfbf8"], - "topics": ["0xAA","0xBB"] - }]` - expected := new(BlockFilterArgs) - expected.Earliest = -1 - expected.Latest = -1 - expected.Max = 100 - expected.Skip = 0 - expected.Address = []string{"0xd5677cf67b5aa051bb40496e68ad359eb97cfbf8"} - expected.Topics = [][]string{[]string{"0xAA"}, []string{"0xBB"}} - - args := new(BlockFilterArgs) - if err := json.Unmarshal([]byte(input), &args); err != nil { - t.Error(err) - } - - if expected.Earliest != args.Earliest { - t.Errorf("Earliest shoud be %#v but is %#v", expected.Earliest, args.Earliest) - } - - if expected.Latest != args.Latest { - t.Errorf("Latest shoud be %#v but is %#v", expected.Latest, args.Latest) - } - - if expected.Max != args.Max { - t.Errorf("Max shoud be %#v but is %#v", expected.Max, args.Max) - } - - if expected.Skip != args.Skip { - t.Errorf("Skip shoud be %#v but is %#v", expected.Skip, args.Skip) - } - - if expected.Address[0] != args.Address[0] { - t.Errorf("Address shoud be %#v but is %#v", expected.Address, args.Address) - } - - if expected.Topics[0][0] != args.Topics[0][0] { - t.Errorf("Topics shoud be %#v but is %#v", expected.Topics, args.Topics) - } - - if expected.Topics[1][0] != args.Topics[1][0] { - t.Errorf("Topics shoud be %#v but is %#v", expected.Topics, args.Topics) - } -} - -func TestBlockFilterArgsWords(t *testing.T) { - input := `[{"fromBlock": "latest", "toBlock": "latest"}]` - expected := new(BlockFilterArgs) - expected.Earliest = -1 - expected.Latest = -1 - - args := new(BlockFilterArgs) - if err := json.Unmarshal([]byte(input), &args); err != nil { - t.Error(err) - } - - if expected.Earliest != args.Earliest { - t.Errorf("Earliest shoud be %#v but is %#v", expected.Earliest, args.Earliest) - } - - input = `[{"toBlock": "pending"}]` - if err := json.Unmarshal([]byte(input), &args); err == nil { - t.Errorf("Pending isn't currently supported and should raise an unsupported error") - } -} - -func TestBlockFilterArgsInvalid(t *testing.T) { - input := `{}` - - args := new(BlockFilterArgs) - str := ExpectDecodeParamError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestBlockFilterArgsFromBool(t *testing.T) { - input := `[{ - "fromBlock": true, - "toBlock": "pending" - }]` - - args := new(BlockFilterArgs) - str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestBlockFilterArgsToBool(t *testing.T) { - input := `[{ - "fromBlock": "pending", - "toBlock": true - }]` - - args := new(BlockFilterArgs) - str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestBlockFilterArgsEmptyArgs(t *testing.T) { - input := `[]` - - args := new(BlockFilterArgs) - err := json.Unmarshal([]byte(input), &args) - if err == nil { - t.Error("Expected error but didn't get one") - } -} - -func TestBlockFilterArgsLimitInvalid(t *testing.T) { - input := `[{"limit": false}]` - - args := new(BlockFilterArgs) - str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestBlockFilterArgsOffsetInvalid(t *testing.T) { - input := `[{"offset": true}]` - - args := new(BlockFilterArgs) - str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestBlockFilterArgsAddressInt(t *testing.T) { - input := `[{ - "address": 1, - "topics": "0x12341234"}]` - - args := new(BlockFilterArgs) - str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestBlockFilterArgsAddressSliceInt(t *testing.T) { - input := `[{ - "address": [1], - "topics": "0x12341234"}]` - - args := new(BlockFilterArgs) - str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestBlockFilterArgsTopicInt(t *testing.T) { - input := `[{ - "address": ["0xd5677cf67b5aa051bb40496e68ad359eb97cfbf8"], - "topics": 1}]` - - args := new(BlockFilterArgs) - str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestBlockFilterArgsTopicSliceInt(t *testing.T) { - input := `[{ - "address": "0xd5677cf67b5aa051bb40496e68ad359eb97cfbf8", - "topics": [1]}]` - - args := new(BlockFilterArgs) - str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestBlockFilterArgsTopicSliceInt2(t *testing.T) { - input := `[{ - "address": "0xd5677cf67b5aa051bb40496e68ad359eb97cfbf8", - "topics": ["0xAA", [1]]}]` - - args := new(BlockFilterArgs) - str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestBlockFilterArgsTopicComplex(t *testing.T) { - input := `[{ - "address": "0xd5677cf67b5aa051bb40496e68ad359eb97cfbf8", - "topics": ["0xAA", ["0xBB", "0xCC"]] - }]` - - args := new(BlockFilterArgs) - if err := json.Unmarshal([]byte(input), &args); err != nil { - t.Error(err) - fmt.Printf("%v\n", args) - return - } - - if args.Topics[0][0] != "0xAA" { - t.Errorf("Topic should be %s but is %s", "0xAA", args.Topics[0][0]) - } - - if args.Topics[1][0] != "0xBB" { - t.Errorf("Topic should be %s but is %s", "0xBB", args.Topics[0][0]) - } - - if args.Topics[1][1] != "0xCC" { - t.Errorf("Topic should be %s but is %s", "0xCC", args.Topics[0][0]) - } -} - -func TestDbArgs(t *testing.T) { - input := `["testDB","myKey","0xbeef"]` - expected := new(DbArgs) - expected.Database = "testDB" - expected.Key = "myKey" - expected.Value = []byte("0xbeef") - - args := new(DbArgs) - if err := json.Unmarshal([]byte(input), &args); err != nil { - t.Error(err) - } - - if err := args.requirements(); err != nil { - t.Error(err) - } - - if expected.Database != args.Database { - t.Errorf("Database shoud be %#v but is %#v", expected.Database, args.Database) - } - - if expected.Key != args.Key { - t.Errorf("Key shoud be %#v but is %#v", expected.Key, args.Key) - } - - if bytes.Compare(expected.Value, args.Value) != 0 { - t.Errorf("Value shoud be %#v but is %#v", expected.Value, args.Value) - } -} - -func TestDbArgsInvalid(t *testing.T) { - input := `{}` - - args := new(DbArgs) - str := ExpectDecodeParamError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestDbArgsEmpty(t *testing.T) { - input := `[]` - - args := new(DbArgs) - str := ExpectInsufficientParamsError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestDbArgsDatabaseType(t *testing.T) { - input := `[true, "keyval", "valval"]` - - args := new(DbArgs) - str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestDbArgsKeyType(t *testing.T) { - input := `["dbval", 3, "valval"]` - - args := new(DbArgs) - str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestDbArgsValType(t *testing.T) { - input := `["dbval", "keyval", {}]` - - args := new(DbArgs) - str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestDbArgsDatabaseEmpty(t *testing.T) { - input := `["", "keyval", "valval"]` - - args := new(DbArgs) - if err := json.Unmarshal([]byte(input), &args); err != nil { - t.Error(err.Error()) - } - - str := ExpectValidationError(args.requirements()) - if len(str) > 0 { - t.Error(str) - } -} - -func TestDbArgsKeyEmpty(t *testing.T) { - input := `["dbval", "", "valval"]` - - args := new(DbArgs) - if err := json.Unmarshal([]byte(input), &args); err != nil { - t.Error(err.Error()) - } - - str := ExpectValidationError(args.requirements()) - if len(str) > 0 { - t.Error(str) - } -} - -func TestDbHexArgs(t *testing.T) { - input := `["testDB","myKey","0xbeef"]` - expected := new(DbHexArgs) - expected.Database = "testDB" - expected.Key = "myKey" - expected.Value = []byte{0xbe, 0xef} - - args := new(DbHexArgs) - if err := json.Unmarshal([]byte(input), &args); err != nil { - t.Error(err) - } - - if err := args.requirements(); err != nil { - t.Error(err) - } - - if expected.Database != args.Database { - t.Errorf("Database shoud be %#v but is %#v", expected.Database, args.Database) - } - - if expected.Key != args.Key { - t.Errorf("Key shoud be %#v but is %#v", expected.Key, args.Key) - } - - if bytes.Compare(expected.Value, args.Value) != 0 { - t.Errorf("Value shoud be %#v but is %#v", expected.Value, args.Value) - } -} - -func TestDbHexArgsInvalid(t *testing.T) { - input := `{}` - - args := new(DbHexArgs) - str := ExpectDecodeParamError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestDbHexArgsEmpty(t *testing.T) { - input := `[]` - - args := new(DbHexArgs) - str := ExpectInsufficientParamsError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestDbHexArgsDatabaseType(t *testing.T) { - input := `[true, "keyval", "valval"]` - - args := new(DbHexArgs) - str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestDbHexArgsKeyType(t *testing.T) { - input := `["dbval", 3, "valval"]` - - args := new(DbHexArgs) - str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestDbHexArgsValType(t *testing.T) { - input := `["dbval", "keyval", {}]` - - args := new(DbHexArgs) - str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestDbHexArgsDatabaseEmpty(t *testing.T) { - input := `["", "keyval", "valval"]` - - args := new(DbHexArgs) - if err := json.Unmarshal([]byte(input), &args); err != nil { - t.Error(err.Error()) - } - - str := ExpectValidationError(args.requirements()) - if len(str) > 0 { - t.Error(str) - } -} - -func TestDbHexArgsKeyEmpty(t *testing.T) { - input := `["dbval", "", "valval"]` - - args := new(DbHexArgs) - if err := json.Unmarshal([]byte(input), &args); err != nil { - t.Error(err.Error()) - } - - str := ExpectValidationError(args.requirements()) - if len(str) > 0 { - t.Error(str) - } -} - -func TestWhisperMessageArgs(t *testing.T) { - input := `[{"from":"0xc931d93e97ab07fe42d923478ba2465f2", - "topics": ["0x68656c6c6f20776f726c64"], - "payload":"0x68656c6c6f20776f726c64", - "ttl": "0x64", - "priority": "0x64"}]` - expected := new(WhisperMessageArgs) - expected.From = "0xc931d93e97ab07fe42d923478ba2465f2" - expected.To = "" - expected.Payload = "0x68656c6c6f20776f726c64" - expected.Priority = 100 - expected.Ttl = 100 - // expected.Topics = []string{"0x68656c6c6f20776f726c64"} - - args := new(WhisperMessageArgs) - if err := json.Unmarshal([]byte(input), &args); err != nil { - t.Error(err) - } - - if expected.From != args.From { - t.Errorf("From shoud be %#v but is %#v", expected.From, args.From) - } - - if expected.To != args.To { - t.Errorf("To shoud be %#v but is %#v", expected.To, args.To) - } - - if expected.Payload != args.Payload { - t.Errorf("Value shoud be %#v but is %#v", expected.Payload, args.Payload) - } - - if expected.Ttl != args.Ttl { - t.Errorf("Ttl shoud be %#v but is %#v", expected.Ttl, args.Ttl) - } - - if expected.Priority != args.Priority { - t.Errorf("Priority shoud be %#v but is %#v", expected.Priority, args.Priority) - } - - // if expected.Topics != args.Topics { - // t.Errorf("Topic shoud be %#v but is %#v", expected.Topic, args.Topic) - // } -} - -func TestWhisperMessageArgsInt(t *testing.T) { - input := `[{"from":"0xc931d93e97ab07fe42d923478ba2465f2", - "topics": ["0x68656c6c6f20776f726c64"], - "payload":"0x68656c6c6f20776f726c64", - "ttl": 12, - "priority": 16}]` - expected := new(WhisperMessageArgs) - expected.From = "0xc931d93e97ab07fe42d923478ba2465f2" - expected.To = "" - expected.Payload = "0x68656c6c6f20776f726c64" - expected.Priority = 16 - expected.Ttl = 12 - // expected.Topics = []string{"0x68656c6c6f20776f726c64"} - - args := new(WhisperMessageArgs) - if err := json.Unmarshal([]byte(input), &args); err != nil { - t.Error(err) - } - - if expected.From != args.From { - t.Errorf("From shoud be %#v but is %#v", expected.From, args.From) - } - - if expected.To != args.To { - t.Errorf("To shoud be %#v but is %#v", expected.To, args.To) - } - - if expected.Payload != args.Payload { - t.Errorf("Value shoud be %#v but is %#v", expected.Payload, args.Payload) - } - - if expected.Ttl != args.Ttl { - t.Errorf("Ttl shoud be %v but is %v", expected.Ttl, args.Ttl) - } - - if expected.Priority != args.Priority { - t.Errorf("Priority shoud be %v but is %v", expected.Priority, args.Priority) - } - - // if expected.Topics != args.Topics { - // t.Errorf("Topic shoud be %#v but is %#v", expected.Topic, args.Topic) - // } -} - -func TestWhisperMessageArgsInvalid(t *testing.T) { - input := `{}` - - args := new(WhisperMessageArgs) - str := ExpectDecodeParamError(json.Unmarshal([]byte(input), args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestWhisperMessageArgsEmpty(t *testing.T) { - input := `[]` - - args := new(WhisperMessageArgs) - str := ExpectInsufficientParamsError(json.Unmarshal([]byte(input), args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestWhisperMessageArgsTtlBool(t *testing.T) { - input := `[{"from":"0xc931d93e97ab07fe42d923478ba2465f2", - "topics": ["0x68656c6c6f20776f726c64"], - "payload":"0x68656c6c6f20776f726c64", - "ttl": true, - "priority": "0x64"}]` - args := new(WhisperMessageArgs) - str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestWhisperMessageArgsPriorityBool(t *testing.T) { - input := `[{"from":"0xc931d93e97ab07fe42d923478ba2465f2", - "topics": ["0x68656c6c6f20776f726c64"], - "payload":"0x68656c6c6f20776f726c64", - "ttl": "0x12", - "priority": true}]` - args := new(WhisperMessageArgs) - str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestFilterIdArgs(t *testing.T) { - input := `["0x7"]` - expected := new(FilterIdArgs) - expected.Id = 7 - - args := new(FilterIdArgs) - if err := json.Unmarshal([]byte(input), &args); err != nil { - t.Error(err) - } - - if expected.Id != args.Id { - t.Errorf("Id shoud be %#v but is %#v", expected.Id, args.Id) - } -} - -func TestFilterIdArgsInvalid(t *testing.T) { - input := `{}` - - args := new(FilterIdArgs) - str := ExpectDecodeParamError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Errorf(str) - } -} - -func TestFilterIdArgsEmpty(t *testing.T) { - input := `[]` - - args := new(FilterIdArgs) - str := ExpectInsufficientParamsError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Errorf(str) - } -} - -func TestFilterIdArgsBool(t *testing.T) { - input := `[true]` - - args := new(FilterIdArgs) - str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Errorf(str) - } -} - -func TestWhisperFilterArgs(t *testing.T) { - input := `[{"topics": ["0x68656c6c6f20776f726c64"], "to": "0x34ag445g3455b34"}]` - expected := new(WhisperFilterArgs) - expected.To = "0x34ag445g3455b34" - expected.Topics = [][]string{[]string{"0x68656c6c6f20776f726c64"}} - - args := new(WhisperFilterArgs) - if err := json.Unmarshal([]byte(input), &args); err != nil { - t.Error(err) - } - - if expected.To != args.To { - t.Errorf("To shoud be %#v but is %#v", expected.To, args.To) - } - - // if expected.Topics != args.Topics { - // t.Errorf("Topics shoud be %#v but is %#v", expected.Topics, args.Topics) - // } -} - -func TestWhisperFilterArgsInvalid(t *testing.T) { - input := `{}` - - args := new(WhisperFilterArgs) - str := ExpectDecodeParamError(json.Unmarshal([]byte(input), args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestWhisperFilterArgsEmpty(t *testing.T) { - input := `[]` - - args := new(WhisperFilterArgs) - str := ExpectInsufficientParamsError(json.Unmarshal([]byte(input), args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestWhisperFilterArgsToInt(t *testing.T) { - input := `[{"to": 2}]` - - args := new(WhisperFilterArgs) - str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestWhisperFilterArgsToBool(t *testing.T) { - input := `[{"topics": ["0x68656c6c6f20776f726c64"], "to": false}]` - - args := new(WhisperFilterArgs) - str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestWhisperFilterArgsToMissing(t *testing.T) { - input := `[{"topics": ["0x68656c6c6f20776f726c64"]}]` - expected := new(WhisperFilterArgs) - expected.To = "" - - args := new(WhisperFilterArgs) - if err := json.Unmarshal([]byte(input), &args); err != nil { - t.Error(err) - } - - if args.To != expected.To { - t.Errorf("To shoud be %v but is %v", expected.To, args.To) - } -} - -func TestWhisperFilterArgsTopicInt(t *testing.T) { - input := `[{"topics": [6], "to": "0x34ag445g3455b34"}]` - - args := new(WhisperFilterArgs) - str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestCompileArgs(t *testing.T) { - input := `["contract test { function multiply(uint a) returns(uint d) { return a * 7; } }"]` - expected := new(CompileArgs) - expected.Source = `contract test { function multiply(uint a) returns(uint d) { return a * 7; } }` - - args := new(CompileArgs) - if err := json.Unmarshal([]byte(input), &args); err != nil { - t.Error(err) - } - - if expected.Source != args.Source { - t.Errorf("Source shoud be %#v but is %#v", expected.Source, args.Source) - } -} - -func TestCompileArgsInvalid(t *testing.T) { - input := `{}` - - args := new(CompileArgs) - str := ExpectDecodeParamError(json.Unmarshal([]byte(input), args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestCompileArgsEmpty(t *testing.T) { - input := `[]` - - args := new(CompileArgs) - str := ExpectInsufficientParamsError(json.Unmarshal([]byte(input), args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestCompileArgsBool(t *testing.T) { - input := `[false]` - - args := new(CompileArgs) - str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestFilterStringArgs(t *testing.T) { - input := `["pending"]` - expected := new(FilterStringArgs) - expected.Word = "pending" - - args := new(FilterStringArgs) - if err := json.Unmarshal([]byte(input), &args); err != nil { - t.Error(err) - } - - if expected.Word != args.Word { - t.Errorf("Word shoud be %#v but is %#v", expected.Word, args.Word) - } -} - -func TestFilterStringEmptyArgs(t *testing.T) { - input := `[]` - - args := new(FilterStringArgs) - str := ExpectInsufficientParamsError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Errorf(str) - } -} - -func TestFilterStringInvalidArgs(t *testing.T) { - input := `{}` - - args := new(FilterStringArgs) - str := ExpectDecodeParamError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Errorf(str) - } -} - -func TestFilterStringWordInt(t *testing.T) { - input := `[7]` - - args := new(FilterStringArgs) - str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Errorf(str) - } -} - -func TestFilterStringWordWrong(t *testing.T) { - input := `["foo"]` - - args := new(FilterStringArgs) - str := ExpectValidationError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Errorf(str) - } -} - -func TestWhisperIdentityArgs(t *testing.T) { - input := `["0xc931d93e97ab07fe42d923478ba2465f283"]` - expected := new(WhisperIdentityArgs) - expected.Identity = "0xc931d93e97ab07fe42d923478ba2465f283" - - args := new(WhisperIdentityArgs) - if err := json.Unmarshal([]byte(input), &args); err != nil { - t.Error(err) - } - - if expected.Identity != args.Identity { - t.Errorf("Identity shoud be %#v but is %#v", expected.Identity, args.Identity) - } -} - -func TestWhisperIdentityArgsInvalid(t *testing.T) { - input := `{}` - - args := new(WhisperIdentityArgs) - str := ExpectDecodeParamError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Errorf(str) - } -} - -func TestWhisperIdentityArgsEmpty(t *testing.T) { - input := `[]` - - args := new(WhisperIdentityArgs) - str := ExpectInsufficientParamsError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Errorf(str) - } -} - -func TestWhisperIdentityArgsInt(t *testing.T) { - input := `[4]` - - args := new(WhisperIdentityArgs) - str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Errorf(str) - } -} - -func TestBlockNumArgs(t *testing.T) { - input := `["0x29a"]` - expected := new(BlockNumIndexArgs) - expected.BlockNumber = 666 - - args := new(BlockNumArg) - if err := json.Unmarshal([]byte(input), &args); err != nil { - t.Error(err) - } - - if expected.BlockNumber != args.BlockNumber { - t.Errorf("BlockNumber shoud be %#v but is %#v", expected.BlockNumber, args.BlockNumber) - } -} - -func TestBlockNumArgsWord(t *testing.T) { - input := `["pending"]` - expected := new(BlockNumIndexArgs) - expected.BlockNumber = -2 - - args := new(BlockNumArg) - if err := json.Unmarshal([]byte(input), &args); err != nil { - t.Error(err) - } - - if expected.BlockNumber != args.BlockNumber { - t.Errorf("BlockNumber shoud be %#v but is %#v", expected.BlockNumber, args.BlockNumber) - } -} - -func TestBlockNumArgsInvalid(t *testing.T) { - input := `{}` - - args := new(BlockNumArg) - str := ExpectDecodeParamError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestBlockNumArgsEmpty(t *testing.T) { - input := `[]` - - args := new(BlockNumArg) - str := ExpectInsufficientParamsError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) - } -} -func TestBlockNumArgsBool(t *testing.T) { - input := `[true]` - - args := new(BlockNumArg) - str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestBlockNumIndexArgs(t *testing.T) { - input := `["0x29a", "0x0"]` - expected := new(BlockNumIndexArgs) - expected.BlockNumber = 666 - expected.Index = 0 - - args := new(BlockNumIndexArgs) - if err := json.Unmarshal([]byte(input), &args); err != nil { - t.Error(err) - } - - if expected.BlockNumber != args.BlockNumber { - t.Errorf("BlockNumber shoud be %#v but is %#v", expected.BlockNumber, args.BlockNumber) - } - - if expected.Index != args.Index { - t.Errorf("Index shoud be %#v but is %#v", expected.Index, args.Index) - } -} - -func TestBlockNumIndexArgsWord(t *testing.T) { - input := `["latest", 67]` - expected := new(BlockNumIndexArgs) - expected.BlockNumber = -1 - expected.Index = 67 - - args := new(BlockNumIndexArgs) - if err := json.Unmarshal([]byte(input), &args); err != nil { - t.Error(err) - } - - if expected.BlockNumber != args.BlockNumber { - t.Errorf("BlockNumber shoud be %#v but is %#v", expected.BlockNumber, args.BlockNumber) - } - - if expected.Index != args.Index { - t.Errorf("Index shoud be %#v but is %#v", expected.Index, args.Index) - } -} - -func TestBlockNumIndexArgsEmpty(t *testing.T) { - input := `[]` - - args := new(BlockNumIndexArgs) - str := ExpectInsufficientParamsError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestBlockNumIndexArgsInvalid(t *testing.T) { - input := `"foo"` - - args := new(BlockNumIndexArgs) - str := ExpectDecodeParamError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestBlockNumIndexArgsBlocknumInvalid(t *testing.T) { - input := `[{}, "0x1"]` - - args := new(BlockNumIndexArgs) - str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestBlockNumIndexArgsIndexInvalid(t *testing.T) { - input := `["0x29a", true]` - - args := new(BlockNumIndexArgs) - str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestHashIndexArgs(t *testing.T) { - input := `["0xc6ef2fc5426d6ad6fd9e2a26abeab0aa2411b7ab17f30a99d3cb96aed1d1055b", "0x1"]` - expected := new(HashIndexArgs) - expected.Hash = "0xc6ef2fc5426d6ad6fd9e2a26abeab0aa2411b7ab17f30a99d3cb96aed1d1055b" - expected.Index = 1 - - args := new(HashIndexArgs) - if err := json.Unmarshal([]byte(input), &args); err != nil { - t.Error(err) - } - - if expected.Hash != args.Hash { - t.Errorf("Hash shoud be %#v but is %#v", expected.Hash, args.Hash) - } - - if expected.Index != args.Index { - t.Errorf("Index shoud be %#v but is %#v", expected.Index, args.Index) - } -} - -func TestHashIndexArgsEmpty(t *testing.T) { - input := `[]` - - args := new(HashIndexArgs) - str := ExpectInsufficientParamsError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestHashIndexArgsInvalid(t *testing.T) { - input := `{}` - - args := new(HashIndexArgs) - str := ExpectDecodeParamError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestHashIndexArgsInvalidHash(t *testing.T) { - input := `[7, "0x1"]` - - args := new(HashIndexArgs) - str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestHashIndexArgsInvalidIndex(t *testing.T) { - input := `["0xc6ef2fc5426d6ad6fd9e2a26abeab0aa2411b7ab17f30a99d3cb96aed1d1055b", false]` - - args := new(HashIndexArgs) - str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestHashArgs(t *testing.T) { - input := `["0xc6ef2fc5426d6ad6fd9e2a26abeab0aa2411b7ab17f30a99d3cb96aed1d1055b"]` - expected := new(HashIndexArgs) - expected.Hash = "0xc6ef2fc5426d6ad6fd9e2a26abeab0aa2411b7ab17f30a99d3cb96aed1d1055b" - - args := new(HashArgs) - if err := json.Unmarshal([]byte(input), &args); err != nil { - t.Error(err) - } - - if expected.Hash != args.Hash { - t.Errorf("Hash shoud be %#v but is %#v", expected.Hash, args.Hash) - } -} - -func TestHashArgsEmpty(t *testing.T) { - input := `[]` - - args := new(HashArgs) - str := ExpectInsufficientParamsError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestHashArgsInvalid(t *testing.T) { - input := `{}` - - args := new(HashArgs) - str := ExpectDecodeParamError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestHashArgsInvalidHash(t *testing.T) { - input := `[7]` - - args := new(HashArgs) - str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestSubmitWorkArgs(t *testing.T) { - input := `["0x0000000000000001", "0x1234567890abcdef1234567890abcdef", "0xD1GE5700000000000000000000000000"]` - expected := new(SubmitWorkArgs) - expected.Nonce = 1 - expected.Header = "0x1234567890abcdef1234567890abcdef" - expected.Digest = "0xD1GE5700000000000000000000000000" - - args := new(SubmitWorkArgs) - if err := json.Unmarshal([]byte(input), &args); err != nil { - t.Error(err) - } - - if expected.Nonce != args.Nonce { - t.Errorf("Nonce shoud be %d but is %d", expected.Nonce, args.Nonce) - } - - if expected.Header != args.Header { - t.Errorf("Header shoud be %#v but is %#v", expected.Header, args.Header) - } - - if expected.Digest != args.Digest { - t.Errorf("Digest shoud be %#v but is %#v", expected.Digest, args.Digest) - } -} - -func TestSubmitWorkArgsInvalid(t *testing.T) { - input := `{}` - - args := new(SubmitWorkArgs) - str := ExpectDecodeParamError(json.Unmarshal([]byte(input), args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestSubmitWorkArgsEmpty(t *testing.T) { - input := `[]` - - args := new(SubmitWorkArgs) - str := ExpectInsufficientParamsError(json.Unmarshal([]byte(input), args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestSubmitWorkArgsNonceInt(t *testing.T) { - input := `[1, "0x1234567890abcdef1234567890abcdef", "0xD1GE5700000000000000000000000000"]` - - args := new(SubmitWorkArgs) - str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), args)) - if len(str) > 0 { - t.Error(str) - } -} -func TestSubmitWorkArgsHeaderInt(t *testing.T) { - input := `["0x0000000000000001", 1, "0xD1GE5700000000000000000000000000"]` - - args := new(SubmitWorkArgs) - str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), args)) - if len(str) > 0 { - t.Error(str) - } -} -func TestSubmitWorkArgsDigestInt(t *testing.T) { - input := `["0x0000000000000001", "0x1234567890abcdef1234567890abcdef", 1]` - - args := new(SubmitWorkArgs) - str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestBlockHeightFromJsonInvalid(t *testing.T) { - var num int64 - var msg json.RawMessage = []byte(`}{`) - str := ExpectDecodeParamError(blockHeightFromJson(msg, &num)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestSourceArgsEmpty(t *testing.T) { - input := `[]` - - args := new(SourceArgs) - str := ExpectInsufficientParamsError(json.Unmarshal([]byte(input), &args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestSigArgs(t *testing.T) { - input := `["0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b", "0x0"]` - expected := new(NewSigArgs) - expected.From = "0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b" - expected.Data = "0x0" - - args := new(NewSigArgs) - if err := json.Unmarshal([]byte(input), &args); err != nil { - t.Error(err) - } - - if expected.From != args.From { - t.Errorf("From should be %v but is %v", expected.From, args.From) - } - - if expected.Data != args.Data { - t.Errorf("Data should be %v but is %v", expected.Data, args.Data) - } -} - -func TestSigArgsEmptyData(t *testing.T) { - input := `["0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b", ""]` - - args := new(NewSigArgs) - str := ExpectValidationError(json.Unmarshal([]byte(input), args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestSigArgsDataType(t *testing.T) { - input := `["0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b", 13]` - - args := new(NewSigArgs) - str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestSigArgsEmptyFrom(t *testing.T) { - input := `["", "0x0"]` - - args := new(NewSigArgs) - str := ExpectValidationError(json.Unmarshal([]byte(input), args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestSigArgsFromType(t *testing.T) { - input := `[false, "0x0"]` - - args := new(NewSigArgs) - str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestSigArgsEmpty(t *testing.T) { - input := `[]` - args := new(NewSigArgs) - str := ExpectInsufficientParamsError(json.Unmarshal([]byte(input), args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestDataArgs(t *testing.T) { - input := `["0x0123"]` - expected := new(NewDataArgs) - expected.Data = "0x0123" - - args := new(NewDataArgs) - if err := json.Unmarshal([]byte(input), &args); err != nil { - t.Error(err) - } - - if expected.Data != args.Data { - t.Errorf("Data should be %v but is %v", expected.Data, args.Data) - } -} - -func TestDataArgsEmptyData(t *testing.T) { - input := `[""]` - - args := new(NewDataArgs) - str := ExpectValidationError(json.Unmarshal([]byte(input), args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestDataArgsDataType(t *testing.T) { - input := `[13]` - - args := new(NewDataArgs) - str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestDataArgsEmpty(t *testing.T) { - input := `[]` - args := new(NewDataArgs) - str := ExpectInsufficientParamsError(json.Unmarshal([]byte(input), args)) - if len(str) > 0 { - t.Error(str) - } -} - -func TestDataArgsInvalid(t *testing.T) { - input := `{}` - - args := new(NewDataArgs) - str := ExpectDecodeParamError(json.Unmarshal([]byte(input), args)) - if len(str) > 0 { - t.Error(str) - } -} diff --git a/rpc/api/db.go b/rpc/api/db.go deleted file mode 100644 index 0eddc410e..000000000 --- a/rpc/api/db.go +++ /dev/null @@ -1,144 +0,0 @@ -// Copyright 2015 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library 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 Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see . - -package api - -import ( - "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" -) - -const ( - DbApiversion = "1.0" -) - -var ( - // mapping between methods and handlers - DbMapping = map[string]dbhandler{ - "db_getString": (*dbApi).GetString, - "db_putString": (*dbApi).PutString, - "db_getHex": (*dbApi).GetHex, - "db_putHex": (*dbApi).PutHex, - } -) - -// db callback handler -type dbhandler func(*dbApi, *shared.Request) (interface{}, error) - -// db api provider -type dbApi struct { - xeth *xeth.XEth - ethereum *eth.Ethereum - methods map[string]dbhandler - codec codec.ApiCoder -} - -// create a new db api instance -func NewDbApi(xeth *xeth.XEth, ethereum *eth.Ethereum, coder codec.Codec) *dbApi { - return &dbApi{ - xeth: xeth, - ethereum: ethereum, - methods: DbMapping, - codec: coder.New(nil), - } -} - -// collection with supported methods -func (self *dbApi) Methods() []string { - methods := make([]string, len(self.methods)) - i := 0 - for k := range self.methods { - methods[i] = k - i++ - } - return methods -} - -// Execute given request -func (self *dbApi) Execute(req *shared.Request) (interface{}, error) { - if callback, ok := self.methods[req.Method]; ok { - return callback(self, req) - } - - return nil, &shared.NotImplementedError{req.Method} -} - -func (self *dbApi) Name() string { - return shared.DbApiName -} - -func (self *dbApi) ApiVersion() string { - return DbApiversion -} - -func (self *dbApi) GetString(req *shared.Request) (interface{}, error) { - args := new(DbArgs) - if err := self.codec.Decode(req.Params, &args); err != nil { - return nil, shared.NewDecodeParamError(err.Error()) - } - - if err := args.requirements(); err != nil { - return nil, err - } - - ret, err := self.xeth.DbGet([]byte(args.Database + args.Key)) - return string(ret), err -} - -func (self *dbApi) PutString(req *shared.Request) (interface{}, error) { - args := new(DbArgs) - if err := self.codec.Decode(req.Params, &args); err != nil { - return nil, shared.NewDecodeParamError(err.Error()) - } - - if err := args.requirements(); err != nil { - return nil, err - } - - return self.xeth.DbPut([]byte(args.Database+args.Key), args.Value), nil -} - -func (self *dbApi) GetHex(req *shared.Request) (interface{}, error) { - args := new(DbHexArgs) - if err := self.codec.Decode(req.Params, &args); err != nil { - return nil, shared.NewDecodeParamError(err.Error()) - } - - if err := args.requirements(); err != nil { - return nil, err - } - - if res, err := self.xeth.DbGet([]byte(args.Database + args.Key)); err == nil { - return newHexData(res), nil - } else { - return nil, err - } -} - -func (self *dbApi) PutHex(req *shared.Request) (interface{}, error) { - args := new(DbHexArgs) - if err := self.codec.Decode(req.Params, &args); err != nil { - return nil, shared.NewDecodeParamError(err.Error()) - } - - if err := args.requirements(); err != nil { - return nil, err - } - - return self.xeth.DbPut([]byte(args.Database+args.Key), args.Value), nil -} diff --git a/rpc/api/db_args.go b/rpc/api/db_args.go deleted file mode 100644 index d61ea77ee..000000000 --- a/rpc/api/db_args.go +++ /dev/null @@ -1,126 +0,0 @@ -// Copyright 2015 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library 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 Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see . - -package api - -import ( - "encoding/json" - - "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/rpc/shared" -) - -type DbArgs struct { - Database string - Key string - Value []byte -} - -func (args *DbArgs) UnmarshalJSON(b []byte) (err error) { - var obj []interface{} - if err := json.Unmarshal(b, &obj); err != nil { - return shared.NewDecodeParamError(err.Error()) - } - - if len(obj) < 2 { - return shared.NewInsufficientParamsError(len(obj), 2) - } - - var objstr string - var ok bool - - if objstr, ok = obj[0].(string); !ok { - return shared.NewInvalidTypeError("database", "not a string") - } - args.Database = objstr - - if objstr, ok = obj[1].(string); !ok { - return shared.NewInvalidTypeError("key", "not a string") - } - args.Key = objstr - - if len(obj) > 2 { - objstr, ok = obj[2].(string) - if !ok { - return shared.NewInvalidTypeError("value", "not a string") - } - - args.Value = []byte(objstr) - } - - return nil -} - -func (a *DbArgs) requirements() error { - if len(a.Database) == 0 { - return shared.NewValidationError("Database", "cannot be blank") - } - if len(a.Key) == 0 { - return shared.NewValidationError("Key", "cannot be blank") - } - return nil -} - -type DbHexArgs struct { - Database string - Key string - Value []byte -} - -func (args *DbHexArgs) UnmarshalJSON(b []byte) (err error) { - var obj []interface{} - if err := json.Unmarshal(b, &obj); err != nil { - return shared.NewDecodeParamError(err.Error()) - } - - if len(obj) < 2 { - return shared.NewInsufficientParamsError(len(obj), 2) - } - - var objstr string - var ok bool - - if objstr, ok = obj[0].(string); !ok { - return shared.NewInvalidTypeError("database", "not a string") - } - args.Database = objstr - - if objstr, ok = obj[1].(string); !ok { - return shared.NewInvalidTypeError("key", "not a string") - } - args.Key = objstr - - if len(obj) > 2 { - objstr, ok = obj[2].(string) - if !ok { - return shared.NewInvalidTypeError("value", "not a string") - } - - args.Value = common.FromHex(objstr) - } - - return nil -} - -func (a *DbHexArgs) requirements() error { - if len(a.Database) == 0 { - return shared.NewValidationError("Database", "cannot be blank") - } - if len(a.Key) == 0 { - return shared.NewValidationError("Key", "cannot be blank") - } - return nil -} diff --git a/rpc/api/db_js.go b/rpc/api/db_js.go deleted file mode 100644 index 899f8abd9..000000000 --- a/rpc/api/db_js.go +++ /dev/null @@ -1,29 +0,0 @@ -// Copyright 2015 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library 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 Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see . - -package api - -const Db_JS = ` -web3._extend({ - property: 'db', - methods: - [ - ], - properties: - [ - ] -}); -` diff --git a/rpc/api/debug.go b/rpc/api/debug.go deleted file mode 100644 index a6faa335e..000000000 --- a/rpc/api/debug.go +++ /dev/null @@ -1,303 +0,0 @@ -// Copyright 2015 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library 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 Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see . - -package api - -import ( - "fmt" - "strings" - "time" - - "github.com/ethereum/ethash" - "github.com/ethereum/go-ethereum/core" - "github.com/ethereum/go-ethereum/core/state" - "github.com/ethereum/go-ethereum/core/vm" - "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" - "github.com/rcrowley/go-metrics" -) - -const ( - DebugApiVersion = "1.0" -) - -var ( - // mapping between methods and handlers - DebugMapping = map[string]debughandler{ - "debug_dumpBlock": (*debugApi).DumpBlock, - "debug_getBlockRlp": (*debugApi).GetBlockRlp, - "debug_printBlock": (*debugApi).PrintBlock, - "debug_processBlock": (*debugApi).ProcessBlock, - "debug_seedHash": (*debugApi).SeedHash, - "debug_setHead": (*debugApi).SetHead, - "debug_metrics": (*debugApi).Metrics, - } -) - -// debug callback handler -type debughandler func(*debugApi, *shared.Request) (interface{}, error) - -// admin api provider -type debugApi struct { - xeth *xeth.XEth - ethereum *eth.Ethereum - methods map[string]debughandler - codec codec.ApiCoder -} - -// create a new debug api instance -func NewDebugApi(xeth *xeth.XEth, ethereum *eth.Ethereum, coder codec.Codec) *debugApi { - return &debugApi{ - xeth: xeth, - ethereum: ethereum, - methods: DebugMapping, - codec: coder.New(nil), - } -} - -// collection with supported methods -func (self *debugApi) Methods() []string { - methods := make([]string, len(self.methods)) - i := 0 - for k := range self.methods { - methods[i] = k - i++ - } - return methods -} - -// Execute given request -func (self *debugApi) Execute(req *shared.Request) (interface{}, error) { - if callback, ok := self.methods[req.Method]; ok { - return callback(self, req) - } - - return nil, &shared.NotImplementedError{req.Method} -} - -func (self *debugApi) Name() string { - return shared.DebugApiName -} - -func (self *debugApi) ApiVersion() string { - return DebugApiVersion -} - -func (self *debugApi) PrintBlock(req *shared.Request) (interface{}, error) { - args := new(BlockNumArg) - if err := self.codec.Decode(req.Params, &args); err != nil { - return nil, shared.NewDecodeParamError(err.Error()) - } - - block := self.xeth.EthBlockByNumber(args.BlockNumber) - return fmt.Sprintf("%s", block), nil -} - -func (self *debugApi) DumpBlock(req *shared.Request) (interface{}, error) { - args := new(BlockNumArg) - if err := self.codec.Decode(req.Params, &args); err != nil { - return nil, shared.NewDecodeParamError(err.Error()) - } - - block := self.xeth.EthBlockByNumber(args.BlockNumber) - if block == nil { - return nil, fmt.Errorf("block #%d not found", args.BlockNumber) - } - - stateDb, err := state.New(block.Root(), self.ethereum.ChainDb()) - if err != nil { - return nil, err - } - - return stateDb.RawDump(), nil -} - -func (self *debugApi) GetBlockRlp(req *shared.Request) (interface{}, error) { - args := new(BlockNumArg) - if err := self.codec.Decode(req.Params, &args); err != nil { - return nil, shared.NewDecodeParamError(err.Error()) - } - - block := self.xeth.EthBlockByNumber(args.BlockNumber) - if block == nil { - return nil, fmt.Errorf("block #%d not found", args.BlockNumber) - } - encoded, err := rlp.EncodeToBytes(block) - return fmt.Sprintf("%x", encoded), err -} - -func (self *debugApi) SetHead(req *shared.Request) (interface{}, error) { - args := new(BlockNumArg) - if err := self.codec.Decode(req.Params, &args); err != nil { - return nil, shared.NewDecodeParamError(err.Error()) - } - self.ethereum.BlockChain().SetHead(uint64(args.BlockNumber)) - - return nil, nil -} - -func (self *debugApi) ProcessBlock(req *shared.Request) (interface{}, error) { - args := new(BlockNumArg) - if err := self.codec.Decode(req.Params, &args); err != nil { - return nil, shared.NewDecodeParamError(err.Error()) - } - - block := self.xeth.EthBlockByNumber(args.BlockNumber) - if block == nil { - return nil, fmt.Errorf("block #%d not found", args.BlockNumber) - } - - old := vm.Debug - defer func() { vm.Debug = old }() - vm.Debug = true - - var ( - blockchain = self.ethereum.BlockChain() - validator = blockchain.Validator() - processor = blockchain.Processor() - ) - - err := core.ValidateHeader(blockchain.AuxValidator(), block.Header(), blockchain.GetHeader(block.ParentHash()), true, false) - if err != nil { - return false, err - } - statedb, err := state.New(blockchain.GetBlock(block.ParentHash()).Root(), self.ethereum.ChainDb()) - if err != nil { - return false, err - } - receipts, _, usedGas, err := processor.Process(block, statedb) - if err != nil { - return false, err - } - err = validator.ValidateState(block, blockchain.GetBlock(block.ParentHash()), statedb, receipts, usedGas) - if err != nil { - return false, err - } - - return true, nil -} - -func (self *debugApi) SeedHash(req *shared.Request) (interface{}, error) { - args := new(BlockNumArg) - if err := self.codec.Decode(req.Params, &args); err != nil { - return nil, shared.NewDecodeParamError(err.Error()) - } - - if hash, err := ethash.GetSeedHash(uint64(args.BlockNumber)); err == nil { - return fmt.Sprintf("0x%x", hash), nil - } else { - return nil, err - } -} - -func (self *debugApi) Metrics(req *shared.Request) (interface{}, error) { - args := new(MetricsArgs) - if err := self.codec.Decode(req.Params, &args); err != nil { - return nil, shared.NewDecodeParamError(err.Error()) - } - // Create a rate formatter - units := []string{"", "K", "M", "G", "T", "E", "P"} - round := func(value float64, prec int) string { - unit := 0 - for value >= 1000 { - unit, value, prec = unit+1, value/1000, 2 - } - return fmt.Sprintf(fmt.Sprintf("%%.%df%s", prec, units[unit]), value) - } - format := func(total float64, rate float64) string { - return fmt.Sprintf("%s (%s/s)", round(total, 0), round(rate, 2)) - } - // Iterate over all the metrics, and just dump for now - counters := make(map[string]interface{}) - metrics.DefaultRegistry.Each(func(name string, metric interface{}) { - // Create or retrieve the counter hierarchy for this metric - root, parts := counters, strings.Split(name, "/") - for _, part := range parts[:len(parts)-1] { - if _, ok := root[part]; !ok { - root[part] = make(map[string]interface{}) - } - root = root[part].(map[string]interface{}) - } - name = parts[len(parts)-1] - - // Fill the counter with the metric details, formatting if requested - if args.Raw { - switch metric := metric.(type) { - case metrics.Meter: - root[name] = map[string]interface{}{ - "AvgRate01Min": metric.Rate1(), - "AvgRate05Min": metric.Rate5(), - "AvgRate15Min": metric.Rate15(), - "MeanRate": metric.RateMean(), - "Overall": float64(metric.Count()), - } - - case metrics.Timer: - root[name] = map[string]interface{}{ - "AvgRate01Min": metric.Rate1(), - "AvgRate05Min": metric.Rate5(), - "AvgRate15Min": metric.Rate15(), - "MeanRate": metric.RateMean(), - "Overall": float64(metric.Count()), - "Percentiles": map[string]interface{}{ - "5": metric.Percentile(0.05), - "20": metric.Percentile(0.2), - "50": metric.Percentile(0.5), - "80": metric.Percentile(0.8), - "95": metric.Percentile(0.95), - }, - } - - default: - root[name] = "Unknown metric type" - } - } else { - switch metric := metric.(type) { - case metrics.Meter: - root[name] = map[string]interface{}{ - "Avg01Min": format(metric.Rate1()*60, metric.Rate1()), - "Avg05Min": format(metric.Rate5()*300, metric.Rate5()), - "Avg15Min": format(metric.Rate15()*900, metric.Rate15()), - "Overall": format(float64(metric.Count()), metric.RateMean()), - } - - case metrics.Timer: - root[name] = map[string]interface{}{ - "Avg01Min": format(metric.Rate1()*60, metric.Rate1()), - "Avg05Min": format(metric.Rate5()*300, metric.Rate5()), - "Avg15Min": format(metric.Rate15()*900, metric.Rate15()), - "Overall": format(float64(metric.Count()), metric.RateMean()), - "Maximum": time.Duration(metric.Max()).String(), - "Minimum": time.Duration(metric.Min()).String(), - "Percentiles": map[string]interface{}{ - "5": time.Duration(metric.Percentile(0.05)).String(), - "20": time.Duration(metric.Percentile(0.2)).String(), - "50": time.Duration(metric.Percentile(0.5)).String(), - "80": time.Duration(metric.Percentile(0.8)).String(), - "95": time.Duration(metric.Percentile(0.95)).String(), - }, - } - - default: - root[name] = "Unknown metric type" - } - } - }) - return counters, nil -} diff --git a/rpc/api/debug_args.go b/rpc/api/debug_args.go deleted file mode 100644 index 041ad6b6a..000000000 --- a/rpc/api/debug_args.go +++ /dev/null @@ -1,87 +0,0 @@ -// Copyright 2015 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library 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 Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see . - -package api - -import ( - "encoding/json" - "fmt" - "math/big" - "reflect" - - "github.com/ethereum/go-ethereum/rpc/shared" -) - -type WaitForBlockArgs struct { - MinHeight int - Timeout int // in seconds -} - -func (args *WaitForBlockArgs) UnmarshalJSON(b []byte) (err error) { - var obj []interface{} - if err := json.Unmarshal(b, &obj); err != nil { - return shared.NewDecodeParamError(err.Error()) - } - - if len(obj) > 2 { - return fmt.Errorf("waitForArgs needs 0, 1, 2 arguments") - } - - // default values when not provided - args.MinHeight = -1 - args.Timeout = -1 - - if len(obj) >= 1 { - var minHeight *big.Int - if minHeight, err = numString(obj[0]); err != nil { - return err - } - args.MinHeight = int(minHeight.Int64()) - } - - if len(obj) >= 2 { - timeout, err := numString(obj[1]) - if err != nil { - return err - } - args.Timeout = int(timeout.Int64()) - } - - return nil -} - -type MetricsArgs struct { - Raw bool -} - -func (args *MetricsArgs) 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 fmt.Errorf("metricsArgs needs 0, 1 arguments") - } - // default values when not provided - if len(obj) >= 1 && obj[0] != nil { - if value, ok := obj[0].(bool); !ok { - return fmt.Errorf("invalid argument %v", reflect.TypeOf(obj[0])) - } else { - args.Raw = value - } - } - return nil -} diff --git a/rpc/api/debug_js.go b/rpc/api/debug_js.go deleted file mode 100644 index 030511add..000000000 --- a/rpc/api/debug_js.go +++ /dev/null @@ -1,83 +0,0 @@ -// Copyright 2015 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library 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 Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see . - -package api - -const Debug_JS = ` -web3._extend({ - property: 'debug', - methods: - [ - new web3._extend.Method({ - name: 'printBlock', - call: 'debug_printBlock', - params: 1, - inputFormatter: [null] - }), - new web3._extend.Method({ - name: 'getBlockRlp', - call: 'debug_getBlockRlp', - params: 1, - inputFormatter: [null] - }), - new web3._extend.Method({ - name: 'setHead', - call: 'debug_setHead', - params: 1, - inputFormatter: [null] - }), - new web3._extend.Method({ - name: 'processBlock', - call: 'debug_processBlock', - params: 1, - inputFormatter: [null] - }), - new web3._extend.Method({ - name: 'seedHash', - call: 'debug_seedHash', - params: 1, - inputFormatter: [null] - }), - new web3._extend.Method({ - name: 'dumpBlock', - call: 'debug_dumpBlock', - params: 1, - inputFormatter: [null] - }), - new web3._extend.Method({ - name: 'metrics', - call: 'debug_metrics', - params: 1, - inputFormatter: [null] - }), - new web3._extend.Method({ - name: 'verbosity', - call: 'debug_verbosity', - params: 1, - inputFormatter: [web3._extend.utils.fromDecimal] - }), - new web3._extend.Method({ - name: 'vmodule', - call: 'debug_vmodule', - params: 1, - inputFormatter: [null] - }), - ], - properties: - [ - ] -}); -` diff --git a/rpc/api/eth.go b/rpc/api/eth.go deleted file mode 100644 index db7a643d8..000000000 --- a/rpc/api/eth.go +++ /dev/null @@ -1,721 +0,0 @@ -// Copyright 2015 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library 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 Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see . - -package api - -import ( - "bytes" - "encoding/json" - "math/big" - - "fmt" - - "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/common/natspec" - "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" - "gopkg.in/fatih/set.v0" -) - -const ( - EthApiVersion = "1.0" -) - -// eth api provider -// See https://github.com/ethereum/wiki/wiki/JSON-RPC -type ethApi struct { - xeth *xeth.XEth - ethereum *eth.Ethereum - methods map[string]ethhandler - codec codec.ApiCoder -} - -// eth callback handler -type ethhandler func(*ethApi, *shared.Request) (interface{}, error) - -var ( - ethMapping = map[string]ethhandler{ - "eth_accounts": (*ethApi).Accounts, - "eth_blockNumber": (*ethApi).BlockNumber, - "eth_getBalance": (*ethApi).GetBalance, - "eth_protocolVersion": (*ethApi).ProtocolVersion, - "eth_coinbase": (*ethApi).Coinbase, - "eth_mining": (*ethApi).IsMining, - "eth_syncing": (*ethApi).IsSyncing, - "eth_gasPrice": (*ethApi).GasPrice, - "eth_getStorage": (*ethApi).GetStorage, - "eth_storageAt": (*ethApi).GetStorage, - "eth_getStorageAt": (*ethApi).GetStorageAt, - "eth_getTransactionCount": (*ethApi).GetTransactionCount, - "eth_getBlockTransactionCountByHash": (*ethApi).GetBlockTransactionCountByHash, - "eth_getBlockTransactionCountByNumber": (*ethApi).GetBlockTransactionCountByNumber, - "eth_getUncleCountByBlockHash": (*ethApi).GetUncleCountByBlockHash, - "eth_getUncleCountByBlockNumber": (*ethApi).GetUncleCountByBlockNumber, - "eth_getData": (*ethApi).GetData, - "eth_getCode": (*ethApi).GetData, - "eth_getNatSpec": (*ethApi).GetNatSpec, - "eth_sign": (*ethApi).Sign, - "eth_sendRawTransaction": (*ethApi).SubmitTransaction, - "eth_submitTransaction": (*ethApi).SubmitTransaction, - "eth_sendTransaction": (*ethApi).SendTransaction, - "eth_signTransaction": (*ethApi).SignTransaction, - "eth_transact": (*ethApi).SendTransaction, - "eth_estimateGas": (*ethApi).EstimateGas, - "eth_call": (*ethApi).Call, - "eth_flush": (*ethApi).Flush, - "eth_getBlockByHash": (*ethApi).GetBlockByHash, - "eth_getBlockByNumber": (*ethApi).GetBlockByNumber, - "eth_getTransactionByHash": (*ethApi).GetTransactionByHash, - "eth_getTransactionByBlockNumberAndIndex": (*ethApi).GetTransactionByBlockNumberAndIndex, - "eth_getTransactionByBlockHashAndIndex": (*ethApi).GetTransactionByBlockHashAndIndex, - "eth_getUncleByBlockHashAndIndex": (*ethApi).GetUncleByBlockHashAndIndex, - "eth_getUncleByBlockNumberAndIndex": (*ethApi).GetUncleByBlockNumberAndIndex, - "eth_getCompilers": (*ethApi).GetCompilers, - "eth_compileSolidity": (*ethApi).CompileSolidity, - "eth_newFilter": (*ethApi).NewFilter, - "eth_newBlockFilter": (*ethApi).NewBlockFilter, - "eth_newPendingTransactionFilter": (*ethApi).NewPendingTransactionFilter, - "eth_uninstallFilter": (*ethApi).UninstallFilter, - "eth_getFilterChanges": (*ethApi).GetFilterChanges, - "eth_getFilterLogs": (*ethApi).GetFilterLogs, - "eth_getLogs": (*ethApi).GetLogs, - "eth_hashrate": (*ethApi).Hashrate, - "eth_getWork": (*ethApi).GetWork, - "eth_submitWork": (*ethApi).SubmitWork, - "eth_submitHashrate": (*ethApi).SubmitHashrate, - "eth_resend": (*ethApi).Resend, - "eth_pendingTransactions": (*ethApi).PendingTransactions, - "eth_getTransactionReceipt": (*ethApi).GetTransactionReceipt, - } -) - -// create new ethApi instance -func NewEthApi(xeth *xeth.XEth, eth *eth.Ethereum, codec codec.Codec) *ethApi { - return ðApi{xeth, eth, ethMapping, codec.New(nil)} -} - -// collection with supported methods -func (self *ethApi) Methods() []string { - methods := make([]string, len(self.methods)) - i := 0 - for k := range self.methods { - methods[i] = k - i++ - } - return methods -} - -// Execute given request -func (self *ethApi) Execute(req *shared.Request) (interface{}, error) { - if callback, ok := self.methods[req.Method]; ok { - return callback(self, req) - } - - return nil, shared.NewNotImplementedError(req.Method) -} - -func (self *ethApi) Name() string { - return shared.EthApiName -} - -func (self *ethApi) ApiVersion() string { - return EthApiVersion -} - -func (self *ethApi) Accounts(req *shared.Request) (interface{}, error) { - return self.xeth.Accounts(), nil -} - -func (self *ethApi) Hashrate(req *shared.Request) (interface{}, error) { - return newHexNum(self.xeth.HashRate()), nil -} - -func (self *ethApi) BlockNumber(req *shared.Request) (interface{}, error) { - num := self.xeth.CurrentBlock().Number() - return newHexNum(num.Bytes()), nil -} - -func (self *ethApi) GetBalance(req *shared.Request) (interface{}, error) { - args := new(GetBalanceArgs) - if err := self.codec.Decode(req.Params, &args); err != nil { - return nil, shared.NewDecodeParamError(err.Error()) - } - - return self.xeth.AtStateNum(args.BlockNumber).BalanceAt(args.Address), nil -} - -func (self *ethApi) ProtocolVersion(req *shared.Request) (interface{}, error) { - return self.xeth.EthVersion(), nil -} - -func (self *ethApi) Coinbase(req *shared.Request) (interface{}, error) { - return newHexData(self.xeth.Coinbase()), nil -} - -func (self *ethApi) IsMining(req *shared.Request) (interface{}, error) { - return self.xeth.IsMining(), nil -} - -func (self *ethApi) IsSyncing(req *shared.Request) (interface{}, error) { - origin, current, height := self.ethereum.Downloader().Progress() - if current < height { - return map[string]interface{}{ - "startingBlock": newHexNum(big.NewInt(int64(origin)).Bytes()), - "currentBlock": newHexNum(big.NewInt(int64(current)).Bytes()), - "highestBlock": newHexNum(big.NewInt(int64(height)).Bytes()), - }, nil - } - return false, nil -} - -func (self *ethApi) GasPrice(req *shared.Request) (interface{}, error) { - return newHexNum(self.xeth.DefaultGasPrice().Bytes()), nil -} - -func (self *ethApi) GetStorage(req *shared.Request) (interface{}, error) { - args := new(GetStorageArgs) - if err := self.codec.Decode(req.Params, &args); err != nil { - return nil, shared.NewDecodeParamError(err.Error()) - } - - return self.xeth.AtStateNum(args.BlockNumber).State().SafeGet(args.Address).Storage(), nil -} - -func (self *ethApi) GetStorageAt(req *shared.Request) (interface{}, error) { - args := new(GetStorageAtArgs) - if err := self.codec.Decode(req.Params, &args); err != nil { - return nil, shared.NewDecodeParamError(err.Error()) - } - - return self.xeth.AtStateNum(args.BlockNumber).StorageAt(args.Address, args.Key), nil -} - -func (self *ethApi) GetTransactionCount(req *shared.Request) (interface{}, error) { - args := new(GetTxCountArgs) - if err := self.codec.Decode(req.Params, &args); err != nil { - return nil, shared.NewDecodeParamError(err.Error()) - } - - count := self.xeth.AtStateNum(args.BlockNumber).TxCountAt(args.Address) - return fmt.Sprintf("%#x", count), nil -} - -func (self *ethApi) GetBlockTransactionCountByHash(req *shared.Request) (interface{}, error) { - args := new(HashArgs) - if err := self.codec.Decode(req.Params, &args); err != nil { - return nil, shared.NewDecodeParamError(err.Error()) - } - block := self.xeth.EthBlockByHash(args.Hash) - if block == nil { - return nil, nil - } - return fmt.Sprintf("%#x", len(block.Transactions())), nil -} - -func (self *ethApi) GetBlockTransactionCountByNumber(req *shared.Request) (interface{}, error) { - args := new(BlockNumArg) - if err := self.codec.Decode(req.Params, &args); err != nil { - return nil, shared.NewDecodeParamError(err.Error()) - } - - block := self.xeth.EthBlockByNumber(args.BlockNumber) - if block == nil { - return nil, nil - } - return fmt.Sprintf("%#x", len(block.Transactions())), nil -} - -func (self *ethApi) GetUncleCountByBlockHash(req *shared.Request) (interface{}, error) { - args := new(HashArgs) - if err := self.codec.Decode(req.Params, &args); err != nil { - return nil, shared.NewDecodeParamError(err.Error()) - } - - block := self.xeth.EthBlockByHash(args.Hash) - if block == nil { - return nil, nil - } - return fmt.Sprintf("%#x", len(block.Uncles())), nil -} - -func (self *ethApi) GetUncleCountByBlockNumber(req *shared.Request) (interface{}, error) { - args := new(BlockNumArg) - if err := self.codec.Decode(req.Params, &args); err != nil { - return nil, shared.NewDecodeParamError(err.Error()) - } - - block := self.xeth.EthBlockByNumber(args.BlockNumber) - if block == nil { - return nil, nil - } - return fmt.Sprintf("%#x", len(block.Uncles())), nil -} - -func (self *ethApi) GetData(req *shared.Request) (interface{}, error) { - args := new(GetDataArgs) - if err := self.codec.Decode(req.Params, &args); err != nil { - return nil, shared.NewDecodeParamError(err.Error()) - } - v := self.xeth.AtStateNum(args.BlockNumber).CodeAtBytes(args.Address) - return newHexData(v), nil -} - -func (self *ethApi) Sign(req *shared.Request) (interface{}, error) { - args := new(NewSigArgs) - if err := self.codec.Decode(req.Params, &args); err != nil { - return nil, shared.NewDecodeParamError(err.Error()) - } - v, err := self.xeth.Sign(args.From, args.Data, false) - if err != nil { - return nil, err - } - return v, nil -} - -func (self *ethApi) SubmitTransaction(req *shared.Request) (interface{}, error) { - args := new(NewDataArgs) - if err := self.codec.Decode(req.Params, &args); err != nil { - return nil, shared.NewDecodeParamError(err.Error()) - } - - v, err := self.xeth.PushTx(args.Data) - if err != nil { - return nil, err - } - return v, nil -} - -// JsonTransaction is returned as response by the JSON RPC. It contains the -// signed RLP encoded transaction as Raw and the signed transaction object as Tx. -type JsonTransaction struct { - Raw string `json:"raw"` - Tx *tx `json:"tx"` -} - -func (self *ethApi) SignTransaction(req *shared.Request) (interface{}, error) { - args := new(NewTxArgs) - if err := self.codec.Decode(req.Params, &args); err != nil { - return nil, shared.NewDecodeParamError(err.Error()) - } - - // nonce may be nil ("guess" mode) - var nonce string - if args.Nonce != nil { - nonce = args.Nonce.String() - } - - var gas, price string - if args.Gas != nil { - gas = args.Gas.String() - } - if args.GasPrice != nil { - price = args.GasPrice.String() - } - tx, err := self.xeth.SignTransaction(args.From, args.To, nonce, args.Value.String(), gas, price, args.Data) - if err != nil { - return nil, err - } - - data, err := rlp.EncodeToBytes(tx) - if err != nil { - return nil, err - } - - return JsonTransaction{"0x" + common.Bytes2Hex(data), newTx(tx)}, nil -} - -func (self *ethApi) SendTransaction(req *shared.Request) (interface{}, error) { - args := new(NewTxArgs) - if err := self.codec.Decode(req.Params, &args); err != nil { - return nil, shared.NewDecodeParamError(err.Error()) - } - - // nonce may be nil ("guess" mode) - var nonce string - if args.Nonce != nil { - nonce = args.Nonce.String() - } - - var gas, price string - if args.Gas != nil { - gas = args.Gas.String() - } - if args.GasPrice != nil { - price = args.GasPrice.String() - } - v, err := self.xeth.Transact(args.From, args.To, nonce, args.Value.String(), gas, price, args.Data) - if err != nil { - return nil, err - } - return v, nil -} - -func (self *ethApi) GetNatSpec(req *shared.Request) (interface{}, error) { - args := new(NewTxArgs) - if err := self.codec.Decode(req.Params, &args); err != nil { - return nil, shared.NewDecodeParamError(err.Error()) - } - - var jsontx = fmt.Sprintf(`{"params":[{"to":"%s","data": "%s"}]}`, args.To, args.Data) - notice := natspec.GetNotice(self.xeth, jsontx, self.ethereum.HTTPClient()) - - return notice, nil -} - -func (self *ethApi) EstimateGas(req *shared.Request) (interface{}, error) { - _, gas, err := self.doCall(req.Params) - if err != nil { - return nil, err - } - - // TODO unwrap the parent method's ToHex call - if len(gas) == 0 { - return newHexNum(0), nil - } else { - return newHexNum(common.String2Big(gas)), err - } -} - -func (self *ethApi) Call(req *shared.Request) (interface{}, error) { - v, _, err := self.doCall(req.Params) - if err != nil { - return nil, err - } - - // TODO unwrap the parent method's ToHex call - if v == "0x0" { - return newHexData([]byte{}), nil - } else { - return newHexData(common.FromHex(v)), nil - } -} - -func (self *ethApi) Flush(req *shared.Request) (interface{}, error) { - return nil, shared.NewNotImplementedError(req.Method) -} - -func (self *ethApi) doCall(params json.RawMessage) (string, string, error) { - args := new(CallArgs) - if err := self.codec.Decode(params, &args); err != nil { - return "", "", err - } - - return self.xeth.AtStateNum(args.BlockNumber).Call(args.From, args.To, args.Value.String(), args.Gas.String(), args.GasPrice.String(), args.Data) -} - -func (self *ethApi) GetBlockByHash(req *shared.Request) (interface{}, error) { - args := new(GetBlockByHashArgs) - if err := self.codec.Decode(req.Params, &args); err != nil { - return nil, shared.NewDecodeParamError(err.Error()) - } - block := self.xeth.EthBlockByHash(args.BlockHash) - if block == nil { - return nil, nil - } - return NewBlockRes(block, self.xeth.Td(block.Hash()), args.IncludeTxs), nil -} - -func (self *ethApi) GetBlockByNumber(req *shared.Request) (interface{}, error) { - args := new(GetBlockByNumberArgs) - if err := json.Unmarshal(req.Params, &args); err != nil { - return nil, shared.NewDecodeParamError(err.Error()) - } - - block := self.xeth.EthBlockByNumber(args.BlockNumber) - if block == nil { - return nil, nil - } - return NewBlockRes(block, self.xeth.Td(block.Hash()), args.IncludeTxs), nil -} - -func (self *ethApi) GetTransactionByHash(req *shared.Request) (interface{}, error) { - args := new(HashArgs) - if err := self.codec.Decode(req.Params, &args); err != nil { - return nil, shared.NewDecodeParamError(err.Error()) - } - - tx, bhash, bnum, txi := self.xeth.EthTransactionByHash(args.Hash) - if tx != nil { - v := NewTransactionRes(tx) - // if the blockhash is 0, assume this is a pending transaction - if bytes.Compare(bhash.Bytes(), bytes.Repeat([]byte{0}, 32)) != 0 { - v.BlockHash = newHexData(bhash) - v.BlockNumber = newHexNum(bnum) - v.TxIndex = newHexNum(txi) - } - return v, nil - } - return nil, nil -} - -func (self *ethApi) GetTransactionByBlockHashAndIndex(req *shared.Request) (interface{}, error) { - args := new(HashIndexArgs) - if err := self.codec.Decode(req.Params, &args); err != nil { - return nil, shared.NewDecodeParamError(err.Error()) - } - - raw := self.xeth.EthBlockByHash(args.Hash) - if raw == nil { - return nil, nil - } - block := NewBlockRes(raw, self.xeth.Td(raw.Hash()), true) - if args.Index >= int64(len(block.Transactions)) || args.Index < 0 { - return nil, nil - } else { - return block.Transactions[args.Index], nil - } -} - -func (self *ethApi) GetTransactionByBlockNumberAndIndex(req *shared.Request) (interface{}, error) { - args := new(BlockNumIndexArgs) - if err := self.codec.Decode(req.Params, &args); err != nil { - return nil, shared.NewDecodeParamError(err.Error()) - } - - raw := self.xeth.EthBlockByNumber(args.BlockNumber) - if raw == nil { - return nil, nil - } - block := NewBlockRes(raw, self.xeth.Td(raw.Hash()), true) - if args.Index >= int64(len(block.Transactions)) || args.Index < 0 { - // return NewValidationError("Index", "does not exist") - return nil, nil - } - return block.Transactions[args.Index], nil -} - -func (self *ethApi) GetUncleByBlockHashAndIndex(req *shared.Request) (interface{}, error) { - args := new(HashIndexArgs) - if err := self.codec.Decode(req.Params, &args); err != nil { - return nil, shared.NewDecodeParamError(err.Error()) - } - - raw := self.xeth.EthBlockByHash(args.Hash) - if raw == nil { - return nil, nil - } - block := NewBlockRes(raw, self.xeth.Td(raw.Hash()), false) - if args.Index >= int64(len(block.Uncles)) || args.Index < 0 { - // return NewValidationError("Index", "does not exist") - return nil, nil - } - return block.Uncles[args.Index], nil -} - -func (self *ethApi) GetUncleByBlockNumberAndIndex(req *shared.Request) (interface{}, error) { - args := new(BlockNumIndexArgs) - if err := self.codec.Decode(req.Params, &args); err != nil { - return nil, shared.NewDecodeParamError(err.Error()) - } - - raw := self.xeth.EthBlockByNumber(args.BlockNumber) - if raw == nil { - return nil, nil - } - block := NewBlockRes(raw, self.xeth.Td(raw.Hash()), true) - if args.Index >= int64(len(block.Uncles)) || args.Index < 0 { - return nil, nil - } else { - return block.Uncles[args.Index], nil - } -} - -func (self *ethApi) GetCompilers(req *shared.Request) (interface{}, error) { - var lang string - if solc, _ := self.xeth.Solc(); solc != nil { - lang = "Solidity" - } - c := []string{lang} - return c, nil -} - -func (self *ethApi) CompileSolidity(req *shared.Request) (interface{}, error) { - solc, _ := self.xeth.Solc() - if solc == nil { - return nil, shared.NewNotAvailableError(req.Method, "solc (solidity compiler) not found") - } - - args := new(SourceArgs) - if err := self.codec.Decode(req.Params, &args); err != nil { - return nil, shared.NewDecodeParamError(err.Error()) - } - - contracts, err := solc.Compile(args.Source) - if err != nil { - return nil, err - } - return contracts, nil -} - -func (self *ethApi) NewFilter(req *shared.Request) (interface{}, error) { - args := new(BlockFilterArgs) - if err := self.codec.Decode(req.Params, &args); err != nil { - return nil, shared.NewDecodeParamError(err.Error()) - } - - id := self.xeth.NewLogFilter(args.Earliest, args.Latest, args.Skip, args.Max, args.Address, args.Topics) - return newHexNum(big.NewInt(int64(id)).Bytes()), nil -} - -func (self *ethApi) NewBlockFilter(req *shared.Request) (interface{}, error) { - return newHexNum(self.xeth.NewBlockFilter()), nil -} - -func (self *ethApi) NewPendingTransactionFilter(req *shared.Request) (interface{}, error) { - return newHexNum(self.xeth.NewTransactionFilter()), nil -} - -func (self *ethApi) UninstallFilter(req *shared.Request) (interface{}, error) { - args := new(FilterIdArgs) - if err := self.codec.Decode(req.Params, &args); err != nil { - return nil, shared.NewDecodeParamError(err.Error()) - } - return self.xeth.UninstallFilter(args.Id), nil -} - -func (self *ethApi) GetFilterChanges(req *shared.Request) (interface{}, error) { - args := new(FilterIdArgs) - if err := self.codec.Decode(req.Params, &args); err != nil { - return nil, shared.NewDecodeParamError(err.Error()) - } - - switch self.xeth.GetFilterType(args.Id) { - case xeth.BlockFilterTy: - return NewHashesRes(self.xeth.BlockFilterChanged(args.Id)), nil - case xeth.TransactionFilterTy: - return NewHashesRes(self.xeth.TransactionFilterChanged(args.Id)), nil - case xeth.LogFilterTy: - return NewLogsRes(self.xeth.LogFilterChanged(args.Id)), nil - default: - return []string{}, nil // reply empty string slice - } -} - -func (self *ethApi) GetFilterLogs(req *shared.Request) (interface{}, error) { - args := new(FilterIdArgs) - if err := self.codec.Decode(req.Params, &args); err != nil { - return nil, shared.NewDecodeParamError(err.Error()) - } - - return NewLogsRes(self.xeth.Logs(args.Id)), nil -} - -func (self *ethApi) GetLogs(req *shared.Request) (interface{}, error) { - args := new(BlockFilterArgs) - if err := self.codec.Decode(req.Params, &args); err != nil { - return nil, shared.NewDecodeParamError(err.Error()) - } - return NewLogsRes(self.xeth.AllLogs(args.Earliest, args.Latest, args.Skip, args.Max, args.Address, args.Topics)), nil -} - -func (self *ethApi) GetWork(req *shared.Request) (interface{}, error) { - self.xeth.SetMining(true, 0) - ret, err := self.xeth.RemoteMining().GetWork() - if err != nil { - return nil, shared.NewNotReadyError("mining work") - } else { - return ret, nil - } -} - -func (self *ethApi) SubmitWork(req *shared.Request) (interface{}, error) { - args := new(SubmitWorkArgs) - if err := self.codec.Decode(req.Params, &args); err != nil { - return nil, shared.NewDecodeParamError(err.Error()) - } - return self.xeth.RemoteMining().SubmitWork(args.Nonce, common.HexToHash(args.Digest), common.HexToHash(args.Header)), nil -} - -func (self *ethApi) SubmitHashrate(req *shared.Request) (interface{}, error) { - args := new(SubmitHashRateArgs) - if err := self.codec.Decode(req.Params, &args); err != nil { - return false, shared.NewDecodeParamError(err.Error()) - } - self.xeth.RemoteMining().SubmitHashrate(common.HexToHash(args.Id), args.Rate) - return true, 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()) - } - - from := common.HexToAddress(args.Tx.From) - - pending := self.ethereum.TxPool().GetTransactions() - for _, p := range pending { - if pFrom, err := p.From(); err == nil && pFrom == from && p.SigHash() == args.Tx.tx.SigHash() { - self.ethereum.TxPool().RemoveTx(common.HexToHash(args.Tx.Hash)) - return self.xeth.Transact(args.Tx.From, args.Tx.To, args.Tx.Nonce, args.Tx.Value, args.GasLimit, args.GasPrice, args.Tx.Data) - } - } - - return nil, fmt.Errorf("Transaction %s not found", args.Tx.Hash) -} - -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 determining 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 -} - -func (self *ethApi) GetTransactionReceipt(req *shared.Request) (interface{}, error) { - args := new(HashArgs) - if err := self.codec.Decode(req.Params, &args); err != nil { - return nil, shared.NewDecodeParamError(err.Error()) - } - - txhash := common.BytesToHash(common.FromHex(args.Hash)) - tx, bhash, bnum, txi := self.xeth.EthTransactionByHash(args.Hash) - rec := self.xeth.GetTxReceipt(txhash) - // We could have an error of "not found". Should disambiguate - // if err != nil { - // return err, nil - // } - if rec != nil && tx != nil { - v := NewReceiptRes(rec) - v.BlockHash = newHexData(bhash) - v.BlockNumber = newHexNum(bnum) - v.TransactionIndex = newHexNum(txi) - return v, nil - } - - return nil, nil -} diff --git a/rpc/api/eth_args.go b/rpc/api/eth_args.go deleted file mode 100644 index ed3d761f1..000000000 --- a/rpc/api/eth_args.go +++ /dev/null @@ -1,1104 +0,0 @@ -// Copyright 2015 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library 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 Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see . - -package api - -import ( - "encoding/json" - "fmt" - "math/big" - "strconv" - "strings" - - "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/core/types" - "github.com/ethereum/go-ethereum/core/vm" - "github.com/ethereum/go-ethereum/rpc/shared" -) - -const ( - defaultLogLimit = 100 - defaultLogOffset = 0 -) - -type GetBalanceArgs struct { - Address string - BlockNumber int64 -} - -func (args *GetBalanceArgs) 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) - } - - addstr, ok := obj[0].(string) - if !ok { - return shared.NewInvalidTypeError("address", "not a string") - } - args.Address = addstr - - if len(obj) > 1 { - if err := blockHeight(obj[1], &args.BlockNumber); err != nil { - return err - } - } else { - args.BlockNumber = -1 - } - - return nil -} - -type GetStorageArgs struct { - Address string - BlockNumber int64 -} - -func (args *GetStorageArgs) 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) - } - - addstr, ok := obj[0].(string) - if !ok { - return shared.NewInvalidTypeError("address", "not a string") - } - args.Address = addstr - - if len(obj) > 1 { - if err := blockHeight(obj[1], &args.BlockNumber); err != nil { - return err - } - } else { - args.BlockNumber = -1 - } - - return nil -} - -type GetStorageAtArgs struct { - Address string - BlockNumber int64 - Key string -} - -func (args *GetStorageAtArgs) UnmarshalJSON(b []byte) (err error) { - var obj []interface{} - if err := json.Unmarshal(b, &obj); err != nil { - return shared.NewDecodeParamError(err.Error()) - } - - if len(obj) < 2 { - return shared.NewInsufficientParamsError(len(obj), 2) - } - - addstr, ok := obj[0].(string) - if !ok { - return shared.NewInvalidTypeError("address", "not a string") - } - args.Address = addstr - - keystr, ok := obj[1].(string) - if !ok { - return shared.NewInvalidTypeError("key", "not a string") - } - args.Key = keystr - - if len(obj) > 2 { - if err := blockHeight(obj[2], &args.BlockNumber); err != nil { - return err - } - } else { - args.BlockNumber = -1 - } - - return nil -} - -type GetTxCountArgs struct { - Address string - BlockNumber int64 -} - -func (args *GetTxCountArgs) 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) - } - - addstr, ok := obj[0].(string) - if !ok { - return shared.NewInvalidTypeError("address", "not a string") - } - args.Address = addstr - - if len(obj) > 1 { - if err := blockHeight(obj[1], &args.BlockNumber); err != nil { - return err - } - } else { - args.BlockNumber = -1 - } - - return nil -} - -type SubmitHashRateArgs struct { - Id string - Rate uint64 -} - -func (args *SubmitHashRateArgs) UnmarshalJSON(b []byte) (err error) { - var obj []interface{} - if err := json.Unmarshal(b, &obj); err != nil { - return shared.NewDecodeParamError(err.Error()) - } - - if len(obj) < 2 { - return shared.NewInsufficientParamsError(len(obj), 2) - } - - arg0, ok := obj[0].(string) - if !ok { - return shared.NewInvalidTypeError("hash", "not a string") - } - args.Id = arg0 - - arg1, ok := obj[1].(string) - if !ok { - return shared.NewInvalidTypeError("rate", "not a string") - } - - args.Rate = common.String2Big(arg1).Uint64() - - 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 shared.NewDecodeParamError(err.Error()) - } - - if len(obj) < 1 { - return shared.NewInsufficientParamsError(len(obj), 1) - } - - arg0, ok := obj[0].(string) - if !ok { - return shared.NewInvalidTypeError("hash", "not a string") - } - args.Hash = arg0 - - 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 shared.NewDecodeParamError(err.Error()) - } - - if len(obj) < 1 { - return shared.NewInsufficientParamsError(len(obj), 1) - } - - if err := blockHeight(obj[0], &args.BlockNumber); err != nil { - return err - } - - return nil -} - -type GetDataArgs struct { - Address string - BlockNumber int64 -} - -func (args *GetDataArgs) 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) - } - - addstr, ok := obj[0].(string) - if !ok { - return shared.NewInvalidTypeError("address", "not a string") - } - args.Address = addstr - - if len(obj) > 1 { - if err := blockHeight(obj[1], &args.BlockNumber); err != nil { - return err - } - } else { - args.BlockNumber = -1 - } - - return nil -} - -type NewDataArgs struct { - Data string -} - -func (args *NewDataArgs) UnmarshalJSON(b []byte) (err error) { - var obj []interface{} - - if err := json.Unmarshal(b, &obj); err != nil { - return shared.NewDecodeParamError(err.Error()) - } - - // Check for sufficient params - if len(obj) < 1 { - return shared.NewInsufficientParamsError(len(obj), 1) - } - - data, ok := obj[0].(string) - if !ok { - return shared.NewInvalidTypeError("data", "not a string") - } - args.Data = data - - if len(args.Data) == 0 { - return shared.NewValidationError("data", "is required") - } - - return nil -} - -type NewSigArgs struct { - From string - Data string -} - -func (args *NewSigArgs) UnmarshalJSON(b []byte) (err error) { - var obj []interface{} - - if err := json.Unmarshal(b, &obj); err != nil { - return shared.NewDecodeParamError(err.Error()) - } - - // Check for sufficient params - if len(obj) < 1 { - return shared.NewInsufficientParamsError(len(obj), 1) - } - - from, ok := obj[0].(string) - if !ok { - return shared.NewInvalidTypeError("from", "not a string") - } - args.From = from - - if len(args.From) == 0 { - return shared.NewValidationError("from", "is required") - } - - data, ok := obj[1].(string) - if !ok { - return shared.NewInvalidTypeError("data", "not a string") - } - args.Data = data - - if len(args.Data) == 0 { - return shared.NewValidationError("data", "is required") - } - - return nil -} - -type NewTxArgs struct { - From string - To string - Nonce *big.Int - Value *big.Int - Gas *big.Int - GasPrice *big.Int - Data string - - BlockNumber int64 -} - -func (args *NewTxArgs) UnmarshalJSON(b []byte) (err error) { - var obj []json.RawMessage - var ext struct { - From string - To string - Nonce interface{} - Value interface{} - Gas interface{} - GasPrice interface{} - Data string - } - - // Decode byte slice to array of RawMessages - if err := json.Unmarshal(b, &obj); err != nil { - return shared.NewDecodeParamError(err.Error()) - } - - // Check for sufficient params - if len(obj) < 1 { - return shared.NewInsufficientParamsError(len(obj), 1) - } - - // Decode 0th RawMessage to temporary struct - if err := json.Unmarshal(obj[0], &ext); err != nil { - return shared.NewDecodeParamError(err.Error()) - } - - if len(ext.From) == 0 { - return shared.NewValidationError("from", "is required") - } - - args.From = ext.From - args.To = ext.To - args.Data = ext.Data - - var num *big.Int - if ext.Nonce != nil { - num, err = numString(ext.Nonce) - if err != nil { - return err - } - } - args.Nonce = num - - if ext.Value == nil { - num = big.NewInt(0) - } else { - num, err = numString(ext.Value) - if err != nil { - return err - } - } - args.Value = num - - num = nil - if ext.Gas != nil { - if num, err = numString(ext.Gas); err != nil { - return err - } - } - args.Gas = num - - num = nil - if ext.GasPrice != nil { - if num, err = numString(ext.GasPrice); err != nil { - return err - } - } - args.GasPrice = num - - // Check for optional BlockNumber param - if len(obj) > 1 { - if err := blockHeightFromJson(obj[1], &args.BlockNumber); err != nil { - return err - } - } else { - args.BlockNumber = -1 - } - - return nil -} - -type SourceArgs struct { - Source string -} - -func (args *SourceArgs) 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) - } - - arg0, ok := obj[0].(string) - if !ok { - return shared.NewInvalidTypeError("source code", "not a string") - } - args.Source = arg0 - - return nil -} - -type CallArgs struct { - From string - To string - Value *big.Int - Gas *big.Int - GasPrice *big.Int - Data string - - BlockNumber int64 -} - -func (args *CallArgs) UnmarshalJSON(b []byte) (err error) { - var obj []json.RawMessage - var ext struct { - From string - To string - Value interface{} - Gas interface{} - GasPrice interface{} - Data string - } - - // Decode byte slice to array of RawMessages - if err := json.Unmarshal(b, &obj); err != nil { - return shared.NewDecodeParamError(err.Error()) - } - - // Check for sufficient params - if len(obj) < 1 { - return shared.NewInsufficientParamsError(len(obj), 1) - } - - // Decode 0th RawMessage to temporary struct - if err := json.Unmarshal(obj[0], &ext); err != nil { - return shared.NewDecodeParamError(err.Error()) - } - - args.From = ext.From - args.To = ext.To - - var num *big.Int - if ext.Value == nil { - num = big.NewInt(0) - } else { - if num, err = numString(ext.Value); err != nil { - return err - } - } - args.Value = num - - if ext.Gas != nil { - if num, err = numString(ext.Gas); err != nil { - return err - } - } else { - num = nil - } - args.Gas = num - - if ext.GasPrice != nil { - if num, err = numString(ext.GasPrice); err != nil { - return err - } - } else { - num = nil - } - args.GasPrice = num - - args.Data = ext.Data - - // Check for optional BlockNumber param - if len(obj) > 1 { - if err := blockHeightFromJson(obj[1], &args.BlockNumber); err != nil { - return err - } - } else { - args.BlockNumber = -1 - } - - return nil -} - -type HashIndexArgs struct { - Hash string - Index int64 -} - -func (args *HashIndexArgs) UnmarshalJSON(b []byte) (err error) { - var obj []interface{} - if err := json.Unmarshal(b, &obj); err != nil { - return shared.NewDecodeParamError(err.Error()) - } - - if len(obj) < 2 { - return shared.NewInsufficientParamsError(len(obj), 2) - } - - arg0, ok := obj[0].(string) - if !ok { - return shared.NewInvalidTypeError("hash", "not a string") - } - args.Hash = arg0 - - arg1, ok := obj[1].(string) - if !ok { - return shared.NewInvalidTypeError("index", "not a string") - } - args.Index = common.Big(arg1).Int64() - - return nil -} - -type BlockNumIndexArgs struct { - BlockNumber int64 - Index int64 -} - -func (args *BlockNumIndexArgs) UnmarshalJSON(b []byte) (err error) { - var obj []interface{} - if err := json.Unmarshal(b, &obj); err != nil { - return shared.NewDecodeParamError(err.Error()) - } - - if len(obj) < 2 { - return shared.NewInsufficientParamsError(len(obj), 2) - } - - if err := blockHeight(obj[0], &args.BlockNumber); err != nil { - return err - } - - var arg1 *big.Int - if arg1, err = numString(obj[1]); err != nil { - return err - } - args.Index = arg1.Int64() - - return nil -} - -type GetBlockByHashArgs struct { - BlockHash string - IncludeTxs bool -} - -func (args *GetBlockByHashArgs) UnmarshalJSON(b []byte) (err error) { - var obj []interface{} - - if err := json.Unmarshal(b, &obj); err != nil { - return shared.NewDecodeParamError(err.Error()) - } - - if len(obj) < 2 { - return shared.NewInsufficientParamsError(len(obj), 2) - } - - argstr, ok := obj[0].(string) - if !ok { - return shared.NewInvalidTypeError("blockHash", "not a string") - } - args.BlockHash = argstr - - args.IncludeTxs = obj[1].(bool) - - if inclTx, ok := obj[1].(bool); ok { - args.IncludeTxs = inclTx - return nil - } - - return shared.NewInvalidTypeError("includeTxs", "not a bool") -} - -type GetBlockByNumberArgs struct { - BlockNumber int64 - IncludeTxs bool -} - -func (args *GetBlockByNumberArgs) UnmarshalJSON(b []byte) (err error) { - var obj []interface{} - if err := json.Unmarshal(b, &obj); err != nil { - return shared.NewDecodeParamError(err.Error()) - } - - if len(obj) < 2 { - return shared.NewInsufficientParamsError(len(obj), 2) - } - - if err := blockHeight(obj[0], &args.BlockNumber); err != nil { - return err - } - - if inclTx, ok := obj[1].(bool); ok { - args.IncludeTxs = inclTx - return nil - } - - return shared.NewInvalidTypeError("includeTxs", "not a bool") -} - -type BlockFilterArgs struct { - Earliest int64 - Latest int64 - Address []string - Topics [][]string - Skip int - Max int -} - -func (args *BlockFilterArgs) UnmarshalJSON(b []byte) (err error) { - var obj []struct { - FromBlock interface{} `json:"fromBlock"` - ToBlock interface{} `json:"toBlock"` - Limit interface{} `json:"limit"` - Offset interface{} `json:"offset"` - Address interface{} `json:"address"` - Topics interface{} `json:"topics"` - } - - if err = json.Unmarshal(b, &obj); err != nil { - return shared.NewDecodeParamError(err.Error()) - } - - if len(obj) < 1 { - return shared.NewInsufficientParamsError(len(obj), 1) - } - - // args.Earliest, err = toNumber(obj[0].ToBlock) - // if err != nil { - // return shared.NewDecodeParamError(fmt.Sprintf("FromBlock %v", err)) - // } - // args.Latest, err = toNumber(obj[0].FromBlock) - // if err != nil { - // return shared.NewDecodeParamError(fmt.Sprintf("ToBlock %v", err)) - - var num int64 - var numBig *big.Int - - // if blank then latest - if obj[0].FromBlock == nil { - num = -1 - } else { - if err := blockHeight(obj[0].FromBlock, &num); err != nil { - return err - } - } - // if -2 or other "silly" number, use latest - if num < 0 { - args.Earliest = -1 //latest block - } else { - args.Earliest = num - } - - // if blank than latest - if obj[0].ToBlock == nil { - num = -1 - } else { - if err := blockHeight(obj[0].ToBlock, &num); err != nil { - return err - } - } - - if num == -2 { - return fmt.Errorf("\"pending\" is unsupported") - } else if num < -2 { - return fmt.Errorf("Invalid to block number") - } - - args.Latest = num - - if obj[0].Limit == nil { - numBig = big.NewInt(defaultLogLimit) - } else { - if numBig, err = numString(obj[0].Limit); err != nil { - return err - } - } - args.Max = int(numBig.Int64()) - - if obj[0].Offset == nil { - numBig = big.NewInt(defaultLogOffset) - } else { - if numBig, err = numString(obj[0].Offset); err != nil { - return err - } - } - args.Skip = int(numBig.Int64()) - - if obj[0].Address != nil { - marg, ok := obj[0].Address.([]interface{}) - if ok { - v := make([]string, len(marg)) - for i, arg := range marg { - argstr, ok := arg.(string) - if !ok { - return shared.NewInvalidTypeError(fmt.Sprintf("address[%d]", i), "is not a string") - } - v[i] = argstr - } - args.Address = v - } else { - argstr, ok := obj[0].Address.(string) - if ok { - v := make([]string, 1) - v[0] = argstr - args.Address = v - } else { - return shared.NewInvalidTypeError("address", "is not a string or array") - } - } - } - - if obj[0].Topics != nil { - other, ok := obj[0].Topics.([]interface{}) - if ok { - topicdbl := make([][]string, len(other)) - for i, iv := range other { - if argstr, ok := iv.(string); ok { - // Found a string, push into first element of array - topicsgl := make([]string, 1) - topicsgl[0] = argstr - topicdbl[i] = topicsgl - } else if argarray, ok := iv.([]interface{}); ok { - // Found an array of other - topicdbl[i] = make([]string, len(argarray)) - for j, jv := range argarray { - if v, ok := jv.(string); ok { - topicdbl[i][j] = v - } else if jv == nil { - topicdbl[i][j] = "" - } else { - return shared.NewInvalidTypeError(fmt.Sprintf("topic[%d][%d]", i, j), "is not a string") - } - } - } else if iv == nil { - topicdbl[i] = []string{""} - } else { - return shared.NewInvalidTypeError(fmt.Sprintf("topic[%d]", i), "not a string or array") - } - } - args.Topics = topicdbl - return nil - } else { - return shared.NewInvalidTypeError("topic", "is not a string or array") - } - } - - return nil -} - -type FilterIdArgs struct { - Id int -} - -func (args *FilterIdArgs) 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) - } - - var num *big.Int - if num, err = numString(obj[0]); err != nil { - return err - } - args.Id = int(num.Int64()) - - return nil -} - -type LogRes struct { - Address *hexdata `json:"address"` - Topics []*hexdata `json:"topics"` - Data *hexdata `json:"data"` - BlockNumber *hexnum `json:"blockNumber"` - LogIndex *hexnum `json:"logIndex"` - BlockHash *hexdata `json:"blockHash"` - TransactionHash *hexdata `json:"transactionHash"` - TransactionIndex *hexnum `json:"transactionIndex"` -} - -func NewLogRes(log *vm.Log) LogRes { - var l LogRes - l.Topics = make([]*hexdata, len(log.Topics)) - for j, topic := range log.Topics { - l.Topics[j] = newHexData(topic) - } - l.Address = newHexData(log.Address) - l.Data = newHexData(log.Data) - l.BlockNumber = newHexNum(log.BlockNumber) - l.LogIndex = newHexNum(log.Index) - l.TransactionHash = newHexData(log.TxHash) - l.TransactionIndex = newHexNum(log.TxIndex) - l.BlockHash = newHexData(log.BlockHash) - - return l -} - -func NewLogsRes(logs vm.Logs) (ls []LogRes) { - ls = make([]LogRes, len(logs)) - - for i, log := range logs { - ls[i] = NewLogRes(log) - } - - return -} - -func NewHashesRes(hs []common.Hash) []string { - hashes := make([]string, len(hs)) - - for i, hash := range hs { - hashes[i] = hash.Hex() - } - - return hashes -} - -type SubmitWorkArgs struct { - Nonce uint64 - Header string - Digest string -} - -func (args *SubmitWorkArgs) UnmarshalJSON(b []byte) (err error) { - var obj []interface{} - if err = json.Unmarshal(b, &obj); err != nil { - return shared.NewDecodeParamError(err.Error()) - } - - if len(obj) < 3 { - return shared.NewInsufficientParamsError(len(obj), 3) - } - - var objstr string - var ok bool - if objstr, ok = obj[0].(string); !ok { - return shared.NewInvalidTypeError("nonce", "not a string") - } - - args.Nonce = common.String2Big(objstr).Uint64() - if objstr, ok = obj[1].(string); !ok { - return shared.NewInvalidTypeError("header", "not a string") - } - - args.Header = objstr - - if objstr, ok = obj[2].(string); !ok { - return shared.NewInvalidTypeError("digest", "not a string") - } - - args.Digest = objstr - - return nil -} - -type tx struct { - tx *types.Transaction - - To string `json:"to"` - From string `json:"from"` - Nonce string `json:"nonce"` - Value string `json:"value"` - Data string `json:"data"` - GasLimit string `json:"gas"` - GasPrice string `json:"gasPrice"` - Hash string `json:"hash"` -} - -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.Value().String(), - Nonce: strconv.Itoa(int(t.Nonce())), - Data: "0x" + common.Bytes2Hex(t.Data()), - GasLimit: t.Gas().String(), - GasPrice: t.GasPrice().String(), - Hash: t.Hash().Hex(), - } -} - -type ResendArgs struct { - Tx *tx - GasPrice string - 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()) - } - - 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["Hash"]; found { - if hashVal, ok := val.(string); ok { - tx.Hash = hashVal - } - } - - if val, found := fields["To"]; found { - if strVal, ok := val.(string); ok && len(strVal) > 0 { - tx.To = strVal - to = common.HexToAddress(strVal) - contractCreation = false - } - } - - 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 nonce, err = strconv.ParseUint(strVal, 10, 64); err != nil { - 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 _, parseOk = amount.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") { - data = common.Hex2Bytes(strVal[2:]) - } else { - data = common.Hex2Bytes(strVal) - } - } - } - - if val, found := fields["GasLimit"]; found { - if strVal, ok := val.(string); ok { - tx.GasLimit = strVal - if _, parseOk = gasLimit.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 _, parseOk = gasPrice.SetString(strVal, 0); !parseOk { - return shared.NewDecodeParamError(fmt.Sprintf("Unable to decode tx.GasPrice - %v", err)) - } - } - } - - if contractCreation { - tx.tx = types.NewContractCreation(nonce, amount, gasLimit, gasPrice, data) - } else { - tx.tx = types.NewTransaction(nonce, to, amount, gasLimit, gasPrice, data) - } - - return nil -} - -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") - } - - if trans == nil || trans.tx == 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 deleted file mode 100644 index dfc104ad8..000000000 --- a/rpc/api/eth_js.go +++ /dev/null @@ -1,66 +0,0 @@ -// Copyright 2015 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library 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 Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see . - -package api - -// JS api provided by web3.js -// eth_sign not standard - -const Eth_JS = ` -web3._extend({ - property: 'eth', - methods: - [ - new web3._extend.Method({ - name: 'sign', - call: 'eth_sign', - params: 2, - inputFormatter: [web3._extend.utils.toAddress, null] - }), - new web3._extend.Method({ - name: 'resend', - call: 'eth_resend', - params: 3, - inputFormatter: [web3._extend.formatters.inputTransactionFormatter, web3._extend.utils.fromDecimal, web3._extend.utils.fromDecimal] - }), - new web3._extend.Method({ - name: 'getNatSpec', - call: 'eth_getNatSpec', - params: 1, - inputFormatter: [web3._extend.formatters.inputTransactionFormatter] - }), - new web3._extend.Method({ - name: 'signTransaction', - call: 'eth_signTransaction', - params: 1, - inputFormatter: [web3._extend.formatters.inputTransactionFormatter] - }), - new web3._extend.Method({ - name: 'submitTransaction', - call: 'eth_submitTransaction', - params: 1, - inputFormatter: [web3._extend.formatters.inputTransactionFormatter] - }) - ], - properties: - [ - new web3._extend.Property({ - name: 'pendingTransactions', - getter: 'eth_pendingTransactions' - }) - ] -}); -` diff --git a/rpc/api/mergedapi.go b/rpc/api/mergedapi.go deleted file mode 100644 index 92e1e2bb7..000000000 --- a/rpc/api/mergedapi.go +++ /dev/null @@ -1,88 +0,0 @@ -// Copyright 2015 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library 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 Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see . - -package api - -import ( - "github.com/ethereum/go-ethereum/logger" - "github.com/ethereum/go-ethereum/logger/glog" - "github.com/ethereum/go-ethereum/rpc/shared" -) - -const ( - MergedApiVersion = "1.0" -) - -// combines multiple API's -type MergedApi struct { - apis map[string]string - methods map[string]shared.EthereumApi -} - -// create new merged api instance -func newMergedApi(apis ...shared.EthereumApi) *MergedApi { - mergedApi := new(MergedApi) - mergedApi.apis = make(map[string]string, len(apis)) - mergedApi.methods = make(map[string]shared.EthereumApi) - - for _, api := range apis { - if api != nil { - mergedApi.apis[api.Name()] = api.ApiVersion() - for _, method := range api.Methods() { - mergedApi.methods[method] = api - } - } - } - return mergedApi -} - -// Supported RPC methods -func (self *MergedApi) Methods() []string { - all := make([]string, len(self.methods)) - for method, _ := range self.methods { - all = append(all, method) - } - return all -} - -// Call the correct API's Execute method for the given request -func (self *MergedApi) Execute(req *shared.Request) (interface{}, error) { - glog.V(logger.Detail).Infof("%s %s", req.Method, req.Params) - - if res, _ := self.handle(req); res != nil { - return res, nil - } - if api, found := self.methods[req.Method]; found { - return api.Execute(req) - } - return nil, shared.NewNotImplementedError(req.Method) -} - -func (self *MergedApi) Name() string { - return shared.MergedApiName -} - -func (self *MergedApi) ApiVersion() string { - return MergedApiVersion -} - -func (self *MergedApi) handle(req *shared.Request) (interface{}, error) { - if req.Method == "modules" { // provided API's - return self.apis, nil - } - - return nil, nil -} diff --git a/rpc/api/miner.go b/rpc/api/miner.go deleted file mode 100644 index e07855dd2..000000000 --- a/rpc/api/miner.go +++ /dev/null @@ -1,177 +0,0 @@ -// Copyright 2015 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library 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 Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see . - -package api - -import ( - "github.com/ethereum/ethash" - "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" -) - -const ( - MinerApiVersion = "1.0" -) - -var ( - // mapping between methods and handlers - MinerMapping = map[string]minerhandler{ - "miner_hashrate": (*minerApi).Hashrate, - "miner_makeDAG": (*minerApi).MakeDAG, - "miner_setExtra": (*minerApi).SetExtra, - "miner_setGasPrice": (*minerApi).SetGasPrice, - "miner_setEtherbase": (*minerApi).SetEtherbase, - "miner_startAutoDAG": (*minerApi).StartAutoDAG, - "miner_start": (*minerApi).StartMiner, - "miner_stopAutoDAG": (*minerApi).StopAutoDAG, - "miner_stop": (*minerApi).StopMiner, - } -) - -// miner callback handler -type minerhandler func(*minerApi, *shared.Request) (interface{}, error) - -// miner api provider -type minerApi struct { - ethereum *eth.Ethereum - methods map[string]minerhandler - codec codec.ApiCoder -} - -// create a new miner api instance -func NewMinerApi(ethereum *eth.Ethereum, coder codec.Codec) *minerApi { - return &minerApi{ - ethereum: ethereum, - methods: MinerMapping, - codec: coder.New(nil), - } -} - -// Execute given request -func (self *minerApi) Execute(req *shared.Request) (interface{}, error) { - if callback, ok := self.methods[req.Method]; ok { - return callback(self, req) - } - - return nil, &shared.NotImplementedError{req.Method} -} - -// collection with supported methods -func (self *minerApi) Methods() []string { - methods := make([]string, len(self.methods)) - i := 0 - for k := range self.methods { - methods[i] = k - i++ - } - return methods -} - -func (self *minerApi) Name() string { - return shared.MinerApiName -} - -func (self *minerApi) ApiVersion() string { - return MinerApiVersion -} - -func (self *minerApi) StartMiner(req *shared.Request) (interface{}, error) { - args := new(StartMinerArgs) - if err := self.codec.Decode(req.Params, &args); err != nil { - return nil, err - } - if args.Threads == -1 { // (not specified by user, use default) - args.Threads = self.ethereum.MinerThreads - } - - self.ethereum.StartAutoDAG() - err := self.ethereum.StartMining(args.Threads, "") - if err == nil { - return true, nil - } - - return false, err -} - -func (self *minerApi) StopMiner(req *shared.Request) (interface{}, error) { - self.ethereum.StopMining() - return true, nil -} - -func (self *minerApi) Hashrate(req *shared.Request) (interface{}, error) { - return self.ethereum.Miner().HashRate(), nil -} - -func (self *minerApi) SetExtra(req *shared.Request) (interface{}, error) { - args := new(SetExtraArgs) - if err := self.codec.Decode(req.Params, &args); err != nil { - return nil, err - } - - if err := self.ethereum.Miner().SetExtra([]byte(args.Data)); err != nil { - return false, err - } - - return true, nil -} - -func (self *minerApi) SetGasPrice(req *shared.Request) (interface{}, error) { - args := new(GasPriceArgs) - if err := self.codec.Decode(req.Params, &args); err != nil { - return false, err - } - - self.ethereum.Miner().SetGasPrice(common.String2Big(args.Price)) - return true, nil -} - -func (self *minerApi) SetEtherbase(req *shared.Request) (interface{}, error) { - args := new(SetEtherbaseArgs) - if err := self.codec.Decode(req.Params, &args); err != nil { - return false, err - } - self.ethereum.SetEtherbase(args.Etherbase) - return nil, nil -} - -func (self *minerApi) StartAutoDAG(req *shared.Request) (interface{}, error) { - self.ethereum.StartAutoDAG() - return true, nil -} - -func (self *minerApi) StopAutoDAG(req *shared.Request) (interface{}, error) { - self.ethereum.StopAutoDAG() - return true, nil -} - -func (self *minerApi) MakeDAG(req *shared.Request) (interface{}, error) { - args := new(MakeDAGArgs) - if err := self.codec.Decode(req.Params, &args); err != nil { - return nil, err - } - - if args.BlockNumber < 0 { - return false, shared.NewValidationError("BlockNumber", "BlockNumber must be positive") - } - - err := ethash.MakeDAG(uint64(args.BlockNumber), "") - if err == nil { - return true, nil - } - return false, err -} diff --git a/rpc/api/miner_args.go b/rpc/api/miner_args.go deleted file mode 100644 index 5ceb244fe..000000000 --- a/rpc/api/miner_args.go +++ /dev/null @@ -1,142 +0,0 @@ -// Copyright 2015 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library 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 Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see . - -package api - -import ( - "encoding/json" - - "math/big" - - "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/rpc/shared" -) - -type StartMinerArgs struct { - Threads int -} - -func (args *StartMinerArgs) UnmarshalJSON(b []byte) (err error) { - var obj []interface{} - if err := json.Unmarshal(b, &obj); err != nil { - return shared.NewDecodeParamError(err.Error()) - } - - if len(obj) == 0 || obj[0] == nil { - args.Threads = -1 - return nil - } - - var num *big.Int - if num, err = numString(obj[0]); err != nil { - return err - } - args.Threads = int(num.Int64()) - return nil -} - -type SetExtraArgs struct { - Data string -} - -func (args *SetExtraArgs) 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) - } - - extrastr, ok := obj[0].(string) - if !ok { - return shared.NewInvalidTypeError("Price", "not a string") - } - args.Data = extrastr - - return nil -} - -type GasPriceArgs struct { - Price string -} - -func (args *GasPriceArgs) 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) - } - - if pricestr, ok := obj[0].(string); ok { - args.Price = pricestr - return nil - } - - return shared.NewInvalidTypeError("Price", "not a string") -} - -type SetEtherbaseArgs struct { - Etherbase common.Address -} - -func (args *SetEtherbaseArgs) 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) - } - - if addr, ok := obj[0].(string); ok { - args.Etherbase = common.HexToAddress(addr) - if (args.Etherbase == common.Address{}) { - return shared.NewInvalidTypeError("Etherbase", "not a valid address") - } - return nil - } - - return shared.NewInvalidTypeError("Etherbase", "not a string") -} - -type MakeDAGArgs struct { - BlockNumber int64 -} - -func (args *MakeDAGArgs) UnmarshalJSON(b []byte) (err error) { - args.BlockNumber = -1 - 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) - } - - if err := blockHeight(obj[0], &args.BlockNumber); err != nil { - return err - } - - return nil -} diff --git a/rpc/api/miner_js.go b/rpc/api/miner_js.go deleted file mode 100644 index 0998a9f41..000000000 --- a/rpc/api/miner_js.go +++ /dev/null @@ -1,83 +0,0 @@ -// Copyright 2015 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library 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 Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see . - -package api - -const Miner_JS = ` -web3._extend({ - property: 'miner', - methods: - [ - new web3._extend.Method({ - name: 'start', - call: 'miner_start', - params: 1, - inputFormatter: [null] - }), - new web3._extend.Method({ - name: 'stop', - call: 'miner_stop', - params: 1, - inputFormatter: [null] - }), - new web3._extend.Method({ - name: 'setEtherbase', - call: 'miner_setEtherbase', - params: 1, - inputFormatter: [web3._extend.formatters.formatInputInt], - outputFormatter: web3._extend.formatters.formatOutputBool - }), - new web3._extend.Method({ - name: 'setExtra', - call: 'miner_setExtra', - params: 1, - inputFormatter: [null] - }), - new web3._extend.Method({ - name: 'setGasPrice', - call: 'miner_setGasPrice', - params: 1, - inputFormatter: [web3._extend.utils.fromDecial] - }), - new web3._extend.Method({ - name: 'startAutoDAG', - call: 'miner_startAutoDAG', - params: 0, - inputFormatter: [] - }), - new web3._extend.Method({ - name: 'stopAutoDAG', - call: 'miner_stopAutoDAG', - params: 0, - inputFormatter: [] - }), - new web3._extend.Method({ - name: 'makeDAG', - call: 'miner_makeDAG', - params: 1, - inputFormatter: [web3._extend.formatters.inputDefaultBlockNumberFormatter] - }) - ], - properties: - [ - new web3._extend.Property({ - name: 'hashrate', - getter: 'miner_hashrate', - outputFormatter: web3._extend.utils.toDecimal - }) - ] -}); -` diff --git a/rpc/api/net.go b/rpc/api/net.go deleted file mode 100644 index 9c6369615..000000000 --- a/rpc/api/net.go +++ /dev/null @@ -1,99 +0,0 @@ -// Copyright 2015 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library 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 Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see . - -package api - -import ( - "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" -) - -const ( - NetApiVersion = "1.0" -) - -var ( - // mapping between methods and handlers - netMapping = map[string]nethandler{ - "net_peerCount": (*netApi).PeerCount, - "net_listening": (*netApi).IsListening, - "net_version": (*netApi).Version, - } -) - -// net callback handler -type nethandler func(*netApi, *shared.Request) (interface{}, error) - -// net api provider -type netApi struct { - xeth *xeth.XEth - ethereum *eth.Ethereum - methods map[string]nethandler - codec codec.ApiCoder -} - -// create a new net api instance -func NewNetApi(xeth *xeth.XEth, eth *eth.Ethereum, coder codec.Codec) *netApi { - return &netApi{ - xeth: xeth, - ethereum: eth, - methods: netMapping, - codec: coder.New(nil), - } -} - -// collection with supported methods -func (self *netApi) Methods() []string { - methods := make([]string, len(self.methods)) - i := 0 - for k := range self.methods { - methods[i] = k - i++ - } - return methods -} - -// Execute given request -func (self *netApi) Execute(req *shared.Request) (interface{}, error) { - if callback, ok := self.methods[req.Method]; ok { - return callback(self, req) - } - - return nil, shared.NewNotImplementedError(req.Method) -} - -func (self *netApi) Name() string { - return shared.NetApiName -} - -func (self *netApi) ApiVersion() string { - return NetApiVersion -} - -// Number of connected peers -func (self *netApi) PeerCount(req *shared.Request) (interface{}, error) { - return newHexNum(self.xeth.PeerCount()), nil -} - -func (self *netApi) IsListening(req *shared.Request) (interface{}, error) { - return self.xeth.IsListening(), nil -} - -func (self *netApi) Version(req *shared.Request) (interface{}, error) { - return self.xeth.NetworkVersion(), nil -} diff --git a/rpc/api/net_js.go b/rpc/api/net_js.go deleted file mode 100644 index 2ee1f0041..000000000 --- a/rpc/api/net_js.go +++ /dev/null @@ -1,39 +0,0 @@ -// Copyright 2015 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library 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 Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see . - -package api - -const Net_JS = ` -web3._extend({ - property: 'net', - methods: - [ - new web3._extend.Method({ - name: 'addPeer', - call: 'net_addPeer', - params: 1, - inputFormatter: [null] - }) - ], - properties: - [ - new web3._extend.Property({ - name: 'version', - getter: 'net_version' - }) - ] -}); -` diff --git a/rpc/api/parsing.go b/rpc/api/parsing.go deleted file mode 100644 index 7667616ff..000000000 --- a/rpc/api/parsing.go +++ /dev/null @@ -1,522 +0,0 @@ -// Copyright 2015 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library 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 Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see . - -package api - -import ( - "bytes" - "encoding/binary" - "encoding/hex" - "encoding/json" - "math/big" - "strings" - - "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/core/types" - "github.com/ethereum/go-ethereum/rpc/shared" -) - -type hexdata struct { - data []byte - isNil bool -} - -func (d *hexdata) String() string { - return "0x" + common.Bytes2Hex(d.data) -} - -func (d *hexdata) MarshalJSON() ([]byte, error) { - if d.isNil { - return json.Marshal(nil) - } - return json.Marshal(d.String()) -} - -func newHexData(input interface{}) *hexdata { - d := new(hexdata) - - if input == nil { - d.isNil = true - return d - } - switch input := input.(type) { - case []byte: - d.data = input - case common.Hash: - d.data = input.Bytes() - case *common.Hash: - if input == nil { - d.isNil = true - } else { - 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 types.Bloom: - d.data = input.Bytes() - case *types.Bloom: - if input == nil { - d.isNil = true - } else { - d.data = input.Bytes() - } - case *big.Int: - if input == nil { - d.isNil = true - } else { - d.data = input.Bytes() - } - case int64: - d.data = big.NewInt(input).Bytes() - case uint64: - buff := make([]byte, 8) - binary.BigEndian.PutUint64(buff, input) - d.data = buff - case int: - 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: - buff := make([]byte, 2) - binary.BigEndian.PutUint16(buff, input) - d.data = buff - case int32: - d.data = big.NewInt(int64(input)).Bytes() - case uint32: - buff := make([]byte, 4) - binary.BigEndian.PutUint32(buff, input) - d.data = buff - case string: // hexstring - // aaargh ffs TODO: avoid back-and-forth hex encodings where unneeded - bytes, err := hex.DecodeString(strings.TrimPrefix(input, "0x")) - if err != nil { - d.isNil = true - } else { - d.data = bytes - } - default: - d.isNil = true - } - - return d -} - -type hexnum struct { - data []byte - isNil bool -} - -func (d *hexnum) String() string { - // Get hex string from bytes - out := common.Bytes2Hex(d.data) - // Trim leading 0s - out = strings.TrimLeft(out, "0") - // Output "0x0" when value is 0 - if len(out) == 0 { - out = "0" - } - return "0x" + out -} - -func (d *hexnum) MarshalJSON() ([]byte, error) { - if d.isNil { - return json.Marshal(nil) - } - return json.Marshal(d.String()) -} - -func newHexNum(input interface{}) *hexnum { - d := new(hexnum) - - d.data = newHexData(input).data - - return d -} - -type BlockRes struct { - fullTx bool - - 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:"transactionsRoot"` - StateRoot *hexdata `json:"stateRoot"` - ReceiptRoot *hexdata `json:"receiptRoot"` - Miner *hexdata `json:"miner"` - Difficulty *hexnum `json:"difficulty"` - TotalDifficulty *hexnum `json:"totalDifficulty"` - Size *hexnum `json:"size"` - ExtraData *hexdata `json:"extraData"` - GasLimit *hexnum `json:"gasLimit"` - GasUsed *hexnum `json:"gasUsed"` - UnixTimestamp *hexnum `json:"timestamp"` - Transactions []*TransactionRes `json:"transactions"` - Uncles []*UncleRes `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:"transactionsRoot"` - StateRoot *hexdata `json:"stateRoot"` - ReceiptRoot *hexdata `json:"receiptRoot"` - Miner *hexdata `json:"miner"` - Difficulty *hexnum `json:"difficulty"` - TotalDifficulty *hexnum `json:"totalDifficulty"` - Size *hexnum `json:"size"` - ExtraData *hexdata `json:"extraData"` - GasLimit *hexnum `json:"gasLimit"` - 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.ReceiptRoot = b.ReceiptRoot - 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.GasUsed = b.GasUsed - ext.UnixTimestamp = b.UnixTimestamp - ext.Transactions = b.Transactions - ext.Uncles = make([]*hexdata, len(b.Uncles)) - for i, u := range b.Uncles { - ext.Uncles[i] = u.BlockHash - } - 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:"transactionsRoot"` - StateRoot *hexdata `json:"stateRoot"` - ReceiptRoot *hexdata `json:"receiptRoot"` - Miner *hexdata `json:"miner"` - Difficulty *hexnum `json:"difficulty"` - TotalDifficulty *hexnum `json:"totalDifficulty"` - Size *hexnum `json:"size"` - ExtraData *hexdata `json:"extraData"` - GasLimit *hexnum `json:"gasLimit"` - 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.ReceiptRoot = b.ReceiptRoot - 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.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 = make([]*hexdata, len(b.Uncles)) - for i, u := range b.Uncles { - ext.Uncles[i] = u.BlockHash - } - return json.Marshal(ext) - } -} - -func NewBlockRes(block *types.Block, td *big.Int, fullTx bool) *BlockRes { - if block == nil { - return nil - } - - res := new(BlockRes) - res.fullTx = fullTx - res.BlockNumber = newHexNum(block.Number()) - res.BlockHash = newHexData(block.Hash()) - res.ParentHash = newHexData(block.ParentHash()) - res.Nonce = newHexData(block.Nonce()) - res.Sha3Uncles = newHexData(block.UncleHash()) - res.LogsBloom = newHexData(block.Bloom()) - res.TransactionRoot = newHexData(block.TxHash()) - res.StateRoot = newHexData(block.Root()) - res.ReceiptRoot = newHexData(block.ReceiptHash()) - res.Miner = newHexData(block.Coinbase()) - res.Difficulty = newHexNum(block.Difficulty()) - res.TotalDifficulty = newHexNum(td) - res.Size = newHexNum(block.Size().Int64()) - res.ExtraData = newHexData(block.Extra()) - res.GasLimit = newHexNum(block.GasLimit()) - res.GasUsed = newHexNum(block.GasUsed()) - res.UnixTimestamp = newHexNum(block.Time()) - - txs := block.Transactions() - res.Transactions = make([]*TransactionRes, len(txs)) - for i, tx := range txs { - res.Transactions[i] = NewTransactionRes(tx) - res.Transactions[i].BlockHash = res.BlockHash - res.Transactions[i].BlockNumber = res.BlockNumber - res.Transactions[i].TxIndex = newHexNum(i) - } - - uncles := block.Uncles() - res.Uncles = make([]*UncleRes, len(uncles)) - for i, uncle := range uncles { - res.Uncles[i] = NewUncleRes(uncle) - } - - return res -} - -type TransactionRes struct { - Hash *hexdata `json:"hash"` - Nonce *hexnum `json:"nonce"` - BlockHash *hexdata `json:"blockHash"` - BlockNumber *hexnum `json:"blockNumber"` - TxIndex *hexnum `json:"transactionIndex"` - From *hexdata `json:"from"` - To *hexdata `json:"to"` - Value *hexnum `json:"value"` - Gas *hexnum `json:"gas"` - GasPrice *hexnum `json:"gasPrice"` - Input *hexdata `json:"input"` -} - -func NewTransactionRes(tx *types.Transaction) *TransactionRes { - if tx == nil { - return nil - } - - var v = new(TransactionRes) - v.Hash = newHexData(tx.Hash()) - v.Nonce = newHexNum(tx.Nonce()) - // v.BlockHash = - // v.BlockNumber = - // v.TxIndex = - from, _ := tx.From() - v.From = newHexData(from) - v.To = newHexData(tx.To()) - v.Value = newHexNum(tx.Value()) - v.Gas = newHexNum(tx.Gas()) - v.GasPrice = newHexNum(tx.GasPrice()) - v.Input = newHexData(tx.Data()) - return v -} - -type UncleRes struct { - BlockNumber *hexnum `json:"number"` - BlockHash *hexdata `json:"hash"` - ParentHash *hexdata `json:"parentHash"` - Nonce *hexdata `json:"nonce"` - Sha3Uncles *hexdata `json:"sha3Uncles"` - ReceiptHash *hexdata `json:"receiptHash"` - LogsBloom *hexdata `json:"logsBloom"` - TransactionRoot *hexdata `json:"transactionsRoot"` - StateRoot *hexdata `json:"stateRoot"` - Miner *hexdata `json:"miner"` - Difficulty *hexnum `json:"difficulty"` - ExtraData *hexdata `json:"extraData"` - GasLimit *hexnum `json:"gasLimit"` - GasUsed *hexnum `json:"gasUsed"` - UnixTimestamp *hexnum `json:"timestamp"` -} - -func NewUncleRes(h *types.Header) *UncleRes { - if h == nil { - return nil - } - - var v = new(UncleRes) - v.BlockNumber = newHexNum(h.Number) - v.BlockHash = newHexData(h.Hash()) - v.ParentHash = newHexData(h.ParentHash) - v.Sha3Uncles = newHexData(h.UncleHash) - v.Nonce = newHexData(h.Nonce[:]) - v.LogsBloom = newHexData(h.Bloom) - v.TransactionRoot = newHexData(h.TxHash) - v.StateRoot = newHexData(h.Root) - v.Miner = newHexData(h.Coinbase) - v.Difficulty = newHexNum(h.Difficulty) - v.ExtraData = newHexData(h.Extra) - v.GasLimit = newHexNum(h.GasLimit) - v.GasUsed = newHexNum(h.GasUsed) - v.UnixTimestamp = newHexNum(h.Time) - v.ReceiptHash = newHexData(h.ReceiptHash) - - return v -} - -// type FilterLogRes struct { -// Hash string `json:"hash"` -// Address string `json:"address"` -// Data string `json:"data"` -// BlockNumber string `json:"blockNumber"` -// TransactionHash string `json:"transactionHash"` -// BlockHash string `json:"blockHash"` -// TransactionIndex string `json:"transactionIndex"` -// LogIndex string `json:"logIndex"` -// } - -// type FilterWhisperRes struct { -// Hash string `json:"hash"` -// From string `json:"from"` -// To string `json:"to"` -// Expiry string `json:"expiry"` -// Sent string `json:"sent"` -// Ttl string `json:"ttl"` -// Topics string `json:"topics"` -// Payload string `json:"payload"` -// WorkProved string `json:"workProved"` -// } - -type ReceiptRes struct { - TransactionHash *hexdata `json:"transactionHash"` - TransactionIndex *hexnum `json:"transactionIndex"` - BlockNumber *hexnum `json:"blockNumber"` - BlockHash *hexdata `json:"blockHash"` - CumulativeGasUsed *hexnum `json:"cumulativeGasUsed"` - GasUsed *hexnum `json:"gasUsed"` - ContractAddress *hexdata `json:"contractAddress"` - Logs *[]interface{} `json:"logs"` -} - -func NewReceiptRes(rec *types.Receipt) *ReceiptRes { - if rec == nil { - return nil - } - - var v = new(ReceiptRes) - v.TransactionHash = newHexData(rec.TxHash) - if rec.GasUsed != nil { - v.GasUsed = newHexNum(rec.GasUsed.Bytes()) - } - v.CumulativeGasUsed = newHexNum(rec.CumulativeGasUsed) - - // If the ContractAddress is 20 0x0 bytes, assume it is not a contract creation - if bytes.Compare(rec.ContractAddress.Bytes(), bytes.Repeat([]byte{0}, 20)) != 0 { - v.ContractAddress = newHexData(rec.ContractAddress) - } - - logs := make([]interface{}, len(rec.Logs)) - for i, log := range rec.Logs { - logs[i] = NewLogRes(log) - } - v.Logs = &logs - - return v -} - -func numString(raw interface{}) (*big.Int, error) { - var number *big.Int - // Parse as integer - num, ok := raw.(float64) - if ok { - number = big.NewInt(int64(num)) - return number, nil - } - - // Parse as string/hexstring - str, ok := raw.(string) - if ok { - number = common.String2Big(str) - return number, nil - } - - return nil, shared.NewInvalidTypeError("", "not a number or string") -} - -func blockHeight(raw interface{}, number *int64) error { - // Parse as integer - num, ok := raw.(float64) - if ok { - *number = int64(num) - return nil - } - - // Parse as string/hexstring - str, ok := raw.(string) - if !ok { - return shared.NewInvalidTypeError("", "not a number or string") - } - - switch str { - case "earliest": - *number = 0 - case "latest": - *number = -1 - case "pending": - *number = -2 - default: - if common.HasHexPrefix(str) { - *number = common.String2Big(str).Int64() - } else { - return shared.NewInvalidTypeError("blockNumber", "is not a valid string") - } - } - - return nil -} - -func blockHeightFromJson(msg json.RawMessage, number *int64) error { - var raw interface{} - if err := json.Unmarshal(msg, &raw); err != nil { - return shared.NewDecodeParamError(err.Error()) - } - return blockHeight(raw, number) -} diff --git a/rpc/api/personal.go b/rpc/api/personal.go deleted file mode 100644 index 4f347c610..000000000 --- a/rpc/api/personal.go +++ /dev/null @@ -1,139 +0,0 @@ -// Copyright 2015 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library 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 Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see . - -package api - -import ( - "fmt" - "time" - - "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" -) - -const ( - PersonalApiVersion = "1.0" -) - -var ( - // mapping between methods and handlers - personalMapping = map[string]personalhandler{ - "personal_listAccounts": (*personalApi).ListAccounts, - "personal_newAccount": (*personalApi).NewAccount, - "personal_unlockAccount": (*personalApi).UnlockAccount, - } -) - -// net callback handler -type personalhandler func(*personalApi, *shared.Request) (interface{}, error) - -// net api provider -type personalApi struct { - xeth *xeth.XEth - ethereum *eth.Ethereum - methods map[string]personalhandler - codec codec.ApiCoder -} - -// create a new net api instance -func NewPersonalApi(xeth *xeth.XEth, eth *eth.Ethereum, coder codec.Codec) *personalApi { - return &personalApi{ - xeth: xeth, - ethereum: eth, - methods: personalMapping, - codec: coder.New(nil), - } -} - -// collection with supported methods -func (self *personalApi) Methods() []string { - methods := make([]string, len(self.methods)) - i := 0 - for k := range self.methods { - methods[i] = k - i++ - } - return methods -} - -// Execute given request -func (self *personalApi) Execute(req *shared.Request) (interface{}, error) { - if callback, ok := self.methods[req.Method]; ok { - return callback(self, req) - } - - return nil, shared.NewNotImplementedError(req.Method) -} - -func (self *personalApi) Name() string { - return shared.PersonalApiName -} - -func (self *personalApi) ApiVersion() string { - return PersonalApiVersion -} - -func (self *personalApi) ListAccounts(req *shared.Request) (interface{}, error) { - return self.xeth.Accounts(), nil -} - -func (self *personalApi) NewAccount(req *shared.Request) (interface{}, error) { - args := new(NewAccountArgs) - if err := self.codec.Decode(req.Params, &args); err != nil { - return nil, shared.NewDecodeParamError(err.Error()) - } - var passwd string - if args.Passphrase == nil { - fe := self.xeth.Frontend() - if fe == nil { - return false, fmt.Errorf("unable to create account: unable to interact with user") - } - var ok bool - passwd, ok = fe.AskPassword() - if !ok { - return false, fmt.Errorf("unable to create account: no password given") - } - } else { - passwd = *args.Passphrase - } - am := self.ethereum.AccountManager() - acc, err := am.NewAccount(passwd) - return acc.Address.Hex(), err -} - -func (self *personalApi) UnlockAccount(req *shared.Request) (interface{}, error) { - args := new(UnlockAccountArgs) - if err := self.codec.Decode(req.Params, &args); err != nil { - return nil, shared.NewDecodeParamError(err.Error()) - } - - if args.Passphrase == nil { - fe := self.xeth.Frontend() - if fe == nil { - return false, fmt.Errorf("No password provided") - } - return fe.UnlockAccount(common.HexToAddress(args.Address).Bytes()), nil - } - - am := self.ethereum.AccountManager() - addr := common.HexToAddress(args.Address) - - err := am.TimedUnlock(addr, *args.Passphrase, time.Duration(args.Duration)*time.Second) - return err == nil, err -} diff --git a/rpc/api/personal_args.go b/rpc/api/personal_args.go deleted file mode 100644 index 5d215c71d..000000000 --- a/rpc/api/personal_args.go +++ /dev/null @@ -1,85 +0,0 @@ -// Copyright 2015 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library 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 Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see . - -package api - -import ( - "encoding/json" - - "github.com/ethereum/go-ethereum/rpc/shared" -) - -type NewAccountArgs struct { - Passphrase *string -} - -func (args *NewAccountArgs) 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 && obj[0] != nil { - if passphrasestr, ok := obj[0].(string); ok { - args.Passphrase = &passphrasestr - } else { - return shared.NewInvalidTypeError("passphrase", "not a string") - } - } - - return nil -} - -type UnlockAccountArgs struct { - Address string - Passphrase *string - Duration int -} - -func (args *UnlockAccountArgs) UnmarshalJSON(b []byte) (err error) { - var obj []interface{} - if err := json.Unmarshal(b, &obj); err != nil { - return shared.NewDecodeParamError(err.Error()) - } - - args.Duration = 0 - - if len(obj) < 1 { - return shared.NewInsufficientParamsError(len(obj), 1) - } - - if addrstr, ok := obj[0].(string); ok { - args.Address = addrstr - } else { - return shared.NewInvalidTypeError("address", "not a string") - } - - if len(obj) >= 2 && obj[1] != nil { - if passphrasestr, ok := obj[1].(string); ok { - args.Passphrase = &passphrasestr - } else { - return shared.NewInvalidTypeError("passphrase", "not a string") - } - } - - if len(obj) >= 3 && obj[2] != nil { - if duration, ok := obj[2].(float64); ok { - args.Duration = int(duration) - } - } - - return nil -} diff --git a/rpc/api/personal_js.go b/rpc/api/personal_js.go deleted file mode 100644 index 84c669af7..000000000 --- a/rpc/api/personal_js.go +++ /dev/null @@ -1,51 +0,0 @@ -// Copyright 2015 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library 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 Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see . - -package api - -const Personal_JS = ` -web3._extend({ - property: 'personal', - methods: - [ - new web3._extend.Method({ - name: 'newAccount', - call: 'personal_newAccount', - params: 1, - inputFormatter: [null], - outputFormatter: web3._extend.utils.toAddress - }), - new web3._extend.Method({ - name: 'unlockAccount', - call: 'personal_unlockAccount', - params: 3, - inputFormatter: [null, null, null] - }), - new web3._extend.Method({ - name: 'lockAccount', - call: 'personal_lockAccount', - params: 1 - }) - ], - properties: - [ - new web3._extend.Property({ - name: 'listAccounts', - getter: 'personal_listAccounts' - }) - ] -}); -` diff --git a/rpc/api/shh.go b/rpc/api/shh.go deleted file mode 100644 index 60e805605..000000000 --- a/rpc/api/shh.go +++ /dev/null @@ -1,196 +0,0 @@ -// Copyright 2015 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library 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 Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see . - -package api - -import ( - "math/big" - - "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" -) - -const ( - ShhApiVersion = "1.0" -) - -var ( - // mapping between methods and handlers - shhMapping = map[string]shhhandler{ - "shh_version": (*shhApi).Version, - "shh_post": (*shhApi).Post, - "shh_hasIdentity": (*shhApi).HasIdentity, - "shh_newIdentity": (*shhApi).NewIdentity, - "shh_newFilter": (*shhApi).NewFilter, - "shh_uninstallFilter": (*shhApi).UninstallFilter, - "shh_getMessages": (*shhApi).GetMessages, - "shh_getFilterChanges": (*shhApi).GetFilterChanges, - } -) - -func newWhisperOfflineError(method string) error { - return shared.NewNotAvailableError(method, "whisper offline") -} - -// net callback handler -type shhhandler func(*shhApi, *shared.Request) (interface{}, error) - -// shh api provider -type shhApi struct { - xeth *xeth.XEth - ethereum *eth.Ethereum - methods map[string]shhhandler - codec codec.ApiCoder -} - -// create a new whisper api instance -func NewShhApi(xeth *xeth.XEth, eth *eth.Ethereum, coder codec.Codec) *shhApi { - return &shhApi{ - xeth: xeth, - ethereum: eth, - methods: shhMapping, - codec: coder.New(nil), - } -} - -// collection with supported methods -func (self *shhApi) Methods() []string { - methods := make([]string, len(self.methods)) - i := 0 - for k := range self.methods { - methods[i] = k - i++ - } - return methods -} - -// Execute given request -func (self *shhApi) Execute(req *shared.Request) (interface{}, error) { - if callback, ok := self.methods[req.Method]; ok { - return callback(self, req) - } - - return nil, shared.NewNotImplementedError(req.Method) -} - -func (self *shhApi) Name() string { - return shared.ShhApiName -} - -func (self *shhApi) ApiVersion() string { - return ShhApiVersion -} - -func (self *shhApi) Version(req *shared.Request) (interface{}, error) { - w := self.xeth.Whisper() - if w == nil { - return nil, newWhisperOfflineError(req.Method) - } - - return w.Version(), nil -} - -func (self *shhApi) Post(req *shared.Request) (interface{}, error) { - w := self.xeth.Whisper() - if w == nil { - return nil, newWhisperOfflineError(req.Method) - } - - args := new(WhisperMessageArgs) - if err := self.codec.Decode(req.Params, &args); err != nil { - return nil, err - } - - err := w.Post(args.Payload, args.To, args.From, args.Topics, args.Priority, args.Ttl) - if err != nil { - return false, err - } - - return true, nil -} - -func (self *shhApi) HasIdentity(req *shared.Request) (interface{}, error) { - w := self.xeth.Whisper() - if w == nil { - return nil, newWhisperOfflineError(req.Method) - } - - args := new(WhisperIdentityArgs) - if err := self.codec.Decode(req.Params, &args); err != nil { - return nil, err - } - - return w.HasIdentity(args.Identity), nil -} - -func (self *shhApi) NewIdentity(req *shared.Request) (interface{}, error) { - w := self.xeth.Whisper() - if w == nil { - return nil, newWhisperOfflineError(req.Method) - } - - return w.NewIdentity(), nil -} - -func (self *shhApi) NewFilter(req *shared.Request) (interface{}, error) { - args := new(WhisperFilterArgs) - if err := self.codec.Decode(req.Params, &args); err != nil { - return nil, err - } - - id := self.xeth.NewWhisperFilter(args.To, args.From, args.Topics) - return newHexNum(big.NewInt(int64(id)).Bytes()), nil -} - -func (self *shhApi) UninstallFilter(req *shared.Request) (interface{}, error) { - args := new(FilterIdArgs) - if err := self.codec.Decode(req.Params, &args); err != nil { - return nil, err - } - return self.xeth.UninstallWhisperFilter(args.Id), nil -} - -func (self *shhApi) GetFilterChanges(req *shared.Request) (interface{}, error) { - w := self.xeth.Whisper() - if w == nil { - return nil, newWhisperOfflineError(req.Method) - } - - // Retrieve all the new messages arrived since the last request - args := new(FilterIdArgs) - if err := self.codec.Decode(req.Params, &args); err != nil { - return nil, err - } - - return self.xeth.WhisperMessagesChanged(args.Id), nil -} - -func (self *shhApi) GetMessages(req *shared.Request) (interface{}, error) { - w := self.xeth.Whisper() - if w == nil { - return nil, newWhisperOfflineError(req.Method) - } - - // Retrieve all the cached messages matching a specific, existing filter - args := new(FilterIdArgs) - if err := self.codec.Decode(req.Params, &args); err != nil { - return nil, err - } - - return self.xeth.WhisperMessages(args.Id), nil -} diff --git a/rpc/api/shh_args.go b/rpc/api/shh_args.go deleted file mode 100644 index 468a0b98f..000000000 --- a/rpc/api/shh_args.go +++ /dev/null @@ -1,174 +0,0 @@ -// Copyright 2015 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library 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 Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see . - -package api - -import ( - "encoding/json" - "fmt" - "math/big" - - "github.com/ethereum/go-ethereum/rpc/shared" -) - -type WhisperMessageArgs struct { - Payload string - To string - From string - Topics []string - Priority uint32 - Ttl uint32 -} - -func (args *WhisperMessageArgs) UnmarshalJSON(b []byte) (err error) { - var obj []struct { - Payload string - To string - From string - Topics []string - Priority interface{} - Ttl interface{} - } - - if err = json.Unmarshal(b, &obj); err != nil { - return shared.NewDecodeParamError(err.Error()) - } - - if len(obj) < 1 { - return shared.NewInsufficientParamsError(len(obj), 1) - } - args.Payload = obj[0].Payload - args.To = obj[0].To - args.From = obj[0].From - args.Topics = obj[0].Topics - - var num *big.Int - if num, err = numString(obj[0].Priority); err != nil { - return err - } - args.Priority = uint32(num.Int64()) - - if num, err = numString(obj[0].Ttl); err != nil { - return err - } - args.Ttl = uint32(num.Int64()) - - return nil -} - -type WhisperIdentityArgs struct { - Identity string -} - -func (args *WhisperIdentityArgs) 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) - } - - argstr, ok := obj[0].(string) - if !ok { - return shared.NewInvalidTypeError("arg0", "not a string") - } - - args.Identity = argstr - - return nil -} - -type WhisperFilterArgs struct { - To string - From string - Topics [][]string -} - -// UnmarshalJSON implements the json.Unmarshaler interface, invoked to convert a -// JSON message blob into a WhisperFilterArgs structure. -func (args *WhisperFilterArgs) UnmarshalJSON(b []byte) (err error) { - // Unmarshal the JSON message and sanity check - var obj []struct { - To interface{} `json:"to"` - From interface{} `json:"from"` - Topics interface{} `json:"topics"` - } - if err := json.Unmarshal(b, &obj); err != nil { - return shared.NewDecodeParamError(err.Error()) - } - if len(obj) < 1 { - return shared.NewInsufficientParamsError(len(obj), 1) - } - // Retrieve the simple data contents of the filter arguments - if obj[0].To == nil { - args.To = "" - } else { - argstr, ok := obj[0].To.(string) - if !ok { - return shared.NewInvalidTypeError("to", "is not a string") - } - args.To = argstr - } - if obj[0].From == nil { - args.From = "" - } else { - argstr, ok := obj[0].From.(string) - if !ok { - return shared.NewInvalidTypeError("from", "is not a string") - } - args.From = argstr - } - // Construct the nested topic array - if obj[0].Topics != nil { - // Make sure we have an actual topic array - list, ok := obj[0].Topics.([]interface{}) - if !ok { - return shared.NewInvalidTypeError("topics", "is not an array") - } - // Iterate over each topic and handle nil, string or array - topics := make([][]string, len(list)) - for idx, field := range list { - switch value := field.(type) { - case nil: - topics[idx] = []string{} - - case string: - topics[idx] = []string{value} - - case []interface{}: - topics[idx] = make([]string, len(value)) - for i, nested := range value { - switch value := nested.(type) { - case nil: - topics[idx][i] = "" - - case string: - topics[idx][i] = value - - default: - return shared.NewInvalidTypeError(fmt.Sprintf("topic[%d][%d]", idx, i), "is not a string") - } - } - default: - return shared.NewInvalidTypeError(fmt.Sprintf("topic[%d]", idx), "not a string or array") - } - } - args.Topics = topics - } - return nil -} diff --git a/rpc/api/shh_js.go b/rpc/api/shh_js.go deleted file mode 100644 index a92ad1644..000000000 --- a/rpc/api/shh_js.go +++ /dev/null @@ -1,34 +0,0 @@ -// Copyright 2015 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library 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 Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see . - -package api - -const Shh_JS = ` -web3._extend({ - property: 'shh', - methods: - [ - - ], - properties: - [ - new web3._extend.Property({ - name: 'version', - getter: 'shh_version' - }) - ] -}); -` diff --git a/rpc/api/txpool.go b/rpc/api/txpool.go deleted file mode 100644 index 27e40cae5..000000000 --- a/rpc/api/txpool.go +++ /dev/null @@ -1,92 +0,0 @@ -// Copyright 2015 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library 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 Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see . - -package api - -import ( - "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" -) - -const ( - TxPoolApiVersion = "1.0" -) - -var ( - // mapping between methods and handlers - txpoolMapping = map[string]txpoolhandler{ - "txpool_status": (*txPoolApi).Status, - } -) - -// net callback handler -type txpoolhandler func(*txPoolApi, *shared.Request) (interface{}, error) - -// txpool api provider -type txPoolApi struct { - xeth *xeth.XEth - ethereum *eth.Ethereum - methods map[string]txpoolhandler - codec codec.ApiCoder -} - -// create a new txpool api instance -func NewTxPoolApi(xeth *xeth.XEth, eth *eth.Ethereum, coder codec.Codec) *txPoolApi { - return &txPoolApi{ - xeth: xeth, - ethereum: eth, - methods: txpoolMapping, - codec: coder.New(nil), - } -} - -// collection with supported methods -func (self *txPoolApi) Methods() []string { - methods := make([]string, len(self.methods)) - i := 0 - for k := range self.methods { - methods[i] = k - i++ - } - return methods -} - -// Execute given request -func (self *txPoolApi) Execute(req *shared.Request) (interface{}, error) { - if callback, ok := self.methods[req.Method]; ok { - return callback(self, req) - } - - return nil, shared.NewNotImplementedError(req.Method) -} - -func (self *txPoolApi) Name() string { - return shared.TxPoolApiName -} - -func (self *txPoolApi) ApiVersion() string { - return TxPoolApiVersion -} - -func (self *txPoolApi) Status(req *shared.Request) (interface{}, error) { - pending, queue := self.ethereum.TxPool().Stats() - return map[string]int{ - "pending": pending, - "queued": queue, - }, nil -} diff --git a/rpc/api/txpool_js.go b/rpc/api/txpool_js.go deleted file mode 100644 index b6c29871a..000000000 --- a/rpc/api/txpool_js.go +++ /dev/null @@ -1,33 +0,0 @@ -// Copyright 2015 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library 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 Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see . - -package api - -const TxPool_JS = ` -web3._extend({ - property: 'txpool', - methods: - [ - ], - properties: - [ - new web3._extend.Property({ - name: 'status', - getter: 'txpool_status' - }) - ] -}); -` diff --git a/rpc/api/utils.go b/rpc/api/utils.go deleted file mode 100644 index 794b6abee..000000000 --- a/rpc/api/utils.go +++ /dev/null @@ -1,226 +0,0 @@ -// Copyright 2015 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library 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 Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see . - -package api - -import ( - "strings" - - "fmt" - - "github.com/ethereum/go-ethereum/eth" - "github.com/ethereum/go-ethereum/node" - "github.com/ethereum/go-ethereum/rpc/codec" - "github.com/ethereum/go-ethereum/rpc/shared" - "github.com/ethereum/go-ethereum/xeth" -) - -var ( - // Mapping between the different methods each api supports - AutoCompletion = map[string][]string{ - "admin": []string{ - "addPeer", - "datadir", - "enableUserAgent", - "exportChain", - "getContractInfo", - "httpGet", - "importChain", - "nodeInfo", - "peers", - "register", - "registerUrl", - "saveInfo", - "setGlobalRegistrar", - "setHashReg", - "setUrlHint", - "setSolc", - "sleep", - "sleepBlocks", - "startNatSpec", - "startRPC", - "stopNatSpec", - "stopRPC", - "verbosity", - }, - "db": []string{ - "getString", - "putString", - "getHex", - "putHex", - }, - "debug": []string{ - "dumpBlock", - "getBlockRlp", - "metrics", - "printBlock", - "processBlock", - "seedHash", - "setHead", - }, - "eth": []string{ - "accounts", - "blockNumber", - "call", - "contract", - "coinbase", - "compile.lll", - "compile.serpent", - "compile.solidity", - "contract", - "defaultAccount", - "defaultBlock", - "estimateGas", - "filter", - "getBalance", - "getBlock", - "getBlockTransactionCount", - "getBlockUncleCount", - "getCode", - "getNatSpec", - "getCompilers", - "gasPrice", - "getStorageAt", - "getTransaction", - "getTransactionCount", - "getTransactionFromBlock", - "getTransactionReceipt", - "getUncle", - "hashrate", - "mining", - "namereg", - "pendingTransactions", - "resend", - "sendRawTransaction", - "sendTransaction", - "sign", - "syncing", - }, - "miner": []string{ - "hashrate", - "makeDAG", - "setEtherbase", - "setExtra", - "setGasPrice", - "startAutoDAG", - "start", - "stopAutoDAG", - "stop", - }, - "net": []string{ - "peerCount", - "listening", - }, - "personal": []string{ - "listAccounts", - "newAccount", - "unlockAccount", - }, - "shh": []string{ - "post", - "newIdentity", - "hasIdentity", - "newGroup", - "addToGroup", - "filter", - }, - "txpool": []string{ - "status", - }, - "web3": []string{ - "sha3", - "version", - "fromWei", - "toWei", - "toHex", - "toAscii", - "fromAscii", - "toBigNumber", - "isAddress", - }, - } -) - -// Parse a comma separated API string to individual api's -func ParseApiString(apistr string, codec codec.Codec, xeth *xeth.XEth, stack *node.Node) ([]shared.EthereumApi, error) { - if len(strings.TrimSpace(apistr)) == 0 { - return nil, fmt.Errorf("Empty apistr provided") - } - - names := strings.Split(apistr, ",") - apis := make([]shared.EthereumApi, len(names)) - - var eth *eth.Ethereum - if stack != nil { - if err := stack.Service(ð); err != nil { - return nil, err - } - } - for i, name := range names { - switch strings.ToLower(strings.TrimSpace(name)) { - case shared.AdminApiName: - apis[i] = NewAdminApi(xeth, stack, codec) - case shared.DebugApiName: - apis[i] = NewDebugApi(xeth, eth, codec) - case shared.DbApiName: - apis[i] = NewDbApi(xeth, eth, codec) - case shared.EthApiName: - apis[i] = NewEthApi(xeth, eth, codec) - case shared.MinerApiName: - apis[i] = NewMinerApi(eth, codec) - case shared.NetApiName: - apis[i] = NewNetApi(xeth, eth, codec) - case shared.ShhApiName: - apis[i] = NewShhApi(xeth, eth, codec) - case shared.TxPoolApiName: - apis[i] = NewTxPoolApi(xeth, eth, codec) - case shared.PersonalApiName: - apis[i] = NewPersonalApi(xeth, eth, codec) - case shared.Web3ApiName: - apis[i] = NewWeb3Api(xeth, codec) - case "rpc": // gives information about the RPC interface - continue - default: - return nil, fmt.Errorf("Unknown API '%s'", name) - } - } - return apis, nil -} - -func Javascript(name string) string { - switch strings.ToLower(strings.TrimSpace(name)) { - case shared.AdminApiName: - return Admin_JS - case shared.DebugApiName: - return Debug_JS - case shared.DbApiName: - return Db_JS - case shared.EthApiName: - return Eth_JS - case shared.MinerApiName: - return Miner_JS - case shared.NetApiName: - return Net_JS - case shared.ShhApiName: - return Shh_JS - case shared.TxPoolApiName: - return TxPool_JS - case shared.PersonalApiName: - return Personal_JS - } - - return "" -} diff --git a/rpc/api/web3.go b/rpc/api/web3.go deleted file mode 100644 index e2d8543d3..000000000 --- a/rpc/api/web3.go +++ /dev/null @@ -1,99 +0,0 @@ -// Copyright 2015 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library 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 Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see . - -package api - -import ( - "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/crypto" - "github.com/ethereum/go-ethereum/rpc/codec" - "github.com/ethereum/go-ethereum/rpc/shared" - "github.com/ethereum/go-ethereum/xeth" -) - -const ( - Web3ApiVersion = "1.0" -) - -var ( - // mapping between methods and handlers - Web3Mapping = map[string]web3handler{ - "web3_sha3": (*web3Api).Sha3, - "web3_clientVersion": (*web3Api).ClientVersion, - } -) - -// web3 callback handler -type web3handler func(*web3Api, *shared.Request) (interface{}, error) - -// web3 api provider -type web3Api struct { - xeth *xeth.XEth - methods map[string]web3handler - codec codec.ApiCoder -} - -// create a new web3 api instance -func NewWeb3Api(xeth *xeth.XEth, coder codec.Codec) *web3Api { - return &web3Api{ - xeth: xeth, - methods: Web3Mapping, - codec: coder.New(nil), - } -} - -// collection with supported methods -func (self *web3Api) Methods() []string { - methods := make([]string, len(self.methods)) - i := 0 - for k := range self.methods { - methods[i] = k - i++ - } - return methods -} - -// Execute given request -func (self *web3Api) Execute(req *shared.Request) (interface{}, error) { - if callback, ok := self.methods[req.Method]; ok { - return callback(self, req) - } - - return nil, &shared.NotImplementedError{req.Method} -} - -func (self *web3Api) Name() string { - return shared.Web3ApiName -} - -func (self *web3Api) ApiVersion() string { - return Web3ApiVersion -} - -// Calculates the sha3 over req.Params.Data -func (self *web3Api) Sha3(req *shared.Request) (interface{}, error) { - args := new(Sha3Args) - if err := self.codec.Decode(req.Params, &args); err != nil { - return nil, err - } - - return common.ToHex(crypto.Sha3(common.FromHex(args.Data))), nil -} - -// returns the xeth client vrsion -func (self *web3Api) ClientVersion(req *shared.Request) (interface{}, error) { - return self.xeth.ClientVersion(), nil -} diff --git a/rpc/api/web3_args.go b/rpc/api/web3_args.go deleted file mode 100644 index 9e39f7130..000000000 --- a/rpc/api/web3_args.go +++ /dev/null @@ -1,45 +0,0 @@ -// Copyright 2015 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library 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 Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see . - -package api - -import ( - "encoding/json" - - "github.com/ethereum/go-ethereum/rpc/shared" -) - -type Sha3Args struct { - Data string -} - -func (args *Sha3Args) 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) - } - - argstr, ok := obj[0].(string) - if !ok { - return shared.NewInvalidTypeError("data", "is not a string") - } - args.Data = argstr - return nil -} diff --git a/rpc/codec/codec.go b/rpc/codec/codec.go deleted file mode 100644 index 786080b44..000000000 --- a/rpc/codec/codec.go +++ /dev/null @@ -1,65 +0,0 @@ -// Copyright 2015 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library 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 Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see . - -package codec - -import ( - "net" - "strconv" - - "github.com/ethereum/go-ethereum/rpc/shared" -) - -type Codec int - -// (de)serialization support for rpc interface -type ApiCoder interface { - // Parse message to request from underlying stream - ReadRequest() ([]*shared.Request, bool, error) - // Parse response message from underlying stream - ReadResponse() (interface{}, error) - // Read raw message from underlying stream - Recv() (interface{}, error) - // Encode response to encoded form in underlying stream - WriteResponse(interface{}) error - // Decode single message from data - Decode([]byte, interface{}) error - // Encode msg to encoded form - Encode(msg interface{}) ([]byte, error) - // close the underlying stream - Close() -} - -// supported codecs -const ( - JSON Codec = iota - nCodecs -) - -var ( - // collection with supported coders - coders = make([]func(net.Conn) ApiCoder, nCodecs) -) - -// create a new coder instance -func (c Codec) New(conn net.Conn) ApiCoder { - switch c { - case JSON: - return NewJsonCoder(conn) - } - - panic("codec: request for codec #" + strconv.Itoa(int(c)) + " is unavailable") -} diff --git a/rpc/codec/json.go b/rpc/codec/json.go deleted file mode 100644 index cfc449143..000000000 --- a/rpc/codec/json.go +++ /dev/null @@ -1,149 +0,0 @@ -// Copyright 2015 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library 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 Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see . - -package codec - -import ( - "encoding/json" - "fmt" - "net" - "time" - "strings" - - "github.com/ethereum/go-ethereum/rpc/shared" -) - -const ( - READ_TIMEOUT = 60 // in seconds - MAX_REQUEST_SIZE = 1024 * 1024 - MAX_RESPONSE_SIZE = 1024 * 1024 -) - -// Json serialization support -type JsonCodec struct { - c net.Conn - d *json.Decoder -} - -// Create new JSON coder instance -func NewJsonCoder(conn net.Conn) ApiCoder { - return &JsonCodec{ - c: conn, - d: json.NewDecoder(conn), - } -} - -// Read incoming request and parse it to RPC request -func (self *JsonCodec) ReadRequest() (requests []*shared.Request, isBatch bool, err error) { - deadline := time.Now().Add(READ_TIMEOUT * time.Second) - if err := self.c.SetDeadline(deadline); err != nil { - return nil, false, err - } - - var incoming json.RawMessage - err = self.d.Decode(&incoming) - if err == nil { - isBatch = incoming[0] == '[' - if isBatch { - requests = make([]*shared.Request, 0) - err = json.Unmarshal(incoming, &requests) - } else { - requests = make([]*shared.Request, 1) - var singleRequest shared.Request - if err = json.Unmarshal(incoming, &singleRequest); err == nil { - requests[0] = &singleRequest - } - } - return - } - - self.c.Close() - return nil, false, err -} - -func (self *JsonCodec) Recv() (interface{}, error) { - var msg json.RawMessage - err := self.d.Decode(&msg) - if err != nil { - self.c.Close() - return nil, err - } - - return msg, err -} - -func (self *JsonCodec) ReadResponse() (interface{}, error) { - in, err := self.Recv() - if err != nil { - return nil, err - } - - if msg, ok := in.(json.RawMessage); ok { - var req *shared.Request - if err = json.Unmarshal(msg, &req); err == nil && strings.HasPrefix(req.Method, "agent_") { - return req, nil - } - - var failure *shared.ErrorResponse - if err = json.Unmarshal(msg, &failure); err == nil && failure.Error != nil { - return failure, fmt.Errorf(failure.Error.Message) - } - - var success *shared.SuccessResponse - if err = json.Unmarshal(msg, &success); err == nil { - return success, nil - } - } - - return in, err -} - -// Decode data -func (self *JsonCodec) Decode(data []byte, msg interface{}) error { - return json.Unmarshal(data, msg) -} - -// Encode message -func (self *JsonCodec) Encode(msg interface{}) ([]byte, error) { - return json.Marshal(msg) -} - -// Parse JSON data from conn to obj -func (self *JsonCodec) WriteResponse(res interface{}) error { - data, err := json.Marshal(res) - if err != nil { - self.c.Close() - return err - } - - bytesWritten := 0 - - for bytesWritten < len(data) { - n, err := self.c.Write(data[bytesWritten:]) - if err != nil { - self.c.Close() - return err - } - bytesWritten += n - } - - return nil -} - -// Close decoder and encoder -func (self *JsonCodec) Close() { - self.c.Close() -} diff --git a/rpc/codec/json_test.go b/rpc/codec/json_test.go deleted file mode 100644 index 01ef77e57..000000000 --- a/rpc/codec/json_test.go +++ /dev/null @@ -1,157 +0,0 @@ -// Copyright 2015 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library 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 Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see . - -package codec - -import ( - "bytes" - "io" - "net" - "testing" - "time" -) - -type jsonTestConn struct { - buffer *bytes.Buffer -} - -func newJsonTestConn(data []byte) *jsonTestConn { - return &jsonTestConn{ - buffer: bytes.NewBuffer(data), - } -} - -func (self *jsonTestConn) Read(p []byte) (n int, err error) { - return self.buffer.Read(p) -} - -func (self *jsonTestConn) Write(p []byte) (n int, err error) { - return self.buffer.Write(p) -} - -func (self *jsonTestConn) Close() error { - // not implemented - return nil -} - -func (self *jsonTestConn) LocalAddr() net.Addr { - // not implemented - return nil -} - -func (self *jsonTestConn) RemoteAddr() net.Addr { - // not implemented - return nil -} - -func (self *jsonTestConn) SetDeadline(t time.Time) error { - return nil -} - -func (self *jsonTestConn) SetReadDeadline(t time.Time) error { - return nil -} - -func (self *jsonTestConn) SetWriteDeadline(t time.Time) error { - return nil -} - -func TestJsonDecoderWithValidRequest(t *testing.T) { - reqdata := []byte(`{"jsonrpc":"2.0","method":"modules","params":[],"id":64}`) - decoder := newJsonTestConn(reqdata) - - jsonDecoder := NewJsonCoder(decoder) - requests, batch, err := jsonDecoder.ReadRequest() - - if err != nil { - t.Errorf("Read valid request failed - %v", err) - } - - if len(requests) != 1 { - t.Errorf("Expected to get a single request but got %d", len(requests)) - } - - if batch { - t.Errorf("Got batch indication while expecting single request") - } - - if requests[0].Id != float64(64) { - t.Errorf("Expected req.Id == 64 but got %v", requests[0].Id) - } - - if requests[0].Method != "modules" { - t.Errorf("Expected req.Method == 'modules' got '%s'", requests[0].Method) - } -} - -func TestJsonDecoderWithValidBatchRequest(t *testing.T) { - reqdata := []byte(`[{"jsonrpc":"2.0","method":"modules","params":[],"id":64}, - {"jsonrpc":"2.0","method":"modules","params":[],"id":64}]`) - decoder := newJsonTestConn(reqdata) - - jsonDecoder := NewJsonCoder(decoder) - requests, batch, err := jsonDecoder.ReadRequest() - - if err != nil { - t.Errorf("Read valid batch request failed - %v", err) - } - - if len(requests) != 2 { - t.Errorf("Expected to get two requests but got %d", len(requests)) - } - - if !batch { - t.Errorf("Got no batch indication while expecting batch request") - } - - for i := 0; i < len(requests); i++ { - if requests[i].Id != float64(64) { - t.Errorf("Expected req.Id == 64 but got %v", requests[i].Id) - } - - if requests[i].Method != "modules" { - t.Errorf("Expected req.Method == 'modules' got '%s'", requests[i].Method) - } - } -} - -func TestJsonDecoderWithInvalidIncompleteMessage(t *testing.T) { - reqdata := []byte(`{"jsonrpc":"2.0","method":"modules","pa`) - decoder := newJsonTestConn(reqdata) - - jsonDecoder := NewJsonCoder(decoder) - requests, batch, err := jsonDecoder.ReadRequest() - - if err != io.ErrUnexpectedEOF { - t.Errorf("Expected to read an incomplete request err but got %v", err) - } - - // remaining message - decoder.Write([]byte(`rams":[],"id:64"}`)) - requests, batch, err = jsonDecoder.ReadRequest() - - if err == nil { - t.Errorf("Expected an error but got nil") - } - - if len(requests) != 0 { - t.Errorf("Expected to get no requests but got %d", len(requests)) - } - - if batch { - t.Errorf("Got batch indication while expecting non batch") - } -} diff --git a/rpc/comms/comms.go b/rpc/comms/comms.go deleted file mode 100644 index 61fba5722..000000000 --- a/rpc/comms/comms.go +++ /dev/null @@ -1,150 +0,0 @@ -// Copyright 2015 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library 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 Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see . - -package comms - -import ( - "io" - "net" - - "fmt" - "strings" - - "strconv" - - "github.com/ethereum/go-ethereum/logger" - "github.com/ethereum/go-ethereum/logger/glog" - "github.com/ethereum/go-ethereum/rpc/codec" - "github.com/ethereum/go-ethereum/rpc/shared" -) - -const ( - maxHttpSizeReqLength = 1024 * 1024 // 1MB -) - -var ( - // List with all API's which are offered over the in proc interface by default - DefaultInProcApis = shared.AllApis - - // List with all API's which are offered over the IPC interface by default - DefaultIpcApis = shared.AllApis - - // List with API's which are offered over thr HTTP/RPC interface by default - DefaultHttpRpcApis = strings.Join([]string{ - shared.DbApiName, shared.EthApiName, shared.NetApiName, shared.Web3ApiName, - }, ",") -) - -type EthereumClient interface { - // Close underlying connection - Close() - // Send request - Send(interface{}) error - // Receive response - Recv() (interface{}, error) - // List with modules this client supports - SupportedModules() (map[string]string, error) -} - -func handle(id int, conn net.Conn, api shared.EthereumApi, c codec.Codec) { - codec := c.New(conn) - - defer func() { - if r := recover(); r != nil { - glog.Errorf("panic: %v\n", r) - } - codec.Close() - }() - - for { - requests, isBatch, err := codec.ReadRequest() - if err == io.EOF { - return - } else if err != nil { - glog.V(logger.Debug).Infof("Closed IPC Conn %06d recv err - %v\n", id, err) - return - } - - if isBatch { - responses := make([]*interface{}, len(requests)) - responseCount := 0 - for _, req := range requests { - res, err := api.Execute(req) - if req.Id != nil { - rpcResponse := shared.NewRpcResponse(req.Id, req.Jsonrpc, res, err) - responses[responseCount] = rpcResponse - responseCount += 1 - } - } - - err = codec.WriteResponse(responses[:responseCount]) - if err != nil { - glog.V(logger.Debug).Infof("Closed IPC Conn %06d send err - %v\n", id, err) - return - } - } else { - var rpcResponse interface{} - res, err := api.Execute(requests[0]) - - rpcResponse = shared.NewRpcResponse(requests[0].Id, requests[0].Jsonrpc, res, err) - err = codec.WriteResponse(rpcResponse) - if err != nil { - glog.V(logger.Debug).Infof("Closed IPC Conn %06d send err - %v\n", id, err) - return - } - } - } -} - -// Endpoint must be in the form of: -// ${protocol}:${path} -// e.g. ipc:/tmp/geth.ipc -// rpc:localhost:8545 -func ClientFromEndpoint(endpoint string, c codec.Codec) (EthereumClient, error) { - if strings.HasPrefix(endpoint, "ipc:") { - cfg := IpcConfig{ - Endpoint: endpoint[4:], - } - return NewIpcClient(cfg, codec.JSON) - } - - if strings.HasPrefix(endpoint, "rpc:") { - parts := strings.Split(endpoint, ":") - addr := "http://localhost" - port := uint(8545) - if len(parts) >= 3 { - addr = parts[1] + ":" + parts[2] - } - - if len(parts) >= 4 { - p, err := strconv.Atoi(parts[3]) - - if err != nil { - return nil, err - } - port = uint(p) - } - - cfg := HttpConfig{ - ListenAddress: addr, - ListenPort: port, - } - - return NewHttpClient(cfg, codec.JSON), nil - } - - return nil, fmt.Errorf("Invalid endpoint") -} diff --git a/rpc/comms/http.go b/rpc/comms/http.go deleted file mode 100644 index f4a930d0e..000000000 --- a/rpc/comms/http.go +++ /dev/null @@ -1,345 +0,0 @@ -// Copyright 2015 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library 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 Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see . - -package comms - -import ( - "encoding/json" - "fmt" - "net" - "net/http" - "strings" - "sync" - "time" - - "bytes" - "io" - "io/ioutil" - - "github.com/ethereum/go-ethereum/logger" - "github.com/ethereum/go-ethereum/logger/glog" - "github.com/ethereum/go-ethereum/rpc/codec" - "github.com/ethereum/go-ethereum/rpc/shared" - "github.com/rs/cors" -) - -const ( - serverIdleTimeout = 10 * time.Second // idle keep-alive connections - serverReadTimeout = 15 * time.Second // per-request read timeout - serverWriteTimeout = 15 * time.Second // per-request read timeout -) - -var ( - httpServerMu sync.Mutex - httpServer *stopServer -) - -type HttpConfig struct { - ListenAddress string - ListenPort uint - CorsDomain string -} - -// stopServer augments http.Server with idle connection tracking. -// Idle keep-alive connections are shut down when Close is called. -type stopServer struct { - *http.Server - l net.Listener - // connection tracking state - mu sync.Mutex - shutdown bool // true when Stop has returned - idle map[net.Conn]struct{} -} - -type handler struct { - codec codec.Codec - api shared.EthereumApi -} - -// StartHTTP starts listening for RPC requests sent via HTTP. -func StartHttp(cfg HttpConfig, codec codec.Codec, api shared.EthereumApi) error { - httpServerMu.Lock() - defer httpServerMu.Unlock() - - addr := fmt.Sprintf("%s:%d", cfg.ListenAddress, cfg.ListenPort) - if httpServer != nil { - if addr != httpServer.Addr { - return fmt.Errorf("RPC service already running on %s ", httpServer.Addr) - } - return nil // RPC service already running on given host/port - } - // Set up the request handler, wrapping it with CORS headers if configured. - handler := http.Handler(&handler{codec, api}) - if len(cfg.CorsDomain) > 0 { - opts := cors.Options{ - AllowedMethods: []string{"POST"}, - AllowedOrigins: strings.Split(cfg.CorsDomain, " "), - } - handler = cors.New(opts).Handler(handler) - } - // Start the server. - s, err := listenHTTP(addr, handler) - if err != nil { - glog.V(logger.Error).Infof("Can't listen on %s:%d: %v", cfg.ListenAddress, cfg.ListenPort, err) - return err - } - httpServer = s - return nil -} - -func (h *handler) ServeHTTP(w http.ResponseWriter, req *http.Request) { - w.Header().Set("Content-Type", "application/json") - - // Limit request size to resist DoS - if req.ContentLength > maxHttpSizeReqLength { - err := fmt.Errorf("Request too large") - response := shared.NewRpcErrorResponse(-1, shared.JsonRpcVersion, -32700, err) - sendJSON(w, &response) - return - } - - defer req.Body.Close() - payload, err := ioutil.ReadAll(req.Body) - if err != nil { - err := fmt.Errorf("Could not read request body") - response := shared.NewRpcErrorResponse(-1, shared.JsonRpcVersion, -32700, err) - sendJSON(w, &response) - return - } - - c := h.codec.New(nil) - var rpcReq shared.Request - if err = c.Decode(payload, &rpcReq); err == nil { - reply, err := h.api.Execute(&rpcReq) - res := shared.NewRpcResponse(rpcReq.Id, rpcReq.Jsonrpc, reply, err) - sendJSON(w, &res) - return - } - - var reqBatch []shared.Request - if err = c.Decode(payload, &reqBatch); err == nil { - resBatch := make([]*interface{}, len(reqBatch)) - resCount := 0 - for i, rpcReq := range reqBatch { - reply, err := h.api.Execute(&rpcReq) - if rpcReq.Id != nil { // this leaves nil entries in the response batch for later removal - resBatch[i] = shared.NewRpcResponse(rpcReq.Id, rpcReq.Jsonrpc, reply, err) - resCount += 1 - } - } - // make response omitting nil entries - sendJSON(w, resBatch[:resCount]) - return - } - - // invalid request - err = fmt.Errorf("Could not decode request") - res := shared.NewRpcErrorResponse(-1, shared.JsonRpcVersion, -32600, err) - sendJSON(w, res) -} - -func sendJSON(w io.Writer, v interface{}) { - if glog.V(logger.Detail) { - if payload, err := json.MarshalIndent(v, "", "\t"); err == nil { - glog.Infof("Sending payload: %s", payload) - } - } - if err := json.NewEncoder(w).Encode(v); err != nil { - glog.V(logger.Error).Infoln("Error sending JSON:", err) - } -} - -// Stop closes all active HTTP connections and shuts down the server. -func StopHttp() { - httpServerMu.Lock() - defer httpServerMu.Unlock() - if httpServer != nil { - httpServer.Close() - httpServer = nil - } -} - -func listenHTTP(addr string, h http.Handler) (*stopServer, error) { - l, err := net.Listen("tcp", addr) - if err != nil { - return nil, err - } - s := &stopServer{l: l, idle: make(map[net.Conn]struct{})} - s.Server = &http.Server{ - Addr: addr, - Handler: h, - ReadTimeout: serverReadTimeout, - WriteTimeout: serverWriteTimeout, - ConnState: s.connState, - } - go s.Serve(l) - return s, nil -} - -func (s *stopServer) connState(c net.Conn, state http.ConnState) { - s.mu.Lock() - defer s.mu.Unlock() - // Close c immediately if we're past shutdown. - if s.shutdown { - if state != http.StateClosed { - c.Close() - } - return - } - if state == http.StateIdle { - s.idle[c] = struct{}{} - } else { - delete(s.idle, c) - } -} - -func (s *stopServer) Close() { - s.mu.Lock() - defer s.mu.Unlock() - // Shut down the acceptor. No new connections can be created. - s.l.Close() - // Drop all idle connections. Non-idle connections will be - // closed by connState as soon as they become idle. - s.shutdown = true - for c := range s.idle { - glog.V(logger.Detail).Infof("closing idle connection %v", c.RemoteAddr()) - c.Close() - delete(s.idle, c) - } -} - -type httpClient struct { - address string - port uint - codec codec.ApiCoder - lastRes interface{} - lastErr error -} - -// Create a new in process client -func NewHttpClient(cfg HttpConfig, c codec.Codec) *httpClient { - return &httpClient{ - address: cfg.ListenAddress, - port: cfg.ListenPort, - codec: c.New(nil), - } -} - -func (self *httpClient) Close() { - // do nothing -} - -func (self *httpClient) Send(req interface{}) error { - var body []byte - var err error - - self.lastRes = nil - self.lastErr = nil - - if body, err = self.codec.Encode(req); err != nil { - return err - } - - httpReq, err := http.NewRequest("POST", fmt.Sprintf("%s:%d", self.address, self.port), bytes.NewBuffer(body)) - if err != nil { - return err - } - httpReq.Header.Set("Content-Type", "application/json") - - client := http.Client{} - resp, err := client.Do(httpReq) - if err != nil { - return err - } - - defer resp.Body.Close() - - if resp.Status == "200 OK" { - reply, _ := ioutil.ReadAll(resp.Body) - var rpcSuccessResponse shared.SuccessResponse - if err = self.codec.Decode(reply, &rpcSuccessResponse); err == nil { - self.lastRes = &rpcSuccessResponse - self.lastErr = err - return nil - } else { - var rpcErrorResponse shared.ErrorResponse - if err = self.codec.Decode(reply, &rpcErrorResponse); err == nil { - self.lastRes = &rpcErrorResponse - self.lastErr = err - return nil - } else { - return err - } - } - } - - return fmt.Errorf("Not implemented") -} - -func (self *httpClient) Recv() (interface{}, error) { - return self.lastRes, self.lastErr -} - -func (self *httpClient) SupportedModules() (map[string]string, error) { - var body []byte - var err error - - payload := shared.Request{ - Id: 1, - Jsonrpc: "2.0", - Method: "modules", - } - - if body, err = self.codec.Encode(payload); err != nil { - return nil, err - } - - req, err := http.NewRequest("POST", fmt.Sprintf("%s:%d", self.address, self.port), bytes.NewBuffer(body)) - if err != nil { - return nil, err - } - req.Header.Set("Content-Type", "application/json") - - client := http.Client{} - resp, err := client.Do(req) - if err != nil { - return nil, err - } - - defer resp.Body.Close() - - if resp.Status == "200 OK" { - reply, _ := ioutil.ReadAll(resp.Body) - var rpcRes shared.SuccessResponse - if err = self.codec.Decode(reply, &rpcRes); err != nil { - return nil, err - } - - result := make(map[string]string) - if modules, ok := rpcRes.Result.(map[string]interface{}); ok { - for a, v := range modules { - result[a] = fmt.Sprintf("%s", v) - } - return result, nil - } - err = fmt.Errorf("Unable to parse module response - %v", rpcRes.Result) - } else { - fmt.Printf("resp.Status = %s\n", resp.Status) - fmt.Printf("err = %v\n", err) - } - - return nil, err -} diff --git a/rpc/comms/inproc.go b/rpc/comms/inproc.go deleted file mode 100644 index e8058e32b..000000000 --- a/rpc/comms/inproc.go +++ /dev/null @@ -1,82 +0,0 @@ -// Copyright 2015 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library 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 Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see . - -package comms - -import ( - "fmt" - - "github.com/ethereum/go-ethereum/rpc/codec" - "github.com/ethereum/go-ethereum/rpc/shared" -) - -type InProcClient struct { - api shared.EthereumApi - codec codec.Codec - lastId interface{} - lastJsonrpc string - lastErr error - lastRes interface{} -} - -// Create a new in process client -func NewInProcClient(codec codec.Codec) *InProcClient { - return &InProcClient{ - codec: codec, - } -} - -func (self *InProcClient) Close() { - // do nothing -} - -// Need to setup api support -func (self *InProcClient) Initialize(offeredApi shared.EthereumApi) { - self.api = offeredApi -} - -func (self *InProcClient) Send(req interface{}) error { - if r, ok := req.(*shared.Request); ok { - self.lastId = r.Id - self.lastJsonrpc = r.Jsonrpc - self.lastRes, self.lastErr = self.api.Execute(r) - return self.lastErr - } - - return fmt.Errorf("Invalid request (%T)", req) -} - -func (self *InProcClient) Recv() (interface{}, error) { - return *shared.NewRpcResponse(self.lastId, self.lastJsonrpc, self.lastRes, self.lastErr), nil -} - -func (self *InProcClient) SupportedModules() (map[string]string, error) { - req := shared.Request{ - Id: 1, - Jsonrpc: "2.0", - Method: "modules", - } - - if res, err := self.api.Execute(&req); err == nil { - if result, ok := res.(map[string]string); ok { - return result, nil - } - } else { - return nil, err - } - - return nil, fmt.Errorf("Invalid response") -} diff --git a/rpc/comms/ipc.go b/rpc/comms/ipc.go deleted file mode 100644 index 3ba747b1d..000000000 --- a/rpc/comms/ipc.go +++ /dev/null @@ -1,158 +0,0 @@ -// Copyright 2015 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library 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 Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see . - -package comms - -import ( - "fmt" - "math/rand" - "net" - "os" - - "encoding/json" - - "github.com/ethereum/go-ethereum/logger" - "github.com/ethereum/go-ethereum/logger/glog" - "github.com/ethereum/go-ethereum/rpc/codec" - "github.com/ethereum/go-ethereum/rpc/shared" -) - -type Stopper interface { - Stop() -} - -type InitFunc func(conn net.Conn) (Stopper, shared.EthereumApi, error) - -type IpcConfig struct { - Endpoint string -} - -type ipcClient struct { - endpoint string - c net.Conn - codec codec.Codec - coder codec.ApiCoder -} - -func (self *ipcClient) Close() { - self.coder.Close() -} - -func (self *ipcClient) Send(msg interface{}) error { - var err error - if err = self.coder.WriteResponse(msg); err != nil { - if err = self.reconnect(); err == nil { - err = self.coder.WriteResponse(msg) - } - } - return err -} - -func (self *ipcClient) Recv() (interface{}, error) { - return self.coder.ReadResponse() -} - -func (self *ipcClient) SupportedModules() (map[string]string, error) { - req := shared.Request{ - Id: 1, - Jsonrpc: "2.0", - Method: "rpc_modules", - } - - if err := self.coder.WriteResponse(req); err != nil { - return nil, err - } - - res, _ := self.coder.ReadResponse() - if sucRes, ok := res.(*shared.SuccessResponse); ok { - data, _ := json.Marshal(sucRes.Result) - modules := make(map[string]string) - if err := json.Unmarshal(data, &modules); err == nil { - return modules, nil - } - } - - // old version uses modules instead of rpc_modules, this can be removed after full migration - req.Method = "modules" - if err := self.coder.WriteResponse(req); err != nil { - return nil, err - } - - res, err := self.coder.ReadResponse() - if err != nil { - return nil, err - } - - if sucRes, ok := res.(*shared.SuccessResponse); ok { - data, _ := json.Marshal(sucRes.Result) - modules := make(map[string]string) - err = json.Unmarshal(data, &modules) - if err == nil { - return modules, nil - } - } - - return nil, fmt.Errorf("Invalid response") -} - -// Create a new IPC client, UNIX domain socket on posix, named pipe on Windows -func NewIpcClient(cfg IpcConfig, codec codec.Codec) (*ipcClient, error) { - return newIpcClient(cfg, codec) -} - -// Start IPC server -func StartIpc(cfg IpcConfig, codec codec.Codec, initializer InitFunc) error { - l, err := ipcListen(cfg) - if err != nil { - return err - } - go ipcLoop(cfg, codec, initializer, l) - return nil -} - -// CreateListener creates an listener, on Unix platforms this is a unix socket, on Windows this is a named pipe -func CreateListener(cfg IpcConfig) (net.Listener, error) { - return ipcListen(cfg) -} - -func ipcLoop(cfg IpcConfig, codec codec.Codec, initializer InitFunc, l net.Listener) { - glog.V(logger.Info).Infof("IPC service started (%s)\n", cfg.Endpoint) - defer os.Remove(cfg.Endpoint) - defer l.Close() - for { - conn, err := l.Accept() - if err != nil { - glog.V(logger.Debug).Infof("accept: %v", err) - return - } - id := newIpcConnId() - go func() { - defer conn.Close() - glog.V(logger.Debug).Infof("new connection with id %06d started", id) - stopper, api, err := initializer(conn) - if err != nil { - glog.V(logger.Error).Infof("Unable to initialize IPC connection: %v", err) - return - } - defer stopper.Stop() - handle(id, conn, api, codec) - }() - } -} - -func newIpcConnId() int { - return rand.Int() % 1000000 -} diff --git a/rpc/comms/ipc_unix.go b/rpc/comms/ipc_unix.go deleted file mode 100644 index 4b839572a..000000000 --- a/rpc/comms/ipc_unix.go +++ /dev/null @@ -1,82 +0,0 @@ -// Copyright 2015 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library 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 Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see . - -// +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris - -package comms - -import ( - "net" - "os" - "path/filepath" - - "github.com/ethereum/go-ethereum/rpc/codec" - "github.com/ethereum/go-ethereum/rpc/shared" - "github.com/ethereum/go-ethereum/rpc/useragent" -) - -func newIpcClient(cfg IpcConfig, codec codec.Codec) (*ipcClient, error) { - c, err := net.DialUnix("unix", nil, &net.UnixAddr{cfg.Endpoint, "unix"}) - if err != nil { - return nil, err - } - - coder := codec.New(c) - msg := shared.Request{ - Id: 0, - Method: useragent.EnableUserAgentMethod, - Jsonrpc: shared.JsonRpcVersion, - Params: []byte("[]"), - } - - coder.WriteResponse(msg) - coder.Recv() - - return &ipcClient{cfg.Endpoint, c, codec, coder}, nil -} - -func (self *ipcClient) reconnect() error { - self.coder.Close() - c, err := net.DialUnix("unix", nil, &net.UnixAddr{self.endpoint, "unix"}) - if err == nil { - self.coder = self.codec.New(c) - - msg := shared.Request{ - Id: 0, - Method: useragent.EnableUserAgentMethod, - Jsonrpc: shared.JsonRpcVersion, - Params: []byte("[]"), - } - self.coder.WriteResponse(msg) - self.coder.Recv() - } - - return err -} - -func ipcListen(cfg IpcConfig) (net.Listener, error) { - // Ensure the IPC path exists and remove any previous leftover - if err := os.MkdirAll(filepath.Dir(cfg.Endpoint), 0751); err != nil { - return nil, err - } - os.Remove(cfg.Endpoint) - l, err := net.Listen("unix", cfg.Endpoint) - if err != nil { - return nil, err - } - os.Chmod(cfg.Endpoint, 0600) - return l, nil -} diff --git a/rpc/comms/ipc_windows.go b/rpc/comms/ipc_windows.go deleted file mode 100644 index e25fba253..000000000 --- a/rpc/comms/ipc_windows.go +++ /dev/null @@ -1,697 +0,0 @@ -// Copyright 2015 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library 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 Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see . - -// +build windows - -package comms - -import ( - "fmt" - "io" - "net" - "os" - "sync" - "syscall" - "time" - "unsafe" - - "github.com/ethereum/go-ethereum/rpc/codec" - "github.com/ethereum/go-ethereum/rpc/shared" - "github.com/ethereum/go-ethereum/rpc/useragent" -) - -var ( - modkernel32 = syscall.NewLazyDLL("kernel32.dll") - - procCreateNamedPipeW = modkernel32.NewProc("CreateNamedPipeW") - procConnectNamedPipe = modkernel32.NewProc("ConnectNamedPipe") - procDisconnectNamedPipe = modkernel32.NewProc("DisconnectNamedPipe") - procWaitNamedPipeW = modkernel32.NewProc("WaitNamedPipeW") - procCreateEventW = modkernel32.NewProc("CreateEventW") - procGetOverlappedResult = modkernel32.NewProc("GetOverlappedResult") - procCancelIoEx = modkernel32.NewProc("CancelIoEx") -) - -func createNamedPipe(name *uint16, openMode uint32, pipeMode uint32, maxInstances uint32, outBufSize uint32, inBufSize uint32, defaultTimeout uint32, sa *syscall.SecurityAttributes) (handle syscall.Handle, err error) { - r0, _, e1 := syscall.Syscall9(procCreateNamedPipeW.Addr(), 8, uintptr(unsafe.Pointer(name)), uintptr(openMode), uintptr(pipeMode), uintptr(maxInstances), uintptr(outBufSize), uintptr(inBufSize), uintptr(defaultTimeout), uintptr(unsafe.Pointer(sa)), 0) - handle = syscall.Handle(r0) - if handle == syscall.InvalidHandle { - if e1 != 0 { - err = error(e1) - } else { - err = syscall.EINVAL - } - } - return -} - -func cancelIoEx(handle syscall.Handle, overlapped *syscall.Overlapped) (err error) { - r1, _, e1 := syscall.Syscall(procCancelIoEx.Addr(), 2, uintptr(handle), uintptr(unsafe.Pointer(overlapped)), 0) - if r1 == 0 { - if e1 != 0 { - err = error(e1) - } else { - err = syscall.EINVAL - } - } - return -} - -func connectNamedPipe(handle syscall.Handle, overlapped *syscall.Overlapped) (err error) { - r1, _, e1 := syscall.Syscall(procConnectNamedPipe.Addr(), 2, uintptr(handle), uintptr(unsafe.Pointer(overlapped)), 0) - if r1 == 0 { - if e1 != 0 { - err = error(e1) - } else { - err = syscall.EINVAL - } - } - return -} - -func disconnectNamedPipe(handle syscall.Handle) (err error) { - r1, _, e1 := syscall.Syscall(procDisconnectNamedPipe.Addr(), 1, uintptr(handle), 0, 0) - if r1 == 0 { - if e1 != 0 { - err = error(e1) - } else { - err = syscall.EINVAL - } - } - return -} - -func waitNamedPipe(name *uint16, timeout uint32) (err error) { - r1, _, e1 := syscall.Syscall(procWaitNamedPipeW.Addr(), 2, uintptr(unsafe.Pointer(name)), uintptr(timeout), 0) - if r1 == 0 { - if e1 != 0 { - err = error(e1) - } else { - err = syscall.EINVAL - } - } - return -} - -func createEvent(sa *syscall.SecurityAttributes, manualReset bool, initialState bool, name *uint16) (handle syscall.Handle, err error) { - var _p0 uint32 - if manualReset { - _p0 = 1 - } else { - _p0 = 0 - } - var _p1 uint32 - if initialState { - _p1 = 1 - } else { - _p1 = 0 - } - r0, _, e1 := syscall.Syscall6(procCreateEventW.Addr(), 4, uintptr(unsafe.Pointer(sa)), uintptr(_p0), uintptr(_p1), uintptr(unsafe.Pointer(name)), 0, 0) - handle = syscall.Handle(r0) - if handle == syscall.InvalidHandle { - if e1 != 0 { - err = error(e1) - } else { - err = syscall.EINVAL - } - } - return -} - -func getOverlappedResult(handle syscall.Handle, overlapped *syscall.Overlapped, transferred *uint32, wait bool) (err error) { - var _p0 uint32 - if wait { - _p0 = 1 - } else { - _p0 = 0 - } - r1, _, e1 := syscall.Syscall6(procGetOverlappedResult.Addr(), 4, uintptr(handle), uintptr(unsafe.Pointer(overlapped)), uintptr(unsafe.Pointer(transferred)), uintptr(_p0), 0, 0) - if r1 == 0 { - if e1 != 0 { - err = error(e1) - } else { - err = syscall.EINVAL - } - } - return -} - -const ( - // openMode - pipe_access_duplex = 0x3 - pipe_access_inbound = 0x1 - pipe_access_outbound = 0x2 - - // openMode write flags - file_flag_first_pipe_instance = 0x00080000 - file_flag_write_through = 0x80000000 - file_flag_overlapped = 0x40000000 - - // openMode ACL flags - write_dac = 0x00040000 - write_owner = 0x00080000 - access_system_security = 0x01000000 - - // pipeMode - pipe_type_byte = 0x0 - pipe_type_message = 0x4 - - // pipeMode read mode flags - pipe_readmode_byte = 0x0 - pipe_readmode_message = 0x2 - - // pipeMode wait mode flags - pipe_wait = 0x0 - pipe_nowait = 0x1 - - // pipeMode remote-client mode flags - pipe_accept_remote_clients = 0x0 - pipe_reject_remote_clients = 0x8 - - pipe_unlimited_instances = 255 - - nmpwait_wait_forever = 0xFFFFFFFF - - // the two not-an-errors below occur if a client connects to the pipe between - // the server's CreateNamedPipe and ConnectNamedPipe calls. - error_no_data syscall.Errno = 0xE8 - error_pipe_connected syscall.Errno = 0x217 - error_pipe_busy syscall.Errno = 0xE7 - error_sem_timeout syscall.Errno = 0x79 - - error_bad_pathname syscall.Errno = 0xA1 - error_invalid_name syscall.Errno = 0x7B - - error_io_incomplete syscall.Errno = 0x3e4 -) - -var _ net.Conn = (*PipeConn)(nil) -var _ net.Listener = (*PipeListener)(nil) - -// ErrClosed is the error returned by PipeListener.Accept when Close is called -// on the PipeListener. -var ErrClosed = PipeError{"Pipe has been closed.", false} - -// PipeError is an error related to a call to a pipe -type PipeError struct { - msg string - timeout bool -} - -// Error implements the error interface -func (e PipeError) Error() string { - return e.msg -} - -// Timeout implements net.AddrError.Timeout() -func (e PipeError) Timeout() bool { - return e.timeout -} - -// Temporary implements net.AddrError.Temporary() -func (e PipeError) Temporary() bool { - return false -} - -// Dial connects to a named pipe with the given address. If the specified pipe is not available, -// it will wait indefinitely for the pipe to become available. -// -// The address must be of the form \\.\\pipe\ for local pipes and \\\pipe\ -// for remote pipes. -// -// Dial will return a PipeError if you pass in a badly formatted pipe name. -// -// Examples: -// // local pipe -// conn, err := Dial(`\\.\pipe\mypipename`) -// -// // remote pipe -// conn, err := Dial(`\\othercomp\pipe\mypipename`) -func Dial(address string) (*PipeConn, error) { - for { - conn, err := dial(address, nmpwait_wait_forever) - if err == nil { - return conn, nil - } - if isPipeNotReady(err) { - <-time.After(100 * time.Millisecond) - continue - } - return nil, err - } -} - -// DialTimeout acts like Dial, but will time out after the duration of timeout -func DialTimeout(address string, timeout time.Duration) (*PipeConn, error) { - deadline := time.Now().Add(timeout) - - now := time.Now() - for now.Before(deadline) { - millis := uint32(deadline.Sub(now) / time.Millisecond) - conn, err := dial(address, millis) - if err == nil { - return conn, nil - } - if err == error_sem_timeout { - // This is WaitNamedPipe's timeout error, so we know we're done - return nil, PipeError{fmt.Sprintf( - "Timed out waiting for pipe '%s' to come available", address), true} - } - if isPipeNotReady(err) { - left := deadline.Sub(time.Now()) - retry := 100 * time.Millisecond - if left > retry { - <-time.After(retry) - } else { - <-time.After(left - time.Millisecond) - } - now = time.Now() - continue - } - return nil, err - } - return nil, PipeError{fmt.Sprintf( - "Timed out waiting for pipe '%s' to come available", address), true} -} - -// isPipeNotReady checks the error to see if it indicates the pipe is not ready -func isPipeNotReady(err error) bool { - // Pipe Busy means another client just grabbed the open pipe end, - // and the server hasn't made a new one yet. - // File Not Found means the server hasn't created the pipe yet. - // Neither is a fatal error. - - return err == syscall.ERROR_FILE_NOT_FOUND || err == error_pipe_busy -} - -// newOverlapped creates a structure used to track asynchronous -// I/O requests that have been issued. -func newOverlapped() (*syscall.Overlapped, error) { - event, err := createEvent(nil, true, true, nil) - if err != nil { - return nil, err - } - return &syscall.Overlapped{HEvent: event}, nil -} - -// waitForCompletion waits for an asynchronous I/O request referred to by overlapped to complete. -// This function returns the number of bytes transferred by the operation and an error code if -// applicable (nil otherwise). -func waitForCompletion(handle syscall.Handle, overlapped *syscall.Overlapped) (uint32, error) { - _, err := syscall.WaitForSingleObject(overlapped.HEvent, syscall.INFINITE) - if err != nil { - return 0, err - } - var transferred uint32 - err = getOverlappedResult(handle, overlapped, &transferred, true) - return transferred, err -} - -// dial is a helper to initiate a connection to a named pipe that has been started by a server. -// The timeout is only enforced if the pipe server has already created the pipe, otherwise -// this function will return immediately. -func dial(address string, timeout uint32) (*PipeConn, error) { - name, err := syscall.UTF16PtrFromString(string(address)) - if err != nil { - return nil, err - } - // If at least one instance of the pipe has been created, this function - // will wait timeout milliseconds for it to become available. - // It will return immediately regardless of timeout, if no instances - // of the named pipe have been created yet. - // If this returns with no error, there is a pipe available. - if err := waitNamedPipe(name, timeout); err != nil { - if err == error_bad_pathname { - // badly formatted pipe name - return nil, badAddr(address) - } - return nil, err - } - pathp, err := syscall.UTF16PtrFromString(address) - if err != nil { - return nil, err - } - handle, err := syscall.CreateFile(pathp, syscall.GENERIC_READ|syscall.GENERIC_WRITE, - uint32(syscall.FILE_SHARE_READ|syscall.FILE_SHARE_WRITE), nil, syscall.OPEN_EXISTING, - syscall.FILE_FLAG_OVERLAPPED, 0) - if err != nil { - return nil, err - } - return &PipeConn{handle: handle, addr: PipeAddr(address)}, nil -} - -// Listen returns a new PipeListener that will listen on a pipe with the given -// address. The address must be of the form \\.\pipe\ -// -// Listen will return a PipeError for an incorrectly formatted pipe name. -func Listen(address string) (*PipeListener, error) { - handle, err := createPipe(address, true) - if err == error_invalid_name { - return nil, badAddr(address) - } - if err != nil { - return nil, err - } - return &PipeListener{ - addr: PipeAddr(address), - handle: handle, - }, nil -} - -// PipeListener is a named pipe listener. Clients should typically -// use variables of type net.Listener instead of assuming named pipe. -type PipeListener struct { - addr PipeAddr - handle syscall.Handle - closed bool - - // acceptHandle contains the current handle waiting for - // an incoming connection or nil. - acceptHandle syscall.Handle - // acceptOverlapped is set before waiting on a connection. - // If not waiting, it is nil. - acceptOverlapped *syscall.Overlapped - // acceptMutex protects the handle and overlapped structure. - acceptMutex sync.Mutex -} - -// Accept implements the Accept method in the net.Listener interface; it -// waits for the next call and returns a generic net.Conn. -func (l *PipeListener) Accept() (net.Conn, error) { - c, err := l.AcceptPipe() - for err == error_no_data { - // Ignore clients that connect and immediately disconnect. - c, err = l.AcceptPipe() - } - if err != nil { - return nil, err - } - return c, nil -} - -// AcceptPipe accepts the next incoming call and returns the new connection. -// It might return an error if a client connected and immediately cancelled -// the connection. -func (l *PipeListener) AcceptPipe() (*PipeConn, error) { - if l == nil || l.addr == "" || l.closed { - return nil, syscall.EINVAL - } - - // the first time we call accept, the handle will have been created by the Listen - // call. This is to prevent race conditions where the client thinks the server - // isn't listening because it hasn't actually called create yet. After the first time, we'll - // have to create a new handle each time - handle := l.handle - if handle == 0 { - var err error - handle, err = createPipe(string(l.addr), false) - if err != nil { - return nil, err - } - } else { - l.handle = 0 - } - - overlapped, err := newOverlapped() - if err != nil { - return nil, err - } - defer syscall.CloseHandle(overlapped.HEvent) - if err := connectNamedPipe(handle, overlapped); err != nil && err != error_pipe_connected { - if err == error_io_incomplete || err == syscall.ERROR_IO_PENDING { - l.acceptMutex.Lock() - l.acceptOverlapped = overlapped - l.acceptHandle = handle - l.acceptMutex.Unlock() - defer func() { - l.acceptMutex.Lock() - l.acceptOverlapped = nil - l.acceptHandle = 0 - l.acceptMutex.Unlock() - }() - - _, err = waitForCompletion(handle, overlapped) - } - if err == syscall.ERROR_OPERATION_ABORTED { - // Return error compatible to net.Listener.Accept() in case the - // listener was closed. - return nil, ErrClosed - } - if err != nil { - return nil, err - } - } - return &PipeConn{handle: handle, addr: l.addr}, nil -} - -// Close stops listening on the address. -// Already Accepted connections are not closed. -func (l *PipeListener) Close() error { - if l.closed { - return nil - } - l.closed = true - if l.handle != 0 { - err := disconnectNamedPipe(l.handle) - if err != nil { - return err - } - err = syscall.CloseHandle(l.handle) - if err != nil { - return err - } - l.handle = 0 - } - l.acceptMutex.Lock() - defer l.acceptMutex.Unlock() - if l.acceptOverlapped != nil && l.acceptHandle != 0 { - // Cancel the pending IO. This call does not block, so it is safe - // to hold onto the mutex above. - if err := cancelIoEx(l.acceptHandle, l.acceptOverlapped); err != nil { - return err - } - err := syscall.CloseHandle(l.acceptOverlapped.HEvent) - if err != nil { - return err - } - l.acceptOverlapped.HEvent = 0 - err = syscall.CloseHandle(l.acceptHandle) - if err != nil { - return err - } - l.acceptHandle = 0 - } - return nil -} - -// Addr returns the listener's network address, a PipeAddr. -func (l *PipeListener) Addr() net.Addr { return l.addr } - -// PipeConn is the implementation of the net.Conn interface for named pipe connections. -type PipeConn struct { - handle syscall.Handle - addr PipeAddr - - // these aren't actually used yet - readDeadline *time.Time - writeDeadline *time.Time -} - -type iodata struct { - n uint32 - err error -} - -// completeRequest looks at iodata to see if a request is pending. If so, it waits for it to either complete or to -// abort due to hitting the specified deadline. Deadline may be set to nil to wait forever. If no request is pending, -// the content of iodata is returned. -func (c *PipeConn) completeRequest(data iodata, deadline *time.Time, overlapped *syscall.Overlapped) (int, error) { - if data.err == error_io_incomplete || data.err == syscall.ERROR_IO_PENDING { - var timer <-chan time.Time - if deadline != nil { - if timeDiff := deadline.Sub(time.Now()); timeDiff > 0 { - timer = time.After(timeDiff) - } - } - done := make(chan iodata) - go func() { - n, err := waitForCompletion(c.handle, overlapped) - done <- iodata{n, err} - }() - select { - case data = <-done: - case <-timer: - syscall.CancelIoEx(c.handle, overlapped) - data = iodata{0, timeout(c.addr.String())} - } - } - // Windows will produce ERROR_BROKEN_PIPE upon closing - // a handle on the other end of a connection. Go RPC - // expects an io.EOF error in this case. - if data.err == syscall.ERROR_BROKEN_PIPE { - data.err = io.EOF - } - return int(data.n), data.err -} - -// Read implements the net.Conn Read method. -func (c *PipeConn) Read(b []byte) (int, error) { - // Use ReadFile() rather than Read() because the latter - // contains a workaround that eats ERROR_BROKEN_PIPE. - overlapped, err := newOverlapped() - if err != nil { - return 0, err - } - defer syscall.CloseHandle(overlapped.HEvent) - var n uint32 - err = syscall.ReadFile(c.handle, b, &n, overlapped) - return c.completeRequest(iodata{n, err}, c.readDeadline, overlapped) -} - -// Write implements the net.Conn Write method. -func (c *PipeConn) Write(b []byte) (int, error) { - overlapped, err := newOverlapped() - if err != nil { - return 0, err - } - defer syscall.CloseHandle(overlapped.HEvent) - var n uint32 - err = syscall.WriteFile(c.handle, b, &n, overlapped) - return c.completeRequest(iodata{n, err}, c.writeDeadline, overlapped) -} - -// Close closes the connection. -func (c *PipeConn) Close() error { - return syscall.CloseHandle(c.handle) -} - -// LocalAddr returns the local network address. -func (c *PipeConn) LocalAddr() net.Addr { - return c.addr -} - -// RemoteAddr returns the remote network address. -func (c *PipeConn) RemoteAddr() net.Addr { - // not sure what to do here, we don't have remote addr.... - return c.addr -} - -// SetDeadline implements the net.Conn SetDeadline method. -// Note that timeouts are only supported on Windows Vista/Server 2008 and above -func (c *PipeConn) SetDeadline(t time.Time) error { - c.SetReadDeadline(t) - c.SetWriteDeadline(t) - return nil -} - -// SetReadDeadline implements the net.Conn SetReadDeadline method. -// Note that timeouts are only supported on Windows Vista/Server 2008 and above -func (c *PipeConn) SetReadDeadline(t time.Time) error { - c.readDeadline = &t - return nil -} - -// SetWriteDeadline implements the net.Conn SetWriteDeadline method. -// Note that timeouts are only supported on Windows Vista/Server 2008 and above -func (c *PipeConn) SetWriteDeadline(t time.Time) error { - c.writeDeadline = &t - return nil -} - -// PipeAddr represents the address of a named pipe. -type PipeAddr string - -// Network returns the address's network name, "pipe". -func (a PipeAddr) Network() string { return "pipe" } - -// String returns the address of the pipe -func (a PipeAddr) String() string { - return string(a) -} - -// createPipe is a helper function to make sure we always create pipes -// with the same arguments, since subsequent calls to create pipe need -// to use the same arguments as the first one. If first is set, fail -// if the pipe already exists. -func createPipe(address string, first bool) (syscall.Handle, error) { - n, err := syscall.UTF16PtrFromString(address) - if err != nil { - return 0, err - } - mode := uint32(pipe_access_duplex | syscall.FILE_FLAG_OVERLAPPED) - if first { - mode |= file_flag_first_pipe_instance - } - return createNamedPipe(n, - mode, - pipe_type_byte, - pipe_unlimited_instances, - 512, 512, 0, nil) -} - -func badAddr(addr string) PipeError { - return PipeError{fmt.Sprintf("Invalid pipe address '%s'.", addr), false} -} -func timeout(addr string) PipeError { - return PipeError{fmt.Sprintf("Pipe IO timed out waiting for '%s'", addr), true} -} - -func newIpcClient(cfg IpcConfig, codec codec.Codec) (*ipcClient, error) { - c, err := Dial(cfg.Endpoint) - if err != nil { - return nil, err - } - - coder := codec.New(c) - msg := shared.Request{ - Id: 0, - Method: useragent.EnableUserAgentMethod, - Jsonrpc: shared.JsonRpcVersion, - Params: []byte("[]"), - } - - coder.WriteResponse(msg) - coder.Recv() - - return &ipcClient{cfg.Endpoint, c, codec, coder}, nil -} - -func (self *ipcClient) reconnect() error { - c, err := Dial(self.endpoint) - if err == nil { - self.coder = self.codec.New(c) - - req := shared.Request{ - Id: 0, - Method: useragent.EnableUserAgentMethod, - Jsonrpc: shared.JsonRpcVersion, - Params: []byte("[]"), - } - self.coder.WriteResponse(req) - self.coder.Recv() - } - return err -} - -func ipcListen(cfg IpcConfig) (net.Listener, error) { - os.Remove(cfg.Endpoint) // in case it still exists from a previous run - l, err := Listen(cfg.Endpoint) - if err != nil { - return nil, err - } - os.Chmod(cfg.Endpoint, 0600) - return l, nil -} diff --git a/rpc/doc.go b/rpc/doc.go new file mode 100644 index 000000000..e8f8f977b --- /dev/null +++ b/rpc/doc.go @@ -0,0 +1,228 @@ +// Copyright 2015 The go-ethereum Authors +// This file is part of the go-ethereum library. +// +// The go-ethereum library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The go-ethereum library 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 Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the go-ethereum library. If not, see . + +/* +Package rpc provides access to the exported methods of an object across a network +or other I/O connection. After creating a server instance objects can be registered, +making it visible from the outside. Exported methods that follow specific +conventions can be called remotely. It also has support for the publish/subscribe +pattern. + +Methods that satisfy the following criteria are made available for remote access: + - object must be exported + - method must be exported + - method returns 0, 1 (response or error) or 2 (response and error) values + - method argument(s) must be exported or builtin types + - method returned value(s) must be exported or builtin types + +An example method: + func (s *CalcService) Div(a, b int) (int, error) + +When the returned error isn't nil the returned integer is ignored and the error is +send back to the client. Otherwise the returned integer is send back to the client. + +The server offers the ServeCodec method which accepts a ServerCodec instance. It will +read requests from the codec, process the request and sends the response back to the +client using the codec. The server can execute requests concurrently. Responses +can be send back to the client out of order. + +An example server which uses the JSON codec: + type CalculatorService struct {} + + func (s *CalculatorService) Add(a, b int) int { + return a + b + } + + func (s *CalculatorService Div(a, b int) (int, error) { + if b == 0 { + return 0, errors.New("divide by zero") + } + return a/b, nil + } + + calculator := new(CalculatorService) + server := NewServer() + server.RegisterName("calculator", calculator") + + l, _ := net.ListenUnix("unix", &net.UnixAddr{Net: "unix", Name: "/tmp/calculator.sock"}) + for { + c, _ := l.AcceptUnix() + codec := v2.NewJSONCodec(c) + go server.ServeCodec(codec) + } + +The package also supports the publish subscribe pattern through the use of subscriptions. +A method that is considered eligible for notifications must satisfy the following criteria: + - object must be exported + - method must be exported + - method argument(s) must be exported or builtin types + - method must return the tuple Subscription, error + + +An example method: + func (s *BlockChainService) Head() (Subscription, error) { + sub := s.bc.eventMux.Subscribe(ChainHeadEvent{}) + return v2.NewSubscription(sub), nil + } + +This method will push all raised ChainHeadEvents to subscribed clients. If the client is only +interested in every N'th block it is possible to add a criteria. + + func (s *BlockChainService) HeadFiltered(nth uint64) (Subscription, error) { + sub := s.bc.eventMux.Subscribe(ChainHeadEvent{}) + + criteria := func(event interface{}) bool { + chainHeadEvent := event.(ChainHeadEvent) + if chainHeadEvent.Block.NumberU64() % nth == 0 { + return true + } + return false + } + + return v2.NewSubscriptionFiltered(sub, criteria), nil + } + +Subscriptions are deleted when: + - the user sends an unsubscribe request + - the connection which was used to create the subscription is closed +*/ +package rpc + +var ( + // Mapping between the different methods each api supports + AutoCompletion = map[string][]string{ + "admin": []string{ + "addPeer", + "datadir", + "enableUserAgent", + "exportChain", + "getContractInfo", + "httpGet", + "importChain", + "nodeInfo", + "peers", + "register", + "registerUrl", + "saveInfo", + "setGlobalRegistrar", + "setHashReg", + "setUrlHint", + "setSolc", + "sleep", + "sleepBlocks", + "startNatSpec", + "startRPC", + "stopNatSpec", + "stopRPC", + "verbosity", + }, + "db": []string{ + "getString", + "putString", + "getHex", + "putHex", + }, + "debug": []string{ + "dumpBlock", + "getBlockRlp", + "metrics", + "printBlock", + "processBlock", + "seedHash", + "setHead", + }, + "eth": []string{ + "accounts", + "blockNumber", + "call", + "contract", + "coinbase", + "compile.lll", + "compile.serpent", + "compile.solidity", + "contract", + "defaultAccount", + "defaultBlock", + "estimateGas", + "filter", + "getBalance", + "getBlock", + "getBlockTransactionCount", + "getBlockUncleCount", + "getCode", + "getNatSpec", + "getCompilers", + "gasPrice", + "getStorageAt", + "getTransaction", + "getTransactionCount", + "getTransactionFromBlock", + "getTransactionReceipt", + "getUncle", + "hashrate", + "mining", + "namereg", + "pendingTransactions", + "resend", + "sendRawTransaction", + "sendTransaction", + "sign", + "syncing", + }, + "miner": []string{ + "hashrate", + "makeDAG", + "setEtherbase", + "setExtra", + "setGasPrice", + "startAutoDAG", + "start", + "stopAutoDAG", + "stop", + }, + "net": []string{ + "peerCount", + "listening", + }, + "personal": []string{ + "listAccounts", + "newAccount", + "unlockAccount", + }, + "shh": []string{ + "post", + "newIdentity", + "hasIdentity", + "newGroup", + "addToGroup", + "filter", + }, + "txpool": []string{ + "status", + }, + "web3": []string{ + "sha3", + "version", + "fromWei", + "toWei", + "toHex", + "toAscii", + "fromAscii", + "toBigNumber", + "isAddress", + }, + } +) diff --git a/rpc/errors.go b/rpc/errors.go new file mode 100644 index 000000000..bc352fc45 --- /dev/null +++ b/rpc/errors.go @@ -0,0 +1,97 @@ +// Copyright 2015 The go-ethereum Authors +// This file is part of the go-ethereum library. +// +// The go-ethereum library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The go-ethereum library 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 Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the go-ethereum library. If not, see . + +package rpc + +import "fmt" + +// request is for an unknown service +type methodNotFoundError struct { + service string + method string +} + +func (e *methodNotFoundError) Code() int { + return -32601 +} + +func (e *methodNotFoundError) Error() string { + return fmt.Sprintf("The method %s%s%s does not exist/is not available", e.service, serviceMethodSeparator, e.method) +} + +// received message isn't a valid request +type invalidRequestError struct { + message string +} + +func (e *invalidRequestError) Code() int { + return -32600 +} + +func (e *invalidRequestError) Error() string { + return e.message +} + +// received message is invalid +type invalidMessageError struct { + message string +} + +func (e *invalidMessageError) Code() int { + return -32700 +} + +func (e *invalidMessageError) Error() string { + return e.message +} + +// unable to decode supplied params, or an invalid number of parameters +type invalidParamsError struct { + message string +} + +func (e *invalidParamsError) Code() int { + return -32602 +} + +func (e *invalidParamsError) Error() string { + return e.message +} + +// logic error, callback returned an error +type callbackError struct { + message string +} + +func (e *callbackError) Code() int { + return -32000 +} + +func (e *callbackError) Error() string { + return e.message +} + +// issued when a request is received after the server is issued to stop. +type shutdownError struct { +} + +func (e *shutdownError) Code() int { + return -32000 +} + +func (e *shutdownError) Error() string { + return "server is shutting down" +} diff --git a/rpc/http.go b/rpc/http.go new file mode 100644 index 000000000..e4b25bed8 --- /dev/null +++ b/rpc/http.go @@ -0,0 +1,368 @@ +// Copyright 2015 The go-ethereum Authors +// This file is part of the go-ethereum library. +// +// The go-ethereum library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The go-ethereum library 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 Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the go-ethereum library. If not, see . + +package rpc + +import ( + "bufio" + "fmt" + "io" + "net" + "net/http" + "strconv" + "strings" + "time" + + "errors" + "sync" + + "bytes" + "encoding/json" + "io/ioutil" + "net/url" + + "github.com/ethereum/go-ethereum/logger" + "github.com/ethereum/go-ethereum/logger/glog" + "gopkg.in/fatih/set.v0" +) + +const ( + httpReadDeadLine = 60 * time.Second // wait max httpReadDeadeline for next request +) + +var ( + httpServerMu sync.Mutex // prevent concurrent access to the httpListener and httpServer + httpListener net.Listener // listener for the http server + httpRPCServer *Server // the node can only start 1 HTTP RPC server instance +) + +// httpMessageStream is the glue between a HTTP connection which is message based +// and the RPC codecs that expect json requests to be read from a stream. It will +// parse HTTP messages and offer the bodies of these requests as a stream through +// the Read method. This will require full control of the connection and thus need +// a "hijacked" HTTP connection. +type httpMessageStream struct { + conn net.Conn // TCP connection + rw *bufio.ReadWriter // buffered where HTTP requests/responses are read/written from/to + currentReq *http.Request // pending request, codec can pass in a too small buffer for a single read + // we need to keep track of the current requests if it was not read at once + payloadBytesRead int64 // number of bytes which are read from the current request + allowedOrigins *set.Set // allowed CORS domains + origin string // origin of this connection/request +} + +// NewHttpMessageStream will create a new http message stream parser that can be +// used by the codes in the RPC package. It will take full control of the given +// connection and thus needs to be hijacked. It will read and write HTTP messages +// from the passed rwbuf. The allowed origins are the RPC CORS domains the user has supplied. +func NewHTTPMessageStream(c net.Conn, rwbuf *bufio.ReadWriter, initialReq *http.Request, allowdOrigins []string) *httpMessageStream { + r := &httpMessageStream{conn: c, rw: rwbuf, currentReq: initialReq, allowedOrigins: set.New()} + for _, origin := range allowdOrigins { + r.allowedOrigins.Add(origin) + } + return r +} + +// handleOptionsRequest handles the HTTP preflight requests (OPTIONS) that browsers +// make to enforce CORS rules. Only the POST method is allowed and the origin must +// be on the rpccorsdomain list the user has specified. +func (h *httpMessageStream) handleOptionsRequest(req *http.Request) error { + headers := req.Header + + if !strings.EqualFold(req.Method, "OPTIONS") { + return fmt.Errorf("preflight aborted: %s!=OPTIONS", req.Method) + } + + origin := headers.Get("Origin") + if origin == "" { + return fmt.Errorf("preflight aborted: empty origin") + } + + responseHeaders := make(http.Header) + responseHeaders.Set("Access-Control-Allow-Methods", "POST") + if h.allowedOrigins.Has(origin) || h.allowedOrigins.Has("*") { + responseHeaders.Set("Access-Control-Allow-Origin", origin) + } else { + glog.V(logger.Info).Infof("origin '%s' not allowed", origin) + } + responseHeaders.Set("Access-Control-Allow-Headers", "Content-Type") + responseHeaders.Set("Date", string(httpTimestamp(time.Now()))) + responseHeaders.Set("Content-Type", "text/plain; charset=utf-8") + responseHeaders.Set("Content-Length", "0") + responseHeaders.Set("Vary", "Origin") + + defer h.rw.Flush() + + if _, err := h.rw.WriteString("HTTP/1.1 200 OK\r\n"); err != nil { + glog.V(logger.Error).Infof("unable to write OPTIONS response: %v\n", err) + return err + } + if err := responseHeaders.Write(h.rw); err != nil { + glog.V(logger.Error).Infof("unable to write OPTIONS headers: %v\n", err) + } + if _, err := h.rw.WriteString("\r\n"); err != nil { + glog.V(logger.Error).Infof("unable to write OPTIONS response: %v\n", err) + } + + return nil +} + +// Read will read incoming HTTP requests and reads the body data from these requests +// as an endless stream of data. +func (h *httpMessageStream) Read(buf []byte) (n int, err error) { + h.conn.SetReadDeadline(time.Now().Add(httpReadDeadLine)) + for { + // if the last request was read completely try to read the next request + if h.currentReq == nil { + if h.currentReq, err = http.ReadRequest(bufio.NewReader(h.rw)); err != nil { + return 0, err + } + } + + // The "options" method is http specific and not interested for the RPC server. + // Handle it internally and wait for the next request. + if strings.EqualFold(h.currentReq.Method, "OPTIONS") { + if err = h.handleOptionsRequest(h.currentReq); err != nil { + glog.V(logger.Info).Infof("RPC/HTTP OPTIONS error: %v\n", err) + h.currentReq = nil + return 0, err + } + + // processed valid request -> reset deadline + h.conn.SetReadDeadline(time.Now().Add(httpReadDeadLine)) + h.currentReq = nil + continue + } + + if strings.EqualFold(h.currentReq.Method, "POST") { + n, err := h.currentReq.Body.Read(buf) + h.payloadBytesRead += int64(n) + + // entire payload read, read new request next time + if err == io.EOF || h.payloadBytesRead >= h.currentReq.ContentLength { + h.origin = h.currentReq.Header.Get("origin") + h.payloadBytesRead = 0 + h.currentReq.Body.Close() + h.currentReq = nil + err = nil // io.EOF is not an error + } else if err != nil { + // unable to read body + h.currentReq.Body.Close() + h.currentReq = nil + h.payloadBytesRead = 0 + } + + // partial read of body + return n, err + } + + h.currentReq = nil + return 0, fmt.Errorf("unsupported HTTP method '%s'", h.currentReq.Method) + } +} + +// Write will create a HTTP response with the given payload and send it to the peer. +func (h *httpMessageStream) Write(payload []byte) (int, error) { + defer h.rw.Flush() + + responseHeaders := make(http.Header) + responseHeaders.Set("Content-Type", "application/json") + responseHeaders.Set("Content-Length", strconv.Itoa(len(payload))) + if h.origin != "" { + responseHeaders.Set("Access-Control-Allow-Origin", h.origin) + } + + h.rw.WriteString("HTTP/1.1 200 OK\r\n") + responseHeaders.Write(h.rw) + h.rw.WriteString("\r\n") + + return h.rw.Write(payload) +} + +// Close will close the underlying TCP connection this instance has taken ownership over. +func (h *httpMessageStream) Close() error { + h.rw.Flush() + return h.conn.Close() +} + +// TimeFormat is the time format to use with time.Parse and time.Time.Format when +// parsing or generating times in HTTP headers. It is like time.RFC1123 but hard +// codes GMT as the time zone. +const TimeFormat = "Mon, 02 Jan 2006 15:04:05 GMT" + +// httpTimestamp formats the given t as specified in RFC1123. +func httpTimestamp(t time.Time) []byte { + const days = "SunMonTueWedThuFriSat" + const months = "JanFebMarAprMayJunJulAugSepOctNovDec" + + b := make([]byte, 0) + t = t.UTC() + yy, mm, dd := t.Date() + hh, mn, ss := t.Clock() + day := days[3 * t.Weekday():] + mon := months[3 * (mm - 1):] + + return append(b, + day[0], day[1], day[2], ',', ' ', + byte('0' + dd / 10), byte('0' + dd % 10), ' ', + mon[0], mon[1], mon[2], ' ', + byte('0' + yy / 1000), byte('0' + (yy / 100) % 10), byte('0' + (yy / 10) % 10), byte('0' + yy % 10), ' ', + byte('0' + hh / 10), byte('0' + hh % 10), ':', + byte('0' + mn / 10), byte('0' + mn % 10), ':', + byte('0' + ss / 10), byte('0' + ss % 10), ' ', + 'G', 'M', 'T') +} + +// httpConnHijacker is a http.Handler implementation that will hijack the HTTP +// connection, wraps it in a HttpMessageStream that is then wrapped in a JSON +// codec which will be served on the rpcServer. +type httpConnHijacker struct { + corsdomains []string + rpcServer *Server +} + +// ServeHTTP will hijack the connection, wraps the captured connection in a +// HttpMessageStream which is then used as codec. +func (h *httpConnHijacker) ServeHTTP(w http.ResponseWriter, req *http.Request) { + hj, ok := w.(http.Hijacker) + if !ok { + http.Error(w, "webserver doesn't support hijacking", http.StatusInternalServerError) + return + } + + conn, rwbuf, err := hj.Hijack() + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + httpRequestStream := NewHTTPMessageStream(conn, rwbuf, req, h.corsdomains) + + codec := NewJSONCodec(httpRequestStream) + go h.rpcServer.ServeCodec(codec) +} + +// StartHTTP will start the JSONRPC HTTP RPC interface when its not yet running. +func StartHTTP(address string, port int, corsdomains []string, apis []API) error { + httpServerMu.Lock() + defer httpServerMu.Unlock() + + if httpRPCServer != nil { + return fmt.Errorf("HTTP RPC interface already started on %s", httpListener.Addr()) + } + + rpcServer := NewServer() + + for _, api := range apis { + if err := rpcServer.RegisterName(api.Namespace, api.Service); err != nil { + return err + } + } + + listener, err := net.Listen("tcp", fmt.Sprintf("%s:%d", address, port)) + if err != nil { + return err + } + + httpServer := http.Server{Handler: &httpConnHijacker{corsdomains, rpcServer}} + go httpServer.Serve(listener) + + httpListener = listener + httpRPCServer = rpcServer + + return nil +} + +// StopHTTP will stop the running HTTP interface. If it is not running an error will be returned. +func StopHTTP() error { + httpServerMu.Lock() + defer httpServerMu.Unlock() + + if httpRPCServer == nil { + return errors.New("HTTP RPC interface not started") + } + + httpListener.Close() + httpRPCServer.Stop() + + httpRPCServer = nil + httpListener = nil + + return nil +} + +// httpClient connects to a geth RPC server over HTTP. +type httpClient struct { + endpoint *url.URL // HTTP-RPC server endpoint + lastRes []byte // HTTP requests are synchronous, store last response +} + +// NewHTTPClient create a new RPC clients that connection to a geth RPC server +// over HTTP. +func NewHTTPClient(endpoint string) (*httpClient, error) { + url, err := url.Parse(endpoint) + if err != nil { + return nil, err + } + return &httpClient{endpoint: url}, nil +} + +// Send will serialize the given msg to JSON and sends it to the RPC server. +// Since HTTP is synchronous the response is stored until Recv is called. +func (client *httpClient) Send(msg interface{}) error { + var body []byte + var err error + + client.lastRes = nil + + if body, err = json.Marshal(msg); err != nil { + return err + } + + httpReq, err := http.NewRequest("POST", client.endpoint.String(), bytes.NewBuffer(body)) + if err != nil { + return err + } + httpReq.Header.Set("Content-Type", "application/json") + + httpClient := http.Client{} + resp, err := httpClient.Do(httpReq) + if err != nil { + return err + } + defer resp.Body.Close() + + if resp.StatusCode == http.StatusOK { + client.lastRes, err = ioutil.ReadAll(resp.Body) + return err + } + + return fmt.Errorf("unable to handle request") +} + +// Recv will try to deserialize the last received response into the given msg. +func (client *httpClient) Recv(msg interface{}) error { + return json.Unmarshal(client.lastRes, &msg) +} + +// Close is not necessary for httpClient +func (client *httpClient) Close() { +} + +// SupportedModules will return the collection of offered RPC modules. +func (client *httpClient) SupportedModules() (map[string]string, error) { + return SupportedModules(client) +} diff --git a/rpc/ipc.go b/rpc/ipc.go new file mode 100644 index 000000000..b87bfcbd7 --- /dev/null +++ b/rpc/ipc.go @@ -0,0 +1,84 @@ +// Copyright 2015 The go-ethereum Authors +// This file is part of the go-ethereum library. +// +// The go-ethereum library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The go-ethereum library 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 Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the go-ethereum library. If not, see . + +package rpc + +import ( + "encoding/json" + "net" +) + +// CreateIPCListener creates an listener, on Unix platforms this is a unix socket, on Windows this is a named pipe +func CreateIPCListener(endpoint string) (net.Listener, error) { + return ipcListen(endpoint) +} + +// ipcClient represent an IPC RPC client. It will connect to a given endpoint and tries to communicate with a node using +// JSON serialization. +type ipcClient struct { + endpoint string + conn net.Conn + out *json.Encoder + in *json.Decoder +} + +// NewIPCClient create a new IPC client that will connect on the given endpoint. Messages are JSON encoded and encoded. +// On Unix it assumes the endpoint is the full path to a unix socket, and Windows the endpoint is an identifier for a +// named pipe. +func NewIPCClient(endpoint string) (*ipcClient, error) { + conn, err := newIPCConnection(endpoint) + if err != nil { + return nil, err + } + return &ipcClient{endpoint: endpoint, conn: conn, in: json.NewDecoder(conn), out: json.NewEncoder(conn)}, nil +} + +// Send will serialize the given message and send it to the server. +// When sending the message fails it will try to reconnect once and send the message again. +func (client *ipcClient) Send(msg interface{}) error { + if err := client.out.Encode(msg); err == nil { + return nil + } + + // retry once + client.conn.Close() + + conn, err := newIPCConnection(client.endpoint) + if err != nil { + return err + } + + client.conn = conn + client.in = json.NewDecoder(conn) + client.out = json.NewEncoder(conn) + + return client.out.Encode(msg) +} + +// Recv will read a message from the connection and tries to parse it. It assumes the received message is JSON encoded. +func (client *ipcClient) Recv(msg interface{}) error { + return client.in.Decode(&msg) +} + +// Close will close the underlying IPC connection +func (client *ipcClient) Close() { + client.conn.Close() +} + +// SupportedModules will return the collection of offered RPC modules. +func (client *ipcClient) SupportedModules() (map[string]string, error) { + return SupportedModules(client) +} diff --git a/rpc/ipc_unix.go b/rpc/ipc_unix.go new file mode 100644 index 000000000..310286e96 --- /dev/null +++ b/rpc/ipc_unix.go @@ -0,0 +1,45 @@ +// Copyright 2015 The go-ethereum Authors +// This file is part of the go-ethereum library. +// +// The go-ethereum library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The go-ethereum library 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 Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the go-ethereum library. If not, see . + +// +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris + +package rpc + +import ( + "net" + "os" + "path/filepath" +) + +// ipcListen will create a Unix socket on the given endpoint. +func ipcListen(endpoint string) (net.Listener, error) { + // Ensure the IPC path exists and remove any previous leftover + if err := os.MkdirAll(filepath.Dir(endpoint), 0751); err != nil { + return nil, err + } + os.Remove(endpoint) + l, err := net.Listen("unix", endpoint) + if err != nil { + return nil, err + } + os.Chmod(endpoint, 0600) + return l, nil +} + +// newIPCConnection will connect to a Unix socket on the given endpoint. +func newIPCConnection(endpoint string) (net.Conn, error) { + return net.DialUnix("unix", nil, &net.UnixAddr{endpoint, "unix"}) +} diff --git a/rpc/ipc_windows.go b/rpc/ipc_windows.go new file mode 100644 index 000000000..1d4672ad2 --- /dev/null +++ b/rpc/ipc_windows.go @@ -0,0 +1,655 @@ +// Copyright 2015 The go-ethereum Authors +// This file is part of the go-ethereum library. +// +// The go-ethereum library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The go-ethereum library 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 Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the go-ethereum library. If not, see . + +// +build windows + +package rpc + +import ( + "fmt" + "io" + "net" + "sync" + "syscall" + "time" + "unsafe" +) + +var ( + modkernel32 = syscall.NewLazyDLL("kernel32.dll") + + procCreateNamedPipeW = modkernel32.NewProc("CreateNamedPipeW") + procConnectNamedPipe = modkernel32.NewProc("ConnectNamedPipe") + procDisconnectNamedPipe = modkernel32.NewProc("DisconnectNamedPipe") + procWaitNamedPipeW = modkernel32.NewProc("WaitNamedPipeW") + procCreateEventW = modkernel32.NewProc("CreateEventW") + procGetOverlappedResult = modkernel32.NewProc("GetOverlappedResult") + procCancelIoEx = modkernel32.NewProc("CancelIoEx") +) + +func createNamedPipe(name *uint16, openMode uint32, pipeMode uint32, maxInstances uint32, outBufSize uint32, inBufSize uint32, defaultTimeout uint32, sa *syscall.SecurityAttributes) (handle syscall.Handle, err error) { + r0, _, e1 := syscall.Syscall9(procCreateNamedPipeW.Addr(), 8, uintptr(unsafe.Pointer(name)), uintptr(openMode), uintptr(pipeMode), uintptr(maxInstances), uintptr(outBufSize), uintptr(inBufSize), uintptr(defaultTimeout), uintptr(unsafe.Pointer(sa)), 0) + handle = syscall.Handle(r0) + if handle == syscall.InvalidHandle { + if e1 != 0 { + err = error(e1) + } else { + err = syscall.EINVAL + } + } + return +} + +func cancelIoEx(handle syscall.Handle, overlapped *syscall.Overlapped) (err error) { + r1, _, e1 := syscall.Syscall(procCancelIoEx.Addr(), 2, uintptr(handle), uintptr(unsafe.Pointer(overlapped)), 0) + if r1 == 0 { + if e1 != 0 { + err = error(e1) + } else { + err = syscall.EINVAL + } + } + return +} + +func connectNamedPipe(handle syscall.Handle, overlapped *syscall.Overlapped) (err error) { + r1, _, e1 := syscall.Syscall(procConnectNamedPipe.Addr(), 2, uintptr(handle), uintptr(unsafe.Pointer(overlapped)), 0) + if r1 == 0 { + if e1 != 0 { + err = error(e1) + } else { + err = syscall.EINVAL + } + } + return +} + +func disconnectNamedPipe(handle syscall.Handle) (err error) { + r1, _, e1 := syscall.Syscall(procDisconnectNamedPipe.Addr(), 1, uintptr(handle), 0, 0) + if r1 == 0 { + if e1 != 0 { + err = error(e1) + } else { + err = syscall.EINVAL + } + } + return +} + +func waitNamedPipe(name *uint16, timeout uint32) (err error) { + r1, _, e1 := syscall.Syscall(procWaitNamedPipeW.Addr(), 2, uintptr(unsafe.Pointer(name)), uintptr(timeout), 0) + if r1 == 0 { + if e1 != 0 { + err = error(e1) + } else { + err = syscall.EINVAL + } + } + return +} + +func createEvent(sa *syscall.SecurityAttributes, manualReset bool, initialState bool, name *uint16) (handle syscall.Handle, err error) { + var _p0 uint32 + if manualReset { + _p0 = 1 + } else { + _p0 = 0 + } + var _p1 uint32 + if initialState { + _p1 = 1 + } else { + _p1 = 0 + } + r0, _, e1 := syscall.Syscall6(procCreateEventW.Addr(), 4, uintptr(unsafe.Pointer(sa)), uintptr(_p0), uintptr(_p1), uintptr(unsafe.Pointer(name)), 0, 0) + handle = syscall.Handle(r0) + if handle == syscall.InvalidHandle { + if e1 != 0 { + err = error(e1) + } else { + err = syscall.EINVAL + } + } + return +} + +func getOverlappedResult(handle syscall.Handle, overlapped *syscall.Overlapped, transferred *uint32, wait bool) (err error) { + var _p0 uint32 + if wait { + _p0 = 1 + } else { + _p0 = 0 + } + r1, _, e1 := syscall.Syscall6(procGetOverlappedResult.Addr(), 4, uintptr(handle), uintptr(unsafe.Pointer(overlapped)), uintptr(unsafe.Pointer(transferred)), uintptr(_p0), 0, 0) + if r1 == 0 { + if e1 != 0 { + err = error(e1) + } else { + err = syscall.EINVAL + } + } + return +} + +const ( + // openMode + pipe_access_duplex = 0x3 + pipe_access_inbound = 0x1 + pipe_access_outbound = 0x2 + + // openMode write flags + file_flag_first_pipe_instance = 0x00080000 + file_flag_write_through = 0x80000000 + file_flag_overlapped = 0x40000000 + + // openMode ACL flags + write_dac = 0x00040000 + write_owner = 0x00080000 + access_system_security = 0x01000000 + + // pipeMode + pipe_type_byte = 0x0 + pipe_type_message = 0x4 + + // pipeMode read mode flags + pipe_readmode_byte = 0x0 + pipe_readmode_message = 0x2 + + // pipeMode wait mode flags + pipe_wait = 0x0 + pipe_nowait = 0x1 + + // pipeMode remote-client mode flags + pipe_accept_remote_clients = 0x0 + pipe_reject_remote_clients = 0x8 + + pipe_unlimited_instances = 255 + + nmpwait_wait_forever = 0xFFFFFFFF + + // the two not-an-errors below occur if a client connects to the pipe between + // the server's CreateNamedPipe and ConnectNamedPipe calls. + error_no_data syscall.Errno = 0xE8 + error_pipe_connected syscall.Errno = 0x217 + error_pipe_busy syscall.Errno = 0xE7 + error_sem_timeout syscall.Errno = 0x79 + + error_bad_pathname syscall.Errno = 0xA1 + error_invalid_name syscall.Errno = 0x7B + + error_io_incomplete syscall.Errno = 0x3e4 +) + +var _ net.Conn = (*PipeConn)(nil) +var _ net.Listener = (*PipeListener)(nil) + +// ErrClosed is the error returned by PipeListener.Accept when Close is called +// on the PipeListener. +var ErrClosed = PipeError{"Pipe has been closed.", false} + +// PipeError is an error related to a call to a pipe +type PipeError struct { + msg string + timeout bool +} + +// Error implements the error interface +func (e PipeError) Error() string { + return e.msg +} + +// Timeout implements net.AddrError.Timeout() +func (e PipeError) Timeout() bool { + return e.timeout +} + +// Temporary implements net.AddrError.Temporary() +func (e PipeError) Temporary() bool { + return false +} + +// Dial connects to a named pipe with the given address. If the specified pipe is not available, +// it will wait indefinitely for the pipe to become available. +// +// The address must be of the form \\.\\pipe\ for local pipes and \\\pipe\ +// for remote pipes. +// +// Dial will return a PipeError if you pass in a badly formatted pipe name. +// +// Examples: +// // local pipe +// conn, err := Dial(`\\.\pipe\mypipename`) +// +// // remote pipe +// conn, err := Dial(`\\othercomp\pipe\mypipename`) +func Dial(address string) (*PipeConn, error) { + for { + conn, err := dial(address, nmpwait_wait_forever) + if err == nil { + return conn, nil + } + if isPipeNotReady(err) { + <-time.After(100 * time.Millisecond) + continue + } + return nil, err + } +} + +// DialTimeout acts like Dial, but will time out after the duration of timeout +func DialTimeout(address string, timeout time.Duration) (*PipeConn, error) { + deadline := time.Now().Add(timeout) + + now := time.Now() + for now.Before(deadline) { + millis := uint32(deadline.Sub(now) / time.Millisecond) + conn, err := dial(address, millis) + if err == nil { + return conn, nil + } + if err == error_sem_timeout { + // This is WaitNamedPipe's timeout error, so we know we're done + return nil, PipeError{fmt.Sprintf( + "Timed out waiting for pipe '%s' to come available", address), true} + } + if isPipeNotReady(err) { + left := deadline.Sub(time.Now()) + retry := 100 * time.Millisecond + if left > retry { + <-time.After(retry) + } else { + <-time.After(left - time.Millisecond) + } + now = time.Now() + continue + } + return nil, err + } + return nil, PipeError{fmt.Sprintf( + "Timed out waiting for pipe '%s' to come available", address), true} +} + +// isPipeNotReady checks the error to see if it indicates the pipe is not ready +func isPipeNotReady(err error) bool { + // Pipe Busy means another client just grabbed the open pipe end, + // and the server hasn't made a new one yet. + // File Not Found means the server hasn't created the pipe yet. + // Neither is a fatal error. + + return err == syscall.ERROR_FILE_NOT_FOUND || err == error_pipe_busy +} + +// newOverlapped creates a structure used to track asynchronous +// I/O requests that have been issued. +func newOverlapped() (*syscall.Overlapped, error) { + event, err := createEvent(nil, true, true, nil) + if err != nil { + return nil, err + } + return &syscall.Overlapped{HEvent: event}, nil +} + +// waitForCompletion waits for an asynchronous I/O request referred to by overlapped to complete. +// This function returns the number of bytes transferred by the operation and an error code if +// applicable (nil otherwise). +func waitForCompletion(handle syscall.Handle, overlapped *syscall.Overlapped) (uint32, error) { + _, err := syscall.WaitForSingleObject(overlapped.HEvent, syscall.INFINITE) + if err != nil { + return 0, err + } + var transferred uint32 + err = getOverlappedResult(handle, overlapped, &transferred, true) + return transferred, err +} + +// dial is a helper to initiate a connection to a named pipe that has been started by a server. +// The timeout is only enforced if the pipe server has already created the pipe, otherwise +// this function will return immediately. +func dial(address string, timeout uint32) (*PipeConn, error) { + name, err := syscall.UTF16PtrFromString(string(address)) + if err != nil { + return nil, err + } + // If at least one instance of the pipe has been created, this function + // will wait timeout milliseconds for it to become available. + // It will return immediately regardless of timeout, if no instances + // of the named pipe have been created yet. + // If this returns with no error, there is a pipe available. + if err := waitNamedPipe(name, timeout); err != nil { + if err == error_bad_pathname { + // badly formatted pipe name + return nil, badAddr(address) + } + return nil, err + } + pathp, err := syscall.UTF16PtrFromString(address) + if err != nil { + return nil, err + } + handle, err := syscall.CreateFile(pathp, syscall.GENERIC_READ|syscall.GENERIC_WRITE, + uint32(syscall.FILE_SHARE_READ|syscall.FILE_SHARE_WRITE), nil, syscall.OPEN_EXISTING, + syscall.FILE_FLAG_OVERLAPPED, 0) + if err != nil { + return nil, err + } + return &PipeConn{handle: handle, addr: PipeAddr(address)}, nil +} + +// Listen returns a new PipeListener that will listen on a pipe with the given +// address. The address must be of the form \\.\pipe\ +// +// Listen will return a PipeError for an incorrectly formatted pipe name. +func Listen(address string) (*PipeListener, error) { + handle, err := createPipe(address, true) + if err == error_invalid_name { + return nil, badAddr(address) + } + if err != nil { + return nil, err + } + return &PipeListener{ + addr: PipeAddr(address), + handle: handle, + }, nil +} + +// PipeListener is a named pipe listener. Clients should typically +// use variables of type net.Listener instead of assuming named pipe. +type PipeListener struct { + addr PipeAddr + handle syscall.Handle + closed bool + + // acceptHandle contains the current handle waiting for + // an incoming connection or nil. + acceptHandle syscall.Handle + // acceptOverlapped is set before waiting on a connection. + // If not waiting, it is nil. + acceptOverlapped *syscall.Overlapped + // acceptMutex protects the handle and overlapped structure. + acceptMutex sync.Mutex +} + +// Accept implements the Accept method in the net.Listener interface; it +// waits for the next call and returns a generic net.Conn. +func (l *PipeListener) Accept() (net.Conn, error) { + c, err := l.AcceptPipe() + for err == error_no_data { + // Ignore clients that connect and immediately disconnect. + c, err = l.AcceptPipe() + } + if err != nil { + return nil, err + } + return c, nil +} + +// AcceptPipe accepts the next incoming call and returns the new connection. +// It might return an error if a client connected and immediately cancelled +// the connection. +func (l *PipeListener) AcceptPipe() (*PipeConn, error) { + if l == nil || l.addr == "" || l.closed { + return nil, syscall.EINVAL + } + + // the first time we call accept, the handle will have been created by the Listen + // call. This is to prevent race conditions where the client thinks the server + // isn't listening because it hasn't actually called create yet. After the first time, we'll + // have to create a new handle each time + handle := l.handle + if handle == 0 { + var err error + handle, err = createPipe(string(l.addr), false) + if err != nil { + return nil, err + } + } else { + l.handle = 0 + } + + overlapped, err := newOverlapped() + if err != nil { + return nil, err + } + defer syscall.CloseHandle(overlapped.HEvent) + if err := connectNamedPipe(handle, overlapped); err != nil && err != error_pipe_connected { + if err == error_io_incomplete || err == syscall.ERROR_IO_PENDING { + l.acceptMutex.Lock() + l.acceptOverlapped = overlapped + l.acceptHandle = handle + l.acceptMutex.Unlock() + defer func() { + l.acceptMutex.Lock() + l.acceptOverlapped = nil + l.acceptHandle = 0 + l.acceptMutex.Unlock() + }() + + _, err = waitForCompletion(handle, overlapped) + } + if err == syscall.ERROR_OPERATION_ABORTED { + // Return error compatible to net.Listener.Accept() in case the + // listener was closed. + return nil, ErrClosed + } + if err != nil { + return nil, err + } + } + return &PipeConn{handle: handle, addr: l.addr}, nil +} + +// Close stops listening on the address. +// Already Accepted connections are not closed. +func (l *PipeListener) Close() error { + if l.closed { + return nil + } + l.closed = true + if l.handle != 0 { + err := disconnectNamedPipe(l.handle) + if err != nil { + return err + } + err = syscall.CloseHandle(l.handle) + if err != nil { + return err + } + l.handle = 0 + } + l.acceptMutex.Lock() + defer l.acceptMutex.Unlock() + if l.acceptOverlapped != nil && l.acceptHandle != 0 { + // Cancel the pending IO. This call does not block, so it is safe + // to hold onto the mutex above. + if err := cancelIoEx(l.acceptHandle, l.acceptOverlapped); err != nil { + return err + } + err := syscall.CloseHandle(l.acceptOverlapped.HEvent) + if err != nil { + return err + } + l.acceptOverlapped.HEvent = 0 + err = syscall.CloseHandle(l.acceptHandle) + if err != nil { + return err + } + l.acceptHandle = 0 + } + return nil +} + +// Addr returns the listener's network address, a PipeAddr. +func (l *PipeListener) Addr() net.Addr { return l.addr } + +// PipeConn is the implementation of the net.Conn interface for named pipe connections. +type PipeConn struct { + handle syscall.Handle + addr PipeAddr + + // these aren't actually used yet + readDeadline *time.Time + writeDeadline *time.Time +} + +type iodata struct { + n uint32 + err error +} + +// completeRequest looks at iodata to see if a request is pending. If so, it waits for it to either complete or to +// abort due to hitting the specified deadline. Deadline may be set to nil to wait forever. If no request is pending, +// the content of iodata is returned. +func (c *PipeConn) completeRequest(data iodata, deadline *time.Time, overlapped *syscall.Overlapped) (int, error) { + if data.err == error_io_incomplete || data.err == syscall.ERROR_IO_PENDING { + var timer <-chan time.Time + if deadline != nil { + if timeDiff := deadline.Sub(time.Now()); timeDiff > 0 { + timer = time.After(timeDiff) + } + } + done := make(chan iodata) + go func() { + n, err := waitForCompletion(c.handle, overlapped) + done <- iodata{n, err} + }() + select { + case data = <-done: + case <-timer: + syscall.CancelIoEx(c.handle, overlapped) + data = iodata{0, timeout(c.addr.String())} + } + } + // Windows will produce ERROR_BROKEN_PIPE upon closing + // a handle on the other end of a connection. Go RPC + // expects an io.EOF error in this case. + if data.err == syscall.ERROR_BROKEN_PIPE { + data.err = io.EOF + } + return int(data.n), data.err +} + +// Read implements the net.Conn Read method. +func (c *PipeConn) Read(b []byte) (int, error) { + // Use ReadFile() rather than Read() because the latter + // contains a workaround that eats ERROR_BROKEN_PIPE. + overlapped, err := newOverlapped() + if err != nil { + return 0, err + } + defer syscall.CloseHandle(overlapped.HEvent) + var n uint32 + err = syscall.ReadFile(c.handle, b, &n, overlapped) + return c.completeRequest(iodata{n, err}, c.readDeadline, overlapped) +} + +// Write implements the net.Conn Write method. +func (c *PipeConn) Write(b []byte) (int, error) { + overlapped, err := newOverlapped() + if err != nil { + return 0, err + } + defer syscall.CloseHandle(overlapped.HEvent) + var n uint32 + err = syscall.WriteFile(c.handle, b, &n, overlapped) + return c.completeRequest(iodata{n, err}, c.writeDeadline, overlapped) +} + +// Close closes the connection. +func (c *PipeConn) Close() error { + return syscall.CloseHandle(c.handle) +} + +// LocalAddr returns the local network address. +func (c *PipeConn) LocalAddr() net.Addr { + return c.addr +} + +// RemoteAddr returns the remote network address. +func (c *PipeConn) RemoteAddr() net.Addr { + // not sure what to do here, we don't have remote addr.... + return c.addr +} + +// SetDeadline implements the net.Conn SetDeadline method. +// Note that timeouts are only supported on Windows Vista/Server 2008 and above +func (c *PipeConn) SetDeadline(t time.Time) error { + c.SetReadDeadline(t) + c.SetWriteDeadline(t) + return nil +} + +// SetReadDeadline implements the net.Conn SetReadDeadline method. +// Note that timeouts are only supported on Windows Vista/Server 2008 and above +func (c *PipeConn) SetReadDeadline(t time.Time) error { + c.readDeadline = &t + return nil +} + +// SetWriteDeadline implements the net.Conn SetWriteDeadline method. +// Note that timeouts are only supported on Windows Vista/Server 2008 and above +func (c *PipeConn) SetWriteDeadline(t time.Time) error { + c.writeDeadline = &t + return nil +} + +// PipeAddr represents the address of a named pipe. +type PipeAddr string + +// Network returns the address's network name, "pipe". +func (a PipeAddr) Network() string { return "pipe" } + +// String returns the address of the pipe +func (a PipeAddr) String() string { + return string(a) +} + +// createPipe is a helper function to make sure we always create pipes +// with the same arguments, since subsequent calls to create pipe need +// to use the same arguments as the first one. If first is set, fail +// if the pipe already exists. +func createPipe(address string, first bool) (syscall.Handle, error) { + n, err := syscall.UTF16PtrFromString(address) + if err != nil { + return 0, err + } + mode := uint32(pipe_access_duplex | syscall.FILE_FLAG_OVERLAPPED) + if first { + mode |= file_flag_first_pipe_instance + } + return createNamedPipe(n, + mode, + pipe_type_byte, + pipe_unlimited_instances, + 512, 512, 0, nil) +} + +func badAddr(addr string) PipeError { + return PipeError{fmt.Sprintf("Invalid pipe address '%s'.", addr), false} +} +func timeout(addr string) PipeError { + return PipeError{fmt.Sprintf("Pipe IO timed out waiting for '%s'", addr), true} +} + +// ipcListen will create a named pipe on the given endpoint. +func ipcListen(endpoint string) (net.Listener, error) { + return Listen(endpoint) +} + +// newIPCConnection will connect to a named pipe with the given endpoint as name. +func newIPCConnection(endpoint string) (net.Conn, error) { + return Dial(endpoint) +} diff --git a/rpc/javascript.go b/rpc/javascript.go new file mode 100644 index 000000000..d147aa045 --- /dev/null +++ b/rpc/javascript.go @@ -0,0 +1,414 @@ +// Copyright 2015 The go-ethereum Authors +// This file is part of the go-ethereum library. +// +// The go-ethereum library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The go-ethereum library 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 Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the go-ethereum library. If not, see . + +package rpc + +var ( + // Holds geth specific RPC extends which can be used to extend web3 + WEB3Extensions = map[string]string{ + "personal": Personal_JS, + "txpool": TxPool_JS, + "admin": Admin_JS, + "db": Db_JS, + "eth": Eth_JS, + "miner": Miner_JS, + "debug": Debug_JS, + "net": Net_JS, + } +) + +const Personal_JS = ` +web3._extend({ + property: 'personal', + methods: + [ + new web3._extend.Method({ + name: 'newAccount', + call: 'personal_newAccount', + params: 1, + outputFormatter: web3._extend.utils.toAddress + }), + new web3._extend.Method({ + name: 'unlockAccount', + call: 'personal_unlockAccount', + params: 3, + }), + new web3._extend.Method({ + name: 'lockAccount', + call: 'personal_lockAccount', + params: 1 + }) + ], + properties: + [ + new web3._extend.Property({ + name: 'listAccounts', + getter: 'personal_listAccounts' + }) + ] +}); +` + +const TxPool_JS = ` +web3._extend({ + property: 'txpool', + methods: + [ + ], + properties: + [ + new web3._extend.Property({ + name: 'status', + getter: 'txpool_status' + outputFormatter: function(status) { + status.pending = web3._extend.utils.toDecimal(status.pending); + status.queued = web3._extend.utils.toDecimal(status.queued); + return status; + } + }) + ] +}); +` + +const Admin_JS = ` +web3._extend({ + property: 'admin', + methods: + [ + new web3._extend.Method({ + name: 'addPeer', + call: 'admin_addPeer', + params: 1 + }), + new web3._extend.Method({ + name: 'exportChain', + call: 'admin_exportChain', + params: 1, + inputFormatter: [null] + }), + new web3._extend.Method({ + name: 'importChain', + call: 'admin_importChain', + params: 1 + }), + new web3._extend.Method({ + name: 'sleepBlocks', + call: 'admin_sleepBlocks', + params: 2 + }), + new web3._extend.Method({ + name: 'verbosity', + call: 'admin_verbosity', + params: 1, + inputFormatter: [web3._extend.utils.fromDecimal] + }), + new web3._extend.Method({ + name: 'setSolc', + call: 'admin_setSolc', + params: 1 + }), + new web3._extend.Method({ + name: 'startRPC', + call: 'admin_startRPC', + params: 4 + }), + new web3._extend.Method({ + name: 'stopRPC', + call: 'admin_stopRPC', + params: 0 + }), + new web3._extend.Method({ + name: 'startWS', + call: 'admin_startWS', + params: 4 + }), + new web3._extend.Method({ + name: 'stopWS', + call: 'admin_stopWS', + params: 0 + }), + new web3._extend.Method({ + name: 'setGlobalRegistrar', + call: 'admin_setGlobalRegistrar', + params: 2 + }), + new web3._extend.Method({ + name: 'setHashReg', + call: 'admin_setHashReg', + params: 2 + }), + new web3._extend.Method({ + name: 'setUrlHint', + call: 'admin_setUrlHint', + params: 2 + }), + new web3._extend.Method({ + name: 'saveInfo', + call: 'admin_saveInfo', + params: 2 + }), + new web3._extend.Method({ + name: 'register', + call: 'admin_register', + params: 3 + }), + new web3._extend.Method({ + name: 'registerUrl', + call: 'admin_registerUrl', + params: 3 + }), + new web3._extend.Method({ + name: 'startNatSpec', + call: 'admin_startNatSpec', + params: 0 + }), + new web3._extend.Method({ + name: 'stopNatSpec', + call: 'admin_stopNatSpec', + params: 0 + }), + new web3._extend.Method({ + name: 'getContractInfo', + call: 'admin_getContractInfo', + params: 1 + }), + new web3._extend.Method({ + name: 'httpGet', + call: 'admin_httpGet', + params: 2 + }) + ], + properties: + [ + new web3._extend.Property({ + name: 'nodeInfo', + getter: 'admin_nodeInfo' + }), + new web3._extend.Property({ + name: 'peers', + getter: 'admin_peers' + }), + new web3._extend.Property({ + name: 'datadir', + getter: 'admin_datadir' + }) + ] +}); +` + +const Db_JS = ` +web3._extend({ + property: 'db', + methods: + [ + ], + properties: + [ + ] +}); +` + +const Eth_JS = ` +web3._extend({ + property: 'eth', + methods: + [ + new web3._extend.Method({ + name: 'sign', + call: 'eth_sign', + params: 2, + inputFormatter: [web3._extend.utils.toAddress, null] + }), + new web3._extend.Method({ + name: 'resend', + call: 'eth_resend', + params: 3, + inputFormatter: [web3._extend.formatters.inputTransactionFormatter, web3._extend.utils.fromDecimal, web3._extend.utils.fromDecimal] + }), + new web3._extend.Method({ + name: 'getNatSpec', + call: 'eth_getNatSpec', + params: 1, + inputFormatter: [web3._extend.formatters.inputTransactionFormatter] + }), + new web3._extend.Method({ + name: 'signTransaction', + call: 'eth_signTransaction', + params: 1, + inputFormatter: [web3._extend.formatters.inputTransactionFormatter] + }), + new web3._extend.Method({ + name: 'submitTransaction', + call: 'eth_submitTransaction', + params: 1, + inputFormatter: [web3._extend.formatters.inputTransactionFormatter] + }) + ], + properties: + [ + new web3._extend.Property({ + name: 'pendingTransactions', + getter: 'eth_pendingTransactions' + }) + ] +}); +` + +const Net_JS = ` +web3._extend({ + property: 'net', + methods: + [ + new web3._extend.Method({ + name: 'addPeer', + call: 'net_addPeer', + params: 1 + }) + ], + properties: + [ + new web3._extend.Property({ + name: 'version', + getter: 'net_version' + }) + ] +}); +` + +const Debug_JS = ` +web3._extend({ + property: 'debug', + methods: + [ + new web3._extend.Method({ + name: 'printBlock', + call: 'debug_printBlock', + params: 1 + }), + new web3._extend.Method({ + name: 'getBlockRlp', + call: 'debug_getBlockRlp', + params: 1 + }), + new web3._extend.Method({ + name: 'setHead', + call: 'debug_setHead', + params: 1 + }), + new web3._extend.Method({ + name: 'processBlock', + call: 'debug_processBlock', + params: 1 + }), + new web3._extend.Method({ + name: 'seedHash', + call: 'debug_seedHash', + params: 1 + }), + new web3._extend.Method({ + name: 'dumpBlock', + call: 'debug_dumpBlock', + params: 1 + }), + new web3._extend.Method({ + name: 'metrics', + call: 'debug_metrics', + params: 1 + }) + ], + properties: + [ + ] +}); +` + +const Miner_JS = ` +web3._extend({ + property: 'miner', + methods: + [ + new web3._extend.Method({ + name: 'start', + call: 'miner_start', + params: 1 + }), + new web3._extend.Method({ + name: 'stop', + call: 'miner_stop', + params: 1 + }), + new web3._extend.Method({ + name: 'setEtherbase', + call: 'miner_setEtherbase', + params: 1, + inputFormatter: [web3._extend.formatters.formatInputInt], + outputFormatter: web3._extend.formatters.formatOutputBool + }), + new web3._extend.Method({ + name: 'setExtra', + call: 'miner_setExtra', + params: 1 + }), + new web3._extend.Method({ + name: 'setGasPrice', + call: 'miner_setGasPrice', + params: 1, + inputFormatter: [web3._extend.utils.fromDecial] + }), + new web3._extend.Method({ + name: 'startAutoDAG', + call: 'miner_startAutoDAG', + params: 0 + }), + new web3._extend.Method({ + name: 'stopAutoDAG', + call: 'miner_stopAutoDAG', + params: 0 + }), + new web3._extend.Method({ + name: 'makeDAG', + call: 'miner_makeDAG', + params: 1, + inputFormatter: [web3._extend.formatters.inputDefaultBlockNumberFormatter] + }) + ], + properties: + [ + new web3._extend.Property({ + name: 'hashrate', + getter: 'miner_hashrate', + outputFormatter: web3._extend.utils.toDecimal + }) + ] +}); +` + +const Shh_JS = ` +web3._extend({ + property: 'shh', + methods: + [ + + ], + properties: + [ + new web3._extend.Property({ + name: 'version', + getter: 'shh_version' + }) + ] +}); +` diff --git a/rpc/jeth.go b/rpc/jeth.go deleted file mode 100644 index b195a4965..000000000 --- a/rpc/jeth.go +++ /dev/null @@ -1,324 +0,0 @@ -// Copyright 2015 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library 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 Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see . - -package rpc - -import ( - "encoding/json" - "fmt" - "time" - - "github.com/ethereum/go-ethereum/cmd/utils" - "github.com/ethereum/go-ethereum/jsre" - "github.com/ethereum/go-ethereum/logger" - "github.com/ethereum/go-ethereum/logger/glog" - "github.com/ethereum/go-ethereum/rpc/comms" - "github.com/ethereum/go-ethereum/rpc/shared" - "github.com/ethereum/go-ethereum/rpc/useragent" - "github.com/ethereum/go-ethereum/xeth" - - "github.com/robertkrimen/otto" -) - -type Jeth struct { - ethApi shared.EthereumApi - re *jsre.JSRE - client comms.EthereumClient - fe xeth.Frontend -} - -func NewJeth(ethApi shared.EthereumApi, re *jsre.JSRE, client comms.EthereumClient, fe xeth.Frontend) *Jeth { - return &Jeth{ethApi, re, client, fe} -} - -func (self *Jeth) err(call otto.FunctionCall, code int, msg string, id interface{}) (response otto.Value) { - m := shared.NewRpcErrorResponse(id, shared.JsonRpcVersion, code, fmt.Errorf(msg)) - errObj, _ := json.Marshal(m.Error) - errRes, _ := json.Marshal(m) - - call.Otto.Run("ret_error = " + string(errObj)) - res, _ := call.Otto.Run("ret_response = " + string(errRes)) - - return res -} - -// UnlockAccount asks the user for the password and than executes the jeth.UnlockAccount callback in the jsre -func (self *Jeth) UnlockAccount(call otto.FunctionCall) (response otto.Value) { - var cmd, account, passwd string - timeout := int64(300) - var ok bool - - if len(call.ArgumentList) == 0 { - fmt.Println("expected address of account to unlock") - return otto.FalseValue() - } - - if len(call.ArgumentList) >= 1 { - if accountExport, err := call.Argument(0).Export(); err == nil { - if account, ok = accountExport.(string); ok { - if len(call.ArgumentList) == 1 { - fmt.Printf("Unlock account %s\n", account) - passwd, err = utils.PromptPassword("Passphrase: ", true) - if err != nil { - return otto.FalseValue() - } - } - } - } - } - if len(call.ArgumentList) >= 2 { - if passwdExport, err := call.Argument(1).Export(); err == nil { - passwd, _ = passwdExport.(string) - } - } - - if len(call.ArgumentList) >= 3 { - if timeoutExport, err := call.Argument(2).Export(); err == nil { - timeout, _ = timeoutExport.(int64) - } - } - - cmd = fmt.Sprintf("jeth.unlockAccount('%s', '%s', %d)", account, passwd, timeout) - if val, err := call.Otto.Run(cmd); err == nil { - return val - } - - return otto.FalseValue() -} - -// NewAccount asks the user for the password and than executes the jeth.newAccount callback in the jsre -func (self *Jeth) NewAccount(call otto.FunctionCall) (response otto.Value) { - if len(call.ArgumentList) == 0 { - passwd, err := utils.PromptPassword("Passphrase: ", true) - if err != nil { - return otto.FalseValue() - } - passwd2, err := utils.PromptPassword("Repeat passphrase: ", true) - if err != nil { - return otto.FalseValue() - } - - if passwd != passwd2 { - fmt.Println("Passphrases don't match") - return otto.FalseValue() - } - - cmd := fmt.Sprintf("jeth.newAccount('%s')", passwd) - if val, err := call.Otto.Run(cmd); err == nil { - return val - } - } else { - fmt.Println("New account doesn't expect argument(s), you will be prompted for a password") - } - - return otto.FalseValue() -} - -func (self *Jeth) Send(call otto.FunctionCall) (response otto.Value) { - reqif, err := call.Argument(0).Export() - if err != nil { - return self.err(call, -32700, err.Error(), nil) - } - - jsonreq, err := json.Marshal(reqif) - var reqs []shared.Request - batch := true - err = json.Unmarshal(jsonreq, &reqs) - if err != nil { - reqs = make([]shared.Request, 1) - err = json.Unmarshal(jsonreq, &reqs[0]) - batch = false - } - - call.Otto.Set("response_len", len(reqs)) - call.Otto.Run("var ret_response = new Array(response_len);") - - for i, req := range reqs { - var respif interface{} - err := self.client.Send(&req) - if err != nil { - return self.err(call, -32603, err.Error(), req.Id) - } - - recv: - respif, err = self.client.Recv() - if err != nil { - return self.err(call, -32603, err.Error(), req.Id) - } - - agentreq, isRequest := respif.(*shared.Request) - if isRequest { - self.handleRequest(agentreq) - goto recv // receive response after agent interaction - } - - sucres, isSuccessResponse := respif.(*shared.SuccessResponse) - errres, isErrorResponse := respif.(*shared.ErrorResponse) - if !isSuccessResponse && !isErrorResponse { - return self.err(call, -32603, fmt.Sprintf("Invalid response type (%T)", respif), req.Id) - } - - call.Otto.Set("ret_jsonrpc", shared.JsonRpcVersion) - call.Otto.Set("ret_id", req.Id) - - var res []byte - if isSuccessResponse { - res, err = json.Marshal(sucres.Result) - } else if isErrorResponse { - res, err = json.Marshal(errres.Error) - } - - call.Otto.Set("ret_result", string(res)) - call.Otto.Set("response_idx", i) - response, err = call.Otto.Run(` - ret_response[response_idx] = { jsonrpc: ret_jsonrpc, id: ret_id, result: JSON.parse(ret_result) }; - `) - } - - if !batch { - call.Otto.Run("ret_response = ret_response[0];") - } - - if call.Argument(1).IsObject() { - call.Otto.Set("callback", call.Argument(1)) - call.Otto.Run(` - if (Object.prototype.toString.call(callback) == '[object Function]') { - callback(null, ret_response); - } - `) - } - - return -} - -// handleRequest will handle user agent requests by interacting with the user and sending -// the user response back to the geth service -func (self *Jeth) handleRequest(req *shared.Request) bool { - var err error - var args []interface{} - if err = json.Unmarshal(req.Params, &args); err != nil { - glog.V(logger.Info).Infof("Unable to parse agent request - %v\n", err) - return false - } - - switch req.Method { - case useragent.AskPasswordMethod: - return self.askPassword(req.Id, req.Jsonrpc, args) - case useragent.ConfirmTransactionMethod: - return self.confirmTransaction(req.Id, req.Jsonrpc, args) - } - - return false -} - -// askPassword will ask the user to supply the password for a given account -func (self *Jeth) askPassword(id interface{}, jsonrpc string, args []interface{}) bool { - var err error - var passwd string - if len(args) >= 1 { - if account, ok := args[0].(string); ok { - fmt.Printf("Unlock account %s\n", account) - } else { - return false - } - } - passwd, err = utils.PromptPassword("Passphrase: ", true) - - if err = self.client.Send(shared.NewRpcResponse(id, jsonrpc, passwd, err)); err != nil { - glog.V(logger.Info).Infof("Unable to send user agent ask password response - %v\n", err) - } - - return err == nil -} - -func (self *Jeth) confirmTransaction(id interface{}, jsonrpc string, args []interface{}) bool { - // Accept all tx which are send from this console - return self.client.Send(shared.NewRpcResponse(id, jsonrpc, true, nil)) == nil -} - -// throwJSExeception panics on an otto value, the Otto VM will then throw msg as a javascript error. -func throwJSExeception(msg interface{}) otto.Value { - p, _ := otto.ToValue(msg) - panic(p) - return p -} - -// Sleep will halt the console for arg[0] seconds. -func (self *Jeth) Sleep(call otto.FunctionCall) (response otto.Value) { - if len(call.ArgumentList) >= 1 { - if call.Argument(0).IsNumber() { - sleep, _ := call.Argument(0).ToInteger() - time.Sleep(time.Duration(sleep) * time.Second) - return otto.TrueValue() - } - } - return throwJSExeception("usage: sleep()") -} - -// SleepBlocks will wait for a specified number of new blocks or max for a -// given of seconds. sleepBlocks(nBlocks[, maxSleep]). -func (self *Jeth) SleepBlocks(call otto.FunctionCall) (response otto.Value) { - nBlocks := int64(0) - maxSleep := int64(9999999999999999) // indefinitely - - nArgs := len(call.ArgumentList) - - if nArgs == 0 { - throwJSExeception("usage: sleepBlocks([, max sleep in seconds])") - } - - if nArgs >= 1 { - if call.Argument(0).IsNumber() { - nBlocks, _ = call.Argument(0).ToInteger() - } else { - throwJSExeception("expected number as first argument") - } - } - - if nArgs >= 2 { - if call.Argument(1).IsNumber() { - maxSleep, _ = call.Argument(1).ToInteger() - } else { - throwJSExeception("expected number as second argument") - } - } - - // go through the console, this will allow web3 to call the appropriate - // callbacks if a delayed response or notification is received. - currentBlockNr := func() int64 { - result, err := call.Otto.Run("eth.blockNumber") - if err != nil { - throwJSExeception(err.Error()) - } - blockNr, err := result.ToInteger() - if err != nil { - throwJSExeception(err.Error()) - } - return blockNr - } - - targetBlockNr := currentBlockNr() + nBlocks - deadline := time.Now().Add(time.Duration(maxSleep) * time.Second) - - for time.Now().Before(deadline) { - if currentBlockNr() >= targetBlockNr { - return otto.TrueValue() - } - time.Sleep(time.Second) - } - - return otto.FalseValue() -} diff --git a/rpc/json.go b/rpc/json.go new file mode 100644 index 000000000..8bdb4665d --- /dev/null +++ b/rpc/json.go @@ -0,0 +1,343 @@ +// Copyright 2015 The go-ethereum Authors +// This file is part of the go-ethereum library. +// +// The go-ethereum library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The go-ethereum library 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 Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the go-ethereum library. If not, see . + +package rpc + +import ( + "encoding/json" + "fmt" + "io" + "reflect" + "strings" + "sync/atomic" + + "github.com/ethereum/go-ethereum/logger" + "github.com/ethereum/go-ethereum/logger/glog" +) + +const ( + jsonRPCVersion = "2.0" + serviceMethodSeparator = "_" + subscribeMethod = "eth_subscribe" + unsubscribeMethod = "eth_unsubscribe" + notificationMethod = "eth_subscription" +) + +// JSON-RPC request +type JSONRequest struct { + Method string `json:"method"` + Version string `json:"jsonrpc"` + Id *int64 `json:"id,omitempty"` + Payload json.RawMessage `json:"params"` +} + +// JSON-RPC response +type JSONSuccessResponse struct { + Version string `json:"jsonrpc"` + Id int64 `json:"id"` + Result interface{} `json:"result,omitempty"` +} + +// JSON-RPC error object +type JSONError struct { + Code int `json:"code"` + Message string `json:"message"` + Data interface{} `json:"data,omitempty"` +} + +// JSON-RPC error response +type JSONErrResponse struct { + Version string `json:"jsonrpc"` + Id *int64 `json:"id,omitempty"` + Error JSONError `json:"error"` +} + +// JSON-RPC notification payload +type jsonSubscription struct { + Subscription string `json:"subscription"` + Result interface{} `json:"result,omitempty"` +} + +// JSON-RPC notification +type jsonNotification struct { + Version string `json:"jsonrpc"` + Method string `json:"method"` + Params jsonSubscription `json:"params"` +} + +// jsonCodec reads and writes JSON-RPC messages to the underlying connection. It also has support for parsing arguments +// and serializing (result) objects. +type jsonCodec struct { + closed chan interface{} + isClosed int32 + d *json.Decoder + e *json.Encoder + req JSONRequest + rw io.ReadWriteCloser +} + +// NewJSONCodec creates a new RPC server codec with support for JSON-RPC 2.0 +func NewJSONCodec(rwc io.ReadWriteCloser) ServerCodec { + d := json.NewDecoder(rwc) + d.UseNumber() + return &jsonCodec{closed: make(chan interface{}), d: d, e: json.NewEncoder(rwc), rw: rwc, isClosed: 0} +} + +// isBatch returns true when the first non-whitespace characters is '[' +func isBatch(msg json.RawMessage) bool { + for _, c := range msg { + // skip insignificant whitespace (http://www.ietf.org/rfc/rfc4627.txt) + if c == 0x20 || c == 0x09 || c == 0x0a || c == 0x0d { + continue + } + return c == '[' + } + return false +} + +// ReadRequestHeaders will read new requests without parsing the arguments. It will return a collection of requests, an +// indication if these requests are in batch form or an error when the incoming message could not be read/parsed. +func (c *jsonCodec) ReadRequestHeaders() ([]rpcRequest, bool, RPCError) { + var incomingMsg json.RawMessage + if err := c.d.Decode(&incomingMsg); err != nil { + return nil, false, &invalidRequestError{err.Error()} + } + + if isBatch(incomingMsg) { + return parseBatchRequest(incomingMsg) + } + + return parseRequest(incomingMsg) +} + +// parseRequest will parse a single request from the given RawMessage. It will return the parsed request, an indication +// if the request was a batch or an error when the request could not be parsed. +func parseRequest(incomingMsg json.RawMessage) ([]rpcRequest, bool, RPCError) { + var in JSONRequest + if err := json.Unmarshal(incomingMsg, &in); err != nil { + return nil, false, &invalidMessageError{err.Error()} + } + + if in.Id == nil { + return nil, false, &invalidMessageError{"Server cannot handle notifications"} + } + + // subscribe are special, they will always use `subscribeMethod` as service method + if in.Method == subscribeMethod { + reqs := []rpcRequest{rpcRequest{id: *in.Id, isPubSub: true}} + if len(in.Payload) > 0 { + // first param must be subscription name + var subscribeMethod [1]string + if err := json.Unmarshal(in.Payload, &subscribeMethod); err != nil { + glog.V(logger.Debug).Infof("Unable to parse subscription method: %v\n", err) + return nil, false, &invalidRequestError{"Unable to parse subscription request"} + } + + // all subscriptions are made on the eth service + reqs[0].service, reqs[0].method = "eth", subscribeMethod[0] + reqs[0].params = in.Payload + return reqs, false, nil + } + return nil, false, &invalidRequestError{"Unable to parse subscription request"} + } + + if in.Method == unsubscribeMethod { + return []rpcRequest{rpcRequest{id: *in.Id, isPubSub: true, + method: unsubscribeMethod, params: in.Payload}}, false, nil + } + + // regular RPC call + elems := strings.Split(in.Method, serviceMethodSeparator) + if len(elems) != 2 { + return nil, false, &methodNotFoundError{in.Method, ""} + } + + if len(in.Payload) == 0 { + return []rpcRequest{rpcRequest{service: elems[0], method: elems[1], id: *in.Id}}, false, nil + } + + return []rpcRequest{rpcRequest{service: elems[0], method: elems[1], id: *in.Id, params: in.Payload}}, false, nil +} + +// parseBatchRequest will parse a batch request into a collection of requests from the given RawMessage, an indication +// if the request was a batch or an error when the request could not be read. +func parseBatchRequest(incomingMsg json.RawMessage) ([]rpcRequest, bool, RPCError) { + var in []JSONRequest + if err := json.Unmarshal(incomingMsg, &in); err != nil { + return nil, false, &invalidMessageError{err.Error()} + } + + requests := make([]rpcRequest, len(in)) + for i, r := range in { + if r.Id == nil { + return nil, true, &invalidMessageError{"Server cannot handle notifications"} + } + + // (un)subscribe are special, they will always use the same service.method + if r.Method == subscribeMethod { + requests[i] = rpcRequest{id: *r.Id, isPubSub: true} + if len(r.Payload) > 0 { + var subscribeMethod [1]string + if err := json.Unmarshal(r.Payload, &subscribeMethod); err != nil { + glog.V(logger.Debug).Infof("Unable to parse subscription method: %v\n", err) + return nil, false, &invalidRequestError{"Unable to parse subscription request"} + } + + // all subscriptions are made on the eth service + requests[i].service, requests[i].method = "eth", subscribeMethod[0] + requests[i].params = r.Payload + continue + } + + return nil, true, &invalidRequestError{"Unable to parse (un)subscribe request arguments"} + } + + if r.Method == unsubscribeMethod { + requests[i] = rpcRequest{id: *r.Id, isPubSub: true, method: unsubscribeMethod, params: r.Payload} + continue + } + + elems := strings.Split(r.Method, serviceMethodSeparator) + if len(elems) != 2 { + return nil, true, &methodNotFoundError{r.Method, ""} + } + + if len(r.Payload) == 0 { + requests[i] = rpcRequest{service: elems[0], method: elems[1], id: *r.Id, params: nil} + } else { + requests[i] = rpcRequest{service: elems[0], method: elems[1], id: *r.Id, params: r.Payload} + } + } + + return requests, true, nil +} + +// ParseRequestArguments tries to parse the given params (json.RawMessage) with the given types. It returns the parsed +// values or an error when the parsing failed. +func (c *jsonCodec) ParseRequestArguments(argTypes []reflect.Type, params interface{}) ([]reflect.Value, RPCError) { + if args, ok := params.(json.RawMessage); !ok { + return nil, &invalidParamsError{"Invalid params supplied"} + } else { + return parsePositionalArguments(args, argTypes) + } +} + +func countArguments(args json.RawMessage) (int, error) { + var cnt []interface{} + if err := json.Unmarshal(args, &cnt); err != nil { + return -1, nil + } + return len(cnt), nil +} + +// parsePositionalArguments tries to parse the given args to an array of values with the given types. It returns the +// parsed values or an error when the args could not be parsed. +func parsePositionalArguments(args json.RawMessage, argTypes []reflect.Type) ([]reflect.Value, RPCError) { + argValues := make([]reflect.Value, len(argTypes)) + params := make([]interface{}, len(argTypes)) + + n, err := countArguments(args) + if err != nil { + return nil, &invalidParamsError{err.Error()} + } + if n != len(argTypes) { + return nil, &invalidParamsError{fmt.Sprintf("insufficient params, want %d have %d", len(argTypes), n)} + } + + for i, t := range argTypes { + if t.Kind() == reflect.Ptr { + // values must be pointers for the Unmarshal method, reflect. + // Dereference otherwise reflect.New would create **SomeType + argValues[i] = reflect.New(t.Elem()) + params[i] = argValues[i].Interface() + + // when not specified blockNumbers are by default latest (-1) + if blockNumber, ok := params[i].(*BlockNumber); ok { + *blockNumber = BlockNumber(-1) + } + } else { + argValues[i] = reflect.New(t) + params[i] = argValues[i].Interface() + + // when not specified blockNumbers are by default latest (-1) + if blockNumber, ok := params[i].(*BlockNumber); ok { + *blockNumber = BlockNumber(-1) + } + } + } + + if err := json.Unmarshal(args, ¶ms); err != nil { + return nil, &invalidParamsError{err.Error()} + } + + // Convert pointers back to values where necessary + for i, a := range argValues { + if a.Kind() != argTypes[i].Kind() { + argValues[i] = reflect.Indirect(argValues[i]) + } + } + + return argValues, nil +} + +// CreateResponse will create a JSON-RPC success response with the given id and reply as result. +func (c *jsonCodec) CreateResponse(id int64, reply interface{}) interface{} { + if isHexNum(reflect.TypeOf(reply)) { + return &JSONSuccessResponse{Version: jsonRPCVersion, Id: id, Result: fmt.Sprintf(`%#x`, reply)} + } + return &JSONSuccessResponse{Version: jsonRPCVersion, Id: id, Result: reply} +} + +// CreateErrorResponse will create a JSON-RPC error response with the given id and error. +func (c *jsonCodec) CreateErrorResponse(id *int64, err RPCError) interface{} { + return &JSONErrResponse{Version: jsonRPCVersion, Id: id, Error: JSONError{Code: err.Code(), Message: err.Error()}} +} + +// CreateErrorResponseWithInfo will create a JSON-RPC error response with the given id and error. +// info is optional and contains additional information about the error. When an empty string is passed it is ignored. +func (c *jsonCodec) CreateErrorResponseWithInfo(id *int64, err RPCError, info interface{}) interface{} { + return &JSONErrResponse{Version: jsonRPCVersion, Id: id, + Error: JSONError{Code: err.Code(), Message: err.Error(), Data: info}} +} + +// CreateNotification will create a JSON-RPC notification with the given subscription id and event as params. +func (c *jsonCodec) CreateNotification(subid string, event interface{}) interface{} { + if isHexNum(reflect.TypeOf(event)) { + return &jsonNotification{Version: jsonRPCVersion, Method: notificationMethod, + Params: jsonSubscription{Subscription: subid, Result: fmt.Sprintf(`%#x`, event)}} + } + + return &jsonNotification{Version: jsonRPCVersion, Method: notificationMethod, + Params: jsonSubscription{Subscription: subid, Result: event}} +} + +// Write message to client +func (c *jsonCodec) Write(res interface{}) error { + return c.e.Encode(res) +} + +// Close the underlying connection +func (c *jsonCodec) Close() { + if atomic.CompareAndSwapInt32(&c.isClosed, 0, 1) { + close(c.closed) + c.rw.Close() + } +} + +// Closed returns a channel which will be closed when Close is called +func (c *jsonCodec) Closed() <-chan interface{} { + return c.closed +} diff --git a/rpc/json_test.go b/rpc/json_test.go new file mode 100644 index 000000000..39aae1f54 --- /dev/null +++ b/rpc/json_test.go @@ -0,0 +1,73 @@ +package rpc + +import ( + "bufio" + "bytes" + "reflect" + "testing" +) + +type RWC struct { + *bufio.ReadWriter +} + +func (rwc *RWC) Close() error { + return nil +} + +func TestJSONRequestParsing(t *testing.T) { + server := NewServer() + service := new(Service) + + if err := server.RegisterName("calc", service); err != nil { + t.Fatalf("%v", err) + } + + req := bytes.NewBufferString(`{"id": 1234, "jsonrpc": "2.0", "method": "calc_add", "params": [11, 22]}`) + var str string + reply := bytes.NewBufferString(str) + rw := &RWC{bufio.NewReadWriter(bufio.NewReader(req), bufio.NewWriter(reply))} + + codec := NewJSONCodec(rw) + + requests, batch, err := codec.ReadRequestHeaders() + if err != nil { + t.Fatalf("%v", err) + } + + if batch { + t.Fatalf("Request isn't a batch") + } + + if len(requests) != 1 { + t.Fatalf("Expected 1 request but got %d requests - %v", len(requests), requests) + } + + if requests[0].service != "calc" { + t.Fatalf("Expected service 'calc' but got '%s'", requests[0].service) + } + + if requests[0].method != "add" { + t.Fatalf("Expected method 'Add' but got '%s'", requests[0].method) + } + + if requests[0].id != 1234 { + t.Fatalf("Expected id 1234 but got %d", requests[0].id) + } + + var arg int + args := []reflect.Type{reflect.TypeOf(arg), reflect.TypeOf(arg)} + + v, err := codec.ParseRequestArguments(args, requests[0].params) + if err != nil { + t.Fatalf("%v", err) + } + + if len(v) != 2 { + t.Fatalf("Expected 2 argument values, got %d", len(v)) + } + + if v[0].Int() != 11 || v[1].Int() != 22 { + t.Fatalf("expected %d == 11 && %d == 22", v[0].Int(), v[1].Int()) + } +} diff --git a/rpc/server.go b/rpc/server.go new file mode 100644 index 000000000..0b93a4e64 --- /dev/null +++ b/rpc/server.go @@ -0,0 +1,438 @@ +// Copyright 2015 The go-ethereum Authors +// This file is part of the go-ethereum library. +// +// The go-ethereum library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The go-ethereum library 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 Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the go-ethereum library. If not, see . + +package rpc + +import ( + "fmt" + "reflect" + "runtime" + "sync/atomic" + "time" + + "github.com/ethereum/go-ethereum/event" + "github.com/ethereum/go-ethereum/logger" + "github.com/ethereum/go-ethereum/logger/glog" + "golang.org/x/net/context" + "gopkg.in/fatih/set.v0" +) + +const ( + stopPendingRequestTimeout = 3 * time.Second // give pending requests stopPendingRequestTimeout the time to finish when the server is stopped + + DefaultIpcApis = "admin,eth,debug,miner,net,shh,txpool,personal,web3" + DefaultHttpRpcApis = "eth,net,web3" +) + +// NewServer will create a new server instance with no registered handlers. +func NewServer() *Server { + server := &Server{ + services: make(serviceRegistry), + subscriptions: make(subscriptionRegistry), + codecs: set.New(), + run: 1, + } + + // register a default service which will provide meta information about the RPC service such as the services and + // methods it offers. + rpcService := &RPCService{server} + server.RegisterName("rpc", rpcService) + + return server +} + +// RPCService gives meta information about the server. +// e.g. gives information about the loaded modules. +type RPCService struct { + server *Server +} + +// Modules returns the list of RPC services with their version number +func (s *RPCService) Modules() map[string]string { + modules := make(map[string]string) + for name, _ := range s.server.services { + modules[name] = "1.0" + } + return modules +} + +// RegisterName will create an service for the given rcvr type under the given name. When no methods on the given rcvr +// match the criteria to be either a RPC method or a subscription an error is returned. Otherwise a new service is +// created and added to the service collection this server instance serves. +func (s *Server) RegisterName(name string, rcvr interface{}) error { + if s.services == nil { + s.services = make(serviceRegistry) + } + + svc := new(service) + svc.typ = reflect.TypeOf(rcvr) + rcvrVal := reflect.ValueOf(rcvr) + + if name == "" { + return fmt.Errorf("no service name for type %s", svc.typ.String()) + } + if !isExported(reflect.Indirect(rcvrVal).Type().Name()) { + return fmt.Errorf("%s is not exported", reflect.Indirect(rcvrVal).Type().Name()) + } + + // already a previous service register under given sname, merge methods/subscriptions + if regsvc, present := s.services[name]; present { + methods, subscriptions := suitableCallbacks(rcvrVal, svc.typ) + if len(methods) == 0 && len(subscriptions) == 0 { + return fmt.Errorf("Service doesn't have any suitable methods/subscriptions to expose") + } + + for _, m := range methods { + regsvc.callbacks[formatName(m.method.Name)] = m + } + for _, s := range subscriptions { + regsvc.subscriptions[formatName(s.method.Name)] = s + } + + return nil + } + + svc.name = name + svc.callbacks, svc.subscriptions = suitableCallbacks(rcvrVal, svc.typ) + + if len(svc.callbacks) == 0 && len(svc.subscriptions) == 0 { + return fmt.Errorf("Service doesn't have any suitable methods/subscriptions to expose") + } + + s.services[svc.name] = svc + + return nil +} + +// ServeCodec reads incoming requests from codec, calls the appropriate callback and writes the +// response back using the given codec. It will block until the codec is closed. +// +// This server will: +// 1. allow for asynchronous and parallel request execution +// 2. supports notifications (pub/sub) +// 3. supports request batches +func (s *Server) ServeCodec(codec ServerCodec) { + defer func() { + if err := recover(); err != nil { + const size = 64 << 10 + buf := make([]byte, size) + buf = buf[:runtime.Stack(buf, false)] + glog.Errorln(string(buf)) + } + codec.Close() + }() + + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + s.codecsMu.Lock() + if atomic.LoadInt32(&s.run) != 1 { // server stopped + s.codecsMu.Unlock() + return + } + s.codecs.Add(codec) + s.codecsMu.Unlock() + + for atomic.LoadInt32(&s.run) == 1 { + reqs, batch, err := s.readRequest(codec) + + if err != nil { + glog.V(logger.Debug).Infof("%v\n", err) + codec.Write(codec.CreateErrorResponse(nil, err)) + break + } + + if atomic.LoadInt32(&s.run) != 1 { + err = &shutdownError{} + if batch { + resps := make([]interface{}, len(reqs)) + for i, r := range reqs { + resps[i] = codec.CreateErrorResponse(&r.id, err) + } + codec.Write(resps) + } else { + codec.Write(codec.CreateErrorResponse(&reqs[0].id, err)) + } + break + } + + if batch { + go s.execBatch(ctx, codec, reqs) + } else { + go s.exec(ctx, codec, reqs[0]) + } + } +} + +// Stop will stop reading new requests, wait for stopPendingRequestTimeout to allow pending requests to finish, +// close all codecs which will cancels pending requests/subscriptions. +func (s *Server) Stop() { + if atomic.CompareAndSwapInt32(&s.run, 1, 0) { + glog.V(logger.Debug).Infoln("RPC Server shutdown initiatied") + time.AfterFunc(stopPendingRequestTimeout, func() { + s.codecsMu.Lock() + defer s.codecsMu.Unlock() + s.codecs.Each(func(c interface{}) bool { + c.(ServerCodec).Close() + return true + }) + }) + } +} + +// sendNotification will create a notification from the given event by serializing member fields of the event. +// It will then send the notification to the client, when it fails the codec is closed. When the event has multiple +// fields an array of values is returned. +func sendNotification(codec ServerCodec, subid string, event interface{}) { + notification := codec.CreateNotification(subid, event) + + if err := codec.Write(notification); err != nil { + codec.Close() + } +} + +// createSubscription will register a new subscription and waits for raised events. When an event is raised it will: +// 1. test if the event is raised matches the criteria the user has (optionally) specified +// 2. create a notification of the event and send it the client when it matches the criteria +// It will unsubscribe the subscription when the socket is closed or the subscription is unsubscribed by the user. +func (s *Server) createSubscription(c ServerCodec, req *serverRequest) (string, error) { + args := []reflect.Value{req.callb.rcvr} + if len(req.args) > 0 { + args = append(args, req.args...) + } + + subid, err := newSubscriptionId() + if err != nil { + return "", err + } + + reply := req.callb.method.Func.Call(args) + + if reply[1].IsNil() { // no error + if subscription, ok := reply[0].Interface().(Subscription); ok { + s.muSubcriptions.Lock() + s.subscriptions[subid] = subscription + s.muSubcriptions.Unlock() + go func() { + cases := []reflect.SelectCase{ + reflect.SelectCase{Dir: reflect.SelectRecv, Chan: reflect.ValueOf(subscription.Chan())}, // new event + reflect.SelectCase{Dir: reflect.SelectRecv, Chan: reflect.ValueOf(c.Closed())}, // connection closed + } + + for { + idx, notification, recvOk := reflect.Select(cases) + switch idx { + case 0: // new event, or channel closed + if recvOk { // send notification + if event, ok := notification.Interface().(*event.Event); ok { + if subscription.match == nil || subscription.match(event.Data) { + sendNotification(c, subid, subscription.format(event.Data)) + } + } + } else { // user send an eth_unsubscribe request + return + } + case 1: // connection closed + s.unsubscribe(subid) + return + } + } + }() + } else { // unable to create subscription + s.muSubcriptions.Lock() + delete(s.subscriptions, subid) + s.muSubcriptions.Unlock() + } + } else { + return "", fmt.Errorf("Unable to create subscription") + } + + return subid, nil +} + +// unsubscribe calls the Unsubscribe method on the subscription and removes a subscription from the subscription +// registry. +func (s *Server) unsubscribe(subid string) bool { + s.muSubcriptions.Lock() + defer s.muSubcriptions.Unlock() + if sub, ok := s.subscriptions[subid]; ok { + sub.Unsubscribe() + delete(s.subscriptions, subid) + return true + } + return false +} + +// handle executes a request and returns the response from the callback. +func (s *Server) handle(ctx context.Context, codec ServerCodec, req *serverRequest) interface{} { + if req.err != nil { + return codec.CreateErrorResponse(&req.id, req.err) + } + + if req.isUnsubscribe { // first param must be the subscription id + if len(req.args) >= 1 && req.args[0].Kind() == reflect.String { + subid := req.args[0].String() + if s.unsubscribe(subid) { + return codec.CreateResponse(req.id, true) + } else { + return codec.CreateErrorResponse(&req.id, + &callbackError{fmt.Sprintf("subscription '%s' not found", subid)}) + } + } + return codec.CreateErrorResponse(&req.id, &invalidParamsError{"Expected subscription id as argument"}) + } + + if req.callb.isSubscribe { + subid, err := s.createSubscription(codec, req) + if err != nil { + return codec.CreateErrorResponse(&req.id, &callbackError{err.Error()}) + } + return codec.CreateResponse(req.id, subid) + } + + // regular RPC call + if len(req.args) != len(req.callb.argTypes) { + rpcErr := &invalidParamsError{fmt.Sprintf("%s%s%s expects %d parameters, got %d", + req.svcname, serviceMethodSeparator, req.callb.method.Name, + len(req.callb.argTypes), len(req.args))} + return codec.CreateErrorResponse(&req.id, rpcErr) + } + + arguments := []reflect.Value{req.callb.rcvr} + if req.callb.hasCtx { + arguments = append(arguments, reflect.ValueOf(ctx)) + } + if len(req.args) > 0 { + arguments = append(arguments, req.args...) + } + + reply := req.callb.method.Func.Call(arguments) + + if len(reply) == 0 { + return codec.CreateResponse(req.id, nil) + } + + if req.callb.errPos >= 0 { // test if method returned an error + if !reply[req.callb.errPos].IsNil() { + e := reply[req.callb.errPos].Interface().(error) + res := codec.CreateErrorResponse(&req.id, &callbackError{e.Error()}) + return res + } + } + + return codec.CreateResponse(req.id, reply[0].Interface()) +} + +// exec executes the given request and writes the result back using the codec. +func (s *Server) exec(ctx context.Context, codec ServerCodec, req *serverRequest) { + var response interface{} + if req.err != nil { + response = codec.CreateErrorResponse(&req.id, req.err) + } else { + response = s.handle(ctx, codec, req) + } + + if err := codec.Write(response); err != nil { + glog.V(logger.Error).Infof("%v\n", err) + codec.Close() + } +} + +// execBatch executes the given requests and writes the result back using the codec. It will only write the response +// back when the last request is processed. +func (s *Server) execBatch(ctx context.Context, codec ServerCodec, requests []*serverRequest) { + responses := make([]interface{}, len(requests)) + for i, req := range requests { + if req.err != nil { + responses[i] = codec.CreateErrorResponse(&req.id, req.err) + } else { + responses[i] = s.handle(ctx, codec, req) + } + } + + if err := codec.Write(responses); err != nil { + glog.V(logger.Error).Infof("%v\n", err) + codec.Close() + } +} + +// readRequest requests the next (batch) request from the codec. It will return the collection of requests, an +// indication if the request was a batch, the invalid request identifier and an error when the request could not be +// read/parsed. +func (s *Server) readRequest(codec ServerCodec) ([]*serverRequest, bool, RPCError) { + reqs, batch, err := codec.ReadRequestHeaders() + if err != nil { + return nil, batch, err + } + + requests := make([]*serverRequest, len(reqs)) + + // verify requests + for i, r := range reqs { + var ok bool + var svc *service + + if r.isPubSub && r.method == unsubscribeMethod { + requests[i] = &serverRequest{id: r.id, isUnsubscribe: true} + argTypes := []reflect.Type{reflect.TypeOf("")} + if args, err := codec.ParseRequestArguments(argTypes, r.params); err == nil { + requests[i].args = args + } else { + requests[i].err = &invalidParamsError{err.Error()} + } + continue + } + + if svc, ok = s.services[r.service]; !ok { + requests[i] = &serverRequest{id: r.id, err: &methodNotFoundError{r.service, r.method}} + continue + } + + if r.isPubSub { // eth_subscribe + if callb, ok := svc.subscriptions[r.method]; ok { + requests[i] = &serverRequest{id: r.id, svcname: svc.name, callb: callb} + if r.params != nil && len(callb.argTypes) > 0 { + argTypes := []reflect.Type{reflect.TypeOf("")} + argTypes = append(argTypes, callb.argTypes...) + if args, err := codec.ParseRequestArguments(argTypes, r.params); err == nil { + requests[i].args = args[1:] // first one is service.method name which isn't an actual argument + } else { + requests[i].err = &invalidParamsError{err.Error()} + } + } + } else { + requests[i] = &serverRequest{id: r.id, err: &methodNotFoundError{subscribeMethod, r.method}} + } + continue + } + + if callb, ok := svc.callbacks[r.method]; ok { + requests[i] = &serverRequest{id: r.id, svcname: svc.name, callb: callb} + if r.params != nil && len(callb.argTypes) > 0 { + if args, err := codec.ParseRequestArguments(callb.argTypes, r.params); err == nil { + requests[i].args = args + } else { + requests[i].err = &invalidParamsError{err.Error()} + } + } + continue + } + + requests[i] = &serverRequest{id: r.id, err: &methodNotFoundError{r.service, r.method}} + } + + return requests, batch, nil +} diff --git a/rpc/server_test.go b/rpc/server_test.go new file mode 100644 index 000000000..5b91fe42a --- /dev/null +++ b/rpc/server_test.go @@ -0,0 +1,271 @@ +// Copyright 2015 The go-ethereum Authors +// This file is part of the go-ethereum library. +// +// The go-ethereum library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The go-ethereum library 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 Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the go-ethereum library. If not, see . + +package rpc + +import ( + "encoding/json" + "fmt" + "reflect" + "testing" + "time" + + "golang.org/x/net/context" +) + +type Service struct{} + +type Args struct { + S string +} + +func (s *Service) NoArgsRets() { +} + +type Result struct { + String string + Int int + Args *Args +} + +func (s *Service) Echo(str string, i int, args *Args) Result { + return Result{str, i, args} +} + +func (s *Service) EchoWithCtx(ctx context.Context, str string, i int, args *Args) Result { + return Result{str, i, args} +} + +func (s *Service) Rets() (string, error) { + return "", nil +} + +func (s *Service) InvalidRets1() (error, string) { + return nil, "" +} + +func (s *Service) InvalidRets2() (string, string) { + return "", "" +} + +func (s *Service) InvalidRets3() (string, string, error) { + return "", "", nil +} + +func (s *Service) Subscription() (Subscription, error) { + return NewSubscription(nil), nil +} + +func TestServerRegisterName(t *testing.T) { + server := NewServer() + service := new(Service) + + if err := server.RegisterName("calc", service); err != nil { + t.Fatalf("%v", err) + } + + if len(server.services) != 2 { + t.Fatalf("Expected 2 service entries, got %d", len(server.services)) + } + + svc, ok := server.services["calc"] + if !ok { + t.Fatalf("Expected service calc to be registered") + } + + if len(svc.callbacks) != 4 { + t.Errorf("Expected 4 callbacks for service 'calc', got %d", len(svc.callbacks)) + } + + if len(svc.subscriptions) != 1 { + t.Errorf("Expected 1 subscription for service 'calc', got %d", len(svc.subscriptions)) + } +} + +// dummy codec used for testing RPC method execution +type ServerTestCodec struct { + counter int + input []byte + output string + closer chan interface{} +} + +func (c *ServerTestCodec) ReadRequestHeaders() ([]rpcRequest, bool, RPCError) { + c.counter += 1 + + if c.counter == 1 { + var req JSONRequest + json.Unmarshal(c.input, &req) + return []rpcRequest{rpcRequest{id: *req.Id, isPubSub: false, service: "test", method: req.Method, params: req.Payload}}, false, nil + } + + // requests are executes in parallel, wait a bit before returning an error so that the previous request has time to + // be executed + timer := time.NewTimer(time.Duration(2) * time.Second) + <-timer.C + + return nil, false, &invalidRequestError{"connection closed"} +} + +func (c *ServerTestCodec) ParseRequestArguments(argTypes []reflect.Type, payload interface{}) ([]reflect.Value, RPCError) { + + args, _ := payload.(json.RawMessage) + + argValues := make([]reflect.Value, len(argTypes)) + params := make([]interface{}, len(argTypes)) + + n, err := countArguments(args) + if err != nil { + return nil, &invalidParamsError{err.Error()} + } + if n != len(argTypes) { + return nil, &invalidParamsError{fmt.Sprintf("insufficient params, want %d have %d", len(argTypes), n)} + + } + + for i, t := range argTypes { + if t.Kind() == reflect.Ptr { + // values must be pointers for the Unmarshal method, reflect. + // Dereference otherwise reflect.New would create **SomeType + argValues[i] = reflect.New(t.Elem()) + params[i] = argValues[i].Interface() + + // when not specified blockNumbers are by default latest (-1) + if blockNumber, ok := params[i].(*BlockNumber); ok { + *blockNumber = BlockNumber(-1) + } + } else { + argValues[i] = reflect.New(t) + params[i] = argValues[i].Interface() + + // when not specified blockNumbers are by default latest (-1) + if blockNumber, ok := params[i].(*BlockNumber); ok { + *blockNumber = BlockNumber(-1) + } + } + } + + if err := json.Unmarshal(args, ¶ms); err != nil { + return nil, &invalidParamsError{err.Error()} + } + + // Convert pointers back to values where necessary + for i, a := range argValues { + if a.Kind() != argTypes[i].Kind() { + argValues[i] = reflect.Indirect(argValues[i]) + } + } + + return argValues, nil +} + +func (c *ServerTestCodec) CreateResponse(id int64, reply interface{}) interface{} { + return &JSONSuccessResponse{Version: jsonRPCVersion, Id: id, Result: reply} +} + +func (c *ServerTestCodec) CreateErrorResponse(id *int64, err RPCError) interface{} { + return &JSONErrResponse{Version: jsonRPCVersion, Id: id, Error: JSONError{Code: err.Code(), Message: err.Error()}} +} + +func (c *ServerTestCodec) CreateErrorResponseWithInfo(id *int64, err RPCError, info interface{}) interface{} { + return &JSONErrResponse{Version: jsonRPCVersion, Id: id, + Error: JSONError{Code: err.Code(), Message: err.Error(), Data: info}} +} + +func (c *ServerTestCodec) CreateNotification(subid string, event interface{}) interface{} { + return &jsonNotification{Version: jsonRPCVersion, Method: notificationMethod, + Params: jsonSubscription{Subscription: subid, Result: event}} +} + +func (c *ServerTestCodec) Write(msg interface{}) error { + if len(c.output) == 0 { // only capture first response + if o, err := json.Marshal(msg); err != nil { + return err + } else { + c.output = string(o) + } + } + + return nil +} + +func (c *ServerTestCodec) Close() { + close(c.closer) +} + +func (c *ServerTestCodec) Closed() <-chan interface{} { + return c.closer +} + +func TestServerMethodExecution(t *testing.T) { + server := NewServer() + service := new(Service) + + if err := server.RegisterName("test", service); err != nil { + t.Fatalf("%v", err) + } + + id := int64(12345) + req := JSONRequest{ + Method: "echo", + Version: "2.0", + Id: &id, + } + args := []interface{}{"string arg", 1122, &Args{"qwerty"}} + req.Payload, _ = json.Marshal(&args) + + input, _ := json.Marshal(&req) + codec := &ServerTestCodec{input: input, closer: make(chan interface{})} + go server.ServeCodec(codec) + + <-codec.closer + + expected := `{"jsonrpc":"2.0","id":12345,"result":{"String":"string arg","Int":1122,"Args":{"S":"qwerty"}}}` + + if expected != codec.output { + t.Fatalf("expected %s, got %s\n", expected, codec.output) + } +} + +func TestServerMethodWithCtx(t *testing.T) { + server := NewServer() + service := new(Service) + + if err := server.RegisterName("test", service); err != nil { + t.Fatalf("%v", err) + } + + id := int64(12345) + req := JSONRequest{ + Method: "echoWithCtx", + Version: "2.0", + Id: &id, + } + args := []interface{}{"string arg", 1122, &Args{"qwerty"}} + req.Payload, _ = json.Marshal(&args) + + input, _ := json.Marshal(&req) + codec := &ServerTestCodec{input: input, closer: make(chan interface{})} + go server.ServeCodec(codec) + + <-codec.closer + + expected := `{"jsonrpc":"2.0","id":12345,"result":{"String":"string arg","Int":1122,"Args":{"S":"qwerty"}}}` + + if expected != codec.output { + t.Fatalf("expected %s, got %s\n", expected, codec.output) + } +} diff --git a/rpc/shared/errors.go b/rpc/shared/errors.go deleted file mode 100644 index d5a7011f9..000000000 --- a/rpc/shared/errors.go +++ /dev/null @@ -1,126 +0,0 @@ -// Copyright 2015 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library 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 Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see . - -package shared - -import "fmt" - -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 NotReadyError struct { - Resource string -} - -func (e *NotReadyError) Error() string { - return fmt.Sprintf("%s not ready", e.Resource) -} - -func NewNotReadyError(resource string) *NotReadyError { - return &NotReadyError{ - Resource: resource, - } -} - -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 NotAvailableError struct { - Method string - Reason string -} - -func (e *NotAvailableError) Error() string { - return fmt.Sprintf("%s method not available: %s", e.Method, e.Reason) -} - -func NewNotAvailableError(method string, reason string) *NotAvailableError { - return &NotAvailableError{ - Method: method, - Reason: reason, - } -} diff --git a/rpc/shared/types.go b/rpc/shared/types.go deleted file mode 100644 index db328234d..000000000 --- a/rpc/shared/types.go +++ /dev/null @@ -1,108 +0,0 @@ -// Copyright 2015 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library 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 Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see . - -package shared - -import ( - "encoding/json" - - "github.com/ethereum/go-ethereum/logger" - "github.com/ethereum/go-ethereum/logger/glog" -) - -// Ethereum RPC API interface -type EthereumApi interface { - // API identifier - Name() string - - // API version - ApiVersion() string - - // Execute the given request and returns the response or an error - Execute(*Request) (interface{}, error) - - // List of supported RCP methods this API provides - Methods() []string -} - -// RPC request -type Request struct { - Id interface{} `json:"id"` - Jsonrpc string `json:"jsonrpc"` - Method string `json:"method"` - Params json.RawMessage `json:"params"` -} - -// RPC response -type Response struct { - Id interface{} `json:"id"` - Jsonrpc string `json:"jsonrpc"` -} - -// RPC success response -type SuccessResponse struct { - Id interface{} `json:"id"` - Jsonrpc string `json:"jsonrpc"` - Result interface{} `json:"result"` -} - -// RPC error response -type ErrorResponse struct { - Id interface{} `json:"id"` - Jsonrpc string `json:"jsonrpc"` - Error *ErrorObject `json:"error"` -} - -// RPC error response details -type ErrorObject struct { - Code int `json:"code"` - Message string `json:"message"` - // Data interface{} `json:"data"` -} - -// Create RPC error response, this allows for custom error codes -func NewRpcErrorResponse(id interface{}, jsonrpcver string, errCode int, err error) *ErrorResponse { - jsonerr := &ErrorObject{errCode, err.Error()} - response := ErrorResponse{Jsonrpc: jsonrpcver, Id: id, Error: jsonerr} - - glog.V(logger.Detail).Infof("Generated error response: %s", response) - return &response -} - -// Create RPC response -func NewRpcResponse(id interface{}, jsonrpcver string, reply interface{}, err error) *interface{} { - var response interface{} - - switch err.(type) { - case nil: - response = &SuccessResponse{Jsonrpc: jsonrpcver, Id: id, Result: reply} - case *NotImplementedError: - jsonerr := &ErrorObject{-32601, err.Error()} - response = &ErrorResponse{Jsonrpc: jsonrpcver, Id: id, Error: jsonerr} - case *NotReadyError: - jsonerr := &ErrorObject{-32000, err.Error()} - response = &ErrorResponse{Jsonrpc: jsonrpcver, Id: id, Error: jsonerr} - case *DecodeParamError, *InsufficientParamsError, *ValidationError, *InvalidTypeError: - jsonerr := &ErrorObject{-32602, err.Error()} - response = &ErrorResponse{Jsonrpc: jsonrpcver, Id: id, Error: jsonerr} - default: - jsonerr := &ErrorObject{-32603, err.Error()} - response = &ErrorResponse{Jsonrpc: jsonrpcver, Id: id, Error: jsonerr} - } - - glog.V(logger.Detail).Infof("Generated response: %T %s", response, response) - return &response -} diff --git a/rpc/shared/utils.go b/rpc/shared/utils.go deleted file mode 100644 index b13e9eb1b..000000000 --- a/rpc/shared/utils.go +++ /dev/null @@ -1,43 +0,0 @@ -// Copyright 2015 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library 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 Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see . - -package shared - -import "strings" - -const ( - AdminApiName = "admin" - EthApiName = "eth" - DbApiName = "db" - DebugApiName = "debug" - MergedApiName = "merged" - MinerApiName = "miner" - NetApiName = "net" - ShhApiName = "shh" - TxPoolApiName = "txpool" - PersonalApiName = "personal" - Web3ApiName = "web3" - - JsonRpcVersion = "2.0" -) - -var ( - // All API's - AllApis = strings.Join([]string{ - AdminApiName, DbApiName, EthApiName, DebugApiName, MinerApiName, NetApiName, - ShhApiName, TxPoolApiName, PersonalApiName, Web3ApiName, - }, ",") -) diff --git a/rpc/types.go b/rpc/types.go new file mode 100644 index 000000000..02295a022 --- /dev/null +++ b/rpc/types.go @@ -0,0 +1,368 @@ +// Copyright 2015 The go-ethereum Authors +// This file is part of the go-ethereum library. +// +// The go-ethereum library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The go-ethereum library 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 Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the go-ethereum library. If not, see . + +package rpc + +import ( + "fmt" + "math" + "math/big" + "reflect" + "strings" + "sync" + + "github.com/ethereum/go-ethereum/event" + "gopkg.in/fatih/set.v0" +) + +// API describes the set of methods offered over the RPC interface +type API struct { + Namespace string // namespace under which the rpc methods of Service are exposed + Version string // api version for DApp's + Service interface{} // receiver instance which holds the methods + Public bool // indication if the methods must be considered safe for public use +} + +// callback is a method callback which was registered in the server +type callback struct { + rcvr reflect.Value // receiver of method + method reflect.Method // callback + argTypes []reflect.Type // input argument types + hasCtx bool // method's first argument is a context (not included in argTypes) + errPos int // err return idx, of -1 when method cannot return error + isSubscribe bool // indication if the callback is a subscription +} + +// service represents a registered object +type service struct { + name string // name for service + rcvr reflect.Value // receiver of methods for the service + typ reflect.Type // receiver type + callbacks callbacks // registered handlers + subscriptions subscriptions // available subscriptions/notifications +} + +// serverRequest is an incoming request +type serverRequest struct { + id int64 + svcname string + rcvr reflect.Value + callb *callback + args []reflect.Value + isUnsubscribe bool + err RPCError +} + +type serviceRegistry map[string]*service // collection of services +type callbacks map[string]*callback // collection of RPC callbacks +type subscriptions map[string]*callback // collection of subscription callbacks +type subscriptionRegistry map[string]Subscription // collection of subscriptions + +// Server represents a RPC server +type Server struct { + services serviceRegistry + muSubcriptions sync.Mutex // protects subscriptions + subscriptions subscriptionRegistry + + run int32 + codecsMu sync.Mutex + codecs *set.Set +} + +// rpcRequest represents a raw incoming RPC request +type rpcRequest struct { + service string + method string + id int64 + isPubSub bool + params interface{} +} + +// RPCError implements RPC error, is add support for error codec over regular go errors +type RPCError interface { + // RPC error code + Code() int + // Error message + Error() string +} + +// ServerCodec implements reading, parsing and writing RPC messages for the server side of +// a RPC session. Implementations must be go-routine safe since the codec can be called in +// multiple go-routines concurrently. +type ServerCodec interface { + // Read next request + ReadRequestHeaders() ([]rpcRequest, bool, RPCError) + // Parse request argument to the given types + ParseRequestArguments([]reflect.Type, interface{}) ([]reflect.Value, RPCError) + // Assemble success response + CreateResponse(int64, interface{}) interface{} + // Assemble error response + CreateErrorResponse(*int64, RPCError) interface{} + // Assemble error response with extra information about the error through info + CreateErrorResponseWithInfo(id *int64, err RPCError, info interface{}) interface{} + // Create notification response + CreateNotification(string, interface{}) interface{} + // Write msg to client. + Write(interface{}) error + // Close underlying data stream + Close() + // Closed when underlying connection is closed + Closed() <-chan interface{} +} + +// SubscriptionMatcher returns true if the given value matches the criteria specified by the user +type SubscriptionMatcher func(interface{}) bool + +// SubscriptionOutputFormat accepts event data and has the ability to format the data before it is send to the client +type SubscriptionOutputFormat func(interface{}) interface{} + +// defaultSubscriptionOutputFormatter returns data and is used as default output format for notifications +func defaultSubscriptionOutputFormatter(data interface{}) interface{} { + return data +} + +// Subscription is used by the server to send notifications to the client +type Subscription struct { + sub event.Subscription + match SubscriptionMatcher + format SubscriptionOutputFormat +} + +// NewSubscription create a new RPC subscription +func NewSubscription(sub event.Subscription) Subscription { + return Subscription{sub, nil, defaultSubscriptionOutputFormatter} +} + +// NewSubscriptionWithOutputFormat create a new RPC subscription which a custom notification output format +func NewSubscriptionWithOutputFormat(sub event.Subscription, formatter SubscriptionOutputFormat) Subscription { + return Subscription{sub, nil, formatter} +} + +// NewSubscriptionFiltered will create a new subscription. For each raised event the given matcher is +// called. If it returns true the event is send as notification to the client, otherwise it is ignored. +func NewSubscriptionFiltered(sub event.Subscription, match SubscriptionMatcher) Subscription { + return Subscription{sub, match, defaultSubscriptionOutputFormatter} +} + +// Chan returns the channel where new events will be published. It's up the user to call the matcher to +// determine if the events are interesting for the client. +func (s *Subscription) Chan() <-chan *event.Event { + return s.sub.Chan() +} + +// Unsubscribe will end the subscription and closes the event channel +func (s *Subscription) Unsubscribe() { + s.sub.Unsubscribe() +} + +// HexNumber serializes a number to hex format using the "%#x" format +type HexNumber big.Int + +// NewHexNumber creates a new hex number instance which will serialize the given val with `%#x` on marshal. +func NewHexNumber(val interface{}) *HexNumber { + if val == nil { + return nil + } + + if v, ok := val.(*big.Int); ok && v != nil { + hn := new(big.Int).Set(v) + return (*HexNumber)(hn) + } + + rval := reflect.ValueOf(val) + + var unsigned uint64 + utype := reflect.TypeOf(unsigned) + if t := rval.Type(); t.ConvertibleTo(utype) { + hn := new(big.Int).SetUint64(rval.Convert(utype).Uint()) + return (*HexNumber)(hn) + } + + var signed int64 + stype := reflect.TypeOf(signed) + if t := rval.Type(); t.ConvertibleTo(stype) { + hn := new(big.Int).SetInt64(rval.Convert(stype).Int()) + return (*HexNumber)(hn) + } + + return nil +} + +func (h *HexNumber) UnmarshalJSON(input []byte) error { + length := len(input) + if length >= 2 && input[0] == '"' && input[length-1] == '"' { + input = input[1 : length-1] + } + + hn := (*big.Int)(h) + if _, ok := hn.SetString(string(input), 0); ok { + return nil + } + + return fmt.Errorf("Unable to parse number") +} + +// MarshalJSON serialize the hex number instance to a hex representation. +func (h *HexNumber) MarshalJSON() ([]byte, error) { + if h != nil { + hn := (*big.Int)(h) + if hn.BitLen() == 0 { + return []byte(`"0x0"`), nil + } + return []byte(fmt.Sprintf(`"0x%x"`, hn)), nil + } + return nil, nil +} + +func (h *HexNumber) Int() int { + hn := (*big.Int)(h) + return int(hn.Int64()) +} + +func (h *HexNumber) Int64() int64 { + hn := (*big.Int)(h) + return hn.Int64() +} + +func (h *HexNumber) Uint() uint { + hn := (*big.Int)(h) + return uint(hn.Uint64()) +} + +func (h *HexNumber) Uint64() uint64 { + hn := (*big.Int)(h) + return hn.Uint64() +} + +func (h *HexNumber) BigInt() *big.Int { + return (*big.Int)(h) +} + +type Number int64 + +func (n *Number) UnmarshalJSON(data []byte) error { + input := strings.TrimSpace(string(data)) + + if len(input) >= 2 && input[0] == '"' && input[len(input)-1] == '"' { + input = input[1 : len(input)-1] + } + + if len(input) == 0 { + *n = Number(latestBlockNumber.Int64()) + return nil + } + + in := new(big.Int) + _, ok := in.SetString(input, 0) + + if !ok { // test if user supplied string tag + return fmt.Errorf(`invalid number %s`, data) + } + + if in.Cmp(earliestBlockNumber) >= 0 && in.Cmp(maxBlockNumber) <= 0 { + *n = Number(in.Int64()) + return nil + } + + return fmt.Errorf("blocknumber not in range [%d, %d]", earliestBlockNumber, maxBlockNumber) +} + +func (n *Number) Int64() int64 { + return *(*int64)(n) +} + +func (n *Number) BigInt() *big.Int { + return big.NewInt(n.Int64()) +} + +var ( + pendingBlockNumber = big.NewInt(-2) + latestBlockNumber = big.NewInt(-1) + earliestBlockNumber = big.NewInt(0) + maxBlockNumber = big.NewInt(math.MaxInt64) +) + +type BlockNumber int64 + +const ( + PendingBlockNumber = BlockNumber(-2) + LatestBlockNumber = BlockNumber(-1) +) + +// UnmarshalJSON parses the given JSON fragement into a BlockNumber. It supports: +// - "latest" or "earliest" as string arguments +// - the block number +// Returned errors: +// - an unsupported error when "pending" is specified (not yet implemented) +// - an invalid block number error when the given argument isn't a known strings +// - an out of range error when the given block number is either too little or too large +func (bn *BlockNumber) UnmarshalJSON(data []byte) error { + input := strings.TrimSpace(string(data)) + + if len(input) >= 2 && input[0] == '"' && input[len(input)-1] == '"' { + input = input[1 : len(input)-1] + } + + if len(input) == 0 { + *bn = BlockNumber(latestBlockNumber.Int64()) + return nil + } + + in := new(big.Int) + _, ok := in.SetString(input, 0) + + if !ok { // test if user supplied string tag + strBlockNumber := input + if strBlockNumber == "latest" { + *bn = BlockNumber(latestBlockNumber.Int64()) + return nil + } + + if strBlockNumber == "earliest" { + *bn = BlockNumber(earliestBlockNumber.Int64()) + return nil + } + + if strBlockNumber == "pending" { + *bn = BlockNumber(pendingBlockNumber.Int64()) + return nil + } + + return fmt.Errorf(`invalid blocknumber %s`, data) + } + + if in.Cmp(earliestBlockNumber) >= 0 && in.Cmp(maxBlockNumber) <= 0 { + *bn = BlockNumber(in.Int64()) + return nil + } + + return fmt.Errorf("blocknumber not in range [%d, %d]", earliestBlockNumber, maxBlockNumber) +} + +func (bn *BlockNumber) Int64() int64 { + return (int64)(*bn) +} + +// Client defines the interface for go client that wants to connect to a geth RPC endpoint +type Client interface { + // SupportedModules returns the collection of API's the server offers + SupportedModules() (map[string]string, error) + + Send(req interface{}) error + Recv(msg interface{}) error + + Close() +} diff --git a/rpc/types_test.go b/rpc/types_test.go new file mode 100644 index 000000000..c2c5c6db6 --- /dev/null +++ b/rpc/types_test.go @@ -0,0 +1,73 @@ +// Copyright 2015 The go-ethereum Authors +// This file is part of the go-ethereum library. +// +// The go-ethereum library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The go-ethereum library 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 Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the go-ethereum library. If not, see . + +package rpc + +import ( + "bytes" + "encoding/json" + "math/big" + "testing" +) + +func TestNewHexNumber(t *testing.T) { + tests := []interface{}{big.NewInt(123), int64(123), uint64(123), int8(123), uint8(123)} + + for i, v := range tests { + hn := NewHexNumber(v) + if hn == nil { + t.Fatalf("Unable to create hex number instance for tests[%d]", i) + } + if hn.Int64() != 123 { + t.Fatalf("expected %d, got %d on value tests[%d]", 123, hn.Int64(), i) + } + } + + failures := []interface{}{"", nil, []byte{1, 2, 3, 4}} + for i, v := range failures { + hn := NewHexNumber(v) + if hn != nil { + t.Fatalf("Creating a nex number instance of %T should fail (failures[%d])", failures[i], i) + } + } +} + +func TestHexNumberUnmarshalJSON(t *testing.T) { + tests := []string{`"0x4d2"`, "1234", `"1234"`} + for i, v := range tests { + var hn HexNumber + if err := json.Unmarshal([]byte(v), &hn); err != nil { + t.Fatalf("Test %d failed - %s", i, err) + } + + if hn.Int64() != 1234 { + t.Fatalf("Expected %d, got %d for test[%d]", 1234, hn.Int64(), i) + } + } +} + +func TestHexNumberMarshalJSON(t *testing.T) { + hn := NewHexNumber(1234567890) + got, err := json.Marshal(hn) + if err != nil { + t.Fatalf("Unable to marshal hex number - %s", err) + } + + exp := []byte(`"0x499602d2"`) + if bytes.Compare(exp, got) != 0 { + t.Fatalf("Invalid json.Marshal, expected '%s', got '%s'", exp, got) + } +} diff --git a/rpc/useragent/agent.go b/rpc/useragent/agent.go deleted file mode 100644 index df0739e65..000000000 --- a/rpc/useragent/agent.go +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright 2015 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library 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 Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see . - -// package user agent provides frontends and agents which can interact with the user -package useragent - -var ( - AskPasswordMethod = "agent_askPassword" - ConfirmTransactionMethod = "agent_confirmTransaction" - EnableUserAgentMethod = "admin_enableUserAgent" -) diff --git a/rpc/useragent/remote_frontend.go b/rpc/useragent/remote_frontend.go deleted file mode 100644 index 944ab287a..000000000 --- a/rpc/useragent/remote_frontend.go +++ /dev/null @@ -1,166 +0,0 @@ -// Copyright 2015 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library 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 Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see . - -package useragent - -import ( - "encoding/json" - "fmt" - "net" - - "github.com/ethereum/go-ethereum/accounts" - "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/logger" - "github.com/ethereum/go-ethereum/logger/glog" - "github.com/ethereum/go-ethereum/rpc/shared" -) - -// remoteFrontend implements xeth.Frontend and will communicate with an external -// user agent over a connection -type RemoteFrontend struct { - enabled bool - mgr *accounts.Manager - d *json.Decoder - e *json.Encoder - n int -} - -// NewRemoteFrontend creates a new frontend which will interact with an user agent -// over the given connection -func NewRemoteFrontend(conn net.Conn, mgr *accounts.Manager) *RemoteFrontend { - return &RemoteFrontend{false, mgr, json.NewDecoder(conn), json.NewEncoder(conn), 0} -} - -// Enable will enable user interaction -func (fe *RemoteFrontend) Enable() { - fe.enabled = true -} - -func (fe *RemoteFrontend) AskPassword() (string, bool) { - if !fe.enabled { - return "", false - } - - err := fe.send(AskPasswordMethod) - if err != nil { - glog.V(logger.Error).Infof("Unable to send password request to agent - %v\n", err) - return "", false - } - - passwdRes, err := fe.recv() - if err != nil { - glog.V(logger.Error).Infof("Unable to recv password response from agent - %v\n", err) - return "", false - } - - if passwd, ok := passwdRes.Result.(string); ok { - return passwd, true - } - - return "", false - -} - -// UnlockAccount asks the user agent for the user password and tries to unlock the account. -// It will try 3 attempts before giving up. -func (fe *RemoteFrontend) UnlockAccount(address []byte) bool { - if !fe.enabled { - return false - } - - err := fe.send(AskPasswordMethod, common.Bytes2Hex(address)) - if err != nil { - glog.V(logger.Error).Infof("Unable to send password request to agent - %v\n", err) - return false - } - - passwdRes, err := fe.recv() - if err != nil { - glog.V(logger.Error).Infof("Unable to recv password response from agent - %v\n", err) - return false - } - - if passwd, ok := passwdRes.Result.(string); ok { - err = fe.mgr.Unlock(common.BytesToAddress(address), passwd) - } - - if err == nil { - return true - } - - glog.V(logger.Debug).Infoln("3 invalid account unlock attempts") - return false -} - -// ConfirmTransaction asks the user for approval -func (fe *RemoteFrontend) ConfirmTransaction(tx string) bool { - if !fe.enabled { - return true // backwards compatibility - } - - err := fe.send(ConfirmTransactionMethod, tx) - if err != nil { - glog.V(logger.Error).Infof("Unable to send tx confirmation request to agent - %v\n", err) - return false - } - - confirmResponse, err := fe.recv() - if err != nil { - glog.V(logger.Error).Infof("Unable to recv tx confirmation response from agent - %v\n", err) - return false - } - - if confirmed, ok := confirmResponse.Result.(bool); ok { - return confirmed - } - - return false -} - -// send request to the agent -func (fe *RemoteFrontend) send(method string, params ...interface{}) error { - fe.n += 1 - - p, err := json.Marshal(params) - if err != nil { - glog.V(logger.Info).Infof("Unable to send agent request %v\n", err) - return err - } - - req := shared.Request{ - Method: method, - Jsonrpc: shared.JsonRpcVersion, - Id: fe.n, - Params: p, - } - - return fe.e.Encode(&req) -} - -// recv user response from agent -func (fe *RemoteFrontend) recv() (*shared.SuccessResponse, error) { - var res json.RawMessage - if err := fe.d.Decode(&res); err != nil { - return nil, err - } - - var response shared.SuccessResponse - if err := json.Unmarshal(res, &response); err == nil { - return &response, nil - } - - return nil, fmt.Errorf("Invalid user agent response") -} diff --git a/rpc/utils.go b/rpc/utils.go new file mode 100644 index 000000000..25321c543 --- /dev/null +++ b/rpc/utils.go @@ -0,0 +1,246 @@ +// Copyright 2015 The go-ethereum Authors +// This file is part of the go-ethereum library. +// +// The go-ethereum library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The go-ethereum library 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 Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the go-ethereum library. If not, see . + +package rpc + +import ( + "crypto/rand" + "encoding/hex" + "errors" + "math/big" + "reflect" + "unicode" + "unicode/utf8" + + "fmt" + + "golang.org/x/net/context" +) + +// Is this an exported - upper case - name? +func isExported(name string) bool { + rune, _ := utf8.DecodeRuneInString(name) + return unicode.IsUpper(rune) +} + +// Is this type exported or a builtin? +func isExportedOrBuiltinType(t reflect.Type) bool { + for t.Kind() == reflect.Ptr { + t = t.Elem() + } + // PkgPath will be non-empty even for an exported type, + // so we need to check the type name as well. + return isExported(t.Name()) || t.PkgPath() == "" +} + +var errorType = reflect.TypeOf((*error)(nil)).Elem() + +// Implements this type the error interface +func isErrorType(t reflect.Type) bool { + for t.Kind() == reflect.Ptr { + t = t.Elem() + } + return t.Implements(errorType) +} + +var subscriptionType = reflect.TypeOf((*Subscription)(nil)).Elem() + +func isSubscriptionType(t reflect.Type) bool { + for t.Kind() == reflect.Ptr { + t = t.Elem() + } + return t == subscriptionType +} + +// isPubSub tests whether the given method return the pair (v2.Subscription, error) +func isPubSub(methodType reflect.Type) bool { + if methodType.NumOut() != 2 { + return false + } + return isSubscriptionType(methodType.Out(0)) && isErrorType(methodType.Out(1)) +} + +// formatName will convert to first character to lower case +func formatName(name string) string { + ret := []rune(name) + if len(ret) > 0 { + ret[0] = unicode.ToLower(ret[0]) + } + return string(ret) +} + +var bigIntType = reflect.TypeOf((*big.Int)(nil)).Elem() + +// Indication if this type should be serialized in hex +func isHexNum(t reflect.Type) bool { + if t == nil { + return false + } + for t.Kind() == reflect.Ptr { + t = t.Elem() + } + + return t == bigIntType +} + +var blockNumberType = reflect.TypeOf((*BlockNumber)(nil)).Elem() + +// Indication if the given block is a BlockNumber +func isBlockNumber(t reflect.Type) bool { + if t == nil { + return false + } + + for t.Kind() == reflect.Ptr { + t = t.Elem() + } + + return t == blockNumberType +} + +var contextType = reflect.TypeOf(new(context.Context)).Elem() + +// suitableCallbacks iterates over the methods of the given type. It will determine if a method satisfies the criteria +// for a RPC callback or a subscription callback and adds it to the collection of callbacks or subscriptions. See server +// documentation for a summary of these criteria. +func suitableCallbacks(rcvr reflect.Value, typ reflect.Type) (callbacks, subscriptions) { + callbacks := make(callbacks) + subscriptions := make(subscriptions) + +METHODS: + for m := 0; m < typ.NumMethod(); m++ { + method := typ.Method(m) + mtype := method.Type + mname := formatName(method.Name) + if method.PkgPath != "" { // method must be exported + continue + } + + var h callback + h.isSubscribe = isPubSub(mtype) + h.rcvr = rcvr + h.method = method + h.errPos = -1 + + firstArg := 1 + numIn := mtype.NumIn() + if numIn >= 2 && mtype.In(1) == contextType { + h.hasCtx = true + firstArg = 2 + } + + if h.isSubscribe { + h.argTypes = make([]reflect.Type, numIn-firstArg) // skip rcvr type + for i := firstArg; i < numIn; i++ { + argType := mtype.In(i) + if isExportedOrBuiltinType(argType) { + h.argTypes[i-firstArg] = argType + } else { + continue METHODS + } + } + + subscriptions[mname] = &h + continue METHODS + } + + // determine method arguments, ignore first arg since it's the receiver type + // Arguments must be exported or builtin types + h.argTypes = make([]reflect.Type, numIn-firstArg) + for i := firstArg; i < numIn; i++ { + argType := mtype.In(i) + if !isExportedOrBuiltinType(argType) { + continue METHODS + } + h.argTypes[i-firstArg] = argType + } + + // check that all returned values are exported or builtin types + for i := 0; i < mtype.NumOut(); i++ { + if !isExportedOrBuiltinType(mtype.Out(i)) { + continue METHODS + } + } + + // when a method returns an error it must be the last returned value + h.errPos = -1 + for i := 0; i < mtype.NumOut(); i++ { + if isErrorType(mtype.Out(i)) { + h.errPos = i + break + } + } + + if h.errPos >= 0 && h.errPos != mtype.NumOut()-1 { + continue METHODS + } + + switch mtype.NumOut() { + case 0, 1: + break + case 2: + if h.errPos == -1 { // method must one return value and 1 error + continue METHODS + } + break + default: + continue METHODS + } + + callbacks[mname] = &h + } + + return callbacks, subscriptions +} + +func newSubscriptionId() (string, error) { + var subid [16]byte + n, _ := rand.Read(subid[:]) + if n != 16 { + return "", errors.New("Unable to generate subscription id") + } + return "0x" + hex.EncodeToString(subid[:]), nil +} + +// SupportedModules returns the collection of API's that the RPC server offers +// on which the given client connects. +func SupportedModules(client Client) (map[string]string, error) { + req := map[string]interface{}{ + "id": 1, + "method": "rpc_modules", + } + + if err := client.Send(req); err != nil { + return nil, err + } + + var response map[string]interface{} + if err := client.Recv(&response); err != nil { + return nil, err + } + + if payload, ok := response["result"]; ok { + mods := make(map[string]string) + if modules, ok := payload.(map[string]interface{}); ok { + for m, v := range modules { + mods[m] = fmt.Sprintf("%s", v) + } + return mods, nil + } + } + + return nil, fmt.Errorf("unable to retrieve modules") +} diff --git a/rpc/v2/doc.go b/rpc/v2/doc.go deleted file mode 100644 index e51494adb..000000000 --- a/rpc/v2/doc.go +++ /dev/null @@ -1,102 +0,0 @@ -// Copyright 2015 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library 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 Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see . - -/* -Package rpc provides access to the exported methods of an object across a network -or other I/O connection. After creating a server instance objects can be registered, -making it visible from the outside. Exported methods that follow specific -conventions can be called remotely. It also has support for the publish/subscribe -pattern. - -Methods that satisfy the following criteria are made available for remote access: - - object must be exported - - method must be exported - - method returns 0, 1 (response or error) or 2 (response and error) values - - method argument(s) must be exported or builtin types - - method returned value(s) must be exported or builtin types - -An example method: - func (s *CalcService) Div(a, b int) (int, error) - -When the returned error isn't nil the returned integer is ignored and the error is -send back to the client. Otherwise the returned integer is send back to the client. - -The server offers the ServeCodec method which accepts a ServerCodec instance. It will -read requests from the codec, process the request and sends the response back to the -client using the codec. The server can execute requests concurrently. Responses -can be send back to the client out of order. - -An example server which uses the JSON codec: - type CalculatorService struct {} - - func (s *CalculatorService) Add(a, b int) int { - return a + b - } - - func (s *CalculatorService Div(a, b int) (int, error) { - if b == 0 { - return 0, errors.New("divide by zero") - } - return a/b, nil - } - - calculator := new(CalculatorService) - server := NewServer() - server.RegisterName("calculator", calculator") - - l, _ := net.ListenUnix("unix", &net.UnixAddr{Net: "unix", Name: "/tmp/calculator.sock"}) - for { - c, _ := l.AcceptUnix() - codec := v2.NewJSONCodec(c) - go server.ServeCodec(codec) - } - -The package also supports the publish subscribe pattern through the use of subscriptions. -A method that is considered eligible for notifications must satisfy the following criteria: - - object must be exported - - method must be exported - - method argument(s) must be exported or builtin types - - method must return the tuple Subscription, error - - -An example method: - func (s *BlockChainService) Head() (Subscription, error) { - sub := s.bc.eventMux.Subscribe(ChainHeadEvent{}) - return v2.NewSubscription(sub), nil - } - -This method will push all raised ChainHeadEvents to subscribed clients. If the client is only -interested in every N'th block it is possible to add a criteria. - - func (s *BlockChainService) HeadFiltered(nth uint64) (Subscription, error) { - sub := s.bc.eventMux.Subscribe(ChainHeadEvent{}) - - criteria := func(event interface{}) bool { - chainHeadEvent := event.(ChainHeadEvent) - if chainHeadEvent.Block.NumberU64() % nth == 0 { - return true - } - return false - } - - return v2.NewSubscriptionFiltered(sub, criteria), nil - } - -Subscriptions are deleted when: - - the user sends an unsubscribe request - - the connection which was used to create the subscription is closed -*/ -package v2 diff --git a/rpc/v2/errors.go b/rpc/v2/errors.go deleted file mode 100644 index a06d19d84..000000000 --- a/rpc/v2/errors.go +++ /dev/null @@ -1,85 +0,0 @@ -// Copyright 2015 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library 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 Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see . - -package v2 - -import "fmt" - -// request is for an unknown service -type methodNotFoundError struct { - service string - method string -} - -func (e *methodNotFoundError) Code() int { - return -32601 -} - -func (e *methodNotFoundError) Error() string { - return fmt.Sprintf("The method %s%s%s does not exist/is not available", e.service, serviceMethodSeparator, e.method) -} - -// received message isn't a valid request -type invalidRequestError struct { - message string -} - -func (e *invalidRequestError) Code() int { - return -32600 -} - -func (e *invalidRequestError) Error() string { - return e.message -} - -// received message is invalid -type invalidMessageError struct { - message string -} - -func (e *invalidMessageError) Code() int { - return -32700 -} - -func (e *invalidMessageError) Error() string { - return e.message -} - -// unable to decode supplied params, or an invalid number of parameters -type invalidParamsError struct { - message string -} - -func (e *invalidParamsError) Code() int { - return -32602 -} - -func (e *invalidParamsError) Error() string { - return e.message -} - -// logic error, callback returned an error -type callbackError struct { - message string -} - -func (e *callbackError) Code() int { - return -32000 -} - -func (e *callbackError) Error() string { - return e.message -} diff --git a/rpc/v2/json.go b/rpc/v2/json.go deleted file mode 100644 index 9208e2d37..000000000 --- a/rpc/v2/json.go +++ /dev/null @@ -1,343 +0,0 @@ -// Copyright 2015 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library 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 Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see . - -package v2 - -import ( - "encoding/json" - "fmt" - "io" - "reflect" - "strings" - "sync/atomic" - - "github.com/ethereum/go-ethereum/logger" - "github.com/ethereum/go-ethereum/logger/glog" -) - -const ( - jsonRPCVersion = "2.0" - serviceMethodSeparator = "_" - subscribeMethod = "eth_subscribe" - unsubscribeMethod = "eth_unsubscribe" - notificationMethod = "eth_subscription" -) - -// JSON-RPC request -type jsonRequest struct { - Method string `json:"method"` - Version string `json:"jsonrpc"` - Id *int64 `json:"id,omitempty"` - Payload json.RawMessage `json:"params"` -} - -// JSON-RPC response -type jsonSuccessResponse struct { - Version string `json:"jsonrpc"` - Id int64 `json:"id"` - Result interface{} `json:"result,omitempty"` -} - -// JSON-RPC error object -type jsonError struct { - Code int `json:"code"` - Message string `json:"message"` - Data interface{} `json:"data,omitempty"` -} - -// JSON-RPC error response -type jsonErrResponse struct { - Version string `json:"jsonrpc"` - Id *int64 `json:"id,omitempty"` - Error jsonError `json:"error"` -} - -// JSON-RPC notification payload -type jsonSubscription struct { - Subscription string `json:"subscription"` - Result interface{} `json:"result,omitempty"` -} - -// JSON-RPC notification -type jsonNotification struct { - Version string `json:"jsonrpc"` - Method string `json:"method"` - Params jsonSubscription `json:"params"` -} - -// jsonCodec reads and writes JSON-RPC messages to the underlying connection. It also has support for parsing arguments -// and serializing (result) objects. -type jsonCodec struct { - closed chan interface{} - isClosed int32 - d *json.Decoder - e *json.Encoder - req jsonRequest - rw io.ReadWriteCloser -} - -// NewJSONCodec creates a new RPC server codec with support for JSON-RPC 2.0 -func NewJSONCodec(rwc io.ReadWriteCloser) ServerCodec { - d := json.NewDecoder(rwc) - d.UseNumber() - return &jsonCodec{closed: make(chan interface{}), d: d, e: json.NewEncoder(rwc), rw: rwc, isClosed: 0} -} - -// isBatch returns true when the first non-whitespace characters is '[' -func isBatch(msg json.RawMessage) bool { - for _, c := range msg { - // skip insignificant whitespace (http://www.ietf.org/rfc/rfc4627.txt) - if c == 0x20 || c == 0x09 || c == 0x0a || c == 0x0d { - continue - } - return c == '[' - } - return false -} - -// ReadRequestHeaders will read new requests without parsing the arguments. It will return a collection of requests, an -// indication if these requests are in batch form or an error when the incoming message could not be read/parsed. -func (c *jsonCodec) ReadRequestHeaders() ([]rpcRequest, bool, RPCError) { - var incomingMsg json.RawMessage - if err := c.d.Decode(&incomingMsg); err != nil { - return nil, false, &invalidRequestError{err.Error()} - } - - if isBatch(incomingMsg) { - return parseBatchRequest(incomingMsg) - } - - return parseRequest(incomingMsg) -} - -// parseRequest will parse a single request from the given RawMessage. It will return the parsed request, an indication -// if the request was a batch or an error when the request could not be parsed. -func parseRequest(incomingMsg json.RawMessage) ([]rpcRequest, bool, RPCError) { - var in jsonRequest - if err := json.Unmarshal(incomingMsg, &in); err != nil { - return nil, false, &invalidMessageError{err.Error()} - } - - if in.Id == nil { - return nil, false, &invalidMessageError{"Server cannot handle notifications"} - } - - // subscribe are special, they will always use `subscribeMethod` as service method - if in.Method == subscribeMethod { - reqs := []rpcRequest{rpcRequest{id: *in.Id, isPubSub: true}} - if len(in.Payload) > 0 { - // first param must be subscription name - var subscribeMethod [1]string - if err := json.Unmarshal(in.Payload, &subscribeMethod); err != nil { - glog.V(logger.Debug).Infof("Unable to parse subscription method: %v\n", err) - return nil, false, &invalidRequestError{"Unable to parse subscription request"} - } - - // all subscriptions are made on the eth service - reqs[0].service, reqs[0].method = "eth", subscribeMethod[0] - reqs[0].params = in.Payload - return reqs, false, nil - } - return nil, false, &invalidRequestError{"Unable to parse subscription request"} - } - - if in.Method == unsubscribeMethod { - return []rpcRequest{rpcRequest{id: *in.Id, isPubSub: true, - method: unsubscribeMethod, params: in.Payload}}, false, nil - } - - // regular RPC call - elems := strings.Split(in.Method, serviceMethodSeparator) - if len(elems) != 2 { - return nil, false, &methodNotFoundError{in.Method, ""} - } - - if len(in.Payload) == 0 { - return []rpcRequest{rpcRequest{service: elems[0], method: elems[1], id: *in.Id}}, false, nil - } - - return []rpcRequest{rpcRequest{service: elems[0], method: elems[1], id: *in.Id, params: in.Payload}}, false, nil -} - -// parseBatchRequest will parse a batch request into a collection of requests from the given RawMessage, an indication -// if the request was a batch or an error when the request could not be read. -func parseBatchRequest(incomingMsg json.RawMessage) ([]rpcRequest, bool, RPCError) { - var in []jsonRequest - if err := json.Unmarshal(incomingMsg, &in); err != nil { - return nil, false, &invalidMessageError{err.Error()} - } - - requests := make([]rpcRequest, len(in)) - for i, r := range in { - if r.Id == nil { - return nil, true, &invalidMessageError{"Server cannot handle notifications"} - } - - // (un)subscribe are special, they will always use the same service.method - if r.Method == subscribeMethod { - requests[i] = rpcRequest{id: *r.Id, isPubSub: true} - if len(r.Payload) > 0 { - var subscribeMethod [1]string - if err := json.Unmarshal(r.Payload, &subscribeMethod); err != nil { - glog.V(logger.Debug).Infof("Unable to parse subscription method: %v\n", err) - return nil, false, &invalidRequestError{"Unable to parse subscription request"} - } - - // all subscriptions are made on the eth service - requests[i].service, requests[i].method = "eth", subscribeMethod[0] - requests[i].params = r.Payload - continue - } - - return nil, true, &invalidRequestError{"Unable to parse (un)subscribe request arguments"} - } - - if r.Method == unsubscribeMethod { - requests[i] = rpcRequest{id: *r.Id, isPubSub: true, method: unsubscribeMethod, params: r.Payload} - continue - } - - elems := strings.Split(r.Method, serviceMethodSeparator) - if len(elems) != 2 { - return nil, true, &methodNotFoundError{r.Method, ""} - } - - if len(r.Payload) == 0 { - requests[i] = rpcRequest{service: elems[0], method: elems[1], id: *r.Id, params: nil} - } else { - requests[i] = rpcRequest{service: elems[0], method: elems[1], id: *r.Id, params: r.Payload} - } - } - - return requests, true, nil -} - -// ParseRequestArguments tries to parse the given params (json.RawMessage) with the given types. It returns the parsed -// values or an error when the parsing failed. -func (c *jsonCodec) ParseRequestArguments(argTypes []reflect.Type, params interface{}) ([]reflect.Value, RPCError) { - if args, ok := params.(json.RawMessage); !ok { - return nil, &invalidParamsError{"Invalid params supplied"} - } else { - return parsePositionalArguments(args, argTypes) - } -} - -func countArguments(args json.RawMessage) (int, error) { - var cnt []interface{} - if err := json.Unmarshal(args, &cnt); err != nil { - return -1, nil - } - return len(cnt), nil -} - -// parsePositionalArguments tries to parse the given args to an array of values with the given types. It returns the -// parsed values or an error when the args could not be parsed. -func parsePositionalArguments(args json.RawMessage, argTypes []reflect.Type) ([]reflect.Value, RPCError) { - argValues := make([]reflect.Value, len(argTypes)) - params := make([]interface{}, len(argTypes)) - - n, err := countArguments(args) - if err != nil { - return nil, &invalidParamsError{err.Error()} - } - if n != len(argTypes) { - return nil, &invalidParamsError{fmt.Sprintf("insufficient params, want %d have %d", len(argTypes), n)} - } - - for i, t := range argTypes { - if t.Kind() == reflect.Ptr { - // values must be pointers for the Unmarshal method, reflect. - // Dereference otherwise reflect.New would create **SomeType - argValues[i] = reflect.New(t.Elem()) - params[i] = argValues[i].Interface() - - // when not specified blockNumbers are by default latest (-1) - if blockNumber, ok := params[i].(*BlockNumber); ok { - *blockNumber = BlockNumber(-1) - } - } else { - argValues[i] = reflect.New(t) - params[i] = argValues[i].Interface() - - // when not specified blockNumbers are by default latest (-1) - if blockNumber, ok := params[i].(*BlockNumber); ok { - *blockNumber = BlockNumber(-1) - } - } - } - - if err := json.Unmarshal(args, ¶ms); err != nil { - return nil, &invalidParamsError{err.Error()} - } - - // Convert pointers back to values where necessary - for i, a := range argValues { - if a.Kind() != argTypes[i].Kind() { - argValues[i] = reflect.Indirect(argValues[i]) - } - } - - return argValues, nil -} - -// CreateResponse will create a JSON-RPC success response with the given id and reply as result. -func (c *jsonCodec) CreateResponse(id int64, reply interface{}) interface{} { - if isHexNum(reflect.TypeOf(reply)) { - return &jsonSuccessResponse{Version: jsonRPCVersion, Id: id, Result: fmt.Sprintf(`%#x`, reply)} - } - return &jsonSuccessResponse{Version: jsonRPCVersion, Id: id, Result: reply} -} - -// CreateErrorResponse will create a JSON-RPC error response with the given id and error. -func (c *jsonCodec) CreateErrorResponse(id *int64, err RPCError) interface{} { - return &jsonErrResponse{Version: jsonRPCVersion, Id: id, Error: jsonError{Code: err.Code(), Message: err.Error()}} -} - -// CreateErrorResponseWithInfo will create a JSON-RPC error response with the given id and error. -// info is optional and contains additional information about the error. When an empty string is passed it is ignored. -func (c *jsonCodec) CreateErrorResponseWithInfo(id *int64, err RPCError, info interface{}) interface{} { - return &jsonErrResponse{Version: jsonRPCVersion, Id: id, - Error: jsonError{Code: err.Code(), Message: err.Error(), Data: info}} -} - -// CreateNotification will create a JSON-RPC notification with the given subscription id and event as params. -func (c *jsonCodec) CreateNotification(subid string, event interface{}) interface{} { - if isHexNum(reflect.TypeOf(event)) { - return &jsonNotification{Version: jsonRPCVersion, Method: notificationMethod, - Params: jsonSubscription{Subscription: subid, Result: fmt.Sprintf(`%#x`, event)}} - } - - return &jsonNotification{Version: jsonRPCVersion, Method: notificationMethod, - Params: jsonSubscription{Subscription: subid, Result: event}} -} - -// Write message to client -func (c *jsonCodec) Write(res interface{}) error { - return c.e.Encode(res) -} - -// Close the underlying connection -func (c *jsonCodec) Close() { - if atomic.CompareAndSwapInt32(&c.isClosed, 0, 1) { - close(c.closed) - c.rw.Close() - } -} - -// Closed returns a channel which will be closed when Close is called -func (c *jsonCodec) Closed() <-chan interface{} { - return c.closed -} diff --git a/rpc/v2/json_test.go b/rpc/v2/json_test.go deleted file mode 100644 index dc8a345d7..000000000 --- a/rpc/v2/json_test.go +++ /dev/null @@ -1,73 +0,0 @@ -package v2 - -import ( - "bufio" - "bytes" - "reflect" - "testing" -) - -type RWC struct { - *bufio.ReadWriter -} - -func (rwc *RWC) Close() error { - return nil -} - -func TestJSONRequestParsing(t *testing.T) { - server := NewServer() - service := new(Service) - - if err := server.RegisterName("calc", service); err != nil { - t.Fatalf("%v", err) - } - - req := bytes.NewBufferString(`{"id": 1234, "jsonrpc": "2.0", "method": "calc_add", "params": [11, 22]}`) - var str string - reply := bytes.NewBufferString(str) - rw := &RWC{bufio.NewReadWriter(bufio.NewReader(req), bufio.NewWriter(reply))} - - codec := NewJSONCodec(rw) - - requests, batch, err := codec.ReadRequestHeaders() - if err != nil { - t.Fatalf("%v", err) - } - - if batch { - t.Fatalf("Request isn't a batch") - } - - if len(requests) != 1 { - t.Fatalf("Expected 1 request but got %d requests - %v", len(requests), requests) - } - - if requests[0].service != "calc" { - t.Fatalf("Expected service 'calc' but got '%s'", requests[0].service) - } - - if requests[0].method != "add" { - t.Fatalf("Expected method 'Add' but got '%s'", requests[0].method) - } - - if requests[0].id != 1234 { - t.Fatalf("Expected id 1234 but got %d", requests[0].id) - } - - var arg int - args := []reflect.Type{reflect.TypeOf(arg), reflect.TypeOf(arg)} - - v, err := codec.ParseRequestArguments(args, requests[0].params) - if err != nil { - t.Fatalf("%v", err) - } - - if len(v) != 2 { - t.Fatalf("Expected 2 argument values, got %d", len(v)) - } - - if v[0].Int() != 11 || v[1].Int() != 22 { - t.Fatalf("expected %d == 11 && %d == 22", v[0].Int(), v[1].Int()) - } -} diff --git a/rpc/v2/server.go b/rpc/v2/server.go deleted file mode 100644 index 4c04f04d2..000000000 --- a/rpc/v2/server.go +++ /dev/null @@ -1,385 +0,0 @@ -// Copyright 2015 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library 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 Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see . - -package v2 - -import ( - "fmt" - "reflect" - - "runtime" - - "github.com/ethereum/go-ethereum/event" - "github.com/ethereum/go-ethereum/logger" - "github.com/ethereum/go-ethereum/logger/glog" - "golang.org/x/net/context" -) - -// NewServer will create a new server instance with no registered handlers. -func NewServer() *Server { - server := &Server{services: make(serviceRegistry), subscriptions: make(subscriptionRegistry)} - - // register a default service which will provide meta information about the RPC service such as the services and - // methods it offers. - rpcService := &RPCService{server} - server.RegisterName("rpc", rpcService) - - return server -} - -// RPCService gives meta information about the server. -// e.g. gives information about the loaded modules. -type RPCService struct { - server *Server -} - -// Modules returns the list of RPC services with their version number -func (s *RPCService) Modules() map[string]string { - modules := make(map[string]string) - for name, _ := range s.server.services { - modules[name] = "1.0" - } - return modules -} - -// RegisterName will create an service for the given rcvr type under the given name. When no methods on the given rcvr -// match the criteria to be either a RPC method or a subscription an error is returned. Otherwise a new service is -// created and added to the service collection this server instance serves. -func (s *Server) RegisterName(name string, rcvr interface{}) error { - if s.services == nil { - s.services = make(serviceRegistry) - } - - svc := new(service) - svc.typ = reflect.TypeOf(rcvr) - rcvrVal := reflect.ValueOf(rcvr) - - if name == "" { - return fmt.Errorf("no service name for type %s", svc.typ.String()) - } - if !isExported(reflect.Indirect(rcvrVal).Type().Name()) { - return fmt.Errorf("%s is not exported", reflect.Indirect(rcvrVal).Type().Name()) - } - - // already a previous service register under given sname, merge methods/subscriptions - if regsvc, present := s.services[name]; present { - methods, subscriptions := suitableCallbacks(rcvrVal, svc.typ) - if len(methods) == 0 && len(subscriptions) == 0 { - return fmt.Errorf("Service doesn't have any suitable methods/subscriptions to expose") - } - - for _, m := range methods { - regsvc.callbacks[formatName(m.method.Name)] = m - } - for _, s := range subscriptions { - regsvc.subscriptions[formatName(s.method.Name)] = s - } - - return nil - } - - svc.name = name - svc.callbacks, svc.subscriptions = suitableCallbacks(rcvrVal, svc.typ) - - if len(svc.callbacks) == 0 && len(svc.subscriptions) == 0 { - return fmt.Errorf("Service doesn't have any suitable methods/subscriptions to expose") - } - - s.services[svc.name] = svc - - return nil -} - -// ServeCodec reads incoming requests from codec, calls the appropriate callback and writes the -// response back using the given codec. It will block until the codec is closed. -// -// This server will: -// 1. allow for asynchronous and parallel request execution -// 2. supports notifications (pub/sub) -// 3. supports request batches -func (s *Server) ServeCodec(codec ServerCodec) { - defer func() { - if err := recover(); err != nil { - const size = 64 << 10 - buf := make([]byte, size) - buf = buf[:runtime.Stack(buf, false)] - glog.Errorln(string(buf)) - } - codec.Close() - }() - - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() - - for { - reqs, batch, err := s.readRequest(codec) - if err != nil { - glog.V(logger.Debug).Infof("%v\n", err) - codec.Write(codec.CreateErrorResponse(nil, err)) - break - } - - if batch { - go s.execBatch(ctx, codec, reqs) - } else { - go s.exec(ctx, codec, reqs[0]) - } - } -} - -// sendNotification will create a notification from the given event by serializing member fields of the event. -// It will then send the notification to the client, when it fails the codec is closed. When the event has multiple -// fields an array of values is returned. -func sendNotification(codec ServerCodec, subid string, event interface{}) { - notification := codec.CreateNotification(subid, event) - - if err := codec.Write(notification); err != nil { - codec.Close() - } -} - -// createSubscription will register a new subscription and waits for raised events. When an event is raised it will: -// 1. test if the event is raised matches the criteria the user has (optionally) specified -// 2. create a notification of the event and send it the client when it matches the criteria -// It will unsubscribe the subscription when the socket is closed or the subscription is unsubscribed by the user. -func (s *Server) createSubscription(c ServerCodec, req *serverRequest) (string, error) { - args := []reflect.Value{req.callb.rcvr} - if len(req.args) > 0 { - args = append(args, req.args...) - } - - subid, err := newSubscriptionId() - if err != nil { - return "", err - } - - reply := req.callb.method.Func.Call(args) - - if reply[1].IsNil() { // no error - if subscription, ok := reply[0].Interface().(Subscription); ok { - s.muSubcriptions.Lock() - s.subscriptions[subid] = subscription - s.muSubcriptions.Unlock() - go func() { - cases := []reflect.SelectCase{ - reflect.SelectCase{Dir: reflect.SelectRecv, Chan: reflect.ValueOf(subscription.Chan())}, // new event - reflect.SelectCase{Dir: reflect.SelectRecv, Chan: reflect.ValueOf(c.Closed())}, // connection closed - } - - for { - idx, notification, recvOk := reflect.Select(cases) - switch idx { - case 0: // new event, or channel closed - if recvOk { // send notification - if event, ok := notification.Interface().(*event.Event); ok { - if subscription.match == nil || subscription.match(event.Data) { - sendNotification(c, subid, subscription.format(event.Data)) - } - } - } else { // user send an eth_unsubscribe request - return - } - case 1: // connection closed - s.unsubscribe(subid) - return - } - } - }() - } else { // unable to create subscription - s.muSubcriptions.Lock() - delete(s.subscriptions, subid) - s.muSubcriptions.Unlock() - } - } else { - return "", fmt.Errorf("Unable to create subscription") - } - - return subid, nil -} - -// unsubscribe calls the Unsubscribe method on the subscription and removes a subscription from the subscription -// registry. -func (s *Server) unsubscribe(subid string) bool { - s.muSubcriptions.Lock() - defer s.muSubcriptions.Unlock() - if sub, ok := s.subscriptions[subid]; ok { - sub.Unsubscribe() - delete(s.subscriptions, subid) - return true - } - return false -} - -// handle executes a request and returns the response from the callback. -func (s *Server) handle(ctx context.Context, codec ServerCodec, req *serverRequest) interface{} { - if req.err != nil { - return codec.CreateErrorResponse(&req.id, req.err) - } - - if req.isUnsubscribe { // first param must be the subscription id - if len(req.args) >= 1 && req.args[0].Kind() == reflect.String { - subid := req.args[0].String() - if s.unsubscribe(subid) { - return codec.CreateResponse(req.id, true) - } else { - return codec.CreateErrorResponse(&req.id, - &callbackError{fmt.Sprintf("subscription '%s' not found", subid)}) - } - } - return codec.CreateErrorResponse(&req.id, &invalidParamsError{"Expected subscription id as argument"}) - } - - if req.callb.isSubscribe { - subid, err := s.createSubscription(codec, req) - if err != nil { - return codec.CreateErrorResponse(&req.id, &callbackError{err.Error()}) - } - return codec.CreateResponse(req.id, subid) - } - - // regular RPC call - if len(req.args) != len(req.callb.argTypes) { - rpcErr := &invalidParamsError{fmt.Sprintf("%s%s%s expects %d parameters, got %d", - req.svcname, serviceMethodSeparator, req.callb.method.Name, - len(req.callb.argTypes), len(req.args))} - return codec.CreateErrorResponse(&req.id, rpcErr) - } - - arguments := []reflect.Value{req.callb.rcvr} - if req.callb.hasCtx { - arguments = append(arguments, reflect.ValueOf(ctx)) - } - if len(req.args) > 0 { - arguments = append(arguments, req.args...) - } - - reply := req.callb.method.Func.Call(arguments) - - if len(reply) == 0 { - return codec.CreateResponse(req.id, nil) - } - - if req.callb.errPos >= 0 { // test if method returned an error - if !reply[req.callb.errPos].IsNil() { - e := reply[req.callb.errPos].Interface().(error) - res := codec.CreateErrorResponse(&req.id, &callbackError{e.Error()}) - return res - } - } - - return codec.CreateResponse(req.id, reply[0].Interface()) -} - -// exec executes the given request and writes the result back using the codec. -func (s *Server) exec(ctx context.Context, codec ServerCodec, req *serverRequest) { - var response interface{} - if req.err != nil { - response = codec.CreateErrorResponse(&req.id, req.err) - } else { - response = s.handle(ctx, codec, req) - } - - if err := codec.Write(response); err != nil { - glog.V(logger.Error).Infof("%v\n", err) - codec.Close() - } -} - -// execBatch executes the given requests and writes the result back using the codec. It will only write the response -// back when the last request is processed. -func (s *Server) execBatch(ctx context.Context, codec ServerCodec, requests []*serverRequest) { - responses := make([]interface{}, len(requests)) - for i, req := range requests { - if req.err != nil { - responses[i] = codec.CreateErrorResponse(&req.id, req.err) - } else { - responses[i] = s.handle(ctx, codec, req) - } - } - - if err := codec.Write(responses); err != nil { - glog.V(logger.Error).Infof("%v\n", err) - codec.Close() - } -} - -// readRequest requests the next (batch) request from the codec. It will return the collection of requests, an -// indication if the request was a batch, the invalid request identifier and an error when the request could not be -// read/parsed. -func (s *Server) readRequest(codec ServerCodec) ([]*serverRequest, bool, RPCError) { - reqs, batch, err := codec.ReadRequestHeaders() - if err != nil { - return nil, batch, err - } - - requests := make([]*serverRequest, len(reqs)) - - // verify requests - for i, r := range reqs { - var ok bool - var svc *service - - if r.isPubSub && r.method == unsubscribeMethod { - requests[i] = &serverRequest{id: r.id, isUnsubscribe: true} - argTypes := []reflect.Type{reflect.TypeOf("")} - if args, err := codec.ParseRequestArguments(argTypes, r.params); err == nil { - requests[i].args = args - } else { - requests[i].err = &invalidParamsError{err.Error()} - } - continue - } - - if svc, ok = s.services[r.service]; !ok { - requests[i] = &serverRequest{id: r.id, err: &methodNotFoundError{r.service, r.method}} - continue - } - - if r.isPubSub { // eth_subscribe - if callb, ok := svc.subscriptions[r.method]; ok { - requests[i] = &serverRequest{id: r.id, svcname: svc.name, callb: callb} - if r.params != nil && len(callb.argTypes) > 0 { - argTypes := []reflect.Type{reflect.TypeOf("")} - argTypes = append(argTypes, callb.argTypes...) - if args, err := codec.ParseRequestArguments(argTypes, r.params); err == nil { - requests[i].args = args[1:] // first one is service.method name which isn't an actual argument - } else { - requests[i].err = &invalidParamsError{err.Error()} - } - } - } else { - requests[i] = &serverRequest{id: r.id, err: &methodNotFoundError{subscribeMethod, r.method}} - } - continue - } - - if callb, ok := svc.callbacks[r.method]; ok { - requests[i] = &serverRequest{id: r.id, svcname: svc.name, callb: callb} - if r.params != nil && len(callb.argTypes) > 0 { - if args, err := codec.ParseRequestArguments(callb.argTypes, r.params); err == nil { - requests[i].args = args - } else { - requests[i].err = &invalidParamsError{err.Error()} - } - } - continue - } - - requests[i] = &serverRequest{id: r.id, err: &methodNotFoundError{r.service, r.method}} - } - - return requests, batch, nil -} diff --git a/rpc/v2/server_test.go b/rpc/v2/server_test.go deleted file mode 100644 index f250c184f..000000000 --- a/rpc/v2/server_test.go +++ /dev/null @@ -1,255 +0,0 @@ -package v2 - -import ( - "encoding/json" - "fmt" - "reflect" - "testing" - "time" - - "golang.org/x/net/context" -) - -type Service struct{} - -type Args struct { - S string -} - -func (s *Service) NoArgsRets() { -} - -type Result struct { - String string - Int int - Args *Args -} - -func (s *Service) Echo(str string, i int, args *Args) Result { - return Result{str, i, args} -} - -func (s *Service) EchoWithCtx(ctx context.Context, str string, i int, args *Args) Result { - return Result{str, i, args} -} - -func (s *Service) Rets() (string, error) { - return "", nil -} - -func (s *Service) InvalidRets1() (error, string) { - return nil, "" -} - -func (s *Service) InvalidRets2() (string, string) { - return "", "" -} - -func (s *Service) InvalidRets3() (string, string, error) { - return "", "", nil -} - -func (s *Service) Subscription() (Subscription, error) { - return NewSubscription(nil), nil -} - -func TestServerRegisterName(t *testing.T) { - server := NewServer() - service := new(Service) - - if err := server.RegisterName("calc", service); err != nil { - t.Fatalf("%v", err) - } - - if len(server.services) != 2 { - t.Fatalf("Expected 2 service entries, got %d", len(server.services)) - } - - svc, ok := server.services["calc"] - if !ok { - t.Fatalf("Expected service calc to be registered") - } - - if len(svc.callbacks) != 4 { - t.Errorf("Expected 4 callbacks for service 'calc', got %d", len(svc.callbacks)) - } - - if len(svc.subscriptions) != 1 { - t.Errorf("Expected 1 subscription for service 'calc', got %d", len(svc.subscriptions)) - } -} - -// dummy codec used for testing RPC method execution -type ServerTestCodec struct { - counter int - input []byte - output string - closer chan interface{} -} - -func (c *ServerTestCodec) ReadRequestHeaders() ([]rpcRequest, bool, RPCError) { - c.counter += 1 - - if c.counter == 1 { - var req jsonRequest - json.Unmarshal(c.input, &req) - return []rpcRequest{rpcRequest{id: *req.Id, isPubSub: false, service: "test", method: req.Method, params: req.Payload}}, false, nil - } - - // requests are executes in parallel, wait a bit before returning an error so that the previous request has time to - // be executed - timer := time.NewTimer(time.Duration(2) * time.Second) - <-timer.C - - return nil, false, &invalidRequestError{"connection closed"} -} - -func (c *ServerTestCodec) ParseRequestArguments(argTypes []reflect.Type, payload interface{}) ([]reflect.Value, RPCError) { - - args, _ := payload.(json.RawMessage) - - argValues := make([]reflect.Value, len(argTypes)) - params := make([]interface{}, len(argTypes)) - - n, err := countArguments(args) - if err != nil { - return nil, &invalidParamsError{err.Error()} - } - if n != len(argTypes) { - return nil, &invalidParamsError{fmt.Sprintf("insufficient params, want %d have %d", len(argTypes), n)} - - } - - for i, t := range argTypes { - if t.Kind() == reflect.Ptr { - // values must be pointers for the Unmarshal method, reflect. - // Dereference otherwise reflect.New would create **SomeType - argValues[i] = reflect.New(t.Elem()) - params[i] = argValues[i].Interface() - - // when not specified blockNumbers are by default latest (-1) - if blockNumber, ok := params[i].(*BlockNumber); ok { - *blockNumber = BlockNumber(-1) - } - } else { - argValues[i] = reflect.New(t) - params[i] = argValues[i].Interface() - - // when not specified blockNumbers are by default latest (-1) - if blockNumber, ok := params[i].(*BlockNumber); ok { - *blockNumber = BlockNumber(-1) - } - } - } - - if err := json.Unmarshal(args, ¶ms); err != nil { - return nil, &invalidParamsError{err.Error()} - } - - // Convert pointers back to values where necessary - for i, a := range argValues { - if a.Kind() != argTypes[i].Kind() { - argValues[i] = reflect.Indirect(argValues[i]) - } - } - - return argValues, nil -} - -func (c *ServerTestCodec) CreateResponse(id int64, reply interface{}) interface{} { - return &jsonSuccessResponse{Version: jsonRPCVersion, Id: id, Result: reply} -} - -func (c *ServerTestCodec) CreateErrorResponse(id *int64, err RPCError) interface{} { - return &jsonErrResponse{Version: jsonRPCVersion, Id: id, Error: jsonError{Code: err.Code(), Message: err.Error()}} -} - -func (c *ServerTestCodec) CreateErrorResponseWithInfo(id *int64, err RPCError, info interface{}) interface{} { - return &jsonErrResponse{Version: jsonRPCVersion, Id: id, - Error: jsonError{Code: err.Code(), Message: err.Error(), Data: info}} -} - -func (c *ServerTestCodec) CreateNotification(subid string, event interface{}) interface{} { - return &jsonNotification{Version: jsonRPCVersion, Method: notificationMethod, - Params: jsonSubscription{Subscription: subid, Result: event}} -} - -func (c *ServerTestCodec) Write(msg interface{}) error { - if len(c.output) == 0 { // only capture first response - if o, err := json.Marshal(msg); err != nil { - return err - } else { - c.output = string(o) - } - } - - return nil -} - -func (c *ServerTestCodec) Close() { - close(c.closer) -} - -func (c *ServerTestCodec) Closed() <-chan interface{} { - return c.closer -} - -func TestServerMethodExecution(t *testing.T) { - server := NewServer() - service := new(Service) - - if err := server.RegisterName("test", service); err != nil { - t.Fatalf("%v", err) - } - - id := int64(12345) - req := jsonRequest{ - Method: "echo", - Version: "2.0", - Id: &id, - } - args := []interface{}{"string arg", 1122, &Args{"qwerty"}} - req.Payload, _ = json.Marshal(&args) - - input, _ := json.Marshal(&req) - codec := &ServerTestCodec{input: input, closer: make(chan interface{})} - go server.ServeCodec(codec) - - <-codec.closer - - expected := `{"jsonrpc":"2.0","id":12345,"result":{"String":"string arg","Int":1122,"Args":{"S":"qwerty"}}}` - - if expected != codec.output { - t.Fatalf("expected %s, got %s\n", expected, codec.output) - } -} - -func TestServerMethodWithCtx(t *testing.T) { - server := NewServer() - service := new(Service) - - if err := server.RegisterName("test", service); err != nil { - t.Fatalf("%v", err) - } - - id := int64(12345) - req := jsonRequest{ - Method: "echoWithCtx", - Version: "2.0", - Id: &id, - } - args := []interface{}{"string arg", 1122, &Args{"qwerty"}} - req.Payload, _ = json.Marshal(&args) - - input, _ := json.Marshal(&req) - codec := &ServerTestCodec{input: input, closer: make(chan interface{})} - go server.ServeCodec(codec) - - <-codec.closer - - expected := `{"jsonrpc":"2.0","id":12345,"result":{"String":"string arg","Int":1122,"Args":{"S":"qwerty"}}}` - - if expected != codec.output { - t.Fatalf("expected %s, got %s\n", expected, codec.output) - } -} diff --git a/rpc/v2/types.go b/rpc/v2/types.go deleted file mode 100644 index 8e638726f..000000000 --- a/rpc/v2/types.go +++ /dev/null @@ -1,352 +0,0 @@ -// Copyright 2015 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library 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 Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see . - -package v2 - -import ( - "fmt" - "math" - "math/big" - "reflect" - "strings" - "sync" - - "github.com/ethereum/go-ethereum/event" -) - -// API describes the set of methods offered over the RPC interface -type API struct { - Namespace string // namespace under which the rpc methods of Service are exposed - Version string // api version for DApp's - Service interface{} // receiver instance which holds the methods - Public bool // indication if the methods must be considered safe for public use -} - -// callback is a method callback which was registered in the server -type callback struct { - rcvr reflect.Value // receiver of method - method reflect.Method // callback - argTypes []reflect.Type // input argument types - hasCtx bool // method's first argument is a context (not included in argTypes) - errPos int // err return idx, of -1 when method cannot return error - isSubscribe bool // indication if the callback is a subscription -} - -// service represents a registered object -type service struct { - name string // name for service - rcvr reflect.Value // receiver of methods for the service - typ reflect.Type // receiver type - callbacks callbacks // registered handlers - subscriptions subscriptions // available subscriptions/notifications -} - -// serverRequest is an incoming request -type serverRequest struct { - id int64 - svcname string - rcvr reflect.Value - callb *callback - args []reflect.Value - isUnsubscribe bool - err RPCError -} - -type serviceRegistry map[string]*service // collection of services -type callbacks map[string]*callback // collection of RPC callbacks -type subscriptions map[string]*callback // collection of subscription callbacks -type subscriptionRegistry map[string]Subscription // collection of subscriptions - -// Server represents a RPC server -type Server struct { - services serviceRegistry - muSubcriptions sync.Mutex // protects subscriptions - subscriptions subscriptionRegistry -} - -// rpcRequest represents a raw incoming RPC request -type rpcRequest struct { - service string - method string - id int64 - isPubSub bool - params interface{} -} - -// RPCError implements RPC error, is add support for error codec over regular go errors -type RPCError interface { - // RPC error code - Code() int - // Error message - Error() string -} - -// ServerCodec implements reading, parsing and writing RPC messages for the server side of -// a RPC session. Implementations must be go-routine safe since the codec can be called in -// multiple go-routines concurrently. -type ServerCodec interface { - // Read next request - ReadRequestHeaders() ([]rpcRequest, bool, RPCError) - // Parse request argument to the given types - ParseRequestArguments([]reflect.Type, interface{}) ([]reflect.Value, RPCError) - // Assemble success response - CreateResponse(int64, interface{}) interface{} - // Assemble error response - CreateErrorResponse(*int64, RPCError) interface{} - // Assemble error response with extra information about the error through info - CreateErrorResponseWithInfo(id *int64, err RPCError, info interface{}) interface{} - // Create notification response - CreateNotification(string, interface{}) interface{} - // Write msg to client. - Write(interface{}) error - // Close underlying data stream - Close() - // Closed when underlying connection is closed - Closed() <-chan interface{} -} - -// SubscriptionMatcher returns true if the given value matches the criteria specified by the user -type SubscriptionMatcher func(interface{}) bool - -// SubscriptionOutputFormat accepts event data and has the ability to format the data before it is send to the client -type SubscriptionOutputFormat func(interface{}) interface{} - -// defaultSubscriptionOutputFormatter returns data and is used as default output format for notifications -func defaultSubscriptionOutputFormatter(data interface{}) interface{} { - return data -} - -// Subscription is used by the server to send notifications to the client -type Subscription struct { - sub event.Subscription - match SubscriptionMatcher - format SubscriptionOutputFormat -} - -// NewSubscription create a new RPC subscription -func NewSubscription(sub event.Subscription) Subscription { - return Subscription{sub, nil, defaultSubscriptionOutputFormatter} -} - -// NewSubscriptionWithOutputFormat create a new RPC subscription which a custom notification output format -func NewSubscriptionWithOutputFormat(sub event.Subscription, formatter SubscriptionOutputFormat) Subscription { - return Subscription{sub, nil, formatter} -} - -// NewSubscriptionFiltered will create a new subscription. For each raised event the given matcher is -// called. If it returns true the event is send as notification to the client, otherwise it is ignored. -func NewSubscriptionFiltered(sub event.Subscription, match SubscriptionMatcher) Subscription { - return Subscription{sub, match, defaultSubscriptionOutputFormatter} -} - -// Chan returns the channel where new events will be published. It's up the user to call the matcher to -// determine if the events are interesting for the client. -func (s *Subscription) Chan() <-chan *event.Event { - return s.sub.Chan() -} - -// Unsubscribe will end the subscription and closes the event channel -func (s *Subscription) Unsubscribe() { - s.sub.Unsubscribe() -} - -// HexNumber serializes a number to hex format using the "%#x" format -type HexNumber big.Int - -// NewHexNumber creates a new hex number instance which will serialize the given val with `%#x` on marshal. -func NewHexNumber(val interface{}) *HexNumber { - if val == nil { - return nil - } - - if v, ok := val.(*big.Int); ok && v != nil { - hn := new(big.Int).Set(v) - return (*HexNumber)(hn) - } - - rval := reflect.ValueOf(val) - - var unsigned uint64 - utype := reflect.TypeOf(unsigned) - if t := rval.Type(); t.ConvertibleTo(utype) { - hn := new(big.Int).SetUint64(rval.Convert(utype).Uint()) - return (*HexNumber)(hn) - } - - var signed int64 - stype := reflect.TypeOf(signed) - if t := rval.Type(); t.ConvertibleTo(stype) { - hn := new(big.Int).SetInt64(rval.Convert(stype).Int()) - return (*HexNumber)(hn) - } - - return nil -} - -func (h *HexNumber) UnmarshalJSON(input []byte) error { - length := len(input) - if length >= 2 && input[0] == '"' && input[length-1] == '"' { - input = input[1 : length-1] - } - - hn := (*big.Int)(h) - if _, ok := hn.SetString(string(input), 0); ok { - return nil - } - - return fmt.Errorf("Unable to parse number") -} - -// MarshalJSON serialize the hex number instance to a hex representation. -func (h *HexNumber) MarshalJSON() ([]byte, error) { - if h != nil { - hn := (*big.Int)(h) - if hn.BitLen() == 0 { - return []byte(`"0x0"`), nil - } - return []byte(fmt.Sprintf(`"0x%x"`, hn)), nil - } - return nil, nil -} - -func (h *HexNumber) Int() int { - hn := (*big.Int)(h) - return int(hn.Int64()) -} - -func (h *HexNumber) Int64() int64 { - hn := (*big.Int)(h) - return hn.Int64() -} - -func (h *HexNumber) Uint() uint { - hn := (*big.Int)(h) - return uint(hn.Uint64()) -} - -func (h *HexNumber) Uint64() uint64 { - hn := (*big.Int)(h) - return hn.Uint64() -} - -func (h *HexNumber) BigInt() *big.Int { - return (*big.Int)(h) -} - -type Number int64 - -func (n *Number) UnmarshalJSON(data []byte) error { - input := strings.TrimSpace(string(data)) - - if len(input) >= 2 && input[0] == '"' && input[len(input)-1] == '"' { - input = input[1 : len(input)-1] - } - - if len(input) == 0 { - *n = Number(latestBlockNumber.Int64()) - return nil - } - - in := new(big.Int) - _, ok := in.SetString(input, 0) - - if !ok { // test if user supplied string tag - return fmt.Errorf(`invalid number %s`, data) - } - - if in.Cmp(earliestBlockNumber) >= 0 && in.Cmp(maxBlockNumber) <= 0 { - *n = Number(in.Int64()) - return nil - } - - return fmt.Errorf("blocknumber not in range [%d, %d]", earliestBlockNumber, maxBlockNumber) -} - -func (n *Number) Int64() int64 { - return *(*int64)(n) -} - -func (n *Number) BigInt() *big.Int { - return big.NewInt(n.Int64()) -} - -var ( - pendingBlockNumber = big.NewInt(-2) - latestBlockNumber = big.NewInt(-1) - earliestBlockNumber = big.NewInt(0) - maxBlockNumber = big.NewInt(math.MaxInt64) -) - -type BlockNumber int64 - -const ( - PendingBlockNumber = BlockNumber(-2) - LatestBlockNumber = BlockNumber(-1) -) - -// UnmarshalJSON parses the given JSON fragement into a BlockNumber. It supports: -// - "latest" or "earliest" as string arguments -// - the block number -// Returned errors: -// - an unsupported error when "pending" is specified (not yet implemented) -// - an invalid block number error when the given argument isn't a known strings -// - an out of range error when the given block number is either too little or too large -func (bn *BlockNumber) UnmarshalJSON(data []byte) error { - input := strings.TrimSpace(string(data)) - - if len(input) >= 2 && input[0] == '"' && input[len(input)-1] == '"' { - input = input[1 : len(input)-1] - } - - if len(input) == 0 { - *bn = BlockNumber(latestBlockNumber.Int64()) - return nil - } - - in := new(big.Int) - _, ok := in.SetString(input, 0) - - if !ok { // test if user supplied string tag - strBlockNumber := input - if strBlockNumber == "latest" { - *bn = BlockNumber(latestBlockNumber.Int64()) - return nil - } - - if strBlockNumber == "earliest" { - *bn = BlockNumber(earliestBlockNumber.Int64()) - return nil - } - - if strBlockNumber == "pending" { - *bn = BlockNumber(pendingBlockNumber.Int64()) - return nil - } - - return fmt.Errorf(`invalid blocknumber %s`, data) - } - - if in.Cmp(earliestBlockNumber) >= 0 && in.Cmp(maxBlockNumber) <= 0 { - *bn = BlockNumber(in.Int64()) - return nil - } - - return fmt.Errorf("blocknumber not in range [%d, %d]", earliestBlockNumber, maxBlockNumber) -} - -func (bn *BlockNumber) Int64() int64 { - return (int64)(*bn) -} diff --git a/rpc/v2/types_test.go b/rpc/v2/types_test.go deleted file mode 100644 index f73a2369e..000000000 --- a/rpc/v2/types_test.go +++ /dev/null @@ -1,57 +0,0 @@ -package v2 - -import ( - "bytes" - "encoding/json" - "math/big" - "testing" -) - -func TestNewHexNumber(t *testing.T) { - tests := []interface{}{big.NewInt(123), int64(123), uint64(123), int8(123), uint8(123)} - - for i, v := range tests { - hn := NewHexNumber(v) - if hn == nil { - t.Fatalf("Unable to create hex number instance for tests[%d]", i) - } - if hn.Int64() != 123 { - t.Fatalf("expected %d, got %d on value tests[%d]", 123, hn.Int64(), i) - } - } - - failures := []interface{}{"", nil, []byte{1, 2, 3, 4}} - for i, v := range failures { - hn := NewHexNumber(v) - if hn != nil { - t.Fatalf("Creating a nex number instance of %T should fail (failures[%d])", failures[i], i) - } - } -} - -func TestHexNumberUnmarshalJSON(t *testing.T) { - tests := []string{`"0x4d2"`, "1234", `"1234"`} - for i, v := range tests { - var hn HexNumber - if err := json.Unmarshal([]byte(v), &hn); err != nil { - t.Fatalf("Test %d failed - %s", i, err) - } - - if hn.Int64() != 1234 { - t.Fatalf("Expected %d, got %d for test[%d]", 1234, hn.Int64(), i) - } - } -} - -func TestHexNumberMarshalJSON(t *testing.T) { - hn := NewHexNumber(1234567890) - got, err := json.Marshal(hn) - if err != nil { - t.Fatalf("Unable to marshal hex number - %s", err) - } - - exp := []byte(`"0x499602d2"`) - if bytes.Compare(exp, got) != 0 { - t.Fatalf("Invalid json.Marshal, expected '%s', got '%s'", exp, got) - } -} diff --git a/rpc/v2/utils.go b/rpc/v2/utils.go deleted file mode 100644 index ca37924a3..000000000 --- a/rpc/v2/utils.go +++ /dev/null @@ -1,214 +0,0 @@ -// Copyright 2015 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library 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 Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see . - -package v2 - -import ( - "crypto/rand" - "encoding/hex" - "errors" - "math/big" - "reflect" - "unicode" - "unicode/utf8" - - "golang.org/x/net/context" -) - -// Is this an exported - upper case - name? -func isExported(name string) bool { - rune, _ := utf8.DecodeRuneInString(name) - return unicode.IsUpper(rune) -} - -// Is this type exported or a builtin? -func isExportedOrBuiltinType(t reflect.Type) bool { - for t.Kind() == reflect.Ptr { - t = t.Elem() - } - // PkgPath will be non-empty even for an exported type, - // so we need to check the type name as well. - return isExported(t.Name()) || t.PkgPath() == "" -} - -var errorType = reflect.TypeOf((*error)(nil)).Elem() - -// Implements this type the error interface -func isErrorType(t reflect.Type) bool { - for t.Kind() == reflect.Ptr { - t = t.Elem() - } - return t.Implements(errorType) -} - -var subscriptionType = reflect.TypeOf((*Subscription)(nil)).Elem() - -func isSubscriptionType(t reflect.Type) bool { - for t.Kind() == reflect.Ptr { - t = t.Elem() - } - return t == subscriptionType -} - -// isPubSub tests whether the given method return the pair (v2.Subscription, error) -func isPubSub(methodType reflect.Type) bool { - if methodType.NumOut() != 2 { - return false - } - return isSubscriptionType(methodType.Out(0)) && isErrorType(methodType.Out(1)) -} - -// formatName will convert to first character to lower case -func formatName(name string) string { - ret := []rune(name) - if len(ret) > 0 { - ret[0] = unicode.ToLower(ret[0]) - } - return string(ret) -} - -var bigIntType = reflect.TypeOf((*big.Int)(nil)).Elem() - -// Indication if this type should be serialized in hex -func isHexNum(t reflect.Type) bool { - if t == nil { - return false - } - for t.Kind() == reflect.Ptr { - t = t.Elem() - } - - return t == bigIntType -} - -var blockNumberType = reflect.TypeOf((*BlockNumber)(nil)).Elem() - -// Indication if the given block is a BlockNumber -func isBlockNumber(t reflect.Type) bool { - if t == nil { - return false - } - - for t.Kind() == reflect.Ptr { - t = t.Elem() - } - - return t == blockNumberType -} - -var contextType = reflect.TypeOf(new(context.Context)).Elem() - -// suitableCallbacks iterates over the methods of the given type. It will determine if a method satisfies the criteria -// for a RPC callback or a subscription callback and adds it to the collection of callbacks or subscriptions. See server -// documentation for a summary of these criteria. -func suitableCallbacks(rcvr reflect.Value, typ reflect.Type) (callbacks, subscriptions) { - callbacks := make(callbacks) - subscriptions := make(subscriptions) - -METHODS: - for m := 0; m < typ.NumMethod(); m++ { - method := typ.Method(m) - mtype := method.Type - mname := formatName(method.Name) - if method.PkgPath != "" { // method must be exported - continue - } - - var h callback - h.isSubscribe = isPubSub(mtype) - h.rcvr = rcvr - h.method = method - h.errPos = -1 - - firstArg := 1 - numIn := mtype.NumIn() - if numIn >= 2 && mtype.In(1) == contextType { - h.hasCtx = true - firstArg = 2 - } - - if h.isSubscribe { - h.argTypes = make([]reflect.Type, numIn-firstArg) // skip rcvr type - for i := firstArg; i < numIn; i++ { - argType := mtype.In(i) - if isExportedOrBuiltinType(argType) { - h.argTypes[i-firstArg] = argType - } else { - continue METHODS - } - } - - subscriptions[mname] = &h - continue METHODS - } - - // determine method arguments, ignore first arg since it's the receiver type - // Arguments must be exported or builtin types - h.argTypes = make([]reflect.Type, numIn-firstArg) - for i := firstArg; i < numIn; i++ { - argType := mtype.In(i) - if !isExportedOrBuiltinType(argType) { - continue METHODS - } - h.argTypes[i-firstArg] = argType - } - - // check that all returned values are exported or builtin types - for i := 0; i < mtype.NumOut(); i++ { - if !isExportedOrBuiltinType(mtype.Out(i)) { - continue METHODS - } - } - - // when a method returns an error it must be the last returned value - h.errPos = -1 - for i := 0; i < mtype.NumOut(); i++ { - if isErrorType(mtype.Out(i)) { - h.errPos = i - break - } - } - - if h.errPos >= 0 && h.errPos != mtype.NumOut()-1 { - continue METHODS - } - - switch mtype.NumOut() { - case 0, 1: - break - case 2: - if h.errPos == -1 { // method must one return value and 1 error - continue METHODS - } - break - default: - continue METHODS - } - - callbacks[mname] = &h - } - - return callbacks, subscriptions -} - -func newSubscriptionId() (string, error) { - var subid [16]byte - n, _ := rand.Read(subid[:]) - if n != 16 { - return "", errors.New("Unable to generate subscription id") - } - return "0x" + hex.EncodeToString(subid[:]), nil -} diff --git a/rpc/websocket.go b/rpc/websocket.go new file mode 100644 index 000000000..b5bcbf4f6 --- /dev/null +++ b/rpc/websocket.go @@ -0,0 +1,235 @@ +// Copyright 2015 The go-ethereum Authors +// This file is part of the go-ethereum library. +// +// The go-ethereum library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The go-ethereum library 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 Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the go-ethereum library. If not, see . + +package rpc + +import ( + "errors" + "fmt" + "net" + "net/http" + "sync" + + "os" + + "github.com/ethereum/go-ethereum/logger" + "github.com/ethereum/go-ethereum/logger/glog" + "golang.org/x/net/websocket" + "gopkg.in/fatih/set.v0" +) + +var ( + wsServerMu sync.Mutex + wsRPCServer *Server + wsListener net.Listener +) + +// wsReaderWriterCloser reads and write payloads from and to a websocket connection. +type wsReaderWriterCloser struct { + c *websocket.Conn +} + +// Read will read incoming payload data into p. +func (rw *wsReaderWriterCloser) Read(p []byte) (int, error) { + return rw.c.Read(p) +} + +// Write writes p to the websocket. +func (rw *wsReaderWriterCloser) Write(p []byte) (int, error) { + return rw.c.Write(p) +} + +// Close closes the websocket connection. +func (rw *wsReaderWriterCloser) Close() error { + return rw.c.Close() +} + +// wsHandler accepts a websocket connection and handles incoming RPC requests. +// Will return when the websocket connection is closed, either by the client or +// server. +func wsHandler(conn *websocket.Conn) { + rwc := &wsReaderWriterCloser{conn} + wsRPCServer.ServeCodec(NewJSONCodec(rwc)) +} + +// wsHandshakeValidator returns a handler that verifies the origin during the +// websocket upgrade process. When a '*' is specified as an allowed origins all +// connections are accepted. +func wsHandshakeValidator(allowedOrigins []string) func(*websocket.Config, *http.Request) error { + origins := set.New() + allowAllOrigins := false + + for _, origin := range allowedOrigins { + if origin == "*" { + allowAllOrigins = true + } + if origin != "" { + origins.Add(origin) + } + } + + // allow localhost if no allowedOrigins are specified + if len(origins.List()) == 0 { + origins.Add("http://localhost") + if hostname, err := os.Hostname(); err == nil { + origins.Add("http://" + hostname) + } + } + + glog.V(logger.Debug).Infof("Allowed origin(s) for WS RPC interface %v\n", origins.List()) + + f := func(cfg *websocket.Config, req *http.Request) error { + origin := req.Header.Get("Origin") + if allowAllOrigins || origins.Has(origin) { + return nil + } + glog.V(logger.Debug).Infof("origin '%s' not allowed on WS-RPC interface\n", origin) + return fmt.Errorf("origin %s not allowed", origin) + } + + return f +} + +// StartWS will start a websocket RPC server on the given address and port. +func StartWS(address string, port int, corsdomains []string, apis []API) error { + wsServerMu.Lock() + defer wsServerMu.Unlock() + + if wsRPCServer != nil { + return fmt.Errorf("WS RPC interface already started on %s", wsListener.Addr()) + } + + rpcServer := NewServer() + for _, api := range apis { + if err := rpcServer.RegisterName(api.Namespace, api.Service); err != nil { + return err + } + } + + listener, err := net.Listen("tcp", fmt.Sprintf("%s:%d", address, port)) + if err != nil { + return err + } + + wsServer := websocket.Server{Handshake: wsHandshakeValidator(corsdomains), Handler: wsHandler} + wsHTTPServer := http.Server{Handler: wsServer} + + go wsHTTPServer.Serve(listener) + + wsListener = listener + wsRPCServer = rpcServer + + return nil +} + +// StopWS stops the running websocket RPC server. +func StopWS() error { + wsServerMu.Lock() + defer wsServerMu.Unlock() + + if wsRPCServer == nil { + return errors.New("HTTP RPC interface not started") + } + + wsListener.Close() + wsRPCServer.Stop() + + wsRPCServer = nil + wsListener = nil + + return nil +} + +// wsClient represents a RPC client that communicates over websockets with a +// RPC server. +type wsClient struct { + endpoint string + connMu sync.Mutex + conn *websocket.Conn +} + +// NewWSClientj creates a new RPC client that communicates with a RPC server +// that is listening on the given endpoint using JSON encoding. +func NewWSClient(endpoint string) (*wsClient, error) { + return &wsClient{endpoint: endpoint}, nil +} + +// connection will return a websocket connection to the RPC server. It will +// (re)connect when necessary. +func (client *wsClient) connection() (*websocket.Conn, error) { + if client.conn != nil { + return client.conn, nil + } + + origin, err := os.Hostname() + if err != nil { + return nil, err + } + + origin = "http://" + origin + client.conn, err = websocket.Dial(client.endpoint, "", origin) + + return client.conn, err +} + +// SupportedModules is the collection of modules the RPC server offers. +func (client *wsClient) SupportedModules() (map[string]string, error) { + return SupportedModules(client) +} + +// Send writes the JSON serialized msg to the websocket. It will create a new +// websocket connection to the server if the client is currently not connected. +func (client *wsClient) Send(msg interface{}) (err error) { + client.connMu.Lock() + defer client.connMu.Unlock() + + var conn *websocket.Conn + if conn, err = client.connection(); err == nil { + if err = websocket.JSON.Send(conn, msg); err != nil { + client.conn.Close() + client.conn = nil + } + } + + return err +} + +// Recv reads a JSON message from the websocket and unmarshals it into msg. +func (client *wsClient) Recv(msg interface{}) (err error) { + client.connMu.Lock() + defer client.connMu.Unlock() + + var conn *websocket.Conn + if conn, err = client.connection(); err == nil { + if err = websocket.JSON.Receive(conn, msg); err != nil { + client.conn.Close() + client.conn = nil + } + } + return +} + +// Close closes the underlaying websocket connection. +func (client *wsClient) Close() { + client.connMu.Lock() + defer client.connMu.Unlock() + + if client.conn != nil { + client.conn.Close() + client.conn = nil + } + +} diff --git a/rpc/xeth.go b/rpc/xeth.go deleted file mode 100644 index 9527a96c0..000000000 --- a/rpc/xeth.go +++ /dev/null @@ -1,77 +0,0 @@ -// Copyright 2015 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library 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 Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see . - -// Package rpc implements the Ethereum JSON-RPC API. -package rpc - -import ( - "encoding/json" - "fmt" - "reflect" - "sync/atomic" - - "github.com/ethereum/go-ethereum/rpc/comms" - "github.com/ethereum/go-ethereum/rpc/shared" -) - -// Xeth is a native API interface to a remote node. -type Xeth struct { - client comms.EthereumClient - reqId uint32 -} - -// NewXeth constructs a new native API interface to a remote node. -func NewXeth(client comms.EthereumClient) *Xeth { - return &Xeth{ - client: client, - } -} - -// Call invokes a method with the given parameters are the remote node. -func (self *Xeth) Call(method string, params []interface{}) (map[string]interface{}, error) { - // Assemble the json RPC request - data, err := json.Marshal(params) - if err != nil { - return nil, err - } - req := &shared.Request{ - Id: atomic.AddUint32(&self.reqId, 1), - Jsonrpc: "2.0", - Method: method, - Params: data, - } - // Send the request over and retrieve the response - if err := self.client.Send(req); err != nil { - return nil, err - } - res, err := self.client.Recv() - if err != nil { - return nil, err - } - // Ensure the response is valid, and extract the results - success, isSuccessResponse := res.(*shared.SuccessResponse) - failure, isFailureResponse := res.(*shared.ErrorResponse) - switch { - case isFailureResponse: - return nil, fmt.Errorf("Method invocation failed: %v", failure.Error) - - case isSuccessResponse: - return success.Result.(map[string]interface{}), nil - - default: - return nil, fmt.Errorf("Invalid response type: %v", reflect.TypeOf(res)) - } -} -- cgit v1.2.3