@@ -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.
78105func (p * P2P ) PeerIDs () []PeerID {
79106 ids := make ([]PeerID , 0 , len (p .nodes ))
0 commit comments