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

Define OpaqueValue #4550

Merged
merged 2 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
21 changes: 5 additions & 16 deletions substrate/primitives/consensus/beefy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ use core::fmt::{Debug, Display};
use scale_info::TypeInfo;
use sp_application_crypto::{AppCrypto, AppPublic, ByteArray, RuntimeAppPublic};
use sp_core::H256;
use sp_runtime::traits::{Hash, Keccak256, NumberFor};
use sp_runtime::{
traits::{Hash, Keccak256, NumberFor},
OpaqueValue,
};

/// Key type for BEEFY module.
pub const KEY_TYPE: sp_core::crypto::KeyTypeId = sp_application_crypto::key_types::BEEFY;
Expand Down Expand Up @@ -399,21 +402,7 @@ impl<AuthorityId> OnNewValidatorSet<AuthorityId> for () {
/// the runtime API boundary this type is unknown and as such we keep this
/// opaque representation, implementors of the runtime API will have to make
/// sure that all usages of `OpaqueKeyOwnershipProof` refer to the same type.
#[derive(Decode, Encode, PartialEq, TypeInfo)]
pub struct OpaqueKeyOwnershipProof(Vec<u8>);
impl OpaqueKeyOwnershipProof {
/// Create a new `OpaqueKeyOwnershipProof` using the given encoded
/// representation.
pub fn new(inner: Vec<u8>) -> OpaqueKeyOwnershipProof {
OpaqueKeyOwnershipProof(inner)
}

/// Try to decode this `OpaqueKeyOwnershipProof` into the given concrete key
/// ownership proof type.
pub fn decode<T: Decode>(self) -> Option<T> {
codec::Decode::decode(&mut &self.0[..]).ok()
}
}
pub type OpaqueKeyOwnershipProof = OpaqueValue;

sp_api::decl_runtime_apis! {
/// API necessary for BEEFY voters.
Expand Down
19 changes: 2 additions & 17 deletions substrate/primitives/consensus/grandpa/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use scale_info::TypeInfo;
use sp_keystore::KeystorePtr;
use sp_runtime::{
traits::{Header as HeaderT, NumberFor},
ConsensusEngineId, RuntimeDebug,
ConsensusEngineId, OpaqueValue, RuntimeDebug,
};

/// The log target to be used by client code.
Expand Down Expand Up @@ -465,22 +465,7 @@ where
/// the runtime API boundary this type is unknown and as such we keep this
/// opaque representation, implementors of the runtime API will have to make
/// sure that all usages of `OpaqueKeyOwnershipProof` refer to the same type.
#[derive(Decode, Encode, PartialEq, TypeInfo)]
pub struct OpaqueKeyOwnershipProof(Vec<u8>);

impl OpaqueKeyOwnershipProof {
/// Create a new `OpaqueKeyOwnershipProof` using the given encoded
/// representation.
pub fn new(inner: Vec<u8>) -> OpaqueKeyOwnershipProof {
OpaqueKeyOwnershipProof(inner)
}

/// Try to decode this `OpaqueKeyOwnershipProof` into the given concrete key
/// ownership proof type.
pub fn decode<T: Decode>(self) -> Option<T> {
codec::Decode::decode(&mut &self.0[..]).ok()
}
}
pub type OpaqueKeyOwnershipProof = OpaqueValue;

sp_api::decl_runtime_apis! {
/// APIs for integrating the GRANDPA finality gadget into runtimes.
Expand Down
15 changes: 15 additions & 0 deletions substrate/primitives/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1009,6 +1009,21 @@ pub enum ExtrinsicInclusionMode {
OnlyInherents,
}

/// Simple blob that hold a value in an encoded form without committing to its type.
#[derive(Decode, Encode, PartialEq, TypeInfo)]
pub struct OpaqueValue(Vec<u8>);
impl OpaqueValue {
/// Create a new `OpaqueValue` using the given encoded representation.
pub fn new(inner: Vec<u8>) -> OpaqueValue {
OpaqueValue(inner)
}

/// Try to decode this `OpaqueValue` into the given concrete type.
pub fn decode<T: Decode>(self) -> Option<T> {
serban300 marked this conversation as resolved.
Show resolved Hide resolved
Decode::decode(&mut &self.0[..]).ok()
}
}

#[cfg(test)]
mod tests {
use crate::traits::BlakeTwo256;
Expand Down
Loading