Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove some unwraps #258

Merged
merged 18 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[workspace]
members = [
"extensions",
# no local deps
"database",
"tendermint",
Expand Down Expand Up @@ -94,6 +95,7 @@ constcat = { version = "0.5.0" } # upgraded std::concat! wh
derive_more = "0.99.17"
former = "0.16.0"
nz = "0.4.1"
itertools = "0.13.0"

# log
log = { version = "0.4.21" }
Expand Down
1 change: 1 addition & 0 deletions address/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ thiserror = { workspace = true }

[dev-dependencies]
serde_json = { workspace = true }
extensions = { path = "../extensions" }

[lints]
workspace = true
42 changes: 24 additions & 18 deletions address/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ pub enum AddressError {
mod tests {

use bech32::ToBase32;
use extensions::testing::UnwrapTesting;

use super::*;

Expand All @@ -270,12 +271,13 @@ mod tests {
input_address.to_base32(),
Variant::Bech32,
)
.unwrap();
.unwrap_test();

let expected_address = BaseAddress::<0>(input_address);

let address = AccAddress::from_bech32(&encoded).unwrap();
let address = AccAddress::from_bech32(&encoded);

assert_eq!(expected_address, address);
assert_eq!(Ok(expected_address), address);
}

#[test]
Expand All @@ -286,20 +288,24 @@ mod tests {
input_address.to_base32(),
Variant::Bech32,
)
.unwrap();
.unwrap_test();

encoded.pop();

let err = AccAddress::from_bech32(&encoded).unwrap_err();
let err = AccAddress::from_bech32(&encoded);

assert_eq!(err, AddressError::Decode(bech32::Error::InvalidChecksum));
assert_eq!(
err,
Err(AddressError::Decode(bech32::Error::InvalidChecksum))
);
}

#[test]
fn from_bech32_failure_wrong_prefix() {
let mut hrp = BECH_32_PREFIX_ACC_ADDR.to_string();
hrp.push_str("atom"); // adding to the BECH_32_PREFIX_ACC_ADDR ensures that hrp is different
let encoded =
bech32::encode(&hrp, vec![0x00, 0x01, 0x02].to_base32(), Variant::Bech32).unwrap();
bech32::encode(&hrp, vec![0x00, 0x01, 0x02].to_base32(), Variant::Bech32).unwrap_test();

let err = AccAddress::from_bech32(&encoded).unwrap_err();

Expand All @@ -319,7 +325,7 @@ mod tests {
vec![0x00, 0x01, 0x02].to_base32(),
Variant::Bech32m,
)
.unwrap();
.unwrap_test();

let err = AccAddress::from_bech32(&encoded).unwrap_err();

Expand All @@ -339,7 +345,7 @@ mod tests {
vec![0x00; 300].to_base32(),
Variant::Bech32,
)
.unwrap();
.unwrap_test();

let err = AccAddress::from_bech32(&encoded).unwrap_err();

Expand All @@ -354,8 +360,8 @@ mod tests {

#[test]
fn from_bech32_failure_empty_address() {
let encoded =
bech32::encode(BECH_32_PREFIX_ACC_ADDR, vec![].to_base32(), Variant::Bech32).unwrap();
let encoded = bech32::encode(BECH_32_PREFIX_ACC_ADDR, vec![].to_base32(), Variant::Bech32)
.unwrap_test();

let err = AccAddress::from_bech32(&encoded).unwrap_err();

Expand All @@ -373,7 +379,7 @@ mod tests {
fn to_string_success() {
let addr = "cosmos1syavy2npfyt9tcncdtsdzf7kny9lh777pahuux".to_string();

let acc_addr = AccAddress::from_bech32(&addr).unwrap();
let acc_addr = AccAddress::from_bech32(&addr).unwrap_test();

assert_eq!(addr, acc_addr.to_string());
}
Expand All @@ -382,35 +388,35 @@ mod tests {
fn string_from_self_success() {
let addr = "cosmos1syavy2npfyt9tcncdtsdzf7kny9lh777pahuux".to_string();

let acc_addr = AccAddress::from_bech32(&addr).unwrap();
let acc_addr = AccAddress::from_bech32(&addr).unwrap_test();

assert_eq!(addr, String::from(acc_addr));
}

#[test]
fn serialize_works() {
let addr = "cosmos1syavy2npfyt9tcncdtsdzf7kny9lh777pahuux".to_string();
let acc_addr = AccAddress::from_bech32(&addr).unwrap();
let acc_addr = AccAddress::from_bech32(&addr).unwrap_test();

let json = serde_json::to_string(&acc_addr).unwrap();
let json = serde_json::to_string(&acc_addr).unwrap_test();

assert_eq!(json, r#""cosmos1syavy2npfyt9tcncdtsdzf7kny9lh777pahuux""#);
}

#[test]
fn deserialize_works() {
let json = r#""cosmos1syavy2npfyt9tcncdtsdzf7kny9lh777pahuux""#;
let addr: AccAddress = serde_json::from_str(json).unwrap();
let addr = serde_json::from_str::<AccAddress>(json).unwrap_test();
assert_eq!(
addr,
AccAddress::from_bech32("cosmos1syavy2npfyt9tcncdtsdzf7kny9lh777pahuux").unwrap()
AccAddress::from_bech32("cosmos1syavy2npfyt9tcncdtsdzf7kny9lh777pahuux").unwrap_test()
)
}

#[test]
fn prefix_len_bytes_works() {
let addr = vec![0x00, 0x01, 0x02];
let acc_addr = AccAddress::try_from(addr.as_slice()).unwrap();
let acc_addr = AccAddress::try_from(addr.as_slice()).unwrap_test();

let prefixed = acc_addr.prefix_len_bytes();

Expand Down
2 changes: 2 additions & 0 deletions database/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ version = "0.1.0"
edition = "2021"

[dependencies]
extensions = { path = "../extensions" }

thiserror = { workspace = true }
rocksdb = { version = "0.22.0", optional = true }
sled = {version = "0.34.7", optional = true }
Expand Down
1 change: 0 additions & 1 deletion database/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#![warn(rust_2018_idioms)]

pub mod error;
pub mod ext;
mod memory;
pub mod prefix;
#[cfg(feature = "rocksdb")]
Expand Down
4 changes: 2 additions & 2 deletions database/src/rocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ mod tests {

#[test]
fn iterator_works() {
let db = RocksDB::new("tmp/1").unwrap();
let db = RocksDB::new("tmp/1").expect("hardcoded is valid");
db.put(vec![1], vec![1]);
db.put(vec![2], vec![2]);
let got_pairs: Vec<(Box<[u8]>, Box<[u8]>)> = db.iterator().collect();
Expand All @@ -86,7 +86,7 @@ mod tests {

#[test]
fn prefix_iterator_works() {
let db = RocksDB::new("tmp/2").unwrap();
let db = RocksDB::new("tmp/2").expect("hardcoded is valid");
db.put(vec![1, 1], vec![1]);
db.put(vec![2, 1], vec![2]);
db.put(vec![3, 1], vec![3]);
Expand Down
4 changes: 3 additions & 1 deletion database/src/sled.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::{error::DatabaseError, ext::UnwrapCorrupt, DBBuilder, Database, DatabaseBuilder};
use extensions::corruption::UnwrapCorrupt;

use crate::{error::DatabaseError, DBBuilder, Database, DatabaseBuilder};

impl DatabaseBuilder<SledDb> for DBBuilder {
type Err = DatabaseError;
Expand Down
11 changes: 11 additions & 0 deletions extensions/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "extensions"
version = "0.1.0"
edition = "2021"

[dependencies]
vec1 = { workspace = true }
itertools = { workspace = true }

[lints]
workspace = true
File renamed without changes.
File renamed without changes.
File renamed without changes.
8 changes: 8 additions & 0 deletions extensions/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
pub mod lock;
pub mod corruption;
pub mod gas;
pub mod infallible;
pub mod pagination;
pub mod socker_addr;
pub mod testing;
pub mod try_map;
22 changes: 22 additions & 0 deletions extensions/src/lock.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use std::sync::{RwLock, RwLockReadGuard, RwLockWriteGuard};

const POISONED_LOCK_MSG: &str = "poisoned lock";

pub trait AcquireRwLock {
type Output;

fn acquire_read(&self) -> RwLockReadGuard<'_, Self::Output>;
fn acquire_write(&self) -> RwLockWriteGuard<'_, Self::Output>;
}

impl<T> AcquireRwLock for RwLock<T> {
type Output = T;

fn acquire_read(&self) -> RwLockReadGuard<'_, Self::Output> {
self.read().expect(POISONED_LOCK_MSG)
}

fn acquire_write(&self) -> RwLockWriteGuard<'_, Self::Output> {
self.write().expect(POISONED_LOCK_MSG)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ use std::borrow::Cow;
use itertools::Itertools;
use vec1::Vec1;

use crate::types::{base::coin::UnsignedCoin, store::gas::errors::GasStoreErrors};

use super::{PaginationResultElement, TwoIterators};

pub type PaginationByKeyResult = PaginationResultElement<Vec<u8>>;
Expand Down Expand Up @@ -85,12 +83,6 @@ impl<T: Iterator<Item = U>, U: PaginationKey> IteratorPaginateByKey for T {
}
}

impl PaginationKey for UnsignedCoin {
fn iterator_key(&self) -> Cow<'_, [u8]> {
Cow::Borrowed(self.denom.as_ref())
}
}

impl<T> PaginationKey for (Cow<'_, Vec<u8>>, T) {
fn iterator_key(&self) -> Cow<'_, [u8]> {
Cow::Borrowed(self.0.as_ref())
Expand All @@ -109,18 +101,19 @@ impl PaginationKey for Vec<u8> {
}
}

impl<T: PaginationKey> PaginationKey for Result<T, GasStoreErrors> {
impl<T: PaginationKey, ERR: PaginationKey> PaginationKey for Result<T, ERR> {
fn iterator_key(&self) -> Cow<'_, [u8]> {
match self {
Ok(var) => var.iterator_key(),
Err(var) => Cow::Borrowed(&var.key),
Err(var) => var.iterator_key(),
}
}
}

#[cfg(test)]
mod tests {
use crate::ext::UnwrapPagination;

use crate::pagination::UnwrapPagination;

use super::*;
use vec1::vec1;
Expand Down
Loading