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

Make api and primitives generic over Runtime #340

Merged
merged 31 commits into from
Dec 6, 2022

Conversation

haerdib
Copy link
Contributor

@haerdib haerdib commented Nov 24, 2022

  • Types defined by the Runtime (Hash, Index, Balance .. ) are now not hardcoded any more, but depend on the Runtime input.
  • Renamed generic parameter P (probably for Pair) to Signer
  • Removed pallet dependencies completely from no_std due to default wasm compilation.

@haerdib haerdib self-assigned this Nov 28, 2022
@haerdib haerdib added F7-enhancement Enhances an already existing functionality E2-breaksapi labels Nov 29, 2022
@haerdib haerdib changed the title Make api generic over runtime Make api generic over Runtime Nov 29, 2022
@haerdib haerdib marked this pull request as ready for review November 29, 2022 07:34
Copy link
Contributor

@echevrier echevrier left a comment

Choose a reason for hiding this comment

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

LGTM

@haerdib haerdib force-pushed the bh/make-api-generic-over-runtime branch 2 times, most recently from 7dae69d to ba25bfc Compare November 29, 2022 13:10
add traits

add balanes impl

add Runtime trait

make api generic over Runtime

remove interfaces

fix clippy

fix staking feature

fix doc

make RuntimeVersion generic over Runtime

revert last commit

fix examples
@haerdib haerdib force-pushed the bh/make-api-generic-over-runtime branch from ba25bfc to 4679f17 Compare November 29, 2022 13:20
@haerdib haerdib marked this pull request as draft November 29, 2022 14:13
@haerdib haerdib marked this pull request as ready for review November 29, 2022 15:00
@haerdib haerdib marked this pull request as draft November 29, 2022 15:03
@haerdib haerdib changed the title Make api generic over Runtime Make api and primitives generic over Runtime Nov 29, 2022
@haerdib haerdib marked this pull request as ready for review November 30, 2022 08:05
@@ -180,7 +180,7 @@ impl EventDetails {

// topics come after the event data in EventRecord. They aren't used for
// anything at the moment, so just decode and throw them away.
let _topics = Vec::<Hash>::decode(input)?;
let _topics = Vec::<H256>::decode(input)?;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hardcoded H256 for now.. as they're not used and are unlikely to be different from H256 at the moment.

@@ -22,20 +22,3 @@ pub use extrinsics::*;

pub mod extrinsic_params;
pub mod extrinsics;

/// The block number type used in this runtime.
pub type BlockNumber = u64;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not needed anymore, because all these params should now be generic inside the api-client.

self.signer = Some(signer);
}

/// Get the public part of the api signer account.
pub fn signer_account(&self) -> Option<AccountId> {
pub fn signer_account(&self) -> Option<AccountId32> {
let pair = self.signer.as_ref()?;
let multi_signer = MultiSigner::from(pair.public());
Some(multi_signer.into_account())
Copy link
Contributor Author

Choose a reason for hiding this comment

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

MultiSigner only implements into_account for AccountId32. Not sure why, but I guess there's a reason.

Copy link
Collaborator

@clangenb clangenb Dec 6, 2022

Choose a reason for hiding this comment

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

Yeah, because moonbeam does in fact implement an account id with 20 bytes, but still use the multisigner if I am not mistaken.

@haerdib haerdib requested review from clangenb and removed request for clangenb December 1, 2022 07:19
Copy link
Collaborator

@clangenb clangenb left a comment

Choose a reason for hiding this comment

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

Nice to see everything getting so generic. However, I am unsure if depending on the runtime directly is a potential blocker for some use cases, especially the no-std world because it compiles to wasm in no-std. So maybe we need to have a layer in between like:

pub trait RuntimeConfig {
    type Balance;

    // etc.

   // We could even define the tips here
}

// We could add feature gated configs like:
impl RuntimeConfig for KitchenSinkRuntime {
   type Balance = KitchenSinkRuntime::Balance

   // etc
}

I am not sure if this creates problems on other ends though.

examples/staking_payout.rs Outdated Show resolved Hide resolved
@haerdib haerdib requested review from clangenb and removed request for clangenb December 5, 2022 09:28
@clangenb
Copy link
Collaborator

clangenb commented Dec 5, 2022

The pallets-itself also assume wasm in no-std, so in my opinion, you can't get around the runtime config trait that I am suggesting. So if you want to merge this PR now, I suggest fixing the trait immediately afterwards, so we don't have a breaking interface in subsequent releases.

@haerdib haerdib marked this pull request as draft December 5, 2022 16:47
@haerdib haerdib marked this pull request as ready for review December 6, 2022 13:00
@haerdib haerdib requested review from clangenb and removed request for clangenb December 6, 2022 13:00
Copy link
Collaborator

@clangenb clangenb left a comment

Choose a reason for hiding this comment

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

Nice looks good now. I like the approach! :)

Only a few minor remarks are left.

Comment on lines +18 to +20
//! Re-defintion of substrate primitives.
//! Needed because substrate pallets compile to wasm in no_std.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe we can note that all these primitives are from the pallet-balances, aren't they?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

They are not. Should I paste a link from where they are taken?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah, this would be fabulous! 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done :)

self.signer = Some(signer);
}

/// Get the public part of the api signer account.
pub fn signer_account(&self) -> Option<AccountId> {
pub fn signer_account(&self) -> Option<AccountId32> {
let pair = self.signer.as_ref()?;
let multi_signer = MultiSigner::from(pair.public());
Some(multi_signer.into_account())
Copy link
Collaborator

@clangenb clangenb Dec 6, 2022

Choose a reason for hiding this comment

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

Yeah, because moonbeam does in fact implement an account id with 20 bytes, but still use the multisigner if I am not mistaken.

src/api/api_client.rs Show resolved Hide resolved
Copy link
Collaborator

@clangenb clangenb left a comment

Choose a reason for hiding this comment

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

Nice, looks good to me!

@haerdib haerdib merged commit d540964 into master Dec 6, 2022
@haerdib haerdib deleted the bh/make-api-generic-over-runtime branch December 6, 2022 15:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
E2-breaksapi F7-enhancement Enhances an already existing functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants