Parcourir la source

runtime: add missing get_tx_hash()

zero il y a 2 ans
Parent
commit
69cf9c3a1a
3 fichiers modifiés avec 42 ajouts et 14 suppressions
  1. 19 5
      src/runtime/import/util.rs
  2. 2 2
      src/runtime/vm_runtime.rs
  3. 21 7
      src/sdk/src/util.rs

+ 19 - 5
src/runtime/import/util.rs

@@ -229,15 +229,29 @@ pub(crate) fn get_verifying_block_height(mut ctx: FunctionEnvMut<Env>) -> u64 {
     env.verifying_block_height
 }
 
-/// Will return current runtime configured verifying block height epoch number
-pub(crate) fn get_verifying_block_height_epoch(mut ctx: FunctionEnvMut<Env>) -> u64 {
+/// Will return current runtime configured transaction hash
+pub(crate) fn get_tx_hash(mut ctx: FunctionEnvMut<Env>) -> i64 {
     let (env, mut store) = ctx.data_and_store_mut();
+    let cid = env.contract_id;
+
+    if let Err(e) =
+        acl_allow(env, &[ContractSection::Deploy, ContractSection::Exec, ContractSection::Metadata])
+    {
+        error!(
+            target: "runtime::util::get_tx",
+            "[WASM] [{}] get_tx(): Called in unauthorized section: {}", cid, e,
+        );
+        return darkfi_sdk::error::CALLER_ACCESS_DENIED
+    }
 
     // Subtract used gas. Here we count the size of the object.
-    // u64 is 8 bytes.
-    env.subtract_gas(&mut store, 8);
+    env.subtract_gas(&mut store, 32);
 
-    darkfi_sdk::blockchain::block_epoch(env.verifying_block_height)
+    // Return the length of the objects Vector.
+    // This is the location of the data that was retrieved and pushed
+    let mut objects = env.objects.borrow_mut();
+    objects.push(env.tx_hash.inner().to_vec());
+    (objects.len() - 1) as i64
 }
 
 /// Will return current blockchain timestamp,

+ 2 - 2
src/runtime/vm_runtime.rs

@@ -293,10 +293,10 @@ impl Runtime {
                     import::util::get_verifying_block_height,
                 ),
 
-                "get_verifying_block_height_epoch_" => Function::new_typed_with_env(
+                "get_tx_hash_" => Function::new_typed_with_env(
                     &mut store,
                     &ctx,
-                    import::util::get_verifying_block_height_epoch,
+                    import::util::get_tx_hash,
                 ),
 
                 "get_blockchain_time_" => Function::new_typed_with_env(

+ 21 - 7
src/sdk/src/util.rs

@@ -18,7 +18,10 @@
 
 use darkfi_serial::Encodable;
 
-use super::error::{ContractError, GenericResult};
+use super::{
+    error::{ContractError, GenericResult},
+    tx::TransactionHash,
+};
 
 /// Calls the `set_return_data` WASM function. Returns Ok(()) on success.
 /// Otherwise, convert the i64 error code into a [`ContractError`].
@@ -89,14 +92,25 @@ pub fn get_verifying_block_height() -> u64 {
     unsafe { get_verifying_block_height_() }
 }
 
-/// Everyone can call this. Will return runtime configured
-/// verifying block height epoch.
+/// Only deploy(), metadata() and exec() can call this. Will return runtime configured
+/// transaction hash.
 ///
 /// ```
-/// epoch = get_verifying_block_height_epoch();
+/// tx_hash = get_tx_hash();
 /// ```
-pub fn get_verifying_block_height_epoch() -> u64 {
-    unsafe { get_verifying_block_height_epoch_() }
+pub fn get_tx_hash() -> GenericResult<TransactionHash> {
+    let ret = unsafe { get_tx_hash_() };
+    if ret < 0 {
+        return Err(ContractError::from(ret))
+    }
+    assert!(ret >= 0);
+    // This should always be possible
+    let obj = ret as u32;
+
+    let mut tx_hash_data = [0u8; 32];
+    assert_eq!(get_object_size(obj), 32);
+    get_object_bytes(&mut tx_hash_data, obj);
+    Ok(TransactionHash(tx_hash_data))
 }
 
 /// Everyone can call this. Will return current blockchain timestamp.
@@ -141,7 +155,7 @@ extern "C" {
     fn get_object_size_(len: u32) -> i64;
 
     fn get_verifying_block_height_() -> u64;
-    fn get_verifying_block_height_epoch_() -> u64;
+    fn get_tx_hash_() -> i64;
     fn get_blockchain_time_() -> i64;
     fn get_last_block_height_() -> i64;
     fn get_tx_(ptr: *const u8) -> i64;