소스 검색

sdk/util: replaced get_system_time with get_blockchain_time, get_current_epoch and get_current_slot added

aggstam 3 년 전
부모
커밋
040a5d5604
4개의 변경된 파일59개의 추가작업 그리고 23개의 파일을 삭제
  1. 9 6
      src/runtime/import/util.rs
  2. 14 2
      src/runtime/vm_runtime.rs
  3. 25 14
      src/sdk/src/util.rs
  4. 11 1
      src/util/time.rs

+ 9 - 6
src/runtime/import/util.rs

@@ -149,11 +149,14 @@ pub(crate) fn get_object_size(ctx: FunctionEnvMut<Env>, idx: u32) -> i64 {
     obj.len() as i64
 }
 
-pub(crate) fn get_system_time(ctx: FunctionEnvMut<Env>) -> u64 {
-    let env = ctx.data();
+pub(crate) fn get_current_epoch(ctx: FunctionEnvMut<Env>) -> u64 {
+    ctx.data().time_keeper.current_epoch()
+}
 
-    match env.time_keeper.unix_timestamp() {
-        Ok(t) => t,
-        Err(_) => 0,
-    }
+pub(crate) fn get_current_slot(ctx: FunctionEnvMut<Env>) -> u64 {
+    ctx.data().time_keeper.current_slot()
+}
+
+pub(crate) fn get_blockchain_time(ctx: FunctionEnvMut<Env>) -> u64 {
+    ctx.data().time_keeper.blockchain_timestamp()
 }

+ 14 - 2
src/runtime/vm_runtime.rs

@@ -258,10 +258,22 @@ impl Runtime {
                     import::merkle::merkle_add,
                 ),
 
-                "get_system_time_" => Function::new_typed_with_env(
+                "get_current_epoch_" => Function::new_typed_with_env(
                     &mut store,
                     &ctx,
-                    import::util::get_system_time,
+                    import::util::get_current_epoch,
+                ),
+
+                "get_current_slot_" => Function::new_typed_with_env(
+                    &mut store,
+                    &ctx,
+                    import::util::get_current_slot,
+                ),
+
+                "get_blockchain_time_" => Function::new_typed_with_env(
+                    &mut store,
+                    &ctx,
+                    import::util::get_blockchain_time,
                 ),
             }
         };

+ 25 - 14
src/sdk/src/util.rs

@@ -16,9 +16,7 @@
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 
-use super::error::{ContractError, GenericResult};
-
-pub const CALL_FAILED: u64 = 0;
+use super::error::ContractError;
 
 pub fn set_return_data(data: &[u8]) -> Result<(), ContractError> {
     unsafe {
@@ -41,20 +39,31 @@ pub fn get_object_size(object_index: u32) -> i64 {
     unsafe { get_object_size_(object_index) }
 }
 
-/// Everyone can call this. Will return current system timestamp.
+/// Everyone can call this. Will return current epoch.
 ///
 /// ```
-/// timestamp = get_system_time();
+/// epoch = get_current_epoch();
 /// ```
-pub fn get_system_time() -> GenericResult<u64> {
-    let ret = unsafe { get_system_time_() };
+pub fn get_current_epoch() -> u64 {
+    unsafe { get_current_epoch_() }
+}
 
-    match ret {
-        // 0 here means system time is less or equal than UNIX_EPOCH
-        CALL_FAILED => return Err(ContractError::GetSystemTimeFailed),
-        // In any other case we can return the value
-        _ => Ok(ret),
-    }
+/// Everyone can call this. Will return current slot.
+///
+/// ```
+/// slot = get_current_slot();
+/// ```
+pub fn get_current_slot() -> u64 {
+    unsafe { get_current_slot_() }
+}
+
+/// Everyone can call this. Will return current blockchain timestamp.
+///
+/// ```
+/// timestamp = get_blockchain_time();
+/// ```
+pub fn get_blockchain_time() -> u64 {
+    unsafe { get_blockchain_time_() }
 }
 
 extern "C" {
@@ -63,5 +72,7 @@ extern "C" {
     fn get_object_bytes_(ptr: *const u8, len: u32) -> i64;
     fn get_object_size_(len: u32) -> i64;
 
-    fn get_system_time_() -> u64;
+    fn get_current_epoch_() -> u64;
+    fn get_current_slot_() -> u64;
+    fn get_blockchain_time_() -> u64;
 }

+ 11 - 1
src/util/time.rs

@@ -80,7 +80,17 @@ impl TimeKeeper {
         self.next_n_slot_start(self.slots_to_next_n_epoch(n))
     }
 
-    pub fn unix_timestamp(&self) -> Result<u64> {
+    /// Calculates current blockchain timestamp.
+    /// Blockchain timestamp is the time elapsed since
+    /// Genesis timestamp, based on slot time ticking,
+    /// therefore representing the starting timestamp of
+    /// current slot.
+    pub fn blockchain_timestamp(&self) -> u64 {
+        self.genesis_ts.0 + self.current_slot() * self.slot_time
+    }
+
+    /// Calculates current system timestamp.
+    pub fn system_timestamp(&self) -> Result<u64> {
         Ok(UNIX_EPOCH.elapsed()?.as_secs())
     }
 }