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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
16 changes: 9 additions & 7 deletions api/build.gradle
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
dependencies {
implementation project(':common')
implementation project(':crypto')
implementation project(':pallet')
implementation project(':rpc')
implementation project(':rpc:rpc-core')
implementation project(':rpc:rpc-sections')
implementation project(':rpc:rpc-types')
implementation project(':rpc:rpc-api')
implementation project(':scale')
implementation project(':storage')
implementation project(':transport')
implementation project(':types')

annotationProcessor project(':pallet:pallet-codegen')
annotationProcessor project(':rpc:rpc-codegen')

testImplementation project(':tests')

testAnnotationProcessor project(':pallet:pallet-codegen')

testImplementation 'ch.qos.logback:logback-classic:1.2.11'
testImplementation 'org.testcontainers:testcontainers:1.17.1'
testImplementation 'org.testcontainers:junit-jupiter:1.17.1'

testAnnotationProcessor project(':pallet:pallet-codegen')
testImplementation 'org.awaitility:awaitility:4.2.0'
}
54 changes: 43 additions & 11 deletions api/src/main/java/com/strategyobject/substrateclient/api/Api.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,65 @@
package com.strategyobject.substrateclient.api;

import com.strategyobject.substrateclient.pallet.GeneratedPalletResolver;
import com.strategyobject.substrateclient.rpc.Rpc;
import com.strategyobject.substrateclient.rpc.RpcImpl;
import com.strategyobject.substrateclient.pallet.PalletResolver;
import com.strategyobject.substrateclient.rpc.RpcGeneratedSectionFactory;
import com.strategyobject.substrateclient.rpc.api.section.State;
import com.strategyobject.substrateclient.transport.ProviderInterface;
import lombok.NonNull;
import lombok.val;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
* Provides the ability to query a node and interact with the Polkadot or Substrate chains.
* It allows interacting with blockchain in various ways: using RPC's queries directly or
* accessing Pallets and its APIs, such as storages, transactions, etc.
*/
public interface Api {
static DefaultApi with(ProviderInterface provider) {
val rpc = RpcImpl.with(provider);
public class Api implements AutoCloseable {
private final @NonNull ProviderInterface providerInterface;
private final @NonNull PalletResolver palletResolver;
private final Map<Class<?>, Object> resolvedCache = new ConcurrentHashMap<>();

private Api(@NonNull ProviderInterface providerInterface) {
this.providerInterface = providerInterface;

return DefaultApi.with(rpc, GeneratedPalletResolver.with(rpc));
val state = RpcGeneratedSectionFactory.create(State.class, providerInterface);
this.palletResolver = GeneratedPalletResolver.with(state);
}

/**
* @return the instance that provides a proper API for querying the RPC's methods.
* Resolves the instance of a rpc by its definition.
*
* @param clazz the class of the rpc
* @param <T> the type of the rpc
* @return appropriate instance of the rpc
*/
Rpc rpc();
public <T> T rpc(Class<T> clazz) {
return clazz.cast(resolvedCache
.computeIfAbsent(clazz, x -> RpcGeneratedSectionFactory.create(x, providerInterface)));
}

/**
* Resolves the instance of a pallet by its definition.
*
* @param clazz the class of the pallet
* @param <T> the type of the pallet
* @param <T> the type of the pallet
* @return appropriate instance of the pallet
*/
<T> T pallet(Class<T> clazz);
}
public <T> T pallet(@NonNull Class<T> clazz) {
return clazz.cast(resolvedCache
.computeIfAbsent(clazz, palletResolver::resolve));
}

public static Api with(ProviderInterface providerInterface) {
return new Api(providerInterface);
}

@Override
public void close() throws Exception {
if (providerInterface instanceof AutoCloseable) {
((AutoCloseable) providerInterface).close();
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
import static org.junit.jupiter.api.Assertions.assertNotNull;

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

@Container
private final TestSubstrateContainer substrate = new TestSubstrateContainer(SubstrateVersion.V3_0_0);

@Test
public void getSystemPalletAndCall() throws Exception { // TODO move the test out of the project
void getSystemPalletAndCall() throws Exception { // TODO move the test out of the project
val wsProvider = WsProvider.builder()
.setEndpoint(substrate.getWsAddress())
.build();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.strategyobject.substrateclient.api;

import com.strategyobject.substrateclient.pallet.annotations.Pallet;
import com.strategyobject.substrateclient.pallet.annotations.Storage;
import com.strategyobject.substrateclient.pallet.annotations.StorageHasher;
import com.strategyobject.substrateclient.pallet.annotations.StorageKey;
import com.strategyobject.substrateclient.rpc.types.BlockHash;
import com.strategyobject.substrateclient.scale.annotations.Scale;
import com.strategyobject.substrateclient.storage.StorageNMap;
import com.strategyobject.substrateclient.pallet.annotation.Pallet;
import com.strategyobject.substrateclient.pallet.annotation.Storage;
import com.strategyobject.substrateclient.pallet.annotation.StorageHasher;
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.scale.annotation.Scale;

@Pallet("System")
public interface SystemPallet {
Expand All @@ -15,7 +15,7 @@ public interface SystemPallet {
keys = {
@StorageKey(
type = @Scale(Integer.class),
hasher = StorageHasher.TwoX64Concat
hasher = StorageHasher.TWOX_64_CONCAT
)
})
StorageNMap<BlockHash> blockHash();
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {

allprojects {
group = 'com.strategyobject.substrateclient'
version = '0.1.1-SNAPSHOT'
version = '0.1.2-SNAPSHOT'

repositories {
mavenLocal()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public boolean isNonGeneric(@NonNull TypeMirror type) {
return type.getKind().isPrimitive() ||
((TypeElement) typeUtils.asElement(type))
.getTypeParameters()
.size() == 0;
.isEmpty();
}

public TypeMirror erasure(@NonNull TypeMirror type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public T traverse(@NonNull TypeTraverser.TypeTreeNode typeOverride) {
}

val declaredType = (DeclaredType) typeOverride.type;
if (typeOverride.children.size() == 0) {
if (typeOverride.children.isEmpty()) {
return whenNonGenericType(declaredType, typeOverride.type);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,7 @@ public static String getSimpleName(TypeMirror type) {

throw new IllegalArgumentException(String.format("Cannot populate the name of %s", type));
}

private TypeUtils() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.strategyobject.substrateclient.common.types;

public interface Array<T> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.strategyobject.substrateclient.common.types;

import com.google.common.base.Preconditions;

/**
* Upperbounds the size of a class.
*
* @param <S> Size
*/
public interface Bounded<S extends Size> {
/**
* Asserts that the size is correct.
*
* @param expected Expected size
* @param actual Actual size
*/
default void assertSize(S expected, int actual) {
Preconditions.checkArgument(
actual <= expected.getValue(),
"Provided size (%s) is invalid. Expected not more than %s.", actual, expected.getValue());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.strategyobject.substrateclient.common.types;

import com.google.common.base.Preconditions;

/**
* Sets the size of a class.
*
* @param <S> Size
*/
public interface Fixed<S extends Size> {

/**
* Asserts that the size is correct.
*
* @param expected Expected size
* @param actual Actual size
*/
default void assertSize(S expected, int actual) {
Preconditions.checkArgument(
actual == expected.getValue(),
"Provided size (%s) is invalid. Expected %s.", actual, expected.getValue());
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
package com.strategyobject.substrateclient.types;
package com.strategyobject.substrateclient.common.types;

import com.google.common.base.Preconditions;
import lombok.EqualsAndHashCode;
import lombok.Getter;

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

protected FixedBytes(byte[] data, S size) {
Preconditions.checkNotNull(data);
Preconditions.checkArgument(
data.length == size.getValue(),
"The data size must be %s; received %s.", size.getValue(), data.length);
assertSize(size, data.length);

this.data = data;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.strategyobject.substrateclient.types;
package com.strategyobject.substrateclient.common.types;

import lombok.val;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.strategyobject.substrateclient.types;
package com.strategyobject.substrateclient.common.types;

public interface Size {
int getValue();
Expand All @@ -8,6 +8,7 @@ public interface Size {
Of64 of64 = new Of64();
Of96 of96 = new Of96();
Of128 of128 = new Of128();
Of256 of256 = new Of256();

class Zero implements Size {
@Override
Expand Down Expand Up @@ -43,4 +44,11 @@ public int getValue() {
return 128;
}
}

class Of256 implements Size {
@Override
public int getValue() {
return 256;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.strategyobject.substrateclient.types;
package com.strategyobject.substrateclient.common.types;

public class Unit {
private static final Unit UNIT = new Unit();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.strategyobject.substrateclient.types.tuples;
package com.strategyobject.substrateclient.common.types.tuple;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.strategyobject.substrateclient.types.union;
package com.strategyobject.substrateclient.common.types.union;

public abstract class Union {
protected int index;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.strategyobject.substrateclient.types.union;
package com.strategyobject.substrateclient.common.types.union;

import lombok.NonNull;
import lombok.val;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.strategyobject.substrateclient.types.union;
package com.strategyobject.substrateclient.common.types.union;

import com.google.common.base.Preconditions;
import lombok.NonNull;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.strategyobject.substrateclient.types.union;
package com.strategyobject.substrateclient.common.types.union;

import com.google.common.base.Preconditions;
import lombok.NonNull;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.strategyobject.substrateclient.types.union;
package com.strategyobject.substrateclient.common.types.union;

import com.google.common.base.Preconditions;
import lombok.NonNull;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.strategyobject.substrateclient.types.union;
package com.strategyobject.substrateclient.common.types.union;

import com.google.common.base.Preconditions;
import lombok.NonNull;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.strategyobject.substrateclient.types.union;
package com.strategyobject.substrateclient.common.types.union;

import com.google.common.base.Preconditions;
import lombok.NonNull;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.strategyobject.substrateclient.types.union;
package com.strategyobject.substrateclient.common.types.union;

import com.google.common.base.Preconditions;
import lombok.NonNull;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.strategyobject.substrateclient.types.union;
package com.strategyobject.substrateclient.common.types.union;

import com.google.common.base.Preconditions;
import lombok.NonNull;
Expand Down
Loading