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.
This commit is contained in:
carla 2020-01-07 16:32:47 +02:00
parent 3aca9d24b8
commit 384c6aa1f1
No known key found for this signature in database
GPG Key ID: 4CA7FE54A6213C91
2 changed files with 55 additions and 11 deletions

@ -210,6 +210,12 @@ func (c *ChannelEventStore) addChannel(channelPoint wire.OutPoint,
// Create an event log for the channel.
eventLog := newEventLog(channelPoint, peer, time.Now)
// If the peer is already online, add a peer online event to record
// the starting state of the peer.
if c.peers[peer] {
eventLog.add(peerOnlineEvent)
}
c.channels[channelPoint] = eventLog
}

@ -469,23 +469,61 @@ func TestGetUptime(t *testing.T) {
// TestAddChannel tests that channels are added to the event store with
// appropriate timestamps. This test addresses a bug where offline channels
// did not have an opened time set.
// did not have an opened time set, and checks that an online event is set for
// peers that are online at the time that a channel is opened.
func TestAddChannel(t *testing.T) {
_, vertex, chanPoint := getTestChannel(t)
store := NewChannelEventStore(&Config{})
tests := []struct {
name string
// Add channel to the store.
store.addChannel(chanPoint, vertex)
// peers maps peers to an online state.
peers map[route.Vertex]bool
// Check that the eventlog is successfully added.
eventlog, ok := store.channels[chanPoint]
if !ok {
t.Fatalf("channel should be in store")
expectedEvents []eventType
}{
{
name: "peer offline",
peers: make(map[route.Vertex]bool),
expectedEvents: []eventType{},
},
{
name: "peer online",
peers: map[route.Vertex]bool{
vertex: true,
},
expectedEvents: []eventType{peerOnlineEvent},
},
}
// Ensure that open time is always set.
if eventlog.openedAt.IsZero() {
t.Fatalf("channel should have opened at set")
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
store := NewChannelEventStore(&Config{})
store.peers = test.peers
// Add channel to the store.
store.addChannel(chanPoint, vertex)
// Check that the eventLog is successfully added.
eventLog, ok := store.channels[chanPoint]
if !ok {
t.Fatalf("channel should be in store")
}
// Check that the eventLog contains the events we
// expect.
for i, e := range test.expectedEvents {
if e != eventLog.events[i].eventType {
t.Fatalf("expected: %v, got: %v",
e, eventLog.events[i].eventType)
}
}
// Ensure that open time is always set.
if eventLog.openedAt.IsZero() {
t.Fatalf("channel should have opened at set")
}
})
}
}