From 5b6527cefbc21de512c98213e2f3e0719fcf5c80 Mon Sep 17 00:00:00 2001 From: Mike Kinney Date: Mon, 9 Dec 2019 22:01:05 -0800 Subject: [PATCH] p2p/simulations: golint fixes --- p2p/simulations/connect.go | 1 + p2p/simulations/http.go | 12 +++++++++--- p2p/simulations/http_test.go | 6 +++--- p2p/simulations/mocker.go | 6 +++--- p2p/simulations/mocker_test.go | 2 +- p2p/simulations/network.go | 13 ++++++------- p2p/simulations/simulation.go | 3 +++ p2p/simulations/test.go | 9 +++++++++ 8 files changed, 35 insertions(+), 17 deletions(-) diff --git a/p2p/simulations/connect.go b/p2p/simulations/connect.go index ede96b34c1..6ab6bf5833 100644 --- a/p2p/simulations/connect.go +++ b/p2p/simulations/connect.go @@ -24,6 +24,7 @@ import ( ) var ( + // ErrNodeNotFound is the node not found error ErrNodeNotFound = errors.New("node not found") ) diff --git a/p2p/simulations/http.go b/p2p/simulations/http.go index 51d45ce8f4..b1a39d58b0 100644 --- a/p2p/simulations/http.go +++ b/p2p/simulations/http.go @@ -364,7 +364,7 @@ func (s *Server) StopMocker(w http.ResponseWriter, req *http.Request) { w.WriteHeader(http.StatusOK) } -// GetMockerList returns a list of available mockers +// GetMockers returns a list of available mockers func (s *Server) GetMockers(w http.ResponseWriter, req *http.Request) { list := GetMockerList() @@ -698,6 +698,12 @@ func (s *Server) JSON(w http.ResponseWriter, status int, data interface{}) { json.NewEncoder(w).Encode(data) } +type contextKey int +const ( + keyNode contextKey = iota + keyPeer +) + // wrapHandler returns a httprouter.Handle which wraps a http.HandlerFunc by // populating request.Context with any objects from the URL params func (s *Server) wrapHandler(handler http.HandlerFunc) httprouter.Handle { @@ -719,7 +725,7 @@ func (s *Server) wrapHandler(handler http.HandlerFunc) httprouter.Handle { http.NotFound(w, req) return } - ctx = context.WithValue(ctx, "node", node) + ctx = context.WithValue(ctx, keyNode, node) } if id := params.ByName("peerid"); id != "" { @@ -734,7 +740,7 @@ func (s *Server) wrapHandler(handler http.HandlerFunc) httprouter.Handle { http.NotFound(w, req) return } - ctx = context.WithValue(ctx, "peer", peer) + ctx = context.WithValue(ctx, keyPeer, peer) } handler(w, req.WithContext(ctx)) diff --git a/p2p/simulations/http_test.go b/p2p/simulations/http_test.go index e88999f48b..87e9e49941 100644 --- a/p2p/simulations/http_test.go +++ b/p2p/simulations/http_test.go @@ -400,11 +400,11 @@ func startTestNetwork(t *testing.T, client *Client) []string { // connect the nodes for i := 0; i < nodeCount-1; i++ { - peerId := i + 1 + peerID := i + 1 if i == nodeCount-1 { - peerId = 0 + peerID = 0 } - if err := client.ConnectNode(nodeIDs[i], nodeIDs[peerId]); err != nil { + if err := client.ConnectNode(nodeIDs[i], nodeIDs[peerID]); err != nil { t.Fatalf("error connecting nodes: %s", err) } } diff --git a/p2p/simulations/mocker.go b/p2p/simulations/mocker.go index 8ce777a010..d5a5fd4112 100644 --- a/p2p/simulations/mocker.go +++ b/p2p/simulations/mocker.go @@ -36,13 +36,13 @@ var mockerList = map[string]func(net *Network, quit chan struct{}, nodeCount int "boot": boot, } -//Lookup a mocker by its name, returns the mockerFn +// LookupMocker looks up a mocker by its name, returns the mockerFn func LookupMocker(mockerType string) func(net *Network, quit chan struct{}, nodeCount int) { return mockerList[mockerType] } -//Get a list of mockers (keys of the map) -//Useful for frontend to build available mocker selection +// GetMockerList will get a list of mockers (keys of the map) +// Useful for frontend to build available mocker selection func GetMockerList() []string { list := make([]string, 0, len(mockerList)) for k := range mockerList { diff --git a/p2p/simulations/mocker_test.go b/p2p/simulations/mocker_test.go index 56d81942bb..d97f55db9c 100644 --- a/p2p/simulations/mocker_test.go +++ b/p2p/simulations/mocker_test.go @@ -101,7 +101,7 @@ func TestMocker(t *testing.T) { nodesComplete = true } } else if event.Conn != nil && nodesComplete { - connCount += 1 + connCount++ } case <-time.After(30 * time.Second): t.Errorf("Timeout waiting for nodes being started up!") diff --git a/p2p/simulations/network.go b/p2p/simulations/network.go index ef5451e77e..1a8ce8b625 100644 --- a/p2p/simulations/network.go +++ b/p2p/simulations/network.go @@ -34,6 +34,7 @@ import ( "github.com/ethereum/go-ethereum/p2p/simulations/adapters" ) +// DialBanTimeout will set the time out for the "dial" (network connection) var DialBanTimeout = 200 * time.Millisecond // NetworkConfig defines configuration options for starting a Network @@ -454,9 +455,8 @@ func (net *Network) getNodeIDs(excludeIDs []enode.ID) []enode.ID { if len(excludeIDs) > 0 { // Return the difference of nodeIDs and excludeIDs return filterIDs(nodeIDs, excludeIDs) - } else { - return nodeIDs } + return nodeIDs } // GetNodes returns the existing nodes. @@ -472,9 +472,8 @@ func (net *Network) getNodes(excludeIDs []enode.ID) []*Node { if len(excludeIDs) > 0 { nodeIDs := net.getNodeIDs(excludeIDs) return net.getNodesByID(nodeIDs) - } else { - return net.Nodes } + return net.Nodes } // GetNodesByID returns existing nodes with the given enode.IDs. @@ -651,7 +650,7 @@ func (net *Network) getConn(oneID, otherID enode.ID) *Conn { return net.Conns[i] } -// InitConn(one, other) retrieves the connection model for the connection between +// InitConn (one, other) retrieves the connection model for the connection between // peers one and other, or creates a new one if it does not exist // the order of nodes does not matter, i.e., Conn(i,j) == Conn(j, i) // it checks if the connection is already up, and if the nodes are running @@ -891,6 +890,7 @@ func (net *Network) Snapshot() (*Snapshot, error) { return net.snapshot(nil, nil) } +// SnapshotWithServices will take a snapshot with the services specified as parameters func (net *Network) SnapshotWithServices(addServices []string, removeServices []string) (*Snapshot, error) { return net.snapshot(addServices, removeServices) } @@ -1098,7 +1098,6 @@ func (net *Network) executeNodeEvent(e *Event) error { func (net *Network) executeConnEvent(e *Event) error { if e.Conn.Up { return net.Connect(e.Conn.One, e.Conn.Other) - } else { - return net.Disconnect(e.Conn.One, e.Conn.Other) } + return net.Disconnect(e.Conn.One, e.Conn.Other) } diff --git a/p2p/simulations/simulation.go b/p2p/simulations/simulation.go index ae62c42b9c..c659c74a9a 100644 --- a/p2p/simulations/simulation.go +++ b/p2p/simulations/simulation.go @@ -113,6 +113,7 @@ func (s *Simulation) watchNetwork(result *StepResult) func() { } } +// Step is a struct for the action, trigger, and expectation type Step struct { // Action is the action to perform for this step Action func(context.Context) error @@ -125,6 +126,7 @@ type Step struct { Expect *Expectation } +// Expectation is a struct to hold the map of nodes and check function type Expectation struct { // Nodes is a list of nodes to check Nodes []enode.ID @@ -139,6 +141,7 @@ func newStepResult() *StepResult { } } +// StepResult is a struct to hold the results from each step type StepResult struct { // Error is the error encountered whilst running the step Error error diff --git a/p2p/simulations/test.go b/p2p/simulations/test.go index 687be6d0b8..da8c16eefc 100644 --- a/p2p/simulations/test.go +++ b/p2p/simulations/test.go @@ -31,12 +31,14 @@ type NoopService struct { c map[enode.ID]chan struct{} } +// NewNoopService will return a NoopService func NewNoopService(ackC map[enode.ID]chan struct{}) *NoopService { return &NoopService{ c: ackC, } } +// Protocols will return the protocols func (t *NoopService) Protocols() []p2p.Protocol { return []p2p.Protocol{ { @@ -62,18 +64,22 @@ func (t *NoopService) Protocols() []p2p.Protocol { } } +// APIs will return the list of rcp APIs func (t *NoopService) APIs() []rpc.API { return []rpc.API{} } +// Start is not implemented func (t *NoopService) Start(server *p2p.Server) error { return nil } +// Stop is not implemented func (t *NoopService) Stop() error { return nil } +// VerifyRing will verify the ring network func VerifyRing(t *testing.T, net *Network, ids []enode.ID) { t.Helper() n := len(ids) @@ -93,6 +99,7 @@ func VerifyRing(t *testing.T, net *Network, ids []enode.ID) { } } +// VerifyChain will verify the chain func VerifyChain(t *testing.T, net *Network, ids []enode.ID) { t.Helper() n := len(ids) @@ -112,6 +119,7 @@ func VerifyChain(t *testing.T, net *Network, ids []enode.ID) { } } +// VerifyFull will verify connections func VerifyFull(t *testing.T, net *Network, ids []enode.ID) { t.Helper() n := len(ids) @@ -130,6 +138,7 @@ func VerifyFull(t *testing.T, net *Network, ids []enode.ID) { } } +// VerifyStar will verify the star network func VerifyStar(t *testing.T, net *Network, ids []enode.ID, centerIndex int) { t.Helper() n := len(ids)