Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Add error type (#99)
Browse files Browse the repository at this point in the history
* Add error type

* Address review
  • Loading branch information
adoerr authored Mar 5, 2021
1 parent 314fa20 commit 4e2f63a
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 14 deletions.
2 changes: 2 additions & 0 deletions client/beefy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ license = "GPL-3.0-or-later WITH Classpath-exception-2.0"

[dependencies]
futures = "0.3"
hex = "0.4"
log = "0.4"
parking_lot = "0.11"
thiserror = "1.0"

codec = { version = "2.0.0", package = "parity-scale-codec", features = ["derive"] }

Expand Down
32 changes: 32 additions & 0 deletions client/beefy/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (C) 2020-2021 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use std::fmt::Debug;

use sp_core::crypto::Public;

/// BEEFY gadget specific errors
/// Note that this type is currently used for BEEFY gadget internal
/// error handling only.
#[derive(Debug, thiserror::Error)]
pub(crate) enum Error<Id: Public + Debug> {
/// Check signature error
#[error("Message signature {0} by {1:?} is invalid.")]
InvalidSignature(String, Id),
/// Sign commitment error
#[error("Failed to sign comitment using key: {0:?}. Reason: {1}")]
CannotSign(Id, String),
}
35 changes: 21 additions & 14 deletions client/beefy/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (C) 2020 Parity Technologies (UK) Ltd.
// Copyright (C) 2020-2021 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
Expand All @@ -19,10 +19,13 @@ use std::convert::{TryFrom, TryInto};
use std::fmt::Debug;
use std::sync::Arc;

use codec::{Codec, Decode, Encode};
use futures::{future, FutureExt, Stream, StreamExt};
use log::{debug, error, info, trace, warn};
use parking_lot::Mutex;
use {
codec::{Codec, Decode, Encode},
futures::{future, FutureExt, Stream, StreamExt},
hex::ToHex,
log::{debug, error, info, trace, warn},
parking_lot::Mutex,
};

use beefy_primitives::{
BeefyApi, Commitment, ConsensusLog, MmrRootHash, SignedCommitment, ValidatorSet, ValidatorSetId, BEEFY_ENGINE_ID,
Expand All @@ -44,6 +47,8 @@ use sp_runtime::{
traits::{Block as BlockT, Hash as HashT, Header as HeaderT, NumberFor, Zero},
};

mod error;

pub mod notification;

use notification::BeefySignedCommitmentSender;
Expand Down Expand Up @@ -264,15 +269,17 @@ where
number == next_block_to_vote_on
}

fn sign_commitment(&self, id: &Id, commitment: &[u8]) -> Option<Signature> {
fn sign_commitment(&self, id: &Id, commitment: &[u8]) -> Result<Signature, error::Error<Id>> {
let sig = SyncCryptoStore::sign_with(&*self.key_store, KEY_TYPE, &id.to_public_crypto_pair(), &commitment)
.ok()
.flatten()?
.map_err(|e| error::Error::CannotSign((*id).clone(), e.to_string()))?
.ok_or_else(|| error::Error::CannotSign((*id).clone(), "No key in KeyStore found".into()))?;

let sig = sig
.clone()
.try_into()
.ok()?;
.map_err(|_| error::Error::InvalidSignature(sig.encode_hex(), (*id).clone()))?;

// TODO #98 - return errors as well
Some(sig)
Ok(sig)
}

fn handle_finality_notification(&mut self, notification: FinalityNotification<Block>) {
Expand Down Expand Up @@ -312,9 +319,9 @@ where
};

let signature = match self.sign_commitment(local_id, commitment.encode().as_ref()) {
Some(sig) => sig,
None => {
warn!(target: "beefy", "🥩 Error signing commitment: {:?}", commitment);
Ok(sig) => sig,
Err(err) => {
warn!(target: "beefy", "🥩 Error signing commitment: {:?}", err);
return;
}
};
Expand Down

0 comments on commit 4e2f63a

Please sign in to comment.