Repository: MystenLabs/sui
File: consensus/core/src/block.rs (Rust)

Code before:
                ..Default::default()
            },
        }
    }

    pub fn set_epoch(mut self, epoch: Epoch) -> Self {
        self.block.epoch = epoch;
        self
    }

    pub fn set_round(mut self, round: Round) -> Self {
        self.block.round = round;
        self
    }

    pub fn set_author(mut self, author: AuthorityIndex) -> Self {
        self.block.author = author;
        self
    }

    pub fn set_timestamp_ms(mut self, timestamp_ms: BlockTimestampMs) -> Self {
        self.block.timestamp_ms = timestamp_ms;
        self
    }

    /// Sorts then sets ancestors in the TestBlock.
    pub fn set_ancestors(mut self, mut ancestors: Vec<BlockRef>) -> Self {
        ancestors.sort_by(|a, b| {
            // Put own authority ancestor at the front.
            if a.author == self.block.author {
                return std::cmp::Ordering::Less;
            }
            if b.author == self.block.author {
                return std::cmp::Ordering::Greater;
            }
            a.author.cmp(&b.author)
        });
        self.block.ancestors = ancestors;
        self
    }

    /// Sets ancestors in the TestBlock exactly as provided.
    pub fn set_ancestors_raw(mut self, ancestors: Vec<BlockRef>) -> Self {
        self.block.ancestors = ancestors;
        self
    }

    pub fn set_transactions(mut self, transactions: Vec<Transaction>) -> Self {
        self.block.transactions = transactions;
        self
    }

Review comment (security):
```suggestion
    /// Sorts then sets ancestors in the TestBlock to make sure that author's own block is always first as expected by the rest of the system.
```

Code after:
                ..Default::default()
            },
        }
    }

    pub fn set_epoch(mut self, epoch: Epoch) -> Self {
        self.block.epoch = epoch;
        self
    }

    pub fn set_round(mut self, round: Round) -> Self {
        self.block.round = round;
        self
    }

    pub fn set_author(mut self, author: AuthorityIndex) -> Self {
        self.block.author = author;
        self
    }

    pub fn set_timestamp_ms(mut self, timestamp_ms: BlockTimestampMs) -> Self {
        self.block.timestamp_ms = timestamp_ms;
        self
    }

    /// Sorts then sets ancestors in the TestBlock.
    /// Author's own block is always first, which is expected by BlockVerifier and
    /// the rest of the system.
    pub fn set_ancestors(mut self, mut ancestors: Vec<BlockRef>) -> Self {
        ancestors.sort_by(|a, b| {
            if a.author == self.block.author {
                return std::cmp::Ordering::Less;
            }
            if b.author == self.block.author {
                return std::cmp::Ordering::Greater;
            }
            a.author.cmp(&b.author)
        });
        self.block.ancestors = ancestors;
        self
    }

    /// Sets ancestors in the TestBlock exactly as provided.
    pub fn set_ancestors_raw(mut self, ancestors: Vec<BlockRef>) -> Self {
        self.block.ancestors = ancestors;
        self
    }

    pub fn set_transactions(mut self, transactions: Vec<Transaction>) -> Self {
        self.block.transactions = transactions;
        self