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 DynError and some chores #21

Closed
wants to merge 2 commits into from
Closed
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
1 change: 1 addition & 0 deletions rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ license = "MIT OR Apache-2.0"
repository = "https://github.com/freenet/freenet-stdlib"

[dependencies]
anyhow = "1"
arbitrary = { version = "1", optional = true, features = ["derive"] }
bincode = "1"
byteorder = "1"
Expand Down
4 changes: 2 additions & 2 deletions rust/src/client_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub enum Error {
#[error("connection closed")]
ConnectionClosed,
#[error("unhandled error: {0}")]
OtherError(Box<dyn std::error::Error + Send + Sync>),
OtherError(anyhow::Error),
}

pub trait TryFromFbs<T>: Sized {
Expand All @@ -56,7 +56,7 @@ pub enum WsApiError {
#[error("Unsupported contract version")]
UnsupportedContractVersion,
#[error("Failed unpacking contract container")]
UnpackingContractContainerError(Box<dyn std::error::Error + Send + Sync + 'static>),
UnpackingContractContainerError(anyhow::Error),
#[error("Failed decoding message from client request: {cause}")]
DeserError { cause: String },
}
Expand Down
6 changes: 3 additions & 3 deletions rust/src/client_api/client_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1378,7 +1378,7 @@ mod client_request_test {
const EXPECTED_ENCODED_CONTRACT_ID: &str = "6kVs66bKaQAC6ohr8b43SvJ95r36tc2hnG7HezmaJHF9";

#[test]
fn test_build_contract_put_op_from_fbs() -> Result<(), Box<dyn std::error::Error>> {
fn test_build_contract_put_op_from_fbs() -> Result<(), anyhow::Error> {
let put_req_op = vec![
4, 0, 0, 0, 244, 255, 255, 255, 16, 0, 0, 0, 0, 0, 0, 1, 8, 0, 12, 0, 11, 0, 4, 0, 8,
0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 1, 198, 255, 255, 255, 12, 0, 0, 0, 20, 0, 0, 0, 36, 0,
Expand Down Expand Up @@ -1418,7 +1418,7 @@ mod client_request_test {
}

#[test]
fn test_build_contract_get_op_from_fbs() -> Result<(), Box<dyn std::error::Error>> {
fn test_build_contract_get_op_from_fbs() -> Result<(), anyhow::Error> {
let get_req_op = vec![
4, 0, 0, 0, 244, 255, 255, 255, 16, 0, 0, 0, 0, 0, 0, 1, 8, 0, 12, 0, 11, 0, 4, 0, 8,
0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 3, 222, 255, 255, 255, 12, 0, 0, 0, 8, 0, 12, 0, 8, 0, 4,
Expand Down Expand Up @@ -1448,7 +1448,7 @@ mod client_request_test {
}

#[test]
fn test_build_contract_update_op_from_fbs() -> Result<(), Box<dyn std::error::Error>> {
fn test_build_contract_update_op_from_fbs() -> Result<(), anyhow::Error> {
let update_op = vec![
4, 0, 0, 0, 220, 255, 255, 255, 8, 0, 0, 0, 0, 0, 0, 1, 232, 255, 255, 255, 8, 0, 0, 0,
0, 0, 0, 2, 204, 255, 255, 255, 16, 0, 0, 0, 52, 0, 0, 0, 8, 0, 12, 0, 11, 0, 4, 0, 8,
Expand Down
9 changes: 3 additions & 6 deletions rust/src/client_api/regular.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,7 @@ mod test {
Server { recv, listener }
}

async fn listen(
self,
tx: tokio::sync::oneshot::Sender<()>,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
async fn listen(self, tx: tokio::sync::oneshot::Sender<()>) -> Result<(), anyhow::Error> {
let (stream, _) =
tokio::time::timeout(Duration::from_millis(10), self.listener.accept()).await??;
let mut stream = tokio_tungstenite::accept_async(stream).await?;
Expand All @@ -248,7 +245,7 @@ mod test {
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_send() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
async fn test_send() -> Result<(), anyhow::Error> {
let port = PORT.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
let server = Server::new(port, true).await;
let (tx, rx) = tokio::sync::oneshot::channel::<()>();
Expand All @@ -266,7 +263,7 @@ mod test {
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_recv() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
async fn test_recv() -> Result<(), anyhow::Error> {
let port = PORT.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
let server = Server::new(port, false).await;
let (tx, rx) = tokio::sync::oneshot::channel::<()>();
Expand Down
12 changes: 6 additions & 6 deletions rust/src/contract_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -863,7 +863,7 @@ impl ContractCode<'_> {
let key_arr = hasher.finalize();
debug_assert_eq!(key_arr[..].len(), CONTRACT_KEY_SIZE);
let mut key = [0; CONTRACT_KEY_SIZE];
key.copy_from_slice(&key_arr);
key.copy_from_slice(key_arr.as_ref());
CodeHash(key)
}
}
Expand Down Expand Up @@ -1052,7 +1052,7 @@ impl ContractKey {
let full_key_arr = hasher.finalize();

let mut spec = [0; CONTRACT_KEY_SIZE];
spec.copy_from_slice(&full_key_arr);
spec.copy_from_slice(full_key_arr.as_ref());
Ok(Self {
instance: ContractInstanceId(spec),
code: Some(CodeHash(code_key)),
Expand Down Expand Up @@ -1134,7 +1134,7 @@ fn generate_id<'a>(

debug_assert_eq!(full_key_arr[..].len(), CONTRACT_KEY_SIZE);
let mut spec = [0; CONTRACT_KEY_SIZE];
spec.copy_from_slice(&full_key_arr);
spec.copy_from_slice(full_key_arr.as_ref());
ContractInstanceId(spec)
}

Expand Down Expand Up @@ -1564,7 +1564,7 @@ mod test {
});

#[test]
fn key_encoding() -> Result<(), Box<dyn std::error::Error>> {
fn key_encoding() -> Result<(), anyhow::Error> {
let code = ContractCode::from(vec![1, 2, 3]);
let expected = ContractKey::from_params_and_code(Parameters::from(vec![]), &code);
// let encoded_key = expected.encode();
Expand All @@ -1579,7 +1579,7 @@ mod test {
}

#[test]
fn key_ser() -> Result<(), Box<dyn std::error::Error>> {
fn key_ser() -> Result<(), anyhow::Error> {
let mut gen = arbitrary::Unstructured::new(&*RND_BYTES);
let expected: ContractKey = gen.arbitrary()?;
let encoded = bs58::encode(expected.as_bytes()).into_string();
Expand All @@ -1594,7 +1594,7 @@ mod test {
}

#[test]
fn contract_ser() -> Result<(), Box<dyn std::error::Error>> {
fn contract_ser() -> Result<(), anyhow::Error> {
let mut gen = arbitrary::Unstructured::new(&*RND_BYTES);
let expected: Contract = gen.arbitrary()?;

Expand Down
6 changes: 3 additions & 3 deletions rust/src/delegate_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ impl DelegateKey {

debug_assert_eq!(full_key_arr[..].len(), DELEGATE_HASH_LENGTH);
let mut key = [0; DELEGATE_HASH_LENGTH];
key.copy_from_slice(&full_key_arr);
key.copy_from_slice(full_key_arr.as_ref());

Ok(Self {
key,
Expand Down Expand Up @@ -309,7 +309,7 @@ fn generate_id<'a>(

debug_assert_eq!(full_key_arr[..].len(), DELEGATE_HASH_LENGTH);
let mut key = [0; DELEGATE_HASH_LENGTH];
key.copy_from_slice(&full_key_arr);
key.copy_from_slice(full_key_arr.as_ref());
key
}

Expand All @@ -328,7 +328,7 @@ impl SecretsId {
hasher.update(&key);
let hashed = hasher.finalize();
let mut hash = [0; 32];
hash.copy_from_slice(&hashed);
hash.copy_from_slice(hashed.as_ref());
Self { key, hash }
}

Expand Down
8 changes: 4 additions & 4 deletions rust/src/memory/buf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ mod test {
(export "initiate_buffer" (func $initiate_buffer))
)"#;

fn build_test_mod() -> Result<(Store, Instance), Box<dyn std::error::Error>> {
fn build_test_mod() -> Result<(Store, Instance), anyhow::Error> {
let wasm_bytes = wat2wasm(TEST_MODULE.as_bytes())?;
let mut store = Store::new(Cranelift::new());
let module = Module::new(&store, wasm_bytes)?;
Expand All @@ -377,7 +377,7 @@ mod test {

#[test]
#[ignore]
fn read_and_write() -> Result<(), Box<dyn std::error::Error>> {
fn read_and_write() -> Result<(), anyhow::Error> {
let (mut store, instance) = build_test_mod()?;
let mem = instance.exports.get_memory("memory")?.view(&store);
let linear_mem = WasmLinearMem {
Expand All @@ -402,7 +402,7 @@ mod test {

#[test]
#[ignore]
fn read_and_write_bytes() -> Result<(), Box<dyn std::error::Error>> {
fn read_and_write_bytes() -> Result<(), anyhow::Error> {
let (mut store, instance) = build_test_mod()?;
let mem = instance.exports.get_memory("memory")?.view(&store);
let linear_mem = WasmLinearMem {
Expand All @@ -427,7 +427,7 @@ mod test {

#[test]
#[ignore]
fn update() -> Result<(), Box<dyn std::error::Error>> {
fn update() -> Result<(), anyhow::Error> {
let (mut store, instance) = build_test_mod()?;
let mem = instance.exports.get_memory("memory")?.view(&store);
let linear_mem = WasmLinearMem {
Expand Down
14 changes: 4 additions & 10 deletions rust/src/versioning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,7 @@ impl DelegateCode<'static> {
}

impl DelegateCode<'_> {
pub fn to_bytes_versioned(
&self,
version: APIVersion,
) -> Result<Vec<u8>, Box<dyn std::error::Error + Send + Sync + 'static>> {
pub fn to_bytes_versioned(&self, version: APIVersion) -> anyhow::Result<Vec<u8>> {
match version {
APIVersion::Version0_0_1 => {
let output_size =
Expand Down Expand Up @@ -362,20 +359,17 @@ impl Display for APIVersion {
}

impl<'a> TryFrom<&'a semver::Version> for APIVersion {
type Error = Box<dyn std::error::Error + Send + Sync>;
type Error = anyhow::Error;
fn try_from(value: &'a semver::Version) -> Result<Self, Self::Error> {
match value {
ver if ver == &semver::Version::new(0, 0, 1) => Ok(APIVersion::Version0_0_1),
other => Err(format!("{other} version not supported").into()),
other => Err(anyhow::anyhow!("{other} version not supported")),
}
}
}

impl ContractCode<'_> {
pub fn to_bytes_versioned(
&self,
version: APIVersion,
) -> Result<Vec<u8>, Box<dyn std::error::Error + Send + Sync + 'static>> {
pub fn to_bytes_versioned(&self, version: APIVersion) -> anyhow::Result<Vec<u8>> {
match version {
APIVersion::Version0_0_1 => {
let output_size =
Expand Down
Loading