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

Extension System: stable extension_id calculation #33

Open
indirection42 opened this issue Jun 25, 2024 · 1 comment
Open

Extension System: stable extension_id calculation #33

indirection42 opened this issue Jun 25, 2024 · 1 comment

Comments

@indirection42
Copy link
Contributor

indirection42 commented Jun 25, 2024

Currently, the extension_id is simply calculated on AST level which is not only stable. see:

fn calculate_hash(trait_items: &[TraitItem]) -> u64 {

Theoretically, we want two trait methods sets produce the same extension id, after the resolution of type aliases.
However, the type aliases can be defined out of the TokenStream that current proc-macro-attribute processed. So how we do with it?
pub type AccountIdFor<T> = <<T as ExtensionFungibles>::Config as Config>::AccountId;
pub type BalanceFor<T> = <<T as ExtensionFungibles>::Config as Config>::Balance;
pub type AssetIdFor<T> = <<T as ExtensionFungibles>::Config as Config>::AssetId;
#[extension]
pub trait ExtensionFungibles {
type Config: Config;
// fungibles::Inspect (not extensive)
// fn total_inssuance(asset: AssetIdFor<Self>) -> BalanceFor<Self>;
// fn minimum_balance(asset: AssetIdFor<Self>) -> BalanceFor<Self>;
fn total_supply(asset: AssetIdFor<Self>) -> BalanceFor<Self>;
fn balance(asset: AssetIdFor<Self>, who: AccountIdFor<Self>) -> BalanceFor<Self>;
// fungibles::InspectEnumerable
// fn asset_ids() -> Vec<AccountIdFor<Self>>;
// fn account_balances(who: AccountIdFor<Self>) -> Vec<(AssetIdFor<Self>, BalanceFor<Self>)>;
}

And we also need a helper function/macro for "client" to easily specify the extension_id which matches the "server" side.
However, these types are concrete types.
#[derive(Encode, Decode)]
enum FungiblesMethod {
TotalSupply { asset: u64 },
Balance { asset: u64, who: [u8; 32] },
}

@indirection42
Copy link
Contributor Author

Currently, we calculate like this (only consider trait_ident and method_ident):

fn calculate_hash(trait_ident: &Ident, trait_items: &[TraitItem]) -> u64 {
let mut hasher = twox_hash::XxHash64::default();
trait_ident.hash(&mut hasher);
for trait_item in trait_items {
if let TraitItem::Fn(method) = trait_item {
method.sig.ident.hash(&mut hasher);
}
}
hasher.finish()
}

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

No branches or pull requests

1 participant