From 95434701f45dae331bb497ee7e014e4b8f309a37 Mon Sep 17 00:00:00 2001 From: aggstam Date: Tue, 3 Oct 2023 16:38:30 +0300 Subject: [PATCH] blockchain/Block: block version to derive from cuttof slot, not hardcoded value --- bin/darkfid2/src/task/miner.rs | 3 --- src/blockchain/block_store.rs | 3 --- src/blockchain/header_store.rs | 5 +++-- src/sdk/src/blockchain.rs | 10 ++++++++++ 4 files changed, 13 insertions(+), 8 deletions(-) diff --git a/bin/darkfid2/src/task/miner.rs b/bin/darkfid2/src/task/miner.rs index b4443183b..54d30414d 100644 --- a/bin/darkfid2/src/task/miner.rs +++ b/bin/darkfid2/src/task/miner.rs @@ -70,9 +70,6 @@ async fn miner_loop(node: &Darkfid, stop_signal: &Receiver<()>) -> Result<()> { // Mine next block let difficulty = module.next_difficulty(); - // TODO: BlockInfo::default() should be based on block version - // TODO: block version should be derived from cuttoff const, not - // hardcoded BLOCK_VERSION let mut next_block = BlockInfo::default(); next_block.header.version = 0; next_block.header.previous = last.hash()?; diff --git a/src/blockchain/block_store.rs b/src/blockchain/block_store.rs index fb9f106fa..b21f4957a 100644 --- a/src/blockchain/block_store.rs +++ b/src/blockchain/block_store.rs @@ -30,9 +30,6 @@ use crate::{tx::Transaction, Error, Result}; use super::{parse_record, parse_u64_key_record, Header, SledDbOverlayPtr}; -/// Block version number -pub const BLOCK_VERSION: u8 = 1; - /// This struct represents a tuple of the form (`header`, `txs`, `signature`). /// The header and transactions are stored as hashes, serving as pointers to the actual data /// in the sled database. diff --git a/src/blockchain/header_store.rs b/src/blockchain/header_store.rs index d7d386f12..2edcdc344 100644 --- a/src/blockchain/header_store.rs +++ b/src/blockchain/header_store.rs @@ -17,6 +17,7 @@ */ use darkfi_sdk::{ + blockchain::block_version, crypto::MerkleTree, pasta::{group::ff::Field, pallas}, }; @@ -27,7 +28,7 @@ use darkfi_serial::{deserialize, serialize, Encodable, SerialDecodable, SerialEn use crate::{util::time::Timestamp, Error, Result}; -use super::{block_store::BLOCK_VERSION, parse_record, SledDbOverlayPtr}; +use super::{parse_record, SledDbOverlayPtr}; /// This struct represents a tuple of the form (version, previous, epoch, height, timestamp, nonce, merkle_root). #[derive(Debug, Clone, PartialEq, Eq, SerialEncodable, SerialDecodable)] @@ -58,7 +59,7 @@ impl Header { timestamp: Timestamp, nonce: pallas::Base, ) -> Self { - let version = BLOCK_VERSION; + let version = block_version(height); let tree = MerkleTree::new(1); Self { version, previous, epoch, height, timestamp, nonce, tree } } diff --git a/src/sdk/src/blockchain.rs b/src/sdk/src/blockchain.rs index c924a0aa7..007b57ef5 100644 --- a/src/sdk/src/blockchain.rs +++ b/src/sdk/src/blockchain.rs @@ -121,6 +121,16 @@ impl Default for Slot { // TODO: This values are experimental, should be replaced with the proper ones once defined pub const POW_CUTOFF: u64 = 1000000; pub const POS_START: u64 = 1000001; + +/// Auxiliary function to calculate provided block height(slot) block version. +/// PoW blocks use version 1, while PoS ones use version 2. +pub fn block_version(height: u64) -> u8 { + match height { + 0..=POW_CUTOFF => 1, + POS_START.. => 2, + } +} + /// Auxiliary function to calculate provided block height(slot) expected reward value. /// Genesis slot(0) always returns reward value 0. /// We use PoW bootstrap, configured to reduce rewards at fixed height numbers, until a cutoff.