Browse Source

sdk: get_verifying_slot_epoch added

aggstam 3 years ago
parent
commit
53682169cd

+ 4 - 4
src/contract/consensus/src/entrypoint/stake_v1.rs

@@ -30,6 +30,7 @@ use darkfi_sdk::{
     error::{ContractError, ContractResult},
     merkle_add, msg,
     pasta::pallas,
+    util::get_verifying_slot_epoch,
     ContractCall,
 };
 use darkfi_serial::{deserialize, serialize, Encodable, WriteExt};
@@ -50,9 +51,8 @@ pub(crate) fn consensus_stake_get_metadata_v1(
     // Public keys for the transaction signatures we have to verify
     let signature_pubkeys = vec![params.input.signature_public];
 
-    // TODO: Grab the minting epoch from the verifying slot
-    //let verifying_slot = get_verifying_slot_epoch();
-    let epoch = PALLAS_ZERO;
+    // Grab the minting epoch of the verifying slot
+    let epoch = get_verifying_slot_epoch();
 
     // Grab the pedersen commitment from the anonymous output
     let output = &params.output;
@@ -60,7 +60,7 @@ pub(crate) fn consensus_stake_get_metadata_v1(
 
     zk_public_inputs.push((
         CONSENSUS_CONTRACT_ZKAS_MINT_NS_V1.to_string(),
-        vec![epoch, output.coin.inner(), *value_coords.x(), *value_coords.y()],
+        vec![epoch.into(), output.coin.inner(), *value_coords.x(), *value_coords.y()],
     ));
 
     // Serialize everything gathered and return it

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

@@ -165,6 +165,11 @@ pub(crate) fn get_verifying_slot(ctx: FunctionEnvMut<Env>) -> u64 {
     ctx.data().time_keeper.verifying_slot
 }
 
+/// Will return current runtime configured verifying slot epoch number.
+pub(crate) fn get_verifying_slot_epoch(ctx: FunctionEnvMut<Env>) -> u64 {
+    ctx.data().time_keeper.verifying_slot_epoch()
+}
+
 /// Will return requested slot checkpoint from `SlotCheckpointStore`.
 pub(crate) fn get_slot_checkpoint(ctx: FunctionEnvMut<Env>, slot: u64) -> i64 {
     let env = ctx.data();

+ 6 - 0
src/runtime/vm_runtime.rs

@@ -269,6 +269,12 @@ impl Runtime {
                     import::util::get_verifying_slot,
                 ),
 
+                "get_verifying_slot_epoch_" => Function::new_typed_with_env(
+                    &mut store,
+                    &ctx,
+                    import::util::get_verifying_slot_epoch,
+                ),
+
                 "get_slot_checkpoint_" => Function::new_typed_with_env(
                     &mut store,
                     &ctx,

+ 12 - 1
src/sdk/src/util.rs

@@ -83,12 +83,22 @@ pub fn get_current_slot() -> u64 {
 /// verifying slot.
 ///
 /// ```
-/// slot = get_current_slot();
+/// slot = get_verifying_slot();
 /// ```
 pub fn get_verifying_slot() -> u64 {
     unsafe { get_verifying_slot_() }
 }
 
+/// Everyone can call this. Will return runtime configured
+/// verifying slot epoch.
+///
+/// ```
+/// slot = get_verifying_slot_epoch();
+/// ```
+pub fn get_verifying_slot_epoch() -> u64 {
+    unsafe { get_verifying_slot_epoch_() }
+}
+
 /// Everyone can call this. Will return requested slot checkpoint from `SlotCheckpointStore`.
 ///
 /// ```
@@ -117,6 +127,7 @@ extern "C" {
     fn get_current_epoch_() -> u64;
     fn get_current_slot_() -> u64;
     fn get_verifying_slot_() -> u64;
+    fn get_verifying_slot_epoch_() -> u64;
     fn get_slot_checkpoint_(slot: u64) -> i64;
     fn get_blockchain_time_() -> u64;
 }

+ 5 - 0
src/util/time.rs

@@ -70,6 +70,11 @@ impl TimeKeeper {
         slot % self.epoch_length
     }
 
+    /// Calculates the epoch of the verifying slot.
+    pub fn verifying_slot_epoch(&self) -> u64 {
+        self.slot_epoch(self.verifying_slot)
+    }
+
     /// Calculates seconds until next Nth slot starting time.
     pub fn next_n_slot_start(&self, n: u64) -> u64 {
         assert!(n > 0);