Search in sources :

Example 1 with BlockInfo

use of io.nem.sdk.model.blockchain.BlockInfo in project nem2-sdk-java by nemtech.

the class Listener method open.

/**
 * @return a {@link CompletableFuture} that resolves when the websocket connection is opened
 */
public CompletableFuture<Void> open() {
    HttpClient httpClient = Vertx.vertx().createHttpClient();
    CompletableFuture<Void> future = new CompletableFuture<>();
    if (this.webSocket != null) {
        return CompletableFuture.completedFuture(null);
    }
    RequestOptions requestOptions = new RequestOptions();
    requestOptions.setHost(this.url.getHost());
    requestOptions.setPort(this.url.getPort());
    requestOptions.setURI("/ws");
    httpClient.websocket(requestOptions, webSocket -> {
        this.webSocket = webSocket;
        webSocket.handler(handler -> {
            JsonObject message = handler.toJsonObject();
            if (message.containsKey("uid")) {
                this.UID = message.getString("uid");
                future.complete(null);
            } else if (message.containsKey("transaction")) {
                this.messageSubject.onNext(new ListenerMessage(ListenerChannel.rawValueOf(message.getJsonObject("meta").getString("channelName")), new TransactionMapping().apply(message)));
            } else if (message.containsKey("block")) {
                final JsonObject meta = message.getJsonObject("meta");
                final JsonObject block = message.getJsonObject("block");
                int rawNetworkType = (int) Long.parseLong(Integer.toHexString(block.getInteger("version")).substring(0, 2), 16);
                final NetworkType networkType;
                if (rawNetworkType == NetworkType.MIJIN_TEST.getValue())
                    networkType = NetworkType.MIJIN_TEST;
                else if (rawNetworkType == NetworkType.MIJIN.getValue())
                    networkType = NetworkType.MIJIN;
                else if (rawNetworkType == NetworkType.MAIN_NET.getValue())
                    networkType = NetworkType.MAIN_NET;
                else
                    networkType = NetworkType.TEST_NET;
                final int version = (int) Long.parseLong(Integer.toHexString(block.getInteger("version")).substring(2, 4), 16);
                this.messageSubject.onNext(new ListenerMessage(ListenerChannel.BLOCK, new BlockInfo(meta.getString("hash"), meta.getString("generationHash"), Optional.empty(), Optional.empty(), block.getString("signature"), new PublicAccount(block.getString("signer"), networkType), networkType, version, block.getInteger("type"), extractBigInteger(block.getJsonArray("height")), extractBigInteger(block.getJsonArray("timestamp")), extractBigInteger(block.getJsonArray("difficulty")), block.getString("previousBlockHash"), block.getString("blockTransactionsHash"))));
            } else if (message.containsKey("status")) {
                this.messageSubject.onNext(new ListenerMessage(ListenerChannel.STATUS, new TransactionStatusError(message.getString("hash"), message.getString("status"), new Deadline(extractBigInteger(message.getJsonArray("deadline"))))));
            } else if (message.containsKey("meta")) {
                this.messageSubject.onNext(new ListenerMessage(ListenerChannel.rawValueOf(message.getJsonObject("meta").getString("channelName")), message.getJsonObject("meta").getString("hash")));
            } else if (message.containsKey("parentHash")) {
                this.messageSubject.onNext(new ListenerMessage(ListenerChannel.COSIGNATURE, new CosignatureSignedTransaction(message.getString("parenthash"), message.getString("signature"), message.getString("signer"))));
            }
        });
    });
    return future;
}
Also used : RequestOptions(io.vertx.core.http.RequestOptions) JsonObject(io.vertx.core.json.JsonObject) CompletableFuture(java.util.concurrent.CompletableFuture) NetworkType(io.nem.sdk.model.blockchain.NetworkType) BlockInfo(io.nem.sdk.model.blockchain.BlockInfo) HttpClient(io.vertx.core.http.HttpClient) PublicAccount(io.nem.sdk.model.account.PublicAccount)

Example 2 with BlockInfo

use of io.nem.sdk.model.blockchain.BlockInfo in project nem2-sdk-java by nemtech.

the class BlockchainHttpTest method getBlockByHeight.

@Test
void getBlockByHeight() throws ExecutionException, InterruptedException {
    BlockInfo blockInfo = blockchainHttp.getBlockByHeight(BigInteger.valueOf(1)).toFuture().get();
    assertEquals(1, blockInfo.getHeight().intValue());
    assertEquals(0, blockInfo.getTimestamp().intValue());
}
Also used : BlockInfo(io.nem.sdk.model.blockchain.BlockInfo) Test(org.junit.jupiter.api.Test)

Example 3 with BlockInfo

use of io.nem.sdk.model.blockchain.BlockInfo in project nem2-sdk-java by nemtech.

the class ListenerTest method shouldReturnNewBlockViaListener.

@Test
void shouldReturnNewBlockViaListener() throws ExecutionException, InterruptedException, IOException {
    Listener listener = new Listener(this.getNodeUrl());
    listener.open().get();
    this.announceStandaloneTransferTransaction();
    BlockInfo blockInfo = listener.newBlock().take(1).toFuture().get();
    assertTrue(blockInfo.getHeight().intValue() > 0);
}
Also used : BlockInfo(io.nem.sdk.model.blockchain.BlockInfo) Test(org.junit.jupiter.api.Test)

Aggregations

BlockInfo (io.nem.sdk.model.blockchain.BlockInfo)3 Test (org.junit.jupiter.api.Test)2 PublicAccount (io.nem.sdk.model.account.PublicAccount)1 NetworkType (io.nem.sdk.model.blockchain.NetworkType)1 HttpClient (io.vertx.core.http.HttpClient)1 RequestOptions (io.vertx.core.http.RequestOptions)1 JsonObject (io.vertx.core.json.JsonObject)1 CompletableFuture (java.util.concurrent.CompletableFuture)1