2018-05-21 18:47:39 +03:00
|
|
|
package chainntnfs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"github.com/lightningnetwork/lnd/channeldb"
|
2021-04-26 20:08:11 +03:00
|
|
|
"github.com/lightningnetwork/lnd/kvdb"
|
2018-05-21 18:47:39 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// spendHintBucket is the name of the bucket which houses the height
|
|
|
|
// hint for outpoints. Each height hint represents the earliest height
|
|
|
|
// at which its corresponding outpoint could have been spent within.
|
|
|
|
spendHintBucket = []byte("spend-hints")
|
|
|
|
|
|
|
|
// confirmHintBucket is the name of the bucket which houses the height
|
|
|
|
// hints for transactions. Each height hint represents the earliest
|
|
|
|
// height at which its corresponding transaction could have been
|
|
|
|
// confirmed within.
|
|
|
|
confirmHintBucket = []byte("confirm-hints")
|
|
|
|
|
|
|
|
// ErrCorruptedHeightHintCache indicates that the on-disk bucketing
|
|
|
|
// structure has altered since the height hint cache instance was
|
|
|
|
// initialized.
|
|
|
|
ErrCorruptedHeightHintCache = errors.New("height hint cache has been " +
|
|
|
|
"corrupted")
|
|
|
|
|
|
|
|
// ErrSpendHintNotFound is an error returned when a spend hint for an
|
|
|
|
// outpoint was not found.
|
|
|
|
ErrSpendHintNotFound = errors.New("spend hint not found")
|
|
|
|
|
|
|
|
// ErrConfirmHintNotFound is an error returned when a confirm hint for a
|
|
|
|
// transaction was not found.
|
|
|
|
ErrConfirmHintNotFound = errors.New("confirm hint not found")
|
|
|
|
)
|
|
|
|
|
2020-07-15 05:19:33 +03:00
|
|
|
// CacheConfig contains the HeightHintCache configuration
|
|
|
|
type CacheConfig struct {
|
|
|
|
// QueryDisable prevents reliance on the Height Hint Cache. This is
|
|
|
|
// necessary to recover from an edge case when the height recorded in
|
|
|
|
// the cache is higher than the actual height of a spend, causing a
|
|
|
|
// channel to become "stuck" in a pending close state.
|
|
|
|
QueryDisable bool
|
2020-06-27 04:40:00 +03:00
|
|
|
}
|
|
|
|
|
2018-05-21 18:47:39 +03:00
|
|
|
// SpendHintCache is an interface whose duty is to cache spend hints for
|
|
|
|
// outpoints. A spend hint is defined as the earliest height in the chain at
|
|
|
|
// which an outpoint could have been spent within.
|
|
|
|
type SpendHintCache interface {
|
|
|
|
// CommitSpendHint commits a spend hint for the outpoints to the cache.
|
2018-12-07 08:13:35 +03:00
|
|
|
CommitSpendHint(height uint32, spendRequests ...SpendRequest) error
|
2018-05-21 18:47:39 +03:00
|
|
|
|
|
|
|
// QuerySpendHint returns the latest spend hint for an outpoint.
|
|
|
|
// ErrSpendHintNotFound is returned if a spend hint does not exist
|
|
|
|
// within the cache for the outpoint.
|
2018-12-07 08:13:35 +03:00
|
|
|
QuerySpendHint(spendRequest SpendRequest) (uint32, error)
|
2018-05-21 18:47:39 +03:00
|
|
|
|
|
|
|
// PurgeSpendHint removes the spend hint for the outpoints from the
|
|
|
|
// cache.
|
2018-12-07 08:13:35 +03:00
|
|
|
PurgeSpendHint(spendRequests ...SpendRequest) error
|
2018-05-21 18:47:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// ConfirmHintCache is an interface whose duty is to cache confirm hints for
|
|
|
|
// transactions. A confirm hint is defined as the earliest height in the chain
|
|
|
|
// at which a transaction could have been included in a block.
|
|
|
|
type ConfirmHintCache interface {
|
|
|
|
// CommitConfirmHint commits a confirm hint for the transactions to the
|
|
|
|
// cache.
|
2018-12-07 08:13:35 +03:00
|
|
|
CommitConfirmHint(height uint32, confRequests ...ConfRequest) error
|
2018-05-21 18:47:39 +03:00
|
|
|
|
|
|
|
// QueryConfirmHint returns the latest confirm hint for a transaction
|
|
|
|
// hash. ErrConfirmHintNotFound is returned if a confirm hint does not
|
|
|
|
// exist within the cache for the transaction hash.
|
2018-12-07 08:13:35 +03:00
|
|
|
QueryConfirmHint(confRequest ConfRequest) (uint32, error)
|
2018-05-21 18:47:39 +03:00
|
|
|
|
|
|
|
// PurgeConfirmHint removes the confirm hint for the transactions from
|
|
|
|
// the cache.
|
2018-12-07 08:13:35 +03:00
|
|
|
PurgeConfirmHint(confRequests ...ConfRequest) error
|
2018-05-21 18:47:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// HeightHintCache is an implementation of the SpendHintCache and
|
|
|
|
// ConfirmHintCache interfaces backed by a channeldb DB instance where the hints
|
|
|
|
// will be stored.
|
|
|
|
type HeightHintCache struct {
|
2020-07-15 05:19:33 +03:00
|
|
|
cfg CacheConfig
|
2020-06-27 04:40:00 +03:00
|
|
|
db *channeldb.DB
|
2018-05-21 18:47:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Compile-time checks to ensure HeightHintCache satisfies the SpendHintCache
|
|
|
|
// and ConfirmHintCache interfaces.
|
|
|
|
var _ SpendHintCache = (*HeightHintCache)(nil)
|
|
|
|
var _ ConfirmHintCache = (*HeightHintCache)(nil)
|
|
|
|
|
|
|
|
// NewHeightHintCache returns a new height hint cache backed by a database.
|
2020-07-15 05:19:33 +03:00
|
|
|
func NewHeightHintCache(cfg CacheConfig, db *channeldb.DB) (*HeightHintCache, error) {
|
2020-06-27 04:40:00 +03:00
|
|
|
cache := &HeightHintCache{cfg, db}
|
2018-05-21 18:47:39 +03:00
|
|
|
if err := cache.initBuckets(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return cache, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// initBuckets ensures that the primary buckets used by the circuit are
|
|
|
|
// initialized so that we can assume their existence after startup.
|
|
|
|
func (c *HeightHintCache) initBuckets() error {
|
2020-01-10 05:46:38 +03:00
|
|
|
return kvdb.Batch(c.db.Backend, func(tx kvdb.RwTx) error {
|
|
|
|
_, err := tx.CreateTopLevelBucket(spendHintBucket)
|
2018-05-21 18:47:39 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-01-10 05:46:38 +03:00
|
|
|
_, err = tx.CreateTopLevelBucket(confirmHintBucket)
|
2018-05-21 18:47:39 +03:00
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// CommitSpendHint commits a spend hint for the outpoints to the cache.
|
2018-12-07 08:13:35 +03:00
|
|
|
func (c *HeightHintCache) CommitSpendHint(height uint32,
|
|
|
|
spendRequests ...SpendRequest) error {
|
|
|
|
|
|
|
|
if len(spendRequests) == 0 {
|
2018-10-05 12:07:55 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-12-07 08:13:35 +03:00
|
|
|
Log.Tracef("Updating spend hint to height %d for %v", height,
|
|
|
|
spendRequests)
|
2018-05-21 18:47:39 +03:00
|
|
|
|
2020-01-10 05:46:38 +03:00
|
|
|
return kvdb.Batch(c.db.Backend, func(tx kvdb.RwTx) error {
|
|
|
|
spendHints := tx.ReadWriteBucket(spendHintBucket)
|
2018-05-21 18:47:39 +03:00
|
|
|
if spendHints == nil {
|
|
|
|
return ErrCorruptedHeightHintCache
|
|
|
|
}
|
|
|
|
|
|
|
|
var hint bytes.Buffer
|
|
|
|
if err := channeldb.WriteElement(&hint, height); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-12-07 08:13:35 +03:00
|
|
|
for _, spendRequest := range spendRequests {
|
|
|
|
spendHintKey, err := spendRequest.SpendHintKey()
|
2018-05-21 18:47:39 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-12-07 08:13:35 +03:00
|
|
|
err = spendHints.Put(spendHintKey, hint.Bytes())
|
2018-05-21 18:47:39 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// QuerySpendHint returns the latest spend hint for an outpoint.
|
|
|
|
// ErrSpendHintNotFound is returned if a spend hint does not exist within the
|
|
|
|
// cache for the outpoint.
|
2018-12-07 08:13:35 +03:00
|
|
|
func (c *HeightHintCache) QuerySpendHint(spendRequest SpendRequest) (uint32, error) {
|
2018-05-21 18:47:39 +03:00
|
|
|
var hint uint32
|
2020-07-15 05:19:33 +03:00
|
|
|
if c.cfg.QueryDisable {
|
2020-07-15 05:25:03 +03:00
|
|
|
Log.Debugf("Ignoring spend height hint for %v (height hint cache "+
|
|
|
|
"query disabled)", spendRequest)
|
2020-06-27 04:40:00 +03:00
|
|
|
return 0, nil
|
|
|
|
}
|
2020-05-07 01:45:50 +03:00
|
|
|
err := kvdb.View(c.db, func(tx kvdb.RTx) error {
|
2020-01-10 05:46:38 +03:00
|
|
|
spendHints := tx.ReadBucket(spendHintBucket)
|
2018-05-21 18:47:39 +03:00
|
|
|
if spendHints == nil {
|
|
|
|
return ErrCorruptedHeightHintCache
|
|
|
|
}
|
|
|
|
|
2018-12-07 08:13:35 +03:00
|
|
|
spendHintKey, err := spendRequest.SpendHintKey()
|
|
|
|
if err != nil {
|
2018-05-21 18:47:39 +03:00
|
|
|
return err
|
|
|
|
}
|
2018-12-07 08:13:35 +03:00
|
|
|
spendHint := spendHints.Get(spendHintKey)
|
2018-05-21 18:47:39 +03:00
|
|
|
if spendHint == nil {
|
|
|
|
return ErrSpendHintNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
return channeldb.ReadElement(bytes.NewReader(spendHint), &hint)
|
2020-10-20 17:18:40 +03:00
|
|
|
}, func() {
|
|
|
|
hint = 0
|
2018-05-21 18:47:39 +03:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return hint, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PurgeSpendHint removes the spend hint for the outpoints from the cache.
|
2018-12-07 08:13:35 +03:00
|
|
|
func (c *HeightHintCache) PurgeSpendHint(spendRequests ...SpendRequest) error {
|
|
|
|
if len(spendRequests) == 0 {
|
2018-10-05 12:07:55 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-12-07 08:13:35 +03:00
|
|
|
Log.Tracef("Removing spend hints for %v", spendRequests)
|
2018-05-21 18:47:39 +03:00
|
|
|
|
2020-01-10 05:46:38 +03:00
|
|
|
return kvdb.Batch(c.db.Backend, func(tx kvdb.RwTx) error {
|
|
|
|
spendHints := tx.ReadWriteBucket(spendHintBucket)
|
2018-05-21 18:47:39 +03:00
|
|
|
if spendHints == nil {
|
|
|
|
return ErrCorruptedHeightHintCache
|
|
|
|
}
|
|
|
|
|
2018-12-07 08:13:35 +03:00
|
|
|
for _, spendRequest := range spendRequests {
|
|
|
|
spendHintKey, err := spendRequest.SpendHintKey()
|
2018-05-21 18:47:39 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-12-07 08:13:35 +03:00
|
|
|
if err := spendHints.Delete(spendHintKey); err != nil {
|
2018-05-21 18:47:39 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// CommitConfirmHint commits a confirm hint for the transactions to the cache.
|
2018-12-07 08:13:35 +03:00
|
|
|
func (c *HeightHintCache) CommitConfirmHint(height uint32,
|
|
|
|
confRequests ...ConfRequest) error {
|
|
|
|
|
|
|
|
if len(confRequests) == 0 {
|
2018-10-05 12:07:55 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-12-07 08:13:35 +03:00
|
|
|
Log.Tracef("Updating confirm hints to height %d for %v", height,
|
|
|
|
confRequests)
|
2018-05-21 18:47:39 +03:00
|
|
|
|
2020-01-10 05:46:38 +03:00
|
|
|
return kvdb.Batch(c.db.Backend, func(tx kvdb.RwTx) error {
|
|
|
|
confirmHints := tx.ReadWriteBucket(confirmHintBucket)
|
2018-05-21 18:47:39 +03:00
|
|
|
if confirmHints == nil {
|
|
|
|
return ErrCorruptedHeightHintCache
|
|
|
|
}
|
|
|
|
|
|
|
|
var hint bytes.Buffer
|
|
|
|
if err := channeldb.WriteElement(&hint, height); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-12-07 08:13:35 +03:00
|
|
|
for _, confRequest := range confRequests {
|
|
|
|
confHintKey, err := confRequest.ConfHintKey()
|
2018-05-21 18:47:39 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-12-07 08:13:35 +03:00
|
|
|
err = confirmHints.Put(confHintKey, hint.Bytes())
|
2018-05-21 18:47:39 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// QueryConfirmHint returns the latest confirm hint for a transaction hash.
|
|
|
|
// ErrConfirmHintNotFound is returned if a confirm hint does not exist within
|
|
|
|
// the cache for the transaction hash.
|
2018-12-07 08:13:35 +03:00
|
|
|
func (c *HeightHintCache) QueryConfirmHint(confRequest ConfRequest) (uint32, error) {
|
2018-05-21 18:47:39 +03:00
|
|
|
var hint uint32
|
2020-07-15 05:19:33 +03:00
|
|
|
if c.cfg.QueryDisable {
|
2020-07-15 05:25:03 +03:00
|
|
|
Log.Debugf("Ignoring confirmation height hint for %v (height hint "+
|
|
|
|
"cache query disabled)", confRequest)
|
2020-06-27 04:40:00 +03:00
|
|
|
return 0, nil
|
|
|
|
}
|
2020-05-07 01:45:50 +03:00
|
|
|
err := kvdb.View(c.db, func(tx kvdb.RTx) error {
|
2020-01-10 05:46:38 +03:00
|
|
|
confirmHints := tx.ReadBucket(confirmHintBucket)
|
2018-05-21 18:47:39 +03:00
|
|
|
if confirmHints == nil {
|
|
|
|
return ErrCorruptedHeightHintCache
|
|
|
|
}
|
|
|
|
|
2018-12-07 08:13:35 +03:00
|
|
|
confHintKey, err := confRequest.ConfHintKey()
|
|
|
|
if err != nil {
|
2018-05-21 18:47:39 +03:00
|
|
|
return err
|
|
|
|
}
|
2018-12-07 08:13:35 +03:00
|
|
|
confirmHint := confirmHints.Get(confHintKey)
|
2018-05-21 18:47:39 +03:00
|
|
|
if confirmHint == nil {
|
|
|
|
return ErrConfirmHintNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
return channeldb.ReadElement(bytes.NewReader(confirmHint), &hint)
|
2020-10-20 17:18:40 +03:00
|
|
|
}, func() {
|
|
|
|
hint = 0
|
2018-05-21 18:47:39 +03:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return hint, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PurgeConfirmHint removes the confirm hint for the transactions from the
|
|
|
|
// cache.
|
2018-12-07 08:13:35 +03:00
|
|
|
func (c *HeightHintCache) PurgeConfirmHint(confRequests ...ConfRequest) error {
|
|
|
|
if len(confRequests) == 0 {
|
2018-10-05 12:07:55 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-12-07 08:13:35 +03:00
|
|
|
Log.Tracef("Removing confirm hints for %v", confRequests)
|
2018-05-21 18:47:39 +03:00
|
|
|
|
2020-01-10 05:46:38 +03:00
|
|
|
return kvdb.Batch(c.db.Backend, func(tx kvdb.RwTx) error {
|
|
|
|
confirmHints := tx.ReadWriteBucket(confirmHintBucket)
|
2018-05-21 18:47:39 +03:00
|
|
|
if confirmHints == nil {
|
|
|
|
return ErrCorruptedHeightHintCache
|
|
|
|
}
|
|
|
|
|
2018-12-07 08:13:35 +03:00
|
|
|
for _, confRequest := range confRequests {
|
|
|
|
confHintKey, err := confRequest.ConfHintKey()
|
2018-05-21 18:47:39 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-12-07 08:13:35 +03:00
|
|
|
if err := confirmHints.Delete(confHintKey); err != nil {
|
2018-05-21 18:47:39 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|