mirror of
https://github.com/sparrowwallet/drongo.git
synced 2024-11-02 18:26:43 +00:00
use an address node cache to avoid repeat wallet address lookups
This commit is contained in:
parent
bd01cb8730
commit
6d0d5b7f62
1 changed files with 26 additions and 10 deletions
|
@ -1,6 +1,7 @@
|
||||||
package com.sparrowwallet.drongo.wallet;
|
package com.sparrowwallet.drongo.wallet;
|
||||||
|
|
||||||
import com.sparrowwallet.drongo.address.Address;
|
import com.sparrowwallet.drongo.address.Address;
|
||||||
|
import com.sparrowwallet.drongo.protocol.Script;
|
||||||
import com.sparrowwallet.drongo.protocol.Sha256Hash;
|
import com.sparrowwallet.drongo.protocol.Sha256Hash;
|
||||||
import com.sparrowwallet.drongo.protocol.Transaction;
|
import com.sparrowwallet.drongo.protocol.Transaction;
|
||||||
import com.sparrowwallet.drongo.psbt.PSBT;
|
import com.sparrowwallet.drongo.psbt.PSBT;
|
||||||
|
@ -21,6 +22,8 @@ public class WalletTransaction {
|
||||||
private final long fee;
|
private final long fee;
|
||||||
private final Map<Sha256Hash, BlockTransaction> inputTransactions;
|
private final Map<Sha256Hash, BlockTransaction> inputTransactions;
|
||||||
|
|
||||||
|
private Map<Wallet, Map<Address, WalletNode>> addressNodeMap = new HashMap<>();
|
||||||
|
|
||||||
public WalletTransaction(Wallet wallet, Transaction transaction, List<UtxoSelector> utxoSelectors, List<Map<BlockTransactionHashIndex, WalletNode>> selectedUtxoSets, List<Payment> payments, long fee) {
|
public WalletTransaction(Wallet wallet, Transaction transaction, List<UtxoSelector> utxoSelectors, List<Map<BlockTransactionHashIndex, WalletNode>> selectedUtxoSets, List<Payment> payments, long fee) {
|
||||||
this(wallet, transaction, utxoSelectors, selectedUtxoSets, payments, Collections.emptyMap(), fee);
|
this(wallet, transaction, utxoSelectors, selectedUtxoSets, payments, Collections.emptyMap(), fee);
|
||||||
}
|
}
|
||||||
|
@ -139,24 +142,37 @@ public class WalletTransaction {
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isWalletSend(Wallet wallet, Payment payment) {
|
public boolean isWalletSend(Wallet wallet, Payment payment) {
|
||||||
if(payment.getAddress() != null && wallet != null) {
|
if(wallet == null) {
|
||||||
return wallet.isWalletOutputScript(payment.getAddress().getOutputScript());
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return getAddressNodeMap(wallet).get(payment.getAddress()) != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<WalletNode> getConsolidationSendNodes() {
|
public void updateAddressNodeMap(Map<Wallet, Map<Address, WalletNode>> addressNodeMap, Wallet wallet) {
|
||||||
List<WalletNode> walletNodes = new ArrayList<>();
|
this.addressNodeMap = addressNodeMap;
|
||||||
|
getAddressNodeMap(wallet);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<Address, WalletNode> getAddressNodeMap(Wallet wallet) {
|
||||||
|
Map<Script, WalletNode> walletOutputScripts = null;
|
||||||
|
|
||||||
|
Map<Address, WalletNode> walletAddressNodeMap = addressNodeMap.computeIfAbsent(wallet, w -> new LinkedHashMap<>());
|
||||||
for(Payment payment : payments) {
|
for(Payment payment : payments) {
|
||||||
if(payment.getAddress() != null && getWallet() != null) {
|
if(walletAddressNodeMap.containsKey(payment.getAddress())) {
|
||||||
WalletNode walletNode = getWallet().getWalletOutputScripts().get(payment.getAddress().getOutputScript());
|
continue;
|
||||||
if(walletNode != null) {
|
}
|
||||||
walletNodes.add(walletNode);
|
|
||||||
|
if(payment.getAddress() != null && wallet != null) {
|
||||||
|
if(walletOutputScripts == null) {
|
||||||
|
walletOutputScripts = wallet.getWalletOutputScripts();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
WalletNode walletNode = walletOutputScripts.get(payment.getAddress().getOutputScript());
|
||||||
|
walletAddressNodeMap.put(payment.getAddress(), walletNode);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return walletNodes;
|
return walletAddressNodeMap;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue