Skip to content

Commit 0f7c209

Browse files
committed
chainntnfs: unit test disabled height hint cache behavior
1 parent 47ce718 commit 0f7c209

File tree

1 file changed

+51
-4
lines changed

1 file changed

+51
-4
lines changed

Diff for: chainntnfs/height_hint_cache_test.go

+51-4
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,22 @@ import (
88
"github.com/btcsuite/btcd/chaincfg/chainhash"
99
"github.com/btcsuite/btcd/wire"
1010
"github.com/lightningnetwork/lnd/channeldb"
11+
"github.com/stretchr/testify/require"
1112
)
1213

1314
func initHintCache(t *testing.T) *HeightHintCache {
1415
t.Helper()
1516

17+
defaultCfg := CacheConfig{
18+
QueryDisable: false,
19+
}
20+
21+
return initHintCacheWithConfig(t, defaultCfg)
22+
}
23+
24+
func initHintCacheWithConfig(t *testing.T, cfg CacheConfig) *HeightHintCache {
25+
t.Helper()
26+
1627
tempDir, err := ioutil.TempDir("", "kek")
1728
if err != nil {
1829
t.Fatalf("unable to create temp dir: %v", err)
@@ -21,10 +32,7 @@ func initHintCache(t *testing.T) *HeightHintCache {
2132
if err != nil {
2233
t.Fatalf("unable to create db: %v", err)
2334
}
24-
testCfg := CacheConfig{
25-
QueryDisable: false,
26-
}
27-
hintCache, err := NewHeightHintCache(testCfg, db)
35+
hintCache, err := NewHeightHintCache(cfg, db)
2836
if err != nil {
2937
t.Fatalf("unable to create hint cache: %v", err)
3038
}
@@ -154,3 +162,42 @@ func TestHeightHintCacheSpends(t *testing.T) {
154162
}
155163
}
156164
}
165+
166+
// TestQueryDisable asserts querying for confirmation or spend hints always
167+
// return height zero when QueryDisabled is set to true in the CacheConfig.
168+
func TestQueryDisable(t *testing.T) {
169+
cfg := CacheConfig{
170+
QueryDisable: true,
171+
}
172+
173+
hintCache := initHintCacheWithConfig(t, cfg)
174+
175+
// Insert a new confirmation hint with a non-zero height.
176+
const confHeight = 100
177+
confRequest := ConfRequest{
178+
TxID: chainhash.Hash{0x01, 0x02, 0x03},
179+
}
180+
err := hintCache.CommitConfirmHint(confHeight, confRequest)
181+
require.Nil(t, err)
182+
183+
// Query for the confirmation hint, which should return zero.
184+
cachedConfHeight, err := hintCache.QueryConfirmHint(confRequest)
185+
require.Nil(t, err)
186+
require.Equal(t, uint32(0), cachedConfHeight)
187+
188+
// Insert a new spend hint with a non-zero height.
189+
const spendHeight = 200
190+
spendRequest := SpendRequest{
191+
OutPoint: wire.OutPoint{
192+
Hash: chainhash.Hash{0x4, 0x05, 0x06},
193+
Index: 42,
194+
},
195+
}
196+
err = hintCache.CommitSpendHint(spendHeight, spendRequest)
197+
require.Nil(t, err)
198+
199+
// Query for the spend hint, which should return zero.
200+
cachedSpendHeight, err := hintCache.QuerySpendHint(spendRequest)
201+
require.Nil(t, err)
202+
require.Equal(t, uint32(0), cachedSpendHeight)
203+
}

0 commit comments

Comments
 (0)