mirror of
https://github.com/sparrowwallet/sparrow.git
synced 2024-12-24 12:46:45 +00:00
subscribe to script hash updates for new mempool txes
This commit is contained in:
parent
7428d21cac
commit
b8edfd9731
8 changed files with 160 additions and 24 deletions
|
@ -3,8 +3,8 @@ package com.sparrowwallet.sparrow.event;
|
||||||
import com.sparrowwallet.drongo.wallet.Wallet;
|
import com.sparrowwallet.drongo.wallet.Wallet;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This event is posted if the wallet block height has changed.
|
* This event is posted if the wallet's stored block height has changed.
|
||||||
* Note that it is not posted if the wallet history has also changed - this event is used mainly to ensure the new block height is saved
|
*
|
||||||
*/
|
*/
|
||||||
public class WalletBlockHeightChangedEvent extends WalletChangedEvent {
|
public class WalletBlockHeightChangedEvent extends WalletChangedEvent {
|
||||||
private final Integer blockHeight;
|
private final Integer blockHeight;
|
||||||
|
|
|
@ -3,7 +3,7 @@ package com.sparrowwallet.sparrow.event;
|
||||||
import com.sparrowwallet.drongo.wallet.Wallet;
|
import com.sparrowwallet.drongo.wallet.Wallet;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The base class for all wallet events that should also trigger saving of the wallet
|
* The base class for all wallet events
|
||||||
*/
|
*/
|
||||||
public class WalletChangedEvent {
|
public class WalletChangedEvent {
|
||||||
private final Wallet wallet;
|
private final Wallet wallet;
|
||||||
|
|
|
@ -0,0 +1,13 @@
|
||||||
|
package com.sparrowwallet.sparrow.event;
|
||||||
|
|
||||||
|
import com.sparrowwallet.drongo.wallet.Wallet;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Indicates that the internal data (non-settings) of the wallet has changed, either from a blockchain update or entry label change etc.
|
||||||
|
* Used to trigger a background save of the wallet
|
||||||
|
*/
|
||||||
|
public class WalletDataChangedEvent extends WalletChangedEvent {
|
||||||
|
public WalletDataChangedEvent(Wallet wallet) {
|
||||||
|
super(wallet);
|
||||||
|
}
|
||||||
|
}
|
|
@ -3,7 +3,11 @@ package com.sparrowwallet.sparrow.event;
|
||||||
import com.sparrowwallet.drongo.wallet.Wallet;
|
import com.sparrowwallet.drongo.wallet.Wallet;
|
||||||
import com.sparrowwallet.sparrow.wallet.Entry;
|
import com.sparrowwallet.sparrow.wallet.Entry;
|
||||||
|
|
||||||
public class WalletEntryLabelChangedEvent extends WalletChangedEvent {
|
/**
|
||||||
|
* This event is fired when a wallet entry (transaction, txi or txo) label is changed.
|
||||||
|
* Extends WalletDataChangedEvent so triggers a background save.
|
||||||
|
*/
|
||||||
|
public class WalletEntryLabelChangedEvent extends WalletDataChangedEvent {
|
||||||
private final Entry entry;
|
private final Entry entry;
|
||||||
|
|
||||||
public WalletEntryLabelChangedEvent(Wallet wallet, Entry entry) {
|
public WalletEntryLabelChangedEvent(Wallet wallet, Entry entry) {
|
||||||
|
|
|
@ -9,13 +9,13 @@ import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This is posted by WalletForm once the history of the wallet has been refreshed, and new transactions detected
|
* This is posted by WalletForm once the history of the wallet has been refreshed, and new transactions detected
|
||||||
* Extends WalletChangedEvent so also saves the wallet.
|
*
|
||||||
*/
|
*/
|
||||||
public class WalletHistoryChangedEvent extends WalletBlockHeightChangedEvent {
|
public class WalletHistoryChangedEvent extends WalletChangedEvent {
|
||||||
private final List<WalletNode> historyChangedNodes;
|
private final List<WalletNode> historyChangedNodes;
|
||||||
|
|
||||||
public WalletHistoryChangedEvent(Wallet wallet, Integer blockHeight, List<WalletNode> historyChangedNodes) {
|
public WalletHistoryChangedEvent(Wallet wallet, List<WalletNode> historyChangedNodes) {
|
||||||
super(wallet, blockHeight);
|
super(wallet);
|
||||||
this.historyChangedNodes = historyChangedNodes;
|
this.historyChangedNodes = historyChangedNodes;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,43 @@
|
||||||
|
package com.sparrowwallet.sparrow.event;
|
||||||
|
|
||||||
|
import com.sparrowwallet.drongo.KeyPurpose;
|
||||||
|
import com.sparrowwallet.drongo.wallet.Wallet;
|
||||||
|
import com.sparrowwallet.drongo.wallet.WalletNode;
|
||||||
|
import com.sparrowwallet.sparrow.io.ElectrumServer;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Used to notify that a wallet node (identified by it's script hash) has been updated on the blockchain.
|
||||||
|
* Does not extend WalletChangedEvent as the wallet is not known when this is fired.
|
||||||
|
*/
|
||||||
|
public class WalletNodeHistoryChangedEvent {
|
||||||
|
private final String scriptHash;
|
||||||
|
|
||||||
|
public WalletNodeHistoryChangedEvent(String scriptHash) {
|
||||||
|
this.scriptHash = scriptHash;
|
||||||
|
}
|
||||||
|
|
||||||
|
public WalletNode getWalletNode(Wallet wallet) {
|
||||||
|
List<KeyPurpose> keyPurposes = List.of(KeyPurpose.RECEIVE, KeyPurpose.CHANGE);
|
||||||
|
for(KeyPurpose keyPurpose : keyPurposes) {
|
||||||
|
WalletNode changedNode = getWalletNode(wallet, keyPurpose);
|
||||||
|
if(changedNode != null) {
|
||||||
|
return changedNode;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private WalletNode getWalletNode(Wallet wallet, KeyPurpose keyPurpose) {
|
||||||
|
WalletNode purposeNode = wallet.getNode(keyPurpose);
|
||||||
|
for(WalletNode addressNode : purposeNode.getChildren()) {
|
||||||
|
if(ElectrumServer.getScriptHash(wallet, addressNode).equals(scriptHash)) {
|
||||||
|
return addressNode;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
|
@ -18,6 +18,7 @@ import com.sparrowwallet.sparrow.EventManager;
|
||||||
import com.sparrowwallet.sparrow.event.ConnectionEvent;
|
import com.sparrowwallet.sparrow.event.ConnectionEvent;
|
||||||
import com.sparrowwallet.sparrow.event.FeeRatesUpdatedEvent;
|
import com.sparrowwallet.sparrow.event.FeeRatesUpdatedEvent;
|
||||||
import com.sparrowwallet.sparrow.event.NewBlockEvent;
|
import com.sparrowwallet.sparrow.event.NewBlockEvent;
|
||||||
|
import com.sparrowwallet.sparrow.event.WalletNodeHistoryChangedEvent;
|
||||||
import com.sparrowwallet.sparrow.wallet.SendController;
|
import com.sparrowwallet.sparrow.wallet.SendController;
|
||||||
import javafx.application.Platform;
|
import javafx.application.Platform;
|
||||||
import javafx.concurrent.ScheduledService;
|
import javafx.concurrent.ScheduledService;
|
||||||
|
@ -37,6 +38,7 @@ import java.security.cert.CertificateException;
|
||||||
import java.security.cert.CertificateFactory;
|
import java.security.cert.CertificateFactory;
|
||||||
import java.security.cert.X509Certificate;
|
import java.security.cert.X509Certificate;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.concurrent.locks.ReentrantLock;
|
import java.util.concurrent.locks.ReentrantLock;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
@ -47,6 +49,8 @@ public class ElectrumServer {
|
||||||
|
|
||||||
private static Transport transport;
|
private static Transport transport;
|
||||||
|
|
||||||
|
private static final Map<String, String> subscribedScriptHashes = Collections.synchronizedMap(new HashMap<>());
|
||||||
|
|
||||||
private static synchronized Transport getTransport() throws ServerException {
|
private static synchronized Transport getTransport() throws ServerException {
|
||||||
if(transport == null) {
|
if(transport == null) {
|
||||||
try {
|
try {
|
||||||
|
@ -171,6 +175,7 @@ public class ElectrumServer {
|
||||||
|
|
||||||
public void getHistory(Wallet wallet, Collection<WalletNode> nodes, Map<WalletNode, Set<BlockTransactionHash>> nodeTransactionMap, int startIndex) throws ServerException {
|
public void getHistory(Wallet wallet, Collection<WalletNode> nodes, Map<WalletNode, Set<BlockTransactionHash>> nodeTransactionMap, int startIndex) throws ServerException {
|
||||||
getReferences(wallet, "blockchain.scripthash.get_history", nodes, nodeTransactionMap, startIndex);
|
getReferences(wallet, "blockchain.scripthash.get_history", nodes, nodeTransactionMap, startIndex);
|
||||||
|
subscribeWalletNodes(wallet, nodes, startIndex);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void getMempool(Wallet wallet, Collection<WalletNode> nodes, Map<WalletNode, Set<BlockTransactionHash>> nodeTransactionMap, int startIndex) throws ServerException {
|
public void getMempool(Wallet wallet, Collection<WalletNode> nodes, Map<WalletNode, Set<BlockTransactionHash>> nodeTransactionMap, int startIndex) throws ServerException {
|
||||||
|
@ -231,6 +236,50 @@ public class ElectrumServer {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void subscribeWalletNodes(Wallet wallet, Collection<WalletNode> nodes, int startIndex) throws ServerException {
|
||||||
|
try {
|
||||||
|
JsonRpcClient client = new JsonRpcClient(getTransport());
|
||||||
|
BatchRequestBuilder<String, String> batchRequest = client.createBatchRequest().keysType(String.class).returnType(String.class);
|
||||||
|
|
||||||
|
Set<String> scriptHashes = new HashSet<>();
|
||||||
|
for(WalletNode node : nodes) {
|
||||||
|
if(node.getIndex() >= startIndex) {
|
||||||
|
String scriptHash = getScriptHash(wallet, node);
|
||||||
|
if(!subscribedScriptHashes.containsKey(scriptHash)) {
|
||||||
|
scriptHashes.add(scriptHash);
|
||||||
|
batchRequest.add(node.getDerivationPath(), "blockchain.scripthash.subscribe", scriptHash);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(scriptHashes.isEmpty()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Map<String, String> result;
|
||||||
|
try {
|
||||||
|
result = batchRequest.execute();
|
||||||
|
} catch(JsonRpcBatchException e) {
|
||||||
|
//Even if we have some successes, failure to subscribe for all script hashes will result in outdated wallet view. Don't proceed.
|
||||||
|
throw new IllegalStateException("Failed to subscribe for updates for paths: " + e.getErrors().keySet());
|
||||||
|
}
|
||||||
|
|
||||||
|
for(String path : result.keySet()) {
|
||||||
|
String status = result.get(path);
|
||||||
|
|
||||||
|
Optional<WalletNode> optionalNode = nodes.stream().filter(n -> n.getDerivationPath().equals(path)).findFirst();
|
||||||
|
if(optionalNode.isPresent()) {
|
||||||
|
WalletNode node = optionalNode.get();
|
||||||
|
subscribedScriptHashes.put(getScriptHash(wallet, node), status);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (IllegalStateException e) {
|
||||||
|
throw new ServerException(e.getCause());
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new ServerException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public List<Set<BlockTransactionHash>> getOutputTransactionReferences(Transaction transaction, int indexStart, int indexEnd) throws ServerException {
|
public List<Set<BlockTransactionHash>> getOutputTransactionReferences(Transaction transaction, int indexStart, int indexEnd) throws ServerException {
|
||||||
try {
|
try {
|
||||||
|
@ -545,7 +594,7 @@ public class ElectrumServer {
|
||||||
return targetBlocksFeeRatesSats;
|
return targetBlocksFeeRatesSats;
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getScriptHash(Wallet wallet, WalletNode node) {
|
public static String getScriptHash(Wallet wallet, WalletNode node) {
|
||||||
byte[] hash = Sha256Hash.hash(wallet.getOutputScript(node).getProgram());
|
byte[] hash = Sha256Hash.hash(wallet.getOutputScript(node).getProgram());
|
||||||
byte[] reversed = Utils.reverseBytes(hash);
|
byte[] reversed = Utils.reverseBytes(hash);
|
||||||
return Utils.bytesToHex(reversed);
|
return Utils.bytesToHex(reversed);
|
||||||
|
@ -630,6 +679,16 @@ public class ElectrumServer {
|
||||||
public void newBlockHeaderTip(@JsonRpcParam("header") final BlockHeaderTip header) {
|
public void newBlockHeaderTip(@JsonRpcParam("header") final BlockHeaderTip header) {
|
||||||
Platform.runLater(() -> EventManager.get().post(new NewBlockEvent(header.height, header.getBlockHeader())));
|
Platform.runLater(() -> EventManager.get().post(new NewBlockEvent(header.height, header.getBlockHeader())));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@JsonRpcMethod("blockchain.scripthash.subscribe")
|
||||||
|
public void scriptHashStatusUpdated(@JsonRpcParam("scripthash") final String scriptHash, @JsonRpcParam("status") final String status) {
|
||||||
|
String oldStatus = subscribedScriptHashes.put(scriptHash, status);
|
||||||
|
if(Objects.equals(oldStatus, status)) {
|
||||||
|
System.out.println("Received script hash status update, but status has not changed");
|
||||||
|
}
|
||||||
|
|
||||||
|
Platform.runLater(() -> EventManager.get().post(new WalletNodeHistoryChangedEvent(scriptHash)));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class TcpTransport implements Transport, Closeable {
|
public static class TcpTransport implements Transport, Closeable {
|
||||||
|
@ -886,6 +945,7 @@ public class ElectrumServer {
|
||||||
BlockHeaderTip tip;
|
BlockHeaderTip tip;
|
||||||
if(subscribe) {
|
if(subscribe) {
|
||||||
tip = electrumServer.subscribeBlockHeaders();
|
tip = electrumServer.subscribeBlockHeaders();
|
||||||
|
subscribedScriptHashes.clear();
|
||||||
} else {
|
} else {
|
||||||
tip = new BlockHeaderTip();
|
tip = new BlockHeaderTip();
|
||||||
}
|
}
|
||||||
|
|
|
@ -60,8 +60,7 @@ public class WalletForm {
|
||||||
if(wallet.isValid() && AppController.isOnline()) {
|
if(wallet.isValid() && AppController.isOnline()) {
|
||||||
ElectrumServer.TransactionHistoryService historyService = new ElectrumServer.TransactionHistoryService(wallet);
|
ElectrumServer.TransactionHistoryService historyService = new ElectrumServer.TransactionHistoryService(wallet);
|
||||||
historyService.setOnSucceeded(workerStateEvent -> {
|
historyService.setOnSucceeded(workerStateEvent -> {
|
||||||
wallet.setStoredBlockHeight(blockHeight);
|
updateWallet(previousWallet, blockHeight);
|
||||||
notifyIfChanged(previousWallet, blockHeight);
|
|
||||||
});
|
});
|
||||||
historyService.setOnFailed(workerStateEvent -> {
|
historyService.setOnFailed(workerStateEvent -> {
|
||||||
workerStateEvent.getSource().getException().printStackTrace();
|
workerStateEvent.getSource().getException().printStackTrace();
|
||||||
|
@ -70,15 +69,32 @@ public class WalletForm {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void updateWallet(Wallet previousWallet, Integer blockHeight) {
|
||||||
|
if(blockHeight != null) {
|
||||||
|
wallet.setStoredBlockHeight(blockHeight);
|
||||||
|
}
|
||||||
|
|
||||||
|
notifyIfChanged(previousWallet, blockHeight);
|
||||||
|
}
|
||||||
|
|
||||||
private void notifyIfChanged(Wallet previousWallet, Integer blockHeight) {
|
private void notifyIfChanged(Wallet previousWallet, Integer blockHeight) {
|
||||||
List<WalletNode> historyChangedNodes = new ArrayList<>();
|
List<WalletNode> historyChangedNodes = new ArrayList<>();
|
||||||
historyChangedNodes.addAll(getHistoryChangedNodes(previousWallet.getNode(KeyPurpose.RECEIVE).getChildren(), wallet.getNode(KeyPurpose.RECEIVE).getChildren()));
|
historyChangedNodes.addAll(getHistoryChangedNodes(previousWallet.getNode(KeyPurpose.RECEIVE).getChildren(), wallet.getNode(KeyPurpose.RECEIVE).getChildren()));
|
||||||
historyChangedNodes.addAll(getHistoryChangedNodes(previousWallet.getNode(KeyPurpose.CHANGE).getChildren(), wallet.getNode(KeyPurpose.CHANGE).getChildren()));
|
historyChangedNodes.addAll(getHistoryChangedNodes(previousWallet.getNode(KeyPurpose.CHANGE).getChildren(), wallet.getNode(KeyPurpose.CHANGE).getChildren()));
|
||||||
|
|
||||||
|
boolean changed = false;
|
||||||
if(!historyChangedNodes.isEmpty()) {
|
if(!historyChangedNodes.isEmpty()) {
|
||||||
Platform.runLater(() -> EventManager.get().post(new WalletHistoryChangedEvent(wallet, blockHeight, historyChangedNodes)));
|
Platform.runLater(() -> EventManager.get().post(new WalletHistoryChangedEvent(wallet, historyChangedNodes)));
|
||||||
} else if(blockHeight != null && !blockHeight.equals(previousWallet.getStoredBlockHeight())) {
|
changed = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(blockHeight != null && !blockHeight.equals(previousWallet.getStoredBlockHeight())) {
|
||||||
Platform.runLater(() -> EventManager.get().post(new WalletBlockHeightChangedEvent(wallet, blockHeight)));
|
Platform.runLater(() -> EventManager.get().post(new WalletBlockHeightChangedEvent(wallet, blockHeight)));
|
||||||
|
changed = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(changed) {
|
||||||
|
Platform.runLater(() -> EventManager.get().post(new WalletDataChangedEvent(wallet)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -146,20 +162,13 @@ public class WalletForm {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Subscribe
|
@Subscribe
|
||||||
public void walletLabelChanged(WalletEntryLabelChangedEvent event) {
|
public void walletDataChanged(WalletDataChangedEvent event) {
|
||||||
if(event.getWallet().equals(wallet)) {
|
if(event.getWallet().equals(wallet)) {
|
||||||
backgroundSaveWallet(event);
|
backgroundSaveWallet();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Subscribe
|
private void backgroundSaveWallet() {
|
||||||
public void walletBlockHeightChanged(WalletBlockHeightChangedEvent event) {
|
|
||||||
if(event.getWallet().equals(wallet)) {
|
|
||||||
backgroundSaveWallet(event);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void backgroundSaveWallet(WalletChangedEvent event) {
|
|
||||||
try {
|
try {
|
||||||
save();
|
save();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
@ -182,11 +191,18 @@ public class WalletForm {
|
||||||
|
|
||||||
@Subscribe
|
@Subscribe
|
||||||
public void newBlock(NewBlockEvent event) {
|
public void newBlock(NewBlockEvent event) {
|
||||||
refreshHistory(event.getHeight());
|
updateWallet(wallet, event.getHeight());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Subscribe
|
@Subscribe
|
||||||
public void connected(ConnectionEvent event) {
|
public void connected(ConnectionEvent event) {
|
||||||
refreshHistory(event.getBlockHeight());
|
refreshHistory(event.getBlockHeight());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Subscribe
|
||||||
|
public void walletNodeHistoryChanged(WalletNodeHistoryChangedEvent event) {
|
||||||
|
if(event.getWalletNode(wallet) != null) {
|
||||||
|
refreshHistory(AppController.getCurrentBlockHeight());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue