Просмотр исходного кода

contract/dao: use actual runtime block target time not hardcoded one in blockwindow()

skoupidi 2 лет назад
Родитель
Сommit
b5cb5a55b7

+ 1 - 0
bin/darkfid/src/rpc.rs

@@ -61,6 +61,7 @@ impl RequestHandler for Darkfid {
             "blockchain.get_tx" => self.blockchain_get_tx(req.id, req.params).await,
             "blockchain.last_known_block" => self.blockchain_last_known_block(req.id, req.params).await,
             "blockchain.best_fork_next_block_height" => self.blockchain_best_fork_next_block_height(req.id, req.params).await,
+            "blockchain.block_target" => self.blockchain_block_target(req.id, req.params).await,
             "blockchain.lookup_zkas" => self.blockchain_lookup_zkas(req.id, req.params).await,
             "blockchain.subscribe_blocks" => self.blockchain_subscribe_blocks(req.id, req.params).await,
             "blockchain.subscribe_txs" =>  self.blockchain_subscribe_txs(req.id, req.params).await,

+ 22 - 0
bin/darkfid/src/rpc_blockchain.rs

@@ -169,6 +169,28 @@ impl Darkfid {
         JsonResponse::new(JsonValue::Number(next_block_height as f64), id).into()
     }
 
+    // RPCAPI:
+    // Queries the validator to get the currently configured block target time.
+    //
+    // **Params:**
+    // * `None`
+    //
+    // **Returns:**
+    // * `f64` Height of the last known block
+    //
+    // --> {"jsonrpc": "2.0", "method": "blockchain.block_target", "params": [], "id": 1}
+    // <-- {"jsonrpc": "2.0", "result": 1234, "id": 1}
+    pub async fn blockchain_block_target(&self, id: u16, params: JsonValue) -> JsonResult {
+        let params = params.get::<Vec<JsonValue>>().unwrap();
+        if !params.is_empty() {
+            return JsonError::new(InvalidParams, None, id).into()
+        }
+
+        let block_target = self.validator.consensus.module.read().await.target;
+
+        JsonResponse::new(JsonValue::Number(block_target as f64), id).into()
+    }
+
     // RPCAPI:
     // Initializes a subscription to new incoming blocks.
     // Once a subscription is established, `darkfid` will send JSON-RPC notifications of

+ 10 - 6
bin/drk/src/dao.rs

@@ -1516,9 +1516,11 @@ impl Drk {
             },
         ];
 
-        // Retrieve current block height and compute current day
-        let current_block_height = self.get_next_block_height().await?;
-        let creation_day = blockwindow(current_block_height);
+        // Retrieve next block height and current block time target,
+        // to compute their window.
+        let next_block_height = self.get_next_block_height().await?;
+        let block_target = self.get_block_target().await?;
+        let creation_day = blockwindow(next_block_height, block_target);
 
         // Create the actual proposal
         let proposal = DaoProposal {
@@ -1917,9 +1919,11 @@ impl Drk {
             inputs.push(input);
         }
 
-        // Retrieve current block height and compute current window
-        let current_block_height = self.get_next_block_height().await?;
-        let current_day = blockwindow(current_block_height);
+        // Retrieve next block height and current block time target,
+        // to compute their window.
+        let next_block_height = self.get_next_block_height().await?;
+        let block_target = self.get_block_target().await?;
+        let current_day = blockwindow(next_block_height, block_target);
 
         // Generate the Money nullifiers Sparse Merkle Tree
         let store = MemoryStorageFp { tree: proposal.nullifiers_smt_snapshot.unwrap() };

+ 5 - 3
bin/drk/src/main.rs

@@ -1479,9 +1479,11 @@ async fn realmain(args: Args, ex: Arc<smol::Executor<'static>>) -> Result<()> {
                     return drk.stop_rpc_client().await
                 }
 
-                // Retrieve current block height and compute current window
-                let current_block_height = drk.get_next_block_height().await?;
-                let current_window = blockwindow(current_block_height);
+                // Retrieve next block height and current block time target,
+                // to compute their window.
+                let next_block_height = drk.get_next_block_height().await?;
+                let block_target = drk.get_block_target().await?;
+                let current_window = blockwindow(next_block_height, block_target);
                 let end_time = proposal.proposal.creation_day + proposal.proposal.duration_days;
                 let (voting_status, proposal_status_message) = if current_window < end_time {
                     ("Ongoing", format!("Current proposal outcome: {outcome}"))

+ 10 - 0
bin/drk/src/rpc.rs

@@ -395,4 +395,14 @@ impl Drk {
 
         Ok(next_height)
     }
+
+    /// Queries darkfid for currently configured block target time.
+    pub async fn get_block_target(&self) -> Result<u32> {
+        let req = JsonRequest::new("blockchain.block_target", JsonValue::Array(vec![]));
+        let rep = self.rpc_client.as_ref().unwrap().request(req).await?;
+
+        let next_height = *rep.get::<f64>().unwrap() as u32;
+
+        Ok(next_height)
+    }
 }

+ 2 - 1
src/contract/dao/src/entrypoint/propose.rs

@@ -86,7 +86,8 @@ pub(crate) fn dao_propose_get_metadata(
     }
 
     // ANCHOR: dao-blockwindow-example-usage
-    let current_day = blockwindow(wasm::util::get_verifying_block_height()?);
+    let current_day =
+        blockwindow(wasm::util::get_verifying_block_height()?, wasm::util::get_block_target()?);
     // ANCHOR_END: dao-blockwindow-example-usage
 
     let total_funds_coords = total_funds_commit.to_affine().coordinates().unwrap();

+ 2 - 1
src/contract/dao/src/entrypoint/vote.rs

@@ -89,7 +89,8 @@ pub(crate) fn dao_vote_get_metadata(
         ));
     }
 
-    let current_day = blockwindow(wasm::util::get_verifying_block_height()?);
+    let current_day =
+        blockwindow(wasm::util::get_verifying_block_height()?, wasm::util::get_block_target()?);
 
     let yes_vote_commit_coords = params.yes_vote_commit.to_affine().coordinates().unwrap();
     let all_vote_commit_coords = all_vote_commit.to_affine().coordinates().unwrap();

+ 3 - 4
src/contract/dao/src/lib.rs

@@ -93,13 +93,12 @@ pub const DAO_CONTRACT_ZKAS_DAO_AUTH_MONEY_TRANSFER_ENC_COIN_NS: &str = "AuthMon
 pub const PROPOSAL_SNAPSHOT_CUTOFF_LIMIT: u32 = 100;
 
 // ANCHOR: dao-blockwindow
-const BLOCK_TIME: u64 = 90;
 const SECS_IN_HOUR: u64 = 60 * 60;
 const WINDOW_TIME_HR: u64 = 4;
 
-/// Blockwindow from blockheight. Used for time limit on DAO proposals.
-pub fn blockwindow(height: u32) -> u64 {
-    let timestamp_secs = height as u64 * BLOCK_TIME;
+/// Blockwindow from block height and target time. Used for time limit on DAO proposals.
+pub fn blockwindow(height: u32, target: u32) -> u64 {
+    let timestamp_secs = (height * target) as u64;
     timestamp_secs / (WINDOW_TIME_HR * SECS_IN_HOUR)
 }
 // ANCHOR_END: dao-blockwindow

+ 2 - 1
src/contract/test-harness/src/dao_propose.rs

@@ -117,7 +117,8 @@ impl TestHarness {
             },
         ];
 
-        let creation_day = blockwindow(block_height);
+        let block_target = wallet.validator.consensus.module.read().await.target;
+        let creation_day = blockwindow(block_height, block_target);
         let proposal = DaoProposal {
             auth_calls,
             creation_day,

+ 2 - 1
src/contract/test-harness/src/dao_vote.rs

@@ -81,7 +81,8 @@ impl TestHarness {
             signature_secret,
         };
 
-        let current_day = blockwindow(block_height);
+        let block_target = wallet.validator.consensus.module.read().await.target;
+        let current_day = blockwindow(block_height, block_target);
         let call = DaoVoteCall {
             money_null_smt: wallet.money_null_smt_snapshot.as_ref().unwrap(),
             inputs: vec![input],