p2p/sim, swarm/network: configurable EnableMsgEvents, and reduced indirection when creating Simulation Nodes

This commit is contained in:
Anton Evangelatov 2018-02-16 15:56:24 +01:00
parent 26390b4021
commit bcab1fc348
11 changed files with 61 additions and 54 deletions

View file

@ -107,7 +107,7 @@ func (s *SimAdapter) NewNode(config *NodeConfig) (Node, error) {
MaxPeers: math.MaxInt32,
NoDiscovery: true,
Dialer: s,
EnableMsgEvents: true,
EnableMsgEvents: config.EnableMsgEvents,
},
NoUSB: true,
Logger: log.New("node.id", id.String()),

View file

@ -109,6 +109,7 @@ type nodeConfigJSON struct {
PrivateKey string `json:"private_key"`
Name string `json:"name"`
Services []string `json:"services"`
EnableMsgEvents bool `json:"enable_msg_events"`
Port uint16 `json:"port"`
}
@ -120,6 +121,7 @@ func (n *NodeConfig) MarshalJSON() ([]byte, error) {
Name: n.Name,
Services: n.Services,
Port: n.Port,
EnableMsgEvents: n.EnableMsgEvents,
}
if n.PrivateKey != nil {
confJSON.PrivateKey = hex.EncodeToString(crypto.FromECDSA(n.PrivateKey))
@ -158,6 +160,7 @@ func (n *NodeConfig) UnmarshalJSON(data []byte) error {
n.Name = confJSON.Name
n.Services = confJSON.Services
n.Port = confJSON.Port
n.EnableMsgEvents = confJSON.EnableMsgEvents
return nil
}
@ -180,6 +183,7 @@ func RandomNodeConfig() *NodeConfig {
Name: fmt.Sprintf("node_%s", id.String()),
PrivateKey: key,
Port: port,
EnableMsgEvents: true,
}
}

View file

@ -26,6 +26,7 @@ import (
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/p2p/discover"
"github.com/ethereum/go-ethereum/p2p/simulations/adapters"
)
//a map of mocker names to its function
@ -165,7 +166,8 @@ func probabilistic(net *Network, quit chan struct{}, nodeCount int) {
func connectNodesInRing(net *Network, nodeCount int) ([]discover.NodeID, error) {
ids := make([]discover.NodeID, nodeCount)
for i := 0; i < nodeCount; i++ {
node, err := net.NewNode()
conf := adapters.RandomNodeConfig()
node, err := net.NewNodeWithConfig(conf)
if err != nil {
log.Error("Error creating a node! %s", err)
return nil, err

View file

@ -78,13 +78,6 @@ func (self *Network) Events() *event.Feed {
return &self.events
}
// NewNode adds a new node to the network with a random ID
func (self *Network) NewNode() (*Node, error) {
conf := adapters.RandomNodeConfig()
conf.Services = []string{self.DefaultService}
return self.NewNodeWithConfig(conf)
}
// NewNodeWithConfig adds a new node to the network with the given config,
// returning an error if a node with the same ID or name already exists
func (self *Network) NewNodeWithConfig(conf *adapters.NodeConfig) (*Node, error) {

View file

@ -41,7 +41,8 @@ func TestNetworkSimulation(t *testing.T) {
nodeCount := 20
ids := make([]discover.NodeID, nodeCount)
for i := 0; i < nodeCount; i++ {
node, err := network.NewNode()
conf := adapters.RandomNodeConfig()
node, err := network.NewNodeWithConfig(conf)
if err != nil {
t.Fatalf("error creating node: %s", err)
}

View file

@ -164,7 +164,8 @@ func discoverySimulation(nodes, conns int, adapter adapters.NodeAdapter) (*simul
trigger := make(chan discover.NodeID)
ids := make([]discover.NodeID, nodes)
for i := 0; i < nodes; i++ {
node, err := net.NewNode()
conf := adapters.RandomNodeConfig()
node, err := net.NewNodeWithConfig(conf)
if err != nil {
return nil, fmt.Errorf("error starting node: %s", err)
}

View file

@ -306,7 +306,7 @@ func TestStreamerDownstreamChunkDeliveryMsgExchange(t *testing.T) {
}
func XTestDeliveryFromNodes(t *testing.T) {
func TestDeliveryFromNodes(t *testing.T) {
testDeliveryFromNodes(t, 2, 1, dataChunkCount, true)
testDeliveryFromNodes(t, 2, 1, dataChunkCount, false)
testDeliveryFromNodes(t, 4, 1, dataChunkCount, true)
@ -326,6 +326,7 @@ func testDeliveryFromNodes(t *testing.T, nodes, conns, chunkCount int, skipCheck
ConnLevel: conns,
ToAddr: toAddr,
Services: services,
EnableMsgEvents: false,
}
sim, teardown, err := streamTesting.NewSimulation(conf)
@ -500,6 +501,7 @@ func benchmarkDeliveryFromNodes(b *testing.B, nodes, conns, chunkCount int, skip
ConnLevel: conns,
ToAddr: toAddr,
Services: services,
EnableMsgEvents: false,
}
sim, teardown, err := streamTesting.NewSimulation(conf)
defer teardown()

View file

@ -36,7 +36,7 @@ import (
const dataChunkCount = 500
func XTestSyncerSimulation(t *testing.T) {
func TestSyncerSimulation(t *testing.T) {
testSyncBetweenNodes(t, 2, 1, dataChunkCount, true, 1)
testSyncBetweenNodes(t, 4, 1, dataChunkCount, true, 1)
testSyncBetweenNodes(t, 8, 1, dataChunkCount, true, 1)
@ -56,6 +56,7 @@ func testSyncBetweenNodes(t *testing.T, nodes, conns, chunkCount int, skipCheck
ConnLevel: conns,
ToAddr: toAddr,
Services: services,
EnableMsgEvents: false,
}
// create context for simulation run
timeout := 30 * time.Second

View file

@ -123,6 +123,7 @@ type RunConfig struct {
ConnLevel int
ToAddr func(discover.NodeID) *network.BzzAddr
Services adapters.Services
EnableMsgEvents bool
}
func NewSimulation(conf *RunConfig) (*Simulation, func(), error) {
@ -144,7 +145,9 @@ func NewSimulation(conf *RunConfig) (*Simulation, func(), error) {
addrs := make([]network.Addr, nodes)
// start nodes
for i := 0; i < nodes; i++ {
node, err := net.NewNode()
nodeconf := adapters.RandomNodeConfig()
nodeconf.EnableMsgEvents = conf.EnableMsgEvents
node, err := net.NewNodeWithConfig(nodeconf)
if err != nil {
return nil, teardown, fmt.Errorf("error creating node: %s", err)
}

View file

@ -180,9 +180,9 @@ func setupNetwork(numnodes int) (clients []*rpc.Client, err error) {
DefaultService: "bzz",
})
for i := 0; i < numnodes; i++ {
nodes[i], err = net.NewNodeWithConfig(&adapters.NodeConfig{
Services: []string{"bzz", "pss"},
})
nodeconf := adapters.RandomNodeConfig()
nodeconf.Services = []string{"bzz", "pss"}
nodes[i], err = net.NewNodeWithConfig(nodeconf)
if err != nil {
return nil, fmt.Errorf("error creating node 1: %v", err)
}

View file

@ -1081,9 +1081,9 @@ func setupNetwork(numnodes int) (clients []*rpc.Client, err error) {
DefaultService: "bzz",
})
for i := 0; i < numnodes; i++ {
nodes[i], err = net.NewNodeWithConfig(&adapters.NodeConfig{
Services: []string{"bzz", pssProtocolName},
})
nodeconf := adapters.RandomNodeConfig()
nodeconf.Services = []string{"bzz", pssProtocolName}
nodes[i], err = net.NewNodeWithConfig(nodeconf)
if err != nil {
return nil, fmt.Errorf("error creating node 1: %v", err)
}