Skip to content

Commit

Permalink
cleanup: use From impls for tower conversions (#4617)
Browse files Browse the repository at this point in the history
  • Loading branch information
jstarry authored Jan 25, 2025
1 parent 8e1c067 commit 4d0fc22
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 49 deletions.
107 changes: 59 additions & 48 deletions core/src/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ pub(crate) struct ComputedBankState {

#[derive(Debug, PartialEq, Clone)]
pub enum TowerVersions {
V1_17_14(Tower1_7_14),
V1_7_14(Tower1_7_14),
V1_14_11(Tower1_14_11),
Current(Tower),
}
Expand All @@ -189,34 +189,8 @@ impl TowerVersions {

pub fn convert_to_current(self) -> Tower {
match self {
TowerVersions::V1_17_14(tower) => {
let box_last_vote = VoteTransaction::from(tower.last_vote.clone());

Tower {
node_pubkey: tower.node_pubkey,
threshold_depth: tower.threshold_depth,
threshold_size: tower.threshold_size,
vote_state: VoteStateVersions::V1_14_11(Box::new(tower.vote_state))
.convert_to_current(),
last_vote: box_last_vote,
last_vote_tx_blockhash: tower.last_vote_tx_blockhash,
last_timestamp: tower.last_timestamp,
stray_restored_slot: tower.stray_restored_slot,
last_switch_threshold_check: tower.last_switch_threshold_check,
}
}
TowerVersions::V1_14_11(tower) => Tower {
node_pubkey: tower.node_pubkey,
threshold_depth: tower.threshold_depth,
threshold_size: tower.threshold_size,
vote_state: VoteStateVersions::V1_14_11(Box::new(tower.vote_state))
.convert_to_current(),
last_vote: tower.last_vote,
last_vote_tx_blockhash: tower.last_vote_tx_blockhash,
last_timestamp: tower.last_timestamp,
stray_restored_slot: tower.stray_restored_slot,
last_switch_threshold_check: tower.last_switch_threshold_check,
},
TowerVersions::V1_7_14(tower) => tower.into(),
TowerVersions::V1_14_11(tower) => tower.into(),
TowerVersions::Current(tower) => tower,
}
}
Expand Down Expand Up @@ -279,6 +253,62 @@ impl Default for Tower {
}
}

// Tower1_14_11 is the persisted data format for the Tower,
// decoupling it from VoteState::Current.
impl From<Tower> for Tower1_14_11 {
fn from(tower: Tower) -> Self {
Self {
node_pubkey: tower.node_pubkey,
threshold_depth: tower.threshold_depth,
threshold_size: tower.threshold_size,
vote_state: VoteState1_14_11::from(tower.vote_state.clone()),
last_vote: tower.last_vote.clone(),
last_vote_tx_blockhash: tower.last_vote_tx_blockhash,
last_timestamp: tower.last_timestamp,
stray_restored_slot: tower.stray_restored_slot,
last_switch_threshold_check: tower.last_switch_threshold_check,
}
}
}

// Tower1_14_11 is the persisted data format for the Tower,
// decoupling it from VoteState::Current.
impl From<Tower1_14_11> for Tower {
fn from(tower: Tower1_14_11) -> Self {
Self {
node_pubkey: tower.node_pubkey,
threshold_depth: tower.threshold_depth,
threshold_size: tower.threshold_size,
vote_state: VoteStateVersions::V1_14_11(Box::new(tower.vote_state))
.convert_to_current(),
last_vote: tower.last_vote,
last_vote_tx_blockhash: tower.last_vote_tx_blockhash,
last_timestamp: tower.last_timestamp,
stray_restored_slot: tower.stray_restored_slot,
last_switch_threshold_check: tower.last_switch_threshold_check,
}
}
}

impl From<Tower1_7_14> for Tower {
fn from(tower: Tower1_7_14) -> Self {
let box_last_vote = VoteTransaction::from(tower.last_vote.clone());

Self {
node_pubkey: tower.node_pubkey,
threshold_depth: tower.threshold_depth,
threshold_size: tower.threshold_size,
vote_state: VoteStateVersions::V1_14_11(Box::new(tower.vote_state))
.convert_to_current(),
last_vote: box_last_vote,
last_vote_tx_blockhash: tower.last_vote_tx_blockhash,
last_timestamp: tower.last_timestamp,
stray_restored_slot: tower.stray_restored_slot,
last_switch_threshold_check: tower.last_switch_threshold_check,
}
}
}

impl Tower {
pub fn new(
node_pubkey: &Pubkey,
Expand Down Expand Up @@ -1656,25 +1686,6 @@ pub enum TowerError {
HardFork(Slot),
}

// Tower1_14_11 is the persisted data format for the Tower, decoupling it from VoteState::Current
// From Tower1_14_11 to Tower is not implemented because it is not an expected conversion
#[allow(clippy::from_over_into)]
impl Into<Tower1_14_11> for Tower {
fn into(self) -> Tower1_14_11 {
Tower1_14_11 {
node_pubkey: self.node_pubkey,
threshold_depth: self.threshold_depth,
threshold_size: self.threshold_size,
vote_state: VoteState1_14_11::from(self.vote_state.clone()),
last_vote: self.last_vote.clone(),
last_vote_tx_blockhash: self.last_vote_tx_blockhash,
last_timestamp: self.last_timestamp,
stray_restored_slot: self.stray_restored_slot,
last_switch_threshold_check: self.last_switch_threshold_check,
}
}
}

impl TowerError {
pub fn is_file_missing(&self) -> bool {
if let TowerError::IoError(io_err) = &self {
Expand Down
2 changes: 1 addition & 1 deletion core/src/consensus/tower_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl SavedTowerVersions {
if !t.signature.verify(node_pubkey.as_ref(), &t.data) {
return Err(TowerError::InvalidSignature);
}
bincode::deserialize(&t.data).map(TowerVersions::V1_17_14)
bincode::deserialize(&t.data).map(TowerVersions::V1_7_14)
}
SavedTowerVersions::Current(t) => {
if !t.signature.verify(node_pubkey.as_ref(), &t.data) {
Expand Down

0 comments on commit 4d0fc22

Please sign in to comment.