Skip to content

Commit 3e6772c

Browse files
authored
Merge pull request #38 from elecbug/elecbug/master
Update v0.11.1
2 parents 29c49be + 6b9453f commit 3e6772c

4 files changed

Lines changed: 49 additions & 14 deletions

File tree

graph/graph.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ func FromMatrix(matrix [][]bool, bidirectional bool) *Graph {
3939
return g
4040
}
4141

42-
// Save serializes the graph to a string.
43-
func Save(g *Graph) (string, error) {
42+
// Serialize serializes the graph to a string.
43+
func Serialize(g *Graph) (string, error) {
4444
if g == nil {
4545
return "", fmt.Errorf("graph is nil")
4646
}
@@ -66,8 +66,8 @@ func Save(g *Graph) (string, error) {
6666
return fmt.Sprintf("%s\n%s\n%s", nodes, edges, bidirectional), nil
6767
}
6868

69-
// Load deserializes the graph from a string.
70-
func Load(data string) (*Graph, error) {
69+
// Deserialize deserializes the graph from a string.
70+
func Deserialize(data string) (*Graph, error) {
7171
lines := strings.Split(data, "\n")
7272
if len(lines) < 3 {
7373
return nil, fmt.Errorf("invalid graph data")

graph/graph_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,15 @@ func TestBidirectionalGraph(t *testing.T) {
102102
g.AddEdge(graph.NodeID(fmt.Sprintf("%d", rand.Intn(nodeCount))), graph.NodeID(fmt.Sprintf("%d", rand.Intn(nodeCount))))
103103
}
104104

105-
str, err := graph.Save(g)
105+
str, err := graph.Serialize(g)
106106

107107
if err != nil {
108108
t.Fatalf("Failed to save graph: %v", err)
109109
}
110110

111111
os.WriteFile("bidirectional.graph.log", []byte(str), fs.ModePerm)
112112

113-
g2, err := graph.Load(str)
113+
g2, err := graph.Deserialize(str)
114114

115115
if err != nil {
116116
t.Fatalf("Failed to load graph: %v", err)
@@ -137,15 +137,15 @@ func TestDirectionalGraph(t *testing.T) {
137137
g.AddEdge(graph.NodeID(fmt.Sprintf("%d", rand.Intn(nodeCount))), graph.NodeID(fmt.Sprintf("%d", rand.Intn(nodeCount))))
138138
}
139139

140-
str, err := graph.Save(g)
140+
str, err := graph.Serialize(g)
141141

142142
if err != nil {
143143
t.Fatalf("Failed to save graph: %v", err)
144144
}
145145

146146
os.WriteFile("directional.graph.log", []byte(str), fs.ModePerm)
147147

148-
g2, err := graph.Load(str)
148+
g2, err := graph.Deserialize(str)
149149

150150
if err != nil {
151151
t.Fatalf("Failed to load graph: %v", err)

p2p/node.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ type p2pNode struct {
1313
nodeLatency float64
1414
edges map[PeerID]p2pEdge
1515

16-
recvFrom map[string]map[PeerID]struct{} // content -> set of senders
17-
sentTo map[string]map[PeerID]struct{} // content -> set of targets
18-
seenAt map[string]time.Time // content -> first arrival time
16+
recvFrom map[string]map[PeerID]struct{} // content -> set of senders
17+
sentTo map[string]map[PeerID]struct{} // content -> set of targets
18+
seenAt map[string]time.Time // content -> first arrival time
19+
firstFrom map[string]PeerID // content -> first sender
1920

2021
msgQueue chan Message
2122
mu sync.Mutex
@@ -36,9 +37,10 @@ func newNode(id PeerID, nodeLatency float64) *p2pNode {
3637
nodeLatency: nodeLatency,
3738
edges: make(map[PeerID]p2pEdge),
3839

39-
recvFrom: make(map[string]map[PeerID]struct{}),
40-
sentTo: make(map[string]map[PeerID]struct{}),
41-
seenAt: make(map[string]time.Time),
40+
recvFrom: make(map[string]map[PeerID]struct{}),
41+
sentTo: make(map[string]map[PeerID]struct{}),
42+
seenAt: make(map[string]time.Time),
43+
firstFrom: make(map[string]PeerID),
4244

4345
msgQueue: make(chan Message, 1000),
4446
mu: sync.Mutex{},
@@ -67,6 +69,7 @@ func (n *p2pNode) eachRun(network *P2P, wg *sync.WaitGroup, ctx context.Context)
6769

6870
if _, ok := n.seenAt[msg.Content]; !ok {
6971
n.seenAt[msg.Content] = time.Now()
72+
n.firstFrom[msg.Content] = msg.From
7073
first = true
7174
}
7275
n.mu.Unlock()

p2p/p2p.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,38 @@ func (p *P2P) FirstMessageReceptionTimes(msg string) []time.Time {
165165
return firstTimes
166166
}
167167

168+
func (p *P2P) FirstMessageReceptions(msg string) []struct {
169+
PeerID graph.NodeID `json:"peer_id"`
170+
From graph.NodeID `json:"from"`
171+
Timestamp time.Time `json:"timestamp"`
172+
} {
173+
receptions := make([]struct {
174+
PeerID graph.NodeID `json:"peer_id"`
175+
From graph.NodeID `json:"from"`
176+
Timestamp time.Time `json:"timestamp"`
177+
}, 0)
178+
179+
for _, node := range p.nodes {
180+
node.mu.Lock()
181+
if t, ok := node.seenAt[msg]; ok {
182+
from := node.firstFrom[msg]
183+
184+
receptions = append(receptions, struct {
185+
PeerID graph.NodeID `json:"peer_id"`
186+
From graph.NodeID `json:"from"`
187+
Timestamp time.Time `json:"timestamp"`
188+
}{
189+
PeerID: graph.NodeID(fmt.Sprintf("%d", node.id)),
190+
From: graph.NodeID(fmt.Sprintf("%d", from)),
191+
Timestamp: t,
192+
})
193+
}
194+
node.mu.Unlock()
195+
}
196+
197+
return receptions
198+
}
199+
168200
// NumberOfDuplicateMessages counts how many duplicate messages were received across all nodes.
169201
func (p *P2P) NumberOfDuplicateMessages(msg string) int {
170202
dupCount := 0

0 commit comments

Comments
 (0)