wallet: add mutex for locked outpoints

This commit is contained in:
Oliver Gugger 2020-08-13 20:33:31 +02:00
parent ca24ed58cf
commit f8cc233758
No known key found for this signature in database
GPG key ID: 8E4256593F177720

View file

@ -95,7 +95,8 @@ type Wallet struct {
chainClientSynced bool chainClientSynced bool
chainClientSyncMtx sync.Mutex chainClientSyncMtx sync.Mutex
lockedOutpoints map[wire.OutPoint]struct{} lockedOutpoints map[wire.OutPoint]struct{}
lockedOutpointsMtx sync.Mutex
recoveryWindow uint32 recoveryWindow uint32
@ -2826,6 +2827,9 @@ func (w *Wallet) ImportPrivateKey(scope waddrmgr.KeyScope, wif *btcutil.WIF,
// LockedOutpoint returns whether an outpoint has been marked as locked and // LockedOutpoint returns whether an outpoint has been marked as locked and
// should not be used as an input for created transactions. // should not be used as an input for created transactions.
func (w *Wallet) LockedOutpoint(op wire.OutPoint) bool { func (w *Wallet) LockedOutpoint(op wire.OutPoint) bool {
w.lockedOutpointsMtx.Lock()
defer w.lockedOutpointsMtx.Unlock()
_, locked := w.lockedOutpoints[op] _, locked := w.lockedOutpoints[op]
return locked return locked
} }
@ -2833,18 +2837,27 @@ func (w *Wallet) LockedOutpoint(op wire.OutPoint) bool {
// LockOutpoint marks an outpoint as locked, that is, it should not be used as // LockOutpoint marks an outpoint as locked, that is, it should not be used as
// an input for newly created transactions. // an input for newly created transactions.
func (w *Wallet) LockOutpoint(op wire.OutPoint) { func (w *Wallet) LockOutpoint(op wire.OutPoint) {
w.lockedOutpointsMtx.Lock()
defer w.lockedOutpointsMtx.Unlock()
w.lockedOutpoints[op] = struct{}{} w.lockedOutpoints[op] = struct{}{}
} }
// UnlockOutpoint marks an outpoint as unlocked, that is, it may be used as an // UnlockOutpoint marks an outpoint as unlocked, that is, it may be used as an
// input for newly created transactions. // input for newly created transactions.
func (w *Wallet) UnlockOutpoint(op wire.OutPoint) { func (w *Wallet) UnlockOutpoint(op wire.OutPoint) {
w.lockedOutpointsMtx.Lock()
defer w.lockedOutpointsMtx.Unlock()
delete(w.lockedOutpoints, op) delete(w.lockedOutpoints, op)
} }
// ResetLockedOutpoints resets the set of locked outpoints so all may be used // ResetLockedOutpoints resets the set of locked outpoints so all may be used
// as inputs for new transactions. // as inputs for new transactions.
func (w *Wallet) ResetLockedOutpoints() { func (w *Wallet) ResetLockedOutpoints() {
w.lockedOutpointsMtx.Lock()
defer w.lockedOutpointsMtx.Unlock()
w.lockedOutpoints = map[wire.OutPoint]struct{}{} w.lockedOutpoints = map[wire.OutPoint]struct{}{}
} }
@ -2852,6 +2865,9 @@ func (w *Wallet) ResetLockedOutpoints() {
// intended to be used by marshaling the result as a JSON array for // intended to be used by marshaling the result as a JSON array for
// listlockunspent RPC results. // listlockunspent RPC results.
func (w *Wallet) LockedOutpoints() []btcjson.TransactionInput { func (w *Wallet) LockedOutpoints() []btcjson.TransactionInput {
w.lockedOutpointsMtx.Lock()
defer w.lockedOutpointsMtx.Unlock()
locked := make([]btcjson.TransactionInput, len(w.lockedOutpoints)) locked := make([]btcjson.TransactionInput, len(w.lockedOutpoints))
i := 0 i := 0
for op := range w.lockedOutpoints { for op := range w.lockedOutpoints {