Skip to content

Commit

Permalink
Lints
Browse files Browse the repository at this point in the history
  • Loading branch information
ebatsell committed Dec 2, 2024
1 parent 129acda commit 0fd6f26
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 28 deletions.
31 changes: 26 additions & 5 deletions core/src/ballot_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,10 @@ impl BallotBox {
self.slot_consensus_reached() > 0 || self.winning_ballot.is_valid()
}

pub fn tie_breaker_set(&self) -> bool {
self.slot_consensus_reached() == 0 && self.winning_ballot.is_valid()
}

pub fn get_winning_ballot(&self) -> Result<Ballot, TipRouterError> {
if self.winning_ballot.is_valid() {
Ok(self.winning_ballot)
Expand Down Expand Up @@ -351,7 +355,7 @@ impl BallotBox {
current_slot: u64,
valid_slots_after_consensus: u64,
) -> Result<(), TipRouterError> {
if !self.is_voting_valid(current_slot, valid_slots_after_consensus) {
if !self.is_voting_valid(current_slot, valid_slots_after_consensus)? {
return Err(TipRouterError::VotingNotValid);
}

Expand Down Expand Up @@ -442,7 +446,12 @@ impl BallotBox {
}

// Check if voting is stalled and setting the tie breaker is eligible
if current_epoch < self.epoch() + epochs_before_stall {
if current_epoch
< self
.epoch()
.checked_add(epochs_before_stall)
.ok_or(TipRouterError::ArithmeticOverflow)?
{
return Err(TipRouterError::VotingNotFinalized);
}

Expand All @@ -461,9 +470,21 @@ impl BallotBox {
self.ballot_tallies.iter().any(|t| t.ballot.eq(ballot))
}

pub fn is_voting_valid(&self, current_slot: u64, valid_slots_after_consensus: u64) -> bool {
!(self.is_consensus_reached()
&& current_slot > self.slot_consensus_reached() + valid_slots_after_consensus)
/// Determines if an operator can still cast their vote.
/// Returns true when:
/// Consensus is not reached OR the voting window is still valid, assuming set_tie_breaker was not invoked
pub fn is_voting_valid(
&self,
current_slot: u64,
valid_slots_after_consensus: u64,
) -> Result<bool, TipRouterError> {
let vote_window_valid = current_slot
<= self
.slot_consensus_reached()
.checked_add(valid_slots_after_consensus)
.ok_or(TipRouterError::ArithmeticOverflow)?;

Ok((!self.is_consensus_reached() || vote_window_valid) && !self.tie_breaker_set())
}

pub fn verify_merkle_root(
Expand Down
2 changes: 2 additions & 0 deletions meta_merkle_tree/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ pub enum MerkleTreeError {
IoError(#[from] std::io::Error),
#[error("Serde Error: {0}")]
SerdeError(#[from] serde_json::Error),
#[error("Arithmetic Overflow/Underflow")]
ArithmeticOverflow,
}
16 changes: 8 additions & 8 deletions meta_merkle_tree/src/generated_merkle_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub struct GeneratedMerkleTree {
impl GeneratedMerkleTreeCollection {
pub fn new_from_stake_meta_collection(
stake_meta_coll: StakeMetaCollection,
) -> Result<GeneratedMerkleTreeCollection, MerkleRootGeneratorError> {
) -> Result<Self, MerkleRootGeneratorError> {
let generated_merkle_trees = stake_meta_coll
.stake_metas
.into_iter()
Expand Down Expand Up @@ -90,7 +90,7 @@ impl GeneratedMerkleTreeCollection {
})
.collect::<Result<Vec<GeneratedMerkleTree>, MerkleRootGeneratorError>>()?;

Ok(GeneratedMerkleTreeCollection {
Ok(Self {
generated_merkle_trees,
bank_hash: stake_meta_coll.bank_hash,
epoch: stake_meta_coll.epoch,
Expand Down Expand Up @@ -127,7 +127,7 @@ pub struct TreeNode {
impl TreeNode {
fn vec_from_stake_meta(
stake_meta: &StakeMeta,
) -> Result<Option<Vec<TreeNode>>, MerkleRootGeneratorError> {
) -> Result<Option<Vec<Self>>, MerkleRootGeneratorError> {
if let Some(tip_distribution_meta) = stake_meta.maybe_tip_distribution_meta.as_ref() {
let validator_amount = (tip_distribution_meta.total_tips as u128)
.checked_mul(tip_distribution_meta.validator_fee_bps as u128)
Expand All @@ -142,7 +142,7 @@ impl TreeNode {
],
&jito_tip_distribution::ID,
);
let mut tree_nodes = vec![TreeNode {
let mut tree_nodes = vec![Self {
claimant: stake_meta.validator_vote_account,
claim_status_pubkey,
claim_status_bump,
Expand Down Expand Up @@ -176,7 +176,7 @@ impl TreeNode {
],
&jito_tip_distribution::ID,
);
Ok(TreeNode {
Ok(Self {
claimant: delegation.stake_account_pubkey,
claim_status_pubkey,
claim_status_bump,
Expand All @@ -186,7 +186,7 @@ impl TreeNode {
proof: None,
})
})
.collect::<Result<Vec<TreeNode>, MerkleRootGeneratorError>>()?,
.collect::<Result<Vec<Self>, MerkleRootGeneratorError>>()?,
);

Ok(Some(tree_nodes))
Expand Down Expand Up @@ -316,14 +316,14 @@ mod pubkey_string_conversion {
use serde::{self, Deserialize, Deserializer, Serializer};
use solana_program::pubkey::Pubkey;

pub(crate) fn serialize<S>(pubkey: &Pubkey, serializer: S) -> Result<S::Ok, S::Error>
pub fn serialize<S>(pubkey: &Pubkey, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&pubkey.to_string())
}

pub(crate) fn deserialize<'de, D>(deserializer: D) -> Result<Pubkey, D::Error>
pub fn deserialize<'de, D>(deserializer: D) -> Result<Pubkey, D::Error>
where
D: Deserializer<'de>,
{
Expand Down
19 changes: 10 additions & 9 deletions meta_merkle_tree/src/merkle_tree.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(clippy::arithmetic_side_effects)]
// https://github.com/jito-foundation/jito-solana/blob/v1.16.19-jito/merkle-tree/src/merkle_tree.rs
use solana_program::hash::{hashv, Hash};

Expand Down Expand Up @@ -38,11 +39,11 @@ impl<'a> ProofEntry<'a> {
Self(target, left_sibling, right_sibling)
}

pub fn get_left_sibling(&self) -> Option<&'a Hash> {
pub const fn get_left_sibling(&self) -> Option<&'a Hash> {
self.1
}

pub fn get_right_sibling(&self) -> Option<&'a Hash> {
pub const fn get_right_sibling(&self) -> Option<&'a Hash> {
self.2
}
}
Expand Down Expand Up @@ -76,8 +77,8 @@ impl<'a> Proof<'a> {
}

impl MerkleTree {
#[inline]
fn next_level_len(level_len: usize) -> usize {
#[allow(clippy::integer_division)]
const fn next_level_len(level_len: usize) -> usize {
if level_len == 1 {
0
} else {
Expand Down Expand Up @@ -109,8 +110,8 @@ impl MerkleTree {
}

pub fn new<T: AsRef<[u8]>>(items: &[T], sorted_hashes: bool) -> Self {
let cap = MerkleTree::calculate_vec_capacity(items.len());
let mut mt = MerkleTree {
let cap = Self::calculate_vec_capacity(items.len());
let mut mt = Self {
leaf_count: items.len(),
nodes: Vec::with_capacity(cap),
};
Expand All @@ -121,7 +122,7 @@ impl MerkleTree {
mt.nodes.push(hash);
}

let mut level_len = MerkleTree::next_level_len(items.len());
let mut level_len = Self::next_level_len(items.len());
let mut level_start = items.len();
let mut prev_level_len = items.len();
let mut prev_level_start = 0;
Expand Down Expand Up @@ -154,7 +155,7 @@ impl MerkleTree {
prev_level_start = level_start;
prev_level_len = level_len;
level_start += level_len;
level_len = MerkleTree::next_level_len(level_len);
level_len = Self::next_level_len(level_len);
}

mt
Expand Down Expand Up @@ -196,7 +197,7 @@ impl MerkleTree {
node_index /= 2;

level_start += level_len;
level_len = MerkleTree::next_level_len(level_len);
level_len = Self::next_level_len(level_len);
}
Some(path)
}
Expand Down
10 changes: 7 additions & 3 deletions meta_merkle_tree/src/meta_merkle_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl MetaMerkleTree {
tree_node.proof = Some(get_proof(&tree, i));
}

let tree = MetaMerkleTree {
let tree = Self {
merkle_root: tree
.get_root()
.ok_or(MerkleTreeError::MerkleRootError)?
Expand Down Expand Up @@ -94,7 +94,7 @@ impl MetaMerkleTree {
pub fn new_from_file(path: &PathBuf) -> Result<Self> {
let file = File::open(path)?;
let reader = BufReader::new(file);
let tree: MetaMerkleTree = serde_json::from_reader(reader)?;
let tree: Self = serde_json::from_reader(reader)?;

Ok(tree)
}
Expand All @@ -118,7 +118,11 @@ impl MetaMerkleTree {

fn validate(&self) -> Result<()> {
// The Merkle tree can be at most height 32, implying a max node count of 2^32 - 1
if self.num_nodes > 2u64.pow(32) - 1 {
let max_nodes = 2u64
.checked_pow(32)
.and_then(|x| x.checked_sub(1))
.ok_or(MerkleTreeError::ArithmeticOverflow)?;
if self.num_nodes > max_nodes {
return Err(MerkleValidationError(format!(
"Max num nodes {} is greater than 2^32 - 1",
self.num_nodes
Expand Down
4 changes: 2 additions & 2 deletions meta_merkle_tree/src/tree_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub struct TreeNode {
}

impl TreeNode {
pub fn new(
pub const fn new(
tip_distribution_account: Pubkey,
validator_merkle_root: [u8; 32],
max_total_claim: u64,
Expand Down Expand Up @@ -50,7 +50,7 @@ impl TreeNode {
// TODO replace this with the GeneratedMerkleTree from the Operator module once that's created
impl From<GeneratedMerkleTree> for TreeNode {
fn from(generated_merkle_tree: GeneratedMerkleTree) -> Self {
TreeNode {
Self {
tip_distribution_account: generated_merkle_tree.tip_distribution_account,
validator_merkle_root: generated_merkle_tree.merkle_root.to_bytes(),
max_total_claim: generated_merkle_tree.max_total_claim,
Expand Down
2 changes: 1 addition & 1 deletion program/src/set_merkle_root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub fn process_set_merkle_root(
BallotBox::load(program_id, ncn.key, epoch, ballot_box, false)?;

let (tip_distribution_address, _) = derive_tip_distribution_account_address(
&tip_distribution_program_id.key,
tip_distribution_program_id.key,
vote_account.key,
epoch,
);
Expand Down

0 comments on commit 0fd6f26

Please sign in to comment.