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

Add a basic version of the CheckMetadataHash signed extension #1590

Merged
merged 7 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 2 additions & 0 deletions core/src/config/default_extrinsic_params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub type DefaultExtrinsicParams<T> = signed_extensions::AnyOf<
signed_extensions::CheckMortality<T>,
signed_extensions::ChargeAssetTxPayment<T>,
signed_extensions::ChargeTransactionPayment,
signed_extensions::CheckMetadataHash,
),
>;

Expand Down Expand Up @@ -151,6 +152,7 @@ impl<T: Config> DefaultExtrinsicParamsBuilder<T> {
check_mortality_params,
charge_asset_tx_params,
charge_transaction_params,
(),
)
}
}
55 changes: 55 additions & 0 deletions core/src/config/signed_extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,61 @@ pub trait SignedExtension<T: Config>: ExtrinsicParams<T> {
fn matches(identifier: &str, _type_id: u32, _types: &PortableRegistry) -> bool;
}

/// The [`CheckMetadataHash`] signed extension.
pub struct CheckMetadataHash {
// Eventually we might provide or calculate the metadata hash here,
// but for now we never provide a hash and so this is empty.
}

impl<T: Config> ExtrinsicParams<T> for CheckMetadataHash {
type Params = ();

fn new(_client: &ClientState<T>, _params: Self::Params) -> Result<Self, ExtrinsicParamsError> {
Ok(CheckMetadataHash {})
}
}

impl ExtrinsicParamsEncoder for CheckMetadataHash {
fn encode_extra_to(&self, v: &mut Vec<u8>) {
// A single 0 byte in the TX payload indicates that the chain should
// _not_ expect any metadata hash to exist in the signer payload.
0u8.encode_to(v);
jsdw marked this conversation as resolved.
Show resolved Hide resolved
}
fn encode_additional_to(&self, v: &mut Vec<u8>) {
// We provide no metadata hash in the signer payload to align with the above.
None::<()>.encode_to(v);
}
}

impl<T: Config> SignedExtension<T> for CheckMetadataHash {
type Decoded = CheckMetadataHashMode;
fn matches(identifier: &str, _type_id: u32, _types: &PortableRegistry) -> bool {
identifier == "CheckMetadataHash"
}
}

/// Is metadata checking enabled or disabled?
// Dev note: The "Disabled" and "Enabled" variant names match those that the
// signed extension will be encoded with, in order that DecodeAsType will work
// properly.
#[derive(Copy, Clone, Debug, DecodeAsType)]
pub enum CheckMetadataHashMode {
/// No hash was provided in the signer payload.
Disabled,
/// A hash was provided in the signer payload.
Enabled,
}

impl CheckMetadataHashMode {
/// Is metadata checking enabled or disabled for this transaction?
pub fn is_enabled(&self) -> bool {
match self {
CheckMetadataHashMode::Disabled => false,
CheckMetadataHashMode::Enabled => true,
}
}
}

/// The [`CheckSpecVersion`] signed extension.
pub struct CheckSpecVersion(u32);

Expand Down
5 changes: 3 additions & 2 deletions subxt/examples/setup_config_signed_extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ impl Config for CustomConfig {
signed_extensions::CheckMortality<Self>,
signed_extensions::ChargeAssetTxPayment<Self>,
signed_extensions::ChargeTransactionPayment,
signed_extensions::CheckMetadataHash,
// And add a new one of our own:
CustomSignedExtension,
),
Expand Down Expand Up @@ -83,8 +84,8 @@ impl ExtrinsicParamsEncoder for CustomSignedExtension {
pub fn custom(
params: DefaultExtrinsicParamsBuilder<CustomConfig>,
) -> <<CustomConfig as Config>::ExtrinsicParams as ExtrinsicParams<CustomConfig>>::Params {
let (a, b, c, d, e, f, g) = params.build();
(a, b, c, d, e, f, g, ())
let (a, b, c, d, e, f, g, h) = params.build();
(a, b, c, d, e, f, g, h, ())
}

#[tokio::main]
Expand Down
Loading