Skip to content

Commit

Permalink
Improvements (#57)
Browse files Browse the repository at this point in the history
* adds redeemId to SendOutputs method
redemption -> redeem

* SendOutputsWithDataAndRedeemId

* SendOutputsWithDataAndRedeemIdCheck

* updates frost version

* disables tx creation logs unless LogTxCreation is true

* newFilterBlocksRequest cleanup

---------

Co-authored-by: dpiatkivskyi <dmytro.piatkikvskyi@gmailcom>
Co-authored-by: Oleg <[email protected]>
  • Loading branch information
3 people authored Dec 29, 2024
1 parent 79b976f commit 0f62051
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 60 deletions.
36 changes: 18 additions & 18 deletions wallet/createtx.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ func (w *Wallet) findEligibleOutputs(dbtx walletdb.ReadTx,
if err != nil {
return nil, err
}
log.Infof("unspent size: %v", len(unspent))
w.logTxCreation("unspent size: %v", len(unspent))

// TODO: Eventually all of these filters (except perhaps output locking)
// should be handled by the call to UnspentOutputs (or similar).
Expand All @@ -424,34 +424,30 @@ func (w *Wallet) findEligibleOutputs(dbtx walletdb.ReadTx,
// Restrict the selected utxos if a filter function is provided.
if allowUtxo != nil &&
!allowUtxo(*output) {
log.Infof("output: %v", output)
log.Info("Restrict the selected utxos if a filter function is provided.")
w.logTxCreation("Restrict the selected utxos if a filter function is provided. Output: %v", output)
continue
}

// Only include this output if it meets the required number of
// confirmations. Coinbase transactions must have reached
// maturity before their outputs may be spent.
if !confirmed(minconf, output.Height, bs.Height) {
log.Infof("output: %v", output)
log.Infof("tx not confirmed, minconf: %v, output height: %v, bs height: %v, output: %v",
w.logTxCreation("tx not confirmed, minconf: %v, output height: %v, bs height: %v, output: %v",
minconf, output.Height, bs.Height, output)
continue
}
if output.FromCoinBase {
target := int32(w.chainParams.CoinbaseMaturity)
if !confirmed(target, output.Height, bs.Height) {
log.Infof("output: %v", output)
log.Infof("output from coinbase not confirmed, target: %v, output height: %v, bs height: %v, output: %v",
w.logTxCreation("output from coinbase not confirmed, target: %v, output height: %v, bs height: %v, output: %v",
target, output.Height, bs.Height, output)
continue
}
}

// Locked unspent outputs are skipped.
if w.LockedOutpoint(output.OutPoint) {
log.Infof("output: %v", output)
log.Infof("locked unspent outputs are skipped")
w.logTxCreation("locked unspent outputs are skipped. Output: %v", output)
continue
}

Expand All @@ -463,32 +459,36 @@ func (w *Wallet) findEligibleOutputs(dbtx walletdb.ReadTx,
_, addrs, _, err := txscript.ExtractPkScriptAddrs(
output.PkScript, w.chainParams)
if err != nil || len(addrs) != 1 {
log.Infof("output: %v", output)
log.Infof("error extracting pk script addrs: %v", err)
w.logTxCreation("error extracting pk script addrs (%v) for output %v", err, output)
continue
}
scopedMgr, addrAcct, err := w.Manager.AddrAccount(addrmgrNs, addrs[0])
if err != nil {
log.Infof("output: %v", output)
log.Infof("error getting addr account: %v", err)
w.logTxCreation("error getting addr account: %v when processing output %v", err, output)
continue
}
if keyScope != nil && scopedMgr.Scope() != *keyScope {
log.Infof("output: %v", output)
log.Infof("key scope not match, scopedMgr: %v, keyScope: %v", scopedMgr.Scope(), *keyScope)
w.logTxCreation("key scope not match, scopedMgr: %v, keyScope: %v when processing output %v",
scopedMgr.Scope(), *keyScope)
continue
}
if addrAcct != account {
log.Infof("output: %v", output)
log.Infof("addr account not match, addrAcct: %v, account: %v", addrAcct, account)
w.logTxCreation("addr account not match, addrAcct: %v, account: %v when processing output %v",
addrAcct, account, output)
continue
}
eligible = append(eligible, *output)
}
log.Infof("eligible outputs size: %v", len(eligible))
w.logTxCreation("eligible outputs size: %v", len(eligible))
return eligible, nil
}

func (w *Wallet) logTxCreation(format string, params ...interface{}) {
if w.LogTxCreation {
log.Infof(format, params...)
}
}

// inputYieldsPositively returns a boolean indicating whether this input yields
// positively if added to a transaction. This determination is based on the
// best-case added virtual size. For edge cases this function can return true
Expand Down
47 changes: 5 additions & 42 deletions wallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ type Wallet struct {
FrostSigner frost.Signer
ChangeAddressKey *btcec.PublicKey

LogTxCreation bool

AddressMapStorage *AddressMapStorage
Pk1, Pk2 *btcec.PublicKey
FeeCoefficient float64
Expand Down Expand Up @@ -885,7 +887,7 @@ expandHorizons:
// construct the filter blocks request. The request includes the range
// of blocks we intend to scan, in addition to the scope-index -> addr
// map for all internal and external branches.
filterReq := newFilterBlocksRequest(w, batch, scopedMgrs, recoveryState)
filterReq := newFilterBlocksRequest(batch, scopedMgrs, recoveryState)

// Initiate the filter blocks request using our chain backend. If an
// error occurs, we are unable to proceed with the recovery.
Expand Down Expand Up @@ -1051,7 +1053,8 @@ func internalKeyPath(index uint32) waddrmgr.DerivationPath {

// newFilterBlocksRequest constructs FilterBlocksRequests using our current
// block range, scoped managers, and recovery state.
func newFilterBlocksRequest(w *Wallet, batch []wtxmgr.BlockMeta, scopedMgrs map[waddrmgr.KeyScope]*waddrmgr.ScopedKeyManager, recoveryState *RecoveryState) *chain.FilterBlocksRequest {
func newFilterBlocksRequest(batch []wtxmgr.BlockMeta, scopedMgrs map[waddrmgr.KeyScope]*waddrmgr.ScopedKeyManager,
recoveryState *RecoveryState) *chain.FilterBlocksRequest {

filterReq := &chain.FilterBlocksRequest{
Blocks: batch,
Expand All @@ -1060,26 +1063,12 @@ func newFilterBlocksRequest(w *Wallet, batch []wtxmgr.BlockMeta, scopedMgrs map[
WatchedOutPoints: recoveryState.WatchedOutPoints(),
}

addresses, err := w.AccountAddresses(waddrmgr.ImportedAddrAccount)
if err == nil {
log.Infof("newFilterBlocksRequest: imported addresses size: %v", len(addresses))
for _, addr := range addresses {
log.Infof("newFilterBlocksRequest: address %v", addr)
}
} else {
log.Errorf("newFilterBlocksRequest: error getting imported addresses: %v", err)
}

// Populate the external and internal addresses by merging the addresses
// sets belong to all currently tracked scopes.
for scope := range scopedMgrs {
scopeState := recoveryState.StateForScope(scope)

for index, addr := range scopeState.ExternalBranch.Addrs() {
present := isPresent(addresses, addr)
if present {
log.Infof("ExternalBranch address for scope %v: %v matches an imported address", scope, addr)
}
scopedIndex := waddrmgr.ScopedIndex{
Scope: scope,
Index: index,
Expand All @@ -1089,10 +1078,6 @@ func newFilterBlocksRequest(w *Wallet, batch []wtxmgr.BlockMeta, scopedMgrs map[
}

for index, addr := range scopeState.InternalBranch.Addrs() {
present := isPresent(addresses, addr)
if present {
log.Infof("InternalBranch address for scope %v: %v matches an imported address", scope, addr)
}
scopedIndex := waddrmgr.ScopedIndex{
Scope: scope,
Index: index,
Expand All @@ -1104,18 +1089,6 @@ func newFilterBlocksRequest(w *Wallet, batch []wtxmgr.BlockMeta, scopedMgrs map[
return filterReq
}

func isPresent(addresses []btcutil.Address, externalBranchAddress btcutil.Address) bool {
if addresses == nil || externalBranchAddress == nil {
return false
}
for _, addr := range addresses {
if addr.EncodeAddress() == externalBranchAddress.EncodeAddress() {
return true
}
}
return false
}

// extendFoundAddresses accepts a filter blocks response that contains addresses
// found on chain, and advances the state of all relevant derivation paths to
// match the highest found child index for each branch.
Expand Down Expand Up @@ -3562,16 +3535,6 @@ func (w *Wallet) SendOutputs(outputs []*wire.TxOut, keyScope *waddrmgr.KeyScope,
)
}

func (w *Wallet) SendOutputsWithData(outputs []*wire.TxOut, keyScope *waddrmgr.KeyScope,
account uint32, minconf int32, satPerKb btcutil.Amount,
coinSelectionStrategy CoinSelectionStrategy, label string, data []byte) (*wire.MsgTx, error) {

return w.sendOutputs(
outputs, keyScope, account, minconf, satPerKb,
coinSelectionStrategy, label, 0, data,
)
}

func (w *Wallet) SendOutputsWithDataAndRedeemIdCheck(outputs []*wire.TxOut, keyScope *waddrmgr.KeyScope,
account uint32, minconf int32, satPerKb btcutil.Amount,
coinSelectionStrategy CoinSelectionStrategy, label string, redeemId uint32, start, end *BlockIdentifier, data []byte) (*wire.MsgTx,
Expand Down

0 comments on commit 0f62051

Please sign in to comment.