Skip to content

Commit

Permalink
feat: make block timestamp repr time block found
Browse files Browse the repository at this point in the history
Closes #149.

The mining loop now updates block_header.timestamp with current time
before calculating each digest.  This means that the timestamp will
represent the moment a block is found, rather than the moment mining
started, as it was before.

Changes:
 * adds Block::set_header_timestamp()
 * call Block::set_header_timestamp(Timestamp::now()) in mining loop

The test `block_timestamp_represents_time_block_found()` now passes.
  • Loading branch information
dan-da committed May 8, 2024
1 parent 318b7a2 commit 5f9c9e8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/mine_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,12 @@ fn mine_block_worker(
// mutate nonce in the block's header.
// Block::hash() will subsequently return a new digest.
block.set_header_nonce(rng.gen());

// See issue #149 and test block_timestamp_represents_time_block_found()
// this ensures header timestamp represents the moment block is found.
// this is simplest impl. Efficiencies can perhaps be gained by only
// performing every N iterations, or other strategies.
block.set_header_timestamp(Timestamp::now());
}

let nonce = block.kernel.header.nonce;
Expand Down Expand Up @@ -669,6 +675,9 @@ mod mine_loop_tests {
///
/// This is a regression test for issue #149.
/// https://github.com/Neptune-Crypto/neptune-core/issues/149
///
/// note: this test fails in 318b7a20baf11a7a99f249660f1f70484c586012
/// and should always pass in later commits.
#[traced_test]
#[tokio::test]
async fn block_timestamp_represents_time_block_found() -> Result<()> {
Expand Down
10 changes: 10 additions & 0 deletions src/models/blockchain/block/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,21 @@ impl Block {
/// sets header header nonce.
///
/// note: this causes block digest to change.
#[inline]
pub fn set_header_nonce(&mut self, nonce: [BFieldElement; 3]) {
self.kernel.header.nonce = nonce;
self.unset_digest();
}

/// sets header timestamp.
///
/// note: this causes block digest to change.
#[inline]
pub fn set_header_timestamp(&mut self, timestamp: Timestamp) {
self.kernel.header.timestamp = timestamp;
self.unset_digest();
}

#[inline]
pub fn header(&self) -> &BlockHeader {
&self.kernel.header
Expand Down

0 comments on commit 5f9c9e8

Please sign in to comment.