diff options
author | Elad <theman@elad.im> | 2018-12-17 19:19:01 +0800 |
---|---|---|
committer | Anton Evangelatov <anton.evangelatov@gmail.com> | 2018-12-17 19:19:01 +0800 |
commit | 472c23a8015cd84b193d2d0efb4592a664de3c62 (patch) | |
tree | 2344bab40a7328567179ec889b2432039cd28cf4 /swarm/network/simulation/node.go | |
parent | d322c9d5504ad3fd4dfeb9dff29e92b35856042f (diff) | |
download | dexon-472c23a8015cd84b193d2d0efb4592a664de3c62.tar dexon-472c23a8015cd84b193d2d0efb4592a664de3c62.tar.gz dexon-472c23a8015cd84b193d2d0efb4592a664de3c62.tar.bz2 dexon-472c23a8015cd84b193d2d0efb4592a664de3c62.tar.lz dexon-472c23a8015cd84b193d2d0efb4592a664de3c62.tar.xz dexon-472c23a8015cd84b193d2d0efb4592a664de3c62.tar.zst dexon-472c23a8015cd84b193d2d0efb4592a664de3c62.zip |
p2p/simulation: move connection methods from swarm/network/simulation (#18323)
Diffstat (limited to 'swarm/network/simulation/node.go')
-rw-r--r-- | swarm/network/simulation/node.go | 64 |
1 files changed, 15 insertions, 49 deletions
diff --git a/swarm/network/simulation/node.go b/swarm/network/simulation/node.go index a916d3fc2..24b659976 100644 --- a/swarm/network/simulation/node.go +++ b/swarm/network/simulation/node.go @@ -127,7 +127,7 @@ func (s *Simulation) AddNodesAndConnectFull(count int, opts ...AddNodeOption) (i if err != nil { return nil, err } - err = s.ConnectNodesFull(ids) + err = s.Net.ConnectNodesFull(ids) if err != nil { return nil, err } @@ -145,7 +145,7 @@ func (s *Simulation) AddNodesAndConnectChain(count int, opts ...AddNodeOption) ( if err != nil { return nil, err } - err = s.ConnectToLastNode(id) + err = s.Net.ConnectToLastNode(id) if err != nil { return nil, err } @@ -154,7 +154,7 @@ func (s *Simulation) AddNodesAndConnectChain(count int, opts ...AddNodeOption) ( return nil, err } ids = append([]enode.ID{id}, ids...) - err = s.ConnectNodesChain(ids) + err = s.Net.ConnectNodesChain(ids) if err != nil { return nil, err } @@ -171,7 +171,7 @@ func (s *Simulation) AddNodesAndConnectRing(count int, opts ...AddNodeOption) (i if err != nil { return nil, err } - err = s.ConnectNodesRing(ids) + err = s.Net.ConnectNodesRing(ids) if err != nil { return nil, err } @@ -188,7 +188,7 @@ func (s *Simulation) AddNodesAndConnectStar(count int, opts ...AddNodeOption) (i if err != nil { return nil, err } - err = s.ConnectNodesStar(ids[0], ids[1:]) + err = s.Net.ConnectNodesStar(ids[0], ids[1:]) if err != nil { return nil, err } @@ -267,27 +267,26 @@ func (s *Simulation) StartNode(id enode.ID) (err error) { // StartRandomNode starts a random node. func (s *Simulation) StartRandomNode() (id enode.ID, err error) { - n := s.randomDownNode() + n := s.Net.GetRandomDownNode() if n == nil { return id, ErrNodeNotFound } - return n.ID, s.Net.Start(n.ID) + return n.ID(), s.Net.Start(n.ID()) } // StartRandomNodes starts random nodes. func (s *Simulation) StartRandomNodes(count int) (ids []enode.ID, err error) { ids = make([]enode.ID, 0, count) - downIDs := s.DownNodeIDs() for i := 0; i < count; i++ { - n := s.randomNode(downIDs, ids...) + n := s.Net.GetRandomDownNode() if n == nil { return nil, ErrNodeNotFound } - err = s.Net.Start(n.ID) + err = s.Net.Start(n.ID()) if err != nil { return nil, err } - ids = append(ids, n.ID) + ids = append(ids, n.ID()) } return ids, nil } @@ -299,27 +298,26 @@ func (s *Simulation) StopNode(id enode.ID) (err error) { // StopRandomNode stops a random node. func (s *Simulation) StopRandomNode() (id enode.ID, err error) { - n := s.RandomUpNode() + n := s.Net.GetRandomUpNode() if n == nil { return id, ErrNodeNotFound } - return n.ID, s.Net.Stop(n.ID) + return n.ID(), s.Net.Stop(n.ID()) } // StopRandomNodes stops random nodes. func (s *Simulation) StopRandomNodes(count int) (ids []enode.ID, err error) { ids = make([]enode.ID, 0, count) - upIDs := s.UpNodeIDs() for i := 0; i < count; i++ { - n := s.randomNode(upIDs, ids...) + n := s.Net.GetRandomUpNode() if n == nil { return nil, ErrNodeNotFound } - err = s.Net.Stop(n.ID) + err = s.Net.Stop(n.ID()) if err != nil { return nil, err } - ids = append(ids, n.ID) + ids = append(ids, n.ID()) } return ids, nil } @@ -328,35 +326,3 @@ func (s *Simulation) StopRandomNodes(count int) (ids []enode.ID, err error) { func init() { rand.Seed(time.Now().UnixNano()) } - -// RandomUpNode returns a random SimNode that is up. -// Arguments are NodeIDs for nodes that should not be returned. -func (s *Simulation) RandomUpNode(exclude ...enode.ID) *adapters.SimNode { - return s.randomNode(s.UpNodeIDs(), exclude...) -} - -// randomDownNode returns a random SimNode that is not up. -func (s *Simulation) randomDownNode(exclude ...enode.ID) *adapters.SimNode { - return s.randomNode(s.DownNodeIDs(), exclude...) -} - -// randomNode returns a random SimNode from the slice of NodeIDs. -func (s *Simulation) randomNode(ids []enode.ID, exclude ...enode.ID) *adapters.SimNode { - for _, e := range exclude { - var i int - for _, id := range ids { - if id == e { - ids = append(ids[:i], ids[i+1:]...) - } else { - i++ - } - } - } - l := len(ids) - if l == 0 { - return nil - } - n := s.Net.GetNode(ids[rand.Intn(l)]) - node, _ := n.Node.(*adapters.SimNode) - return node -} |