Update to parity-scale-codec (#3232)

* WIP: update codec

* WIP

* compiling

* WIP

* rename parity-scale-codec to codec

* WIP

* fix

* remove old comments

* use published crates

* fix expected error msg

* bump version

* fmt and fix

* remove old comment

* fix wrong decoding impl

* implement encode like for structures

* undo removal of old pending changes

* trailingzeroinput

* Apply suggestions from code review

Co-Authored-By: Bastian Köcher <bkchr@users.noreply.github.com>
Co-Authored-By: DemiMarie-parity <48690212+DemiMarie-parity@users.noreply.github.com>

* update codec

* fmt

* version is 1.0.0

* show more error

* fmt
This commit is contained in:
thiolliere
2019-08-06 19:36:23 +02:00
committed by Bastian Köcher
parent a0d442333f
commit 4ed67e03a4
211 changed files with 867 additions and 682 deletions
+2 -2
View File
@@ -6,8 +6,8 @@ description = "BABE consensus algorithm for substrate"
edition = "2018"
[dependencies]
parity-codec = { version = "4.1.1", features = ["derive"] }
babe-primitives = { package = "substrate-consensus-babe-primitives", path = "primitives" }
codec = { package = "parity-scale-codec", version = "1.0.0", features = ["derive"] }
babe_primitives = { package = "substrate-consensus-babe-primitives", path = "primitives" }
primitives = { package = "substrate-primitives", path = "../../primitives" }
num-bigint = "0.2"
num-rational = "0.2"
@@ -11,7 +11,7 @@ rstd = { package = "sr-std", path = "../../../sr-std", default-features = false
sr-primitives = { path = "../../../sr-primitives", default-features = false }
primitives = { package = "substrate-primitives", path = "../../../primitives", default-features = false }
slots = { package = "substrate-consensus-slots", path = "../../slots", optional = true }
parity-codec = { version = "4.1.1", default-features = false }
codec = { package = "parity-scale-codec", version = "1.0.0", default-features = false }
schnorrkel = { version = "0.1.1", optional = true }
[features]
@@ -20,7 +20,7 @@ std = [
"rstd/std",
"sr-primitives/std",
"substrate-client/std",
"parity-codec/std",
"codec/std",
"schnorrkel",
"slots",
]
@@ -27,11 +27,14 @@ use super::SlotNumber;
use sr_primitives::{DigestItem, generic::OpaqueDigestItemId};
#[cfg(feature = "std")]
use std::fmt::Debug;
use parity_codec::{Decode, Encode};
use codec::{Decode, Encode};
#[cfg(feature = "std")]
use parity_codec::{Codec, Input};
use codec::{Codec, Input, Error};
#[cfg(feature = "std")]
use schnorrkel::vrf::{VRFProof, VRFOutput, VRF_OUTPUT_LENGTH, VRF_PROOF_LENGTH};
use schnorrkel::{
SignatureError, errors::MultiSignatureStage,
vrf::{VRFProof, VRFOutput, VRF_OUTPUT_LENGTH, VRF_PROOF_LENGTH}
};
/// A BABE pre-digest
#[cfg(feature = "std")]
@@ -72,21 +75,26 @@ impl Encode for BabePreDigest {
authority_index: self.authority_index,
slot_number: self.slot_number,
};
parity_codec::Encode::encode(&tmp)
codec::Encode::encode(&tmp)
}
}
#[cfg(feature = "std")]
impl codec::EncodeLike for BabePreDigest {}
#[cfg(feature = "std")]
impl Decode for BabePreDigest {
fn decode<R: Input>(i: &mut R) -> Option<Self> {
fn decode<R: Input>(i: &mut R) -> Result<Self, Error> {
let RawBabePreDigest { vrf_output, vrf_proof, authority_index, slot_number } = Decode::decode(i)?;
// Verify (at compile time) that the sizes in babe_primitives are correct
let _: [u8; super::VRF_OUTPUT_LENGTH] = vrf_output;
let _: [u8; super::VRF_PROOF_LENGTH] = vrf_proof;
Some(BabePreDigest {
vrf_proof: VRFProof::from_bytes(&vrf_proof).ok()?,
vrf_output: VRFOutput::from_bytes(&vrf_output).ok()?,
Ok(BabePreDigest {
vrf_proof: VRFProof::from_bytes(&vrf_proof)
.map_err(convert_error)?,
vrf_output: VRFOutput::from_bytes(&vrf_output)
.map_err(convert_error)?,
authority_index,
slot_number,
})
@@ -136,3 +144,33 @@ impl<Hash> CompatibleDigestItem for DigestItem<Hash> where
self.try_to(OpaqueDigestItemId::Consensus(&BABE_ENGINE_ID))
}
}
#[cfg(feature = "std")]
fn convert_error(e: SignatureError) -> codec::Error {
use SignatureError::*;
use MultiSignatureStage::*;
match e {
EquationFalse => "Signature error: `EquationFalse`".into(),
PointDecompressionError => "Signature error: `PointDecompressionError`".into(),
ScalarFormatError => "Signature error: `ScalarFormatError`".into(),
BytesLengthError { .. } => "Signature error: `BytesLengthError`".into(),
MuSigAbsent { musig_stage: Commitment } =>
"Signature error: `MuSigAbsent` at stage `Commitment`".into(),
MuSigAbsent { musig_stage: Reveal } =>
"Signature error: `MuSigAbsent` at stage `Reveal`".into(),
MuSigAbsent { musig_stage: Cosignature } =>
"Signature error: `MuSigAbsent` at stage `Commitment`".into(),
MuSigInconsistent { musig_stage: Commitment, duplicate: true } =>
"Signature error: `MuSigInconsistent` at strage `Commitment` on duplicate".into(),
MuSigInconsistent { musig_stage: Commitment, duplicate: false } =>
"Signature error: `MuSigInconsistent` at strage `Commitment` on not duplicate".into(),
MuSigInconsistent { musig_stage: Reveal, duplicate: true } =>
"Signature error: `MuSigInconsistent` at strage `Reveal` on duplicate".into(),
MuSigInconsistent { musig_stage: Reveal, duplicate: false } =>
"Signature error: `MuSigInconsistent` at strage `Reveal` on not duplicate".into(),
MuSigInconsistent { musig_stage: Cosignature, duplicate: true } =>
"Signature error: `MuSigInconsistent` at strage `Cosignature` on duplicate".into(),
MuSigInconsistent { musig_stage: Cosignature, duplicate: false } =>
"Signature error: `MuSigInconsistent` at strage `Cosignature` on not duplicate".into(),
}
}
@@ -21,7 +21,7 @@
mod digest;
use parity_codec::{Encode, Decode};
use codec::{Encode, Decode};
use rstd::vec::Vec;
use sr_primitives::ConsensusEngineId;
use primitives::sr25519;
@@ -17,7 +17,7 @@
//! Schema for BABE epoch changes in the aux-db.
use log::info;
use parity_codec::{Decode, Encode};
use codec::{Decode, Encode};
use client::backend::AuxStore;
use client::error::{Result as ClientResult, Error as ClientError};
@@ -32,10 +32,12 @@ fn load_decode<B, T>(backend: &B, key: &[u8]) -> ClientResult<Option<T>>
B: AuxStore,
T: Decode,
{
let corrupt = || ClientError::Backend(format!("BABE DB is corrupted.")).into();
let corrupt = |e: codec::Error| {
ClientError::Backend(format!("BABE DB is corrupted. Decode error: {}", e.what())).into()
};
match backend.get_aux(key)? {
None => Ok(None),
Some(t) => T::decode(&mut &t[..]).ok_or_else(corrupt).map(Some)
Some(t) => T::decode(&mut &t[..]).map(Some).map_err(corrupt)
}
}
+3 -3
View File
@@ -33,7 +33,7 @@ use sr_primitives::traits::{
};
use std::{collections::HashMap, sync::Arc, u64, fmt::{Debug, Display}, pin::Pin, time::{Instant, Duration}};
use runtime_support::serde::{Serialize, Deserialize};
use parity_codec::{Decode, Encode};
use codec::{Decode, Encode};
use parking_lot::{Mutex, MutexGuard};
use primitives::{Blake2Hasher, H256, Pair, Public, sr25519};
use merlin::Transcript;
@@ -733,7 +733,7 @@ fn epoch<B, C>(client: &C, at: &BlockId<B>) -> Result<Epoch, ConsensusError> whe
client
.cache()
.and_then(|cache| cache.get_at(&well_known_cache_keys::EPOCH, at)
.and_then(|v| Decode::decode(&mut &v[..])))
.and_then(|v| Decode::decode(&mut &v[..]).ok()))
.or_else(|| {
if client.runtime_api().has_api::<dyn BabeApi<B>>(at).unwrap_or(false) {
let s = BabeApi::epoch(&*client.runtime_api(), at).ok()?;
@@ -856,7 +856,7 @@ fn initialize_authorities_cache<B, C>(client: &C) -> Result<(), ConsensusError>
let genesis_id = BlockId::Number(Zero::zero());
let genesis_epoch: Option<Epoch> = cache
.get_at(&well_known_cache_keys::EPOCH, &genesis_id)
.and_then(|v| Decode::decode(&mut &v[..]));
.and_then(|v| Decode::decode(&mut &v[..]).ok());
if genesis_epoch.is_some() {
return Ok(());
}
@@ -165,12 +165,6 @@ impl TestNetFactory for BabeTestNet {
}
}
#[test]
fn can_serialize_block() {
let _ = env_logger::try_init();
assert!(BabePreDigest::decode(&mut &b""[..]).is_none());
}
#[test]
#[should_panic]
fn rejects_empty_block() {