Skip to content

Commit a9a7a1b

Browse files
serban300bkchr
authored andcommitted
Define OpaqueValue (paritytech#4550)
Define `OpaqueValue` and use it instead of `grandpa::OpaqueKeyOwnershipProof` and `beefy:OpaqueKeyOwnershipProof` Related to paritytech#4522 (comment) We'll need to introduce a runtime API method that calls the `report_fork_voting_unsigned()` extrinsic. This method will need to receive the ancestry proof as a paramater. I'm still not sure, but there is a chance that we'll send the ancestry proof as an opaque type. So let's introduce this `OpaqueValue`. We can already use it to replace `grandpa::OpaqueKeyOwnershipProof` and `beefy:OpaqueKeyOwnershipProof` and maybe we'll need it for the ancestry proof as well. --------- Co-authored-by: Bastian Köcher <[email protected]>
1 parent 7a34837 commit a9a7a1b

File tree

3 files changed

+22
-33
lines changed

3 files changed

+22
-33
lines changed

substrate/primitives/consensus/beefy/src/lib.rs

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ use core::fmt::{Debug, Display};
5252
use scale_info::TypeInfo;
5353
use sp_application_crypto::{AppCrypto, AppPublic, ByteArray, RuntimeAppPublic};
5454
use sp_core::H256;
55-
use sp_runtime::traits::{Hash, Keccak256, NumberFor};
55+
use sp_runtime::{
56+
traits::{Hash, Keccak256, NumberFor},
57+
OpaqueValue,
58+
};
5659

5760
/// Key type for BEEFY module.
5861
pub const KEY_TYPE: sp_core::crypto::KeyTypeId = sp_application_crypto::key_types::BEEFY;
@@ -399,21 +402,7 @@ impl<AuthorityId> OnNewValidatorSet<AuthorityId> for () {
399402
/// the runtime API boundary this type is unknown and as such we keep this
400403
/// opaque representation, implementors of the runtime API will have to make
401404
/// sure that all usages of `OpaqueKeyOwnershipProof` refer to the same type.
402-
#[derive(Decode, Encode, PartialEq, TypeInfo)]
403-
pub struct OpaqueKeyOwnershipProof(Vec<u8>);
404-
impl OpaqueKeyOwnershipProof {
405-
/// Create a new `OpaqueKeyOwnershipProof` using the given encoded
406-
/// representation.
407-
pub fn new(inner: Vec<u8>) -> OpaqueKeyOwnershipProof {
408-
OpaqueKeyOwnershipProof(inner)
409-
}
410-
411-
/// Try to decode this `OpaqueKeyOwnershipProof` into the given concrete key
412-
/// ownership proof type.
413-
pub fn decode<T: Decode>(self) -> Option<T> {
414-
codec::Decode::decode(&mut &self.0[..]).ok()
415-
}
416-
}
405+
pub type OpaqueKeyOwnershipProof = OpaqueValue;
417406

418407
sp_api::decl_runtime_apis! {
419408
/// API necessary for BEEFY voters.

substrate/primitives/consensus/grandpa/src/lib.rs

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use scale_info::TypeInfo;
3131
use sp_keystore::KeystorePtr;
3232
use sp_runtime::{
3333
traits::{Header as HeaderT, NumberFor},
34-
ConsensusEngineId, RuntimeDebug,
34+
ConsensusEngineId, OpaqueValue, RuntimeDebug,
3535
};
3636

3737
/// The log target to be used by client code.
@@ -465,22 +465,7 @@ where
465465
/// the runtime API boundary this type is unknown and as such we keep this
466466
/// opaque representation, implementors of the runtime API will have to make
467467
/// sure that all usages of `OpaqueKeyOwnershipProof` refer to the same type.
468-
#[derive(Decode, Encode, PartialEq, TypeInfo)]
469-
pub struct OpaqueKeyOwnershipProof(Vec<u8>);
470-
471-
impl OpaqueKeyOwnershipProof {
472-
/// Create a new `OpaqueKeyOwnershipProof` using the given encoded
473-
/// representation.
474-
pub fn new(inner: Vec<u8>) -> OpaqueKeyOwnershipProof {
475-
OpaqueKeyOwnershipProof(inner)
476-
}
477-
478-
/// Try to decode this `OpaqueKeyOwnershipProof` into the given concrete key
479-
/// ownership proof type.
480-
pub fn decode<T: Decode>(self) -> Option<T> {
481-
codec::Decode::decode(&mut &self.0[..]).ok()
482-
}
483-
}
468+
pub type OpaqueKeyOwnershipProof = OpaqueValue;
484469

485470
sp_api::decl_runtime_apis! {
486471
/// APIs for integrating the GRANDPA finality gadget into runtimes.

substrate/primitives/runtime/src/lib.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,6 +1009,21 @@ pub enum ExtrinsicInclusionMode {
10091009
OnlyInherents,
10101010
}
10111011

1012+
/// Simple blob that hold a value in an encoded form without committing to its type.
1013+
#[derive(Decode, Encode, PartialEq, TypeInfo)]
1014+
pub struct OpaqueValue(Vec<u8>);
1015+
impl OpaqueValue {
1016+
/// Create a new `OpaqueValue` using the given encoded representation.
1017+
pub fn new(inner: Vec<u8>) -> OpaqueValue {
1018+
OpaqueValue(inner)
1019+
}
1020+
1021+
/// Try to decode this `OpaqueValue` into the given concrete type.
1022+
pub fn decode<T: Decode>(&self) -> Option<T> {
1023+
Decode::decode(&mut &self.0[..]).ok()
1024+
}
1025+
}
1026+
10121027
#[cfg(test)]
10131028
mod tests {
10141029
use crate::traits::BlakeTwo256;

0 commit comments

Comments
 (0)