-
Notifications
You must be signed in to change notification settings - Fork 248
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
wip: Remove subxt Config associated types by relying on enriched metadata (v16) #1566
Draft
lexnv
wants to merge
10
commits into
master
Choose a base branch
from
lexnv/codegen-config
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 5 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
0440bfa
codegen: Export custom types from md to separate module
lexnv 7c73b6d
config: Add config notes about extracted values
lexnv 21bbdc4
codegen: Use quote::idents instead of &str for naming types
lexnv df101a1
codegen: Make super types available for custom ty
lexnv a1434a9
example: Create an example Config out of the MetadataConfig
lexnv 609b701
config: Handle hasher and header
lexnv b13fcd6
Merge remote-tracking branch 'origin/master' into lexnv/codegen-config
lexnv 46bc4f2
Update to latest frame-metadata changes
lexnv d354546
metadata: Handle new format of v16
lexnv 59b9d10
cargo: Update frame-metadata to git dep
lexnv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#![allow(missing_docs)] | ||
use subxt::{OnlineClient, SubstrateConfig}; | ||
use subxt_core::config::{Config, DefaultExtrinsicParams}; | ||
use subxt_signer::sr25519::dev; | ||
|
||
// Generate an interface that we can use from the node's metadata. | ||
#[subxt::subxt(runtime_metadata_insecure_url = "ws://localhost:9999")] | ||
pub mod polkadot {} | ||
|
||
// Derives aren't strictly needed, they just make developer life easier. | ||
#[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)] | ||
pub enum MetadataConfig {} | ||
|
||
impl Config for MetadataConfig { | ||
// Extracted from metadata directly: | ||
type Hash = polkadot::custom_types::Hash; | ||
type AccountId = polkadot::custom_types::AccountId; | ||
type AssetId = polkadot::custom_types::AssetId; | ||
type Address = polkadot::custom_types::Address; | ||
|
||
// Present in metadata but this PoC needs to add | ||
// fn specific per name of type to impl the hashing fn. | ||
type Hasher = <SubstrateConfig as Config>::Hasher; | ||
// Present in metadata but this PoC needs to impl the header | ||
// trait to make use of this. | ||
type Header = <SubstrateConfig as Config>::Header; | ||
// Same story, present in md but needs subxt::tx::Signer<T>. | ||
// type Signature = polkadot::custom_types::Signature; | ||
type Signature = <SubstrateConfig as Config>::Signature; | ||
|
||
// Not exposed in metadata, seems like heavily involved with | ||
// code functionality which cannot safely be expressed in the | ||
// metadata. | ||
type ExtrinsicParams = DefaultExtrinsicParams<Self>; | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
// Create a new API client, configured to talk to nodes. | ||
let api = OnlineClient::<MetadataConfig>::from_insecure_url("ws://localhost:9999").await?; | ||
|
||
// Build a balance transfer extrinsic. | ||
let dest = dev::bob().public_key().into(); | ||
let balance_transfer_tx = polkadot::tx().balances().transfer_allow_death(dest, 10_000); | ||
|
||
// Submit the balance transfer extrinsic from Alice, and wait for it to be successful | ||
// and in a finalized block. We get back the extrinsic events if all is well. | ||
let from = dev::alice(); | ||
let events = api | ||
.tx() | ||
.sign_and_submit_then_watch_default(&balance_transfer_tx, &from) | ||
.await? | ||
.wait_for_finalized_success() | ||
.await?; | ||
|
||
// Find a Transfer event and print it. | ||
let transfer_event = events.find_first::<polkadot::balances::events::Transfer>()?; | ||
if let Some(event) = transfer_event { | ||
println!("Balance transfer success: {event:?}"); | ||
} | ||
|
||
Ok(()) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The thought I had for this sort of thing was originally going to be more like:
And then this hasher type (
DynamicHasher
here) is given the metadata when it tries to hash something, and can inspect the name of the hashing function and do the correct hashing logic itself.In either case though, you need it to line up with the
type Hash
I think, which would be interesting/messy to do. It might be that we can infer and generate theHash
type based on what we see that theHasher
is though, rather than trying to extract it from the metadata. So maybe you'd end up with:But we could theoretically do it all with codegen instead and extend the Subxt macro so that we can provide the necessary config types or override existing ones, but by default it will codegen relevant types like
Hasher
andHash
based on the metadata!This would have the advantage of being faster/more correct and the disadvantage of needing more different
Config
impls for different chains rather than potentially being able to rely on oneConfig
impl for multiple chains (or customising it for one chain).Something to ponder :)