mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-17 17:33:47 +00:00
p2p/sim, swarm/network: configurable EnableMsgEvents, and reduced indirection when creating Simulation Nodes
This commit is contained in:
parent
26390b4021
commit
bcab1fc348
11 changed files with 61 additions and 54 deletions
|
|
@ -107,7 +107,7 @@ func (s *SimAdapter) NewNode(config *NodeConfig) (Node, error) {
|
||||||
MaxPeers: math.MaxInt32,
|
MaxPeers: math.MaxInt32,
|
||||||
NoDiscovery: true,
|
NoDiscovery: true,
|
||||||
Dialer: s,
|
Dialer: s,
|
||||||
EnableMsgEvents: true,
|
EnableMsgEvents: config.EnableMsgEvents,
|
||||||
},
|
},
|
||||||
NoUSB: true,
|
NoUSB: true,
|
||||||
Logger: log.New("node.id", id.String()),
|
Logger: log.New("node.id", id.String()),
|
||||||
|
|
|
||||||
|
|
@ -105,21 +105,23 @@ type NodeConfig struct {
|
||||||
// nodeConfigJSON is used to encode and decode NodeConfig as JSON by encoding
|
// nodeConfigJSON is used to encode and decode NodeConfig as JSON by encoding
|
||||||
// all fields as strings
|
// all fields as strings
|
||||||
type nodeConfigJSON struct {
|
type nodeConfigJSON struct {
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
PrivateKey string `json:"private_key"`
|
PrivateKey string `json:"private_key"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Services []string `json:"services"`
|
Services []string `json:"services"`
|
||||||
Port uint16 `json:"port"`
|
EnableMsgEvents bool `json:"enable_msg_events"`
|
||||||
|
Port uint16 `json:"port"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// MarshalJSON implements the json.Marshaler interface by encoding the config
|
// MarshalJSON implements the json.Marshaler interface by encoding the config
|
||||||
// fields as strings
|
// fields as strings
|
||||||
func (n *NodeConfig) MarshalJSON() ([]byte, error) {
|
func (n *NodeConfig) MarshalJSON() ([]byte, error) {
|
||||||
confJSON := nodeConfigJSON{
|
confJSON := nodeConfigJSON{
|
||||||
ID: n.ID.String(),
|
ID: n.ID.String(),
|
||||||
Name: n.Name,
|
Name: n.Name,
|
||||||
Services: n.Services,
|
Services: n.Services,
|
||||||
Port: n.Port,
|
Port: n.Port,
|
||||||
|
EnableMsgEvents: n.EnableMsgEvents,
|
||||||
}
|
}
|
||||||
if n.PrivateKey != nil {
|
if n.PrivateKey != nil {
|
||||||
confJSON.PrivateKey = hex.EncodeToString(crypto.FromECDSA(n.PrivateKey))
|
confJSON.PrivateKey = hex.EncodeToString(crypto.FromECDSA(n.PrivateKey))
|
||||||
|
|
@ -158,6 +160,7 @@ func (n *NodeConfig) UnmarshalJSON(data []byte) error {
|
||||||
n.Name = confJSON.Name
|
n.Name = confJSON.Name
|
||||||
n.Services = confJSON.Services
|
n.Services = confJSON.Services
|
||||||
n.Port = confJSON.Port
|
n.Port = confJSON.Port
|
||||||
|
n.EnableMsgEvents = confJSON.EnableMsgEvents
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
@ -176,10 +179,11 @@ func RandomNodeConfig() *NodeConfig {
|
||||||
panic("unable to assign tcp port")
|
panic("unable to assign tcp port")
|
||||||
}
|
}
|
||||||
return &NodeConfig{
|
return &NodeConfig{
|
||||||
ID: id,
|
ID: id,
|
||||||
Name: fmt.Sprintf("node_%s", id.String()),
|
Name: fmt.Sprintf("node_%s", id.String()),
|
||||||
PrivateKey: key,
|
PrivateKey: key,
|
||||||
Port: port,
|
Port: port,
|
||||||
|
EnableMsgEvents: true,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -26,6 +26,7 @@ import (
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/log"
|
"github.com/ethereum/go-ethereum/log"
|
||||||
"github.com/ethereum/go-ethereum/p2p/discover"
|
"github.com/ethereum/go-ethereum/p2p/discover"
|
||||||
|
"github.com/ethereum/go-ethereum/p2p/simulations/adapters"
|
||||||
)
|
)
|
||||||
|
|
||||||
//a map of mocker names to its function
|
//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) {
|
func connectNodesInRing(net *Network, nodeCount int) ([]discover.NodeID, error) {
|
||||||
ids := make([]discover.NodeID, nodeCount)
|
ids := make([]discover.NodeID, nodeCount)
|
||||||
for i := 0; i < nodeCount; i++ {
|
for i := 0; i < nodeCount; i++ {
|
||||||
node, err := net.NewNode()
|
conf := adapters.RandomNodeConfig()
|
||||||
|
node, err := net.NewNodeWithConfig(conf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Error creating a node! %s", err)
|
log.Error("Error creating a node! %s", err)
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
|
||||||
|
|
@ -78,13 +78,6 @@ func (self *Network) Events() *event.Feed {
|
||||||
return &self.events
|
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,
|
// 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
|
// returning an error if a node with the same ID or name already exists
|
||||||
func (self *Network) NewNodeWithConfig(conf *adapters.NodeConfig) (*Node, error) {
|
func (self *Network) NewNodeWithConfig(conf *adapters.NodeConfig) (*Node, error) {
|
||||||
|
|
|
||||||
|
|
@ -41,7 +41,8 @@ func TestNetworkSimulation(t *testing.T) {
|
||||||
nodeCount := 20
|
nodeCount := 20
|
||||||
ids := make([]discover.NodeID, nodeCount)
|
ids := make([]discover.NodeID, nodeCount)
|
||||||
for i := 0; i < nodeCount; i++ {
|
for i := 0; i < nodeCount; i++ {
|
||||||
node, err := network.NewNode()
|
conf := adapters.RandomNodeConfig()
|
||||||
|
node, err := network.NewNodeWithConfig(conf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("error creating node: %s", err)
|
t.Fatalf("error creating node: %s", err)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -164,7 +164,8 @@ func discoverySimulation(nodes, conns int, adapter adapters.NodeAdapter) (*simul
|
||||||
trigger := make(chan discover.NodeID)
|
trigger := make(chan discover.NodeID)
|
||||||
ids := make([]discover.NodeID, nodes)
|
ids := make([]discover.NodeID, nodes)
|
||||||
for i := 0; i < nodes; i++ {
|
for i := 0; i < nodes; i++ {
|
||||||
node, err := net.NewNode()
|
conf := adapters.RandomNodeConfig()
|
||||||
|
node, err := net.NewNodeWithConfig(conf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error starting node: %s", err)
|
return nil, fmt.Errorf("error starting node: %s", err)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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, true)
|
||||||
testDeliveryFromNodes(t, 2, 1, dataChunkCount, false)
|
testDeliveryFromNodes(t, 2, 1, dataChunkCount, false)
|
||||||
testDeliveryFromNodes(t, 4, 1, dataChunkCount, true)
|
testDeliveryFromNodes(t, 4, 1, dataChunkCount, true)
|
||||||
|
|
@ -321,11 +321,12 @@ func testDeliveryFromNodes(t *testing.T, nodes, conns, chunkCount int, skipCheck
|
||||||
defaultSkipCheck = skipCheck
|
defaultSkipCheck = skipCheck
|
||||||
toAddr = network.NewAddrFromNodeID
|
toAddr = network.NewAddrFromNodeID
|
||||||
conf := &streamTesting.RunConfig{
|
conf := &streamTesting.RunConfig{
|
||||||
Adapter: *adapter,
|
Adapter: *adapter,
|
||||||
NodeCount: nodes,
|
NodeCount: nodes,
|
||||||
ConnLevel: conns,
|
ConnLevel: conns,
|
||||||
ToAddr: toAddr,
|
ToAddr: toAddr,
|
||||||
Services: services,
|
Services: services,
|
||||||
|
EnableMsgEvents: false,
|
||||||
}
|
}
|
||||||
|
|
||||||
sim, teardown, err := streamTesting.NewSimulation(conf)
|
sim, teardown, err := streamTesting.NewSimulation(conf)
|
||||||
|
|
@ -495,11 +496,12 @@ func benchmarkDeliveryFromNodes(b *testing.B, nodes, conns, chunkCount int, skip
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
conf := &streamTesting.RunConfig{
|
conf := &streamTesting.RunConfig{
|
||||||
Adapter: *adapter,
|
Adapter: *adapter,
|
||||||
NodeCount: nodes,
|
NodeCount: nodes,
|
||||||
ConnLevel: conns,
|
ConnLevel: conns,
|
||||||
ToAddr: toAddr,
|
ToAddr: toAddr,
|
||||||
Services: services,
|
Services: services,
|
||||||
|
EnableMsgEvents: false,
|
||||||
}
|
}
|
||||||
sim, teardown, err := streamTesting.NewSimulation(conf)
|
sim, teardown, err := streamTesting.NewSimulation(conf)
|
||||||
defer teardown()
|
defer teardown()
|
||||||
|
|
|
||||||
|
|
@ -36,7 +36,7 @@ import (
|
||||||
|
|
||||||
const dataChunkCount = 500
|
const dataChunkCount = 500
|
||||||
|
|
||||||
func XTestSyncerSimulation(t *testing.T) {
|
func TestSyncerSimulation(t *testing.T) {
|
||||||
testSyncBetweenNodes(t, 2, 1, dataChunkCount, true, 1)
|
testSyncBetweenNodes(t, 2, 1, dataChunkCount, true, 1)
|
||||||
testSyncBetweenNodes(t, 4, 1, dataChunkCount, true, 1)
|
testSyncBetweenNodes(t, 4, 1, dataChunkCount, true, 1)
|
||||||
testSyncBetweenNodes(t, 8, 1, dataChunkCount, true, 1)
|
testSyncBetweenNodes(t, 8, 1, dataChunkCount, true, 1)
|
||||||
|
|
@ -51,11 +51,12 @@ func testSyncBetweenNodes(t *testing.T, nodes, conns, chunkCount int, skipCheck
|
||||||
return addr
|
return addr
|
||||||
}
|
}
|
||||||
conf := &streamTesting.RunConfig{
|
conf := &streamTesting.RunConfig{
|
||||||
Adapter: *adapter,
|
Adapter: *adapter,
|
||||||
NodeCount: nodes,
|
NodeCount: nodes,
|
||||||
ConnLevel: conns,
|
ConnLevel: conns,
|
||||||
ToAddr: toAddr,
|
ToAddr: toAddr,
|
||||||
Services: services,
|
Services: services,
|
||||||
|
EnableMsgEvents: false,
|
||||||
}
|
}
|
||||||
// create context for simulation run
|
// create context for simulation run
|
||||||
timeout := 30 * time.Second
|
timeout := 30 * time.Second
|
||||||
|
|
|
||||||
|
|
@ -117,12 +117,13 @@ func CheckResult(t *testing.T, result *simulations.StepResult, startedAt, finish
|
||||||
}
|
}
|
||||||
|
|
||||||
type RunConfig struct {
|
type RunConfig struct {
|
||||||
Adapter string
|
Adapter string
|
||||||
Step *simulations.Step
|
Step *simulations.Step
|
||||||
NodeCount int
|
NodeCount int
|
||||||
ConnLevel int
|
ConnLevel int
|
||||||
ToAddr func(discover.NodeID) *network.BzzAddr
|
ToAddr func(discover.NodeID) *network.BzzAddr
|
||||||
Services adapters.Services
|
Services adapters.Services
|
||||||
|
EnableMsgEvents bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSimulation(conf *RunConfig) (*Simulation, func(), error) {
|
func NewSimulation(conf *RunConfig) (*Simulation, func(), error) {
|
||||||
|
|
@ -144,7 +145,9 @@ func NewSimulation(conf *RunConfig) (*Simulation, func(), error) {
|
||||||
addrs := make([]network.Addr, nodes)
|
addrs := make([]network.Addr, nodes)
|
||||||
// start nodes
|
// start nodes
|
||||||
for i := 0; i < nodes; i++ {
|
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 {
|
if err != nil {
|
||||||
return nil, teardown, fmt.Errorf("error creating node: %s", err)
|
return nil, teardown, fmt.Errorf("error creating node: %s", err)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -180,9 +180,9 @@ func setupNetwork(numnodes int) (clients []*rpc.Client, err error) {
|
||||||
DefaultService: "bzz",
|
DefaultService: "bzz",
|
||||||
})
|
})
|
||||||
for i := 0; i < numnodes; i++ {
|
for i := 0; i < numnodes; i++ {
|
||||||
nodes[i], err = net.NewNodeWithConfig(&adapters.NodeConfig{
|
nodeconf := adapters.RandomNodeConfig()
|
||||||
Services: []string{"bzz", "pss"},
|
nodeconf.Services = []string{"bzz", "pss"}
|
||||||
})
|
nodes[i], err = net.NewNodeWithConfig(nodeconf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error creating node 1: %v", err)
|
return nil, fmt.Errorf("error creating node 1: %v", err)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1081,9 +1081,9 @@ func setupNetwork(numnodes int) (clients []*rpc.Client, err error) {
|
||||||
DefaultService: "bzz",
|
DefaultService: "bzz",
|
||||||
})
|
})
|
||||||
for i := 0; i < numnodes; i++ {
|
for i := 0; i < numnodes; i++ {
|
||||||
nodes[i], err = net.NewNodeWithConfig(&adapters.NodeConfig{
|
nodeconf := adapters.RandomNodeConfig()
|
||||||
Services: []string{"bzz", pssProtocolName},
|
nodeconf.Services = []string{"bzz", pssProtocolName}
|
||||||
})
|
nodes[i], err = net.NewNodeWithConfig(nodeconf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error creating node 1: %v", err)
|
return nil, fmt.Errorf("error creating node 1: %v", err)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue