wallet: parameterize lock duration

Allow arbitrary lock durations instead of a hard-coded ten minute
period.
This commit is contained in:
Joost Jager 2021-03-12 08:45:36 +01:00
parent ff2577a0e7
commit 8932811695
3 changed files with 7 additions and 8 deletions

View file

@ -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

View file

@ -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
}

View file

@ -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)
}