From 9aa8b10898bb92d1586e5f3955b4e67c76a9aa77 Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Tue, 9 Jun 2020 11:21:34 +0200 Subject: [PATCH] handle new blocks --- build.gradle | 3 +++ .../sparrowwallet/sparrow/AppController.java | 6 +++++ .../sparrow/event/NewBlockEvent.java | 21 +++++++++++++++++ .../sparrow/io/ElectrumServer.java | 23 +++++++++++++++++-- .../sparrow/wallet/WalletForm.java | 6 +++++ src/main/java/module-info.java | 1 + 6 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/sparrowwallet/sparrow/event/NewBlockEvent.java diff --git a/build.gradle b/build.gradle index c815c276..eb7912fb 100644 --- a/build.gradle +++ b/build.gradle @@ -34,6 +34,9 @@ dependencies { implementation('no.tornado:tornadofx-controls:1.0.4') implementation('com.google.zxing:javase:3.4.0') implementation('com.github.arteam:simple-json-rpc-client:1.0') + implementation('com.github.arteam:simple-json-rpc-server:1.0') { + exclude group: 'org.slf4j' + } implementation('de.codecentric.centerdevice:centerdevice-nsmenufx:2.1.7') implementation('org.controlsfx:controlsfx:11.0.1' ) { exclude group: 'org.openjfx', module: 'javafx-base' diff --git a/src/main/java/com/sparrowwallet/sparrow/AppController.java b/src/main/java/com/sparrowwallet/sparrow/AppController.java index 00f0cf16..88c21a5c 100644 --- a/src/main/java/com/sparrowwallet/sparrow/AppController.java +++ b/src/main/java/com/sparrowwallet/sparrow/AppController.java @@ -677,4 +677,10 @@ public class AppController implements Initializable { String status = "Connection error: " + reason; EventManager.get().post(new StatusEvent(status)); } + + @Subscribe + public void newBlock(NewBlockEvent event) { + String status = "Updating to new block height " + event.getHeight(); + EventManager.get().post(new StatusEvent(status)); + } } diff --git a/src/main/java/com/sparrowwallet/sparrow/event/NewBlockEvent.java b/src/main/java/com/sparrowwallet/sparrow/event/NewBlockEvent.java new file mode 100644 index 00000000..366a1e85 --- /dev/null +++ b/src/main/java/com/sparrowwallet/sparrow/event/NewBlockEvent.java @@ -0,0 +1,21 @@ +package com.sparrowwallet.sparrow.event; + +import com.sparrowwallet.drongo.protocol.BlockHeader; + +public class NewBlockEvent { + private final int height; + private final BlockHeader blockHeader; + + public NewBlockEvent(int height, BlockHeader blockHeader) { + this.height = height; + this.blockHeader = blockHeader; + } + + public int getHeight() { + return height; + } + + public BlockHeader getBlockHeader() { + return blockHeader; + } +} diff --git a/src/main/java/com/sparrowwallet/sparrow/io/ElectrumServer.java b/src/main/java/com/sparrowwallet/sparrow/io/ElectrumServer.java index 5e836147..ef843ea6 100644 --- a/src/main/java/com/sparrowwallet/sparrow/io/ElectrumServer.java +++ b/src/main/java/com/sparrowwallet/sparrow/io/ElectrumServer.java @@ -2,12 +2,19 @@ package com.sparrowwallet.sparrow.io; import com.github.arteam.simplejsonrpc.client.*; import com.github.arteam.simplejsonrpc.client.builder.BatchRequestBuilder; +import com.github.arteam.simplejsonrpc.core.annotation.JsonRpcMethod; +import com.github.arteam.simplejsonrpc.core.annotation.JsonRpcParam; +import com.github.arteam.simplejsonrpc.core.annotation.JsonRpcService; +import com.github.arteam.simplejsonrpc.server.JsonRpcServer; import com.google.common.net.HostAndPort; import com.sparrowwallet.drongo.KeyPurpose; import com.sparrowwallet.drongo.Utils; import com.sparrowwallet.drongo.protocol.*; import com.sparrowwallet.drongo.wallet.*; +import com.sparrowwallet.sparrow.EventManager; import com.sparrowwallet.sparrow.event.ConnectionEvent; +import com.sparrowwallet.sparrow.event.NewBlockEvent; +import javafx.application.Platform; import javafx.concurrent.ScheduledService; import javafx.concurrent.Service; import javafx.concurrent.Task; @@ -390,6 +397,14 @@ public class ElectrumServer { } } + @JsonRpcService + public static class SubscriptionService { + @JsonRpcMethod("blockchain.headers.subscribe") + public void newBlockHeaderTip(@JsonRpcParam("header") final BlockHeaderTip header) { + Platform.runLater(() -> EventManager.get().post(new NewBlockEvent(header.height, header.getBlockHeader()))); + } + } + public static class TcpTransport implements Transport, Closeable { public static final int DEFAULT_PORT = 50001; @@ -404,6 +419,9 @@ public class ElectrumServer { private boolean running = false; private boolean reading = true; + private final JsonRpcServer jsonRpcServer = new JsonRpcServer(); + private final SubscriptionService subscriptionService = new SubscriptionService(); + public TcpTransport(HostAndPort server) { this.server = server; this.socketFactory = SocketFactory.getDefault(); @@ -447,9 +465,10 @@ public class ElectrumServer { try { String received = readInputStream(); if(received.contains("method")) { - //Handle notification - System.out.println("Notification: " + received); + //Handle subscription notification + jsonRpcServer.handle(received, subscriptionService); } else { + //Handle client's response response = received; reading = false; notifyAll(); diff --git a/src/main/java/com/sparrowwallet/sparrow/wallet/WalletForm.java b/src/main/java/com/sparrowwallet/sparrow/wallet/WalletForm.java index 8b7c7eb9..6d951547 100644 --- a/src/main/java/com/sparrowwallet/sparrow/wallet/WalletForm.java +++ b/src/main/java/com/sparrowwallet/sparrow/wallet/WalletForm.java @@ -5,6 +5,7 @@ import com.sparrowwallet.drongo.KeyPurpose; import com.sparrowwallet.drongo.wallet.Wallet; import com.sparrowwallet.drongo.wallet.WalletNode; import com.sparrowwallet.sparrow.EventManager; +import com.sparrowwallet.sparrow.event.NewBlockEvent; import com.sparrowwallet.sparrow.event.WalletChangedEvent; import com.sparrowwallet.sparrow.io.ElectrumServer; import com.sparrowwallet.sparrow.io.Storage; @@ -119,4 +120,9 @@ public class WalletForm { } } } + + @Subscribe + public void newBlock(NewBlockEvent event) { + refreshHistory(); + } } diff --git a/src/main/java/module-info.java b/src/main/java/module-info.java index dd009883..a1b72022 100644 --- a/src/main/java/module-info.java +++ b/src/main/java/module-info.java @@ -13,6 +13,7 @@ open module com.sparrowwallet.sparrow { requires com.google.zxing; requires com.google.zxing.javase; requires simple.json.rpc.client; + requires simple.json.rpc.server; requires simple.json.rpc.core; requires org.jetbrains.annotations; requires com.fasterxml.jackson.databind;