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
@@ -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;