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
@@ -19,7 +19,7 @@
use fork_tree::ForkTree;
use parking_lot::RwLock;
use grandpa::voter_set::VoterSet;
use parity_codec::{Encode, Decode};
use codec::{Encode, Decode};
use log::{debug, info};
use substrate_telemetry::{telemetry, CONSENSUS_INFO};
use fg_primitives::AuthorityId;
@@ -403,7 +403,7 @@ pub(crate) struct PendingChange<H, N> {
}
impl<H: Decode, N: Decode> Decode for PendingChange<H, N> {
fn decode<I: parity_codec::Input>(value: &mut I) -> Option<Self> {
fn decode<I: codec::Input>(value: &mut I) -> Result<Self, codec::Error> {
let next_authorities = Decode::decode(value)?;
let delay = Decode::decode(value)?;
let canon_height = Decode::decode(value)?;
@@ -411,7 +411,7 @@ impl<H: Decode, N: Decode> Decode for PendingChange<H, N> {
let delay_kind = DelayKind::decode(value).unwrap_or(DelayKind::Finalized);
Some(PendingChange {
Ok(PendingChange {
next_authorities,
delay,
canon_height,
@@ -18,7 +18,7 @@
use std::fmt::Debug;
use std::sync::Arc;
use parity_codec::{Encode, Decode};
use codec::{Encode, Decode};
use client::backend::AuxStore;
use client::error::{Result as ClientResult, Error as ClientError};
use fork_tree::ForkTree;
@@ -108,8 +108,8 @@ pub(crate) fn load_decode<B: AuxStore, T: Decode>(backend: &B, key: &[u8]) -> Cl
match backend.get_aux(key)? {
None => Ok(None),
Some(t) => T::decode(&mut &t[..])
.ok_or_else(
|| ClientError::Backend(format!("GRANDPA DB is corrupted.")),
.map_err(
|e| ClientError::Backend(format!("GRANDPA DB is corrupted: {}", e.what())),
)
.map(Some)
}
@@ -85,7 +85,7 @@
use sr_primitives::traits::{NumberFor, Block as BlockT, Zero};
use network::consensus_gossip::{self as network_gossip, MessageIntent, ValidatorContext};
use network::{config::Roles, PeerId};
use parity_codec::{Encode, Decode};
use codec::{Encode, Decode};
use crate::ed25519::Public as AuthorityId;
use substrate_telemetry::{telemetry, CONSENSUS_DEBUG};
@@ -977,10 +977,10 @@ impl<Block: BlockT> GossipValidator<Block> {
let action = {
match GossipMessage::<Block>::decode(&mut data) {
Some(GossipMessage::VoteOrPrecommit(ref message))
Ok(GossipMessage::VoteOrPrecommit(ref message))
=> self.inner.write().validate_round_message(who, message),
Some(GossipMessage::Commit(ref message)) => self.inner.write().validate_commit_message(who, message),
Some(GossipMessage::Neighbor(update)) => {
Ok(GossipMessage::Commit(ref message)) => self.inner.write().validate_commit_message(who, message),
Ok(GossipMessage::Neighbor(update)) => {
let (topics, action, catch_up, report) = self.inner.write().import_neighbor_message(
who,
update.into_neighbor_packet(),
@@ -994,9 +994,9 @@ impl<Block: BlockT> GossipValidator<Block> {
peer_reply = catch_up;
action
}
Some(GossipMessage::CatchUp(ref message))
Ok(GossipMessage::CatchUp(ref message))
=> self.inner.write().validate_catch_up_message(who, message),
Some(GossipMessage::CatchUpRequest(request)) => {
Ok(GossipMessage::CatchUpRequest(request)) => {
let (reply, action) = self.inner.write().handle_catch_up_request(
who,
request,
@@ -1006,8 +1006,8 @@ impl<Block: BlockT> GossipValidator<Block> {
peer_reply = reply;
action
}
None => {
debug!(target: "afg", "Error decoding message");
Err(e) => {
debug!(target: "afg", "Error decoding message: {}", e.what());
telemetry!(CONSENSUS_DEBUG; "afg.err_decoding_msg"; "" => "");
let len = std::cmp::min(i32::max_value() as usize, data.len()) as i32;
@@ -1127,17 +1127,17 @@ impl<Block: BlockT> network_gossip::Validator<Block> for GossipValidator<Block>
let peer_best_commit = peer.view.last_commit;
match GossipMessage::<Block>::decode(&mut data) {
None => false,
Some(GossipMessage::Commit(full)) => {
Err(_) => false,
Ok(GossipMessage::Commit(full)) => {
// we only broadcast our best commit and only if it's
// better than last received by peer.
Some(full.message.target_number) == our_best_commit
&& Some(full.message.target_number) > peer_best_commit
}
Some(GossipMessage::Neighbor(_)) => false,
Some(GossipMessage::CatchUpRequest(_)) => false,
Some(GossipMessage::CatchUp(_)) => false,
Some(GossipMessage::VoteOrPrecommit(_)) => false, // should not be the case.
Ok(GossipMessage::Neighbor(_)) => false,
Ok(GossipMessage::CatchUpRequest(_)) => false,
Ok(GossipMessage::CatchUp(_)) => false,
Ok(GossipMessage::VoteOrPrecommit(_)) => false, // should not be the case.
}
})
}
@@ -1162,10 +1162,10 @@ impl<Block: BlockT> network_gossip::Validator<Block> for GossipValidator<Block>
let best_commit = local_view.last_commit;
match GossipMessage::<Block>::decode(&mut data) {
None => true,
Some(GossipMessage::Commit(full))
Err(_) => true,
Ok(GossipMessage::Commit(full))
=> Some(full.message.target_number) != best_commit,
Some(_) => true,
Ok(_) => true,
}
})
}
@@ -35,7 +35,7 @@ use futures::prelude::*;
use futures::sync::{oneshot, mpsc};
use log::{debug, trace};
use tokio_executor::Executor;
use parity_codec::{Encode, Decode};
use codec::{Encode, Decode};
use primitives::{ed25519, Pair};
use substrate_telemetry::{telemetry, CONSENSUS_DEBUG, CONSENSUS_INFO};
use sr_primitives::traits::{Block as BlockT, Hash as HashT, Header as HeaderT};
@@ -367,10 +367,10 @@ impl<B: BlockT, N: Network<B>> NetworkBridge<B, N> {
let incoming = self.service.messages_for(topic)
.filter_map(|notification| {
let decoded = GossipMessage::<B>::decode(&mut &notification.message[..]);
if decoded.is_none() {
debug!(target: "afg", "Skipping malformed message {:?}", notification);
if let Err(ref e) = decoded {
debug!(target: "afg", "Skipping malformed message {:?}: {}", notification, e);
}
decoded
decoded.ok()
})
.and_then(move |msg| {
match msg {
@@ -583,10 +583,10 @@ fn incoming_global<B: BlockT, N: Network<B>>(
.filter_map(|notification| {
// this could be optimized by decoding piecewise.
let decoded = GossipMessage::<B>::decode(&mut &notification.message[..]);
if decoded.is_none() {
trace!(target: "afg", "Skipping malformed commit message {:?}", notification);
if let Err(ref e) = decoded {
trace!(target: "afg", "Skipping malformed commit message {:?}: {}", notification, e);
}
decoded.map(move |d| (notification, d))
decoded.map(move |d| (notification, d)).ok()
})
.filter_map(move |(notification, msg)| {
match msg {
@@ -23,7 +23,7 @@ use sr_primitives::traits::{NumberFor, Block as BlockT};
use network::PeerId;
use tokio_timer::Delay;
use log::warn;
use parity_codec::Encode;
use codec::Encode;
use std::time::{Instant, Duration};
@@ -24,7 +24,7 @@ use network_gossip::Validator;
use tokio::runtime::current_thread;
use std::sync::Arc;
use keyring::Ed25519Keyring;
use parity_codec::Encode;
use codec::Encode;
use crate::environment::SharedVoterSetState;
use super::gossip::{self, GossipValidator};
@@ -15,7 +15,7 @@
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
use std::sync::Arc;
use parity_codec::{Encode, Decode};
use codec::{Encode, Decode};
/// Consensus-related data changes tracker.
#[derive(Clone, Debug, Encode, Decode)]
@@ -20,7 +20,7 @@ use std::sync::Arc;
use std::time::{Duration, Instant};
use log::{debug, warn, info};
use parity_codec::{Decode, Encode};
use codec::{Decode, Encode};
use futures::prelude::*;
use tokio_timer::Delay;
use parking_lot::RwLock;
@@ -95,8 +95,10 @@ impl<Block: BlockT> Encode for CompletedRounds<Block> {
}
}
impl<Block: BlockT> codec::EncodeLike for CompletedRounds<Block> {}
impl<Block: BlockT> Decode for CompletedRounds<Block> {
fn decode<I: parity_codec::Input>(value: &mut I) -> Option<Self> {
fn decode<I: codec::Input>(value: &mut I) -> Result<Self, codec::Error> {
<(Vec<CompletedRound<Block>>, u64, Vec<AuthorityId>)>::decode(value)
.map(|(rounds, set_id, voters)| CompletedRounds {
rounds: rounds.into(),
@@ -43,7 +43,7 @@ use client::{
light::fetcher::{FetchChecker, RemoteCallRequest},
ExecutionStrategy, NeverOffchainExt,
};
use parity_codec::{Encode, Decode};
use codec::{Encode, Decode};
use grandpa::BlockNumberOps;
use sr_primitives::{Justification, generic::BlockId};
use sr_primitives::traits::{
@@ -81,8 +81,8 @@ impl<B, E, Block: BlockT<Hash=H256>, RA> AuthoritySetForFinalityProver<Block> fo
ExecutionStrategy::NativeElseWasm,
NeverOffchainExt::new(),
).and_then(|call_result| Decode::decode(&mut &call_result[..])
.ok_or_else(|| ClientError::CallResultDecode(
"failed to decode GRANDPA authorities set proof".into(),
.map_err(|err| ClientError::CallResultDecode(
"failed to decode GRANDPA authorities set proof".into(), err
)))
}
@@ -121,8 +121,8 @@ impl<Block: BlockT> AuthoritySetForFinalityChecker<Block> for Arc<dyn FetchCheck
self.check_execution_proof(&request, proof)
.and_then(|authorities| {
let authorities: Vec<(AuthorityId, u64)> = Decode::decode(&mut &authorities[..])
.ok_or_else(|| ClientError::CallResultDecode(
"failed to decode GRANDPA authorities set proof".into(),
.map_err(|err| ClientError::CallResultDecode(
"failed to decode GRANDPA authorities set proof".into(), err
))?;
Ok(authorities.into_iter().collect())
})
@@ -167,8 +167,8 @@ impl<B, E, Block, RA> network::FinalityProofProvider<Block> for FinalityProofPro
request: &[u8],
) -> Result<Option<Vec<u8>>, ClientError> {
let request: FinalityProofRequest<Block::Hash> = Decode::decode(&mut &request[..])
.ok_or_else(|| {
warn!(target: "finality", "Unable to decode finality proof request.");
.map_err(|e| {
warn!(target: "finality", "Unable to decode finality proof request: {}", e.what());
ClientError::Backend(format!("Invalid finality proof request"))
})?;
match request {
@@ -445,7 +445,7 @@ fn do_check_finality_proof<Block: BlockT<Hash=H256>, B, J>(
{
// decode finality proof
let proof = FinalityProof::<Block::Header>::decode(&mut &remote_proof[..])
.ok_or_else(|| ClientError::BadJustification("failed to decode finality proof".into()))?;
.map_err(|_| ClientError::BadJustification("failed to decode finality proof".into()))?;
// empty proof can't prove anything
if proof.is_empty() {
@@ -500,7 +500,7 @@ fn check_finality_proof_fragment<Block: BlockT<Hash=H256>, B, J>(
// verify justification using previous authorities set
let (mut current_set_id, mut current_authorities) = authority_set.extract_authorities();
let justification: J = Decode::decode(&mut &proof_fragment.justification[..])
.ok_or_else(|| ClientError::JustificationDecode)?;
.map_err(|_| ClientError::JustificationDecode)?;
justification.verify(current_set_id, &current_authorities)?;
// and now verify new authorities proof (if provided)
@@ -560,7 +560,8 @@ pub(crate) trait ProvableJustification<Header: HeaderT>: Encode + Decode {
set_id: u64,
authorities: &[(AuthorityId, u64)],
) -> ClientResult<Self> {
let justification = Self::decode(&mut &**justification).ok_or(ClientError::JustificationDecode)?;
let justification = Self::decode(&mut &**justification)
.map_err(|_| ClientError::JustificationDecode)?;
justification.verify(set_id, authorities)?;
Ok(justification)
}
@@ -17,7 +17,7 @@
use std::{sync::Arc, collections::HashMap};
use log::{debug, trace, info};
use parity_codec::Encode;
use codec::Encode;
use futures::sync::mpsc;
use parking_lot::RwLockWriteGuard;
@@ -20,7 +20,7 @@ use client::{CallExecutor, Client};
use client::backend::Backend;
use client::blockchain::HeaderBackend;
use client::error::Error as ClientError;
use parity_codec::{Encode, Decode};
use codec::{Encode, Decode};
use grandpa::voter_set::VoterSet;
use grandpa::{Error as GrandpaError};
use sr_primitives::generic::BlockId;
@@ -104,7 +104,7 @@ impl<Block: BlockT<Hash=H256>> GrandpaJustification<Block> {
{
let justification = GrandpaJustification::<Block>::decode(&mut &*encoded)
.ok_or(ClientError::JustificationDecode)?;
.map_err(|_| ClientError::JustificationDecode)?;
if (justification.commit.target_hash, justification.commit.target_number) != finalized_target {
let msg = "invalid commit target in grandpa justification".to_string();
+1 -1
View File
@@ -57,7 +57,7 @@ use log::{debug, info, warn};
use futures::sync::mpsc;
use client::{BlockchainEvents, CallExecutor, Client, backend::Backend, error::Error as ClientError};
use client::blockchain::HeaderBackend;
use parity_codec::Encode;
use codec::Encode;
use sr_primitives::traits::{
NumberFor, Block as BlockT, DigestFor, ProvideRuntimeApi,
};
@@ -25,7 +25,7 @@ use client::{
blockchain::HeaderBackend,
error::Error as ClientError,
};
use parity_codec::{Encode, Decode};
use codec::{Encode, Decode};
use consensus_common::{
import_queue::Verifier, well_known_cache_keys,
BlockOrigin, BlockImport, FinalityProofImport, BlockImportParams, ImportResult, ImportedAux,
+2 -2
View File
@@ -34,7 +34,7 @@ use consensus_common::{BlockOrigin, ForkChoiceStrategy, ImportedAux, BlockImport
use consensus_common::import_queue::{BoxBlockImport, BoxJustificationImport, BoxFinalityProofImport};
use std::collections::{HashMap, HashSet};
use std::result;
use parity_codec::Decode;
use codec::Decode;
use sr_primitives::traits::{ApiRef, ProvideRuntimeApi, Header as HeaderT};
use sr_primitives::generic::BlockId;
use primitives::{NativeOrEncoded, ExecutionContext};
@@ -336,7 +336,7 @@ impl AuthoritySetForFinalityChecker<Block> for TestApi {
proof: Vec<Vec<u8>>,
) -> Result<Vec<(AuthorityId, u64)>> {
Decode::decode(&mut &proof[0][..])
.ok_or_else(|| unreachable!("incorrect value is passed as GRANDPA authorities proof"))
.map_err(|_| unreachable!("incorrect value is passed as GRANDPA authorities proof"))
}
}