blockchain/header: removed redundant epoch number

This commit is contained in:
skoupidi
2024-02-12 13:07:19 +02:00
parent 0d7d306a70
commit 7f0f954671
6 changed files with 27 additions and 46 deletions

View File

@@ -1 +1 @@
YGugtxfzw7nNYDoY49avu1EgWwsVbpi3u59x3MQ2pJjDGEU5QTzCEuHKasjmKzJTyEV1V1AVoC7TkSsh1iVJZCF8pPXrDiDbpCHANpbqjKUxrLoGBhoM1FD5eoFB9vZTuKMWRZad2iSzi6G8odmBsfJ8s4f6188PjBaqBhMrNi67pNcEibXRVTin4KJZqZ5crmXnk3rcZzJAtrZUEDiAkRSXQuUEqyHb5pLgrPHz6aqaUf5mThjBdUAw3ibct7zm5
jMzUeHqekqCSaE33631ze2H9K94hxHiUFsoTUgeTdme6WGZSmS9v6ntUmm8BCch7UUcfrQo48LFKBEECgQ7zeSYTfMW6CLqR9hMmWnnJFmysucuC1y5tFSCCARqkfVGJwwyYppNRfg6Bz7rwDpEbMebJuJrxEEzQAxhciocjYEyYsuJuJ5g6iFrWZRfGQAtDe67J7aX6T3c59fj6v53zXkM6Bm1Kc61R1YADWaLbNn7E3KeMW3pBVh

View File

@@ -1 +1 @@
YGugtxfzw7nNYDoY49avu1EgWwsVbpi3u59x3MQ2pJjDGEU5QTzCEuHKasjmKzJTyEV1V1AVoC7TkSsh1iVJZCF8pPXrDiDbpCHANpbqjKUxrLoGBhoM1FD5eoFB9vZTuKMWRZad2iSzi6G8odmBsfJ8s4f6188PjBaqBhMrNi67pNcEibXRVTin4KJZqZ5crmXnk3rcZzJAtrZUEDiAkRSXQuUEqyHb5pLgrPHz6aqaUf5mThjBdUAw3ibct7zm5
jMzUeHqekqCSaE33631ze2H9K94hxHiUFsoTUgeTdme6WGZSmS9v6ntUmm8BCch7UUcfrQo48LFKBEECgQ7zeSYTfMW6CLqR9hMmWnnJFmysucuC1y5tFSCCARqkfVGJwwyYppNRfg6Bz7rwDpEbMebJuJrxEEzQAxhciocjYEyYsuJuJ5g6iFrWZRfGQAtDe67J7aX6T3c59fj6v53zXkM6Bm1Kc61R1YADWaLbNn7E3KeMW3pBVh

View File

@@ -1 +1 @@
YGugtxfzw7nNYDoY49avu1EgWwsVbpi3u59x3MQ2pJjDGEU5QTzCEuHKasjmKzJTyEV1V1AVoC7TkSsh1iVJZCF8pPXrDiDbpCHANpbqjKUxrLoGBhoM1FD5eoFB9vZTuKMWRZad2iSzi6G8odmBsfJ8s4f6188PjBaqBhMrNi67pNcEibXRVTin4KJZqZ5crmXnk3rcZzJAtrZUEDiAkRSXQuUEqyHb5pLgrPHz6aqaUf5mThjBdUAw3ibct7zm5
jMzUeHqekqCSaE33631ze2H9K94hxHiUFsoTUgeTdme6WGZSmS9v6ntUmm8BCch7UUcfrQo48LFKBEECgQ7zeSYTfMW6CLqR9hMmWnnJFmysucuC1y5tFSCCARqkfVGJwwyYppNRfg6Bz7rwDpEbMebJuJrxEEzQAxhciocjYEyYsuJuJ5g6iFrWZRfGQAtDe67J7aX6T3c59fj6v53zXkM6Bm1Kc61R1YADWaLbNn7E3KeMW3pBVh

View File

@@ -64,12 +64,12 @@ new best fork.
## Ranking
Block producers create a reward transaction containing a `ECVRF` proof that
contributes to ranking logic. The `VRF` is built using the `pallas::Base` of
the $(n-1)$-block proposal's nonce, the $(n-2)$-block proposal's hash, and the
`pallas::Base` of the block proposal's block height. The `VRF`'s purpose is to
eliminate long range attacks by predicting a high-ranking future block that we
can produce in advance.
Block producers create a reward transaction containing a `ECVRF` proof (`VRF`)
that contributes to ranking logic. The `VRF` is built using the `pallas::Base`
of the $(n-1)$-block proposal's nonce, the $(n-2)$-block proposal's hash, and
the `pallas::Base` of the block proposal's block height. The `VRF`'s purpose
is to eliminate long range attacks by predicting a high-ranking future block
that we can produce in advance.
Each block proposal is ranked based on the modulus of the $(n-2)$-block
proposal's `VRF` proof (attached to the block producer's reward transaction)
@@ -107,12 +107,12 @@ block.
Upon receiving a block, one of the following cases may occur:
| Description | Handling |
|-------------------------------------------|---------------------------------------------------------------------|
| Block extends a known fork at its end | Append block to fork |
| Block extends a known fork not at its end | Create a new fork up to the extended block and append the new block |
| Block extends canonical blockchain | Create a new fork containing the new block |
| Block doesn't extend any known chain | Ignore block |
| Description | Handling |
|-----------------------------------------------|---------------------------------------------------------------------|
| Block extends a known fork at its end | Append block to fork |
| Block extends a known fork not at its end | Create a new fork up to the extended block and append the new block |
| Block extends canonical blockchain at its end | Create a new fork containing the new block |
| Block doesn't extend any known chain | Ignore block |
### Visual Examples
@@ -234,7 +234,6 @@ used by the protocol.
|-------------|----------------|------------------------------------------------|
| `version` | `u8` | Block version |
| `previous` | `blake3::Hash` | Previous block hash |
| `epoch` | `u64` | Epoch number |
| `height` | `u64` | Block height |
| `timestamp` | `Timestamp` | Block creation timestamp |
| `nonce` | `u64` | The block's nonce value |

View File

@@ -16,10 +16,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use darkfi_sdk::{
blockchain::{block_epoch, block_version},
crypto::MerkleTree,
};
use darkfi_sdk::{blockchain::block_version, crypto::MerkleTree};
#[cfg(feature = "async-serial")]
use darkfi_serial::async_trait;
@@ -29,15 +26,13 @@ use crate::{util::time::Timestamp, Error, Result};
use super::{parse_record, SledDbOverlayPtr};
/// This struct represents a tuple of the form (version, previous, epoch, height, timestamp, nonce, merkle_root).
/// This struct represents a tuple of the form (version, previous, height, timestamp, nonce, merkle_tree).
#[derive(Debug, Clone, PartialEq, Eq, SerialEncodable, SerialDecodable)]
pub struct Header {
/// Block version
pub version: u8,
/// Previous block hash
pub previous: blake3::Hash,
/// Epoch number
pub epoch: u64,
/// Block height
pub height: u64,
/// Block creation timestamp
@@ -51,9 +46,8 @@ pub struct Header {
impl Header {
pub fn new(previous: blake3::Hash, height: u64, timestamp: Timestamp, nonce: u64) -> Self {
let version = block_version(height);
let epoch = block_epoch(height);
let tree = MerkleTree::new(1);
Self { version, previous, epoch, height, timestamp, nonce, tree }
Self { version, previous, height, timestamp, nonce, tree }
}
/// Compute the header's hash
@@ -62,7 +56,6 @@ impl Header {
self.version.encode(&mut hasher)?;
self.previous.encode(&mut hasher)?;
self.epoch.encode(&mut hasher)?;
self.height.encode(&mut hasher)?;
self.timestamp.encode(&mut hasher)?;
self.nonce.encode(&mut hasher)?;

View File

@@ -19,7 +19,7 @@
use std::collections::HashMap;
use darkfi_sdk::{
blockchain::{block_epoch, block_version},
blockchain::block_version,
crypto::{
schnorr::SchnorrPublic, ContractId, MerkleTree, PublicKey, DEPLOYOOOR_CONTRACT_ID,
MONEY_CONTRACT_ID,
@@ -66,11 +66,6 @@ pub async fn verify_genesis_block(overlay: &BlockchainOverlayPtr, block: &BlockI
return Err(Error::BlockIsInvalid(block_hash))
}
// Block epoch must be correct
if block.header.epoch != block_epoch(block.header.height) {
return Err(Error::BlockIsInvalid(block_hash))
}
// Block version must be correct
if block.header.version != block_version(block.header.height) {
return Err(Error::BlockIsInvalid(block_hash))
@@ -116,11 +111,10 @@ pub async fn verify_genesis_block(overlay: &BlockchainOverlayPtr, block: &BlockI
/// A block is considered valid when the following rules apply:
/// 1. Block version is correct for its height
/// 2. Block epoch corresponds to the one for its height
/// 3. Parent hash is equal to the hash of the previous block
/// 4. Block height increments previous block height by 1
/// 5. Timestamp is valid based on PoWModule validation
/// 6. Block hash is valid based on PoWModule validation
/// 2. Parent hash is equal to the hash of the previous block
/// 3. Block height increments previous block height by 1
/// 4. Timestamp is valid based on PoWModule validation
/// 5. Block hash is valid based on PoWModule validation
/// Additional validity rules can be applied.
pub fn validate_block(block: &BlockInfo, previous: &BlockInfo, module: &PoWModule) -> Result<()> {
// Check block version (1)
@@ -128,28 +122,23 @@ pub fn validate_block(block: &BlockInfo, previous: &BlockInfo, module: &PoWModul
return Err(Error::BlockIsInvalid(block.hash()?.to_string()))
}
// Check block epoch (2)
if block.header.epoch != block_epoch(block.header.height) {
return Err(Error::BlockIsInvalid(block.hash()?.to_string()))
}
// Check previous hash (3)
// Check previous hash (2)
let previous_hash = previous.hash()?;
if block.header.previous != previous_hash {
return Err(Error::BlockIsInvalid(block.hash()?.to_string()))
}
// Check heights are incremental (4)
// Check heights are incremental (3)
if block.header.height != previous.header.height + 1 {
return Err(Error::BlockIsInvalid(block.hash()?.to_string()))
}
// Check timestamp validity (5)
// Check timestamp validity (4)
if !module.verify_timestamp_by_median(block.header.timestamp.0) {
return Err(Error::BlockIsInvalid(block.hash()?.to_string()))
}
// Check block hash corresponds to next one (6)
// Check block hash corresponds to next one (5)
module.verify_block_hash(block)?;
Ok(())