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

add MetadataProvider #24

Merged
merged 1 commit into from
Jul 12, 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
11 changes: 11 additions & 0 deletions api/src/main/java/com/strategyobject/substrateclient/api/Api.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.inject.Module;
import com.strategyobject.substrateclient.pallet.PalletFactory;
import com.strategyobject.substrateclient.rpc.RpcSectionFactory;
import com.strategyobject.substrateclient.rpc.metadata.MetadataProvider;
import com.strategyobject.substrateclient.transport.ProviderInterface;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
Expand All @@ -20,6 +21,7 @@
public class Api implements AutoCloseable {
private final @NonNull RpcSectionFactory rpcSectionFactory;
private final @NonNull PalletFactory palletFactory;
private final @NonNull MetadataProvider metadataProvider;
private final Map<Class<?>, Object> resolvedCache = new ConcurrentHashMap<>();


Expand All @@ -45,6 +47,15 @@ public <T> T pallet(@NonNull Class<T> clazz) {
return clazz.cast(resolvedCache.computeIfAbsent(clazz, palletFactory::create));
}

/**
* Provides access to current version of metadata in use.
*
* @return MetadataProvider instance
*/
public MetadataProvider metadata() {
return metadataProvider;
}

@Override
public void close() throws Exception {
if (rpcSectionFactory instanceof AutoCloseable) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,20 @@
import com.google.inject.AbstractModule;
import com.google.inject.Provides;
import com.google.inject.Singleton;
import com.strategyobject.substrateclient.common.types.AutoRegistry;
import com.strategyobject.substrateclient.crypto.ss58.SS58AddressFormat;
import com.strategyobject.substrateclient.pallet.GeneratedPalletFactory;
import com.strategyobject.substrateclient.pallet.PalletFactory;
import com.strategyobject.substrateclient.rpc.GeneratedRpcSectionFactory;
import com.strategyobject.substrateclient.rpc.RpcSectionFactory;
import com.strategyobject.substrateclient.rpc.api.section.State;
import com.strategyobject.substrateclient.rpc.context.RpcDecoderContext;
import com.strategyobject.substrateclient.rpc.context.RpcDecoderContextFactory;
import com.strategyobject.substrateclient.rpc.context.RpcEncoderContext;
import com.strategyobject.substrateclient.rpc.context.RpcEncoderContextFactory;
import com.strategyobject.substrateclient.rpc.metadata.ManualMetadataProvider;
import com.strategyobject.substrateclient.rpc.metadata.MetadataProvider;
import com.strategyobject.substrateclient.rpc.metadata.Pallet;
import com.strategyobject.substrateclient.rpc.metadata.PalletCollection;
import com.strategyobject.substrateclient.rpc.registries.RpcDecoderRegistry;
import com.strategyobject.substrateclient.rpc.registries.RpcEncoderRegistry;
import com.strategyobject.substrateclient.scale.registries.ScaleReaderRegistry;
Expand All @@ -18,20 +26,21 @@
import lombok.RequiredArgsConstructor;
import lombok.val;

import java.util.function.BiConsumer;
import java.util.function.Consumer;

@RequiredArgsConstructor
public class DefaultModule extends AbstractModule {
private final @NonNull ProviderInterface providerInterface;
private static final String PREFIX = "com.strategyobject.substrateclient";

private Consumer<ScaleReaderRegistry> configureScaleReaderRegistry = this::autoRegister;
private Consumer<ScaleWriterRegistry> configureScaleWriterRegistry = this::autoRegister;
private Consumer<RpcDecoderRegistry> configureRpcDecoderRegistry = this::autoRegister;
private Consumer<RpcEncoderRegistry> configureRpcEncoderRegistry = this::autoRegister;
private final @NonNull ProviderInterface providerInterface;

private void autoRegister(AutoRegistry registry) {
registry.registerAnnotatedFrom("com.strategyobject.substrateclient");
}
private Consumer<ScaleReaderRegistry> configureScaleReaderRegistry = x -> x.registerAnnotatedFrom(PREFIX);
private Consumer<ScaleWriterRegistry> configureScaleWriterRegistry = x -> x.registerAnnotatedFrom(PREFIX);
private BiConsumer<RpcDecoderRegistry, RpcDecoderContextFactory> configureRpcDecoderRegistry =
(registry, contextFactory) -> registry.registerAnnotatedFrom(contextFactory, PREFIX);
private BiConsumer<RpcEncoderRegistry, RpcEncoderContextFactory> configureRpcEncoderRegistry =
(registry, contextFactory) -> registry.registerAnnotatedFrom(contextFactory, PREFIX);

public DefaultModule configureScaleReaderRegistry(Consumer<ScaleReaderRegistry> configure) {
configureScaleReaderRegistry = configureScaleReaderRegistry.andThen(configure);
Expand All @@ -43,12 +52,12 @@ public DefaultModule configureScaleWriterRegistry(Consumer<ScaleWriterRegistry>
return this;
}

public DefaultModule configureRpcDecoderRegistry(Consumer<RpcDecoderRegistry> configure) {
public DefaultModule configureRpcDecoderRegistry(BiConsumer<RpcDecoderRegistry, RpcDecoderContextFactory> configure) {
configureRpcDecoderRegistry = configureRpcDecoderRegistry.andThen(configure);
return this;
}

public DefaultModule configureRpcEncoderRegistry(Consumer<RpcEncoderRegistry> configure) {
public DefaultModule configureRpcEncoderRegistry(BiConsumer<RpcEncoderRegistry, RpcEncoderContextFactory> configure) {
configureRpcEncoderRegistry = configureRpcEncoderRegistry.andThen(configure);
return this;
}
Expand Down Expand Up @@ -78,7 +87,8 @@ protected void configure() {
.toConstructor(
Api.class.getConstructor(
RpcSectionFactory.class,
PalletFactory.class))
PalletFactory.class,
MetadataProvider.class))
.asEagerSingleton();
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
Expand All @@ -103,20 +113,77 @@ public ScaleWriterRegistry provideScaleWriterRegistry() {

@Provides
@Singleton
public RpcDecoderRegistry provideRpcDecoderRegistry(ScaleReaderRegistry scaleReaderRegistry) {
val registry = new RpcDecoderRegistry(scaleReaderRegistry);
configureRpcDecoderRegistry.accept(registry);
public RpcDecoderRegistry provideRpcDecoderRegistry(MetadataProvider metadataProvider,
ScaleReaderRegistry scaleReaderRegistry) {
val registry = new RpcDecoderRegistry();
val context = new RpcDecoderContext(
metadataProvider,
registry,
scaleReaderRegistry);

configureRpcDecoderRegistry.accept(registry, () -> context);
return registry;
}

@Provides
@Singleton
public RpcEncoderRegistry provideRpcEncoderRegistry(ScaleWriterRegistry scaleWriterRegistry) {
val registry = new RpcEncoderRegistry(scaleWriterRegistry);
configureRpcEncoderRegistry.accept(registry);
public RpcEncoderRegistry provideRpcEncoderRegistry(MetadataProvider metadataProvider,
ScaleWriterRegistry scaleWriterRegistry) {
val registry = new RpcEncoderRegistry();
val context = new RpcEncoderContext(
metadataProvider,
registry,
scaleWriterRegistry);

configureRpcEncoderRegistry.accept(registry, () -> context);
return registry;
}

@Provides
@Singleton
public MetadataProvider provideMetadata() {
// TODO. Use provider based on real Metadata
return new ManualMetadataProvider(
SS58AddressFormat.SUBSTRATE_ACCOUNT,
new PalletCollection(
new Pallet(0, "System"),
new Pallet(1, "Utility"),
new Pallet(2, "Babe"),
new Pallet(3, "Timestamp"),
new Pallet(4, "Authorship"),
new Pallet(5, "Indices"),
new Pallet(6, "Balances"),
new Pallet(7, "TransactionPayment"),
new Pallet(8, "Staking"),
new Pallet(9, "Session"),
new Pallet(10, "Democracy"),
new Pallet(11, "Council"),
new Pallet(12, "TechnicalCommittee"),
new Pallet(13, "Elections"),
new Pallet(14, "TechnicalMembership"),
new Pallet(15, "Grandpa"),
new Pallet(16, "Treasury"),
new Pallet(17, "Contracts"),
new Pallet(18, "Sudo"),
new Pallet(19, "ImOnline"),
new Pallet(20, "AuthorityDiscovery"),
new Pallet(21, "Offences"),
new Pallet(22, "Historical"),
new Pallet(23, "RandomnessCollectiveFlip"),
new Pallet(24, "Identity"),
new Pallet(25, "Society"),
new Pallet(26, "Recovery"),
new Pallet(27, "Vesting"),
new Pallet(28, "Scheduler"),
new Pallet(29, "Proxy"),
new Pallet(30, "Multisig"),
new Pallet(31, "Bounties"),
new Pallet(32, "Tips"),
new Pallet(33, "Assets"),
new Pallet(34, "Mmr"),
new Pallet(35, "Lottery")));
}

@Provides
@Singleton
public State provideState(RpcSectionFactory rpcSectionFactory) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.inject.AbstractModule;
import com.strategyobject.substrateclient.pallet.PalletFactory;
import com.strategyobject.substrateclient.rpc.RpcSectionFactory;
import com.strategyobject.substrateclient.rpc.metadata.MetadataProvider;
import com.strategyobject.substrateclient.rpc.registries.RpcDecoderRegistry;
import com.strategyobject.substrateclient.rpc.registries.RpcEncoderRegistry;
import com.strategyobject.substrateclient.scale.registries.ScaleReaderRegistry;
Expand All @@ -17,6 +18,7 @@ protected void configure() {
requireBinding(ScaleWriterRegistry.class);
requireBinding(RpcDecoderRegistry.class);
requireBinding(RpcEncoderRegistry.class);
requireBinding(MetadataProvider.class);
requireBinding(RpcSectionFactory.class);
requireBinding(PalletFactory.class);
requireBinding(Api.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.inject.CreationException;
import com.google.inject.util.Modules;
import com.strategyobject.substrateclient.common.convert.HexConverter;
import com.strategyobject.substrateclient.crypto.ss58.SS58AddressFormat;
import com.strategyobject.substrateclient.pallet.PalletFactory;
import com.strategyobject.substrateclient.rpc.api.AccountId;
import com.strategyobject.substrateclient.rpc.api.BlockNumber;
Expand All @@ -24,7 +25,7 @@
import static org.mockito.Mockito.verify;

@Testcontainers
class ApiTests {
class ApiTest {
private static final int WAIT_TIMEOUT = 1000;

@Container
Expand Down Expand Up @@ -61,6 +62,19 @@ void getSystemSectionAndCall() throws Exception {
}
}

@Test
void getSS58AddressFormat() throws Exception {
val wsProvider = WsProvider.builder()
.setEndpoint(substrate.getWsAddress());

try (val api = Api.with(wsProvider).build().join()) {
val ss58AddressFormat = api.metadata().getSS58AddressFormat();

assertNotNull(ss58AddressFormat);
assertEquals(SS58AddressFormat.SUBSTRATE_ACCOUNT, ss58AddressFormat);
}
}

@Test
void configureApi() throws Exception {
val wsProvider = WsProvider.builder()
Expand All @@ -69,10 +83,8 @@ void configureApi() throws Exception {
val expected = mock(Index.class);
try (val api = Api.with(wsProvider)
.configure(defaultModule ->
defaultModule.configureRpcDecoderRegistry(registry ->
registry.register(
(value, decoders) -> expected,
Index.class)))
defaultModule.configureRpcDecoderRegistry((registry, _factory) ->
registry.register((value, decoders) -> expected, Index.class)))
.build()
.join()) {
val system = api.rpc(System.class);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
@Getter
public class AddressWithPrefix {
private final byte @NonNull [] address;
private final short prefix;
private final SS58AddressFormat prefix;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.strategyobject.substrateclient.crypto.ss58;

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

@Getter
@RequiredArgsConstructor(staticName = "of")
@EqualsAndHashCode
public class SS58AddressFormat {
private final short prefix;

/**
* Polkadot Relay-chain, standard account (*25519).
*/
public static final SS58AddressFormat POLKADOT_ACCOUNT = new SS58AddressFormat((short) 0);

/**
* Bare 32-bit Schnorr/Ristretto 25519 (S/R 25519) key.
*/
public static final SS58AddressFormat BARE_SR_25519 = new SS58AddressFormat((short) 1);

/**
* Kusama Relay-chain, standard account (*25519).
*/
public static final SS58AddressFormat KUSAMA_ACCOUNT = new SS58AddressFormat((short) 2);

/**
* Bare 32-bit Edwards Ed25519 key.
*/
public static final SS58AddressFormat BARE_ED_25519 = new SS58AddressFormat((short) 3);

/**
* Any Substrate network, standard account (*25519).
*/
public static final SS58AddressFormat SUBSTRATE_ACCOUNT = new SS58AddressFormat((short) 42);
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,17 @@ public static AddressWithPrefix decode(@NonNull String encoded) {
throw new IllegalArgumentException("Incorrect checksum.");
}

return AddressWithPrefix.from(Arrays.copyOfRange(data, typeLen, typeLen + ADDRESS_LENGTH), prefix);
return AddressWithPrefix.from(
Arrays.copyOfRange(data, typeLen, typeLen + ADDRESS_LENGTH),
SS58AddressFormat.of(prefix));
}

public static String encode(byte @NonNull [] address, short prefix) {
public static String encode(byte @NonNull [] address, SS58AddressFormat prefix) {
Preconditions.checkArgument(address.length == ADDRESS_LENGTH,
"The length of address must be 32, but was: " + address.length);

val ident = prefix & 0b0011_1111_1111_1111;
Preconditions.checkArgument(ident == prefix,
val ident = prefix.getPrefix() & 0b0011_1111_1111_1111;
Preconditions.checkArgument(ident == prefix.getPrefix(),
"The prefix size is restricted by 14 bits.");

byte[] data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class SS58CodecTests {
},
delimiterString = ":")
void encode(String expected, String hex, short prefix) {
val actual = SS58Codec.encode(HexConverter.toBytes(hex), prefix);
val actual = SS58Codec.encode(HexConverter.toBytes(hex), SS58AddressFormat.of(prefix));

assertEquals(expected, actual);
}
Expand All @@ -33,7 +33,9 @@ void encode(String expected, String hex, short prefix) {
},
delimiterString = ":")
void encodeThrows(String hex, short prefix) {
assertThrows(IllegalArgumentException.class, () -> SS58Codec.encode(HexConverter.toBytes(hex), prefix));
val format = SS58AddressFormat.of(prefix);
val address = HexConverter.toBytes(hex);
assertThrows(IllegalArgumentException.class, () -> SS58Codec.encode(address, format));
}

@ParameterizedTest
Expand All @@ -49,7 +51,7 @@ void encodeThrows(String hex, short prefix) {
void decode(String source, String hex, short prefix) {
val actual = SS58Codec.decode(source);

val expected = AddressWithPrefix.from(HexConverter.toBytes(hex), prefix);
val expected = AddressWithPrefix.from(HexConverter.toBytes(hex), SS58AddressFormat.of(prefix));
assertEquals(expected, actual);
}

Expand Down
Loading