Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add timestamp field to Block #3734

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions api/proto/blockchain.proto
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ message Block {

// Hash of the block's contents.
BlockContentsHash contents_hash = 7;

// The block's timestamp. ms since Unix epoch
uint64 timestamp = 8;
}

message BlockContents {
Expand Down
7 changes: 7 additions & 0 deletions api/src/convert/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ impl From<&Block> for blockchain::Block {
block.set_cumulative_txo_count(other.cumulative_txo_count);
block.set_root_element((&other.root_element).into());
block.set_contents_hash(blockchain::BlockContentsHash::from(&other.contents_hash));
block.set_timestamp(other.timestamp);
block
}
}
Expand All @@ -39,6 +40,7 @@ impl TryFrom<&blockchain::Block> for Block {
cumulative_txo_count: value.cumulative_txo_count,
root_element,
contents_hash,
timestamp: value.timestamp,
};
Ok(block)
}
Expand All @@ -65,6 +67,7 @@ mod tests {
hash: TxOutMembershipHash::from([12u8; 32]),
},
contents_hash: BlockContentsHash::try_from(&[66u8; 32][..]).unwrap(),
timestamp: 357,
};

let block = blockchain::Block::from(&source_block);
Expand All @@ -77,6 +80,7 @@ mod tests {
assert_eq!(block.get_root_element().get_range().get_to(), 20);
assert_eq!(block.get_root_element().get_hash().get_data(), &[12u8; 32]);
assert_eq!(block.get_contents_hash().get_data(), [66u8; 32]);
assert_eq!(block.get_timestamp(), 357);
}

#[test]
Expand All @@ -103,6 +107,7 @@ mod tests {
source_block.set_index(2);
source_block.set_root_element(root_element);
source_block.set_contents_hash(contents_hash);
source_block.set_timestamp(411);

let block = Block::try_from(&source_block).unwrap();
assert_eq!(block.id.as_ref(), [10u8; 32]);
Expand All @@ -113,6 +118,7 @@ mod tests {
assert_eq!(block.root_element.range.to, 20);
assert_eq!(block.root_element.hash.as_ref(), &[13u8; 32]);
assert_eq!(block.contents_hash.as_ref(), [66u8; 32]);
assert_eq!(block.timestamp, 411);
}

#[test]
Expand All @@ -131,6 +137,7 @@ mod tests {
hash: TxOutMembershipHash::from([12u8; 32]),
},
contents_hash: BlockContentsHash::try_from(&[66u8; 32][..]).unwrap(),
timestamp: 911,
};

// Encode using `protobuf`, decode using `prost`.
Expand Down
14 changes: 13 additions & 1 deletion blockchain/test-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,18 @@ pub fn get_blocks_with_recipients<R: RngCore + CryptoRng>(

let block = match &prev_block {
Some(parent) => {
Block::new_with_parent(block_version, parent, &Default::default(), &block_contents)
let timestamp = if block_version.timestamps_are_supported() {
parent.timestamp + 1
} else {
0
};
Block::new_with_parent(
block_version,
parent,
&Default::default(),
&block_contents,
timestamp,
)
}
None => Block::new_origin_block(&block_contents.outputs),
};
Expand Down Expand Up @@ -242,6 +253,7 @@ mod tests {
block.cumulative_txo_count,
&block.root_element,
&block.contents_hash,
block.timestamp,
);
assert_eq!(derived_block_id, block.id);

Expand Down
Loading
Loading