Skip to content

Commit

Permalink
process: include closed channels in getChanInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
carlaKC committed Oct 11, 2023
1 parent 5bbbcd5 commit 5373c1b
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 10 deletions.
10 changes: 6 additions & 4 deletions lndclient_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,18 @@ type lndclientMock struct {
htlcInterceptorRequests chan *interceptedEvent
htlcInterceptorResponses chan *interceptResponse

channels map[uint64]*channel
channels map[uint64]*channel
closedChannels map[uint64]*channel
}

func newLndclientMock(channels map[uint64]*channel) *lndclientMock {
func newLndclientMock(channels, closedChannels map[uint64]*channel) *lndclientMock {
return &lndclientMock{
htlcEvents: make(chan *resolvedEvent),
htlcInterceptorRequests: make(chan *interceptedEvent),
htlcInterceptorResponses: make(chan *interceptResponse),

channels: channels,
channels: channels,
closedChannels: closedChannels,
}
}

Expand All @@ -51,7 +53,7 @@ func (l *lndclientMock) listChannels() (map[uint64]*channel, error) {
}

func (l *lndclientMock) listClosedChannels() (map[uint64]*channel, error) {
return make(map[uint64]*channel), nil
return l.closedChannels, nil
}

func (l *lndclientMock) subscribeHtlcEvents(ctx context.Context) (
Expand Down
17 changes: 17 additions & 0 deletions process.go
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,23 @@ func (p *process) getChanInfo(channel uint64) (*channel, error) {
return ch, nil
}

// If the channel is not open, fall back to checking our closed
// channels.
closedChannels, err := p.client.listClosedChannels()
if err != nil {
return nil, err
}

// Add to cache and try again.
for chanId, ch := range closedChannels {
p.chanMap[chanId] = ch
}

ch, ok = p.chanMap[channel]
if ok {
return ch, nil
}

// Channel not found.
return nil, fmt.Errorf("%w: %v", errChannelNotFound, channel)
}
51 changes: 45 additions & 6 deletions process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const (
)

func testProcess(t *testing.T, event resolveEvent) {
client := newLndclientMock(testChannels)
client := newLndclientMock(testChannels, nil)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

Expand Down Expand Up @@ -130,7 +130,7 @@ func testRateLimit(t *testing.T, mode Mode) {
},
}

client := newLndclientMock(testChannels)
client := newLndclientMock(testChannels, nil)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

Expand Down Expand Up @@ -222,7 +222,7 @@ func testMaxPending(t *testing.T, mode Mode) {
},
}

client := newLndclientMock(testChannels)
client := newLndclientMock(testChannels, nil)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

Expand Down Expand Up @@ -278,7 +278,7 @@ func testMaxPending(t *testing.T, mode Mode) {

func TestNewPeer(t *testing.T) {
// Initialize lnd with test channels.
client := newLndclientMock(testChannels)
client := newLndclientMock(testChannels, nil)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand Down Expand Up @@ -335,7 +335,7 @@ func TestBlocked(t *testing.T) {
},
}

client := newLndclientMock(testChannels)
client := newLndclientMock(testChannels, nil)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

Expand Down Expand Up @@ -370,7 +370,7 @@ func TestBlocked(t *testing.T) {
// TestChannelNotFound tests that we'll successfully exit when we cannot lookup the
// channel that a htlc belongs to.
func TestChannelNotFound(t *testing.T) {
client := newLndclientMock(testChannels)
client := newLndclientMock(testChannels, nil)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand Down Expand Up @@ -407,3 +407,42 @@ func TestChannelNotFound(t *testing.T) {
t.Fatalf("timeout on process error")
}
}

func TestClosedChannelHtlc(t *testing.T) {
// Initialize lnd with a closed channel.
var testClosedChannels = map[uint64]*channel{
5: {peer: route.Vertex{2}},
}
client := newLndclientMock(testChannels, testClosedChannels)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

db, cleanup := setupTestDb(t, defaultFwdHistoryLimit)
defer cleanup()

log := zaptest.NewLogger(t).Sugar()

cfg := &Limits{}

p := NewProcess(client, log, cfg, db)

exit := make(chan error)

go func() {
exit <- p.Run(ctx)
}()

// Send a htlc that is from a closed channel, it should be given the go-ahead to
// resume.
key := circuitKey{
channel: 5,
htlc: 3,
}
client.htlcInterceptorRequests <- &interceptedEvent{
circuitKey: key,
}

resp := <-client.htlcInterceptorResponses
require.Equal(t, key, resp.key)
}

0 comments on commit 5373c1b

Please sign in to comment.