Skip to content

Commit 7d3b515

Browse files
authored
Merge pull request #20 from strategyobject/feature/abstract-hash
Revise rpc-api types
2 parents 751668d + 438f0ea commit 7d3b515

File tree

42 files changed

+599
-190
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+599
-190
lines changed

api/src/test/java/com/strategyobject/substrateclient/api/ApiTests.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.strategyobject.substrateclient.api;
22

3+
import com.strategyobject.substrateclient.rpc.api.BlockNumber;
34
import com.strategyobject.substrateclient.tests.containers.SubstrateVersion;
45
import com.strategyobject.substrateclient.tests.containers.TestSubstrateContainer;
56
import com.strategyobject.substrateclient.transport.ws.WsProvider;
@@ -32,7 +33,7 @@ void getSystemPalletAndCall() throws Exception { // TODO move the test out of th
3233
val systemPallet = api.pallet(SystemPallet.class);
3334
val blockHash = systemPallet
3435
.blockHash()
35-
.get(0)
36+
.get(BlockNumber.GENESIS)
3637
.get(WAIT_TIMEOUT, TimeUnit.SECONDS);
3738

3839
assertNotNull(blockHash);

api/src/test/java/com/strategyobject/substrateclient/api/SystemPallet.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.strategyobject.substrateclient.pallet.annotation.StorageKey;
77
import com.strategyobject.substrateclient.pallet.storage.StorageNMap;
88
import com.strategyobject.substrateclient.rpc.api.BlockHash;
9+
import com.strategyobject.substrateclient.rpc.api.BlockNumber;
910
import com.strategyobject.substrateclient.scale.annotation.Scale;
1011

1112
@Pallet("System")
@@ -14,7 +15,7 @@ public interface SystemPallet {
1415
value = "BlockHash",
1516
keys = {
1617
@StorageKey(
17-
type = @Scale(Integer.class),
18+
type = @Scale(BlockNumber.class),
1819
hasher = StorageHasher.TWOX_64_CONCAT
1920
)
2021
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.strategyobject.substrateclient.common.types;
2+
3+
@FunctionalInterface
4+
public interface Bytes {
5+
byte[] getData();
6+
}

common/src/main/java/com/strategyobject/substrateclient/common/types/FixedBytes.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import lombok.Getter;
66

77
@EqualsAndHashCode
8-
public abstract class FixedBytes<S extends Size> implements Fixed<S> {
8+
public abstract class FixedBytes<S extends Size> implements Fixed<S>, Bytes {
99
@Getter
1010
private final byte[] data;
1111

pallet/src/main/java/com/strategyobject/substrateclient/pallet/storage/StorageNMapImpl.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import com.strategyobject.substrateclient.common.types.tuple.Pair;
44
import com.strategyobject.substrateclient.rpc.api.BlockHash;
55
import com.strategyobject.substrateclient.rpc.api.Hash;
6-
import com.strategyobject.substrateclient.rpc.api.section.State;
76
import com.strategyobject.substrateclient.rpc.api.StorageKey;
7+
import com.strategyobject.substrateclient.rpc.api.section.State;
88
import com.strategyobject.substrateclient.scale.ScaleReader;
99
import lombok.NonNull;
1010
import lombok.RequiredArgsConstructor;

pallet/src/test/java/com/strategyobject/substrateclient/pallet/storage/IdentityTests.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.strategyobject.substrateclient.pallet.storage;
22

33
import com.strategyobject.substrateclient.rpc.api.BlockHash;
4+
import com.strategyobject.substrateclient.rpc.api.impl.Hash256;
45
import com.strategyobject.substrateclient.scale.ScaleWriter;
56
import com.strategyobject.substrateclient.scale.registries.ScaleWriterRegistry;
67
import lombok.SneakyThrows;
@@ -19,7 +20,7 @@ class IdentityTests {
1920
private static Stream<byte[]> getTestCasesForGetHash() {
2021
return Stream.of(
2122
encode(Integer.class, -175),
22-
encode(BlockHash.class, BlockHash.fromBytes(random(32))),
23+
encode(BlockHash.class, Hash256.fromBytes(random(32))),
2324
"TestString".getBytes(StandardCharsets.UTF_8),
2425
random(new Random().nextInt(128) + 1));
2526
}

pallet/src/test/java/com/strategyobject/substrateclient/pallet/storage/KeyHasherTests.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.strategyobject.substrateclient.crypto.ss58.SS58Codec;
55
import com.strategyobject.substrateclient.rpc.api.AccountId;
66
import com.strategyobject.substrateclient.rpc.api.BlockHash;
7+
import com.strategyobject.substrateclient.rpc.api.impl.Hash256;
78
import com.strategyobject.substrateclient.scale.ScaleReader;
89
import com.strategyobject.substrateclient.scale.ScaleWriter;
910
import com.strategyobject.substrateclient.scale.readers.CompactIntegerReader;
@@ -44,7 +45,7 @@ private static Stream<Arguments> getTestCasesForGetHash() {
4445
"0x4f9aea1afa791265fae359272badc1cf8eaf04151687736326c9fea17e25fc5287613693c912909cb226aa4794f26a48"
4546
),
4647
Arguments.of(
47-
BlockHash.fromBytes(HexConverter.toBytes("0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef")),
48+
Hash256.fromBytes(HexConverter.toBytes("0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef")),
4849
ScaleReaderRegistry.getInstance().resolve(BlockHash.class),
4950
ScaleWriterRegistry.getInstance().resolve(BlockHash.class),
5051
Identity.getInstance(),
@@ -115,7 +116,7 @@ private static Stream<Arguments> getTestCasesForExtractKey() {
115116
ScaleReaderRegistry.getInstance().resolve(BlockHash.class),
116117
ScaleWriterRegistry.getInstance().resolve(BlockHash.class),
117118
Identity.getInstance(),
118-
BlockHash.fromBytes(HexConverter.toBytes("0xabcdef98765432100123456789abcdefabcdef98765432100123456789abcdef")),
119+
Hash256.fromBytes(HexConverter.toBytes("0xabcdef98765432100123456789abcdefabcdef98765432100123456789abcdef")),
119120
0
120121
),
121122
Arguments.of(

pallet/src/test/java/com/strategyobject/substrateclient/pallet/storage/StorageNMapImplTests.java

+35-25
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.strategyobject.substrateclient.rpc.RpcGeneratedSectionFactory;
66
import com.strategyobject.substrateclient.rpc.api.AccountId;
77
import com.strategyobject.substrateclient.rpc.api.BlockHash;
8+
import com.strategyobject.substrateclient.rpc.api.BlockNumber;
89
import com.strategyobject.substrateclient.rpc.api.section.Chain;
910
import com.strategyobject.substrateclient.rpc.api.section.State;
1011
import com.strategyobject.substrateclient.scale.ScaleReader;
@@ -48,8 +49,8 @@ private static StorageNMapImpl<BlockHash> newSystemBlockHashStorage(State state)
4849
state,
4950
(ScaleReader<BlockHash>) ScaleReaderRegistry.getInstance().resolve(BlockHash.class),
5051
StorageKeyProvider.of("System", "BlockHash")
51-
.use(KeyHasher.with((ScaleWriter<Integer>) ScaleWriterRegistry.getInstance().resolve(Integer.class),
52-
(ScaleReader<Integer>) ScaleReaderRegistry.getInstance().resolve(Integer.class),
52+
.use(KeyHasher.with((ScaleWriter<BlockNumber>) ScaleWriterRegistry.getInstance().resolve(BlockNumber.class),
53+
(ScaleReader<BlockNumber>) ScaleReaderRegistry.getInstance().resolve(BlockNumber.class),
5354
TwoX64Concat.getInstance())));
5455
}
5556

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

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

80-
assertEquals(0, blockNumber.get());
81+
assertEquals(BlockNumber.GENESIS, blockNumber.get());
8182

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

103104
val getKey = storageValue.query();
104-
val getHash = storageMap.query(0);
105+
val getHash = storageMap.query(BlockNumber.GENESIS);
105106

106107
val multi = getKey.join(getHash);
107108
val collection = multi.execute().get();
@@ -110,7 +111,7 @@ void multiToDifferentStorages() throws Exception {
110111
SS58Codec.decode(
111112
"5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY")
112113
.getAddress());
113-
val expectedBlock = storageMap.get(0).get();
114+
val expectedBlock = storageMap.get(BlockNumber.GENESIS).get();
114115

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

131132
assertNotNull(collection);
132133

133-
val blockNumber = new AtomicReference<Integer>(null);
134+
val blockNumber = new AtomicReference<BlockNumber>(null);
134135
val blockHash = new AtomicReference<BlockHash>(null);
135136
collection.iterator().forEachRemaining(e -> e.consume((value, keys) -> {
136137
blockHash.set(value);
137-
blockNumber.set((Integer) keys.get(0));
138+
blockNumber.set((BlockNumber) keys.get(0));
138139
}));
139140

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

151-
val collection = storage.multi(Arg.of(0), Arg.of(1)).get();
152+
val collection = storage.multi(
153+
Arg.of(BlockNumber.GENESIS),
154+
Arg.of(BlockNumber.of(1)))
155+
.get();
152156
assertNotNull(collection);
153157

154-
val list = new ArrayList<Pair<Integer, BlockHash>>();
155-
collection.iterator().forEachRemaining(e -> e.consume((value, keys) -> list.add(Pair.of((Integer) keys.get(0), value))));
158+
val list = new ArrayList<Pair<BlockNumber, BlockHash>>();
159+
collection.iterator()
160+
.forEachRemaining(e ->
161+
e.consume((value, keys) ->
162+
list.add(Pair.of((BlockNumber) keys.get(0), value))));
156163

157164
assertEquals(2, list.size());
158165

159-
assertEquals(0, list.get(0).getValue0());
166+
assertEquals(BlockNumber.GENESIS, list.get(0).getValue0());
160167
assertNotEquals(BigInteger.ZERO, new BigInteger(list.get(0).getValue1().getData()));
161168

162-
assertEquals(1, list.get(1).getValue0());
169+
assertEquals(BlockNumber.of(1), list.get(1).getValue0());
163170
assertNull(list.get(1).getValue1());
164171
}
165172
}
@@ -204,14 +211,17 @@ void entriesPaged() throws Exception {
204211
assertNotNull(pages);
205212

206213
var pageCount = 0;
207-
val pairs = new ArrayList<Pair<Integer, BlockHash>>();
214+
val pairs = new ArrayList<Pair<BlockNumber, BlockHash>>();
208215
while (pages.moveNext().join()) {
209-
pages.current().iterator().forEachRemaining(e -> e.consume((value, keys) -> {
210-
val key = (Integer) keys.get(0);
211-
assertNotEquals(BigInteger.ZERO, new BigInteger(value.getData()));
212-
213-
pairs.add(Pair.of(key, value));
214-
}));
216+
pages.current()
217+
.iterator()
218+
.forEachRemaining(e ->
219+
e.consume((value, keys) -> {
220+
val key = (BlockNumber) keys.get(0);
221+
assertNotEquals(BigInteger.ZERO, new BigInteger(value.getData()));
222+
223+
pairs.add(Pair.of(key, value));
224+
}));
215225
pageCount++;
216226
assertEquals(pageCount, pages.number());
217227
}
@@ -227,16 +237,16 @@ void entriesPaged() throws Exception {
227237
void subscribe() throws Exception {
228238
try (val wsProvider = getConnectedProvider()) {
229239
val state = RpcGeneratedSectionFactory.create(State.class, wsProvider);
230-
val blockNumber = 2;
240+
val blockNumber = BlockNumber.of(2);
231241
val storage = newSystemBlockHashStorage(state);
232242
val blockHash = new AtomicReference<BlockHash>();
233243
val value = new AtomicReference<BlockHash>();
234-
val argument = new AtomicInteger();
244+
val argument = new AtomicReference<BlockNumber>();
235245
val unsubscribe = storage.subscribe((exception, block, v, keys) -> {
236246
if (exception == null) {
237247
blockHash.set(block);
238248
value.set(v);
239-
argument.set((Integer) keys.get(0));
249+
argument.set((BlockNumber) keys.get(0));
240250
}
241251
}, Arg.of(blockNumber))
242252
.get(WAIT_TIMEOUT, TimeUnit.SECONDS);

pallet/src/test/java/com/strategyobject/substrateclient/pallet/storage/StorageValueImplTests.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.strategyobject.substrateclient.crypto.ss58.SS58Codec;
44
import com.strategyobject.substrateclient.rpc.RpcGeneratedSectionFactory;
55
import com.strategyobject.substrateclient.rpc.api.AccountId;
6+
import com.strategyobject.substrateclient.rpc.api.BlockNumber;
67
import com.strategyobject.substrateclient.rpc.api.section.Chain;
78
import com.strategyobject.substrateclient.rpc.api.section.State;
89
import com.strategyobject.substrateclient.scale.registries.ScaleReaderRegistry;
@@ -64,7 +65,7 @@ void sudoKeyAtGenesis() throws Exception {
6465
val state = RpcGeneratedSectionFactory.create(State.class, wsProvider);
6566
val chain = RpcGeneratedSectionFactory.create(Chain.class, wsProvider);
6667

67-
val blockHash = chain.getBlockHash(0).get();
68+
val blockHash = chain.getBlockHash(BlockNumber.GENESIS).get();
6869
val storage = StorageValueImpl.with(
6970
state,
7071
ScaleReaderRegistry.getInstance().resolve(AccountId.class),
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,4 @@
11
package com.strategyobject.substrateclient.rpc.api;
22

3-
import com.strategyobject.substrateclient.common.types.FixedBytes;
4-
import com.strategyobject.substrateclient.common.types.Size;
5-
import com.strategyobject.substrateclient.scale.ScaleSelfWritable;
6-
import lombok.NonNull;
7-
8-
public class BlockHash
9-
extends FixedBytes<Size.Of32>
10-
implements ScaleSelfWritable<BlockHash> {
11-
protected BlockHash(byte[] data) {
12-
super(data, Size.of32);
13-
}
14-
15-
public static BlockHash fromBytes(byte @NonNull [] data) {
16-
return new BlockHash(data);
17-
}
3+
public interface BlockHash extends Hash {
184
}

rpc/rpc-api/src/main/java/com/strategyobject/substrateclient/rpc/api/BlockHashReader.java

-21
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.strategyobject.substrateclient.rpc.api;
2+
3+
import com.strategyobject.substrateclient.scale.ScaleSelfWritable;
4+
import lombok.EqualsAndHashCode;
5+
import lombok.Getter;
6+
import lombok.RequiredArgsConstructor;
7+
8+
import java.math.BigInteger;
9+
10+
@RequiredArgsConstructor(staticName = "of")
11+
@Getter
12+
@EqualsAndHashCode
13+
public class BlockNumber implements ScaleSelfWritable<BlockNumber> {
14+
public static final BlockNumber GENESIS = BlockNumber.of(BigInteger.ZERO);
15+
16+
private final BigInteger value;
17+
18+
public static BlockNumber of(long value) {
19+
return of(BigInteger.valueOf(value));
20+
}
21+
}

rpc/rpc-api/src/main/java/com/strategyobject/substrateclient/rpc/api/NumberDecoder.java rpc/rpc-api/src/main/java/com/strategyobject/substrateclient/rpc/api/BlockNumberDecoder.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88

99
import java.math.BigInteger;
1010

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

18-
return Number.of(number);
18+
return BlockNumber.of(number);
1919
}
2020
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.strategyobject.substrateclient.rpc.api;
2+
3+
import com.strategyobject.substrateclient.rpc.EncoderPair;
4+
import com.strategyobject.substrateclient.rpc.RpcEncoder;
5+
import com.strategyobject.substrateclient.rpc.annotation.AutoRegister;
6+
7+
@AutoRegister(types = BlockNumber.class)
8+
public class BlockNumberEncoder implements RpcEncoder<BlockNumber> {
9+
10+
@Override
11+
public Object encode(BlockNumber source, EncoderPair<?>... encoders) {
12+
return "0x" + source.getValue().toString(16);
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.strategyobject.substrateclient.rpc.api;
2+
3+
import com.google.common.base.Preconditions;
4+
import com.strategyobject.substrateclient.scale.ScaleReader;
5+
import com.strategyobject.substrateclient.scale.ScaleType;
6+
import com.strategyobject.substrateclient.scale.annotation.AutoRegister;
7+
import com.strategyobject.substrateclient.scale.registries.ScaleReaderRegistry;
8+
import lombok.NonNull;
9+
import lombok.val;
10+
11+
import java.io.IOException;
12+
import java.io.InputStream;
13+
14+
@AutoRegister(types = BlockNumber.class)
15+
public class BlockNumberU32Reader implements ScaleReader<BlockNumber> {
16+
@SuppressWarnings("unchecked")
17+
@Override
18+
public BlockNumber read(@NonNull InputStream stream, ScaleReader<?>... readers) throws IOException {
19+
Preconditions.checkArgument(readers == null || readers.length == 0);
20+
21+
val u32Reader = (ScaleReader<Long>) ScaleReaderRegistry.getInstance().resolve(ScaleType.U32.class);
22+
return BlockNumber.of(u32Reader.read(stream));
23+
}
24+
}

0 commit comments

Comments
 (0)