Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.strategyobject.substrateclient.rpc.core.annotations.RpcSubscription;
import com.strategyobject.substrateclient.rpc.types.BlockHash;
import com.strategyobject.substrateclient.rpc.types.Header;
import com.strategyobject.substrateclient.rpc.types.SignedBlock;
import com.strategyobject.substrateclient.scale.annotations.Scale;

import java.util.concurrent.CompletableFuture;
Expand All @@ -23,4 +24,7 @@ public interface Chain {
@RpcCall(method = "getBlockHash")
@Scale
CompletableFuture<BlockHash> getBlockHash(long number);

@RpcCall(method = "getBlock")
CompletableFuture<SignedBlock> getBlock(@Scale BlockHash hash);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@

import static org.awaitility.Awaitility.await;
import static org.hamcrest.Matchers.greaterThan;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.*;

@Testcontainers
public class ChainTests {
Expand All @@ -42,11 +41,11 @@ void getFinalizedHead() throws ExecutionException, InterruptedException, Timeout
wsProvider.connect().get(WAIT_TIMEOUT, TimeUnit.SECONDS);

val sectionFactory = new RpcGeneratedSectionFactory();
Chain rpcSection = sectionFactory.create(Chain.class, wsProvider);
val rpcSection = sectionFactory.create(Chain.class, wsProvider);

BlockHash result = rpcSection.getFinalizedHead().get(WAIT_TIMEOUT, TimeUnit.SECONDS);
val result = rpcSection.getFinalizedHead().get(WAIT_TIMEOUT, TimeUnit.SECONDS);

assertNotEquals(new BigInteger(result.getData()), BigInteger.ZERO);
assertNotEquals(BigInteger.ZERO, new BigInteger(result.getData()));
}
}

Expand All @@ -59,7 +58,7 @@ void subscribeNewHeads() throws ExecutionException, InterruptedException, Timeou
wsProvider.connect().get(WAIT_TIMEOUT, TimeUnit.SECONDS);

val sectionFactory = new RpcGeneratedSectionFactory();
Chain rpcSection = sectionFactory.create(Chain.class, wsProvider);
val rpcSection = sectionFactory.create(Chain.class, wsProvider);

val blockCount = new AtomicInteger(0);
val blockHash = new AtomicReference<BlockHash>(null);
Expand All @@ -76,7 +75,7 @@ void subscribeNewHeads() throws ExecutionException, InterruptedException, Timeou
.atMost(WAIT_TIMEOUT * 2, TimeUnit.SECONDS)
.untilAtomic(blockCount, greaterThan(2));

assertNotEquals(new BigInteger(blockHash.get().getData()), BigInteger.ZERO);
assertNotEquals(BigInteger.ZERO, new BigInteger(blockHash.get().getData()));

val result = unsubscribeFunc.get().get(WAIT_TIMEOUT, TimeUnit.SECONDS);

Expand All @@ -86,6 +85,23 @@ void subscribeNewHeads() throws ExecutionException, InterruptedException, Timeou

@Test
void getBlockHash() throws ExecutionException, InterruptedException, TimeoutException, RpcInterfaceInitializationException {
try (WsProvider wsProvider = WsProvider.builder()
.setEndpoint(substrate.getWsAddress())
.disableAutoConnect()
.build()) {
wsProvider.connect().get(WAIT_TIMEOUT, TimeUnit.SECONDS);

val sectionFactory = new RpcGeneratedSectionFactory();
val rpcSection = sectionFactory.create(Chain.class, wsProvider);

val result = rpcSection.getBlockHash(0).get(WAIT_TIMEOUT, TimeUnit.SECONDS);

assertNotEquals(BigInteger.ZERO, new BigInteger(result.getData()));
}
}

@Test
void getBlock() throws ExecutionException, InterruptedException, TimeoutException, RpcInterfaceInitializationException {
try (WsProvider wsProvider = WsProvider.builder()
.setEndpoint(substrate.getWsAddress())
.disableAutoConnect()
Expand All @@ -95,9 +111,26 @@ void getBlockHash() throws ExecutionException, InterruptedException, TimeoutExce
val sectionFactory = new RpcGeneratedSectionFactory();
Chain rpcSection = sectionFactory.create(Chain.class, wsProvider);

BlockHash result = rpcSection.getBlockHash(0).get(WAIT_TIMEOUT, TimeUnit.SECONDS);
val height = new AtomicInteger(0);
rpcSection.subscribeNewHeads((e, header) -> {
if (header != null) {
height.set(header.getNumber().getValue().intValue());
}
});

await()
.atMost(WAIT_TIMEOUT * 3, TimeUnit.SECONDS)
.untilAtomic(height, greaterThan(1));

val number = height.get();
val blockHash = rpcSection.getBlockHash(number).get(WAIT_TIMEOUT, TimeUnit.SECONDS);

assertNotEquals(BigInteger.ZERO, new BigInteger(blockHash.getData()));

val block = rpcSection.getBlock(blockHash).get(WAIT_TIMEOUT, TimeUnit.SECONDS);

assertNotEquals(new BigInteger(result.getData()), BigInteger.ZERO);
assertNotEquals(BigInteger.ZERO, new BigInteger(block.getBlock().getHeader().getParentHash().getData()));
assertEquals(number, block.getBlock().getHeader().getNumber().getValue().intValue());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.strategyobject.substrateclient.rpc.types;

import com.strategyobject.substrateclient.rpc.core.annotations.RpcDecoder;
import lombok.Getter;
import lombok.Setter;

import java.util.List;

@RpcDecoder
@Getter
@Setter
public class Block {
private Header header;

private List<String> extrinsics; // TODO think about a more strict type
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
@Getter
@Setter
@RpcDecoder
public class Header {
public class Header { // TODO add rest fields
@Scale
private BlockHash parentHash;

private Number number; /* TODO probably it would be better to change the type to BigInteger
and support specific decoders */
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.strategyobject.substrateclient.rpc.types;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

import java.math.BigInteger;

@RequiredArgsConstructor(staticName = "of")
@Getter
public class Number {
private final BigInteger value;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.strategyobject.substrateclient.rpc.types;

import com.strategyobject.substrateclient.rpc.core.DecoderPair;
import com.strategyobject.substrateclient.rpc.core.annotations.AutoRegister;
import com.strategyobject.substrateclient.rpc.core.decoders.AbstractDecoder;
import lombok.val;

import java.math.BigInteger;

@AutoRegister(types = Number.class)
public class NumberDecoder extends AbstractDecoder<Number> {
@Override
protected Number decodeNonNull(Object value, DecoderPair<?>[] decoders) {
val stringValue = (String) value;
val number = new BigInteger(stringValue.substring(2), 16);

return Number.of(number);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.strategyobject.substrateclient.rpc.types;

import com.strategyobject.substrateclient.rpc.core.annotations.RpcDecoder;
import lombok.Getter;
import lombok.Setter;

@RpcDecoder
@Getter
@Setter
public class SignedBlock { // TODO add rest fields
private Block block;
}