forked from lightningnetwork/lnd
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathresolution_store_test.go
146 lines (119 loc) · 4.27 KB
/
resolution_store_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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package htlcswitch
import (
"path/filepath"
"testing"
"github.com/lightningnetwork/lnd/contractcourt"
"github.com/lightningnetwork/lnd/kvdb"
"github.com/lightningnetwork/lnd/lnwire"
"github.com/stretchr/testify/require"
)
// TestInsertAndDelete tests that an inserted resolution message can be
// deleted.
func TestInsertAndDelete(t *testing.T) {
t.Parallel()
scid := lnwire.NewShortChanIDFromInt(1)
failResMsg := &contractcourt.ResolutionMsg{
SourceChan: scid,
HtlcIndex: 2,
Failure: &lnwire.FailTemporaryChannelFailure{},
}
settleBytes := [32]byte{
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
}
settleResMsg := &contractcourt.ResolutionMsg{
SourceChan: scid,
HtlcIndex: 3,
PreImage: &settleBytes,
}
// Create the backend database and use it to create the resolution
// store.
dbPath := filepath.Join(t.TempDir(), "testdb")
db, err := kvdb.Create(
kvdb.BoltBackendName, dbPath, true, kvdb.DefaultDBTimeout,
)
require.NoError(t, err)
t.Cleanup(func() {
db.Close()
})
resStore := newResolutionStore(db)
// We'll add the failure resolution message first, then check that it
// exists in the store.
err = resStore.addResolutionMsg(failResMsg)
require.NoError(t, err)
// Assert that checkResolutionMsg returns nil, signalling that the
// resolution message was properly stored.
outKey := &CircuitKey{
ChanID: failResMsg.SourceChan,
HtlcID: failResMsg.HtlcIndex,
}
err = resStore.checkResolutionMsg(outKey)
require.NoError(t, err)
resMsgs, err := resStore.fetchAllResolutionMsg()
require.NoError(t, err)
require.Equal(t, 1, len(resMsgs))
// It should match failResMsg above.
require.Equal(t, failResMsg.SourceChan, resMsgs[0].SourceChan)
require.Equal(t, failResMsg.HtlcIndex, resMsgs[0].HtlcIndex)
require.NotNil(t, resMsgs[0].Failure)
require.Nil(t, resMsgs[0].PreImage)
// We'll add the settleResMsg now.
err = resStore.addResolutionMsg(settleResMsg)
require.NoError(t, err)
// Check that checkResolutionMsg returns nil for the settle CircuitKey.
outKey.ChanID = settleResMsg.SourceChan
outKey.HtlcID = settleResMsg.HtlcIndex
err = resStore.checkResolutionMsg(outKey)
require.NoError(t, err)
// We should have two resolution messages in the store, one failure and
// one success.
resMsgs, err = resStore.fetchAllResolutionMsg()
require.NoError(t, err)
require.Equal(t, 2, len(resMsgs))
// The first resolution message should be the failure.
require.Equal(t, failResMsg.SourceChan, resMsgs[0].SourceChan)
require.Equal(t, failResMsg.HtlcIndex, resMsgs[0].HtlcIndex)
require.NotNil(t, resMsgs[0].Failure)
require.Nil(t, resMsgs[0].PreImage)
// The second resolution message should be the success.
require.Equal(t, settleResMsg.SourceChan, resMsgs[1].SourceChan)
require.Equal(t, settleResMsg.HtlcIndex, resMsgs[1].HtlcIndex)
require.Nil(t, resMsgs[1].Failure)
require.Equal(t, settleBytes, *resMsgs[1].PreImage)
// We'll now delete the failure resolution message and assert that only
// the success is left.
failKey := &CircuitKey{
ChanID: scid,
HtlcID: failResMsg.HtlcIndex,
}
err = resStore.deleteResolutionMsg(failKey)
require.NoError(t, err)
// Assert that checkResolutionMsg returns errResMsgNotFound.
err = resStore.checkResolutionMsg(failKey)
require.ErrorIs(t, err, errResMsgNotFound)
resMsgs, err = resStore.fetchAllResolutionMsg()
require.NoError(t, err)
require.Equal(t, 1, len(resMsgs))
// Assert that the success is left.
require.Equal(t, settleResMsg.SourceChan, resMsgs[0].SourceChan)
require.Equal(t, settleResMsg.HtlcIndex, resMsgs[0].HtlcIndex)
require.Nil(t, resMsgs[0].Failure)
require.Equal(t, settleBytes, *resMsgs[0].PreImage)
// Now we'll delete the settle resolution message and assert that the
// store is empty.
settleKey := &CircuitKey{
ChanID: scid,
HtlcID: settleResMsg.HtlcIndex,
}
err = resStore.deleteResolutionMsg(settleKey)
require.NoError(t, err)
// Assert that checkResolutionMsg returns errResMsgNotFound for the
// settle key.
err = resStore.checkResolutionMsg(settleKey)
require.ErrorIs(t, err, errResMsgNotFound)
resMsgs, err = resStore.fetchAllResolutionMsg()
require.NoError(t, err)
require.Equal(t, 0, len(resMsgs))
}