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
+1 -1
View File
@@ -6,7 +6,7 @@ description = "Aura consensus algorithm for substrate"
edition = "2018"
[dependencies]
parity-codec = "4.1.1"
codec = { package = "parity-scale-codec", version = "1.0.0" }
primitives = { package = "substrate-primitives", path = "../../primitives" }
runtime_support = { package = "srml-support", path = "../../../srml/support" }
runtime_version = { package = "sr-version", path = "../../sr-version" }
@@ -6,7 +6,7 @@ description = "Primitives for Aura consensus"
edition = "2018"
[dependencies]
parity-codec = { version = "4.1.1", default-features = false }
codec = { package = "parity-scale-codec", version = "1.0.0", default-features = false }
substrate-client = { path = "../../../client", default-features = false }
primitives = { package = "substrate-primitives", path = "../../../primitives", default-features = false }
rstd = { package = "sr-std", path = "../../../sr-std", default-features = false }
@@ -16,7 +16,7 @@ sr-primitives = { path = "../../../sr-primitives", default-features = false }
default = ["std"]
std = [
"rstd/std",
"parity-codec/std",
"codec/std",
"sr-primitives/std",
"substrate-client/std",
"primitives/std",
@@ -18,7 +18,7 @@
#![cfg_attr(not(feature = "std"), no_std)]
use parity_codec::{Encode, Decode, Codec};
use codec::{Encode, Decode, Codec};
use substrate_client::decl_runtime_apis;
use rstd::vec::Vec;
use sr_primitives::ConsensusEngineId;
+1 -1
View File
@@ -22,7 +22,7 @@
use primitives::Pair;
use aura_primitives::AURA_ENGINE_ID;
use sr_primitives::generic::{DigestItem, OpaqueDigestItemId};
use parity_codec::{Encode, Codec};
use codec::{Encode, Codec};
use std::fmt::Debug;
type Signature<P> = <P as Pair>::Signature;
+3 -3
View File
@@ -30,7 +30,7 @@
#![forbid(missing_docs, unsafe_code)]
use std::{sync::Arc, time::Duration, thread, marker::PhantomData, hash::Hash, fmt::Debug, pin::Pin};
use parity_codec::{Encode, Decode, Codec};
use codec::{Encode, Decode, Codec};
use consensus_common::{self, BlockImport, Environment, Proposer,
ForkChoiceStrategy, BlockImportParams, BlockOrigin, Error as ConsensusError,
SelectChain, well_known_cache_keys::{self, Id as CacheKeyId}
@@ -623,7 +623,7 @@ fn initialize_authorities_cache<A, B, C>(client: &C) -> Result<(), ConsensusErro
let genesis_id = BlockId::Number(Zero::zero());
let genesis_authorities: Option<Vec<A>> = cache
.get_at(&well_known_cache_keys::AUTHORITIES, &genesis_id)
.and_then(|v| Decode::decode(&mut &v[..]));
.and_then(|v| Decode::decode(&mut &v[..]).ok());
if genesis_authorities.is_some() {
return Ok(());
}
@@ -651,7 +651,7 @@ fn authorities<A, B, C>(client: &C, at: &BlockId<B>) -> Result<Vec<A>, Consensus
.cache()
.and_then(|cache| cache
.get_at(&well_known_cache_keys::AUTHORITIES, at)
.and_then(|v| Decode::decode(&mut &v[..]))
.and_then(|v| Decode::decode(&mut &v[..]).ok())
)
.or_else(|| AuraApi::authorities(&*client.runtime_api(), at).ok())
.ok_or_else(|| consensus_common::Error::InvalidAuthoritiesSet.into())
+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() {
+1 -1
View File
@@ -16,7 +16,7 @@ futures-timer = "0.2.1"
rstd = { package = "sr-std", path = "../../sr-std" }
runtime_version = { package = "sr-version", path = "../../sr-version" }
sr-primitives = { path = "../../sr-primitives" }
parity-codec = { version = "4.1.1", features = ["derive"] }
codec = { package = "parity-scale-codec", version = "1.0.0", features = ["derive"] }
parking_lot = "0.8.0"
[dev-dependencies]
@@ -6,7 +6,7 @@ description = "Common consensus primitives"
edition = "2018"
[dependencies]
parity-codec = { version = "4.1.1", default-features = false }
codec = { package = "parity-scale-codec", default-features = false, version = "1.0.3" }
client = { package = "substrate-client", path = "../../../client", default-features = false }
sr-primitives = { path = "../../../sr-primitives", default-features = false }
rstd = { package = "sr-std", path = "../../../sr-std", default-features = false }
@@ -16,6 +16,6 @@ default = ["std"]
std = [
"rstd/std",
"client/std",
"parity-codec/std",
"codec/std",
"sr-primitives/std"
]
@@ -18,7 +18,7 @@
#![cfg_attr(not(feature = "std"), no_std)]
use parity_codec::Codec;
use codec::Codec;
use client::decl_runtime_apis;
use rstd::vec::Vec;
@@ -18,7 +18,7 @@
use super::MAX_BLOCK_SIZE;
use parity_codec::Encode;
use codec::Encode;
use sr_primitives::traits::{Block as BlockT, Header as HeaderT, One, CheckedConversion};
// This is just a best effort to encode the number. None indicated that it's too big to encode
@@ -32,8 +32,8 @@ pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, derive_more::Display)]
pub enum Error {
/// Proposal provided not a block.
#[display(fmt="Proposal provided not a block.")]
BadProposalFormat,
#[display(fmt="Proposal provided not a block: decoding error: {}", _0)]
BadProposalFormat(codec::Error),
/// Proposal had wrong parent hash.
#[display(fmt="Proposal had wrong parent hash. Expected {:?}, got {:?}", expected, got)]
WrongParentHash { expected: String, got: String },
@@ -60,7 +60,7 @@ pub fn evaluate_initial<Block: BlockT>(
let encoded = Encode::encode(proposal);
let proposal = Block::decode(&mut &encoded[..])
.ok_or_else(|| Error::BadProposalFormat)?;
.map_err(|e| Error::BadProposalFormat(e))?;
if encoded.len() > MAX_BLOCK_SIZE {
return Err(Error::ProposalTooLarge(encoded.len()))
+2 -2
View File
@@ -8,7 +8,7 @@ edition = "2018"
[dependencies]
derive_more = "0.14.0"
futures = "0.1.17"
codec = { package = "parity-codec", version = "4.1.1", features = ["derive"] }
codec = { package = "parity-scale-codec", version = "1.0.0", features = ["derive"] }
primitives = { package = "substrate-primitives", path = "../../primitives" }
consensus = { package = "substrate-consensus-common", path = "../common" }
client = { package = "substrate-client", path = "../../client" }
@@ -21,7 +21,7 @@ runtime_io = { package = "sr-io", path = "../../sr-io" }
tokio = "0.1.7"
parking_lot = "0.8.0"
log = "0.4"
rhododendron = { version = "0.6.0", features = ["codec"] }
rhododendron = { version = "0.7.0", features = ["codec"] }
exit-future = "0.1"
[dev-dependencies]
+1 -1
View File
@@ -40,7 +40,7 @@ use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::{self, Instant, Duration};
use parity_codec::{Decode, Encode};
use codec::{Decode, Encode};
use consensus::offline_tracker::OfflineTracker;
use consensus::error::{ErrorKind as CommonErrorKind};
use consensus::{Authorities, BlockImport, Environment, Proposer as BaseProposer};
+1 -1
View File
@@ -6,7 +6,7 @@ description = "Generic slots-based utilities for consensus"
edition = "2018"
[dependencies]
codec = { package = "parity-codec", version = "4.1.1" }
codec = { package = "parity-scale-codec", version = "1.0.0" }
client = { package = "substrate-client", path = "../../client" }
primitives = { package = "substrate-primitives", path = "../../primitives" }
sr-primitives = { path = "../../sr-primitives" }
@@ -37,8 +37,8 @@ fn load_decode<C, T>(backend: &C, key: &[u8]) -> ClientResult<Option<T>>
match backend.get_aux(key)? {
None => Ok(None),
Some(t) => T::decode(&mut &t[..])
.ok_or_else(
|| ClientError::Backend(format!("Slots DB is corrupted.")).into(),
.map_err(
|e| ClientError::Backend(format!("Slots DB is corrupted. Decode error: {}", e.what())).into(),
)
.map(Some)
}
+1 -1
View File
@@ -188,7 +188,7 @@ impl<T: Clone> SlotDuration<T> {
match client.get_aux(T::SLOT_KEY)? {
Some(v) => <T as codec::Decode>::decode(&mut &v[..])
.map(SlotDuration)
.ok_or_else(|| {
.map_err(|_| {
::client::error::Error::Backend({
error!(target: "slots", "slot duration kept in invalid format");
format!("slot duration kept in invalid format")