Skip to content

Commit

Permalink
Remove dependency on eyre from post-cbindings crate
Browse files Browse the repository at this point in the history
  • Loading branch information
poszu committed May 18, 2023
1 parent e2f2a20 commit b416213
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 23 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

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

1 change: 0 additions & 1 deletion ffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ name = "post"
crate_type = ["staticlib", "cdylib"]

[dependencies]
eyre = "0.6.8"
log = { version = "0.4.17", features = ["std"] }
post-rs = { path = "../" }
scrypt-ocl = { path = "../scrypt-ocl" }
Expand Down
23 changes: 7 additions & 16 deletions ffi/src/initialization.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{ffi::c_char, fmt::Debug};
use std::{error::Error, ffi::c_char, fmt::Debug};

use post::{
initialize::{CpuInitializer, Initialize},
Expand Down Expand Up @@ -50,17 +50,6 @@ pub enum DeviceClass {
GPU = 2,
}

impl Debug for Provider {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Provider")
.field("name", unsafe {
// SAFETY: The name is always null terminated.
&std::ffi::CStr::from_ptr(self.name.as_ptr())
})
.finish()
}
}

/// Returns the number of providers available.
#[no_mangle]
pub extern "C" fn get_providers_count() -> usize {
Expand Down Expand Up @@ -163,7 +152,7 @@ pub extern "C" fn new_initializer(
vrf_difficulty: *const u8,
) -> *mut Initializer {
match _new_initializer(provider_id, n, commitment, vrf_difficulty) {
Ok(initializer) => initializer,
Ok(initializer) => Box::into_raw(initializer) as _,
Err(e) => {
log::error!("Error creating initializer: {e:?}");
std::ptr::null_mut()
Expand All @@ -176,8 +165,10 @@ fn _new_initializer(
n: usize,
commitment: *const u8,
vrf_difficulty: *const u8,
) -> eyre::Result<*mut Initializer> {
eyre::ensure!(n.is_power_of_two(), "scrypt N must be a power of two");
) -> Result<Box<InitializerWrapper>, Box<dyn Error>> {
if !n.is_power_of_two() {
return Err("scrypt N must be a power of two".into());
}
let commitment = unsafe { std::slice::from_raw_parts(commitment, 32) };
let commitment = commitment.try_into()?;

Expand Down Expand Up @@ -206,7 +197,7 @@ fn _new_initializer(
vrf_difficulty,
});

Ok(Box::into_raw(initializer) as *mut Initializer)
Ok(initializer)
}

#[no_mangle]
Expand Down
1 change: 1 addition & 0 deletions ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod log;
mod post_impl;

#[repr(C)]
#[derive(Debug)]
pub struct ArrayU8 {
ptr: *mut u8,
len: usize,
Expand Down
33 changes: 28 additions & 5 deletions ffi/src/post_impl.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::{
error::Error,
ffi::{c_char, c_uchar, CStr},
mem::{self, ManuallyDrop},
path::Path,
};

use eyre::Context;
pub use post::config::Config;
pub use post::metadata::ProofMetadata;
pub use post::ScryptParams;
Expand All @@ -16,6 +16,7 @@ use post::{
use crate::ArrayU8;

#[repr(C)]
#[derive(Debug)]
pub struct Proof {
nonce: u32,
indices: ArrayU8,
Expand Down Expand Up @@ -46,7 +47,7 @@ pub extern "C" fn generate_proof(
threads: usize,
) -> *mut Proof {
match _generate_proof(datadir, challenge, cfg, nonces, threads) {
Ok(proof) => proof,
Ok(proof) => Box::into_raw(proof),
Err(e) => {
//TODO(poszu) communicate errors better
log::error!("{e:?}");
Expand All @@ -61,9 +62,13 @@ fn _generate_proof(
cfg: Config,
nonces: usize,
threads: usize,
) -> eyre::Result<*mut Proof> {
) -> Result<Box<Proof>, Box<dyn Error>> {
let datadir = unsafe { CStr::from_ptr(datadir) };
let datadir = Path::new(datadir.to_str().context("parsing datadir as UTF-8")?);
let datadir = Path::new(
datadir
.to_str()
.map_err(|e| format!("reading datadir: {e:?}"))?,
);

let challenge = unsafe { std::slice::from_raw_parts(challenge, 32) };
let challenge = challenge.try_into()?;
Expand All @@ -78,7 +83,7 @@ fn _generate_proof(
k2_pow: proof.k2_pow,
});

Ok(Box::into_raw(proof))
Ok(proof)
}

#[repr(C)]
Expand Down Expand Up @@ -129,3 +134,21 @@ pub unsafe extern "C" fn verify_proof(
mem::forget(proof.indices);
result
}

#[cfg(test)]
mod tests {
#[test]
fn datadir_must_be_utf8() {
let datadir = std::ffi::CString::new([159, 146, 150]).unwrap();
let cfg = super::Config {
k1: 10,
k2: 20,
k3: 20,
k2_pow_difficulty: u64::MAX,
pow_scrypt: super::ScryptParams::new(1, 1, 1),
scrypt: super::ScryptParams::new(1, 1, 1),
};
let result = super::_generate_proof(datadir.as_ptr(), [0u8; 32].as_ptr(), cfg, 1, 0);
assert!(result.unwrap_err().to_string().contains("Utf8Error"));
}
}

0 comments on commit b416213

Please sign in to comment.