-
Notifications
You must be signed in to change notification settings - Fork 262
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
Reference key storage api #447
Merged
Merged
Changes from 15 commits
Commits
Show all changes
37 commits
Select commit
Hold shift + click to select a range
b9d419f
codegen: Update polkadot.rs
lexnv eb768d8
codegen: Reference key storage api
lexnv 0419623
codegen: Regenerate polkadot.rs with reference api
lexnv 9a2c9ca
tests: Update tests with reference interface
lexnv 6fe8818
cli: Fix polkadot.rs license check
lexnv 2970d05
codegen: Update polkadot.rs with copyright
lexnv bfc7fe1
Revert "codegen: Update polkadot.rs with copyright"
lexnv 553573a
codegen: Implement AccountData trait in the expected order
lexnv cd65398
codegen: Store implementation of StorageEntry
lexnv 9651879
codegen: Generate AccountDefaultData wrapper struct
lexnv 9816182
codegen: Allow `Account` references
lexnv 7c19fde
codegen: Update polkadot.rs
lexnv f02ccf8
codegen: Utilize AccountDefaultData instead of Account
lexnv 7cb306a
codegen: Update polkadot.rs
lexnv a8d709c
tests: Update tests to utilize `Account` reference
lexnv e5c8db7
codegen: Rename AccountDefaultData to AccountOwned
lexnv 8925c3f
codegen: Add comments for wrapper account
lexnv b06264d
codegen: Obtain vector type parameter for TypePath::Type
lexnv 4b7806e
codegen: Use slices instead of `& std::vec` in storage API
lexnv 508116d
codegen: Update polkadot.rs
lexnv f09a213
Merge remote-tracking branch 'origin/master' into 411_ref_key_storage
lexnv 763a836
codegen: Fix documentation
lexnv fc352ac
tests: Remove extra reference
lexnv c94a21f
examples: Add staking example to exercise storage API
lexnv 4465ef2
Merge remote-tracking branch 'origin/master' into 411_ref_key_storage
lexnv 8b13c4c
Merge remote-tracking branch 'origin/master' into 411_temp
lexnv fa9f03a
codegen: Update polkadot.rs
lexnv d541736
tests: Update storage tests
lexnv 2bcd009
Fix cargo clippy
lexnv b9e84c6
codegen: Simplify vec_type_param
lexnv bb97e6a
examples: Rename staking_details.rs to fetch_staking_details.rs
lexnv ac98865
tests: Remove dummy variable
lexnv 6bee03b
examples: Update polkadot version
lexnv d485513
Apply rust-fmt
lexnv 3420b86
Merge remote-tracking branch 'origin/master' into 411_ref_key_storage
lexnv a17058a
codegen: Regenerate polkadot.rs
lexnv 66fc95a
examples: Remove comment
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
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -74,12 +74,15 @@ fn generate_storage_entry_fns( | |||||
storage_entry: &StorageEntryMetadata<PortableForm>, | ||||||
) -> (TokenStream2, TokenStream2) { | ||||||
let entry_struct_ident = format_ident!("{}", storage_entry.name); | ||||||
let (fields, entry_struct, constructor, key_impl) = match storage_entry.ty { | ||||||
let is_account_wrapper = pallet.name == "System" && storage_entry.name == "Account"; | ||||||
let wrapper_struct_ident = format_ident!("{}DefaultData", storage_entry.name); | ||||||
let (fields, entry_struct, constructor, key_impl, should_ref) = match storage_entry.ty | ||||||
{ | ||||||
StorageEntryType::Plain(_) => { | ||||||
let entry_struct = quote!( pub struct #entry_struct_ident; ); | ||||||
let constructor = quote!( #entry_struct_ident ); | ||||||
let key_impl = quote!(::subxt::StorageEntryKey::Plain); | ||||||
(vec![], entry_struct, constructor, key_impl) | ||||||
(vec![], entry_struct, constructor, key_impl, false) | ||||||
} | ||||||
StorageEntryType::Map { | ||||||
ref key, | ||||||
|
@@ -120,7 +123,7 @@ fn generate_storage_entry_fns( | |||||
fields.iter().map(|(_, field_type)| field_type); | ||||||
let field_names = fields.iter().map(|(field_name, _)| field_name); | ||||||
let entry_struct = quote! { | ||||||
pub struct #entry_struct_ident( #( pub #tuple_struct_fields ),* ); | ||||||
pub struct #entry_struct_ident <'a>( #( pub &'a #tuple_struct_fields ),* ); | ||||||
}; | ||||||
let constructor = | ||||||
quote!( #entry_struct_ident( #( #field_names ),* ) ); | ||||||
|
@@ -135,13 +138,28 @@ fn generate_storage_entry_fns( | |||||
vec![ #( #keys ),* ] | ||||||
) | ||||||
}; | ||||||
(fields, entry_struct, constructor, key_impl) | ||||||
(fields, entry_struct, constructor, key_impl, true) | ||||||
} | ||||||
_ => { | ||||||
let (lifetime_param, lifetime_ref) = (quote!(<'a>), quote!(&'a)); | ||||||
|
||||||
let ty_path = type_gen.resolve_type_path(key.id(), &[]); | ||||||
let fields = vec![(format_ident!("_0"), ty_path.clone())]; | ||||||
// `::system::storage::Account` was utilized as associated type `StorageEntry` | ||||||
// for `::subxt::AccountData` implementation of generated `DefaultAccountData`. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
// Due to changes in the storage API, `::system::storage::Account` cannot be | ||||||
// used without specifying a lifetime. To satisfy `::subxt::AccountData` | ||||||
// implementation, a non-reference wrapper `AccountDefaultData` is generated. | ||||||
let wrapper_struct = if is_account_wrapper { | ||||||
quote!( | ||||||
pub struct #wrapper_struct_ident ( pub #ty_path ); | ||||||
) | ||||||
} else { | ||||||
quote!() | ||||||
}; | ||||||
let entry_struct = quote! { | ||||||
pub struct #entry_struct_ident( pub #ty_path ); | ||||||
pub struct #entry_struct_ident #lifetime_param( pub #lifetime_ref #ty_path ); | ||||||
#wrapper_struct | ||||||
}; | ||||||
let constructor = quote!( #entry_struct_ident(_0) ); | ||||||
let hasher = hashers.get(0).unwrap_or_else(|| { | ||||||
|
@@ -152,7 +170,7 @@ fn generate_storage_entry_fns( | |||||
vec![ ::subxt::StorageMapKey::new(&self.0, #hasher) ] | ||||||
) | ||||||
}; | ||||||
(fields, entry_struct, constructor, key_impl) | ||||||
(fields, entry_struct, constructor, key_impl, true) | ||||||
} | ||||||
} | ||||||
} | ||||||
|
@@ -178,25 +196,47 @@ fn generate_storage_entry_fns( | |||||
} | ||||||
}; | ||||||
|
||||||
let (lifetime_param, reference, anon_lifetime) = if should_ref { | ||||||
(quote!(<'a>), quote!(&), quote!(<'_>)) | ||||||
} else { | ||||||
(quote!(), quote!(), quote!()) | ||||||
}; | ||||||
|
||||||
let storage_entry_impl = quote! ( | ||||||
const PALLET: &'static str = #pallet_name; | ||||||
const STORAGE: &'static str = #storage_name; | ||||||
type Value = #storage_entry_value_ty; | ||||||
fn key(&self) -> ::subxt::StorageEntryKey { | ||||||
#key_impl | ||||||
} | ||||||
); | ||||||
|
||||||
let wrapper_entry_impl = if is_account_wrapper { | ||||||
jsdw marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
quote!( | ||||||
impl ::subxt::StorageEntry for #wrapper_struct_ident { | ||||||
#storage_entry_impl | ||||||
} | ||||||
) | ||||||
} else { | ||||||
quote!() | ||||||
}; | ||||||
|
||||||
let storage_entry_type = quote! { | ||||||
#entry_struct | ||||||
|
||||||
impl ::subxt::StorageEntry for #entry_struct_ident { | ||||||
const PALLET: &'static str = #pallet_name; | ||||||
const STORAGE: &'static str = #storage_name; | ||||||
type Value = #storage_entry_value_ty; | ||||||
fn key(&self) -> ::subxt::StorageEntryKey { | ||||||
#key_impl | ||||||
} | ||||||
impl ::subxt::StorageEntry for #entry_struct_ident #anon_lifetime { | ||||||
#storage_entry_impl | ||||||
} | ||||||
|
||||||
#wrapper_entry_impl | ||||||
}; | ||||||
|
||||||
let client_iter_fn = if matches!(storage_entry.ty, StorageEntryType::Map { .. }) { | ||||||
quote! ( | ||||||
pub async fn #fn_name_iter( | ||||||
&self, | ||||||
hash: ::core::option::Option<T::Hash>, | ||||||
) -> ::core::result::Result<::subxt::KeyIter<'a, T, #entry_struct_ident>, ::subxt::BasicError> { | ||||||
) -> ::core::result::Result<::subxt::KeyIter<'a, T, #entry_struct_ident #lifetime_param>, ::subxt::BasicError> { | ||||||
self.client.storage().iter(hash).await | ||||||
} | ||||||
) | ||||||
|
@@ -206,7 +246,7 @@ fn generate_storage_entry_fns( | |||||
|
||||||
let key_args = fields | ||||||
.iter() | ||||||
.map(|(field_name, field_type)| quote!( #field_name: #field_type )); | ||||||
.map(|(field_name, field_type)| quote!( #field_name: #reference #field_type )); | ||||||
let client_fns = quote! { | ||||||
pub async fn #fn_name( | ||||||
&self, | ||||||
|
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.
nitpicking: I'm not super fond of the name
AccountDefaultData
; I'd lean towards something likeAccountOwned
I guess.. but I'm not too fussed :DThere 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.
AccountOwned
sounds better :D Thanks