diff --git a/.gitignore b/.gitignore index 2d534cd4477843f9b0cb9d3cb7afc070cc20d6eb..d2482dea1fddf517c65e3d27012dfaffb6fcb809 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,5 @@ vendor # Temporary / cached *.swp +debug +.idea diff --git a/core/pipes/net.go b/core/pipes/net.go new file mode 100644 index 0000000000000000000000000000000000000000..39412c07e7a9e16b2d690e2fd4f42f7ba7a04659 --- /dev/null +++ b/core/pipes/net.go @@ -0,0 +1,99 @@ +// Copyright 2015, 2016 Eris Industries (UK) Ltd. +// This file is part of Eris-RT + +// Eris-RT is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Eris-RT 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 General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Eris-RT. If not, see <http://www.gnu.org/licenses/>. + +// Net is part of the pipe for ErisMint and provides the implementation +// for the pipe to call into the ErisMint application +package pipes + +import ( + core_types "github.com/eris-ltd/eris-db/core/types" + "github.com/eris-ltd/eris-db/definitions" +) + +//----------------------------------------------------------------------------- + +// Get the complete pipe info. +func NetInfo(pipe definitions.Pipe) (*core_types.NetworkInfo, error) { + thisNode := pipe.GetConsensusEngine().NodeInfo() + listeners := []string{} + for _, listener := range pipe.GetConsensusEngine().Listeners() { + listeners = append(listeners, listener.String()) + } + peers := make([]*core_types.Peer, 0) + for _, peer := range pipe.GetConsensusEngine().Peers() { + p := &core_types.Peer{ + NodeInfo: &peer.NodeInfo, + IsOutbound: peer.IsOutbound, + } + peers = append(peers, p) + } + return &core_types.NetworkInfo{ + ClientVersion: thisNode.Version, + Moniker: thisNode.Moniker, + Listening: pipe.GetConsensusEngine().IsListening(), + Listeners: listeners, + Peers: peers, + }, nil +} + +// Get the client version +func ClientVersion(pipe definitions.Pipe) (string, error) { + return pipe.GetConsensusEngine().NodeInfo().Version, nil +} + +// Get the moniker +func Moniker(pipe definitions.Pipe) (string, error) { + return pipe.GetConsensusEngine().NodeInfo().Moniker, nil +} + +// Is the network currently listening for connections. +func Listening(pipe definitions.Pipe) (bool, error) { + return pipe.GetConsensusEngine().IsListening(), nil +} + +// Is the network currently listening for connections. +func Listeners(pipe definitions.Pipe) ([]string, error) { + listeners := []string{} + for _, listener := range pipe.GetConsensusEngine().Listeners() { + listeners = append(listeners, listener.String()) + } + return listeners, nil +} + +// Get a list of all peers. +func Peers(pipe definitions.Pipe) ([]*core_types.Peer, error) { + peers := make([]*core_types.Peer, 0) + for _, peer := range pipe.GetConsensusEngine().Peers() { + p := &core_types.Peer{ + NodeInfo: &peer.NodeInfo, + IsOutbound: peer.IsOutbound, + } + peers = append(peers, p) + } + return peers, nil +} + +func Peer(pipe definitions.Pipe, address string) (*core_types.Peer, error) { + for _, peer := range pipe.GetConsensusEngine().Peers() { + if peer.RemoteAddr == address { + return &core_types.Peer{ + NodeInfo: &peer.NodeInfo, + IsOutbound: peer.IsOutbound, + }, nil + } + } + return nil, nil +} \ No newline at end of file diff --git a/core/types/types.go b/core/types/types.go index 274900ee723b91a8d6c06fec7a3a58294c3fc183..8e49c3e282106ec857c6cf4e001a2003b54a8af4 100644 --- a/core/types/types.go +++ b/core/types/types.go @@ -132,7 +132,7 @@ type ( // used in Peers and BlockchainInfo Peer struct { - nodeInfo *p2p.NodeInfo `json:"node_info"` + NodeInfo *p2p.NodeInfo `json:"node_info"` IsOutbound bool `json:"is_outbound"` } diff --git a/definitions/pipe.go b/definitions/pipe.go index 285f8b53fccac575cd034b8742d8fcb83c9a18c2..ffbec38dac807769ef9719759ca142976ed29e48 100644 --- a/definitions/pipe.go +++ b/definitions/pipe.go @@ -40,7 +40,6 @@ type Pipe interface { Blockchain() blockchain_types.Blockchain Events() event.EventEmitter NameReg() NameReg - Net() Net Transactor() Transactor // Hash of Genesis state GenesisHash() []byte @@ -68,16 +67,6 @@ type NameReg interface { Entries([]*event.FilterData) (*types.ResultListNames, error) } -type Net interface { - Info() (*types.NetworkInfo, error) - ClientVersion() (string, error) - Moniker() (string, error) - Listening() (bool, error) - Listeners() ([]string, error) - Peers() ([]*types.Peer, error) - Peer(string) (*types.Peer, error) -} - type Transactor interface { Call(fromAddress, toAddress, data []byte) (*types.Call, error) CallCode(fromAddress, code, data []byte) (*types.Call, error) diff --git a/manager/eris-mint/net.go b/manager/eris-mint/net.go deleted file mode 100644 index 66bd60c3f7a0f35fb2ad40c8056658986801f377..0000000000000000000000000000000000000000 --- a/manager/eris-mint/net.go +++ /dev/null @@ -1,77 +0,0 @@ -// Copyright 2015, 2016 Eris Industries (UK) Ltd. -// This file is part of Eris-RT - -// Eris-RT is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. - -// Eris-RT 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 General Public License for more details. - -// You should have received a copy of the GNU General Public License -// along with Eris-RT. If not, see <http://www.gnu.org/licenses/>. - -// Net is part of the pipe for ErisMint and provides the implementation -// for the pipe to call into the ErisMint application -package erismint - -import ( - core_types "github.com/eris-ltd/eris-db/core/types" -) - -// TODO-RPC! - -// The network structure -type network struct { -} - -func newNetwork() *network { - return &network{} -} - -//------------------------------------------------------------------------------ -// Tendermint Pipe implementation - -//----------------------------------------------------------------------------- -// Eris-DB v0 Pipe implementation - -// Get the complete net info. -func (this *network) Info() (*core_types.NetworkInfo, error) { - return &core_types.NetworkInfo{}, nil -} - -// Get the client version -func (this *network) ClientVersion() (string, error) { - return "not-fully-loaded-yet", nil -} - -// Get the moniker -func (this *network) Moniker() (string, error) { - return "rekinom", nil -} - -// Is the network currently listening for connections. -func (this *network) Listening() (bool, error) { - return false, nil -} - -// Is the network currently listening for connections. -func (this *network) Listeners() ([]string, error) { - return []string{}, nil -} - -// Get a list of all peers. -func (this *network) Peers() ([]*core_types.Peer, error) { - return []*core_types.Peer{}, nil -} - -// Get a peer. TODO Need to do something about the address. -func (this *network) Peer(address string) (*core_types.Peer, error) { - return &core_types.Peer{}, nil -} - -//------------------------------------------------------------------------------ -// diff --git a/manager/eris-mint/pipe.go b/manager/eris-mint/pipe.go index fe07891316f8961262c01d48c9fad34aa4798017..5370e0778d4074043518aa7a818e094594b40d2b 100644 --- a/manager/eris-mint/pipe.go +++ b/manager/eris-mint/pipe.go @@ -55,7 +55,6 @@ type erisMintPipe struct { consensusEngine consensus_types.ConsensusEngine events edb_event.EventEmitter namereg *namereg - network *network transactor *transactor // Genesis cache genesisDoc *state_types.GenesisDoc @@ -111,8 +110,6 @@ func NewErisMintPipe(moduleConfig *config.ModuleConfig, // genesis cache genesisDoc: genesisDoc, genesisState: nil, - // TODO: What network-level information do we need? - network: newNetwork(), // consensus and blockchain should both be loaded into the pipe by a higher // authority - this is a sort of dependency injection pattern consensusEngine: nil, @@ -189,10 +186,6 @@ func (pipe *erisMintPipe) NameReg() definitions.NameReg { return pipe.namereg } -func (pipe *erisMintPipe) Net() definitions.Net { - return pipe.network -} - func (pipe *erisMintPipe) Transactor() definitions.Transactor { return pipe.transactor } diff --git a/rpc/v0/methods.go b/rpc/v0/methods.go index a7bcf609ad334f1a6e160e752d1da35a3b28f247..c15b3a65d04aee50406cd327247000908deb9415 100644 --- a/rpc/v0/methods.go +++ b/rpc/v0/methods.go @@ -266,7 +266,7 @@ func (erisDbMethods *ErisDbMethods) Validators(request *rpc.RPCRequest, requeste // *************************************** Net ************************************ func (erisDbMethods *ErisDbMethods) NetworkInfo(request *rpc.RPCRequest, requester interface{}) (interface{}, int, error) { - info, errC := erisDbMethods.pipe.Net().Info() + info, errC := pipes.NetInfo(erisDbMethods.pipe) if errC != nil { return nil, rpc.INTERNAL_ERROR, errC } @@ -274,7 +274,7 @@ func (erisDbMethods *ErisDbMethods) NetworkInfo(request *rpc.RPCRequest, request } func (erisDbMethods *ErisDbMethods) ClientVersion(request *rpc.RPCRequest, requester interface{}) (interface{}, int, error) { - version, errC := erisDbMethods.pipe.Net().ClientVersion() + version, errC := pipes.ClientVersion(erisDbMethods.pipe) if errC != nil { return nil, rpc.INTERNAL_ERROR, errC } @@ -282,7 +282,7 @@ func (erisDbMethods *ErisDbMethods) ClientVersion(request *rpc.RPCRequest, reque } func (erisDbMethods *ErisDbMethods) Moniker(request *rpc.RPCRequest, requester interface{}) (interface{}, int, error) { - moniker, errC := erisDbMethods.pipe.Net().Moniker() + moniker, errC := pipes.Moniker(erisDbMethods.pipe) if errC != nil { return nil, rpc.INTERNAL_ERROR, errC } @@ -290,7 +290,7 @@ func (erisDbMethods *ErisDbMethods) Moniker(request *rpc.RPCRequest, requester i } func (erisDbMethods *ErisDbMethods) Listening(request *rpc.RPCRequest, requester interface{}) (interface{}, int, error) { - listening, errC := erisDbMethods.pipe.Net().Listening() + listening, errC := pipes.Listening(erisDbMethods.pipe) if errC != nil { return nil, rpc.INTERNAL_ERROR, errC } @@ -298,7 +298,7 @@ func (erisDbMethods *ErisDbMethods) Listening(request *rpc.RPCRequest, requester } func (erisDbMethods *ErisDbMethods) Listeners(request *rpc.RPCRequest, requester interface{}) (interface{}, int, error) { - listeners, errC := erisDbMethods.pipe.Net().Listeners() + listeners, errC := pipes.Listeners(erisDbMethods.pipe) if errC != nil { return nil, rpc.INTERNAL_ERROR, errC } @@ -306,7 +306,7 @@ func (erisDbMethods *ErisDbMethods) Listeners(request *rpc.RPCRequest, requester } func (erisDbMethods *ErisDbMethods) Peers(request *rpc.RPCRequest, requester interface{}) (interface{}, int, error) { - peers, errC := erisDbMethods.pipe.Net().Peers() + peers, errC := pipes.Peers(erisDbMethods.pipe) if errC != nil { return nil, rpc.INTERNAL_ERROR, errC } @@ -320,7 +320,7 @@ func (erisDbMethods *ErisDbMethods) Peer(request *rpc.RPCRequest, requester inte return nil, rpc.INVALID_PARAMS, err } address := param.Address - peer, errC := erisDbMethods.pipe.Net().Peer(address) + peer, errC := pipes.Peer(erisDbMethods.pipe, address) if errC != nil { return nil, rpc.INTERNAL_ERROR, errC } diff --git a/rpc/v0/restServer.go b/rpc/v0/restServer.go index 2c94bdfe708e1b30cd67be181c07879d5b57d574..aae27d4effd70b9abcf60373d087d158e83a5832 100644 --- a/rpc/v0/restServer.go +++ b/rpc/v0/restServer.go @@ -297,7 +297,7 @@ func (restServer *RestServer) handleNameRegEntry(c *gin.Context) { // ********************************* Network ********************************* func (restServer *RestServer) handleNetworkInfo(c *gin.Context) { - nInfo, err := restServer.pipe.Net().Info() + nInfo, err := pipes.NetInfo(restServer.pipe) if err != nil { c.AbortWithError(500, err) } @@ -306,7 +306,7 @@ func (restServer *RestServer) handleNetworkInfo(c *gin.Context) { } func (restServer *RestServer) handleClientVersion(c *gin.Context) { - version, err := restServer.pipe.Net().ClientVersion() + version, err := pipes.ClientVersion(restServer.pipe) if err != nil { c.AbortWithError(500, err) } @@ -315,7 +315,7 @@ func (restServer *RestServer) handleClientVersion(c *gin.Context) { } func (restServer *RestServer) handleMoniker(c *gin.Context) { - moniker, err := restServer.pipe.Net().Moniker() + moniker, err := pipes.Moniker(restServer.pipe) if err != nil { c.AbortWithError(500, err) } @@ -324,7 +324,7 @@ func (restServer *RestServer) handleMoniker(c *gin.Context) { } func (restServer *RestServer) handleListening(c *gin.Context) { - listening, err := restServer.pipe.Net().Listening() + listening, err := pipes.Listening(restServer.pipe) if err != nil { c.AbortWithError(500, err) } @@ -333,7 +333,7 @@ func (restServer *RestServer) handleListening(c *gin.Context) { } func (restServer *RestServer) handleListeners(c *gin.Context) { - listeners, err := restServer.pipe.Net().Listeners() + listeners, err := pipes.Listeners(restServer.pipe) if err != nil { c.AbortWithError(500, err) } @@ -342,7 +342,7 @@ func (restServer *RestServer) handleListeners(c *gin.Context) { } func (restServer *RestServer) handlePeers(c *gin.Context) { - peers, err := restServer.pipe.Net().Peers() + peers, err := pipes.Peers(restServer.pipe) if err != nil { c.AbortWithError(500, err) } @@ -352,7 +352,7 @@ func (restServer *RestServer) handlePeers(c *gin.Context) { func (restServer *RestServer) handlePeer(c *gin.Context) { address := c.MustGet("address").(string) - peer, err := restServer.pipe.Net().Peer(address) + peer, err := pipes.Peer(restServer.pipe, address) if err != nil { c.AbortWithError(500, err) }