2018-12-07 09:02:11 +01:00
|
|
|
package sweep
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
|
|
|
"errors"
|
2023-10-25 12:45:25 +02:00
|
|
|
"io"
|
2018-12-07 09:02:11 +01:00
|
|
|
|
|
|
|
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
|
|
|
"github.com/btcsuite/btcd/wire"
|
2021-04-26 19:08:11 +02:00
|
|
|
"github.com/lightningnetwork/lnd/kvdb"
|
2023-10-25 12:45:25 +02:00
|
|
|
"github.com/lightningnetwork/lnd/tlv"
|
2018-12-07 09:02:11 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// txHashesBucketKey is the key that points to a bucket containing the
|
|
|
|
// hashes of all sweep txes that were published successfully.
|
|
|
|
//
|
2023-10-25 12:45:25 +02:00
|
|
|
// maps: txHash -> TxRecord
|
2018-12-07 09:02:11 +01:00
|
|
|
txHashesBucketKey = []byte("sweeper-tx-hashes")
|
|
|
|
|
|
|
|
// utxnChainPrefix is the bucket prefix for nursery buckets.
|
|
|
|
utxnChainPrefix = []byte("utxn")
|
|
|
|
|
|
|
|
// utxnHeightIndexKey is the sub bucket where the nursery stores the
|
|
|
|
// height index.
|
|
|
|
utxnHeightIndexKey = []byte("height-index")
|
|
|
|
|
|
|
|
// utxnFinalizedKndrTxnKey is a static key that can be used to locate
|
|
|
|
// the nursery finalized kindergarten sweep txn.
|
|
|
|
utxnFinalizedKndrTxnKey = []byte("finalized-kndr-txn")
|
|
|
|
|
|
|
|
byteOrder = binary.BigEndian
|
2020-05-05 21:10:11 +02:00
|
|
|
|
|
|
|
errNoTxHashesBucket = errors.New("tx hashes bucket does not exist")
|
2023-10-25 12:45:25 +02:00
|
|
|
|
|
|
|
// ErrTxNotFound is returned when querying using a txid that's not
|
|
|
|
// found in our db.
|
|
|
|
ErrTxNotFound = errors.New("tx not found")
|
2018-12-07 09:02:11 +01:00
|
|
|
)
|
|
|
|
|
2023-10-25 12:45:25 +02:00
|
|
|
// TxRecord specifies a record of a tx that's stored in the database.
|
|
|
|
type TxRecord struct {
|
|
|
|
// Txid is the sweeping tx's txid, which is used as the key to store
|
|
|
|
// the following values.
|
|
|
|
Txid chainhash.Hash
|
|
|
|
|
|
|
|
// FeeRate is the fee rate of the sweeping tx, unit is sats/kw.
|
|
|
|
FeeRate uint64
|
|
|
|
|
|
|
|
// Fee is the fee of the sweeping tx, unit is sat.
|
|
|
|
Fee uint64
|
|
|
|
|
|
|
|
// Published indicates whether the tx has been published.
|
|
|
|
Published bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// toTlvStream converts TxRecord into a tlv representation.
|
|
|
|
func (t *TxRecord) toTlvStream() (*tlv.Stream, error) {
|
|
|
|
const (
|
|
|
|
// A set of tlv type definitions used to serialize TxRecord.
|
|
|
|
// We define it here instead of the head of the file to avoid
|
|
|
|
// naming conflicts.
|
|
|
|
//
|
|
|
|
// NOTE: A migration should be added whenever the existing type
|
|
|
|
// changes.
|
|
|
|
//
|
|
|
|
// NOTE: Txid is stored as the key, so it's not included here.
|
|
|
|
feeRateType tlv.Type = 0
|
|
|
|
feeType tlv.Type = 1
|
|
|
|
boolType tlv.Type = 2
|
|
|
|
)
|
|
|
|
|
|
|
|
return tlv.NewStream(
|
|
|
|
tlv.MakeBigSizeRecord(feeRateType, &t.FeeRate),
|
|
|
|
tlv.MakeBigSizeRecord(feeType, &t.Fee),
|
|
|
|
tlv.MakePrimitiveRecord(boolType, &t.Published),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// serializeTxRecord serializes a TxRecord based on tlv format.
|
|
|
|
func serializeTxRecord(w io.Writer, tx *TxRecord) error {
|
|
|
|
// Create the tlv stream.
|
|
|
|
tlvStream, err := tx.toTlvStream()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Encode the tlv stream.
|
|
|
|
var buf bytes.Buffer
|
|
|
|
if err := tlvStream.Encode(&buf); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write the tlv stream.
|
|
|
|
if _, err = w.Write(buf.Bytes()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// deserializeTxRecord deserializes a TxRecord based on tlv format.
|
|
|
|
func deserializeTxRecord(r io.Reader) (*TxRecord, error) {
|
|
|
|
var tx TxRecord
|
|
|
|
|
|
|
|
// Create the tlv stream.
|
|
|
|
tlvStream, err := tx.toTlvStream()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tlvStream.Decode(r); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &tx, nil
|
|
|
|
}
|
|
|
|
|
2018-12-07 09:02:11 +01:00
|
|
|
// SweeperStore stores published txes.
|
|
|
|
type SweeperStore interface {
|
|
|
|
// IsOurTx determines whether a tx is published by us, based on its
|
|
|
|
// hash.
|
|
|
|
IsOurTx(hash chainhash.Hash) (bool, error)
|
|
|
|
|
2023-10-25 08:01:36 +02:00
|
|
|
// StoreTx stores a tx hash we are about to publish.
|
2023-10-25 12:45:25 +02:00
|
|
|
StoreTx(*TxRecord) error
|
2018-12-07 09:02:11 +01:00
|
|
|
|
2020-05-05 21:10:11 +02:00
|
|
|
// ListSweeps lists all the sweeps we have successfully published.
|
|
|
|
ListSweeps() ([]chainhash.Hash, error)
|
2018-12-07 09:02:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type sweeperStore struct {
|
2020-01-10 03:46:00 +01:00
|
|
|
db kvdb.Backend
|
2018-12-07 09:02:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewSweeperStore returns a new store instance.
|
2020-01-10 03:46:00 +01:00
|
|
|
func NewSweeperStore(db kvdb.Backend, chainHash *chainhash.Hash) (
|
2018-12-07 09:02:11 +01:00
|
|
|
SweeperStore, error) {
|
|
|
|
|
2020-01-10 03:46:00 +01:00
|
|
|
err := kvdb.Update(db, func(tx kvdb.RwTx) error {
|
|
|
|
if tx.ReadWriteBucket(txHashesBucketKey) != nil {
|
2018-12-07 09:02:11 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-10 03:46:00 +01:00
|
|
|
txHashesBucket, err := tx.CreateTopLevelBucket(
|
|
|
|
txHashesBucketKey,
|
|
|
|
)
|
2018-12-07 09:02:11 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use non-existence of tx hashes bucket as a signal to migrate
|
|
|
|
// nursery finalized txes.
|
|
|
|
err = migrateTxHashes(tx, txHashesBucket, chainHash)
|
|
|
|
|
|
|
|
return err
|
2020-10-26 14:06:32 +01:00
|
|
|
}, func() {})
|
2018-12-07 09:02:11 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &sweeperStore{
|
|
|
|
db: db,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// migrateTxHashes migrates nursery finalized txes to the tx hashes bucket. This
|
|
|
|
// is not implemented as a database migration, to keep the downgrade path open.
|
2023-10-25 12:45:25 +02:00
|
|
|
//
|
|
|
|
// TODO(yy): delete this function once nursery is removed.
|
2020-01-10 03:46:00 +01:00
|
|
|
func migrateTxHashes(tx kvdb.RwTx, txHashesBucket kvdb.RwBucket,
|
2018-12-07 09:02:11 +01:00
|
|
|
chainHash *chainhash.Hash) error {
|
|
|
|
|
|
|
|
log.Infof("Migrating UTXO nursery finalized TXIDs")
|
|
|
|
|
|
|
|
// Compose chain bucket key.
|
|
|
|
var b bytes.Buffer
|
|
|
|
if _, err := b.Write(utxnChainPrefix); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := b.Write(chainHash[:]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get chain bucket if exists.
|
2020-01-10 03:46:00 +01:00
|
|
|
chainBucket := tx.ReadWriteBucket(b.Bytes())
|
2018-12-07 09:02:11 +01:00
|
|
|
if chainBucket == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieve the existing height index.
|
2020-01-10 03:46:00 +01:00
|
|
|
hghtIndex := chainBucket.NestedReadWriteBucket(utxnHeightIndexKey)
|
2018-12-07 09:02:11 +01:00
|
|
|
if hghtIndex == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieve all heights.
|
|
|
|
err := hghtIndex.ForEach(func(k, v []byte) error {
|
2020-01-10 03:46:00 +01:00
|
|
|
heightBucket := hghtIndex.NestedReadWriteBucket(k)
|
2018-12-07 09:02:11 +01:00
|
|
|
if heightBucket == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get finalized tx for height.
|
|
|
|
txBytes := heightBucket.Get(utxnFinalizedKndrTxnKey)
|
|
|
|
if txBytes == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deserialize and skip tx if it cannot be deserialized.
|
|
|
|
tx := &wire.MsgTx{}
|
|
|
|
err := tx.Deserialize(bytes.NewReader(txBytes))
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("Cannot deserialize utxn tx")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate hash.
|
|
|
|
hash := tx.TxHash()
|
|
|
|
|
|
|
|
// Insert utxn tx hash in hashes bucket.
|
|
|
|
log.Debugf("Inserting nursery tx %v in hash list "+
|
|
|
|
"(height=%v)", hash, byteOrder.Uint32(k))
|
|
|
|
|
2023-10-25 12:45:25 +02:00
|
|
|
// Create the transaction record. Since this is an old record,
|
|
|
|
// we can assume it's already been published. Although it's
|
|
|
|
// possible to calculate the fees and fee rate used here, we
|
|
|
|
// skip it as it's unlikely we'd perform RBF on these old
|
|
|
|
// sweeping transactions.
|
|
|
|
tr := &TxRecord{
|
|
|
|
Txid: hash,
|
|
|
|
Published: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Serialize tx record.
|
|
|
|
var b bytes.Buffer
|
|
|
|
err = serializeTxRecord(&b, tr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return txHashesBucket.Put(tr.Txid[:], b.Bytes())
|
2018-12-07 09:02:11 +01:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-25 08:01:36 +02:00
|
|
|
// StoreTx stores that we are about to publish a tx.
|
2023-10-25 12:45:25 +02:00
|
|
|
func (s *sweeperStore) StoreTx(tr *TxRecord) error {
|
2020-01-10 03:46:00 +01:00
|
|
|
return kvdb.Update(s.db, func(tx kvdb.RwTx) error {
|
|
|
|
txHashesBucket := tx.ReadWriteBucket(txHashesBucketKey)
|
2018-12-07 09:02:11 +01:00
|
|
|
if txHashesBucket == nil {
|
2020-05-05 21:10:11 +02:00
|
|
|
return errNoTxHashesBucket
|
2018-12-07 09:02:11 +01:00
|
|
|
}
|
|
|
|
|
2023-10-25 12:45:25 +02:00
|
|
|
// Serialize tx record.
|
|
|
|
var b bytes.Buffer
|
|
|
|
err := serializeTxRecord(&b, tr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return txHashesBucket.Put(tr.Txid[:], b.Bytes())
|
2020-10-26 14:06:32 +01:00
|
|
|
}, func() {})
|
2018-12-07 09:02:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsOurTx determines whether a tx is published by us, based on its
|
|
|
|
// hash.
|
|
|
|
func (s *sweeperStore) IsOurTx(hash chainhash.Hash) (bool, error) {
|
|
|
|
var ours bool
|
|
|
|
|
2020-05-07 00:45:50 +02:00
|
|
|
err := kvdb.View(s.db, func(tx kvdb.RTx) error {
|
2020-01-10 03:46:00 +01:00
|
|
|
txHashesBucket := tx.ReadBucket(txHashesBucketKey)
|
2018-12-07 09:02:11 +01:00
|
|
|
if txHashesBucket == nil {
|
2020-05-05 21:10:11 +02:00
|
|
|
return errNoTxHashesBucket
|
2018-12-07 09:02:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ours = txHashesBucket.Get(hash[:]) != nil
|
|
|
|
|
|
|
|
return nil
|
2020-10-20 16:18:40 +02:00
|
|
|
}, func() {
|
|
|
|
ours = false
|
2018-12-07 09:02:11 +01:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ours, nil
|
|
|
|
}
|
|
|
|
|
2020-05-05 21:10:11 +02:00
|
|
|
// ListSweeps lists all the sweep transactions we have in the sweeper store.
|
|
|
|
func (s *sweeperStore) ListSweeps() ([]chainhash.Hash, error) {
|
|
|
|
var sweepTxns []chainhash.Hash
|
|
|
|
|
2020-05-07 00:45:50 +02:00
|
|
|
if err := kvdb.View(s.db, func(tx kvdb.RTx) error {
|
2020-05-05 21:10:11 +02:00
|
|
|
txHashesBucket := tx.ReadBucket(txHashesBucketKey)
|
|
|
|
if txHashesBucket == nil {
|
|
|
|
return errNoTxHashesBucket
|
|
|
|
}
|
|
|
|
|
|
|
|
return txHashesBucket.ForEach(func(resKey, _ []byte) error {
|
|
|
|
txid, err := chainhash.NewHash(resKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
sweepTxns = append(sweepTxns, *txid)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
2020-10-20 16:18:40 +02:00
|
|
|
}, func() {
|
|
|
|
sweepTxns = nil
|
2020-05-05 21:10:11 +02:00
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return sweepTxns, nil
|
|
|
|
}
|
|
|
|
|
2018-12-07 09:02:11 +01:00
|
|
|
// Compile-time constraint to ensure sweeperStore implements SweeperStore.
|
|
|
|
var _ SweeperStore = (*sweeperStore)(nil)
|