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

Annotations for pallets and their storages #13

Merged
merged 1 commit into from
Apr 26, 2022

Conversation

vnabiev
Copy link
Collaborator

@vnabiev vnabiev commented Apr 14, 2022

Added annotations Pallet and Storage and related entities: StorageKey, StorageHasher.
Added PalletInterfaceProccessor which generates proxies for pallets to the blockchain by their annotations.
Added corresponding and auxiliary classes.

@vnabiev vnabiev force-pushed the feature/pallet-query-api branch 2 times, most recently from 0967419 to eb7bb80 Compare April 15, 2022 14:37
@vnabiev vnabiev changed the base branch from develop to feature/storage-api April 15, 2022 14:38
@vnabiev vnabiev force-pushed the feature/pallet-query-api branch 2 times, most recently from 748dd45 to 59105ba Compare April 15, 2022 17:02
val ctor = implClazz.getConstructor(Rpc.class);

return interfaceClass.cast(ctor.newInstance(rpc));
} catch (ClassCastException | ClassNotFoundException | NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ClassCastException is already a RuntimeException
also all the rest exceptions are subtypes of ReflectiveOperationException, so can be reduced to
catch (ReflectiveOperationException e) if you want


@Override
public <T> T pallet(@NonNull Class<T> clazz) {
return palletResolver.resolve(clazz);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pallet can be memoized to not instantiate it each time

@Retention(RetentionPolicy.RUNTIME)
public @interface Pallet {
@NonNull
String name();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't it better to call this field value instead of name so that an enduser can write @Pallet("System") instead of @Pallet(name = "System")

.build();
}

private MethodSpec privateMethod(String palletName,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this name is a bit misleading. I expected it to generate some internal method that it triggered by the public method defined later. But it turned out to be a some sort of initializer

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

renamed to backFieldInitializer

.addAnnotation(suppressWarnings("unchecked", "rawtypes"))
.returns(TypeName.get(returnType));

declareReaderAndWriterRegistries(methodSpecBuilder);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cannot it be moved upper to a class level? registries are threadsafe and needed by multiple implementations.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's not necessary because there are just variables which refer to singletons.

@vnabiev vnabiev force-pushed the feature/storage-api branch 2 times, most recently from 871ff7b to d2811c1 Compare April 22, 2022 13:49
@vnabiev vnabiev force-pushed the feature/pallet-query-api branch 2 times, most recently from 996ae0b to 280c558 Compare April 25, 2022 12:18
@vnabiev vnabiev changed the title Feature/pallet query api Annotations for pallets and their storages Apr 25, 2022
@vnabiev vnabiev changed the base branch from feature/storage-api to develop April 25, 2022 12:28
@vnabiev vnabiev force-pushed the feature/pallet-query-api branch from 280c558 to 7243809 Compare April 25, 2022 12:54

/**
* Indicates the pallet which represents a proxy to the blockchain's pallet.
* For interfaces annotated with it processor will generate proper implementations.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The processor will generate proper implementations for the interfaces annotated with this method.

public <T> T resolve(Class<T> interfaceClass) {
if (interfaceClass.getDeclaredAnnotationsByType(Pallet.class).length == 0) {
throw new IllegalArgumentException(
String.format("`%s` can't be constructed because isn't annotated with `@%s`.",
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

String.format("%s can't be constructed because it is not annotated with @%s.",


assertThat(compilation).failed();
assertThat(compilation)
.hadErrorContaining("Must be set `type` or `generic` of `@StorageKey`");
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.hadErrorContaining("Please set either type or generic parameter of @StorageKey");


/**
* Provides the ability to query a node and interact with the Polkadot or Substrate chains.
* It allows interacting with blockchain in different ways: using RPC's queries directly or
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It allows interacting with blockchain in two different ways: using RPC's queries directly or
OR
It allows interacting with blockchain in various ways: using RPC's queries directly or

/**
* Provides the ability to query a node and interact with the Polkadot or Substrate chains.
* It allows interacting with blockchain in different ways: using RPC's queries directly or
* accessing Pallets and its APIs such storages, transactions, etc.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

accessing Pallets and its APIs, such as storages, transactions, etc.

}

/**
* @return the instance that provides proper API for querying RPC's methods.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@return the instance that provides a proper API for querying the RPC's methods.

Rpc rpc();

/**
* Resolves the instance of pallet by its definition.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolves the instance of a pallet by its definition.
OR
Resolves a pallet instance by its definition.


assertThat(compilation).failed();
assertThat(compilation)
.hadErrorContaining("Must be set only one of both parameters of `@StorageKey`: `type` or `generic`.");
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.hadErrorContaining("Please set only one parameter of @StorageKey: type or generic.");

throw new ProcessingException(
palletElement,
"`@%s` of `%s.%s` isn't adjusted correctly. " +
"Must be set `type` or `generic` of `@StorageKey`.",
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Please set either type or generic parameter of @StorageKey.",

palletElement,
"`@%s` of `%s.%s` isn't adjusted correctly. " +
"Ambiguous scale type of key. " +
"Must be set only one of both parameters of `@StorageKey`: `type` or `generic`.",
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Please set only one parameter of @StorageKey: type or generic.",

Added annotations `Pallet` and `Storage` and related entities: `StorageKey`, `StorageHasher`.
Added `PalletInterfaceProccessor` which generates proxies for pallets to the blockchain by their annotations.
Added corresponding and auxiliary classes.
@vnabiev vnabiev force-pushed the feature/pallet-query-api branch from 7243809 to 8b5fe92 Compare April 26, 2022 14:37
@vnabiev vnabiev merged commit 942bf3c into develop Apr 26, 2022
@vnabiev vnabiev deleted the feature/pallet-query-api branch April 26, 2022 15:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants