Skip to content

Commit

Permalink
wallet: add ReleaseInputs
Browse files Browse the repository at this point in the history
  • Loading branch information
peterjan committed Jan 30, 2024
1 parent 22a9296 commit ce5821f
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions wallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,14 +243,14 @@ func (sw *SingleAddressWallet) SpendableOutputs() ([]types.SiacoinElement, error
// transaction. If necessary, a change output will also be added. The inputs
// will not be available to future calls to FundTransaction unless ReleaseInputs
// is called.
func (sw *SingleAddressWallet) FundTransaction(txn *types.Transaction, amount types.Currency, useUnconfirmed bool) ([]types.Hash256, func(), error) {
func (sw *SingleAddressWallet) FundTransaction(txn *types.Transaction, amount types.Currency, useUnconfirmed bool) ([]types.Hash256, error) {
if amount.IsZero() {
return nil, func() {}, nil
return nil, nil
}

utxos, err := sw.store.UnspentSiacoinElements()
if err != nil {
return nil, nil, err
return nil, err
}

tpoolSpent := make(map[types.Hash256]bool)
Expand Down Expand Up @@ -327,10 +327,10 @@ func (sw *SingleAddressWallet) FundTransaction(txn *types.Transaction, amount ty

if inputSum.Cmp(amount) < 0 {
// still not enough funds
return nil, nil, ErrNotEnoughFunds
return nil, ErrNotEnoughFunds
}
} else if inputSum.Cmp(amount) < 0 {
return nil, nil, ErrNotEnoughFunds
return nil, ErrNotEnoughFunds
}

// check if remaining utxos should be defragged
Expand Down Expand Up @@ -371,14 +371,7 @@ func (sw *SingleAddressWallet) FundTransaction(txn *types.Transaction, amount ty
sw.locked[sce.ID] = time.Now().Add(sw.cfg.ReservationDuration)
}

release := func() {
sw.mu.Lock()
defer sw.mu.Unlock()
for _, id := range toSign {
delete(sw.locked, id)
}
}
return toSign, release, nil
return toSign, nil
}

// SignTransaction adds a signature to each of the specified inputs.
Expand Down Expand Up @@ -576,6 +569,19 @@ func (sw *SingleAddressWallet) Redistribute(outputs int, amount, feePerByte type
return
}

// ReleaseInputs is a helper function that releases the inputs of txn for use in
// other transactions. It should only be called on transactions that are invalid
// or will never be broadcast.
func (sw *SingleAddressWallet) ReleaseInputs(txns ...types.Transaction) {
sw.mu.Lock()
defer sw.mu.Unlock()
for _, txn := range txns {
for _, in := range txn.SiacoinInputs {
delete(sw.locked, types.Hash256(in.ParentID))
}
}
}

// IsRelevantTransaction returns true if the v1 transaction is relevant to the
// address
func IsRelevantTransaction(txn types.Transaction, addr types.Address) bool {
Expand Down

0 comments on commit ce5821f

Please sign in to comment.