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

Reference key storage api #447

Merged
merged 37 commits into from
Mar 2, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
b9d419f
codegen: Update polkadot.rs
lexnv Feb 14, 2022
eb768d8
codegen: Reference key storage api
lexnv Feb 14, 2022
0419623
codegen: Regenerate polkadot.rs with reference api
lexnv Feb 14, 2022
9a2c9ca
tests: Update tests with reference interface
lexnv Feb 14, 2022
6fe8818
cli: Fix polkadot.rs license check
lexnv Feb 14, 2022
2970d05
codegen: Update polkadot.rs with copyright
lexnv Feb 14, 2022
bfc7fe1
Revert "codegen: Update polkadot.rs with copyright"
lexnv Feb 14, 2022
553573a
codegen: Implement AccountData trait in the expected order
lexnv Feb 14, 2022
cd65398
codegen: Store implementation of StorageEntry
lexnv Feb 15, 2022
9651879
codegen: Generate AccountDefaultData wrapper struct
lexnv Feb 15, 2022
9816182
codegen: Allow `Account` references
lexnv Feb 15, 2022
7c19fde
codegen: Update polkadot.rs
lexnv Feb 15, 2022
f02ccf8
codegen: Utilize AccountDefaultData instead of Account
lexnv Feb 15, 2022
7cb306a
codegen: Update polkadot.rs
lexnv Feb 15, 2022
a8d709c
tests: Update tests to utilize `Account` reference
lexnv Feb 15, 2022
e5c8db7
codegen: Rename AccountDefaultData to AccountOwned
lexnv Feb 16, 2022
8925c3f
codegen: Add comments for wrapper account
lexnv Feb 16, 2022
b06264d
codegen: Obtain vector type parameter for TypePath::Type
lexnv Feb 16, 2022
4b7806e
codegen: Use slices instead of `& std::vec` in storage API
lexnv Feb 16, 2022
508116d
codegen: Update polkadot.rs
lexnv Feb 16, 2022
f09a213
Merge remote-tracking branch 'origin/master' into 411_ref_key_storage
lexnv Feb 16, 2022
763a836
codegen: Fix documentation
lexnv Feb 16, 2022
fc352ac
tests: Remove extra reference
lexnv Feb 16, 2022
c94a21f
examples: Add staking example to exercise storage API
lexnv Feb 17, 2022
4465ef2
Merge remote-tracking branch 'origin/master' into 411_ref_key_storage
lexnv Feb 18, 2022
8b13c4c
Merge remote-tracking branch 'origin/master' into 411_temp
lexnv Mar 1, 2022
fa9f03a
codegen: Update polkadot.rs
lexnv Mar 1, 2022
d541736
tests: Update storage tests
lexnv Mar 1, 2022
2bcd009
Fix cargo clippy
lexnv Mar 1, 2022
b9e84c6
codegen: Simplify vec_type_param
lexnv Mar 1, 2022
bb97e6a
examples: Rename staking_details.rs to fetch_staking_details.rs
lexnv Mar 1, 2022
ac98865
tests: Remove dummy variable
lexnv Mar 1, 2022
6bee03b
examples: Update polkadot version
lexnv Mar 1, 2022
d485513
Apply rust-fmt
lexnv Mar 1, 2022
3420b86
Merge remote-tracking branch 'origin/master' into 411_ref_key_storage
lexnv Mar 2, 2022
a17058a
codegen: Regenerate polkadot.rs
lexnv Mar 2, 2022
66fc95a
examples: Remove comment
lexnv Mar 2, 2022
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
22 changes: 21 additions & 1 deletion cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,26 @@ fn codegen<I: Input>(
derives.append(p.into_iter());

let runtime_api = generator.generate_runtime(item_mod, derives);
println!("{}", runtime_api);
println!(
"
// Copyright 2019-2022 Parity Technologies (UK) Ltd.
// This file is part of subxt.
//
// subxt is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// subxt is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with subxt. If not, see <http://www.gnu.org/licenses/>.
dvdplm marked this conversation as resolved.
Show resolved Hide resolved

{}",
runtime_api
);
Ok(())
}
32 changes: 23 additions & 9 deletions codegen/src/api/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,13 @@ 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 (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,
Expand Down Expand Up @@ -120,7 +121,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 ),* ) );
Expand All @@ -135,13 +136,20 @@ fn generate_storage_entry_fns(
vec![ #( #keys ),* ]
)
};
(fields, entry_struct, constructor, key_impl)
(fields, entry_struct, constructor, key_impl, true)
}
_ => {
let should_ref = storage_entry.name != "Account";
Copy link
Collaborator

Choose a reason for hiding this comment

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

So I think this is the case because of the DefaultAccountData thing that's generated not taking a ref?

I don't really like the inconsistency this would introduce in the storage API (this call doesn't take a ref but other similar calls do). I think it'd be good to see whether the generated DefaultAccountData impl can be tweaked to take/allow the ref. @ascjones any thoughts on this?

let (lifetime_param, lifetime_ref) = if should_ref {
(quote!(<'a>), quote!(&'a))
} else {
(quote!(), quote!())
};

let ty_path = type_gen.resolve_type_path(key.id(), &[]);
let fields = vec![(format_ident!("_0"), ty_path.clone())];
let entry_struct = quote! {
pub struct #entry_struct_ident( pub #ty_path );
pub struct #entry_struct_ident #lifetime_param( pub #lifetime_ref #ty_path );
};
let constructor = quote!( #entry_struct_ident(_0) );
let hasher = hashers.get(0).unwrap_or_else(|| {
Expand All @@ -152,7 +160,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, should_ref)
}
}
}
Expand All @@ -178,10 +186,16 @@ 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_type = quote! {
#entry_struct

impl ::subxt::StorageEntry for #entry_struct_ident {
impl ::subxt::StorageEntry for #entry_struct_ident #anon_lifetime {
const PALLET: &'static str = #pallet_name;
const STORAGE: &'static str = #storage_name;
type Value = #storage_entry_value_ty;
Expand All @@ -196,7 +210,7 @@ fn generate_storage_entry_fns(
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
}
)
Expand All @@ -206,7 +220,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,
Expand Down
Loading