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

Commit f82120d

Browse files
committed
Updated to match approved go-ethereum PR + Resolve requested changes
1 parent 7ea04a4 commit f82120d

File tree

3 files changed

+51
-65
lines changed

3 files changed

+51
-65
lines changed

network/simulation/node.go

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ import (
3737

3838
var (
3939
BucketKeyBzzPrivateKey BucketKey = "bzzprivkey"
40+
41+
// PropertyBootnode is a property string for NodeConfig, representing that a node is a bootnode
42+
PropertyBootnode = "bootnode"
4043
)
4144

4245
// NodeIDs returns NodeIDs for all nodes in the network.
@@ -83,10 +86,11 @@ func AddNodeWithMsgEvents(enable bool) AddNodeOption {
8386
}
8487
}
8588

86-
// AddNodeAsBootNode toggles whether the node will be configured as a bootnode
87-
func AddNodeAsBootNode(enable bool) AddNodeOption {
89+
// AddNodeWithProperty specifies a property that this node should hold
90+
// in the running services. (e.g. "bootnode", etc)
91+
func AddNodeWithProperty(propertyName string) AddNodeOption {
8892
return func(o *adapters.NodeConfig) {
89-
o.BootNode = enable
93+
o.Properties = append(o.Properties, propertyName)
9094
}
9195
}
9296

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

123127
enodeParams := &network.EnodeParams{
124128
PrivateKey: bzzPrivateKey,
125-
Bootnode: conf.BootNode,
129+
}
130+
131+
// Check for any properties relevant to the creation of the Enode Record
132+
for _, property := range conf.Properties {
133+
if property == PropertyBootnode {
134+
enodeParams.Bootnode = true
135+
}
126136
}
127137

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

156-
// AddBootNode creates a bootnode using AddNode(opts) and appends it to Simulation.bootNodes
157-
func (s *Simulation) AddBootNode(opts ...AddNodeOption) (id enode.ID, err error) {
158-
opts = append(opts, AddNodeAsBootNode(true))
166+
// AddBootnode creates a bootnode using AddNode(opts) and appends it to Simulation.bootNodes
167+
func (s *Simulation) AddBootnode(opts ...AddNodeOption) (id enode.ID, err error) {
168+
opts = append(opts, AddNodeWithProperty(PropertyBootnode))
159169
id, err = s.AddNode(opts...)
160170
if err != nil {
161171
return id, err
@@ -164,19 +174,7 @@ func (s *Simulation) AddBootNode(opts ...AddNodeOption) (id enode.ID, err error)
164174
return id, nil
165175
}
166176

167-
// AddBootNodes creates count number of bootnodes using AddNodes(count, opts)
168-
// and appends them to Simulation.bootNodes
169-
func (s *Simulation) AddBootNodes(count int, opts ...AddNodeOption) (ids []enode.ID, err error) {
170-
opts = append(opts, AddNodeAsBootNode(true))
171-
ids, err = s.AddNodes(count, opts...)
172-
if err != nil {
173-
return nil, err
174-
}
175-
176-
return ids, err
177-
}
178-
179-
// AddNodesAndConnectFull is a helpper method that combines
177+
// AddNodesAndConnectFull is a helper method that combines
180178
// AddNodes and ConnectNodesFull. Only new nodes will be connected.
181179
func (s *Simulation) AddNodesAndConnectFull(count int, opts ...AddNodeOption) (ids []enode.ID, err error) {
182180
if count < 2 {
@@ -237,7 +235,7 @@ func (s *Simulation) AddNodesAndConnectRing(count int, opts ...AddNodeOption) (i
237235
return ids, nil
238236
}
239237

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

257-
// AddNodesAndConnectToBootNode is a helper method that combines
258-
// AddNodes, AddBootNode and ConnectNodesStar, where the center node is a new bootnode.
255+
// AddNodesAndConnectToBootnode is a helper method that combines
256+
// AddNodes, AddBootnode and ConnectNodesStar, where the center node is a new bootnode.
259257
// The count parameter excludes the bootnode.
260-
func (s *Simulation) AddNodesAndConnectToBootNode(count int, opts ...AddNodeOption) (ids []enode.ID, bootNodeID enode.ID, err error) {
261-
bootNodeID, err = s.AddBootNode(opts...)
258+
func (s *Simulation) AddNodesAndConnectToBootnode(count int, opts ...AddNodeOption) (ids []enode.ID, bootNodeID enode.ID, err error) {
259+
bootNodeID, err = s.AddBootnode(opts...)
262260
if err != nil {
263261
return nil, bootNodeID, err
264262
}

network/simulation/node_test.go

Lines changed: 19 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -219,62 +219,42 @@ func TestAddNodes(t *testing.T) {
219219
}
220220
}
221221

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

226-
id, err := sim.AddBootNode()
229+
// Add some normal nodes for the sake of the bootnode not being the only node
230+
_, err := sim.AddNodes(4)
231+
232+
bootnodeID, err := sim.AddBootnode()
227233
if err != nil {
228234
t.Fatalf("Failed to add bootnode: %s", err)
229235
}
230236

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

236-
for _, node := range sim.Net.GetBootNodes() {
237-
if !bytes.Equal(node.ID().Bytes(), bootNode.ID().Bytes()) {
238-
t.Fatalf("Found an unexpected bootnode with ID: %s", node.ID().String())
239-
}
240-
}
241-
}
242-
243-
func TestAddBootNodes(t *testing.T) {
244-
bootNodeCount := 10
245-
246-
sim := NewInProc(noopServiceFuncMap)
247-
defer sim.Close()
248-
249-
ids, err := sim.AddBootNodes(bootNodeCount)
250-
if err != nil {
251-
t.Fatalf("Failed to add bootnodes: %s", err)
252-
}
253-
254-
bootNodes := sim.Net.GetBootNodes()
255-
for _, id := range ids {
256-
match := false
257-
for _, node := range bootNodes {
258-
if bytes.Equal(id.Bytes(), node.ID().Bytes()) {
259-
match = true
260-
}
261-
}
262-
263-
if !match {
264-
t.Fatalf("Added a bootnode with enode.ID, %s, but it was not found via GetBootNodes", id.String())
265-
}
242+
if !bytes.Equal(gotBootnodeIDs[0].Bytes(), bootnodeID.Bytes()) {
243+
t.Fatalf("Found an unexpected bootnode with ID: %s", gotBootnodeIDs[0].String())
266244
}
267245
}
268246

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

272252
sim := NewInProc(noopServiceFuncMap)
273253
defer sim.Close()
274254

275-
ids, bootNodeID, err := sim.AddNodesAndConnectToBootNode(nodeCount)
255+
ids, bootNodeID, err := sim.AddNodesAndConnectToBootnode(nodeCount)
276256
if err != nil {
277-
t.Fatalf("AddNodesAndConnectToBootNode Failed: %s", err)
257+
t.Fatalf("AddNodesAndConnectToBootnode Failed: %s", err)
278258
}
279259

280260
// VerifyStar takes a map and an index to the bootnode, so append it and use the index

network/simulation/simulation.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,16 @@ func NewBzzInProc(services map[string]ServiceFunc, disableAutoConnect bool) (s *
117117
OverlayAddr: addr.Over(),
118118
UnderlayAddr: addr.Under(),
119119
HiveParams: hp,
120-
BootnodeMode: ctx.Config.BootNode,
121120
}
121+
122+
// Check for relevant properties
123+
for _, property := range ctx.Config.Properties {
124+
switch property {
125+
case PropertyBootnode:
126+
config.BootnodeMode = true
127+
}
128+
}
129+
122130
return network.NewBzz(config, kad, nil, nil, nil, nil, nil), nil, nil
123131
}
124132

0 commit comments

Comments
 (0)