mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 10:52:25 +00:00
p2p/simulations: golint fixes
This commit is contained in:
parent
967a7b4339
commit
5b6527cefb
8 changed files with 35 additions and 17 deletions
|
|
@ -24,6 +24,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
// ErrNodeNotFound is the node not found error
|
||||||
ErrNodeNotFound = errors.New("node not found")
|
ErrNodeNotFound = errors.New("node not found")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -364,7 +364,7 @@ func (s *Server) StopMocker(w http.ResponseWriter, req *http.Request) {
|
||||||
w.WriteHeader(http.StatusOK)
|
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) {
|
func (s *Server) GetMockers(w http.ResponseWriter, req *http.Request) {
|
||||||
|
|
||||||
list := GetMockerList()
|
list := GetMockerList()
|
||||||
|
|
@ -698,6 +698,12 @@ func (s *Server) JSON(w http.ResponseWriter, status int, data interface{}) {
|
||||||
json.NewEncoder(w).Encode(data)
|
json.NewEncoder(w).Encode(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type contextKey int
|
||||||
|
const (
|
||||||
|
keyNode contextKey = iota
|
||||||
|
keyPeer
|
||||||
|
)
|
||||||
|
|
||||||
// wrapHandler returns a httprouter.Handle which wraps a http.HandlerFunc by
|
// wrapHandler returns a httprouter.Handle which wraps a http.HandlerFunc by
|
||||||
// populating request.Context with any objects from the URL params
|
// populating request.Context with any objects from the URL params
|
||||||
func (s *Server) wrapHandler(handler http.HandlerFunc) httprouter.Handle {
|
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)
|
http.NotFound(w, req)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ctx = context.WithValue(ctx, "node", node)
|
ctx = context.WithValue(ctx, keyNode, node)
|
||||||
}
|
}
|
||||||
|
|
||||||
if id := params.ByName("peerid"); id != "" {
|
if id := params.ByName("peerid"); id != "" {
|
||||||
|
|
@ -734,7 +740,7 @@ func (s *Server) wrapHandler(handler http.HandlerFunc) httprouter.Handle {
|
||||||
http.NotFound(w, req)
|
http.NotFound(w, req)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ctx = context.WithValue(ctx, "peer", peer)
|
ctx = context.WithValue(ctx, keyPeer, peer)
|
||||||
}
|
}
|
||||||
|
|
||||||
handler(w, req.WithContext(ctx))
|
handler(w, req.WithContext(ctx))
|
||||||
|
|
|
||||||
|
|
@ -400,11 +400,11 @@ func startTestNetwork(t *testing.T, client *Client) []string {
|
||||||
|
|
||||||
// connect the nodes
|
// connect the nodes
|
||||||
for i := 0; i < nodeCount-1; i++ {
|
for i := 0; i < nodeCount-1; i++ {
|
||||||
peerId := i + 1
|
peerID := i + 1
|
||||||
if i == nodeCount-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)
|
t.Fatalf("error connecting nodes: %s", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -36,13 +36,13 @@ var mockerList = map[string]func(net *Network, quit chan struct{}, nodeCount int
|
||||||
"boot": boot,
|
"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) {
|
func LookupMocker(mockerType string) func(net *Network, quit chan struct{}, nodeCount int) {
|
||||||
return mockerList[mockerType]
|
return mockerList[mockerType]
|
||||||
}
|
}
|
||||||
|
|
||||||
//Get a list of mockers (keys of the map)
|
// GetMockerList will get a list of mockers (keys of the map)
|
||||||
//Useful for frontend to build available mocker selection
|
// Useful for frontend to build available mocker selection
|
||||||
func GetMockerList() []string {
|
func GetMockerList() []string {
|
||||||
list := make([]string, 0, len(mockerList))
|
list := make([]string, 0, len(mockerList))
|
||||||
for k := range mockerList {
|
for k := range mockerList {
|
||||||
|
|
|
||||||
|
|
@ -101,7 +101,7 @@ func TestMocker(t *testing.T) {
|
||||||
nodesComplete = true
|
nodesComplete = true
|
||||||
}
|
}
|
||||||
} else if event.Conn != nil && nodesComplete {
|
} else if event.Conn != nil && nodesComplete {
|
||||||
connCount += 1
|
connCount++
|
||||||
}
|
}
|
||||||
case <-time.After(30 * time.Second):
|
case <-time.After(30 * time.Second):
|
||||||
t.Errorf("Timeout waiting for nodes being started up!")
|
t.Errorf("Timeout waiting for nodes being started up!")
|
||||||
|
|
|
||||||
|
|
@ -34,6 +34,7 @@ import (
|
||||||
"github.com/ethereum/go-ethereum/p2p/simulations/adapters"
|
"github.com/ethereum/go-ethereum/p2p/simulations/adapters"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// DialBanTimeout will set the time out for the "dial" (network connection)
|
||||||
var DialBanTimeout = 200 * time.Millisecond
|
var DialBanTimeout = 200 * time.Millisecond
|
||||||
|
|
||||||
// NetworkConfig defines configuration options for starting a Network
|
// 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 {
|
if len(excludeIDs) > 0 {
|
||||||
// Return the difference of nodeIDs and excludeIDs
|
// Return the difference of nodeIDs and excludeIDs
|
||||||
return filterIDs(nodeIDs, excludeIDs)
|
return filterIDs(nodeIDs, excludeIDs)
|
||||||
} else {
|
|
||||||
return nodeIDs
|
|
||||||
}
|
}
|
||||||
|
return nodeIDs
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetNodes returns the existing nodes.
|
// GetNodes returns the existing nodes.
|
||||||
|
|
@ -472,9 +472,8 @@ func (net *Network) getNodes(excludeIDs []enode.ID) []*Node {
|
||||||
if len(excludeIDs) > 0 {
|
if len(excludeIDs) > 0 {
|
||||||
nodeIDs := net.getNodeIDs(excludeIDs)
|
nodeIDs := net.getNodeIDs(excludeIDs)
|
||||||
return net.getNodesByID(nodeIDs)
|
return net.getNodesByID(nodeIDs)
|
||||||
} else {
|
|
||||||
return net.Nodes
|
|
||||||
}
|
}
|
||||||
|
return net.Nodes
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetNodesByID returns existing nodes with the given enode.IDs.
|
// 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]
|
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
|
// 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)
|
// 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
|
// 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)
|
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) {
|
func (net *Network) SnapshotWithServices(addServices []string, removeServices []string) (*Snapshot, error) {
|
||||||
return net.snapshot(addServices, removeServices)
|
return net.snapshot(addServices, removeServices)
|
||||||
}
|
}
|
||||||
|
|
@ -1098,7 +1098,6 @@ func (net *Network) executeNodeEvent(e *Event) error {
|
||||||
func (net *Network) executeConnEvent(e *Event) error {
|
func (net *Network) executeConnEvent(e *Event) error {
|
||||||
if e.Conn.Up {
|
if e.Conn.Up {
|
||||||
return net.Connect(e.Conn.One, e.Conn.Other)
|
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)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -113,6 +113,7 @@ func (s *Simulation) watchNetwork(result *StepResult) func() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Step is a struct for the action, trigger, and expectation
|
||||||
type Step struct {
|
type Step struct {
|
||||||
// Action is the action to perform for this step
|
// Action is the action to perform for this step
|
||||||
Action func(context.Context) error
|
Action func(context.Context) error
|
||||||
|
|
@ -125,6 +126,7 @@ type Step struct {
|
||||||
Expect *Expectation
|
Expect *Expectation
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Expectation is a struct to hold the map of nodes and check function
|
||||||
type Expectation struct {
|
type Expectation struct {
|
||||||
// Nodes is a list of nodes to check
|
// Nodes is a list of nodes to check
|
||||||
Nodes []enode.ID
|
Nodes []enode.ID
|
||||||
|
|
@ -139,6 +141,7 @@ func newStepResult() *StepResult {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// StepResult is a struct to hold the results from each step
|
||||||
type StepResult struct {
|
type StepResult struct {
|
||||||
// Error is the error encountered whilst running the step
|
// Error is the error encountered whilst running the step
|
||||||
Error error
|
Error error
|
||||||
|
|
|
||||||
|
|
@ -31,12 +31,14 @@ type NoopService struct {
|
||||||
c map[enode.ID]chan struct{}
|
c map[enode.ID]chan struct{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewNoopService will return a NoopService
|
||||||
func NewNoopService(ackC map[enode.ID]chan struct{}) *NoopService {
|
func NewNoopService(ackC map[enode.ID]chan struct{}) *NoopService {
|
||||||
return &NoopService{
|
return &NoopService{
|
||||||
c: ackC,
|
c: ackC,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Protocols will return the protocols
|
||||||
func (t *NoopService) Protocols() []p2p.Protocol {
|
func (t *NoopService) Protocols() []p2p.Protocol {
|
||||||
return []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 {
|
func (t *NoopService) APIs() []rpc.API {
|
||||||
return []rpc.API{}
|
return []rpc.API{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Start is not implemented
|
||||||
func (t *NoopService) Start(server *p2p.Server) error {
|
func (t *NoopService) Start(server *p2p.Server) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Stop is not implemented
|
||||||
func (t *NoopService) Stop() error {
|
func (t *NoopService) Stop() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// VerifyRing will verify the ring network
|
||||||
func VerifyRing(t *testing.T, net *Network, ids []enode.ID) {
|
func VerifyRing(t *testing.T, net *Network, ids []enode.ID) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
n := len(ids)
|
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) {
|
func VerifyChain(t *testing.T, net *Network, ids []enode.ID) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
n := len(ids)
|
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) {
|
func VerifyFull(t *testing.T, net *Network, ids []enode.ID) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
n := len(ids)
|
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) {
|
func VerifyStar(t *testing.T, net *Network, ids []enode.ID, centerIndex int) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
n := len(ids)
|
n := len(ids)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue