From 89328116958a27745b6a69deb4efd79576b20fe5 Mon Sep 17 00:00:00 2001 From: Joost Jager Date: Fri, 12 Mar 2021 08:45:36 +0100 Subject: [PATCH] wallet: parameterize lock duration Allow arbitrary lock durations instead of a hard-coded ten minute period. --- wallet/wallet.go | 6 ++++-- wtxmgr/tx.go | 7 ++----- wtxmgr/tx_test.go | 2 +- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/wallet/wallet.go b/wallet/wallet.go index 760462d..079fda0 100644 --- a/wallet/wallet.go +++ b/wallet/wallet.go @@ -2908,12 +2908,14 @@ func (w *Wallet) LockedOutpoints() []btcjson.TransactionInput { // // NOTE: This differs from LockOutpoint in that outputs are locked for a limited // amount of time and their locks are persisted to disk. -func (w *Wallet) LeaseOutput(id wtxmgr.LockID, op wire.OutPoint) (time.Time, error) { +func (w *Wallet) LeaseOutput(id wtxmgr.LockID, op wire.OutPoint, + duration time.Duration) (time.Time, error) { + var expiry time.Time err := walletdb.Update(w.db, func(tx walletdb.ReadWriteTx) error { ns := tx.ReadWriteBucket(wtxmgrNamespaceKey) var err error - expiry, err = w.TxStore.LockOutput(ns, id, op) + expiry, err = w.TxStore.LockOutput(ns, id, op, duration) return err }) return expiry, err diff --git a/wtxmgr/tx.go b/wtxmgr/tx.go index 9353fe7..19c5fc2 100644 --- a/wtxmgr/tx.go +++ b/wtxmgr/tx.go @@ -24,9 +24,6 @@ import ( const ( // TxLabelLimit is the length limit we impose on transaction labels. TxLabelLimit = 500 - - // DefaultLockDuration is the default duration used to lock outputs. - DefaultLockDuration = 10 * time.Minute ) var ( @@ -1162,7 +1159,7 @@ func isKnownOutput(ns walletdb.ReadWriteBucket, op wire.OutPoint) bool { // already been locked to a different ID, then ErrOutputAlreadyLocked is // returned. func (s *Store) LockOutput(ns walletdb.ReadWriteBucket, id LockID, - op wire.OutPoint) (time.Time, error) { + op wire.OutPoint, duration time.Duration) (time.Time, error) { // Make sure the output is known. if !isKnownOutput(ns, op) { @@ -1175,7 +1172,7 @@ func (s *Store) LockOutput(ns walletdb.ReadWriteBucket, id LockID, return time.Time{}, ErrOutputAlreadyLocked } - expiry := s.clock.Now().Add(DefaultLockDuration) + expiry := s.clock.Now().Add(duration) if err := lockOutput(ns, id, op, expiry); err != nil { return time.Time{}, err } diff --git a/wtxmgr/tx_test.go b/wtxmgr/tx_test.go index 6a2f305..9434845 100644 --- a/wtxmgr/tx_test.go +++ b/wtxmgr/tx_test.go @@ -2454,7 +2454,7 @@ func lock(t *testing.T, s *Store, ns walletdb.ReadWriteBucket, t.Helper() - expiry, err := s.LockOutput(ns, id, op) + expiry, err := s.LockOutput(ns, id, op, 10*time.Minute) if err != exp { t.Fatalf("expected err %q, got %q", exp, err) }