sweep: add list sweeps function

This commit is contained in:
carla
2020-05-05 21:10:11 +02:00
parent 537dac3c62
commit 99a45e968a
4 changed files with 74 additions and 2 deletions

View File

@@ -39,6 +39,8 @@ var (
utxnFinalizedKndrTxnKey = []byte("finalized-kndr-txn")
byteOrder = binary.BigEndian
errNoTxHashesBucket = errors.New("tx hashes bucket does not exist")
)
// SweeperStore stores published txes.
@@ -53,6 +55,9 @@ type SweeperStore interface {
// GetLastPublishedTx returns the last tx that we called NotifyPublishTx
// for.
GetLastPublishedTx() (*wire.MsgTx, error)
// ListSweeps lists all the sweeps we have successfully published.
ListSweeps() ([]chainhash.Hash, error)
}
type sweeperStore struct {
@@ -173,7 +178,7 @@ func (s *sweeperStore) NotifyPublishTx(sweepTx *wire.MsgTx) error {
txHashesBucket := tx.ReadWriteBucket(txHashesBucketKey)
if txHashesBucket == nil {
return errors.New("tx hashes bucket does not exist")
return errNoTxHashesBucket
}
var b bytes.Buffer
@@ -230,7 +235,7 @@ func (s *sweeperStore) IsOurTx(hash chainhash.Hash) (bool, error) {
err := kvdb.View(s.db, func(tx kvdb.ReadTx) error {
txHashesBucket := tx.ReadBucket(txHashesBucketKey)
if txHashesBucket == nil {
return errors.New("tx hashes bucket does not exist")
return errNoTxHashesBucket
}
ours = txHashesBucket.Get(hash[:]) != nil
@@ -244,5 +249,32 @@ func (s *sweeperStore) IsOurTx(hash chainhash.Hash) (bool, error) {
return ours, nil
}
// ListSweeps lists all the sweep transactions we have in the sweeper store.
func (s *sweeperStore) ListSweeps() ([]chainhash.Hash, error) {
var sweepTxns []chainhash.Hash
if err := kvdb.View(s.db, func(tx kvdb.ReadTx) error {
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
})
}); err != nil {
return nil, err
}
return sweepTxns, nil
}
// Compile-time constraint to ensure sweeperStore implements SweeperStore.
var _ SweeperStore = (*sweeperStore)(nil)