diff options
author | Bas van Kervel <bas@ethdev.com> | 2015-06-22 18:47:32 +0800 |
---|---|---|
committer | Bas van Kervel <bas@ethdev.com> | 2015-06-22 18:47:32 +0800 |
commit | 2e0b56a72b3eafc89938003da29c06496ac9ad4e (patch) | |
tree | eba3eb3a822da0ba7de98d9c7b6dc8601a63db0a /rpc/api | |
parent | 2737baa6577a337f33f020d587af100c9bda3585 (diff) | |
download | go-tangerine-2e0b56a72b3eafc89938003da29c06496ac9ad4e.tar.gz go-tangerine-2e0b56a72b3eafc89938003da29c06496ac9ad4e.tar.zst go-tangerine-2e0b56a72b3eafc89938003da29c06496ac9ad4e.zip |
added RPC start/stop support
Diffstat (limited to 'rpc/api')
-rw-r--r-- | rpc/api/admin.go | 60 | ||||
-rw-r--r-- | rpc/api/admin_args.go | 52 | ||||
-rw-r--r-- | rpc/api/admin_js.go | 14 | ||||
-rw-r--r-- | rpc/api/api.go | 43 | ||||
-rw-r--r-- | rpc/api/args.go | 3 | ||||
-rw-r--r-- | rpc/api/args_test.go | 1 | ||||
-rw-r--r-- | rpc/api/db.go | 2 | ||||
-rw-r--r-- | rpc/api/debug.go | 2 | ||||
-rw-r--r-- | rpc/api/eth.go | 2 | ||||
-rw-r--r-- | rpc/api/mergedapi.go | 8 | ||||
-rw-r--r-- | rpc/api/mergedapi_js.go | 1 | ||||
-rw-r--r-- | rpc/api/miner.go | 2 | ||||
-rw-r--r-- | rpc/api/net.go | 2 | ||||
-rw-r--r-- | rpc/api/personal.go | 2 | ||||
-rw-r--r-- | rpc/api/shh.go | 2 | ||||
-rw-r--r-- | rpc/api/txpool.go | 2 | ||||
-rw-r--r-- | rpc/api/utils.go | 43 | ||||
-rw-r--r-- | rpc/api/web3.go | 2 |
18 files changed, 152 insertions, 91 deletions
diff --git a/rpc/api/admin.go b/rpc/api/admin.go index 368375c93..7e79fd4d0 100644 --- a/rpc/api/admin.go +++ b/rpc/api/admin.go @@ -11,6 +11,7 @@ import ( "github.com/ethereum/go-ethereum/logger/glog" "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/xeth" ) @@ -32,6 +33,8 @@ var ( "admin_chainSyncStatus": (*adminApi).ChainSyncStatus, "admin_setSolc": (*adminApi).SetSolc, "admin_datadir": (*adminApi).DataDir, + "admin_startRPC": (*adminApi).StartRPC, + "admin_stopRPC": (*adminApi).StopRPC, } ) @@ -42,25 +45,25 @@ type adminhandler func(*adminApi, *shared.Request) (interface{}, error) type adminApi struct { xeth *xeth.XEth ethereum *eth.Ethereum - methods map[string]adminhandler - codec codec.ApiCoder + codec codec.Codec + coder codec.ApiCoder } // create a new admin api instance -func NewAdminApi(xeth *xeth.XEth, ethereum *eth.Ethereum, coder codec.Codec) *adminApi { +func NewAdminApi(xeth *xeth.XEth, ethereum *eth.Ethereum, codec codec.Codec) *adminApi { return &adminApi{ xeth: xeth, ethereum: ethereum, - methods: AdminMapping, - codec: coder.New(nil), + codec: codec, + coder: codec.New(nil), } } // collection with supported methods func (self *adminApi) Methods() []string { - methods := make([]string, len(self.methods)) + methods := make([]string, len(AdminMapping)) i := 0 - for k := range self.methods { + for k := range AdminMapping { methods[i] = k i++ } @@ -69,7 +72,7 @@ func (self *adminApi) Methods() []string { // Execute given request func (self *adminApi) Execute(req *shared.Request) (interface{}, error) { - if callback, ok := self.methods[req.Method]; ok { + if callback, ok := AdminMapping[req.Method]; ok { return callback(self, req) } @@ -77,7 +80,7 @@ func (self *adminApi) Execute(req *shared.Request) (interface{}, error) { } func (self *adminApi) Name() string { - return AdminApiName + return shared.AdminApiName } func (self *adminApi) ApiVersion() string { @@ -86,7 +89,7 @@ func (self *adminApi) ApiVersion() string { func (self *adminApi) AddPeer(req *shared.Request) (interface{}, error) { args := new(AddPeerArgs) - if err := self.codec.Decode(req.Params, &args); err != nil { + if err := self.coder.Decode(req.Params, &args); err != nil { return nil, shared.NewDecodeParamError(err.Error()) } @@ -120,7 +123,7 @@ func hasAllBlocks(chain *core.ChainManager, bs []*types.Block) bool { func (self *adminApi) ImportChain(req *shared.Request) (interface{}, error) { args := new(ImportExportChainArgs) - if err := self.codec.Decode(req.Params, &args); err != nil { + if err := self.coder.Decode(req.Params, &args); err != nil { return nil, shared.NewDecodeParamError(err.Error()) } @@ -163,7 +166,7 @@ func (self *adminApi) ImportChain(req *shared.Request) (interface{}, error) { func (self *adminApi) ExportChain(req *shared.Request) (interface{}, error) { args := new(ImportExportChainArgs) - if err := self.codec.Decode(req.Params, &args); err != nil { + if err := self.coder.Decode(req.Params, &args); err != nil { return nil, shared.NewDecodeParamError(err.Error()) } @@ -181,7 +184,7 @@ func (self *adminApi) ExportChain(req *shared.Request) (interface{}, error) { func (self *adminApi) Verbosity(req *shared.Request) (interface{}, error) { args := new(VerbosityArgs) - if err := self.codec.Decode(req.Params, &args); err != nil { + if err := self.coder.Decode(req.Params, &args); err != nil { return nil, shared.NewDecodeParamError(err.Error()) } @@ -202,7 +205,7 @@ func (self *adminApi) ChainSyncStatus(req *shared.Request) (interface{}, error) func (self *adminApi) SetSolc(req *shared.Request) (interface{}, error) { args := new(SetSolcArgs) - if err := self.codec.Decode(req.Params, &args); err != nil { + if err := self.coder.Decode(req.Params, &args); err != nil { return nil, shared.NewDecodeParamError(err.Error()) } @@ -212,3 +215,32 @@ func (self *adminApi) SetSolc(req *shared.Request) (interface{}, error) { } return solc.Info(), nil } + +func (self *adminApi) StartRPC(req *shared.Request) (interface{}, error) { + var err 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, + } + + if apis, err := ParseApiString(args.Apis, self.codec, self.xeth, self.ethereum); err == nil { + 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 +} diff --git a/rpc/api/admin_args.go b/rpc/api/admin_args.go index 56bb57e20..5437971ca 100644 --- a/rpc/api/admin_args.go +++ b/rpc/api/admin_args.go @@ -95,3 +95,55 @@ func (args *SetSolcArgs) UnmarshalJSON(b []byte) (err error) { 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 { + if addr, ok := obj[0].(string); ok { + args.ListenAddress = addr + } else { + return shared.NewInvalidTypeError("listenAddress", "not a string") + } + } + + if len(obj) >= 2 { + 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 { + if corsDomain, ok := obj[2].(string); ok { + args.CorsDomain = corsDomain + } else { + return shared.NewInvalidTypeError("corsDomain", "not a string") + } + } + + if len(obj) >= 4 { + if apis, ok := obj[3].(string); ok { + args.Apis = apis + } else { + return shared.NewInvalidTypeError("apis", "not a string") + } + } + + return nil +} diff --git a/rpc/api/admin_js.go b/rpc/api/admin_js.go index c3e713c67..97642ade7 100644 --- a/rpc/api/admin_js.go +++ b/rpc/api/admin_js.go @@ -39,6 +39,20 @@ web3._extend({ params: 1, inputFormatter: [web3._extend.utils.formatInputString], outputFormatter: web3._extend.formatters.formatOutputString + }), + new web3._extend.Method({ + name: 'startRPC', + call: 'admin_startRPC', + params: 4, + inputFormatter: [web3._extend.utils.formatInputString,web3._extend.utils.formatInputInteger,web3._extend.utils.formatInputString,web3._extend.utils.formatInputString], + outputFormatter: web3._extend.formatters.formatOutputBool + }), + new web3._extend.Method({ + name: 'stopRPC', + call: 'admin_stopRPC', + params: 0, + inputFormatter: [], + outputFormatter: web3._extend.formatters.formatOutputBool }) ], properties: diff --git a/rpc/api/api.go b/rpc/api/api.go index 10b448594..ca1ccb9a5 100644 --- a/rpc/api/api.go +++ b/rpc/api/api.go @@ -1,51 +1,10 @@ package api import ( - "strings" - "github.com/ethereum/go-ethereum/rpc/shared" ) -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, - }, ",") -) - -// 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(*shared.Request) (interface{}, error) - - // List of supported RCP methods this API provides - Methods() []string -} - // Merge multiple API's to a single API instance -func Merge(apis ...EthereumApi) EthereumApi { +func Merge(apis ...shared.EthereumApi) shared.EthereumApi { return newMergedApi(apis...) } diff --git a/rpc/api/args.go b/rpc/api/args.go index 4ef1b26e7..fc85448e6 100644 --- a/rpc/api/args.go +++ b/rpc/api/args.go @@ -2,6 +2,7 @@ package api import ( "encoding/json" + "github.com/ethereum/go-ethereum/rpc/shared" ) @@ -54,4 +55,4 @@ func (args *FilterStringArgs) UnmarshalJSON(b []byte) (err error) { } args.Word = argstr return nil -}
\ No newline at end of file +} diff --git a/rpc/api/args_test.go b/rpc/api/args_test.go index 4153d5432..a30f247bc 100644 --- a/rpc/api/args_test.go +++ b/rpc/api/args_test.go @@ -6,6 +6,7 @@ import ( "fmt" "math/big" "testing" + "github.com/ethereum/go-ethereum/rpc/shared" ) diff --git a/rpc/api/db.go b/rpc/api/db.go index 745269168..6f10d6447 100644 --- a/rpc/api/db.go +++ b/rpc/api/db.go @@ -63,7 +63,7 @@ func (self *dbApi) Execute(req *shared.Request) (interface{}, error) { } func (self *dbApi) Name() string { - return DbApiName + return shared.DbApiName } func (self *dbApi) ApiVersion() string { diff --git a/rpc/api/debug.go b/rpc/api/debug.go index 5b6a449dc..b451d8662 100644 --- a/rpc/api/debug.go +++ b/rpc/api/debug.go @@ -71,7 +71,7 @@ func (self *debugApi) Execute(req *shared.Request) (interface{}, error) { } func (self *debugApi) Name() string { - return DebugApiName + return shared.DebugApiName } func (self *debugApi) ApiVersion() string { diff --git a/rpc/api/eth.go b/rpc/api/eth.go index cafa05e34..0bb8d449b 100644 --- a/rpc/api/eth.go +++ b/rpc/api/eth.go @@ -100,7 +100,7 @@ func (self *ethApi) Execute(req *shared.Request) (interface{}, error) { } func (self *ethApi) Name() string { - return EthApiName + return shared.EthApiName } func (self *ethApi) ApiVersion() string { diff --git a/rpc/api/mergedapi.go b/rpc/api/mergedapi.go index 8cf4ae96e..bc4fa32e8 100644 --- a/rpc/api/mergedapi.go +++ b/rpc/api/mergedapi.go @@ -13,14 +13,14 @@ const ( // combines multiple API's type MergedApi struct { apis map[string]string - methods map[string]EthereumApi + methods map[string]shared.EthereumApi } // create new merged api instance -func newMergedApi(apis ...EthereumApi) *MergedApi { +func newMergedApi(apis ...shared.EthereumApi) *MergedApi { mergedApi := new(MergedApi) mergedApi.apis = make(map[string]string, len(apis)) - mergedApi.methods = make(map[string]EthereumApi) + mergedApi.methods = make(map[string]shared.EthereumApi) for _, api := range apis { mergedApi.apis[api.Name()] = api.ApiVersion() @@ -54,7 +54,7 @@ func (self *MergedApi) Execute(req *shared.Request) (interface{}, error) { } func (self *MergedApi) Name() string { - return MergedApiName + return shared.MergedApiName } func (self *MergedApi) ApiVersion() string { diff --git a/rpc/api/mergedapi_js.go b/rpc/api/mergedapi_js.go deleted file mode 100644 index 778f64ec1..000000000 --- a/rpc/api/mergedapi_js.go +++ /dev/null @@ -1 +0,0 @@ -package api diff --git a/rpc/api/miner.go b/rpc/api/miner.go index 0b5e74f52..7a84cb9ae 100644 --- a/rpc/api/miner.go +++ b/rpc/api/miner.go @@ -66,7 +66,7 @@ func (self *minerApi) Methods() []string { } func (self *minerApi) Name() string { - return MinerApiName + return shared.MinerApiName } func (self *minerApi) ApiVersion() string { diff --git a/rpc/api/net.go b/rpc/api/net.go index 247c23827..761654661 100644 --- a/rpc/api/net.go +++ b/rpc/api/net.go @@ -63,7 +63,7 @@ func (self *netApi) Execute(req *shared.Request) (interface{}, error) { } func (self *netApi) Name() string { - return NetApiName + return shared.NetApiName } func (self *netApi) ApiVersion() string { diff --git a/rpc/api/personal.go b/rpc/api/personal.go index 7a6c91c82..b4a63ea7a 100644 --- a/rpc/api/personal.go +++ b/rpc/api/personal.go @@ -66,7 +66,7 @@ func (self *personalApi) Execute(req *shared.Request) (interface{}, error) { } func (self *personalApi) Name() string { - return PersonalApiName + return shared.PersonalApiName } func (self *personalApi) ApiVersion() string { diff --git a/rpc/api/shh.go b/rpc/api/shh.go index e83a7b22e..18a8fd15d 100644 --- a/rpc/api/shh.go +++ b/rpc/api/shh.go @@ -72,7 +72,7 @@ func (self *shhApi) Execute(req *shared.Request) (interface{}, error) { } func (self *shhApi) Name() string { - return ShhApiName + return shared.ShhApiName } func (self *shhApi) ApiVersion() string { diff --git a/rpc/api/txpool.go b/rpc/api/txpool.go index 64550bdaf..25ad6e9b2 100644 --- a/rpc/api/txpool.go +++ b/rpc/api/txpool.go @@ -60,7 +60,7 @@ func (self *txPoolApi) Execute(req *shared.Request) (interface{}, error) { } func (self *txPoolApi) Name() string { - return TxPoolApiName + return shared.TxPoolApiName } func (self *txPoolApi) ApiVersion() string { diff --git a/rpc/api/utils.go b/rpc/api/utils.go index a4ce6409a..9995e1f7c 100644 --- a/rpc/api/utils.go +++ b/rpc/api/utils.go @@ -7,6 +7,7 @@ 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" ) @@ -23,6 +24,8 @@ var ( "chainSyncStatus", "setSolc", "datadir", + "startRPC", + "stopRPC", }, "db": []string{ "getString", @@ -129,35 +132,35 @@ var ( ) // Parse a comma separated API string to individual api's -func ParseApiString(apistr string, codec codec.Codec, xeth *xeth.XEth, eth *eth.Ethereum) ([]EthereumApi, error) { +func ParseApiString(apistr string, codec codec.Codec, xeth *xeth.XEth, eth *eth.Ethereum) ([]shared.EthereumApi, error) { if len(strings.TrimSpace(apistr)) == 0 { return nil, fmt.Errorf("Empty apistr provided") } names := strings.Split(apistr, ",") - apis := make([]EthereumApi, len(names)) + apis := make([]shared.EthereumApi, len(names)) for i, name := range names { switch strings.ToLower(strings.TrimSpace(name)) { - case AdminApiName: + case shared.AdminApiName: apis[i] = NewAdminApi(xeth, eth, codec) - case DebugApiName: + case shared.DebugApiName: apis[i] = NewDebugApi(xeth, eth, codec) - case DbApiName: + case shared.DbApiName: apis[i] = NewDbApi(xeth, eth, codec) - case EthApiName: + case shared.EthApiName: apis[i] = NewEthApi(xeth, codec) - case MinerApiName: + case shared.MinerApiName: apis[i] = NewMinerApi(eth, codec) - case NetApiName: + case shared.NetApiName: apis[i] = NewNetApi(xeth, eth, codec) - case ShhApiName: + case shared.ShhApiName: apis[i] = NewShhApi(xeth, eth, codec) - case TxPoolApiName: + case shared.TxPoolApiName: apis[i] = NewTxPoolApi(xeth, eth, codec) - case PersonalApiName: + case shared.PersonalApiName: apis[i] = NewPersonalApi(xeth, eth, codec) - case Web3ApiName: + case shared.Web3ApiName: apis[i] = NewWeb3Api(xeth, codec) default: return nil, fmt.Errorf("Unknown API '%s'", name) @@ -169,21 +172,21 @@ func ParseApiString(apistr string, codec codec.Codec, xeth *xeth.XEth, eth *eth. func Javascript(name string) string { switch strings.ToLower(strings.TrimSpace(name)) { - case AdminApiName: + case shared.AdminApiName: return Admin_JS - case DebugApiName: + case shared.DebugApiName: return Debug_JS - case DbApiName: + case shared.DbApiName: return Db_JS - case MinerApiName: + case shared.MinerApiName: return Miner_JS - case NetApiName: + case shared.NetApiName: return Net_JS - case ShhApiName: + case shared.ShhApiName: return Shh_JS - case TxPoolApiName: + case shared.TxPoolApiName: return TxPool_JS - case PersonalApiName: + case shared.PersonalApiName: return Personal_JS } diff --git a/rpc/api/web3.go b/rpc/api/web3.go index ed5008446..4c20baa25 100644 --- a/rpc/api/web3.go +++ b/rpc/api/web3.go @@ -60,7 +60,7 @@ func (self *web3Api) Execute(req *shared.Request) (interface{}, error) { } func (self *web3Api) Name() string { - return Web3ApiName + return shared.Web3ApiName } func (self *web3Api) ApiVersion() string { |