Skip to content

Commit f97ce8b

Browse files
authored
Merge pull request #35 from elecbug/elecbug/master
Update v0.10.0
2 parents ba54904 + 05a4df8 commit f97ce8b

4 files changed

Lines changed: 36 additions & 9 deletions

File tree

p2p/node.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ func (n *p2pNode) eachPublish(network *P2P, msg Message) {
137137
receivedEdges = append(receivedEdges, senderID)
138138
}
139139

140-
targets := msg.CustomProtocol(msg, allEdges, sentEdges, receivedEdges, network.cfg.CustomParams)
140+
targets := msg.CustomProtocol(n.id, msg, allEdges, sentEdges, receivedEdges, network.cfg.CustomParams)
141141

142142
for _, targetID := range *targets {
143143
for _, edge := range n.edges {

p2p/p2p.go

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ type P2P struct {
1919

2020
// GenerateP2P creates a P2P network from the given graph.
2121
// nodeLatency and edgeLatency are functions that generate latencies for nodes and edges respectively.
22-
func GenerateP2P(g *graph.Graph, nodeLatency, edgeLatency func() float64, cfg *Config) (*P2P, error) {
22+
func GenerateP2P(g *graph.Graph, nodeLatency func(src PeerID) float64, edgeLatency func(src PeerID, dst PeerID) float64, cfg *Config) (*P2P, error) {
2323
nodes := make(map[PeerID]*p2pNode)
2424
maps := make(map[graph.NodeID]PeerID)
2525

@@ -31,7 +31,7 @@ func GenerateP2P(g *graph.Graph, nodeLatency, edgeLatency func() float64, cfg *C
3131
return nil, err
3232
}
3333

34-
n := newNode(PeerID(num), nodeLatency())
34+
n := newNode(PeerID(num), nodeLatency(PeerID(num)))
3535
n.edges = make(map[PeerID]p2pEdge)
3636

3737
nodes[n.id] = n
@@ -52,7 +52,7 @@ func GenerateP2P(g *graph.Graph, nodeLatency, edgeLatency func() float64, cfg *C
5252

5353
edge := p2pEdge{
5454
targetID: PeerID(j),
55-
edgeLatency: edgeLatency(),
55+
edgeLatency: edgeLatency(PeerID(num), PeerID(j)),
5656
}
5757

5858
n.edges[edge.targetID] = edge
@@ -74,6 +74,33 @@ func (p *P2P) SimulateP2P(ctx context.Context) {
7474
wg.Wait()
7575
}
7676

77+
// ExpireSimulation runs the simulation until the reachability of the specified message stabilizes or a timeout occurs.
78+
func (p *P2P) ExpireSimulation(cancel context.CancelFunc, msg string, expirationDuration, timeoutDuration, checkInterval time.Duration) {
79+
startTime := time.Now()
80+
lastChangeTime := startTime
81+
beforeRch := p.Reachability(msg)
82+
83+
for {
84+
currentRch := p.Reachability(msg)
85+
86+
if currentRch > beforeRch {
87+
beforeRch = currentRch
88+
lastChangeTime = time.Now()
89+
}
90+
91+
if time.Since(lastChangeTime) > expirationDuration {
92+
break
93+
}
94+
if time.Since(startTime) > timeoutDuration {
95+
break
96+
}
97+
98+
time.Sleep(checkInterval)
99+
}
100+
101+
cancel()
102+
}
103+
77104
// PeerIDs returns a slice of all node IDs in the network.
78105
func (p *P2P) PeerIDs() []PeerID {
79106
ids := make([]PeerID, 0, len(p.nodes))

p2p/p2p_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ func TestGenerateNetwork(t *testing.T) {
1818
g := sg.ErdosRenyiGraph(1000, 50.000/1000, true)
1919
t.Logf("Generated graph with %d nodes and %d edges\n", len(g.Nodes()), g.EdgeCount())
2020

21-
nodeLatency := func() float64 { return p2p.LogNormalRand(math.Log(100), 0.5) }
22-
edgeLatency := func() float64 { return p2p.LogNormalRand(math.Log(100), 0.3) }
21+
nodeLatency := func(id p2p.PeerID) float64 { return p2p.LogNormalRand(math.Log(100), 0.5) }
22+
edgeLatency := func(id1, id2 p2p.PeerID) float64 { return p2p.LogNormalRand(math.Log(100), 0.3) }
2323

2424
nw, err := p2p.GenerateP2P(g, nodeLatency, edgeLatency, &p2p.Config{GossipFactor: 0.35})
2525
if err != nil {
@@ -87,8 +87,8 @@ func TestMetrics(t *testing.T) {
8787
g := sg.ErdosRenyiGraph(1000, 50.000/1000, true)
8888
t.Logf("Generated graph with %d nodes and %d edges\n", len(g.Nodes()), g.EdgeCount())
8989

90-
nodeLatency := func() float64 { return p2p.LogNormalRand(math.Log(100), 0.5) }
91-
edgeLatency := func() float64 { return p2p.LogNormalRand(math.Log(100), 0.3) }
90+
nodeLatency := func(id p2p.PeerID) float64 { return p2p.LogNormalRand(math.Log(100), 0.5) }
91+
edgeLatency := func(id1, id2 p2p.PeerID) float64 { return p2p.LogNormalRand(math.Log(100), 0.3) }
9292

9393
nw, err := p2p.GenerateP2P(g, nodeLatency, edgeLatency, &p2p.Config{GossipFactor: 0.35})
9494
if err != nil {

p2p/type.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ type Config struct {
1818
CustomParams map[string]any // parameters for custom protocols
1919
}
2020

21-
type CustomProtocolFunc func(msg Message, neighbours []PeerID, sentPeers []PeerID, receivedPeers []PeerID, customParams map[string]any) *[]PeerID
21+
type CustomProtocolFunc func(id PeerID, msg Message, neighbours []PeerID, sentPeers []PeerID, receivedPeers []PeerID, customParams map[string]any) *[]PeerID

0 commit comments

Comments
 (0)