From 6cea4ab10f1a38c3a78474f22221c18ab549bd8f Mon Sep 17 00:00:00 2001 From: Jean-Marie PEDURAND <jm81@hotmail.fr> Date: Sun, 29 Dec 2019 14:08:06 +0100 Subject: [PATCH] wip [feat] gva: add humanTime field --- lib/dubp/block-doc/src/block.rs | 8 ++++++++ lib/dubp/block-doc/src/block/v10.rs | 7 +++++++ lib/modules/gva/resources/schema.gql | 1 + lib/modules/gva/src/schema/entities/block.rs | 9 +++++++++ 4 files changed, 25 insertions(+) diff --git a/lib/dubp/block-doc/src/block.rs b/lib/dubp/block-doc/src/block.rs index 22ae22a0..443d4a5c 100644 --- a/lib/dubp/block-doc/src/block.rs +++ b/lib/dubp/block-doc/src/block.rs @@ -49,6 +49,8 @@ pub enum VerifyBlockHashError { pub trait BlockDocumentTrait { /// Common time in block (also known as 'blockchain time') fn common_time(&self) -> u64; + /// Human time + fn human_time(&self) -> u64; /// Compute hash fn compute_hash(&self) -> BlockHash; /// Compute inner hash @@ -175,6 +177,12 @@ impl BlockDocumentTrait for BlockDocument { } } #[inline] + fn human_time(&self) -> u64 { + match self { + BlockDocument::V10(block) => block.human_time(), + } + } + #[inline] fn number(&self) -> BlockNumber { match self { BlockDocument::V10(block) => block.number(), diff --git a/lib/dubp/block-doc/src/block/v10.rs b/lib/dubp/block-doc/src/block/v10.rs index 807a34de..13e74381 100644 --- a/lib/dubp/block-doc/src/block/v10.rs +++ b/lib/dubp/block-doc/src/block/v10.rs @@ -107,6 +107,13 @@ impl BlockDocumentTrait for BlockDocumentV10 { fn common_time(&self) -> u64 { self.median_time } + fn human_time(&self) -> u64 { + if let Some(parameters) = self.parameters { + self.median_time + parameters.median_time_blocks as u64 * parameters.avg_gen_time / 2 + } else { + 0 + } + } fn compute_hash(&self) -> BlockHash { BlockHash(Hash::compute_str(&self.compute_will_hashed_string())) } diff --git a/lib/modules/gva/resources/schema.gql b/lib/modules/gva/resources/schema.gql index 5779e610..6d2ef3a3 100644 --- a/lib/modules/gva/resources/schema.gql +++ b/lib/modules/gva/resources/schema.gql @@ -87,6 +87,7 @@ type Block { number: Int! hash: String!, blockchainTime: DateTimeUtc! + humanTime: DateTimeUtc! powMin: Int! } diff --git a/lib/modules/gva/src/schema/entities/block.rs b/lib/modules/gva/src/schema/entities/block.rs index a0e3aeed..8301e438 100644 --- a/lib/modules/gva/src/schema/entities/block.rs +++ b/lib/modules/gva/src/schema/entities/block.rs @@ -36,6 +36,7 @@ pub struct Block { number: i32, hash: String, blockchain_time: NaiveDateTime, + human_time: NaiveDateTime, pow_min: i32, } @@ -68,6 +69,7 @@ impl Block { .unwrap_or_else(|| fatal_error!("BlockDb without hash.")) .to_string(), blockchain_time: NaiveDateTime::from_timestamp(block_db.block.common_time() as i64, 0), + human_time: NaiveDateTime::from_timestamp(block_db.block.human_time() as i64, 0), pow_min: block_db.block.pow_min() as i32, }) } @@ -109,6 +111,13 @@ impl super::super::BlockFields for Block { Ok(&self.blockchain_time) } #[inline] + fn field_human_time( + &self, + _executor: &Executor<'_, QueryContext>, + ) -> FieldResult<&NaiveDateTime> { + Ok(&self.human_time) + } + #[inline] fn field_pow_min(&self, _executor: &Executor<'_, QueryContext>) -> FieldResult<&i32> { Ok(&self.pow_min) } -- GitLab