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

Jaden/tipsetconversions #404

Merged
merged 14 commits into from
May 12, 2020
Merged

Jaden/tipsetconversions #404

merged 14 commits into from
May 12, 2020

Conversation

flodesi
Copy link
Contributor

@flodesi flodesi commented May 11, 2020

Summary of changes
Changes introduced in this pull request:

  • Remove result when converting from FullTipset to Tipset
  • Add From traits to convert from FullTipset to Tipset

Reference issue to close (if applicable)

Closes #398

@CLAassistant
Copy link

CLAassistant commented May 11, 2020

CLA assistant check
All committers have signed the CLA.

@flodesi flodesi marked this pull request as ready for review May 11, 2020 21:02
blockchain/blocks/src/tipset.rs Outdated Show resolved Hide resolved
blockchain/blocks/src/tipset.rs Outdated Show resolved Hide resolved
blockchain/blocks/src/tipset.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@austinabell austinabell left a comment

Choose a reason for hiding this comment

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

Approving because my suggested change is functionally the same, but I think it makes it a bit more readable

Comment on lines 176 to 190
pub fn new(blocks: Vec<Block>) -> Result<Self, Error> {
verify_blocks(blocks.iter().map(Block::header))?;
Ok(Self { blocks })

// sort blocks on creation to allow for more seamless conversions between FullTipset
// and Tipset
let mut sorted_blocks = blocks;
sorted_blocks.sort_by_key(|block| {
(
block.header.ticket().vrfproof.clone(),
block.header.cid().to_bytes(),
)
});
Ok(Self {
blocks: sorted_blocks,
})
Copy link
Contributor

Choose a reason for hiding this comment

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

I can't add a suggestion, because a few lines are outside of the changes, but can simplify to:

    pub fn new(mut blocks: Vec<Block>) -> Result<Self, Error> {
        verify_blocks(blocks.iter().map(Block::header))?;

        // sort blocks on creation to allow for more seamless conversions between FullTipset
        // and Tipset
        blocks.sort_by_key(|block| {
            (
                block.header.ticket().vrfproof.clone(),
                block.header.cid().to_bytes(),
            )
        });
        Ok(Self { blocks })
    }

Copy link
Contributor

Choose a reason for hiding this comment

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

Why aren't we only sorting by the ticket anyway? Aren't those already unique? (I saw that Tipset::new sorts by a tuple as well)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah didn't catch that, will add that change and push commit.

Copy link
Contributor

Choose a reason for hiding this comment

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

It is (or was) written into the spec to handle the secondary sorting. I'll try to find again, because quickly looking it is just referring to only sorting by ticket.

Copy link
Contributor

Choose a reason for hiding this comment

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

If tickets are guaranteed to be unique then it will never hit the second tuple element — if not, then it's indeed needed for a canonical ordering

Copy link
Contributor

Choose a reason for hiding this comment

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

The spec reads as follows:

The blocks in a tipset are canonically ordered by the lexicographic 
ordering of the bytes in each block’s ticket, breaking ties with the bytes
of the CID of the block itself.

My interpretation of that is that we order based on ticket size and if ticket sizes are equal we check the cid of the block itself.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks @dutterbutter for finding :) Yeah the comparison can still be improved by not cloning the ticket and only comparing cid bytes when other matches (having some sort function) if you want to take that on now @flodesi

Copy link
Contributor

Choose a reason for hiding this comment

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

My wording was a bit vague, I was questioning whether block.header.cid().to_bytes() was needed there as well. But you raise a good point, Ticket's ordering is defined by the ordering of its vrfproof so leaving out .vrfproof there will have no impact on the ordering whatsoever.

We unfortunately can't get rid of the .clone() though because of a language limitation (StackOverflow)

Copy link
Contributor

Choose a reason for hiding this comment

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

what I'm suggesting is not sorting by keys and just having a sort function, there should be no need to clone the ticket, only cids in the extremely rare case of a ticket match

Copy link
Contributor

Choose a reason for hiding this comment

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

@flodesi If you call max_by instead of max_by_key, you get in two blocks which lets you compare the tickets without cloning them, and only compare the CIDs if necessary like Austin suggested.

@flodesi flodesi requested a review from ec2 May 12, 2020 14:54
@flodesi flodesi merged commit fa12fff into master May 12, 2020
@flodesi flodesi deleted the jaden/tipsetconversions branch May 12, 2020 15:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve conversions between Tipset types
7 participants