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

fix: stop MTP attack #3357

Merged
merged 1 commit into from
Sep 16, 2021

Conversation

SWvheerden
Copy link
Collaborator

@SWvheerden SWvheerden commented Sep 16, 2021

Description

This changes the timestamp of a new block template to be always greater than the median time past (MTP) of the past blocks as per consensus.

Motivation and Context

It is possible for an attack to forward the median time to be greater than now, but less than the future time limit(FTL). This means that all valid blocks with the current timestamp will be rejected. This will ensure that ll newly created blocks always have a timestamp greater than the MTP. Due to consensus we also know that MTP will always be less than FTL. This ensures that all new block timestamps are MTP < Timestamp < FTL

For a description see: Graft network
and MTP Attack (Jagernan)

How Has This Been Tested?

All current unit tests pass.

@@ -643,7 +650,24 @@ where B: BlockchainBackend
pub fn prepare_block_merkle_roots(&self, template: NewBlockTemplate) -> Result<Block, ChainStorageError> {
let NewBlockTemplate { header, mut body, .. } = template;
body.sort();
let header = BlockHeader::from(header);
let mut header = BlockHeader::from(header);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method now does more than it says it does. It should be renamed or this logic should be moved elsewhere

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I renamed the method to prepare_new_block. The timestamp was already set by this method, it was done inside of let header = BlockHeader::from(header);. All it does not is add an extra check to what it should set it and not just blanketly set it to now. But I do agree with the fact that the function name and what it does is misaligned.

sdbondi
sdbondi previously approved these changes Sep 16, 2021
Copy link
Member

@sdbondi sdbondi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch - some nits that you can safely ignore :)

self.timestamp = timestamp;
// if the timestamp has been advanced by the base_node due to the median time we should not reverse it but we
// should only change the timestamp if we move it forward.
if timestamp > self.timestamp {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I'd rather go for putting the check outside the function call (it's only used in one place anyhow). It isn't expected for set_timestamp not to set the timestamp. Alternatively, name the function set_forward_timestamp (or something) and explain in a docstring that is only sets the timestamp forward from what it is. This is a nit so feel free to ignore.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree good idea

@@ -643,7 +650,24 @@ where B: BlockchainBackend
pub fn prepare_block_merkle_roots(&self, template: NewBlockTemplate) -> Result<Block, ChainStorageError> {
let NewBlockTemplate { header, mut body, .. } = template;
body.sort();
let header = BlockHeader::from(header);
let mut header = BlockHeader::from(header);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: We may as well call this function prepare_new_block(NewBlocktemplate) -> Block

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, will rename. That is what the function did anyway. All we do now is we do an extra check before we set the timestamp. Before this, the set_timestamp happened in let header = BlockHeader::from(header);

rename merkle root function
@aviator-app aviator-app bot merged commit a82638a into tari-project:development Sep 16, 2021
@SWvheerden SWvheerden deleted the sw_stop_MTP_attack branch September 17, 2021 06:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants