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

src: moved SlotCheckpoint to sdk so it can be used by contracts

aggstam 3 лет назад
Родитель
Сommit
7deaab24e1

+ 2 - 1
src/blockchain/mod.rs

@@ -20,10 +20,11 @@ use std::sync::{Arc, Mutex};
 
 use log::debug;
 
+use darkfi_sdk::blockchain::SlotCheckpoint;
 use darkfi_serial::serialize;
 
 use crate::{
-    consensus::{Block, BlockInfo, SlotCheckpoint},
+    consensus::{Block, BlockInfo},
     tx::Transaction,
     util::time::Timestamp,
     Result,

+ 2 - 1
src/blockchain/slot_checkpoint_store.rs

@@ -16,9 +16,10 @@
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 
+use darkfi_sdk::blockchain::SlotCheckpoint;
 use darkfi_serial::{deserialize, serialize};
 
-use crate::{blockchain::SledDbOverlayPtr, consensus::SlotCheckpoint, Error, Result};
+use crate::{blockchain::SledDbOverlayPtr, Error, Result};
 
 const SLED_SLOT_CHECKPOINT_TREE: &[u8] = b"_slot_checkpoints";
 

+ 0 - 1
src/consensus/mod.rs

@@ -33,7 +33,6 @@ pub use lead_info::{LeadInfo, LeadProof};
 
 /// Consensus state
 pub mod state;
-pub use state::SlotCheckpoint;
 
 /// Consensus validator state
 pub mod validator;

+ 3 - 1
src/consensus/proto/protocol_sync.rs

@@ -21,10 +21,12 @@ use async_trait::async_trait;
 use log::{debug, error, info};
 use smol::Executor;
 
+use darkfi_sdk::blockchain::SlotCheckpoint;
+
 use crate::{
     consensus::{
         block::{BlockInfo, BlockOrder, BlockResponse},
-        state::{SlotCheckpoint, SlotCheckpointRequest, SlotCheckpointResponse},
+        state::{SlotCheckpointRequest, SlotCheckpointResponse},
         ValidatorStatePtr,
     },
     net::{

+ 2 - 46
src/consensus/state.rs

@@ -17,7 +17,8 @@
  */
 
 use darkfi_sdk::{
-    crypto::{pasta_prelude::*, MerkleTree},
+    blockchain::SlotCheckpoint,
+    crypto::MerkleTree,
     pasta::{group::ff::PrimeField, pallas},
 };
 use darkfi_serial::{deserialize, serialize, SerialDecodable, SerialEncodable};
@@ -696,51 +697,6 @@ impl net::Message for ConsensusSlotCheckpointsResponse {
     }
 }
 
-/// Auxiliary structure used to keep track of slot validation parameters.
-#[derive(Debug, Clone, SerialEncodable, SerialDecodable)]
-pub struct SlotCheckpoint {
-    /// Slot UID
-    pub slot: u64,
-    /// Previous slot eta
-    pub previous_eta: pallas::Base,
-    /// Previous slot forks last proposal/block hashes,
-    /// as observed by the validator
-    pub fork_hashes: Vec<blake3::Hash>,
-    /// Previous slot second to last proposal/block hashes,
-    /// as observed by the validator
-    pub fork_previous_hashes: Vec<blake3::Hash>,
-    /// Slot sigma1
-    pub sigma1: pallas::Base,
-    /// Slot sigma2
-    pub sigma2: pallas::Base,
-}
-
-impl SlotCheckpoint {
-    pub fn new(
-        slot: u64,
-        previous_eta: pallas::Base,
-        fork_hashes: Vec<blake3::Hash>,
-        fork_previous_hashes: Vec<blake3::Hash>,
-        sigma1: pallas::Base,
-        sigma2: pallas::Base,
-    ) -> Self {
-        Self { slot, previous_eta, fork_hashes, fork_previous_hashes, sigma1, sigma2 }
-    }
-
-    /// Generate the genesis slot checkpoint.
-    pub fn genesis_slot_checkpoint(genesis_block: blake3::Hash) -> Self {
-        let previous_eta = pallas::Base::ZERO;
-        let fork_hashes = vec![];
-        // Since genesis block has no previous,
-        // we will use its own hash as its previous.
-        let fork_previous_hashes = vec![genesis_block];
-        let sigma1 = pallas::Base::ZERO;
-        let sigma2 = pallas::Base::ZERO;
-
-        Self::new(0, previous_eta, fork_hashes, fork_previous_hashes, sigma1, sigma2)
-    }
-}
-
 impl net::Message for SlotCheckpoint {
     fn name() -> &'static str {
         "slotcheckpoint"

+ 2 - 1
src/consensus/validator.rs

@@ -20,6 +20,7 @@ use std::{collections::HashMap, io::Cursor};
 
 use async_std::sync::{Arc, RwLock};
 use darkfi_sdk::{
+    blockchain::SlotCheckpoint,
     crypto::{
         contract_id::{CONSENSUS_CONTRACT_ID, DAO_CONTRACT_ID, MONEY_CONTRACT_ID},
         schnorr::{SchnorrPublic, SchnorrSecret},
@@ -36,7 +37,7 @@ use serde_json::json;
 use super::{
     constants,
     lead_coin::LeadCoin,
-    state::{ConsensusState, Fork, SlotCheckpoint, StateCheckpoint},
+    state::{ConsensusState, Fork, StateCheckpoint},
     BlockInfo, BlockProposal, Header, LeadInfo, LeadProof,
 };
 

+ 1 - 1
src/contract/consensus/src/client/proposal_v1.rs

@@ -19,7 +19,6 @@
 //! This API is crufty. Please rework it into something nice to read and nice to use.
 
 use darkfi::{
-    consensus::SlotCheckpoint,
     zk::{halo2::Value, Proof, ProvingKey, Witness, ZkCircuit},
     zkas::ZkBinary,
     Result,
@@ -29,6 +28,7 @@ use darkfi_money_contract::{
     model::{Coin, ConsensusInput, ConsensusOutput},
 };
 use darkfi_sdk::{
+    blockchain::SlotCheckpoint,
     bridgetree::Hashable,
     crypto::{
         ecvrf::VrfProof, note::AeadEncryptedNote, pasta_prelude::*, pedersen_commitment_u64,

+ 3 - 2
src/contract/consensus/src/entrypoint/proposal_v1.rs

@@ -23,6 +23,7 @@ use darkfi_money_contract::{
     CONSENSUS_CONTRACT_ZKAS_PROPOSAL_NS_V1,
 };
 use darkfi_sdk::{
+    blockchain::SlotCheckpoint,
     crypto::{pasta_prelude::*, pedersen_commitment_u64, poseidon_hash, ContractId, MerkleNode},
     db::{db_contains_key, db_lookup, db_set},
     error::{ContractError, ContractResult},
@@ -36,8 +37,8 @@ use darkfi_serial::{deserialize, serialize, Encodable, WriteExt};
 use crate::{
     error::ConsensusError,
     model::{
-        ConsensusProposalParamsV1, ConsensusProposalUpdateV1, SlotCheckpoint, GRACE_PERIOD,
-        HEADSTART, MU_RHO_PREFIX, MU_Y_PREFIX, REWARD, REWARD_PALLAS,
+        ConsensusProposalParamsV1, ConsensusProposalUpdateV1, GRACE_PERIOD, HEADSTART,
+        MU_RHO_PREFIX, MU_Y_PREFIX, REWARD, REWARD_PALLAS,
     },
     ConsensusFunction,
 };

+ 0 - 20
src/contract/consensus/src/model.rs

@@ -114,26 +114,6 @@ pub const HEADSTART: pallas::Base = pallas::Base::from_raw([
     46116860184273879,
 ]);
 
-/// Auxiliary structure to decode `darkfi::consensus::state::SlotCheckpoint`
-/// to use in contract.
-#[derive(SerialDecodable)]
-pub struct SlotCheckpoint {
-    /// Slot UID
-    pub slot: u64,
-    /// Previous slot eta
-    pub previous_eta: pallas::Base,
-    /// Previous slot forks last proposal/block hashes,
-    /// as observed by the validator
-    pub fork_hashes: Vec<blake3::Hash>,
-    /// Previous slot second to last proposal/block hashes,
-    /// as observed by the validator
-    pub fork_previous_hashes: Vec<blake3::Hash>,
-    /// Slot sigma1
-    pub sigma1: pallas::Base,
-    /// Slot sigma2
-    pub sigma2: pallas::Base,
-}
-
 /// Auxiliary function to calculate the grace (locked) period, denominated
 /// in epochs.
 #[inline]

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

@@ -18,13 +18,14 @@
 
 use std::time::Instant;
 
-use darkfi::{consensus::SlotCheckpoint, tx::Transaction, Result};
+use darkfi::{tx::Transaction, Result};
 use darkfi_consensus_contract::{
     client::proposal_v1::ConsensusProposalCallBuilder, model::ConsensusProposalParamsV1,
     ConsensusFunction,
 };
 use darkfi_money_contract::{client::ConsensusOwnCoin, CONSENSUS_CONTRACT_ZKAS_PROPOSAL_NS_V1};
 use darkfi_sdk::{
+    blockchain::SlotCheckpoint,
     crypto::{MerkleNode, SecretKey, CONSENSUS_CONTRACT_ID},
     ContractCall,
 };

+ 7 - 4
src/contract/test-harness/src/lib.rs

@@ -19,7 +19,7 @@
 use std::collections::HashMap;
 
 use darkfi::{
-    consensus::{SlotCheckpoint, TESTNET_GENESIS_HASH_BYTES, TESTNET_GENESIS_TIMESTAMP},
+    consensus::{TESTNET_GENESIS_HASH_BYTES, TESTNET_GENESIS_TIMESTAMP},
     runtime::vm_runtime::SMART_CONTRACT_ZKAS_DB_NAME,
     util::time::TimeKeeper,
     validator::{Validator, ValidatorConfig, ValidatorPtr},
@@ -41,9 +41,12 @@ use darkfi_money_contract::{
     MONEY_CONTRACT_ZKAS_MINT_NS_V1, MONEY_CONTRACT_ZKAS_TOKEN_FRZ_NS_V1,
     MONEY_CONTRACT_ZKAS_TOKEN_MINT_NS_V1,
 };
-use darkfi_sdk::crypto::{
-    poseidon_hash, Keypair, MerkleNode, MerkleTree, Nullifier, PublicKey, SecretKey,
-    CONSENSUS_CONTRACT_ID, DAO_CONTRACT_ID, MONEY_CONTRACT_ID,
+use darkfi_sdk::{
+    blockchain::SlotCheckpoint,
+    crypto::{
+        poseidon_hash, Keypair, MerkleNode, MerkleTree, Nullifier, PublicKey, SecretKey,
+        CONSENSUS_CONTRACT_ID, DAO_CONTRACT_ID, MONEY_CONTRACT_ID,
+    },
 };
 use darkfi_serial::{deserialize, serialize};
 use log::{info, warn};

+ 65 - 0
src/sdk/src/blockchain.rs

@@ -0,0 +1,65 @@
+/* This file is part of DarkFi (https://dark.fi)
+ *
+ * Copyright (C) 2020-2023 Dyne.org foundation
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ */
+
+use darkfi_serial::{SerialDecodable, SerialEncodable};
+use pasta_curves::{group::ff::Field, pallas};
+
+/// Auxiliary structure used to keep track of slot validation parameters.
+#[derive(Debug, Clone, SerialEncodable, SerialDecodable)]
+pub struct SlotCheckpoint {
+    /// Slot UID
+    pub slot: u64,
+    /// Previous slot eta
+    pub previous_eta: pallas::Base,
+    /// Previous slot forks last proposal/block hashes,
+    /// as observed by the validator
+    pub fork_hashes: Vec<blake3::Hash>,
+    /// Previous slot second to last proposal/block hashes,
+    /// as observed by the validator
+    pub fork_previous_hashes: Vec<blake3::Hash>,
+    /// Slot sigma1
+    pub sigma1: pallas::Base,
+    /// Slot sigma2
+    pub sigma2: pallas::Base,
+}
+
+impl SlotCheckpoint {
+    pub fn new(
+        slot: u64,
+        previous_eta: pallas::Base,
+        fork_hashes: Vec<blake3::Hash>,
+        fork_previous_hashes: Vec<blake3::Hash>,
+        sigma1: pallas::Base,
+        sigma2: pallas::Base,
+    ) -> Self {
+        Self { slot, previous_eta, fork_hashes, fork_previous_hashes, sigma1, sigma2 }
+    }
+
+    /// Generate the genesis slot checkpoint.
+    pub fn genesis_slot_checkpoint(genesis_block: blake3::Hash) -> Self {
+        let previous_eta = pallas::Base::ZERO;
+        let fork_hashes = vec![];
+        // Since genesis block has no previous,
+        // we will use its own hash as its previous.
+        let fork_previous_hashes = vec![genesis_block];
+        let sigma1 = pallas::Base::ZERO;
+        let sigma2 = pallas::Base::ZERO;
+
+        Self::new(0, previous_eta, fork_hashes, fork_previous_hashes, sigma1, sigma2)
+    }
+}

+ 4 - 0
src/sdk/src/lib.rs

@@ -21,6 +21,10 @@ pub use num_bigint;
 pub use num_traits;
 pub use pasta_curves as pasta;
 
+/// Blockchain structures
+pub mod blockchain;
+pub use blockchain::SlotCheckpoint;
+
 /// Database functions
 pub mod db;
 

+ 1 - 1
src/validator/mod.rs

@@ -20,6 +20,7 @@ use std::{collections::HashMap, io::Cursor};
 
 use async_std::sync::{Arc, RwLock};
 use darkfi_sdk::{
+    blockchain::SlotCheckpoint,
     crypto::{PublicKey, CONSENSUS_CONTRACT_ID, DAO_CONTRACT_ID, MONEY_CONTRACT_ID},
     pasta::pallas,
 };
@@ -28,7 +29,6 @@ use log::{debug, error, info, warn};
 
 use crate::{
     blockchain::{Blockchain, BlockchainOverlay, BlockchainOverlayPtr},
-    consensus::SlotCheckpoint,
     error::TxVerifyFailed,
     runtime::vm_runtime::Runtime,
     tx::Transaction,