Skip to content
This repository has been archived by the owner on Aug 2, 2021. It is now read-only.

Commit

Permalink
Updated to match approved go-ethereum PR + Resolve requested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
chadsr committed Oct 17, 2019
1 parent 7ea04a4 commit f82120d
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 65 deletions.
48 changes: 23 additions & 25 deletions network/simulation/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ import (

var (
BucketKeyBzzPrivateKey BucketKey = "bzzprivkey"

// PropertyBootnode is a property string for NodeConfig, representing that a node is a bootnode
PropertyBootnode = "bootnode"
)

// NodeIDs returns NodeIDs for all nodes in the network.
Expand Down Expand Up @@ -83,10 +86,11 @@ func AddNodeWithMsgEvents(enable bool) AddNodeOption {
}
}

// AddNodeAsBootNode toggles whether the node will be configured as a bootnode
func AddNodeAsBootNode(enable bool) AddNodeOption {
// AddNodeWithProperty specifies a property that this node should hold
// in the running services. (e.g. "bootnode", etc)
func AddNodeWithProperty(propertyName string) AddNodeOption {
return func(o *adapters.NodeConfig) {
o.BootNode = enable
o.Properties = append(o.Properties, propertyName)
}
}

Expand Down Expand Up @@ -122,7 +126,13 @@ func (s *Simulation) AddNode(opts ...AddNodeOption) (id enode.ID, err error) {

enodeParams := &network.EnodeParams{
PrivateKey: bzzPrivateKey,
Bootnode: conf.BootNode,
}

// Check for any properties relevant to the creation of the Enode Record
for _, property := range conf.Properties {
if property == PropertyBootnode {
enodeParams.Bootnode = true
}
}

record, err := network.NewEnodeRecord(enodeParams)
Expand Down Expand Up @@ -153,9 +163,9 @@ func (s *Simulation) AddNodes(count int, opts ...AddNodeOption) (ids []enode.ID,
return ids, nil
}

// AddBootNode creates a bootnode using AddNode(opts) and appends it to Simulation.bootNodes
func (s *Simulation) AddBootNode(opts ...AddNodeOption) (id enode.ID, err error) {
opts = append(opts, AddNodeAsBootNode(true))
// AddBootnode creates a bootnode using AddNode(opts) and appends it to Simulation.bootNodes
func (s *Simulation) AddBootnode(opts ...AddNodeOption) (id enode.ID, err error) {
opts = append(opts, AddNodeWithProperty(PropertyBootnode))
id, err = s.AddNode(opts...)
if err != nil {
return id, err
Expand All @@ -164,19 +174,7 @@ func (s *Simulation) AddBootNode(opts ...AddNodeOption) (id enode.ID, err error)
return id, nil
}

// AddBootNodes creates count number of bootnodes using AddNodes(count, opts)
// and appends them to Simulation.bootNodes
func (s *Simulation) AddBootNodes(count int, opts ...AddNodeOption) (ids []enode.ID, err error) {
opts = append(opts, AddNodeAsBootNode(true))
ids, err = s.AddNodes(count, opts...)
if err != nil {
return nil, err
}

return ids, err
}

// AddNodesAndConnectFull is a helpper method that combines
// AddNodesAndConnectFull is a helper method that combines
// AddNodes and ConnectNodesFull. Only new nodes will be connected.
func (s *Simulation) AddNodesAndConnectFull(count int, opts ...AddNodeOption) (ids []enode.ID, err error) {
if count < 2 {
Expand Down Expand Up @@ -237,7 +235,7 @@ func (s *Simulation) AddNodesAndConnectRing(count int, opts ...AddNodeOption) (i
return ids, nil
}

// AddNodesAndConnectStar is a helpper method that combines
// AddNodesAndConnectStar is a helper method that combines
// AddNodes and ConnectNodesStar.
func (s *Simulation) AddNodesAndConnectStar(count int, opts ...AddNodeOption) (ids []enode.ID, err error) {
if count < 2 {
Expand All @@ -254,11 +252,11 @@ func (s *Simulation) AddNodesAndConnectStar(count int, opts ...AddNodeOption) (i
return ids, nil
}

// AddNodesAndConnectToBootNode is a helper method that combines
// AddNodes, AddBootNode and ConnectNodesStar, where the center node is a new bootnode.
// AddNodesAndConnectToBootnode is a helper method that combines
// AddNodes, AddBootnode and ConnectNodesStar, where the center node is a new bootnode.
// The count parameter excludes the bootnode.
func (s *Simulation) AddNodesAndConnectToBootNode(count int, opts ...AddNodeOption) (ids []enode.ID, bootNodeID enode.ID, err error) {
bootNodeID, err = s.AddBootNode(opts...)
func (s *Simulation) AddNodesAndConnectToBootnode(count int, opts ...AddNodeOption) (ids []enode.ID, bootNodeID enode.ID, err error) {
bootNodeID, err = s.AddBootnode(opts...)
if err != nil {
return nil, bootNodeID, err
}
Expand Down
58 changes: 19 additions & 39 deletions network/simulation/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,62 +219,42 @@ func TestAddNodes(t *testing.T) {
}
}

func TestAddBootNode(t *testing.T) {
// TestAddBootnode adds a bootnode to a simulation network
// and checks that the bootnode is returned by GetNodeIDsByProperty with PropertyBootnode
// If other nodes are returned, or the bootnode ID is incorrect, the test fails
func TestAddBootnode(t *testing.T) {
sim := NewInProc(noopServiceFuncMap)
defer sim.Close()

id, err := sim.AddBootNode()
// Add some normal nodes for the sake of the bootnode not being the only node
_, err := sim.AddNodes(4)

bootnodeID, err := sim.AddBootnode()
if err != nil {
t.Fatalf("Failed to add bootnode: %s", err)
}

bootNode := sim.Net.GetNode(id)
if !bootNode.Config.BootNode {
t.Fatalf("Bootnode did not have the respective flag in its config")
gotBootnodeIDs := sim.Net.GetNodeIDsByProperty(PropertyBootnode)
if len(gotBootnodeIDs) != 1 {
t.Fatalf("Expected 1 bootnode, got %d", len(gotBootnodeIDs))
}

for _, node := range sim.Net.GetBootNodes() {
if !bytes.Equal(node.ID().Bytes(), bootNode.ID().Bytes()) {
t.Fatalf("Found an unexpected bootnode with ID: %s", node.ID().String())
}
}
}

func TestAddBootNodes(t *testing.T) {
bootNodeCount := 10

sim := NewInProc(noopServiceFuncMap)
defer sim.Close()

ids, err := sim.AddBootNodes(bootNodeCount)
if err != nil {
t.Fatalf("Failed to add bootnodes: %s", err)
}

bootNodes := sim.Net.GetBootNodes()
for _, id := range ids {
match := false
for _, node := range bootNodes {
if bytes.Equal(id.Bytes(), node.ID().Bytes()) {
match = true
}
}

if !match {
t.Fatalf("Added a bootnode with enode.ID, %s, but it was not found via GetBootNodes", id.String())
}
if !bytes.Equal(gotBootnodeIDs[0].Bytes(), bootnodeID.Bytes()) {
t.Fatalf("Found an unexpected bootnode with ID: %s", gotBootnodeIDs[0].String())
}
}

func TestAddNodesAndConnectToBootNode(t *testing.T) {
nodeCount := 20
// TestAddNodesAndConnectToBootnode adds nodeCount nodes, and a bootnode in a star topology
// VerifyStar is then used to confirm that the nodes connected to the bootnode as expected
func TestAddNodesAndConnectToBootnode(t *testing.T) {
nodeCount := 5

sim := NewInProc(noopServiceFuncMap)
defer sim.Close()

ids, bootNodeID, err := sim.AddNodesAndConnectToBootNode(nodeCount)
ids, bootNodeID, err := sim.AddNodesAndConnectToBootnode(nodeCount)
if err != nil {
t.Fatalf("AddNodesAndConnectToBootNode Failed: %s", err)
t.Fatalf("AddNodesAndConnectToBootnode Failed: %s", err)
}

// VerifyStar takes a map and an index to the bootnode, so append it and use the index
Expand Down
10 changes: 9 additions & 1 deletion network/simulation/simulation.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,16 @@ func NewBzzInProc(services map[string]ServiceFunc, disableAutoConnect bool) (s *
OverlayAddr: addr.Over(),
UnderlayAddr: addr.Under(),
HiveParams: hp,
BootnodeMode: ctx.Config.BootNode,
}

// Check for relevant properties
for _, property := range ctx.Config.Properties {
switch property {
case PropertyBootnode:
config.BootnodeMode = true
}
}

return network.NewBzz(config, kad, nil, nil, nil, nil, nil), nil, nil
}

Expand Down

0 comments on commit f82120d

Please sign in to comment.