mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-01-19 05:45:21 +01:00
chainntnfs/height_hint_cache_test: add tests for disabled cache
This commit is contained in:
parent
7df9ae0266
commit
45a2c9aca8
@ -10,7 +10,7 @@ import (
|
|||||||
"github.com/lightningnetwork/lnd/channeldb"
|
"github.com/lightningnetwork/lnd/channeldb"
|
||||||
)
|
)
|
||||||
|
|
||||||
func initHintCache(t *testing.T) *HeightHintCache {
|
func initHintCache(t *testing.T, disable bool) *HeightHintCache {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
tempDir, err := ioutil.TempDir("", "kek")
|
tempDir, err := ioutil.TempDir("", "kek")
|
||||||
@ -21,7 +21,7 @@ func initHintCache(t *testing.T) *HeightHintCache {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("unable to create db: %v", err)
|
t.Fatalf("unable to create db: %v", err)
|
||||||
}
|
}
|
||||||
hintCache, err := NewHeightHintCache(db)
|
hintCache, err := NewHeightHintCache(db, disable)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("unable to create hint cache: %v", err)
|
t.Fatalf("unable to create hint cache: %v", err)
|
||||||
}
|
}
|
||||||
@ -34,7 +34,7 @@ func initHintCache(t *testing.T) *HeightHintCache {
|
|||||||
func TestHeightHintCacheConfirms(t *testing.T) {
|
func TestHeightHintCacheConfirms(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
hintCache := initHintCache(t)
|
hintCache := initHintCache(t, false)
|
||||||
|
|
||||||
// Querying for a transaction hash not found within the cache should
|
// Querying for a transaction hash not found within the cache should
|
||||||
// return an error indication so.
|
// return an error indication so.
|
||||||
@ -93,7 +93,7 @@ func TestHeightHintCacheConfirms(t *testing.T) {
|
|||||||
func TestHeightHintCacheSpends(t *testing.T) {
|
func TestHeightHintCacheSpends(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
hintCache := initHintCache(t)
|
hintCache := initHintCache(t, false)
|
||||||
|
|
||||||
// Querying for an outpoint not found within the cache should return an
|
// Querying for an outpoint not found within the cache should return an
|
||||||
// error indication so.
|
// error indication so.
|
||||||
@ -146,3 +146,76 @@ func TestHeightHintCacheSpends(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TestHeightHintCacheDisabled asserts that a disabled height hint cache never
|
||||||
|
// returns spend or confirm hints that are committed.
|
||||||
|
func TestHeightHintCacheDisabled(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
const height uint32 = 100
|
||||||
|
|
||||||
|
// Create a disabled height hint cache.
|
||||||
|
hintCache := initHintCache(t, true)
|
||||||
|
|
||||||
|
// Querying a disabled cache w/ no spend hint should return not found.
|
||||||
|
var outpoint wire.OutPoint
|
||||||
|
_, err := hintCache.QuerySpendHint(outpoint)
|
||||||
|
if err != ErrSpendHintNotFound {
|
||||||
|
t.Fatalf("expected ErrSpendHintNotFound, got: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Commit a spend hint to the disabled cache, which should be a noop.
|
||||||
|
if err := hintCache.CommitSpendHint(height, outpoint); err != nil {
|
||||||
|
t.Fatalf("unable to commit spend hint: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Querying a disabled cache after commit noop should return not found.
|
||||||
|
_, err = hintCache.QuerySpendHint(outpoint)
|
||||||
|
if err != ErrSpendHintNotFound {
|
||||||
|
t.Fatalf("expected ErrSpendHintNotFound, got: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reenable the cache, this time actually committing a spend hint.
|
||||||
|
hintCache.disabled = false
|
||||||
|
if err := hintCache.CommitSpendHint(height, outpoint); err != nil {
|
||||||
|
t.Fatalf("unable to commit spend hint: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Disable the cache again, spend hint should not be found.
|
||||||
|
hintCache.disabled = true
|
||||||
|
_, err = hintCache.QuerySpendHint(outpoint)
|
||||||
|
if err != ErrSpendHintNotFound {
|
||||||
|
t.Fatalf("expected ErrSpendHintNotFound, got: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Querying a disabled cache w/ no conf hint should return not found.
|
||||||
|
var txid chainhash.Hash
|
||||||
|
_, err = hintCache.QueryConfirmHint(txid)
|
||||||
|
if err != ErrConfirmHintNotFound {
|
||||||
|
t.Fatalf("expected ErrConfirmHintNotFound, got: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Commit a conf hint to the disabled cache, which should be a noop.
|
||||||
|
if err := hintCache.CommitConfirmHint(height, txid); err != nil {
|
||||||
|
t.Fatalf("unable to commit spend hint: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Querying a disabled cache after commit noop should return not found.
|
||||||
|
_, err = hintCache.QueryConfirmHint(txid)
|
||||||
|
if err != ErrConfirmHintNotFound {
|
||||||
|
t.Fatalf("expected ErrConfirmHintNotFound, got: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reenable the cache, this time actually committing a conf hint.
|
||||||
|
hintCache.disabled = false
|
||||||
|
if err := hintCache.CommitConfirmHint(height, txid); err != nil {
|
||||||
|
t.Fatalf("unable to commit spend hint: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Disable the cache again, conf hint should not be found.
|
||||||
|
hintCache.disabled = true
|
||||||
|
_, err = hintCache.QueryConfirmHint(txid)
|
||||||
|
if err != ErrConfirmHintNotFound {
|
||||||
|
t.Fatalf("expected ErrConfirmHintNotFound, got: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user