Add some trivial improvements to primitives runtime (#8528)

* Add some trivial improvements

* Finish primitives/runtime
This commit is contained in:
Liu-Cheng Xu
2021-04-06 02:20:36 +08:00
committed by GitHub
parent b16bc0552e
commit 00432a5ab7
10 changed files with 96 additions and 110 deletions
@@ -40,7 +40,7 @@ pub struct Digest<Hash> {
impl<Item> Default for Digest<Item> {
fn default() -> Self {
Digest { logs: Vec::new(), }
Self { logs: Vec::new(), }
}
}
@@ -71,7 +71,6 @@ impl<Hash> Digest<Hash> {
}
}
/// Digest item that is able to encode/decode 'system' digest items and
/// provide opaque access to other items.
#[derive(PartialEq, Eq, Clone, RuntimeDebug)]
@@ -209,14 +208,14 @@ pub enum OpaqueDigestItemId<'a> {
impl<Hash> DigestItem<Hash> {
/// Returns a 'referencing view' for this digest item.
pub fn dref<'a>(&'a self) -> DigestItemRef<'a, Hash> {
pub fn dref(&self) -> DigestItemRef<Hash> {
match *self {
DigestItem::ChangesTrieRoot(ref v) => DigestItemRef::ChangesTrieRoot(v),
DigestItem::PreRuntime(ref v, ref s) => DigestItemRef::PreRuntime(v, s),
DigestItem::Consensus(ref v, ref s) => DigestItemRef::Consensus(v, s),
DigestItem::Seal(ref v, ref s) => DigestItemRef::Seal(v, s),
DigestItem::ChangesTrieSignal(ref s) => DigestItemRef::ChangesTrieSignal(s),
DigestItem::Other(ref v) => DigestItemRef::Other(v),
Self::ChangesTrieRoot(ref v) => DigestItemRef::ChangesTrieRoot(v),
Self::PreRuntime(ref v, ref s) => DigestItemRef::PreRuntime(v, s),
Self::Consensus(ref v, ref s) => DigestItemRef::Consensus(v, s),
Self::Seal(ref v, ref s) => DigestItemRef::Seal(v, s),
Self::ChangesTrieSignal(ref s) => DigestItemRef::ChangesTrieSignal(s),
Self::Other(ref v) => DigestItemRef::Other(v),
}
}
@@ -298,25 +297,25 @@ impl<Hash: Decode> Decode for DigestItem<Hash> {
fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
let item_type: DigestItemType = Decode::decode(input)?;
match item_type {
DigestItemType::ChangesTrieRoot => Ok(DigestItem::ChangesTrieRoot(
DigestItemType::ChangesTrieRoot => Ok(Self::ChangesTrieRoot(
Decode::decode(input)?,
)),
DigestItemType::PreRuntime => {
let vals: (ConsensusEngineId, Vec<u8>) = Decode::decode(input)?;
Ok(DigestItem::PreRuntime(vals.0, vals.1))
Ok(Self::PreRuntime(vals.0, vals.1))
},
DigestItemType::Consensus => {
let vals: (ConsensusEngineId, Vec<u8>) = Decode::decode(input)?;
Ok(DigestItem::Consensus(vals.0, vals.1))
Ok(Self::Consensus(vals.0, vals.1))
}
DigestItemType::Seal => {
let vals: (ConsensusEngineId, Vec<u8>) = Decode::decode(input)?;
Ok(DigestItem::Seal(vals.0, vals.1))
Ok(Self::Seal(vals.0, vals.1))
},
DigestItemType::ChangesTrieSignal => Ok(DigestItem::ChangesTrieSignal(
DigestItemType::ChangesTrieSignal => Ok(Self::ChangesTrieSignal(
Decode::decode(input)?,
)),
DigestItemType::Other => Ok(DigestItem::Other(
DigestItemType::Other => Ok(Self::Other(
Decode::decode(input)?,
)),
}
@@ -327,7 +326,7 @@ impl<'a, Hash> DigestItemRef<'a, Hash> {
/// Cast this digest item into `ChangesTrieRoot`.
pub fn as_changes_trie_root(&self) -> Option<&'a Hash> {
match *self {
DigestItemRef::ChangesTrieRoot(ref changes_trie_root) => Some(changes_trie_root),
Self::ChangesTrieRoot(ref changes_trie_root) => Some(changes_trie_root),
_ => None,
}
}
@@ -335,7 +334,7 @@ impl<'a, Hash> DigestItemRef<'a, Hash> {
/// Cast this digest item into `PreRuntime`
pub fn as_pre_runtime(&self) -> Option<(ConsensusEngineId, &'a [u8])> {
match *self {
DigestItemRef::PreRuntime(consensus_engine_id, ref data) => Some((*consensus_engine_id, data)),
Self::PreRuntime(consensus_engine_id, ref data) => Some((*consensus_engine_id, data)),
_ => None,
}
}
@@ -343,7 +342,7 @@ impl<'a, Hash> DigestItemRef<'a, Hash> {
/// Cast this digest item into `Consensus`
pub fn as_consensus(&self) -> Option<(ConsensusEngineId, &'a [u8])> {
match *self {
DigestItemRef::Consensus(consensus_engine_id, ref data) => Some((*consensus_engine_id, data)),
Self::Consensus(consensus_engine_id, ref data) => Some((*consensus_engine_id, data)),
_ => None,
}
}
@@ -351,7 +350,7 @@ impl<'a, Hash> DigestItemRef<'a, Hash> {
/// Cast this digest item into `Seal`
pub fn as_seal(&self) -> Option<(ConsensusEngineId, &'a [u8])> {
match *self {
DigestItemRef::Seal(consensus_engine_id, ref data) => Some((*consensus_engine_id, data)),
Self::Seal(consensus_engine_id, ref data) => Some((*consensus_engine_id, data)),
_ => None,
}
}
@@ -359,7 +358,7 @@ impl<'a, Hash> DigestItemRef<'a, Hash> {
/// Cast this digest item into `ChangesTrieSignal`.
pub fn as_changes_trie_signal(&self) -> Option<&'a ChangesTrieSignal> {
match *self {
DigestItemRef::ChangesTrieSignal(ref changes_trie_signal) => Some(changes_trie_signal),
Self::ChangesTrieSignal(ref changes_trie_signal) => Some(changes_trie_signal),
_ => None,
}
}
@@ -367,7 +366,7 @@ impl<'a, Hash> DigestItemRef<'a, Hash> {
/// Cast this digest item into `PreRuntime`
pub fn as_other(&self) -> Option<&'a [u8]> {
match *self {
DigestItemRef::Other(ref data) => Some(data),
Self::Other(ref data) => Some(data),
_ => None,
}
}
@@ -376,11 +375,11 @@ impl<'a, Hash> DigestItemRef<'a, Hash> {
/// return the opaque data it contains.
pub fn try_as_raw(&self, id: OpaqueDigestItemId) -> Option<&'a [u8]> {
match (id, self) {
(OpaqueDigestItemId::Consensus(w), &DigestItemRef::Consensus(v, s)) |
(OpaqueDigestItemId::Seal(w), &DigestItemRef::Seal(v, s)) |
(OpaqueDigestItemId::PreRuntime(w), &DigestItemRef::PreRuntime(v, s))
(OpaqueDigestItemId::Consensus(w), &Self::Consensus(v, s)) |
(OpaqueDigestItemId::Seal(w), &Self::Seal(v, s)) |
(OpaqueDigestItemId::PreRuntime(w), &Self::PreRuntime(v, s))
if v == w => Some(&s[..]),
(OpaqueDigestItemId::Other, &DigestItemRef::Other(s)) => Some(&s[..]),
(OpaqueDigestItemId::Other, &Self::Other(s)) => Some(&s[..]),
_ => None,
}
}
@@ -432,27 +431,27 @@ impl<'a, Hash: Encode> Encode for DigestItemRef<'a, Hash> {
let mut v = Vec::new();
match *self {
DigestItemRef::ChangesTrieRoot(changes_trie_root) => {
Self::ChangesTrieRoot(changes_trie_root) => {
DigestItemType::ChangesTrieRoot.encode_to(&mut v);
changes_trie_root.encode_to(&mut v);
},
DigestItemRef::Consensus(val, data) => {
Self::Consensus(val, data) => {
DigestItemType::Consensus.encode_to(&mut v);
(val, data).encode_to(&mut v);
},
DigestItemRef::Seal(val, sig) => {
Self::Seal(val, sig) => {
DigestItemType::Seal.encode_to(&mut v);
(val, sig).encode_to(&mut v);
},
DigestItemRef::PreRuntime(val, data) => {
Self::PreRuntime(val, data) => {
DigestItemType::PreRuntime.encode_to(&mut v);
(val, data).encode_to(&mut v);
},
DigestItemRef::ChangesTrieSignal(changes_trie_signal) => {
Self::ChangesTrieSignal(changes_trie_signal) => {
DigestItemType::ChangesTrieSignal.encode_to(&mut v);
changes_trie_signal.encode_to(&mut v);
},
DigestItemRef::Other(val) => {
Self::Other(val) => {
DigestItemType::Other.encode_to(&mut v);
val.encode_to(&mut v);
},
@@ -466,7 +465,7 @@ impl ChangesTrieSignal {
/// Try to cast this signal to NewConfiguration.
pub fn as_new_configuration(&self) -> Option<&Option<ChangesTrieConfiguration>> {
match self {
ChangesTrieSignal::NewConfiguration(config) => Some(config),
Self::NewConfiguration(config) => Some(config),
}
}
}
@@ -488,7 +487,7 @@ mod tests {
};
assert_eq!(
::serde_json::to_string(&digest).unwrap(),
serde_json::to_string(&digest).unwrap(),
r#"{"logs":["0x0204000000","0x000c010203","0x05746573740c010203"]}"#
);
}
+11 -14
View File
@@ -72,36 +72,33 @@ impl Era {
let quantize_factor = (period >> 12).max(1);
let quantized_phase = phase / quantize_factor * quantize_factor;
Era::Mortal(period, quantized_phase)
Self::Mortal(period, quantized_phase)
}
/// Create an "immortal" transaction.
pub fn immortal() -> Self {
Era::Immortal
Self::Immortal
}
/// `true` if this is an immortal transaction.
pub fn is_immortal(&self) -> bool {
match self {
Era::Immortal => true,
_ => false,
}
matches!(self, Self::Immortal)
}
/// Get the block number of the start of the era whose properties this object
/// describes that `current` belongs to.
pub fn birth(self, current: u64) -> u64 {
match self {
Era::Immortal => 0,
Era::Mortal(period, phase) => (current.max(phase) - phase) / period * period + phase,
Self::Immortal => 0,
Self::Mortal(period, phase) => (current.max(phase) - phase) / period * period + phase,
}
}
/// Get the block number of the first block at which the era has ended.
pub fn death(self, current: u64) -> u64 {
match self {
Era::Immortal => u64::max_value(),
Era::Mortal(period, _) => self.birth(current) + period,
Self::Immortal => u64::max_value(),
Self::Mortal(period, _) => self.birth(current) + period,
}
}
}
@@ -109,8 +106,8 @@ impl Era {
impl Encode for Era {
fn encode_to<T: Output + ?Sized>(&self, output: &mut T) {
match self {
Era::Immortal => output.push_byte(0),
Era::Mortal(period, phase) => {
Self::Immortal => output.push_byte(0),
Self::Mortal(period, phase) => {
let quantize_factor = (*period as u64 >> 12).max(1);
let encoded = (period.trailing_zeros() - 1).max(1).min(15) as u16 | ((phase / quantize_factor) << 4) as u16;
encoded.encode_to(output);
@@ -125,14 +122,14 @@ impl Decode for Era {
fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
let first = input.read_byte()?;
if first == 0 {
Ok(Era::Immortal)
Ok(Self::Immortal)
} else {
let encoded = first as u64 + ((input.read_byte()? as u64) << 8);
let period = 2 << (encoded % (1 << 4));
let quantize_factor = (period >> 12).max(1);
let phase = (encoded >> 4) * quantize_factor;
if period >= 4 && phase < period {
Ok(Era::Mortal(period, phase))
Ok(Self::Mortal(period, phase))
} else {
Err("Invalid period and phase".into())
}
@@ -91,7 +91,7 @@ impl<Number, Hash> Decode for Header<Number, Hash> where
Hash::Output: Decode,
{
fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
Ok(Header {
Ok(Self {
parent_hash: Decode::decode(input)?,
number: <<Number as HasCompact>::Type>::decode(input)?.into(),
state_root: Decode::decode(input)?,
@@ -160,7 +160,7 @@ impl<Number, Hash> traits::Header for Header<Number, Hash> where
parent_hash: Self::Hash,
digest: Digest<Self::Hash>,
) -> Self {
Header {
Self {
number,
extrinsics_root,
state_root,
@@ -70,7 +70,7 @@ impl<Address, Call, Signature, Extra: SignedExtension>
signature: Signature,
extra: Extra
) -> Self {
UncheckedExtrinsic {
Self {
signature: Some((signed, signature, extra)),
function,
}
@@ -78,7 +78,7 @@ impl<Address, Call, Signature, Extra: SignedExtension>
/// New instance of an unsigned extrinsic aka "inherent".
pub fn new_unsigned(function: Call) -> Self {
UncheckedExtrinsic {
Self {
signature: None,
function,
}
@@ -102,9 +102,9 @@ impl<Address, Call, Signature, Extra: SignedExtension> Extrinsic
fn new(function: Call, signed_data: Option<Self::SignaturePayload>) -> Option<Self> {
Some(if let Some((address, signature, extra)) = signed_data {
UncheckedExtrinsic::new_signed(function, address, signature, extra)
Self::new_signed(function, address, signature, extra)
} else {
UncheckedExtrinsic::new_unsigned(function)
Self::new_unsigned(function)
})
}
}
@@ -238,7 +238,7 @@ where
return Err("Invalid transaction version".into());
}
Ok(UncheckedExtrinsic {
Ok(Self {
signature: if is_signed { Some(Decode::decode(input)?) } else { None },
function: Decode::decode(input)?,
})
@@ -327,7 +327,7 @@ where
Extra: SignedExtension,
{
fn from(extrinsic: UncheckedExtrinsic<Address, Call, Signature, Extra>) -> Self {
OpaqueExtrinsic::from_bytes(extrinsic.encode().as_slice())
Self::from_bytes(extrinsic.encode().as_slice())
.expect(
"both OpaqueExtrinsic and UncheckedExtrinsic have encoding that is compatible with \
raw Vec<u8> encoding; qed"