Skip to content

Commit f1256ba

Browse files
authored
Merge pull request #1788 from cfromknecht/disable-height-hint-cache
Disable height hint cache
2 parents 26f68da + 28a5936 commit f1256ba

File tree

9 files changed

+132
-22
lines changed

9 files changed

+132
-22
lines changed

chainntnfs/bitcoindnotify/bitcoind_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func initHintCache(t *testing.T) *chainntnfs.HeightHintCache {
2525
if err != nil {
2626
t.Fatalf("unable to create db: %v", err)
2727
}
28-
hintCache, err := chainntnfs.NewHeightHintCache(db)
28+
hintCache, err := chainntnfs.NewHeightHintCache(db, true)
2929
if err != nil {
3030
t.Fatalf("unable to create hint cache: %v", err)
3131
}

chainntnfs/btcdnotify/btcd_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func initHintCache(t *testing.T) *chainntnfs.HeightHintCache {
2323
if err != nil {
2424
t.Fatalf("unable to create db: %v", err)
2525
}
26-
hintCache, err := chainntnfs.NewHeightHintCache(db)
26+
hintCache, err := chainntnfs.NewHeightHintCache(db, true)
2727
if err != nil {
2828
t.Fatalf("unable to create hint cache: %v", err)
2929
}

chainntnfs/height_hint_cache.go

+31-3
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ type ConfirmHintCache interface {
8585
// ConfirmHintCache interfaces backed by a channeldb DB instance where the hints
8686
// will be stored.
8787
type HeightHintCache struct {
88-
db *channeldb.DB
88+
db *channeldb.DB
89+
disabled bool
8990
}
9091

9192
// Compile-time checks to ensure HeightHintCache satisfies the SpendHintCache
@@ -94,8 +95,11 @@ var _ SpendHintCache = (*HeightHintCache)(nil)
9495
var _ ConfirmHintCache = (*HeightHintCache)(nil)
9596

9697
// NewHeightHintCache returns a new height hint cache backed by a database.
97-
func NewHeightHintCache(db *channeldb.DB) (*HeightHintCache, error) {
98-
cache := &HeightHintCache{db}
98+
func NewHeightHintCache(db *channeldb.DB, disable bool) (*HeightHintCache, error) {
99+
cache := &HeightHintCache{
100+
db: db,
101+
disabled: disable,
102+
}
99103
if err := cache.initBuckets(); err != nil {
100104
return nil, err
101105
}
@@ -119,6 +123,10 @@ func (c *HeightHintCache) initBuckets() error {
119123

120124
// CommitSpendHint commits a spend hint for the outpoints to the cache.
121125
func (c *HeightHintCache) CommitSpendHint(height uint32, ops ...wire.OutPoint) error {
126+
if c.disabled {
127+
return nil
128+
}
129+
122130
Log.Tracef("Updating spend hint to height %d for %v", height, ops)
123131

124132
return c.db.Batch(func(tx *bolt.Tx) error {
@@ -153,6 +161,10 @@ func (c *HeightHintCache) CommitSpendHint(height uint32, ops ...wire.OutPoint) e
153161
// ErrSpendHintNotFound is returned if a spend hint does not exist within the
154162
// cache for the outpoint.
155163
func (c *HeightHintCache) QuerySpendHint(op wire.OutPoint) (uint32, error) {
164+
if c.disabled {
165+
return 0, ErrSpendHintNotFound
166+
}
167+
156168
var hint uint32
157169
err := c.db.View(func(tx *bolt.Tx) error {
158170
spendHints := tx.Bucket(spendHintBucket)
@@ -181,6 +193,10 @@ func (c *HeightHintCache) QuerySpendHint(op wire.OutPoint) (uint32, error) {
181193

182194
// PurgeSpendHint removes the spend hint for the outpoints from the cache.
183195
func (c *HeightHintCache) PurgeSpendHint(ops ...wire.OutPoint) error {
196+
if c.disabled {
197+
return nil
198+
}
199+
184200
Log.Tracef("Removing spend hints for %v", ops)
185201

186202
return c.db.Batch(func(tx *bolt.Tx) error {
@@ -208,6 +224,10 @@ func (c *HeightHintCache) PurgeSpendHint(ops ...wire.OutPoint) error {
208224

209225
// CommitConfirmHint commits a confirm hint for the transactions to the cache.
210226
func (c *HeightHintCache) CommitConfirmHint(height uint32, txids ...chainhash.Hash) error {
227+
if c.disabled {
228+
return nil
229+
}
230+
211231
Log.Tracef("Updating confirm hints to height %d for %v", height, txids)
212232

213233
return c.db.Batch(func(tx *bolt.Tx) error {
@@ -242,6 +262,10 @@ func (c *HeightHintCache) CommitConfirmHint(height uint32, txids ...chainhash.Ha
242262
// ErrConfirmHintNotFound is returned if a confirm hint does not exist within
243263
// the cache for the transaction hash.
244264
func (c *HeightHintCache) QueryConfirmHint(txid chainhash.Hash) (uint32, error) {
265+
if c.disabled {
266+
return 0, ErrConfirmHintNotFound
267+
}
268+
245269
var hint uint32
246270
err := c.db.View(func(tx *bolt.Tx) error {
247271
confirmHints := tx.Bucket(confirmHintBucket)
@@ -271,6 +295,10 @@ func (c *HeightHintCache) QueryConfirmHint(txid chainhash.Hash) (uint32, error)
271295
// PurgeConfirmHint removes the confirm hint for the transactions from the
272296
// cache.
273297
func (c *HeightHintCache) PurgeConfirmHint(txids ...chainhash.Hash) error {
298+
if c.disabled {
299+
return nil
300+
}
301+
274302
Log.Tracef("Removing confirm hints for %v", txids)
275303

276304
return c.db.Batch(func(tx *bolt.Tx) error {

chainntnfs/height_hint_cache_test.go

+77-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"github.com/lightningnetwork/lnd/channeldb"
1111
)
1212

13-
func initHintCache(t *testing.T) *HeightHintCache {
13+
func initHintCache(t *testing.T, disable bool) *HeightHintCache {
1414
t.Helper()
1515

1616
tempDir, err := ioutil.TempDir("", "kek")
@@ -21,7 +21,7 @@ func initHintCache(t *testing.T) *HeightHintCache {
2121
if err != nil {
2222
t.Fatalf("unable to create db: %v", err)
2323
}
24-
hintCache, err := NewHeightHintCache(db)
24+
hintCache, err := NewHeightHintCache(db, disable)
2525
if err != nil {
2626
t.Fatalf("unable to create hint cache: %v", err)
2727
}
@@ -34,7 +34,7 @@ func initHintCache(t *testing.T) *HeightHintCache {
3434
func TestHeightHintCacheConfirms(t *testing.T) {
3535
t.Parallel()
3636

37-
hintCache := initHintCache(t)
37+
hintCache := initHintCache(t, false)
3838

3939
// Querying for a transaction hash not found within the cache should
4040
// return an error indication so.
@@ -93,7 +93,7 @@ func TestHeightHintCacheConfirms(t *testing.T) {
9393
func TestHeightHintCacheSpends(t *testing.T) {
9494
t.Parallel()
9595

96-
hintCache := initHintCache(t)
96+
hintCache := initHintCache(t, false)
9797

9898
// Querying for an outpoint not found within the cache should return an
9999
// error indication so.
@@ -146,3 +146,76 @@ func TestHeightHintCacheSpends(t *testing.T) {
146146
}
147147
}
148148
}
149+
150+
// TestHeightHintCacheDisabled asserts that a disabled height hint cache never
151+
// returns spend or confirm hints that are committed.
152+
func TestHeightHintCacheDisabled(t *testing.T) {
153+
t.Parallel()
154+
155+
const height uint32 = 100
156+
157+
// Create a disabled height hint cache.
158+
hintCache := initHintCache(t, true)
159+
160+
// Querying a disabled cache w/ no spend hint should return not found.
161+
var outpoint wire.OutPoint
162+
_, err := hintCache.QuerySpendHint(outpoint)
163+
if err != ErrSpendHintNotFound {
164+
t.Fatalf("expected ErrSpendHintNotFound, got: %v", err)
165+
}
166+
167+
// Commit a spend hint to the disabled cache, which should be a noop.
168+
if err := hintCache.CommitSpendHint(height, outpoint); err != nil {
169+
t.Fatalf("unable to commit spend hint: %v", err)
170+
}
171+
172+
// Querying a disabled cache after commit noop should return not found.
173+
_, err = hintCache.QuerySpendHint(outpoint)
174+
if err != ErrSpendHintNotFound {
175+
t.Fatalf("expected ErrSpendHintNotFound, got: %v", err)
176+
}
177+
178+
// Reenable the cache, this time actually committing a spend hint.
179+
hintCache.disabled = false
180+
if err := hintCache.CommitSpendHint(height, outpoint); err != nil {
181+
t.Fatalf("unable to commit spend hint: %v", err)
182+
}
183+
184+
// Disable the cache again, spend hint should not be found.
185+
hintCache.disabled = true
186+
_, err = hintCache.QuerySpendHint(outpoint)
187+
if err != ErrSpendHintNotFound {
188+
t.Fatalf("expected ErrSpendHintNotFound, got: %v", err)
189+
}
190+
191+
// Querying a disabled cache w/ no conf hint should return not found.
192+
var txid chainhash.Hash
193+
_, err = hintCache.QueryConfirmHint(txid)
194+
if err != ErrConfirmHintNotFound {
195+
t.Fatalf("expected ErrConfirmHintNotFound, got: %v", err)
196+
}
197+
198+
// Commit a conf hint to the disabled cache, which should be a noop.
199+
if err := hintCache.CommitConfirmHint(height, txid); err != nil {
200+
t.Fatalf("unable to commit spend hint: %v", err)
201+
}
202+
203+
// Querying a disabled cache after commit noop should return not found.
204+
_, err = hintCache.QueryConfirmHint(txid)
205+
if err != ErrConfirmHintNotFound {
206+
t.Fatalf("expected ErrConfirmHintNotFound, got: %v", err)
207+
}
208+
209+
// Reenable the cache, this time actually committing a conf hint.
210+
hintCache.disabled = false
211+
if err := hintCache.CommitConfirmHint(height, txid); err != nil {
212+
t.Fatalf("unable to commit spend hint: %v", err)
213+
}
214+
215+
// Disable the cache again, conf hint should not be found.
216+
hintCache.disabled = true
217+
_, err = hintCache.QueryConfirmHint(txid)
218+
if err != ErrConfirmHintNotFound {
219+
t.Fatalf("expected ErrConfirmHintNotFound, got: %v", err)
220+
}
221+
}

chainntnfs/interface_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1572,7 +1572,7 @@ func TestInterfaces(t *testing.T) {
15721572
if err != nil {
15731573
t.Fatalf("unable to create db: %v", err)
15741574
}
1575-
hintCache, err := chainntnfs.NewHeightHintCache(db)
1575+
hintCache, err := chainntnfs.NewHeightHintCache(db, true)
15761576
if err != nil {
15771577
t.Fatalf("unable to create height hint cache: %v", err)
15781578
}

chainregistry.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,8 @@ func newChainControlFromConfig(cfg *config, chanDB *channeldb.DB,
181181
cleanUp func()
182182
)
183183

184-
// Initialize the height hint cache within the chain directory.
185-
hintCache, err := chainntnfs.NewHeightHintCache(chanDB)
184+
// Initialize disabled height hint cache within the chain directory.
185+
hintCache, err := chainntnfs.NewHeightHintCache(chanDB, true)
186186
if err != nil {
187187
return nil, nil, fmt.Errorf("unable to initialize height hint "+
188188
"cache: %v", err)

lnd_test.go

+12-5
Original file line numberDiff line numberDiff line change
@@ -2411,15 +2411,22 @@ func testChannelForceClosure(net *lntest.NetworkHarness, t *harnessTest) {
24112411
t.Fatalf(err.Error())
24122412
}
24132413

2414-
// The htlc funds will still be shown as limbo, since they are still in
2415-
// their first stage. The commitment funds will have been recovered
2416-
// after the commit txn was included in the last block.
2414+
// The commitment funds will have been recovered after the commit txn
2415+
// was included in the last block. The htlc funds will not be shown in
2416+
// limbo, since they are still in their first stage and the nursery
2417+
// hasn't received them from the contract court.
24172418
forceClose, err := findForceClosedChannel(pendingChanResp, &op)
24182419
if err != nil {
24192420
t.Fatalf(err.Error())
24202421
}
2421-
if forceClose.LimboBalance == 0 {
2422-
t.Fatalf("htlc funds should still be in limbo")
2422+
err = checkPendingChannelNumHtlcs(forceClose, 0)
2423+
if err != nil {
2424+
t.Fatalf("expected 0 pending htlcs, found %d",
2425+
len(forceClose.PendingHtlcs))
2426+
}
2427+
if forceClose.LimboBalance != 0 {
2428+
t.Fatalf("expected 0 funds in limbo, found %d",
2429+
forceClose.LimboBalance)
24232430
}
24242431

24252432
// Compute the height preceding that which will cause the htlc CLTV

lnwallet/interface_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -2076,7 +2076,7 @@ func TestLightningWallet(t *testing.T) {
20762076
if err != nil {
20772077
t.Fatalf("unable to create db: %v", err)
20782078
}
2079-
hintCache, err := chainntnfs.NewHeightHintCache(db)
2079+
hintCache, err := chainntnfs.NewHeightHintCache(db, true)
20802080
if err != nil {
20812081
t.Fatalf("unable to create height hint cache: %v", err)
20822082
}

peer.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -804,14 +804,16 @@ func (ms *msgStream) msgConsumer() {
804804

805805
// AddMsg adds a new message to the msgStream. This function is safe for
806806
// concurrent access.
807-
func (ms *msgStream) AddMsg(msg lnwire.Message) {
807+
func (ms *msgStream) AddMsg(msg lnwire.Message, quit chan struct{}) {
808808
// First, we'll attempt to receive from the producerSema struct. This
809809
// acts as a sempahore to prevent us from indefinitely buffering
810810
// incoming items from the wire. Either the msg queue isn't full, and
811811
// we'll not block, or the queue is full, and we'll block until either
812812
// we're signalled to quit, or a slot is freed up.
813813
select {
814814
case <-ms.producerSema:
815+
case <-quit:
816+
return
815817
case <-ms.quit:
816818
return
817819
}
@@ -1020,7 +1022,7 @@ out:
10201022
// forward the error to all channels with this peer.
10211023
case msg.ChanID == lnwire.ConnectionWideID:
10221024
for chanID, chanStream := range chanMsgStreams {
1023-
chanStream.AddMsg(nextMsg)
1025+
chanStream.AddMsg(nextMsg, p.quit)
10241026

10251027
// Also marked this channel as failed,
10261028
// so we won't try to restart it on
@@ -1082,7 +1084,7 @@ out:
10821084
*lnwire.ReplyChannelRange,
10831085
*lnwire.ReplyShortChanIDsEnd:
10841086

1085-
discStream.AddMsg(msg)
1087+
discStream.AddMsg(msg, p.quit)
10861088

10871089
default:
10881090
peerLog.Errorf("unknown message %v received from peer "+
@@ -1105,7 +1107,7 @@ out:
11051107

11061108
// With the stream obtained, add the message to the
11071109
// stream so we can continue processing message.
1108-
chanStream.AddMsg(nextMsg)
1110+
chanStream.AddMsg(nextMsg, p.quit)
11091111
}
11101112

11111113
idleTimer.Reset(idleTimeout)

0 commit comments

Comments
 (0)