From ff2577a0e76edb96f35caa7e6a9af9b3bb718da2 Mon Sep 17 00:00:00 2001 From: Joost Jager Date: Fri, 12 Mar 2021 09:26:37 +0100 Subject: [PATCH] wtxmgr: add ListLeases --- wallet/wallet.go | 13 +++++++++++++ wtxmgr/tx.go | 30 ++++++++++++++++++++++++++++++ wtxmgr/tx_test.go | 17 +++++++++-------- 3 files changed, 52 insertions(+), 8 deletions(-) diff --git a/wallet/wallet.go b/wallet/wallet.go index e4d7172..760462d 100644 --- a/wallet/wallet.go +++ b/wallet/wallet.go @@ -2659,6 +2659,19 @@ func (w *Wallet) ListUnspent(minconf, maxconf int32, return results, err } +// ListLeasedOutputs returns a list of objects representing the currently locked +// utxos. +func (w *Wallet) ListLeasedOutputs() ([]*wtxmgr.LockedOutput, error) { + var outputs []*wtxmgr.LockedOutput + err := walletdb.View(w.db, func(tx walletdb.ReadTx) error { + ns := tx.ReadBucket(wtxmgrNamespaceKey) + var err error + outputs, err = w.TxStore.ListLockedOutputs(ns) + return err + }) + return outputs, err +} + // DumpPrivKeys returns the WIF-encoded private keys for all addresses with // private keys in a wallet. func (w *Wallet) DumpPrivKeys() ([]string, error) { diff --git a/wtxmgr/tx.go b/wtxmgr/tx.go index 9ef7329..9353fe7 100644 --- a/wtxmgr/tx.go +++ b/wtxmgr/tx.go @@ -125,6 +125,14 @@ type TxRecord struct { SerializedTx []byte // Optional: may be nil } +// LockedOutput is a type that contains an outpoint of an UTXO and its lock +// lease information. +type LockedOutput struct { + Outpoint wire.OutPoint + LockID LockID + Expiration time.Time +} + // NewTxRecord creates a new transaction record that may be inserted into the // store. It uses memoization to save the transaction hash and the serialized // transaction. @@ -1226,3 +1234,25 @@ func (s *Store) DeleteExpiredLockedOutputs(ns walletdb.ReadWriteBucket) error { return nil } + +// ListLockedOutputs returns a list of objects representing the currently locked +// utxos. +func (s *Store) ListLockedOutputs(ns walletdb.ReadBucket) ([]*LockedOutput, + error) { + + var outputs []*LockedOutput + err := forEachLockedOutput( + ns, func(op wire.OutPoint, id LockID, expiration time.Time) { + outputs = append(outputs, &LockedOutput{ + Outpoint: op, + LockID: id, + Expiration: expiration, + }) + }, + ) + if err != nil { + return nil, err + } + + return outputs, nil +} diff --git a/wtxmgr/tx_test.go b/wtxmgr/tx_test.go index 344b905..6a2f305 100644 --- a/wtxmgr/tx_test.go +++ b/wtxmgr/tx_test.go @@ -2425,19 +2425,20 @@ func assertOutputLocksExist(t *testing.T, s *Store, ns walletdb.ReadBucket, t.Helper() - var found []wire.OutPoint - forEachLockedOutput(ns, func(op wire.OutPoint, _ LockID, _ time.Time) { - found = append(found, op) - }) - if len(found) != len(exp) { + outputs, err := s.ListLockedOutputs(ns) + if err != nil { + t.Fatal(err) + } + + if len(outputs) != len(exp) { t.Fatalf("expected to find %v locked output(s), found %v", - len(exp), len(found)) + len(exp), len(outputs)) } for _, expOp := range exp { exists := false - for _, foundOp := range found { - if expOp == foundOp { + for _, found := range outputs { + if expOp == found.Outpoint { exists = true break }