p2p/simulations: golint fixes

This commit is contained in:
Mike Kinney 2019-12-09 22:01:05 -08:00
parent 967a7b4339
commit 5b6527cefb
8 changed files with 35 additions and 17 deletions

View file

@ -24,6 +24,7 @@ import (
)
var (
// ErrNodeNotFound is the node not found error
ErrNodeNotFound = errors.New("node not found")
)

View file

@ -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))

View file

@ -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)
}
}

View file

@ -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 {

View file

@ -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!")

View file

@ -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)
}

View file

@ -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

View file

@ -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)