Skip to content

Commit

Permalink
vote: add TowerSync ix (#365)
Browse files Browse the repository at this point in the history
* vote: add TowerSync ix

* fork_id -> block_id
  • Loading branch information
AshwinSekar authored Apr 2, 2024
1 parent 64765bf commit a468ff2
Show file tree
Hide file tree
Showing 10 changed files with 318 additions and 8 deletions.
13 changes: 12 additions & 1 deletion core/src/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ impl SwitchForkDecision {
v,
))
}
(SwitchForkDecision::SameFork, VoteTransaction::TowerSync(t)) => Some(
vote_instruction::tower_sync(vote_account_pubkey, authorized_voter_pubkey, t),
),
(SwitchForkDecision::SwitchProof(switch_proof_hash), VoteTransaction::Vote(v)) => {
Some(vote_instruction::vote_switch(
vote_account_pubkey,
Expand All @@ -114,6 +117,14 @@ impl SwitchForkDecision {
v,
*switch_proof_hash,
)),
(SwitchForkDecision::SwitchProof(switch_proof_hash), VoteTransaction::TowerSync(t)) => {
Some(vote_instruction::tower_sync_switch(
vote_account_pubkey,
authorized_voter_pubkey,
t,
*switch_proof_hash,
))
}
(SwitchForkDecision::SameFork, VoteTransaction::CompactVoteStateUpdate(v)) => {
Some(vote_instruction::compact_update_vote_state(
vote_account_pubkey,
Expand Down Expand Up @@ -221,7 +232,7 @@ pub(crate) enum BlockhashStatus {
Blockhash(Hash),
}

#[frozen_abi(digest = "iZi6s9BvytU3HbRsibrAD71jwMLvrqHdCjVk6qKcVvd")]
#[frozen_abi(digest = "679XkZ4upGc389SwqAsjs5tr2qB4wisqjbwtei7fGhxC")]
#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, AbiExample)]
pub struct Tower {
pub node_pubkey: Pubkey,
Expand Down
2 changes: 1 addition & 1 deletion core/src/consensus/tower1_14_11.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use {
},
};

#[frozen_abi(digest = "F83xHQA1wxoFDy25MTKXXmFXTc9Jbp6SXRXEPcehtKbQ")]
#[frozen_abi(digest = "4LayQwoKrE2jPhbNtg3TSpKrtEtjcPiwsVPJN7aCavri")]
#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, AbiExample)]
pub struct Tower1_14_11 {
pub(crate) node_pubkey: Pubkey,
Expand Down
13 changes: 12 additions & 1 deletion programs/vote/src/vote_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use {
sysvar_cache::get_sysvar_with_account_check,
},
solana_sdk::{
feature_set,
instruction::InstructionError,
program_utils::limited_deserialize,
pubkey::Pubkey,
Expand Down Expand Up @@ -192,7 +193,17 @@ declare_process_instruction!(Entrypoint, DEFAULT_COMPUTE_UNITS, |invoke_context|
&invoke_context.feature_set,
)
}

VoteInstruction::TowerSync(_tower_sync)
| VoteInstruction::TowerSyncSwitch(_tower_sync, _) => {
if !invoke_context
.feature_set
.is_active(&feature_set::enable_tower_sync_ix::id())
{
return Err(InstructionError::InvalidInstructionData);
}
// TODO: will fill in future PR
return Err(InstructionError::InvalidInstructionData);
}
VoteInstruction::Withdraw(lamports) => {
instruction_context.check_number_of_instruction_accounts(2)?;
let rent_sysvar = invoke_context.get_sysvar_cache().get_rent()?;
Expand Down
18 changes: 17 additions & 1 deletion programs/vote/src/vote_state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ use {
},
};

#[frozen_abi(digest = "2AuJFjx7SYrJ2ugCfH1jFh3Lr9UHMEPfKwwk1NcjqND1")]
#[frozen_abi(digest = "EcS3xgfomytEAQ1eVd8R76ZejwyHp2Ed8dHqQWh6zi5v")]
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize, AbiEnumVisitor, AbiExample)]
pub enum VoteTransaction {
Vote(Vote),
VoteStateUpdate(VoteStateUpdate),
#[serde(with = "serde_compact_vote_state_update")]
CompactVoteStateUpdate(VoteStateUpdate),
#[serde(with = "serde_tower_sync")]
TowerSync(TowerSync),
}

impl VoteTransaction {
Expand All @@ -43,6 +45,7 @@ impl VoteTransaction {
VoteTransaction::Vote(vote) => vote.slots.clone(),
VoteTransaction::VoteStateUpdate(vote_state_update) => vote_state_update.slots(),
VoteTransaction::CompactVoteStateUpdate(vote_state_update) => vote_state_update.slots(),
VoteTransaction::TowerSync(tower_sync) => tower_sync.slots(),
}
}

Expand All @@ -53,6 +56,7 @@ impl VoteTransaction {
| VoteTransaction::CompactVoteStateUpdate(vote_state_update) => {
vote_state_update.lockouts[i].slot()
}
VoteTransaction::TowerSync(tower_sync) => tower_sync.lockouts[i].slot(),
}
}

Expand All @@ -63,6 +67,7 @@ impl VoteTransaction {
| VoteTransaction::CompactVoteStateUpdate(vote_state_update) => {
vote_state_update.lockouts.len()
}
VoteTransaction::TowerSync(tower_sync) => tower_sync.lockouts.len(),
}
}

Expand All @@ -73,6 +78,7 @@ impl VoteTransaction {
| VoteTransaction::CompactVoteStateUpdate(vote_state_update) => {
vote_state_update.lockouts.is_empty()
}
VoteTransaction::TowerSync(tower_sync) => tower_sync.lockouts.is_empty(),
}
}

Expand All @@ -81,6 +87,7 @@ impl VoteTransaction {
VoteTransaction::Vote(vote) => vote.hash,
VoteTransaction::VoteStateUpdate(vote_state_update) => vote_state_update.hash,
VoteTransaction::CompactVoteStateUpdate(vote_state_update) => vote_state_update.hash,
VoteTransaction::TowerSync(tower_sync) => tower_sync.hash,
}
}

Expand All @@ -91,6 +98,7 @@ impl VoteTransaction {
| VoteTransaction::CompactVoteStateUpdate(vote_state_update) => {
vote_state_update.timestamp
}
VoteTransaction::TowerSync(tower_sync) => tower_sync.timestamp,
}
}

Expand All @@ -101,6 +109,7 @@ impl VoteTransaction {
| VoteTransaction::CompactVoteStateUpdate(vote_state_update) => {
vote_state_update.timestamp = ts
}
VoteTransaction::TowerSync(tower_sync) => tower_sync.timestamp = ts,
}
}

Expand All @@ -111,6 +120,7 @@ impl VoteTransaction {
| VoteTransaction::CompactVoteStateUpdate(vote_state_update) => {
vote_state_update.last_voted_slot()
}
VoteTransaction::TowerSync(tower_sync) => tower_sync.last_voted_slot(),
}
}

Expand All @@ -131,6 +141,12 @@ impl From<VoteStateUpdate> for VoteTransaction {
}
}

impl From<TowerSync> for VoteTransaction {
fn from(tower_sync: TowerSync) -> Self {
VoteTransaction::TowerSync(tower_sync)
}
}

// utility function, used by Stakes, tests
pub fn from<T: ReadableAccount>(account: &T) -> Option<VoteState> {
VoteState::deserialize(account.data()).ok()
Expand Down
63 changes: 60 additions & 3 deletions sdk/program/src/vote/instruction.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Vote program instructions
use {
super::state::TowerSync,
crate::{
clock::{Slot, UnixTimestamp},
hash::Hash,
Expand All @@ -10,7 +11,7 @@ use {
vote::{
program::id,
state::{
serde_compact_vote_state_update, Vote, VoteAuthorize,
serde_compact_vote_state_update, serde_tower_sync, Vote, VoteAuthorize,
VoteAuthorizeCheckedWithSeedArgs, VoteAuthorizeWithSeedArgs, VoteInit,
VoteStateUpdate, VoteStateVersions,
},
Expand Down Expand Up @@ -146,6 +147,21 @@ pub enum VoteInstruction {
#[serde(with = "serde_compact_vote_state_update")] VoteStateUpdate,
Hash,
),

/// Sync the onchain vote state with local tower
///
/// # Account references
/// 0. `[Write]` Vote account to vote with
/// 1. `[SIGNER]` Vote authority
#[serde(with = "serde_tower_sync")]
TowerSync(TowerSync),

/// Sync the onchain vote state with local tower along with a switching proof
///
/// # Account references
/// 0. `[Write]` Vote account to vote with
/// 1. `[SIGNER]` Vote authority
TowerSyncSwitch(#[serde(with = "serde_tower_sync")] TowerSync, Hash),
}

impl VoteInstruction {
Expand All @@ -157,7 +173,9 @@ impl VoteInstruction {
| Self::UpdateVoteState(_)
| Self::UpdateVoteStateSwitch(_, _)
| Self::CompactUpdateVoteState(_)
| Self::CompactUpdateVoteStateSwitch(_, _),
| Self::CompactUpdateVoteStateSwitch(_, _)
| Self::TowerSync(_)
| Self::TowerSyncSwitch(_, _),
)
}

Expand All @@ -167,7 +185,9 @@ impl VoteInstruction {
Self::UpdateVoteState(_)
| Self::UpdateVoteStateSwitch(_, _)
| Self::CompactUpdateVoteState(_)
| Self::CompactUpdateVoteStateSwitch(_, _),
| Self::CompactUpdateVoteStateSwitch(_, _)
| Self::TowerSync(_)
| Self::TowerSyncSwitch(_, _),
)
}

Expand All @@ -182,6 +202,9 @@ impl VoteInstruction {
| Self::CompactUpdateVoteStateSwitch(vote_state_update, _) => {
vote_state_update.last_voted_slot()
}
Self::TowerSync(tower_sync) | Self::TowerSyncSwitch(tower_sync, _) => {
tower_sync.last_voted_slot()
}
_ => panic!("Tried to get slot on non simple vote instruction"),
}
}
Expand All @@ -197,6 +220,9 @@ impl VoteInstruction {
| Self::CompactUpdateVoteStateSwitch(vote_state_update, _) => {
vote_state_update.timestamp
}
Self::TowerSync(tower_sync) | Self::TowerSyncSwitch(tower_sync, _) => {
tower_sync.timestamp
}
_ => panic!("Tried to get timestamp on non simple vote instruction"),
}
}
Expand Down Expand Up @@ -514,6 +540,37 @@ pub fn compact_update_vote_state_switch(
)
}

pub fn tower_sync(
vote_pubkey: &Pubkey,
authorized_voter_pubkey: &Pubkey,
tower_sync: TowerSync,
) -> Instruction {
let account_metas = vec![
AccountMeta::new(*vote_pubkey, false),
AccountMeta::new_readonly(*authorized_voter_pubkey, true),
];

Instruction::new_with_bincode(id(), &VoteInstruction::TowerSync(tower_sync), account_metas)
}

pub fn tower_sync_switch(
vote_pubkey: &Pubkey,
authorized_voter_pubkey: &Pubkey,
tower_sync: TowerSync,
proof_hash: Hash,
) -> Instruction {
let account_metas = vec![
AccountMeta::new(*vote_pubkey, false),
AccountMeta::new_readonly(*authorized_voter_pubkey, true),
];

Instruction::new_with_bincode(
id(),
&VoteInstruction::TowerSyncSwitch(tower_sync, proof_hash),
account_metas,
)
}

pub fn withdraw(
vote_pubkey: &Pubkey,
authorized_withdrawer_pubkey: &Pubkey,
Expand Down
Loading

0 comments on commit a468ff2

Please sign in to comment.