Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revise rpc-api types #20

Merged
merged 1 commit into from
Jun 14, 2022
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
@@ -1,5 +1,6 @@
package com.strategyobject.substrateclient.api;

import com.strategyobject.substrateclient.rpc.api.BlockNumber;
import com.strategyobject.substrateclient.tests.containers.SubstrateVersion;
import com.strategyobject.substrateclient.tests.containers.TestSubstrateContainer;
import com.strategyobject.substrateclient.transport.ws.WsProvider;
Expand Down Expand Up @@ -32,7 +33,7 @@ void getSystemPalletAndCall() throws Exception { // TODO move the test out of th
val systemPallet = api.pallet(SystemPallet.class);
val blockHash = systemPallet
.blockHash()
.get(0)
.get(BlockNumber.GENESIS)
.get(WAIT_TIMEOUT, TimeUnit.SECONDS);

assertNotNull(blockHash);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.strategyobject.substrateclient.pallet.annotation.StorageKey;
import com.strategyobject.substrateclient.pallet.storage.StorageNMap;
import com.strategyobject.substrateclient.rpc.api.BlockHash;
import com.strategyobject.substrateclient.rpc.api.BlockNumber;
import com.strategyobject.substrateclient.scale.annotation.Scale;

@Pallet("System")
Expand All @@ -14,7 +15,7 @@ public interface SystemPallet {
value = "BlockHash",
keys = {
@StorageKey(
type = @Scale(Integer.class),
type = @Scale(BlockNumber.class),
hasher = StorageHasher.TWOX_64_CONCAT
)
})
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.strategyobject.substrateclient.common.types;

@FunctionalInterface
public interface Bytes {
byte[] getData();
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import lombok.Getter;

@EqualsAndHashCode
public abstract class FixedBytes<S extends Size> implements Fixed<S> {
public abstract class FixedBytes<S extends Size> implements Fixed<S>, Bytes {
@Getter
private final byte[] data;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import com.strategyobject.substrateclient.common.types.tuple.Pair;
import com.strategyobject.substrateclient.rpc.api.BlockHash;
import com.strategyobject.substrateclient.rpc.api.Hash;
import com.strategyobject.substrateclient.rpc.api.section.State;
import com.strategyobject.substrateclient.rpc.api.StorageKey;
import com.strategyobject.substrateclient.rpc.api.section.State;
import com.strategyobject.substrateclient.scale.ScaleReader;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.strategyobject.substrateclient.pallet.storage;

import com.strategyobject.substrateclient.rpc.api.BlockHash;
import com.strategyobject.substrateclient.rpc.api.impl.Hash256;
import com.strategyobject.substrateclient.scale.ScaleWriter;
import com.strategyobject.substrateclient.scale.registries.ScaleWriterRegistry;
import lombok.SneakyThrows;
Expand All @@ -19,7 +20,7 @@ class IdentityTests {
private static Stream<byte[]> getTestCasesForGetHash() {
return Stream.of(
encode(Integer.class, -175),
encode(BlockHash.class, BlockHash.fromBytes(random(32))),
encode(BlockHash.class, Hash256.fromBytes(random(32))),
"TestString".getBytes(StandardCharsets.UTF_8),
random(new Random().nextInt(128) + 1));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.strategyobject.substrateclient.crypto.ss58.SS58Codec;
import com.strategyobject.substrateclient.rpc.api.AccountId;
import com.strategyobject.substrateclient.rpc.api.BlockHash;
import com.strategyobject.substrateclient.rpc.api.impl.Hash256;
import com.strategyobject.substrateclient.scale.ScaleReader;
import com.strategyobject.substrateclient.scale.ScaleWriter;
import com.strategyobject.substrateclient.scale.readers.CompactIntegerReader;
Expand Down Expand Up @@ -44,7 +45,7 @@ private static Stream<Arguments> getTestCasesForGetHash() {
"0x4f9aea1afa791265fae359272badc1cf8eaf04151687736326c9fea17e25fc5287613693c912909cb226aa4794f26a48"
),
Arguments.of(
BlockHash.fromBytes(HexConverter.toBytes("0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef")),
Hash256.fromBytes(HexConverter.toBytes("0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef")),
ScaleReaderRegistry.getInstance().resolve(BlockHash.class),
ScaleWriterRegistry.getInstance().resolve(BlockHash.class),
Identity.getInstance(),
Expand Down Expand Up @@ -115,7 +116,7 @@ private static Stream<Arguments> getTestCasesForExtractKey() {
ScaleReaderRegistry.getInstance().resolve(BlockHash.class),
ScaleWriterRegistry.getInstance().resolve(BlockHash.class),
Identity.getInstance(),
BlockHash.fromBytes(HexConverter.toBytes("0xabcdef98765432100123456789abcdefabcdef98765432100123456789abcdef")),
Hash256.fromBytes(HexConverter.toBytes("0xabcdef98765432100123456789abcdefabcdef98765432100123456789abcdef")),
0
),
Arguments.of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.strategyobject.substrateclient.rpc.RpcGeneratedSectionFactory;
import com.strategyobject.substrateclient.rpc.api.AccountId;
import com.strategyobject.substrateclient.rpc.api.BlockHash;
import com.strategyobject.substrateclient.rpc.api.BlockNumber;
import com.strategyobject.substrateclient.rpc.api.section.Chain;
import com.strategyobject.substrateclient.rpc.api.section.State;
import com.strategyobject.substrateclient.scale.ScaleReader;
Expand Down Expand Up @@ -48,8 +49,8 @@ private static StorageNMapImpl<BlockHash> newSystemBlockHashStorage(State state)
state,
(ScaleReader<BlockHash>) ScaleReaderRegistry.getInstance().resolve(BlockHash.class),
StorageKeyProvider.of("System", "BlockHash")
.use(KeyHasher.with((ScaleWriter<Integer>) ScaleWriterRegistry.getInstance().resolve(Integer.class),
(ScaleReader<Integer>) ScaleReaderRegistry.getInstance().resolve(Integer.class),
.use(KeyHasher.with((ScaleWriter<BlockNumber>) ScaleWriterRegistry.getInstance().resolve(BlockNumber.class),
(ScaleReader<BlockNumber>) ScaleReaderRegistry.getInstance().resolve(BlockNumber.class),
TwoX64Concat.getInstance())));
}

Expand All @@ -74,10 +75,10 @@ void keys() throws Exception {
assertNotNull(collection);
assertEquals(1, collection.size());

val blockNumber = new AtomicReference<Integer>(null);
collection.iterator().forEachRemaining(q -> q.consume((o -> blockNumber.set((Integer) o.get(0)))));
val blockNumber = new AtomicReference<BlockNumber>(null);
collection.iterator().forEachRemaining(q -> q.consume((o -> blockNumber.set((BlockNumber) o.get(0)))));

assertEquals(0, blockNumber.get());
assertEquals(BlockNumber.GENESIS, blockNumber.get());

val blocks = collection.multi().execute().get();
val list = new ArrayList<>();
Expand All @@ -101,7 +102,7 @@ void multiToDifferentStorages() throws Exception {
val storageMap = newSystemBlockHashStorage(state);

val getKey = storageValue.query();
val getHash = storageMap.query(0);
val getHash = storageMap.query(BlockNumber.GENESIS);

val multi = getKey.join(getHash);
val collection = multi.execute().get();
Expand All @@ -110,7 +111,7 @@ void multiToDifferentStorages() throws Exception {
SS58Codec.decode(
"5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY")
.getAddress());
val expectedBlock = storageMap.get(0).get();
val expectedBlock = storageMap.get(BlockNumber.GENESIS).get();

val list = new ArrayList<>(2);
collection.iterator().forEachRemaining(e -> e.consume((value, keys) -> list.add(value)));
Expand All @@ -130,14 +131,14 @@ void entries() throws Exception {

assertNotNull(collection);

val blockNumber = new AtomicReference<Integer>(null);
val blockNumber = new AtomicReference<BlockNumber>(null);
val blockHash = new AtomicReference<BlockHash>(null);
collection.iterator().forEachRemaining(e -> e.consume((value, keys) -> {
blockHash.set(value);
blockNumber.set((Integer) keys.get(0));
blockNumber.set((BlockNumber) keys.get(0));
}));

assertEquals(0, blockNumber.get());
assertEquals(BlockNumber.GENESIS, blockNumber.get());
assertNotEquals(BigInteger.ZERO, new BigInteger(blockHash.get().getData()));
}
}
Expand All @@ -148,18 +149,24 @@ void multi() throws Exception {
val state = RpcGeneratedSectionFactory.create(State.class, wsProvider);
val storage = newSystemBlockHashStorage(state);

val collection = storage.multi(Arg.of(0), Arg.of(1)).get();
val collection = storage.multi(
Arg.of(BlockNumber.GENESIS),
Arg.of(BlockNumber.of(1)))
.get();
assertNotNull(collection);

val list = new ArrayList<Pair<Integer, BlockHash>>();
collection.iterator().forEachRemaining(e -> e.consume((value, keys) -> list.add(Pair.of((Integer) keys.get(0), value))));
val list = new ArrayList<Pair<BlockNumber, BlockHash>>();
collection.iterator()
.forEachRemaining(e ->
e.consume((value, keys) ->
list.add(Pair.of((BlockNumber) keys.get(0), value))));

assertEquals(2, list.size());

assertEquals(0, list.get(0).getValue0());
assertEquals(BlockNumber.GENESIS, list.get(0).getValue0());
assertNotEquals(BigInteger.ZERO, new BigInteger(list.get(0).getValue1().getData()));

assertEquals(1, list.get(1).getValue0());
assertEquals(BlockNumber.of(1), list.get(1).getValue0());
assertNull(list.get(1).getValue1());
}
}
Expand Down Expand Up @@ -204,14 +211,17 @@ void entriesPaged() throws Exception {
assertNotNull(pages);

var pageCount = 0;
val pairs = new ArrayList<Pair<Integer, BlockHash>>();
val pairs = new ArrayList<Pair<BlockNumber, BlockHash>>();
while (pages.moveNext().join()) {
pages.current().iterator().forEachRemaining(e -> e.consume((value, keys) -> {
val key = (Integer) keys.get(0);
assertNotEquals(BigInteger.ZERO, new BigInteger(value.getData()));

pairs.add(Pair.of(key, value));
}));
pages.current()
.iterator()
.forEachRemaining(e ->
e.consume((value, keys) -> {
val key = (BlockNumber) keys.get(0);
assertNotEquals(BigInteger.ZERO, new BigInteger(value.getData()));

pairs.add(Pair.of(key, value));
}));
pageCount++;
assertEquals(pageCount, pages.number());
}
Expand All @@ -227,16 +237,16 @@ void entriesPaged() throws Exception {
void subscribe() throws Exception {
try (val wsProvider = getConnectedProvider()) {
val state = RpcGeneratedSectionFactory.create(State.class, wsProvider);
val blockNumber = 2;
val blockNumber = BlockNumber.of(2);
val storage = newSystemBlockHashStorage(state);
val blockHash = new AtomicReference<BlockHash>();
val value = new AtomicReference<BlockHash>();
val argument = new AtomicInteger();
val argument = new AtomicReference<BlockNumber>();
val unsubscribe = storage.subscribe((exception, block, v, keys) -> {
if (exception == null) {
blockHash.set(block);
value.set(v);
argument.set((Integer) keys.get(0));
argument.set((BlockNumber) keys.get(0));
}
}, Arg.of(blockNumber))
.get(WAIT_TIMEOUT, TimeUnit.SECONDS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.strategyobject.substrateclient.crypto.ss58.SS58Codec;
import com.strategyobject.substrateclient.rpc.RpcGeneratedSectionFactory;
import com.strategyobject.substrateclient.rpc.api.AccountId;
import com.strategyobject.substrateclient.rpc.api.BlockNumber;
import com.strategyobject.substrateclient.rpc.api.section.Chain;
import com.strategyobject.substrateclient.rpc.api.section.State;
import com.strategyobject.substrateclient.scale.registries.ScaleReaderRegistry;
Expand Down Expand Up @@ -64,7 +65,7 @@ void sudoKeyAtGenesis() throws Exception {
val state = RpcGeneratedSectionFactory.create(State.class, wsProvider);
val chain = RpcGeneratedSectionFactory.create(Chain.class, wsProvider);

val blockHash = chain.getBlockHash(0).get();
val blockHash = chain.getBlockHash(BlockNumber.GENESIS).get();
val storage = StorageValueImpl.with(
state,
ScaleReaderRegistry.getInstance().resolve(AccountId.class),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,4 @@
package com.strategyobject.substrateclient.rpc.api;

import com.strategyobject.substrateclient.common.types.FixedBytes;
import com.strategyobject.substrateclient.common.types.Size;
import com.strategyobject.substrateclient.scale.ScaleSelfWritable;
import lombok.NonNull;

public class BlockHash
extends FixedBytes<Size.Of32>
implements ScaleSelfWritable<BlockHash> {
protected BlockHash(byte[] data) {
super(data, Size.of32);
}

public static BlockHash fromBytes(byte @NonNull [] data) {
return new BlockHash(data);
}
public interface BlockHash extends Hash {
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.strategyobject.substrateclient.rpc.api;

import com.strategyobject.substrateclient.scale.ScaleSelfWritable;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

import java.math.BigInteger;

@RequiredArgsConstructor(staticName = "of")
@Getter
@EqualsAndHashCode
public class BlockNumber implements ScaleSelfWritable<BlockNumber> {
public static final BlockNumber GENESIS = BlockNumber.of(BigInteger.ZERO);

private final BigInteger value;

public static BlockNumber of(long value) {
return of(BigInteger.valueOf(value));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@

import java.math.BigInteger;

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

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

import com.strategyobject.substrateclient.rpc.EncoderPair;
import com.strategyobject.substrateclient.rpc.RpcEncoder;
import com.strategyobject.substrateclient.rpc.annotation.AutoRegister;

@AutoRegister(types = BlockNumber.class)
public class BlockNumberEncoder implements RpcEncoder<BlockNumber> {

@Override
public Object encode(BlockNumber source, EncoderPair<?>... encoders) {
return "0x" + source.getValue().toString(16);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.strategyobject.substrateclient.rpc.api;

import com.google.common.base.Preconditions;
import com.strategyobject.substrateclient.scale.ScaleReader;
import com.strategyobject.substrateclient.scale.ScaleType;
import com.strategyobject.substrateclient.scale.annotation.AutoRegister;
import com.strategyobject.substrateclient.scale.registries.ScaleReaderRegistry;
import lombok.NonNull;
import lombok.val;

import java.io.IOException;
import java.io.InputStream;

@AutoRegister(types = BlockNumber.class)
public class BlockNumberU32Reader implements ScaleReader<BlockNumber> {
@SuppressWarnings("unchecked")
@Override
public BlockNumber read(@NonNull InputStream stream, ScaleReader<?>... readers) throws IOException {
Preconditions.checkArgument(readers == null || readers.length == 0);

val u32Reader = (ScaleReader<Long>) ScaleReaderRegistry.getInstance().resolve(ScaleType.U32.class);
return BlockNumber.of(u32Reader.read(stream));
}
}
Loading