forked from lightningnetwork/lnd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheld_htlc_set_test.go
126 lines (100 loc) · 2.47 KB
/
held_htlc_set_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package htlcswitch
import (
"testing"
"github.com/lightningnetwork/lnd/channeldb/models"
"github.com/lightningnetwork/lnd/lnwire"
"github.com/stretchr/testify/require"
)
func TestHeldHtlcSetEmpty(t *testing.T) {
set := newHeldHtlcSet()
// Test operations on an empty set.
require.False(t, set.exists(models.CircuitKey{}))
_, err := set.pop(models.CircuitKey{})
require.Error(t, err)
set.popAll(
func(_ InterceptedForward) {
require.Fail(t, "unexpected fwd")
},
)
}
func TestHeldHtlcSet(t *testing.T) {
set := newHeldHtlcSet()
key := models.CircuitKey{
ChanID: lnwire.NewShortChanIDFromInt(1),
HtlcID: 2,
}
// Test pushing a nil forward.
require.Error(t, set.push(key, nil))
// Test pushing a forward.
fwd := &interceptedForward{
htlc: &lnwire.UpdateAddHTLC{},
}
require.NoError(t, set.push(key, fwd))
// Re-pushing should fail.
require.Error(t, set.push(key, fwd))
// Test popping the fwd.
poppedFwd, err := set.pop(key)
require.NoError(t, err)
require.Equal(t, fwd, poppedFwd)
_, err = set.pop(key)
require.Error(t, err)
// Pushing the forward again.
require.NoError(t, set.push(key, fwd))
// Test for each.
var cbCalled bool
set.forEach(func(_ InterceptedForward) {
cbCalled = true
require.Equal(t, fwd, poppedFwd)
})
require.True(t, cbCalled)
// Test popping all forwards.
cbCalled = false
set.popAll(
func(_ InterceptedForward) {
cbCalled = true
require.Equal(t, fwd, poppedFwd)
},
)
require.True(t, cbCalled)
_, err = set.pop(key)
require.Error(t, err)
}
func TestHeldHtlcSetAutoFails(t *testing.T) {
set := newHeldHtlcSet()
key := models.CircuitKey{
ChanID: lnwire.NewShortChanIDFromInt(1),
HtlcID: 2,
}
const autoFailHeight = 100
fwd := &interceptedForward{
packet: &htlcPacket{},
htlc: &lnwire.UpdateAddHTLC{},
autoFailHeight: autoFailHeight,
}
require.NoError(t, set.push(key, fwd))
// Test popping auto fails up to one block before the auto-fail height
// of our forward.
set.popAutoFails(
autoFailHeight-1,
func(_ InterceptedForward) {
require.Fail(t, "unexpected fwd")
},
)
// Popping succeeds at the auto-fail height.
cbCalled := false
set.popAutoFails(
autoFailHeight,
func(poppedFwd InterceptedForward) {
cbCalled = true
require.Equal(t, fwd, poppedFwd)
},
)
require.True(t, cbCalled)
// After this, there should be nothing more to pop.
set.popAutoFails(
autoFailHeight,
func(_ InterceptedForward) {
require.Fail(t, "unexpected fwd")
},
)
}