Răsfoiți Sursa

vote-program: plumb `target_version` through the program (#8191)

Joe C 1 lună în urmă
părinte
comite
b5751beb59

+ 38 - 5
programs/vote/src/vote_processor.rs

@@ -19,6 +19,7 @@ fn process_authorize_with_seed_instruction(
     invoke_context: &InvokeContext,
     instruction_context: &InstructionContext,
     vote_account: &mut BorrowedInstructionAccount,
+    target_version: vote_state::VoteStateTargetVersion,
     new_authority: &Pubkey,
     authorization_type: VoteAuthorize,
     current_authority_derived_key_owner: &Pubkey,
@@ -41,6 +42,7 @@ fn process_authorize_with_seed_instruction(
     };
     vote_state::authorize(
         vote_account,
+        target_version,
         new_authority,
         authorization_type,
         &expected_authority_keys,
@@ -64,6 +66,9 @@ declare_process_instruction!(Entrypoint, DEFAULT_COMPUTE_UNITS, |invoke_context|
         return Err(InstructionError::InvalidAccountOwner);
     }
 
+    // Determine the target vote state version to use for all operations.
+    let target_version = vote_state::TEMP_HARDCODED_TARGET_VERSION;
+
     let signers = instruction_context.get_signers()?;
     match limited_deserialize(data, solana_packet::PACKET_DATA_SIZE as u64)? {
         VoteInstruction::InitializeAccount(vote_init) => {
@@ -74,12 +79,19 @@ declare_process_instruction!(Entrypoint, DEFAULT_COMPUTE_UNITS, |invoke_context|
             }
             let clock =
                 get_sysvar_with_account_check::clock(invoke_context, &instruction_context, 2)?;
-            vote_state::initialize_account(&mut me, &vote_init, &signers, &clock)
+            vote_state::initialize_account(&mut me, target_version, &vote_init, &signers, &clock)
         }
         VoteInstruction::Authorize(voter_pubkey, vote_authorize) => {
             let clock =
                 get_sysvar_with_account_check::clock(invoke_context, &instruction_context, 1)?;
-            vote_state::authorize(&mut me, &voter_pubkey, vote_authorize, &signers, &clock)
+            vote_state::authorize(
+                &mut me,
+                target_version,
+                &voter_pubkey,
+                vote_authorize,
+                &signers,
+                &clock,
+            )
         }
         VoteInstruction::AuthorizeWithSeed(args) => {
             instruction_context.check_number_of_instruction_accounts(3)?;
@@ -87,6 +99,7 @@ declare_process_instruction!(Entrypoint, DEFAULT_COMPUTE_UNITS, |invoke_context|
                 invoke_context,
                 &instruction_context,
                 &mut me,
+                target_version,
                 &args.new_authority,
                 args.authorization_type,
                 &args.current_authority_derived_key_owner,
@@ -103,6 +116,7 @@ declare_process_instruction!(Entrypoint, DEFAULT_COMPUTE_UNITS, |invoke_context|
                 invoke_context,
                 &instruction_context,
                 &mut me,
+                target_version,
                 new_authority,
                 args.authorization_type,
                 &args.current_authority_derived_key_owner,
@@ -112,13 +126,14 @@ declare_process_instruction!(Entrypoint, DEFAULT_COMPUTE_UNITS, |invoke_context|
         VoteInstruction::UpdateValidatorIdentity => {
             instruction_context.check_number_of_instruction_accounts(2)?;
             let node_pubkey = instruction_context.get_key_of_instruction_account(1)?;
-            vote_state::update_validator_identity(&mut me, node_pubkey, &signers)
+            vote_state::update_validator_identity(&mut me, target_version, node_pubkey, &signers)
         }
         VoteInstruction::UpdateCommission(commission) => {
             let sysvar_cache = invoke_context.get_sysvar_cache();
 
             vote_state::update_commission(
                 &mut me,
+                target_version,
                 commission,
                 &signers,
                 sysvar_cache.get_epoch_schedule()?.as_ref(),
@@ -136,7 +151,14 @@ declare_process_instruction!(Entrypoint, DEFAULT_COMPUTE_UNITS, |invoke_context|
             )?;
             let clock =
                 get_sysvar_with_account_check::clock(invoke_context, &instruction_context, 2)?;
-            vote_state::process_vote_with_account(&mut me, &slot_hashes, &clock, &vote, &signers)
+            vote_state::process_vote_with_account(
+                &mut me,
+                target_version,
+                &slot_hashes,
+                &clock,
+                &vote,
+                &signers,
+            )
         }
         VoteInstruction::UpdateVoteState(vote_state_update)
         | VoteInstruction::UpdateVoteStateSwitch(vote_state_update, _) => {
@@ -148,6 +170,7 @@ declare_process_instruction!(Entrypoint, DEFAULT_COMPUTE_UNITS, |invoke_context|
             let clock = sysvar_cache.get_clock()?;
             vote_state::process_vote_state_update(
                 &mut me,
+                target_version,
                 slot_hashes.slot_hashes(),
                 &clock,
                 vote_state_update,
@@ -164,6 +187,7 @@ declare_process_instruction!(Entrypoint, DEFAULT_COMPUTE_UNITS, |invoke_context|
             let clock = sysvar_cache.get_clock()?;
             vote_state::process_vote_state_update(
                 &mut me,
+                target_version,
                 slot_hashes.slot_hashes(),
                 &clock,
                 vote_state_update,
@@ -177,6 +201,7 @@ declare_process_instruction!(Entrypoint, DEFAULT_COMPUTE_UNITS, |invoke_context|
             let clock = sysvar_cache.get_clock()?;
             vote_state::process_tower_sync(
                 &mut me,
+                target_version,
                 slot_hashes.slot_hashes(),
                 &clock,
                 tower_sync,
@@ -192,6 +217,7 @@ declare_process_instruction!(Entrypoint, DEFAULT_COMPUTE_UNITS, |invoke_context|
             vote_state::withdraw(
                 &instruction_context,
                 0,
+                target_version,
                 lamports,
                 1,
                 &signers,
@@ -207,7 +233,14 @@ declare_process_instruction!(Entrypoint, DEFAULT_COMPUTE_UNITS, |invoke_context|
             }
             let clock =
                 get_sysvar_with_account_check::clock(invoke_context, &instruction_context, 1)?;
-            vote_state::authorize(&mut me, voter_pubkey, vote_authorize, &signers, &clock)
+            vote_state::authorize(
+                &mut me,
+                target_version,
+                voter_pubkey,
+                vote_authorize,
+                &signers,
+                &clock,
+            )
         }
     }
 });

+ 26 - 0
programs/vote/src/vote_state/handler.rs

@@ -945,6 +945,14 @@ impl VoteStateHandler {
         Self::new_v4(VoteStateV4::default())
     }
 
+    #[cfg(test)]
+    pub fn as_ref_v3(&self) -> &VoteStateV3 {
+        match &self.target_state {
+            TargetVoteState::V3(v3) => v3,
+            _ => panic!("not a v3"),
+        }
+    }
+
     #[cfg(test)]
     pub fn as_ref_v4(&self) -> &VoteStateV4 {
         match &self.target_state {
@@ -952,6 +960,24 @@ impl VoteStateHandler {
             _ => panic!("not a v4"),
         }
     }
+
+    #[cfg(test)]
+    pub fn serialize(self) -> Vec<u8> {
+        match self.target_state {
+            TargetVoteState::V3(v3) => {
+                let mut data = vec![0; VoteStateV3::size_of()];
+                let versioned = VoteStateVersions::V3(Box::new(v3));
+                bincode::serialize_into(&mut data[..], &versioned).unwrap();
+                data
+            }
+            TargetVoteState::V4(v4) => {
+                let mut data = vec![0; VoteStateV4::size_of()];
+                let versioned = VoteStateVersions::V4(Box::new(v4));
+                bincode::serialize_into(&mut data[..], &versioned).unwrap();
+                data
+            }
+        }
+    }
 }
 
 // Computes the vote latency for vote on voted_for_slot where the vote itself landed in current_slot

Fișier diff suprimat deoarece este prea mare
+ 259 - 248
programs/vote/src/vote_state/mod.rs


Unele fișiere nu au fost afișate deoarece prea multe fișiere au fost modificate în acest diff