diff --git a/p2p/discover/node.go b/p2p/discover/node.go index 3b0c84115c..55fe0c4d64 100644 --- a/p2p/discover/node.go +++ b/p2p/discover/node.go @@ -235,7 +235,7 @@ func (n NodeID) String() string { return fmt.Sprintf("%x", n[:]) } -// The Go syntax representation of a NodeID is a call to HexID. +// GoString is the Go syntax representation of a NodeID is a call to HexID. func (n NodeID) GoString() string { return fmt.Sprintf("discover.HexID(\"%x\")", n[:]) } @@ -317,11 +317,11 @@ func PubkeyID(pub *ecdsa.PublicKey) NodeID { // Pubkey returns the public key represented by the node ID. // It returns an error if the ID is not a point on the curve. -func (id NodeID) Pubkey() (*ecdsa.PublicKey, error) { +func (n NodeID) Pubkey() (*ecdsa.PublicKey, error) { p := &ecdsa.PublicKey{Curve: crypto.S256(), X: new(big.Int), Y: new(big.Int)} - half := len(id) / 2 - p.X.SetBytes(id[:half]) - p.Y.SetBytes(id[half:]) + half := len(n) / 2 + p.X.SetBytes(n[:half]) + p.Y.SetBytes(n[half:]) if !p.Curve.IsOnCurve(p.X, p.Y) { return nil, errors.New("id is invalid secp256k1 curve point") } diff --git a/p2p/discover/table_test.go b/p2p/discover/table_test.go index ed55ebd9a9..01737fda8a 100644 --- a/p2p/discover/table_test.go +++ b/p2p/discover/table_test.go @@ -209,9 +209,8 @@ func (t *pingRecorder) ping(toid NodeID, toaddr *net.UDPAddr) error { t.pinged[toid] = true if t.dead[toid] { return errTimeout - } else { - return nil } + return nil } func (t *pingRecorder) close() {} diff --git a/p2p/discv5/net.go b/p2p/discv5/net.go index 9b0bd0c80a..8854a20fb2 100644 --- a/p2p/discv5/net.go +++ b/p2p/discv5/net.go @@ -567,12 +567,11 @@ loop: net.ticketStore.searchLookupDone(res.target, res.nodes, func(n *Node, topic Topic) []byte { if n.state != nil && n.state.canQuery { return net.conn.send(n, topicQueryPacket, topicQuery{Topic: topic}) // TODO: set expiration - } else { - if n.state == unknown { - net.ping(n, n.addr()) - } - return nil } + if n.state == unknown { + net.ping(n, n.addr()) + } + return nil }) case <-statsDump.C: diff --git a/p2p/discv5/node.go b/p2p/discv5/node.go index 3d47485122..0befb2db66 100644 --- a/p2p/discv5/node.go +++ b/p2p/discv5/node.go @@ -268,7 +268,7 @@ func (n NodeID) String() string { return fmt.Sprintf("%x", n[:]) } -// The Go syntax representation of a NodeID is a call to HexID. +// GoString is the Go syntax representation of a NodeID is a call to HexID. func (n NodeID) GoString() string { return fmt.Sprintf("discover.HexID(\"%x\")", n[:]) } @@ -326,8 +326,8 @@ func (n NodeID) Pubkey() (*ecdsa.PublicKey, error) { return p, nil } -func (id NodeID) mustPubkey() ecdsa.PublicKey { - pk, err := id.Pubkey() +func (n NodeID) mustPubkey() ecdsa.PublicKey { + pk, err := n.Pubkey() if err != nil { panic(err) } diff --git a/p2p/discv5/table_test.go b/p2p/discv5/table_test.go index a29943dab9..dce469d74a 100644 --- a/p2p/discv5/table_test.go +++ b/p2p/discv5/table_test.go @@ -176,9 +176,8 @@ func (t *pingRecorder) ping(toid NodeID, toaddr *net.UDPAddr) error { t.pinged[toid] = true if t.responding[toid] { return nil - } else { - return errTimeout } + return errTimeout } func TestTable_closest(t *testing.T) { diff --git a/p2p/discv5/ticket.go b/p2p/discv5/ticket.go index ae4b18e7cd..017e29c1be 100644 --- a/p2p/discv5/ticket.go +++ b/p2p/discv5/ticket.go @@ -689,7 +689,7 @@ func (b *topicRadiusBucket) update(now mclock.AbsTime) { for target, tm := range b.lookupSent { if now-tm > mclock.AbsTime(respTimeout) { - b.weights[trNoAdjust] += 1 + b.weights[trNoAdjust]++ delete(b.lookupSent, target) } } @@ -698,10 +698,10 @@ func (b *topicRadiusBucket) update(now mclock.AbsTime) { func (b *topicRadiusBucket) adjust(now mclock.AbsTime, inside float64) { b.update(now) if inside <= 0 { - b.weights[trOutside] += 1 + b.weights[trOutside]++ } else { if inside >= 1 { - b.weights[trInside] += 1 + b.weights[trInside]++ } else { b.weights[trInside] += inside b.weights[trOutside] += 1 - inside diff --git a/p2p/nat/nat.go b/p2p/nat/nat.go index a254648c66..b517762953 100644 --- a/p2p/nat/nat.go +++ b/p2p/nat/nat.go @@ -29,7 +29,7 @@ import ( "github.com/jackpal/go-nat-pmp" ) -// An implementation of nat.Interface can map local ports to ports +// Interface is an implementation of nat.Interface that can map local ports to ports // accessible from the Internet. type Interface interface { // These methods manage a mapping between a port on the local @@ -226,9 +226,8 @@ func (n *autodisc) String() string { defer n.mu.Unlock() if n.found == nil { return n.what - } else { - return n.found.String() } + return n.found.String() } // wait blocks until auto-discovery has been performed. diff --git a/p2p/server.go b/p2p/server.go index cdb5b1926e..4464976934 100644 --- a/p2p/server.go +++ b/p2p/server.go @@ -300,7 +300,7 @@ func (srv *Server) RemovePeer(node *discover.Node) { } } -// SubscribePeers subscribes the given channel to peer events +// SubscribeEvents subscribes the given channel to peer events func (srv *Server) SubscribeEvents(ch chan *PeerEvent) event.Subscription { return srv.peerFeed.Subscribe(ch) } diff --git a/p2p/simulations/http.go b/p2p/simulations/http.go index 24001f1949..9cae034a46 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() diff --git a/p2p/simulations/http_test.go b/p2p/simulations/http_test.go index 732d49f546..40e94fd7ad 100644 --- a/p2p/simulations/http_test.go +++ b/p2p/simulations/http_test.go @@ -386,11 +386,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 389b1e3ec3..146b1fc6c1 100644 --- a/p2p/simulations/mocker.go +++ b/p2p/simulations/mocker.go @@ -29,19 +29,19 @@ import ( "github.com/ethereum/go-ethereum/p2p/simulations/adapters" ) -//a map of mocker names to its function +// mockerList maps mocker names to its function var mockerList = map[string]func(net *Network, quit chan struct{}, nodeCount int){ "startStop": startStop, "probabilistic": probabilistic, "boot": boot, } -//Lookup a mocker by its name, returns the mockerFn +// LookupMocker retrieves 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) +// GetMockerList returns 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)) @@ -51,7 +51,7 @@ func GetMockerList() []string { return list } -//The boot mockerFn only connects the node in a ring and doesn't do anything else +// boot mockerFn only connects the node in a ring and doesn't do anything else func boot(net *Network, quit chan struct{}, nodeCount int) { _, err := connectNodesInRing(net, nodeCount) if err != nil { @@ -59,7 +59,7 @@ func boot(net *Network, quit chan struct{}, nodeCount int) { } } -//The startStop mockerFn stops and starts nodes in a defined period (ticker) +// startStop mockerFn stops and starts nodes in a defined period (ticker) func startStop(net *Network, quit chan struct{}, nodeCount int) { nodes, err := connectNodesInRing(net, nodeCount) if err != nil { @@ -96,10 +96,10 @@ func startStop(net *Network, quit chan struct{}, nodeCount int) { } } -//The probabilistic mocker func has a more probabilistic pattern -//(the implementation could probably be improved): -//nodes are connected in a ring, then a varying number of random nodes is selected, -//mocker then stops and starts them in random intervals, and continues the loop +// probabilistic mocker func has a more probabilistic pattern +// (the implementation could probably be improved): +// nodes are connected in a ring, then a varying number of random nodes is selected, +// mocker then stops and starts them in random intervals, and continues the loop func probabilistic(net *Network, quit chan struct{}, nodeCount int) { nodes, err := connectNodesInRing(net, nodeCount) if err != nil { diff --git a/p2p/simulations/mocker_test.go b/p2p/simulations/mocker_test.go index de8ec0b337..5f26b1e3d2 100644 --- a/p2p/simulations/mocker_test.go +++ b/p2p/simulations/mocker_test.go @@ -101,7 +101,7 @@ func TestMocker(t *testing.T) { //time.Sleep( 3 *time.Second) } } else if event.Conn != nil && nodesComplete { - connCount += 1 + connCount++ if connCount == (nodeCount-1)*2 { wg.Done() return @@ -135,13 +135,13 @@ func TestMocker(t *testing.T) { wg.Wait() //check there are nodeCount number of nodes in the network - nodes_info, err := client.GetNodes() + nodesInfo, err := client.GetNodes() if err != nil { t.Fatalf("Could not get nodes list: %s", err) } - if len(nodes_info) != nodeCount { - t.Fatalf("Expected %d number of nodes, got: %d", nodeCount, len(nodes_info)) + if len(nodesInfo) != nodeCount { + t.Fatalf("Expected %d number of nodes, got: %d", nodeCount, len(nodesInfo)) } //stop the mocker @@ -160,12 +160,12 @@ func TestMocker(t *testing.T) { } //now the number of nodes in the network should be zero - nodes_info, err = client.GetNodes() + nodesInfo, err = client.GetNodes() if err != nil { t.Fatalf("Could not get nodes list: %s", err) } - if len(nodes_info) != 0 { - t.Fatalf("Expected empty list of nodes, got: %d", len(nodes_info)) + if len(nodesInfo) != 0 { + t.Fatalf("Expected empty list of nodes, got: %d", len(nodesInfo)) } } diff --git a/p2p/simulations/network.go b/p2p/simulations/network.go index 0fb7485ad0..04cfca12ce 100644 --- a/p2p/simulations/network.go +++ b/p2p/simulations/network.go @@ -370,7 +370,7 @@ func (net *Network) GetNode(id discover.NodeID) *Node { return net.getNode(id) } -// GetNode gets the node with the given name, returning nil if the node does +// GetNodeByName gets the node with the given name, returning nil if the node does // not exist func (net *Network) GetNodeByName(name string) *Node { net.lock.Lock() @@ -454,7 +454,7 @@ func (net *Network) getConn(oneID, otherID discover.NodeID) *Conn { return net.Conns[i] } -// InitConn(one, other) retrieves the connectiton model for the connection between +// InitConn retrieves the connectiton 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 @@ -737,7 +737,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) }