瀏覽代碼

wasm: replace current mess of macros with a single define_contract!()

x 3 年之前
父節點
當前提交
dbdbc42705
共有 2 個文件被更改,包括 13 次插入30 次删除
  1. 8 6
      example/smart-contract/src/lib.rs
  2. 5 24
      src/sdk/src/entrypoint.rs

+ 8 - 6
example/smart-contract/src/lib.rs

@@ -1,11 +1,10 @@
 use darkfi_sdk::{
     db::{db_begin_tx, db_end_tx, db_get, db_init, db_lookup, db_set},
-    entrypoint,
     error::ContractResult,
-    initialize, msg,
+    msg,
     state::set_update,
     tx::FuncCall,
-    update_state,
+    define_contract,
 };
 use darkfi_serial::{deserialize, serialize, SerialDecodable, SerialEncodable};
 
@@ -45,7 +44,12 @@ pub struct FooUpdate {
     pub age: u32,
 }
 
-initialize!(init_contract);
+define_contract!(
+    init: init_contract,
+    exec: process_instruction,
+    apply: process_update
+);
+
 fn init_contract(_ix: &[u8]) -> ContractResult {
     msg!("wakeup wagies!");
     db_init("wagies")?;
@@ -64,7 +68,6 @@ fn init_contract(_ix: &[u8]) -> ContractResult {
 // This is the main entrypoint function where the payload is fed.
 // Through here, you can branch out into different functions inside
 // this library.
-entrypoint!(process_instruction);
 fn process_instruction(ix: &[u8]) -> ContractResult {
     match Function::from(ix[0]) {
         Function::Foo => {
@@ -124,7 +127,6 @@ fn process_instruction(ix: &[u8]) -> ContractResult {
     Ok(())
 }
 
-update_state!(process_update);
 fn process_update(update_data: &[u8]) -> ContractResult {
     msg!("Make update!");
 

+ 5 - 24
src/sdk/src/entrypoint.rs

@@ -22,51 +22,32 @@ use std::{mem::size_of, slice::from_raw_parts};
 pub const SUCCESS: u64 = 0;
 
 #[macro_export]
-macro_rules! initialize {
-    ($process_init:ident) => {
+macro_rules! define_contract {
+    (init: $init_func:ident, exec: $exec_func:ident, apply: $apply_func:ident) => {
         /// # Safety
         #[no_mangle]
         pub unsafe extern "C" fn __initialize(input: *mut u8) -> u64 {
             let instruction_data = $crate::entrypoint::deserialize(input);
 
-            match $process_init(&instruction_data) {
+            match $init_func(&instruction_data) {
                 Ok(()) => $crate::entrypoint::SUCCESS,
                 Err(e) => e.into(),
             }
         }
-    };
-}
-
-/// This macro is used to flag the contract entrypoint function.
-/// All contracts must provide such a function and accept a payload.
-///
-/// The payload is a slice of u8 prepended with a little-endian u64
-/// that tells the slice's length.
-#[macro_export]
-macro_rules! entrypoint {
-    ($process_instruction:ident) => {
-        /// # Safety
         #[no_mangle]
         pub unsafe extern "C" fn __entrypoint(input: *mut u8) -> u64 {
             let instruction_data = $crate::entrypoint::deserialize(input);
 
-            match $process_instruction(&instruction_data) {
+            match $exec_func(&instruction_data) {
                 Ok(()) => $crate::entrypoint::SUCCESS,
                 Err(e) => e.into(),
             }
         }
-    };
-}
-
-#[macro_export]
-macro_rules! update_state {
-    ($process_update:ident) => {
-        /// # Safety
         #[no_mangle]
         pub unsafe extern "C" fn __update(input: *mut u8) -> u64 {
             let update_data = $crate::entrypoint::deserialize(input);
 
-            match $process_update(&update_data) {
+            match $apply_func(&update_data) {
                 Ok(()) => $crate::entrypoint::SUCCESS,
                 Err(e) => e.into(),
             }