diff options
author | Bas van Kervel <bas@ethdev.com> | 2015-12-16 17:58:01 +0800 |
---|---|---|
committer | Jeffrey Wilcke <geffobscura@gmail.com> | 2016-01-26 20:51:50 +0800 |
commit | 19b2640e89465c1c57f1bbea0274d52d97151f60 (patch) | |
tree | 980e063693dae7fa6105646821ee6755b176b6e2 /node/api.go | |
parent | f2ab351e8d3b0a4e569ce56f6a4f17725ca5ba65 (diff) | |
download | dexon-19b2640e89465c1c57f1bbea0274d52d97151f60.tar dexon-19b2640e89465c1c57f1bbea0274d52d97151f60.tar.gz dexon-19b2640e89465c1c57f1bbea0274d52d97151f60.tar.bz2 dexon-19b2640e89465c1c57f1bbea0274d52d97151f60.tar.lz dexon-19b2640e89465c1c57f1bbea0274d52d97151f60.tar.xz dexon-19b2640e89465c1c57f1bbea0274d52d97151f60.tar.zst dexon-19b2640e89465c1c57f1bbea0274d52d97151f60.zip |
rpc: migrated the RPC insterface to a new reflection based RPC layer
Diffstat (limited to 'node/api.go')
-rw-r--r-- | node/api.go | 113 |
1 files changed, 97 insertions, 16 deletions
diff --git a/node/api.go b/node/api.go index 909329e57..7b247dc51 100644 --- a/node/api.go +++ b/node/api.go @@ -21,11 +21,15 @@ import ( "strings" "time" + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/logger/glog" "github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/p2p/discover" - "github.com/ethereum/go-ethereum/rpc/comms" + "github.com/ethereum/go-ethereum/rpc" "github.com/rcrowley/go-metrics" + + "gopkg.in/fatih/set.v0" ) // PrivateAdminAPI is the collection of administrative API methods exposed only @@ -59,27 +63,83 @@ func (api *PrivateAdminAPI) AddPeer(url string) (bool, error) { // StartRPC starts the HTTP RPC API server. func (api *PrivateAdminAPI) StartRPC(address string, port int, cors string, apis string) (bool, error) { - /*// Parse the list of API modules to make available - apis, err := api.ParseApiString(apis, codec.JSON, xeth.New(api.node, nil), api.node) - if err != nil { - return false, err + var offeredAPIs []rpc.API + if len(apis) > 0 { + namespaces := set.New() + for _, a := range strings.Split(apis, ",") { + namespaces.Add(strings.TrimSpace(a)) + } + for _, api := range api.node.APIs() { + if namespaces.Has(api.Namespace) { + offeredAPIs = append(offeredAPIs, api) + } + } + } else { // use by default all public API's + for _, api := range api.node.APIs() { + if api.Public { + offeredAPIs = append(offeredAPIs, api) + } + } } - // Configure and start the HTTP RPC server - config := comms.HttpConfig{ - ListenAddress: address, - ListenPort: port, - CorsDomain: cors, + + if address == "" { + address = "127.0.0.1" } - if err := comms.StartHttp(config, self.codec, api.Merge(apis...)); err != nil { - return false, err + if port == 0 { + port = 8545 } - return true, nil*/ - return false, fmt.Errorf("needs new RPC implementation to resolve circular dependency") + + corsDomains := strings.Split(cors, " ") + err := rpc.StartHTTP(address, port, corsDomains, offeredAPIs) + return err == nil, err } // StopRPC terminates an already running HTTP RPC API endpoint. -func (api *PrivateAdminAPI) StopRPC() { - comms.StopHttp() +func (api *PrivateAdminAPI) StopRPC() (bool, error) { + err := rpc.StopHTTP() + return err == nil, err +} + + +// StartWS starts the websocket RPC API server. +func (api *PrivateAdminAPI) StartWS(address string, port int, cors string, apis string) (bool, error) { + var offeredAPIs []rpc.API + if len(apis) > 0 { + namespaces := set.New() + for _, a := range strings.Split(apis, ",") { + namespaces.Add(strings.TrimSpace(a)) + } + for _, api := range api.node.APIs() { + if namespaces.Has(api.Namespace) { + offeredAPIs = append(offeredAPIs, api) + } + } + } else { + // use by default all public API's + for _, api := range api.node.APIs() { + if api.Public { + offeredAPIs = append(offeredAPIs, api) + } + } + } + + if address == "" { + address = "127.0.0.1" + } + if port == 0 { + port = 8546 + } + + corsDomains := strings.Split(cors, " ") + + err := rpc.StartWS(address, port, corsDomains, offeredAPIs) + return err == nil, err +} + +// StopRPC terminates an already running websocket RPC API endpoint. +func (api *PrivateAdminAPI) StopWS() (bool, error) { + err := rpc.StopWS() + return err == nil, err } // PublicAdminAPI is the collection of administrative API methods exposed over @@ -247,3 +307,24 @@ func (api *PublicDebugAPI) Metrics(raw bool) (map[string]interface{}, error) { }) return counters, nil } + +// PublicWeb3API offers helper utils +type PublicWeb3API struct { + stack *Node +} + +// NewPublicWeb3API creates a new Web3Service instance +func NewPublicWeb3API(stack *Node) *PublicWeb3API { + return &PublicWeb3API{stack} +} + +// ClientVersion returns the node name +func (s *PublicWeb3API) ClientVersion() string { + return s.stack.Server().Name +} + +// Sha3 applies the ethereum sha3 implementation on the input. +// It assumes the input is hex encoded. +func (s *PublicWeb3API) Sha3(input string) string { + return common.ToHex(crypto.Sha3(common.FromHex(input))) +} |