Skip to content

Commit 384c6aa

Browse files
committed
chanfitness: save initial online event for channels with online peers
This commit adds an initial peer online event for channels that have peers that are online when they are created. This addresses a race between the peer coming online and an existing channel being added to the event store; if the peer comes online first, the existing channel will not have its initial online event added.
1 parent 3aca9d2 commit 384c6aa

File tree

2 files changed

+55
-11
lines changed

2 files changed

+55
-11
lines changed

chanfitness/chaneventstore.go

+6
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,12 @@ func (c *ChannelEventStore) addChannel(channelPoint wire.OutPoint,
210210
// Create an event log for the channel.
211211
eventLog := newEventLog(channelPoint, peer, time.Now)
212212

213+
// If the peer is already online, add a peer online event to record
214+
// the starting state of the peer.
215+
if c.peers[peer] {
216+
eventLog.add(peerOnlineEvent)
217+
}
218+
213219
c.channels[channelPoint] = eventLog
214220
}
215221

chanfitness/chaneventstore_test.go

+49-11
Original file line numberDiff line numberDiff line change
@@ -469,23 +469,61 @@ func TestGetUptime(t *testing.T) {
469469

470470
// TestAddChannel tests that channels are added to the event store with
471471
// appropriate timestamps. This test addresses a bug where offline channels
472-
// did not have an opened time set.
472+
// did not have an opened time set, and checks that an online event is set for
473+
// peers that are online at the time that a channel is opened.
473474
func TestAddChannel(t *testing.T) {
474475
_, vertex, chanPoint := getTestChannel(t)
475476

476-
store := NewChannelEventStore(&Config{})
477+
tests := []struct {
478+
name string
477479

478-
// Add channel to the store.
479-
store.addChannel(chanPoint, vertex)
480+
// peers maps peers to an online state.
481+
peers map[route.Vertex]bool
480482

481-
// Check that the eventlog is successfully added.
482-
eventlog, ok := store.channels[chanPoint]
483-
if !ok {
484-
t.Fatalf("channel should be in store")
483+
expectedEvents []eventType
484+
}{
485+
{
486+
name: "peer offline",
487+
peers: make(map[route.Vertex]bool),
488+
expectedEvents: []eventType{},
489+
},
490+
{
491+
name: "peer online",
492+
peers: map[route.Vertex]bool{
493+
vertex: true,
494+
},
495+
expectedEvents: []eventType{peerOnlineEvent},
496+
},
485497
}
486498

487-
// Ensure that open time is always set.
488-
if eventlog.openedAt.IsZero() {
489-
t.Fatalf("channel should have opened at set")
499+
for _, test := range tests {
500+
test := test
501+
t.Run(test.name, func(t *testing.T) {
502+
store := NewChannelEventStore(&Config{})
503+
store.peers = test.peers
504+
505+
// Add channel to the store.
506+
store.addChannel(chanPoint, vertex)
507+
508+
// Check that the eventLog is successfully added.
509+
eventLog, ok := store.channels[chanPoint]
510+
if !ok {
511+
t.Fatalf("channel should be in store")
512+
}
513+
514+
// Check that the eventLog contains the events we
515+
// expect.
516+
for i, e := range test.expectedEvents {
517+
if e != eventLog.events[i].eventType {
518+
t.Fatalf("expected: %v, got: %v",
519+
e, eventLog.events[i].eventType)
520+
}
521+
}
522+
523+
// Ensure that open time is always set.
524+
if eventLog.openedAt.IsZero() {
525+
t.Fatalf("channel should have opened at set")
526+
}
527+
})
490528
}
491529
}

0 commit comments

Comments
 (0)