Skip to content

Commit

Permalink
Provide proposed timestamp along with tx
Browse files Browse the repository at this point in the history
When values are proposed to SCP, a timestamp is provided with the
proposed value. These timestamps are validated and consolidated to
populate the timestamp field of the `Block`.
  • Loading branch information
nick-mobilecoin committed Nov 20, 2023
1 parent 8544735 commit 8423ec7
Show file tree
Hide file tree
Showing 41 changed files with 1,617 additions and 443 deletions.
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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

0 comments on commit 8423ec7

Please sign in to comment.