Skip to content

Commit f0e360f

Browse files
committed
graphdb: test querying chans without a policy
In this commit we add TestChanUpdatesInHorizonWithNoPolicies to test that we can query channels with no policy attached to it. We also test that we can query channels with policies alongside those without.
1 parent 15177ef commit f0e360f

File tree

2 files changed

+139
-0
lines changed

2 files changed

+139
-0
lines changed

graph/db/graph_test.go

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3693,6 +3693,140 @@ func TestChannelEdgePruningUpdateIndexDeletion(t *testing.T) {
36933693
checkIndexTimestamps()
36943694
}
36953695

3696+
// TestChanUpdatesInHorizonWithNoPolicies tests that we're able to properly
3697+
// retrieve channels with no policies within the time range.
3698+
func TestChanUpdatesInHorizonWithNoPolicies(t *testing.T) {
3699+
t.Parallel()
3700+
ctx := t.Context()
3701+
3702+
graph := MakeTestGraph(t)
3703+
3704+
// This test is only valid for the SQL graph DB. So we skip it for
3705+
// kvdb.
3706+
sqlStore, ok := graph.V1Store.(*SQLStore)
3707+
if !ok {
3708+
t.Skip("skipping test that is aimed at a SQL graph DB")
3709+
}
3710+
3711+
// We'll start by creating two nodes which will seed our test graph.
3712+
node1 := createTestVertex(t)
3713+
require.NoError(t, sqlStore.AddNode(ctx, node1))
3714+
3715+
node2 := createTestVertex(t)
3716+
require.NoError(t, sqlStore.AddNode(ctx, node2))
3717+
3718+
// Note: startTime and endTime only works if the channels have
3719+
// policies. If not, the channels are included irrespective of the
3720+
// time range.
3721+
startTime := time.Unix(1234, 0)
3722+
endTime := startTime
3723+
edges := make([]ChannelEdge, 0, 10)
3724+
3725+
// We'll now create 10 channels between the two nodes, with no policies.
3726+
const numChans = 10
3727+
for i := range numChans {
3728+
channel, chanID := createEdge(
3729+
uint32(i*10), 0, 0, 0, node1, node2,
3730+
)
3731+
require.NoError(t, sqlStore.AddChannelEdge(ctx, &channel))
3732+
3733+
// The first 5 channels will have no policies.
3734+
if i < numChans/2 {
3735+
edges = append(edges, ChannelEdge{
3736+
Info: &channel,
3737+
})
3738+
3739+
continue
3740+
}
3741+
3742+
edge1UpdateTime := endTime
3743+
edge2UpdateTime := edge1UpdateTime.Add(time.Second)
3744+
endTime = endTime.Add(time.Second * 10)
3745+
3746+
edge1 := newEdgePolicy(
3747+
chanID.ToUint64(), edge1UpdateTime.Unix(),
3748+
)
3749+
edge1.ChannelFlags = 0
3750+
edge1.ToNode = node2.PubKeyBytes
3751+
edge1.SigBytes = testSig.Serialize()
3752+
require.NoError(t, graph.UpdateEdgePolicy(ctx, edge1))
3753+
3754+
edge2 := newEdgePolicy(
3755+
chanID.ToUint64(), edge2UpdateTime.Unix(),
3756+
)
3757+
edge2.ChannelFlags = 1
3758+
edge2.ToNode = node1.PubKeyBytes
3759+
edge2.SigBytes = testSig.Serialize()
3760+
require.NoError(t, graph.UpdateEdgePolicy(ctx, edge2))
3761+
3762+
edges = append(edges, ChannelEdge{
3763+
Info: &channel,
3764+
Policy1: edge1,
3765+
Policy2: edge2,
3766+
})
3767+
}
3768+
3769+
// With our channels loaded, we'll now start our series of queries.
3770+
queryCases := []struct {
3771+
start time.Time
3772+
end time.Time
3773+
resp []ChannelEdge
3774+
}{
3775+
// If we query for a time range that's strictly below our set
3776+
// of updates, then we'll get only the 5 channels with no
3777+
// policies.
3778+
{
3779+
start: time.Unix(100, 0),
3780+
end: time.Unix(200, 0),
3781+
resp: edges[:5],
3782+
},
3783+
3784+
// If we query for a time range that's well beyond our set of
3785+
// updates, we should get only the 5 channels with no
3786+
// policies.
3787+
{
3788+
start: time.Unix(99999, 0),
3789+
end: time.Unix(999999, 0),
3790+
resp: edges[:5],
3791+
},
3792+
3793+
// If we query for the start time, and 10 seconds directly
3794+
// after it, we should only get the 5 channels with no
3795+
// policies and one channel with a policy.
3796+
{
3797+
start: time.Unix(1234, 0),
3798+
end: startTime.Add(time.Second * 10),
3799+
resp: edges[:6],
3800+
},
3801+
3802+
// If we use the start and end time as is, we should get the
3803+
// entire range.
3804+
{
3805+
start: startTime,
3806+
end: endTime,
3807+
3808+
resp: edges[:10],
3809+
},
3810+
}
3811+
3812+
for _, queryCase := range queryCases {
3813+
respIter := sqlStore.ChanUpdatesInHorizon(
3814+
queryCase.start, queryCase.end,
3815+
)
3816+
3817+
resp, err := fn.CollectErr(respIter)
3818+
require.NoError(t, err)
3819+
require.Equal(t, len(resp), len(queryCase.resp))
3820+
3821+
for i := range len(resp) {
3822+
chanExp := queryCase.resp[i]
3823+
chanRet := resp[i]
3824+
3825+
require.Equal(t, chanExp.Info, chanRet.Info)
3826+
}
3827+
}
3828+
}
3829+
36963830
// TestPruneGraphNodes tests that unconnected vertexes are pruned via the
36973831
// PruneSyncState method.
36983832
func TestPruneGraphNodes(t *testing.T) {

graph/db/sql_store.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,6 +1098,11 @@ func (s *SQLStore) updateChanCacheBatch(edgesToCache map[uint64]ChannelEdge) {
10981098
// 5. Update cache after successful batch
10991099
// 6. Repeat with updated pagination cursor until no more results
11001100
//
1101+
// Note: Ideally each channel should have at least one policy. However, if a
1102+
// channel is created and never updated, it will not have any policies.
1103+
// In this case, we'll return the channel with no policies at all regardless of
1104+
// the time range. This helps us prune zombie channels with no policies.
1105+
//
11011106
// NOTE: This is part of the V1Store interface.
11021107
func (s *SQLStore) ChanUpdatesInHorizon(startTime, endTime time.Time,
11031108
opts ...IteratorOption) iter.Seq2[ChannelEdge, error] {

0 commit comments

Comments
 (0)