look at used transactions for fresh nodes

This commit is contained in:
Craig Raw 2020-06-10 14:15:15 +02:00
parent fa30f37e23
commit 0d56692784
2 changed files with 23 additions and 8 deletions

View file

@ -101,29 +101,32 @@ public class Wallet {
}
public int getLookAhead(WalletNode node) {
//TODO: Calculate using seen transactions
int lookAhead = DEFAULT_LOOKAHEAD;
Integer maxIndex = node.getHighestUsedIndex();
if(maxIndex != null) {
lookAhead = Math.max(maxIndex + lookAhead/2, lookAhead);
Integer highestUsed = node.getHighestUsedIndex();
if(highestUsed != null) {
lookAhead = Math.max(highestUsed + lookAhead/2, lookAhead);
}
return lookAhead;
}
public WalletNode getFreshNode(KeyPurpose keyPurpose) {
//TODO: Calculate using seen transactions
return getFreshNode(keyPurpose, null);
}
public WalletNode getFreshNode(KeyPurpose keyPurpose, WalletNode current) {
//TODO: Calculate using seen transactions
int index = 0;
if(current != null) {
WalletNode node = getNode(keyPurpose);
Integer highestUsed = node.getHighestUsedIndex();
if(highestUsed != null) {
index = highestUsed + 1;
}
if(current != null && current.getIndex() >= index) {
index = current.getIndex() + 1;
}
WalletNode node = getNode(keyPurpose);
if(index >= node.getChildren().size()) {
node.fillToIndex(index);
}

View file

@ -107,6 +107,15 @@ public class WalletNode implements Comparable<WalletNode> {
return unspentTXOs.stream().filter(txo -> !txo.isSpent()).collect(Collectors.toCollection(HashSet::new));
}
public long getUnspentValue() {
long value = 0L;
for(BlockTransactionHashIndex utxo : getUnspentTransactionOutputs()) {
value += utxo.getValue();
}
return value;
}
public void fillToIndex(int index) {
for(int i = 0; i <= index; i++) {
WalletNode node = new WalletNode(getKeyPurpose(), i);
@ -114,6 +123,9 @@ public class WalletNode implements Comparable<WalletNode> {
}
}
/**
* @return The highest used index, or null if no addresses are used
*/
public Integer getHighestUsedIndex() {
WalletNode highestNode = null;
for(WalletNode childNode : getChildren()) {