Explorar el Código

sdk: Add initial state query draft.

The current implenmentation is able to scan the nullifier set.
Luther Blissett hace 3 años
padre
commit
aaee981136
Se han modificado 3 ficheros con 45 adiciones y 0 borrados
  1. 18 0
      src/sdk/src/error.rs
  2. 3 0
      src/sdk/src/lib.rs
  3. 24 0
      src/sdk/src/state.rs

+ 18 - 0
src/sdk/src/error.rs

@@ -13,6 +13,12 @@ pub enum ContractError {
 
     #[error("Internal error")]
     Internal,
+
+    #[error("IO error: {0}")]
+    IoError(String),
+
+    #[error("Error checking if nullifier exists")]
+    NullifierExistCheck,
 }
 
 /// Builtin return values occupy the upper 32 bits
@@ -25,11 +31,15 @@ macro_rules! to_builtin {
 
 pub const CUSTOM_ZERO: u64 = to_builtin!(1);
 pub const INTERNAL_ERROR: u64 = to_builtin!(2);
+pub const IO_ERROR: u64 = to_builtin!(3);
+pub const NULLIFIER_EXIST_CHECK: u64 = to_builtin!(4);
 
 impl From<ContractError> for u64 {
     fn from(err: ContractError) -> Self {
         match err {
             ContractError::Internal => INTERNAL_ERROR,
+            ContractError::IoError(_) => IO_ERROR,
+            ContractError::NullifierExistCheck => NULLIFIER_EXIST_CHECK,
             ContractError::Custom(error) => {
                 if error == 0 {
                     CUSTOM_ZERO
@@ -46,7 +56,15 @@ impl From<u64> for ContractError {
         match error {
             CUSTOM_ZERO => Self::Custom(0),
             INTERNAL_ERROR => Self::Internal,
+            IO_ERROR => Self::IoError("Unknown".to_string()),
+            NULLIFIER_EXIST_CHECK => Self::NullifierExistCheck,
             _ => Self::Custom(error as u32),
         }
     }
 }
+
+impl From<std::io::Error> for ContractError {
+    fn from(err: std::io::Error) -> Self {
+        Self::IoError(format!("{}", err))
+    }
+}

+ 3 - 0
src/sdk/src/lib.rs

@@ -9,3 +9,6 @@ pub mod log;
 
 /// Crypto-related definitions
 pub mod crypto;
+
+/// Functions for state queries
+pub mod state;

+ 24 - 0
src/sdk/src/state.rs

@@ -0,0 +1,24 @@
+use super::{crypto::Nullifier, error::ContractError};
+
+pub fn nullifier_exists(nullifier: &Nullifier) -> Result<bool, ContractError> {
+    #[cfg(target_arch = "wasm32")]
+    unsafe {
+        // Convert to bytes, and pass pointer to first byte in slice to the function.
+        let nf = nullifier.to_bytes();
+        return match nullifier_exists_(&nf as *const u8, 32) {
+            0 => Ok(false),
+            1 => Ok(true),
+            -1 => Err(ContractError::NullifierExistCheck),
+            -2 => Err(ContractError::Internal),
+            _ => unreachable!(),
+        }
+    }
+
+    #[cfg(not(target_arch = "wasm32"))]
+    todo!("nullifier_exists({:?}", nullifier);
+}
+
+#[cfg(target_arch = "wasm32")]
+extern "C" {
+    fn nullifier_exists_(ptr: *const u8, len: u32) -> i32;
+}