Skip to content
This repository has been archived by the owner on Jun 25, 2021. It is now read-only.

Commit

Permalink
feat(errors): additional error types
Browse files Browse the repository at this point in the history
  • Loading branch information
bochaco committed Dec 28, 2020
1 parent b1b3bf1 commit ad71e64
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 18 deletions.
18 changes: 7 additions & 11 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub enum Error {
AccessDenied(PublicKey),
/// Serialization error
#[error("Serialisation error: {0}")]
Bincode(String),
Serialisation(String),
/// Requested data not found
#[error("Requested data not found")]
NoSuchData,
Expand Down Expand Up @@ -105,11 +105,10 @@ pub enum Error {
/// While parsing, precision would be lost.
#[error("Lost precision on the number of coins during parsing")]
LossOfPrecision,
/// The coin amount would exceed
/// [the maximum value for `Coins`](constant.MAX_COINS_VALUE.html).
#[error("Overflow on number of coins (check the MAX_COINS_VALUE const)")]
/// The amount would exceed the maximum value for `Money` (u64::MAX).
#[error("The money amount would exceed the maximum value (u64::MAX)")]
ExcessiveValue,
/// Failed to parse the string as [`Coins`](struct.Coins.html).
/// Failed to parse a string.
#[error("Failed to parse: {0}")]
FailedToParse(String),
/// Transaction ID already exists.
Expand All @@ -135,19 +134,16 @@ pub enum Error {
/// Expected data size exceeded.
#[error("Size of the structure exceeds the limit")]
ExceededSize,
/// Could not be serlialised
#[error("Could not deserialize as ed25519 secret key")]
Ed25519SecretKey,
/// The operation has not been signed by an actor PK and so cannot be validated.
#[error("CRDT operation missing actor signature")]
CrdtMissingOpSignature,
/// The data for a given policy could not be located, so CRDT operations cannot be applied
#[error("CRDT data is in an unexpected state. No data ffound for requested policy.")]
/// The data for a given policy could not be located, so CRDT operations cannot be applied.
#[error("CRDT data is in an unexpected and/or inconsistent state. No data found for current policy.")]
CrdtUnexpectedState,
}

pub(crate) fn convert_bincode_error(err: bincode::Error) -> Error {
Error::Bincode(err.as_ref().to_string())
Error::Serialisation(err.as_ref().to_string())
}

/// Entry error for `Error::InvalidEntryActions`.
Expand Down
4 changes: 3 additions & 1 deletion src/keys/keypair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ impl Keypair {
let bytes = keypair.secret.to_bytes();
match ed25519_dalek::SecretKey::from_bytes(&bytes) {
Ok(sk) => Ok(SecretKey::Ed25519(sk)),
Err(_) => Err(Error::Ed25519SecretKey),
Err(_) => Err(Error::FailedToParse(
"Could not deserialise Ed25519 secret key".to_string(),
)),
}
}
Self::Bls(keypair) => Ok(SecretKey::Bls(keypair.secret.clone())),
Expand Down
8 changes: 6 additions & 2 deletions src/sequence/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -450,8 +450,12 @@ mod tests {
mut op: SequencePolicyWriteOp<SequencePrivatePolicy>,
keypair: &Keypair,
) -> Result<SequencePolicyWriteOp<SequencePrivatePolicy>> {
let bytes = utils::serialise(&op.crdt_op)
.map_err(|_| Error::Bincode("Could not serialise".to_string()))?;
let bytes = utils::serialise(&op.crdt_op).map_err(|err| {
Error::Serialisation(format!(
"Could not serialise CRDT policy write operation to generate signature: {}",
err
))
})?;
let signature = keypair.sign(&bytes);
op.signature = Some(signature);
Ok(op)
Expand Down
16 changes: 12 additions & 4 deletions src/sequence/seq_crdt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,12 @@ where
// First check op is validly signed.
// Note: Perms for the op are checked at the upper Sequence layer.
let sig = op.signature.ok_or(Error::CrdtMissingOpSignature)?;
let bytes_to_verify = utils::serialise(&op.crdt_op)
.map_err(|_| Error::Bincode("Could not serialize crdt op".to_string()))?;
let bytes_to_verify = utils::serialise(&op.crdt_op).map_err(|err| {
Error::Serialisation(format!(
"Could not serialise CRDT data operation to verify signature: {}",
err
))
})?;
op.source.verify(&sig, &bytes_to_verify)?;

let policy_id = op.ctx.clone();
Expand Down Expand Up @@ -278,8 +282,12 @@ where
// First check op is validly signed.
// Note: Perms for the op are checked at the upper Sequence layer.
let sig = op.signature.ok_or(Error::CrdtMissingOpSignature)?;
let bytes_to_verify = utils::serialise(&op.crdt_op)
.map_err(|_| Error::Bincode("Could not serialise CRDT operation".to_string()))?;
let bytes_to_verify = utils::serialise(&op.crdt_op).map_err(|err| {
Error::Serialisation(format!(
"Could not serialise CRDT data operation to verify signature: {}",
err
))
})?;
op.source.verify(&sig, &bytes_to_verify)?;

let new_lseq = if let Some((policy_id, item_id)) = op.ctx {
Expand Down

0 comments on commit ad71e64

Please sign in to comment.