diff --git a/CHANGELOG.md b/CHANGELOG.md index d7c4fe43..f53a6b52 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ All notable changes to this project will be documented in this file. +## Version 1.11.0 + +- Use modern crates anyhow and thiserror instead of failure + ## Version 1.10.4 - Fix build for WASM target diff --git a/Cargo.toml b/Cargo.toml index 8c61adb4..51a0d180 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,29 +2,30 @@ build = 'build.rs' edition = '2021' name = 'ever_block' -version = '1.10.4' +version = '1.11.0' [dependencies] aes-ctr = '0.6' +anyhow = '1.0' base64 = '0.13' blst = { features = [ 'portable' ], version = '0.3.5' } crc = '3.0' curve25519-dalek = '4.0' ed25519 = '2.2' ed25519-dalek = { features = [ 'hazmat', 'rand_core' ], version = '2.0' } -failure = '0.1' getrandom = { features = [ 'js' ], version = '0.2' } hex = '0.4' lazy_static = '1.4' log = '0.4' num = '0.4' -num-derive = '0.3' +num-derive = '0.4' num-traits = '0.2' rand = '0.8' serde = { features = [ 'derive', 'rc' ], version = '1.0.105' } serde_json = '1.0' sha2 = '0.10' smallvec = { features = [ 'const_new', 'union', 'write' ], version = '1.10' } +thiserror = '1.0' x25519-dalek = '2.0' lockfree = { git = 'https://github.com/everx-labs/lockfree.git' } @@ -39,6 +40,7 @@ export_key = [ ] gosh = [ ] groth = [ ] signature_with_id = [ ] +std = [ ] [[bench]] harness = false diff --git a/src/bls.rs b/src/bls.rs index cca5ba58..eb36545b 100644 --- a/src/bls.rs +++ b/src/bls.rs @@ -12,6 +12,7 @@ */ use crate::{fail, Result}; +use rand::{Rng, RngCore}; use std::collections::HashMap; /* @@ -423,7 +424,7 @@ impl BlsKeyPair { pub fn gen_key_pair() -> Result { let mut ikm = [0u8; BLS_KEY_MATERIAL_LEN]; - rand::RngCore::fill_bytes(&mut rand::thread_rng(), &mut ikm); + rand::thread_rng().fill_bytes(&mut ikm); BlsKeyPair::gen_key_pair_based_on_key_material(&ikm) } @@ -580,15 +581,15 @@ impl NodesInfo { */ pub fn generate_random_msg() -> Vec { - let msg_len = rand::Rng::gen_range(&mut rand::thread_rng(), 2..100); + let msg_len = rand::thread_rng().gen_range(2..100); let mut msg = vec![0u8; msg_len as usize]; - rand::RngCore::fill_bytes(&mut rand::thread_rng(), &mut msg); + rand::thread_rng().fill_bytes(&mut msg); msg } pub fn generate_random_msg_of_fixed_len( msg_len: i32) -> Vec { let mut msg = vec![0u8; msg_len as usize]; - rand::RngCore::fill_bytes(&mut rand::thread_rng(), &mut msg); + rand::thread_rng().fill_bytes(&mut msg); msg } @@ -599,7 +600,7 @@ pub fn gen_signer_indexes(n: u16, k: u16) -> Vec { let mut indexes = Vec::new(); for _i in 0..k { - indexes.push(rand::Rng::gen_range(&mut rng, 0..n)); + indexes.push(rng.gen_range(0..n)); } if indexes.len() == (k as usize) { @@ -609,8 +610,7 @@ pub fn gen_signer_indexes(n: u16, k: u16) -> Vec { } pub fn gen_random_index(n: u16) -> u16 { - let mut rng = rand::thread_rng(); - rand::Rng::gen_range(&mut rng, 0..n) + rand::thread_rng().gen_range(0..n) } pub fn create_random_nodes_info(total_num_of_nodes: u16, attempts: u16) -> NodesInfo{ diff --git a/src/cell/mod.rs b/src/cell/mod.rs index d3d90730..ad9308d7 100644 --- a/src/cell/mod.rs +++ b/src/cell/mod.rs @@ -880,7 +880,7 @@ fn build_big_cell_buf( level_mask: u8, refs: usize, store_hashes: bool, - hashes: Option<[UInt256; 4]>, + hashes: Option<&[UInt256; 4]>, depths: Option<[u16; 4]> ) -> Result> { if level_mask != 0 { @@ -911,7 +911,7 @@ fn build_cell_buf( level_mask: u8, refs: usize, store_hashes: bool, - hashes: Option<[UInt256; 4]>, + hashes: Option<&[UInt256; 4]>, depths: Option<[u16; 4]> ) -> Result> { if cell_type == CellType::Big { @@ -1105,9 +1105,9 @@ impl CellData { depths: Option<[u16; 4]> ) -> Result { let buffer = if cell_type == CellType::Big { - build_big_cell_buf(data, level_mask, refs as usize, store_hashes, hashes.clone(), depths)? + build_big_cell_buf(data, level_mask, refs as usize, store_hashes, hashes.as_ref(), depths)? } else { - build_cell_buf(cell_type, data, level_mask, refs as usize, store_hashes, hashes.clone(), depths)? + build_cell_buf(cell_type, data, level_mask, refs as usize, store_hashes, hashes.as_ref(), depths)? }; #[cfg(test)] check_cell_buf(&buffer[..], false)?; diff --git a/src/error.rs b/src/error.rs index edd8e1bc..2aad46bf 100644 --- a/src/error.rs +++ b/src/error.rs @@ -11,53 +11,53 @@ * limitations under the License. */ -#[derive(Debug, failure::Fail)] +#[derive(Debug, thiserror::Error)] pub enum BlockError { /// Fatal error. - #[fail(display = "Fatal error: {}", 0)] + #[error("Fatal error: {0}")] FatalError(String), /// Invalid argument. - #[fail(display = "Invalid argument: {}", 0)] + #[error("Invalid argument: {0}")] InvalidArg(String), /// Invalid TL-B constructor tag. - #[fail(display = "Invalid TL-B constructor tag `#{:x}` while parsing `{}` struct", t, s)] + #[error("Invalid TL-B constructor tag `#{:x}` while parsing `{}` struct", .t, .s)] InvalidConstructorTag { t: u32, s: String, }, /// Invalid data. - #[fail(display = "Invalid data: {}", 0)] + #[error("Invalid data: {0}")] InvalidData(String), /// Invalid index. - #[fail(display = "Invalid index: {}", 0)] + #[error("Invalid index: {0}")] InvalidIndex(usize), /// Invalid operation. - #[fail(display = "Invalid operation: {}", 0)] + #[error("Invalid operation: {0}")] InvalidOperation(String), /// Item is not found. - #[fail(display = "{} is not found", 0)] + #[error("{0} is not found")] NotFound(String), /// Other error. - #[fail(display = "{}", 0)] + #[error("{0}")] Other(String), /// Attempting to read data from pruned branch cell. - #[fail(display = "Attempting to read {} from pruned branch cell", 0)] + #[error("Attempting to read {0} from pruned branch cell")] PrunedCellAccess(String), /// Wrong hash. - #[fail(display = "Wrong hash")] + #[error("Wrong hash")] WrongHash, /// Wrong merkle proof. - #[fail(display = "Wrong merkle proof: {}", 0)] + #[error("Wrong merkle proof: {0}")] WrongMerkleProof(String), /// Wrong merkle update. - #[fail(display = "Wrong merkle update: {}", 0)] + #[error("Wrong merkle update: {0}")] WrongMerkleUpdate(String), - #[fail(display = "Bad signature")] + #[error("Bad signature")] BadSignature, - #[fail(display = "Unexpected struct variant: exp={} real={}", 0, 1)] + #[error("Unexpected struct variant: exp={0} real={1}")] UnexpectedStructVariant(String, String), - #[fail(display = "Unsupported serde opts: {} {:x}", 0, 1)] + #[error("Unsupported serde opts: {0} {:x}", .1)] UnsupportedSerdeOptions(String, usize), - #[fail(display = "Mismatched serde options: {} exp={} real={}", 0, 1, 2)] + #[error("Mismatched serde options: {0} exp={1} real={2}")] MismatchedSerdeOptions(String, usize, usize), } diff --git a/src/types.rs b/src/types.rs index 50ae5e57..0ee88172 100644 --- a/src/types.rs +++ b/src/types.rs @@ -31,7 +31,7 @@ use std::{ }; use smallvec::SmallVec; -pub type Error = failure::Error; +pub type Error = anyhow::Error; pub type Result = std::result::Result; pub type Failure = Option; pub type Status = Result<()>; @@ -39,20 +39,20 @@ pub type Status = Result<()>; #[macro_export] macro_rules! error { ($error:literal) => { - failure::err_msg(format!("{} {}:{}", $error, file!(), line!())) + anyhow::format_err!("{} {}:{}", $error, file!(), line!()) }; ($error:expr) => { - failure::Error::from($error) + anyhow::Error::from($error) }; ($fmt:expr, $($arg:tt)+) => { - failure::err_msg(format!("{} {}:{}", format!($fmt, $($arg)*), file!(), line!())) + anyhow::format_err!("{} {}:{}", format!($fmt, $($arg)*), file!(), line!()) }; } #[macro_export] macro_rules! fail { ($error:literal) => { - return Err(failure::err_msg(format!("{} {}:{}", $error, file!(), line!()))) + return Err(anyhow::format_err!("{} {}:{}", $error, file!(), line!())) }; // uncomment to explicit panic for any ExceptionCode // (ExceptionCode::CellUnderflow) => { @@ -62,7 +62,7 @@ macro_rules! fail { return Err($crate::error!($error)) }; ($fmt:expr, $($arg:tt)*) => { - return Err(failure::err_msg(format!("{} {}:{}", format!($fmt, $($arg)*), file!(), line!()))) + return Err(anyhow::format_err!("{} {}:{}", format!($fmt, $($arg)*), file!(), line!())) }; } @@ -305,41 +305,41 @@ impl FromStr for AccountId { // Exceptions ***************************************************************** -#[derive(Clone, Copy, Debug, num_derive::FromPrimitive, PartialEq, Eq, failure::Fail)] +#[derive(Clone, Copy, Debug, num_derive::FromPrimitive, PartialEq, Eq, thiserror::Error)] pub enum ExceptionCode { - #[fail(display = "normal termination")] + #[error("normal termination")] NormalTermination = 0, - #[fail(display = "alternative termination")] + #[error("alternative termination")] AlternativeTermination = 1, - #[fail(display = "stack underflow")] + #[error("stack underflow")] StackUnderflow = 2, - #[fail(display = "stack overflow")] + #[error("stack overflow")] StackOverflow = 3, - #[fail(display = "integer overflow")] + #[error("integer overflow")] IntegerOverflow = 4, - #[fail(display = "range check error")] + #[error("range check error")] RangeCheckError = 5, - #[fail(display = "invalid opcode")] + #[error("invalid opcode")] InvalidOpcode = 6, - #[fail(display = "type check error")] + #[error("type check error")] TypeCheckError = 7, - #[fail(display = "cell overflow")] + #[error("cell overflow")] CellOverflow = 8, - #[fail(display = "cell underflow")] + #[error("cell underflow")] CellUnderflow = 9, - #[fail(display = "dictionaty error")] + #[error("dictionaty error")] DictionaryError = 10, - #[fail(display = "unknown error")] + #[error("unknown error")] UnknownError = 11, - #[fail(display = "fatal error")] + #[error("fatal error")] FatalError = 12, - #[fail(display = "out of gas")] + #[error("out of gas")] OutOfGas = 13, - #[fail(display = "illegal instruction")] + #[error("illegal instruction")] IllegalInstruction = 14, - #[fail(display = "pruned cell")] + #[error("pruned cell")] PrunedCellAccess = 15, - #[fail(display = "big cell")] + #[error("big cell")] BigCellAccess = 16 } diff --git a/src/validators.rs b/src/validators.rs index f6ea2475..1a55cbc2 100644 --- a/src/validators.rs +++ b/src/validators.rs @@ -496,7 +496,7 @@ impl ValidatorSet { let mut prng = ValidatorSetPRNG::new(shard_pfx, workchain_id, cc_seqno); let full_list = if cc_config.isolate_mc_validators { if self.total <= self.main && !(self.main == 0 && self.total == 0) { - fail!(failure::format_err!("Count of validators is too small to make sharde's subset while `isolate_mc_validators` flag is set (total={}, main={})", self.total, self.main)) + fail!("Count of validators is too small to make sharde's subset while `isolate_mc_validators` flag is set (total={}, main={})", self.total, self.main) } let list = self.list[self.main.as_usize()..].to_vec(); Cow::Owned(