handle new blocks

This commit is contained in:
Craig Raw 2020-06-09 11:21:34 +02:00
parent 50ef1c1a07
commit 9aa8b10898
6 changed files with 58 additions and 2 deletions

View file

@ -34,6 +34,9 @@ dependencies {
implementation('no.tornado:tornadofx-controls:1.0.4') implementation('no.tornado:tornadofx-controls:1.0.4')
implementation('com.google.zxing:javase:3.4.0') 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-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('de.codecentric.centerdevice:centerdevice-nsmenufx:2.1.7')
implementation('org.controlsfx:controlsfx:11.0.1' ) { implementation('org.controlsfx:controlsfx:11.0.1' ) {
exclude group: 'org.openjfx', module: 'javafx-base' exclude group: 'org.openjfx', module: 'javafx-base'

View file

@ -677,4 +677,10 @@ public class AppController implements Initializable {
String status = "Connection error: " + reason; String status = "Connection error: " + reason;
EventManager.get().post(new StatusEvent(status)); 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));
}
} }

View file

@ -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;
}
}

View file

@ -2,12 +2,19 @@ package com.sparrowwallet.sparrow.io;
import com.github.arteam.simplejsonrpc.client.*; import com.github.arteam.simplejsonrpc.client.*;
import com.github.arteam.simplejsonrpc.client.builder.BatchRequestBuilder; 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.google.common.net.HostAndPort;
import com.sparrowwallet.drongo.KeyPurpose; import com.sparrowwallet.drongo.KeyPurpose;
import com.sparrowwallet.drongo.Utils; import com.sparrowwallet.drongo.Utils;
import com.sparrowwallet.drongo.protocol.*; import com.sparrowwallet.drongo.protocol.*;
import com.sparrowwallet.drongo.wallet.*; import com.sparrowwallet.drongo.wallet.*;
import com.sparrowwallet.sparrow.EventManager;
import com.sparrowwallet.sparrow.event.ConnectionEvent; import com.sparrowwallet.sparrow.event.ConnectionEvent;
import com.sparrowwallet.sparrow.event.NewBlockEvent;
import javafx.application.Platform;
import javafx.concurrent.ScheduledService; import javafx.concurrent.ScheduledService;
import javafx.concurrent.Service; import javafx.concurrent.Service;
import javafx.concurrent.Task; 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 class TcpTransport implements Transport, Closeable {
public static final int DEFAULT_PORT = 50001; public static final int DEFAULT_PORT = 50001;
@ -404,6 +419,9 @@ public class ElectrumServer {
private boolean running = false; private boolean running = false;
private boolean reading = true; private boolean reading = true;
private final JsonRpcServer jsonRpcServer = new JsonRpcServer();
private final SubscriptionService subscriptionService = new SubscriptionService();
public TcpTransport(HostAndPort server) { public TcpTransport(HostAndPort server) {
this.server = server; this.server = server;
this.socketFactory = SocketFactory.getDefault(); this.socketFactory = SocketFactory.getDefault();
@ -447,9 +465,10 @@ public class ElectrumServer {
try { try {
String received = readInputStream(); String received = readInputStream();
if(received.contains("method")) { if(received.contains("method")) {
//Handle notification //Handle subscription notification
System.out.println("Notification: " + received); jsonRpcServer.handle(received, subscriptionService);
} else { } else {
//Handle client's response
response = received; response = received;
reading = false; reading = false;
notifyAll(); notifyAll();

View file

@ -5,6 +5,7 @@ import com.sparrowwallet.drongo.KeyPurpose;
import com.sparrowwallet.drongo.wallet.Wallet; import com.sparrowwallet.drongo.wallet.Wallet;
import com.sparrowwallet.drongo.wallet.WalletNode; import com.sparrowwallet.drongo.wallet.WalletNode;
import com.sparrowwallet.sparrow.EventManager; import com.sparrowwallet.sparrow.EventManager;
import com.sparrowwallet.sparrow.event.NewBlockEvent;
import com.sparrowwallet.sparrow.event.WalletChangedEvent; import com.sparrowwallet.sparrow.event.WalletChangedEvent;
import com.sparrowwallet.sparrow.io.ElectrumServer; import com.sparrowwallet.sparrow.io.ElectrumServer;
import com.sparrowwallet.sparrow.io.Storage; import com.sparrowwallet.sparrow.io.Storage;
@ -119,4 +120,9 @@ public class WalletForm {
} }
} }
} }
@Subscribe
public void newBlock(NewBlockEvent event) {
refreshHistory();
}
} }

View file

@ -13,6 +13,7 @@ open module com.sparrowwallet.sparrow {
requires com.google.zxing; requires com.google.zxing;
requires com.google.zxing.javase; requires com.google.zxing.javase;
requires simple.json.rpc.client; requires simple.json.rpc.client;
requires simple.json.rpc.server;
requires simple.json.rpc.core; requires simple.json.rpc.core;
requires org.jetbrains.annotations; requires org.jetbrains.annotations;
requires com.fasterxml.jackson.databind; requires com.fasterxml.jackson.databind;