diff --git a/consensus/tendermint/tendermint.go b/consensus/tendermint/tendermint.go index 65a58d48603e00ab83bdb63237c64d17c0715e17..507e63518269e1c2d6e079b7239ac00ac6090579 100644 --- a/consensus/tendermint/tendermint.go +++ b/consensus/tendermint/tendermint.go @@ -180,11 +180,12 @@ func (tendermint *Tendermint) Listeners() []p2p.Listener { return copyListeners } -func (tendermint *Tendermint) Peers() []consensus_types.Peer { - peers := []consensus_types.Peer{} - for _, peer := range tendermint.tmintNode.Switch().Peers().List() { - peers = append(peers, consensus_types.Peer{ - NodeInfo: *peer.NodeInfo, +func (tendermint *Tendermint) Peers() []*consensus_types.Peer { + p2pPeers := tendermint.tmintNode.Switch().Peers().List() + peers := make([]*consensus_types.Peer, 0) + for _, peer := range p2pPeers { + peers = append(peers, &consensus_types.Peer{ + NodeInfo: peer.NodeInfo, IsOutbound: peer.IsOutbound(), }) } diff --git a/consensus/types/consensus_engine.go b/consensus/types/consensus_engine.go index 8600bb3e827091cb22f99be2956449fad0f76783..ea02d3b2ec36459765f755cfd2f11930a622b38c 100644 --- a/consensus/types/consensus_engine.go +++ b/consensus/types/consensus_engine.go @@ -13,7 +13,7 @@ type ConsensusEngine interface { IsListening() bool Listeners() []p2p.Listener NodeInfo() *p2p.NodeInfo - Peers() []Peer + Peers() []*Peer // Private Validator PublicValidatorKey() crypto.PubKey diff --git a/consensus/types/peer.go b/consensus/types/peer.go index 99f8ee8ec8954abfde5d76850a0beffe4f5a7519..81b59ee640ad678eea203522c586abf72ce3ce95 100644 --- a/consensus/types/peer.go +++ b/consensus/types/peer.go @@ -3,6 +3,6 @@ package types import "github.com/tendermint/go-p2p" type Peer struct { - p2p.NodeInfo `json:"node_info"` + NodeInfo *p2p.NodeInfo `json:"node_info"` IsOutbound bool `json:"is_outbound"` } diff --git a/core/types/types.go b/core/types/types.go index 8e49c3e282106ec857c6cf4e001a2003b54a8af4..ecb6fc95293cf98d6125ccf0f4450a33fdc0b0e3 100644 --- a/core/types/types.go +++ b/core/types/types.go @@ -20,7 +20,7 @@ package types import ( - "github.com/tendermint/go-p2p" // NodeInfo (drop this!) + // NodeInfo (drop this!) "github.com/tendermint/tendermint/types" account "github.com/eris-ltd/eris-db/account" @@ -105,15 +105,6 @@ type ( // *********************************** Network *********************************** - // NetworkInfo - NetworkInfo struct { - ClientVersion string `json:"client_version"` - Moniker string `json:"moniker"` - Listening bool `json:"listening"` - Listeners []string `json:"listeners"` - Peers []*Peer `json:"peers"` - } - ClientVersion struct { ClientVersion string `json:"client_version"` } @@ -130,12 +121,6 @@ type ( Listeners []string `json:"listeners"` } - // used in Peers and BlockchainInfo - Peer struct { - NodeInfo *p2p.NodeInfo `json:"node_info"` - IsOutbound bool `json:"is_outbound"` - } - // *********************************** Transactions *********************************** // Call or CallCode diff --git a/rpc/tendermint/core/types/responses.go b/rpc/tendermint/core/types/responses.go index a9cf010e939fcc207b1bfd8d2f5d1ea84162ab53..69a4dd458a0f62c9713e6a9535b5d671d80b2813 100644 --- a/rpc/tendermint/core/types/responses.go +++ b/rpc/tendermint/core/types/responses.go @@ -70,9 +70,9 @@ type ResultUnsubscribe struct { } type ResultNetInfo struct { - Listening bool `json:"listening"` - Listeners []string `json:"listeners"` - Peers []consensus_types.Peer `json:"peers"` + Listening bool `json:"listening"` + Listeners []string `json:"listeners"` + Peers []*consensus_types.Peer `json:"peers"` } type ResultListValidators struct { @@ -83,7 +83,7 @@ type ResultListValidators struct { type ResultDumpConsensusState struct { ConsensusState *consensus_types.ConsensusState `json:"consensus_state"` - PeerConsensusStates []*ResultPeerConsensusState `json:"peer_consensus_states"` + PeerConsensusStates []*ResultPeerConsensusState `json:"peer_consensus_states"` } type ResultPeerConsensusState struct { diff --git a/rpc/v0/methods.go b/rpc/v0/methods.go index c15b3a65d04aee50406cd327247000908deb9415..e7a65045c620e31a7c2242ddbb790cece65bc549 100644 --- a/rpc/v0/methods.go +++ b/rpc/v0/methods.go @@ -2,11 +2,11 @@ package rpc_v0 import ( "github.com/eris-ltd/eris-db/blockchain" - pipes "github.com/eris-ltd/eris-db/core/pipes" core_types "github.com/eris-ltd/eris-db/core/types" definitions "github.com/eris-ltd/eris-db/definitions" "github.com/eris-ltd/eris-db/event" - rpc "github.com/eris-ltd/eris-db/rpc" + "github.com/eris-ltd/eris-db/rpc" + "github.com/eris-ltd/eris-db/rpc/v0/shared" "github.com/eris-ltd/eris-db/txs" ) @@ -205,7 +205,7 @@ func (erisDbMethods *ErisDbMethods) AccountStorageAt(request *rpc.RPCRequest, re // *************************************** Blockchain ************************************ func (erisDbMethods *ErisDbMethods) BlockchainInfo(request *rpc.RPCRequest, requester interface{}) (interface{}, int, error) { - return pipes.BlockchainInfo(erisDbMethods.pipe), 0, nil + return shared.BlockchainInfo(erisDbMethods.pipe), 0, nil } func (erisDbMethods *ErisDbMethods) ChainId(request *rpc.RPCRequest, requester interface{}) (interface{}, int, error) { @@ -266,50 +266,32 @@ func (erisDbMethods *ErisDbMethods) Validators(request *rpc.RPCRequest, requeste // *************************************** Net ************************************ func (erisDbMethods *ErisDbMethods) NetworkInfo(request *rpc.RPCRequest, requester interface{}) (interface{}, int, error) { - info, errC := pipes.NetInfo(erisDbMethods.pipe) - if errC != nil { - return nil, rpc.INTERNAL_ERROR, errC - } + info := shared.NetInfo(erisDbMethods.pipe) return info, 0, nil } func (erisDbMethods *ErisDbMethods) ClientVersion(request *rpc.RPCRequest, requester interface{}) (interface{}, int, error) { - version, errC := pipes.ClientVersion(erisDbMethods.pipe) - if errC != nil { - return nil, rpc.INTERNAL_ERROR, errC - } + version := shared.ClientVersion(erisDbMethods.pipe) return &core_types.ClientVersion{version}, 0, nil } func (erisDbMethods *ErisDbMethods) Moniker(request *rpc.RPCRequest, requester interface{}) (interface{}, int, error) { - moniker, errC := pipes.Moniker(erisDbMethods.pipe) - if errC != nil { - return nil, rpc.INTERNAL_ERROR, errC - } + moniker := shared.Moniker(erisDbMethods.pipe) return &core_types.Moniker{moniker}, 0, nil } func (erisDbMethods *ErisDbMethods) Listening(request *rpc.RPCRequest, requester interface{}) (interface{}, int, error) { - listening, errC := pipes.Listening(erisDbMethods.pipe) - if errC != nil { - return nil, rpc.INTERNAL_ERROR, errC - } + listening := shared.Listening(erisDbMethods.pipe) return &core_types.Listening{listening}, 0, nil } func (erisDbMethods *ErisDbMethods) Listeners(request *rpc.RPCRequest, requester interface{}) (interface{}, int, error) { - listeners, errC := pipes.Listeners(erisDbMethods.pipe) - if errC != nil { - return nil, rpc.INTERNAL_ERROR, errC - } + listeners := shared.Listeners(erisDbMethods.pipe) return &core_types.Listeners{listeners}, 0, nil } func (erisDbMethods *ErisDbMethods) Peers(request *rpc.RPCRequest, requester interface{}) (interface{}, int, error) { - peers, errC := pipes.Peers(erisDbMethods.pipe) - if errC != nil { - return nil, rpc.INTERNAL_ERROR, errC - } + peers := erisDbMethods.pipe.GetConsensusEngine().Peers() return peers, 0, nil } @@ -320,10 +302,7 @@ func (erisDbMethods *ErisDbMethods) Peer(request *rpc.RPCRequest, requester inte return nil, rpc.INVALID_PARAMS, err } address := param.Address - peer, errC := pipes.Peer(erisDbMethods.pipe, address) - if errC != nil { - return nil, rpc.INTERNAL_ERROR, errC - } + peer := shared.Peer(erisDbMethods.pipe, address) return peer, 0, nil } diff --git a/rpc/v0/restServer.go b/rpc/v0/restServer.go index aae27d4effd70b9abcf60373d087d158e83a5832..2af84e19e666efc6eb312a40271bced1cf44d5b1 100644 --- a/rpc/v0/restServer.go +++ b/rpc/v0/restServer.go @@ -9,11 +9,11 @@ import ( "github.com/gin-gonic/gin" "github.com/eris-ltd/eris-db/blockchain" - "github.com/eris-ltd/eris-db/core/pipes" core_types "github.com/eris-ltd/eris-db/core/types" definitions "github.com/eris-ltd/eris-db/definitions" event "github.com/eris-ltd/eris-db/event" - rpc "github.com/eris-ltd/eris-db/rpc" + "github.com/eris-ltd/eris-db/rpc" + "github.com/eris-ltd/eris-db/rpc/v0/shared" server "github.com/eris-ltd/eris-db/server" "github.com/eris-ltd/eris-db/txs" "github.com/eris-ltd/eris-db/util" @@ -165,7 +165,7 @@ func (restServer *RestServer) handleStorageAt(c *gin.Context) { // ********************************* Blockchain ********************************* func (restServer *RestServer) handleBlockchainInfo(c *gin.Context) { - bci := pipes.BlockchainInfo(restServer.pipe) + bci := shared.BlockchainInfo(restServer.pipe) c.Writer.WriteHeader(200) restServer.codec.Encode(bci, c.Writer) } @@ -297,65 +297,44 @@ func (restServer *RestServer) handleNameRegEntry(c *gin.Context) { // ********************************* Network ********************************* func (restServer *RestServer) handleNetworkInfo(c *gin.Context) { - nInfo, err := pipes.NetInfo(restServer.pipe) - if err != nil { - c.AbortWithError(500, err) - } + nInfo := shared.NetInfo(restServer.pipe) c.Writer.WriteHeader(200) restServer.codec.Encode(nInfo, c.Writer) } func (restServer *RestServer) handleClientVersion(c *gin.Context) { - version, err := pipes.ClientVersion(restServer.pipe) - if err != nil { - c.AbortWithError(500, err) - } + version := shared.ClientVersion(restServer.pipe) c.Writer.WriteHeader(200) restServer.codec.Encode(&core_types.ClientVersion{version}, c.Writer) } func (restServer *RestServer) handleMoniker(c *gin.Context) { - moniker, err := pipes.Moniker(restServer.pipe) - if err != nil { - c.AbortWithError(500, err) - } + moniker := shared.Moniker(restServer.pipe) c.Writer.WriteHeader(200) restServer.codec.Encode(&core_types.Moniker{moniker}, c.Writer) } func (restServer *RestServer) handleListening(c *gin.Context) { - listening, err := pipes.Listening(restServer.pipe) - if err != nil { - c.AbortWithError(500, err) - } + listening := shared.Listening(restServer.pipe) c.Writer.WriteHeader(200) restServer.codec.Encode(&core_types.Listening{listening}, c.Writer) } func (restServer *RestServer) handleListeners(c *gin.Context) { - listeners, err := pipes.Listeners(restServer.pipe) - if err != nil { - c.AbortWithError(500, err) - } + listeners := shared.Listeners(restServer.pipe) c.Writer.WriteHeader(200) restServer.codec.Encode(&core_types.Listeners{listeners}, c.Writer) } func (restServer *RestServer) handlePeers(c *gin.Context) { - peers, err := pipes.Peers(restServer.pipe) - if err != nil { - c.AbortWithError(500, err) - } + peers := restServer.pipe.GetConsensusEngine().Peers() c.Writer.WriteHeader(200) restServer.codec.Encode(peers, c.Writer) } func (restServer *RestServer) handlePeer(c *gin.Context) { address := c.MustGet("address").(string) - peer, err := pipes.Peer(restServer.pipe, address) - if err != nil { - c.AbortWithError(500, err) - } + peer := shared.Peer(restServer.pipe, address) c.Writer.WriteHeader(200) restServer.codec.Encode(peer, c.Writer) } diff --git a/core/pipes/net.go b/rpc/v0/shared/net.go similarity index 55% rename from core/pipes/net.go rename to rpc/v0/shared/net.go index 39412c07e7a9e16b2d690e2fd4f42f7ba7a04659..778b53595bc34896d54c61fc7cbe95029c4eb0ba 100644 --- a/core/pipes/net.go +++ b/rpc/v0/shared/net.go @@ -16,84 +16,69 @@ // Net is part of the pipe for ErisMint and provides the implementation // for the pipe to call into the ErisMint application -package pipes +package shared import ( - core_types "github.com/eris-ltd/eris-db/core/types" + consensus_types "github.com/eris-ltd/eris-db/consensus/types" "github.com/eris-ltd/eris-db/definitions" ) //----------------------------------------------------------------------------- // Get the complete pipe info. -func NetInfo(pipe definitions.Pipe) (*core_types.NetworkInfo, error) { +func NetInfo(pipe definitions.Pipe) *NetworkInfo { 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{ + return &NetworkInfo{ ClientVersion: thisNode.Version, Moniker: thisNode.Moniker, Listening: pipe.GetConsensusEngine().IsListening(), Listeners: listeners, - Peers: peers, - }, nil + Peers: pipe.GetConsensusEngine().Peers(), + } } // Get the client version -func ClientVersion(pipe definitions.Pipe) (string, error) { - return pipe.GetConsensusEngine().NodeInfo().Version, nil +func ClientVersion(pipe definitions.Pipe) string { + return pipe.GetConsensusEngine().NodeInfo().Version } // Get the moniker -func Moniker(pipe definitions.Pipe) (string, error) { - return pipe.GetConsensusEngine().NodeInfo().Moniker, nil +func Moniker(pipe definitions.Pipe) string { + return pipe.GetConsensusEngine().NodeInfo().Moniker } // Is the network currently listening for connections. -func Listening(pipe definitions.Pipe) (bool, error) { - return pipe.GetConsensusEngine().IsListening(), nil +func Listening(pipe definitions.Pipe) bool { + return pipe.GetConsensusEngine().IsListening() } // Is the network currently listening for connections. -func Listeners(pipe definitions.Pipe) ([]string, error) { +func Listeners(pipe definitions.Pipe) []string { listeners := []string{} for _, listener := range pipe.GetConsensusEngine().Listeners() { listeners = append(listeners, listener.String()) } - return listeners, nil + return listeners } -// Get a list of all peers. -func Peers(pipe definitions.Pipe) ([]*core_types.Peer, error) { - peers := make([]*core_types.Peer, 0) +func Peer(pipe definitions.Pipe, address string) *consensus_types.Peer { for _, peer := range pipe.GetConsensusEngine().Peers() { - p := &core_types.Peer{ - NodeInfo: &peer.NodeInfo, - IsOutbound: peer.IsOutbound, + if peer.NodeInfo.RemoteAddr == address { + return peer } - peers = append(peers, p) } - return peers, nil + return 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 +// NetworkInfo +type NetworkInfo struct { + ClientVersion string `json:"client_version"` + Moniker string `json:"moniker"` + Listening bool `json:"listening"` + Listeners []string `json:"listeners"` + Peers []*consensus_types.Peer `json:"peers"` +} diff --git a/core/pipes/pipes.go b/rpc/v0/shared/pipes.go similarity index 97% rename from core/pipes/pipes.go rename to rpc/v0/shared/pipes.go index 2bed4157cc31c12255fdd4be5e1f900764cba3b8..2256953ef7b2596ff81fdc946d60b556185e809c 100644 --- a/core/pipes/pipes.go +++ b/rpc/v0/shared/pipes.go @@ -1,4 +1,4 @@ -package pipes +package shared // Shared extension methods for Pipe and its derivatives diff --git a/test/mock/mock_web_api_test.go b/test/mock/mock_web_api_test.go index 7ab88688c05e5661afabf98503d8b30d6e88b099..fc0603e0114ae86d83368a0c6ebfd6cc350b10dc 100644 --- a/test/mock/mock_web_api_test.go +++ b/test/mock/mock_web_api_test.go @@ -9,6 +9,8 @@ import ( "runtime" "testing" + consensus_types "github.com/eris-ltd/eris-db/consensus/types" + account "github.com/eris-ltd/eris-db/account" core_types "github.com/eris-ltd/eris-db/core/types" event "github.com/eris-ltd/eris-db/event" @@ -18,6 +20,7 @@ import ( td "github.com/eris-ltd/eris-db/test/testdata/testdata" "github.com/eris-ltd/eris-db/txs" + "github.com/eris-ltd/eris-db/rpc/v0/shared" "github.com/gin-gonic/gin" "github.com/stretchr/testify/suite" "github.com/tendermint/log15" @@ -41,7 +44,7 @@ type MockSuite struct { testData *td.TestData } -func (this *MockSuite) SetupSuite() { +func (mockSuite *MockSuite) SetupSuite() { gin.SetMode(gin.ReleaseMode) // Load the supporting objects. testData := td.LoadTestData() @@ -58,262 +61,262 @@ func (this *MockSuite) SetupSuite() { if err != nil { panic(err) } - this.serveProcess = proc - this.codec = rpc_v0.NewTCodec() - this.testData = testData - this.sUrl = "http://localhost:31402" + mockSuite.serveProcess = proc + mockSuite.codec = rpc_v0.NewTCodec() + mockSuite.testData = testData + mockSuite.sUrl = "http://localhost:31402" } -func (this *MockSuite) TearDownSuite() { - sec := this.serveProcess.StopEventChannel() - this.serveProcess.Stop(0) +func (mockSuite *MockSuite) TearDownSuite() { + sec := mockSuite.serveProcess.StopEventChannel() + mockSuite.serveProcess.Stop(0) <-sec } // ********************************************* Accounts ********************************************* -func (this *MockSuite) TestGetAccounts() { - resp := this.get("/accounts") +func (mockSuite *MockSuite) TestGetAccounts() { + resp := mockSuite.get("/accounts") ret := &core_types.AccountList{} - errD := this.codec.Decode(ret, resp.Body) - this.NoError(errD) - this.Equal(ret, this.testData.GetAccounts.Output) + errD := mockSuite.codec.Decode(ret, resp.Body) + mockSuite.NoError(errD) + mockSuite.Equal(mockSuite.testData.GetAccounts.Output, ret) } -func (this *MockSuite) TestGetAccount() { - addr := hex.EncodeToString(this.testData.GetAccount.Input.Address) - resp := this.get("/accounts/" + addr) +func (mockSuite *MockSuite) TestGetAccount() { + addr := hex.EncodeToString(mockSuite.testData.GetAccount.Input.Address) + resp := mockSuite.get("/accounts/" + addr) ret := &account.Account{} - errD := this.codec.Decode(ret, resp.Body) - this.NoError(errD) - this.Equal(ret, this.testData.GetAccount.Output) + errD := mockSuite.codec.Decode(ret, resp.Body) + mockSuite.NoError(errD) + mockSuite.Equal(mockSuite.testData.GetAccount.Output, ret) } -func (this *MockSuite) TestGetStorage() { - addr := hex.EncodeToString(this.testData.GetStorage.Input.Address) - resp := this.get("/accounts/" + addr + "/storage") +func (mockSuite *MockSuite) TestGetStorage() { + addr := hex.EncodeToString(mockSuite.testData.GetStorage.Input.Address) + resp := mockSuite.get("/accounts/" + addr + "/storage") ret := &core_types.Storage{} - errD := this.codec.Decode(ret, resp.Body) - this.NoError(errD) - this.Equal(ret, this.testData.GetStorage.Output) + errD := mockSuite.codec.Decode(ret, resp.Body) + mockSuite.NoError(errD) + mockSuite.Equal(mockSuite.testData.GetStorage.Output, ret) } -func (this *MockSuite) TestGetStorageAt() { - addr := hex.EncodeToString(this.testData.GetStorageAt.Input.Address) - key := hex.EncodeToString(this.testData.GetStorageAt.Input.Key) - resp := this.get("/accounts/" + addr + "/storage/" + key) +func (mockSuite *MockSuite) TestGetStorageAt() { + addr := hex.EncodeToString(mockSuite.testData.GetStorageAt.Input.Address) + key := hex.EncodeToString(mockSuite.testData.GetStorageAt.Input.Key) + resp := mockSuite.get("/accounts/" + addr + "/storage/" + key) ret := &core_types.StorageItem{} - errD := this.codec.Decode(ret, resp.Body) - this.NoError(errD) - this.Equal(ret, this.testData.GetStorageAt.Output) + errD := mockSuite.codec.Decode(ret, resp.Body) + mockSuite.NoError(errD) + mockSuite.Equal(mockSuite.testData.GetStorageAt.Output, ret) } // ********************************************* Blockchain ********************************************* -func (this *MockSuite) TestGetBlockchainInfo() { - resp := this.get("/blockchain") +func (mockSuite *MockSuite) TestGetBlockchainInfo() { + resp := mockSuite.get("/blockchain") ret := &core_types.BlockchainInfo{} - errD := this.codec.Decode(ret, resp.Body) - this.NoError(errD) - this.Equal(this.testData.GetBlockchainInfo.Output, ret) + errD := mockSuite.codec.Decode(ret, resp.Body) + mockSuite.NoError(errD) + mockSuite.Equal(mockSuite.testData.GetBlockchainInfo.Output, ret) } -func (this *MockSuite) TestGetChainId() { - resp := this.get("/blockchain/chain_id") +func (mockSuite *MockSuite) TestGetChainId() { + resp := mockSuite.get("/blockchain/chain_id") ret := &core_types.ChainId{} - errD := this.codec.Decode(ret, resp.Body) - this.NoError(errD) - this.Equal(this.testData.GetChainId.Output, ret) + errD := mockSuite.codec.Decode(ret, resp.Body) + mockSuite.NoError(errD) + mockSuite.Equal(mockSuite.testData.GetChainId.Output, ret) } -func (this *MockSuite) TestGetGenesisHash() { - resp := this.get("/blockchain/genesis_hash") +func (mockSuite *MockSuite) TestGetGenesisHash() { + resp := mockSuite.get("/blockchain/genesis_hash") ret := &core_types.GenesisHash{} - errD := this.codec.Decode(ret, resp.Body) - this.NoError(errD) - this.Equal(this.testData.GetGenesisHash.Output, ret) + errD := mockSuite.codec.Decode(ret, resp.Body) + mockSuite.NoError(errD) + mockSuite.Equal(mockSuite.testData.GetGenesisHash.Output, ret) } -func (this *MockSuite) TestLatestBlockHeight() { - resp := this.get("/blockchain/latest_block_height") +func (mockSuite *MockSuite) TestLatestBlockHeight() { + resp := mockSuite.get("/blockchain/latest_block_height") ret := &core_types.LatestBlockHeight{} - errD := this.codec.Decode(ret, resp.Body) - this.NoError(errD) - this.Equal(this.testData.GetLatestBlockHeight.Output, ret) + errD := mockSuite.codec.Decode(ret, resp.Body) + mockSuite.NoError(errD) + mockSuite.Equal(mockSuite.testData.GetLatestBlockHeight.Output, ret) } -func (this *MockSuite) TestBlocks() { - resp := this.get("/blockchain/blocks") +func (mockSuite *MockSuite) TestBlocks() { + resp := mockSuite.get("/blockchain/blocks") ret := &core_types.Blocks{} - errD := this.codec.Decode(ret, resp.Body) - this.NoError(errD) - this.Equal(this.testData.GetBlocks.Output, ret) + errD := mockSuite.codec.Decode(ret, resp.Body) + mockSuite.NoError(errD) + mockSuite.Equal(mockSuite.testData.GetBlocks.Output, ret) } // ********************************************* Consensus ********************************************* // TODO: re-enable these when implemented -//func (this *MockSuite) TestGetConsensusState() { -// resp := this.get("/consensus") +//func (mockSuite *MockSuite) TestGetConsensusState() { +// resp := mockSuite.get("/consensus") // ret := &core_types.ConsensusState{} -// errD := this.codec.Decode(ret, resp.Body) -// this.NoError(errD) +// errD := mockSuite.codec.Decode(ret, resp.Body) +// mockSuite.NoError(errD) // ret.StartTime = "" -// this.Equal(this.testData.GetConsensusState.Output, ret) +// mockSuite.Equal(mockSuite.testData.GetConsensusState.Output, ret) //} // -//func (this *MockSuite) TestGetValidators() { -// resp := this.get("/consensus/validators") +//func (mockSuite *MockSuite) TestGetValidators() { +// resp := mockSuite.get("/consensus/validators") // ret := &core_types.ValidatorList{} -// errD := this.codec.Decode(ret, resp.Body) -// this.NoError(errD) -// this.Equal(this.testData.GetValidators.Output, ret) +// errD := mockSuite.codec.Decode(ret, resp.Body) +// mockSuite.NoError(errD) +// mockSuite.Equal(mockSuite.testData.GetValidators.Output, ret) //} // ********************************************* NameReg ********************************************* -func (this *MockSuite) TestGetNameRegEntry() { - resp := this.get("/namereg/" + this.testData.GetNameRegEntry.Input.Name) +func (mockSuite *MockSuite) TestGetNameRegEntry() { + resp := mockSuite.get("/namereg/" + mockSuite.testData.GetNameRegEntry.Input.Name) ret := &core_types.NameRegEntry{} - errD := this.codec.Decode(ret, resp.Body) - this.NoError(errD) - this.Equal(this.testData.GetNameRegEntry.Output, ret) + errD := mockSuite.codec.Decode(ret, resp.Body) + mockSuite.NoError(errD) + mockSuite.Equal(mockSuite.testData.GetNameRegEntry.Output, ret) } -func (this *MockSuite) TestGetNameRegEntries() { - resp := this.get("/namereg") +func (mockSuite *MockSuite) TestGetNameRegEntries() { + resp := mockSuite.get("/namereg") ret := &core_types.ResultListNames{} - errD := this.codec.Decode(ret, resp.Body) - this.NoError(errD) - this.Equal(this.testData.GetNameRegEntries.Output, ret) + errD := mockSuite.codec.Decode(ret, resp.Body) + mockSuite.NoError(errD) + mockSuite.Equal(mockSuite.testData.GetNameRegEntries.Output, ret) } // ********************************************* Network ********************************************* -func (this *MockSuite) TestGetNetworkInfo() { - resp := this.get("/network") - ret := &core_types.NetworkInfo{} - errD := this.codec.Decode(ret, resp.Body) - this.NoError(errD) - this.Equal(ret, this.testData.GetNetworkInfo.Output) +func (mockSuite *MockSuite) TestGetNetworkInfo() { + resp := mockSuite.get("/network") + ret := &shared.NetworkInfo{} + errD := mockSuite.codec.Decode(ret, resp.Body) + mockSuite.NoError(errD) + mockSuite.Equal(mockSuite.testData.GetNetworkInfo.Output, ret) } -func (this *MockSuite) TestGetClientVersion() { - resp := this.get("/network/client_version") +func (mockSuite *MockSuite) TestGetClientVersion() { + resp := mockSuite.get("/network/client_version") ret := &core_types.ClientVersion{} - errD := this.codec.Decode(ret, resp.Body) - this.NoError(errD) - this.Equal(ret, this.testData.GetClientVersion.Output) + errD := mockSuite.codec.Decode(ret, resp.Body) + mockSuite.NoError(errD) + mockSuite.Equal(mockSuite.testData.GetClientVersion.Output, ret) } -func (this *MockSuite) TestGetMoniker() { - resp := this.get("/network/moniker") +func (mockSuite *MockSuite) TestGetMoniker() { + resp := mockSuite.get("/network/moniker") ret := &core_types.Moniker{} - errD := this.codec.Decode(ret, resp.Body) - this.NoError(errD) - this.Equal(ret, this.testData.GetMoniker.Output) + errD := mockSuite.codec.Decode(ret, resp.Body) + mockSuite.NoError(errD) + mockSuite.Equal(mockSuite.testData.GetMoniker.Output, ret) } -func (this *MockSuite) TestIsListening() { - resp := this.get("/network/listening") +func (mockSuite *MockSuite) TestIsListening() { + resp := mockSuite.get("/network/listening") ret := &core_types.Listening{} - errD := this.codec.Decode(ret, resp.Body) - this.NoError(errD) - this.Equal(ret, this.testData.IsListening.Output) + errD := mockSuite.codec.Decode(ret, resp.Body) + mockSuite.NoError(errD) + mockSuite.Equal(mockSuite.testData.IsListening.Output, ret) } -func (this *MockSuite) TestGetListeners() { - resp := this.get("/network/listeners") +func (mockSuite *MockSuite) TestGetListeners() { + resp := mockSuite.get("/network/listeners") ret := &core_types.Listeners{} - errD := this.codec.Decode(ret, resp.Body) - this.NoError(errD) - this.Equal(ret, this.testData.GetListeners.Output) + errD := mockSuite.codec.Decode(ret, resp.Body) + mockSuite.NoError(errD) + mockSuite.Equal(mockSuite.testData.GetListeners.Output, ret) } -func (this *MockSuite) TestGetPeers() { - resp := this.get("/network/peers") - ret := []*core_types.Peer{} - errD := this.codec.Decode(ret, resp.Body) - this.NoError(errD) - this.Equal(ret, this.testData.GetPeers.Output) +func (mockSuite *MockSuite) TestGetPeers() { + resp := mockSuite.get("/network/peers") + ret := []*consensus_types.Peer{} + errD := mockSuite.codec.Decode(ret, resp.Body) + mockSuite.NoError(errD) + mockSuite.Equal(mockSuite.testData.GetPeers.Output, ret) } /* -func (this *MockSuite) TestGetPeer() { - addr := this.testData.GetPeer.Input.Address - resp := this.get("/network/peer/" + addr) +func (mockSuite *MockSuite) TestGetPeer() { + addr := mockSuite.testData.GetPeer.Input.Address + resp := mockSuite.get("/network/peer/" + addr) ret := []*core_types.Peer{} - errD := this.codec.Decode(ret, resp.Body) - this.NoError(errD) - this.Equal(ret, this.testData.GetPeers.Output) + errD := mockSuite.codec.Decode(ret, resp.Body) + mockSuite.NoError(errD) + mockSuite.Equal(mockSuite.testData.GetPeers.Output) } */ // ********************************************* Transactions ********************************************* -func (this *MockSuite) TestTransactCreate() { - resp := this.postJson("/unsafe/txpool", this.testData.TransactCreate.Input) +func (mockSuite *MockSuite) TestTransactCreate() { + resp := mockSuite.postJson("/unsafe/txpool", mockSuite.testData.TransactCreate.Input) ret := &txs.Receipt{} - errD := this.codec.Decode(ret, resp.Body) - this.NoError(errD) - this.Equal(ret, this.testData.TransactCreate.Output) + errD := mockSuite.codec.Decode(ret, resp.Body) + mockSuite.NoError(errD) + mockSuite.Equal(mockSuite.testData.TransactCreate.Output, ret) } -func (this *MockSuite) TestTransact() { - resp := this.postJson("/unsafe/txpool", this.testData.Transact.Input) +func (mockSuite *MockSuite) TestTransact() { + resp := mockSuite.postJson("/unsafe/txpool", mockSuite.testData.Transact.Input) ret := &txs.Receipt{} - errD := this.codec.Decode(ret, resp.Body) - this.NoError(errD) - this.Equal(ret, this.testData.Transact.Output) + errD := mockSuite.codec.Decode(ret, resp.Body) + mockSuite.NoError(errD) + mockSuite.Equal(mockSuite.testData.Transact.Output, ret) } -func (this *MockSuite) TestTransactNameReg() { - resp := this.postJson("/unsafe/namereg/txpool", this.testData.TransactNameReg.Input) +func (mockSuite *MockSuite) TestTransactNameReg() { + resp := mockSuite.postJson("/unsafe/namereg/txpool", mockSuite.testData.TransactNameReg.Input) ret := &txs.Receipt{} - errD := this.codec.Decode(ret, resp.Body) - this.NoError(errD) - this.Equal(ret, this.testData.TransactNameReg.Output) + errD := mockSuite.codec.Decode(ret, resp.Body) + mockSuite.NoError(errD) + mockSuite.Equal(mockSuite.testData.TransactNameReg.Output, ret) } -func (this *MockSuite) TestGetUnconfirmedTxs() { - resp := this.get("/txpool") +func (mockSuite *MockSuite) TestGetUnconfirmedTxs() { + resp := mockSuite.get("/txpool") ret := &txs.UnconfirmedTxs{} - errD := this.codec.Decode(ret, resp.Body) - this.NoError(errD) - this.Equal(ret, this.testData.GetUnconfirmedTxs.Output) + errD := mockSuite.codec.Decode(ret, resp.Body) + mockSuite.NoError(errD) + mockSuite.Equal(mockSuite.testData.GetUnconfirmedTxs.Output, ret) } -func (this *MockSuite) TestCallCode() { - resp := this.postJson("/codecalls", this.testData.CallCode.Input) +func (mockSuite *MockSuite) TestCallCode() { + resp := mockSuite.postJson("/codecalls", mockSuite.testData.CallCode.Input) ret := &core_types.Call{} - errD := this.codec.Decode(ret, resp.Body) - this.NoError(errD) - this.Equal(ret, this.testData.CallCode.Output) + errD := mockSuite.codec.Decode(ret, resp.Body) + mockSuite.NoError(errD) + mockSuite.Equal(mockSuite.testData.CallCode.Output, ret) } -func (this *MockSuite) TestCall() { - resp := this.postJson("/calls", this.testData.Call.Input) +func (mockSuite *MockSuite) TestCall() { + resp := mockSuite.postJson("/calls", mockSuite.testData.Call.Input) ret := &core_types.Call{} - errD := this.codec.Decode(ret, resp.Body) - this.NoError(errD) - this.Equal(ret, this.testData.CallCode.Output) + errD := mockSuite.codec.Decode(ret, resp.Body) + mockSuite.NoError(errD) + mockSuite.Equal(mockSuite.testData.CallCode.Output, ret) } // ********************************************* Utilities ********************************************* -func (this *MockSuite) get(endpoint string) *http.Response { - resp, errG := http.Get(this.sUrl + endpoint) - this.NoError(errG) - this.Equal(200, resp.StatusCode) +func (mockSuite *MockSuite) get(endpoint string) *http.Response { + resp, errG := http.Get(mockSuite.sUrl + endpoint) + mockSuite.NoError(errG) + mockSuite.Equal(200, resp.StatusCode) return resp } -func (this *MockSuite) postJson(endpoint string, v interface{}) *http.Response { - bts, errE := this.codec.EncodeBytes(v) - this.NoError(errE) - resp, errP := http.Post(this.sUrl+endpoint, "application/json", bytes.NewBuffer(bts)) - this.NoError(errP) - this.Equal(200, resp.StatusCode) +func (mockSuite *MockSuite) postJson(endpoint string, v interface{}) *http.Response { + bts, errE := mockSuite.codec.EncodeBytes(v) + mockSuite.NoError(errE) + resp, errP := http.Post(mockSuite.sUrl+endpoint, "application/json", bytes.NewBuffer(bts)) + mockSuite.NoError(errP) + mockSuite.Equal(200, resp.StatusCode) return resp } diff --git a/test/mock/pipe.go b/test/mock/pipe.go index d57f781e7c6a6965e07f21846f2e1663b0c8ba70..e279ad18253e2fc755f9948d8ec5108d89588a41 100644 --- a/test/mock/pipe.go +++ b/test/mock/pipe.go @@ -28,7 +28,6 @@ type MockPipe struct { consensusEngine consensus_types.ConsensusEngine events event.EventEmitter namereg definitions.NameReg - net definitions.Net transactor definitions.Transactor } @@ -39,7 +38,6 @@ func NewMockPipe(td *td.TestData) definitions.Pipe { consensusEngine := &consensusEngine{td} eventer := &eventer{td} namereg := &namereg{td} - net := &network{td} transactor := &transactor{td} return &MockPipe{ td, @@ -48,7 +46,6 @@ func NewMockPipe(td *td.TestData) definitions.Pipe { consensusEngine, eventer, namereg, - net, transactor, } } @@ -74,10 +71,6 @@ func (pipe *MockPipe) NameReg() definitions.NameReg { return pipe.namereg } -func (pipe *MockPipe) Net() definitions.Net { - return pipe.net -} - func (pipe *MockPipe) Transactor() definitions.Transactor { return pipe.transactor } @@ -176,27 +169,36 @@ func (cons *consensusEngine) BroadcastTransaction(transaction []byte, } func (cons *consensusEngine) IsListening() bool { - return true + return cons.testData.IsListening.Output.Listening } func (cons *consensusEngine) Listeners() []p2p.Listener { - return make([]p2p.Listener, 0) + p2pListeners := make([]p2p.Listener, 0) + + for _, name := range cons.testData.GetListeners.Output.Listeners { + p2pListeners = append(p2pListeners, p2p.NewDefaultListener("tcp", name, true)) + } + + return p2pListeners } func (cons *consensusEngine) NodeInfo() *p2p.NodeInfo { - return &p2p.NodeInfo{} + return &p2p.NodeInfo{ + Version: cons.testData.GetNetworkInfo.Output.ClientVersion, + Moniker: cons.testData.GetNetworkInfo.Output.Moniker, + } } -func (cons *consensusEngine) Peers() []consensus_types.Peer { - return make([]consensus_types.Peer, 0) +func (cons *consensusEngine) Peers() []*consensus_types.Peer { + return cons.testData.GetPeers.Output } func (cons *consensusEngine) PublicValidatorKey() crypto.PubKey { return crypto.PubKeyEd25519{ - 1,2,3,4,5,6,7,8, - 1,2,3,4,5,6,7,8, - 1,2,3,4,5,6,7,8, - 1,2,3,4,5,6,7,8, + 1, 2, 3, 4, 5, 6, 7, 8, + 1, 2, 3, 4, 5, 6, 7, 8, + 1, 2, 3, 4, 5, 6, 7, 8, + 1, 2, 3, 4, 5, 6, 7, 8, } } @@ -246,40 +248,6 @@ func (nmreg *namereg) Entries(filters []*event.FilterData) (*core_types.ResultLi return nmreg.testData.GetNameRegEntries.Output, nil } -// Net -type network struct { - testData *td.TestData -} - -func (net *network) Info() (*core_types.NetworkInfo, error) { - return net.testData.GetNetworkInfo.Output, nil -} - -func (net *network) ClientVersion() (string, error) { - return net.testData.GetClientVersion.Output.ClientVersion, nil -} - -func (net *network) Moniker() (string, error) { - return net.testData.GetMoniker.Output.Moniker, nil -} - -func (net *network) Listening() (bool, error) { - return net.testData.IsListening.Output.Listening, nil -} - -func (net *network) Listeners() ([]string, error) { - return net.testData.GetListeners.Output.Listeners, nil -} - -func (net *network) Peers() ([]*core_types.Peer, error) { - return net.testData.GetPeers.Output, nil -} - -func (net *network) Peer(address string) (*core_types.Peer, error) { - // return net.testData.GetPeer.Output, nil - return nil, nil -} - // Txs type transactor struct { testData *td.TestData diff --git a/test/testdata/testdata/testdata.go b/test/testdata/testdata/testdata.go index 89b24a470acdaa7f407dc630d97af61ca138502f..5fd2be000a2773de50fb7e469ed2a08bc820f2a5 100644 --- a/test/testdata/testdata/testdata.go +++ b/test/testdata/testdata/testdata.go @@ -7,6 +7,7 @@ import ( event "github.com/eris-ltd/eris-db/event" stypes "github.com/eris-ltd/eris-db/manager/eris-mint/state/types" rpc_v0 "github.com/eris-ltd/eris-db/rpc/v0" + "github.com/eris-ltd/eris-db/rpc/v0/shared" transaction "github.com/eris-ltd/eris-db/txs" mintTypes "github.com/tendermint/tendermint/types" ) @@ -605,7 +606,7 @@ type ( } GetNetworkInfoData struct { - Output *core_types.NetworkInfo `json:"output"` + Output *shared.NetworkInfo `json:"output"` } GetClientVersionData struct { @@ -625,12 +626,12 @@ type ( } GetPeersData struct { - Output []*core_types.Peer `json:"output"` + Output []*consensus_types.Peer `json:"output"` } GetPeerData struct { - Input *rpc_v0.PeerParam `json:"input"` - Output *core_types.Peer `json:"output"` + Input *rpc_v0.PeerParam `json:"input"` + Output *consensus_types.Peer `json:"output"` } TransactData struct {