@@ -19,7 +19,6 @@ use solana_program::{
1919/// 3. Close the state diff account
2020/// 4. Close the commit state record
2121///
22- ///
2322/// Accounts expected: Authority Record, Buffer PDA, Delegated PDA
2423pub fn process_finalize (
2524 _program_id : & Pubkey ,
@@ -46,64 +45,57 @@ pub fn process_finalize(
4645 let mut delegation_metadata =
4746 DelegationMetadata :: try_from_bytes_with_discriminator ( & delegation_metadata_data) ?;
4847
49- // Load committed state
48+ // Load delegation record
49+ let mut delegation_record_data = delegation_record_account. try_borrow_mut_data ( ) ?;
50+ let delegation_record =
51+ DelegationRecord :: try_from_bytes_with_discriminator_mut ( & mut delegation_record_data) ?;
52+
53+ // Load commit record
5054 let commit_record_data = commit_record_account. try_borrow_data ( ) ?;
5155 let commit_record = CommitRecord :: try_from_bytes_with_discriminator ( & commit_record_data) ?;
5256
53- // If the commit slot is greater than the last update slot, we verify and finalize the state
54- // If slot is equal or less, we simply close the commitment accounts
55- // TODO - This could probably be done in the commit_state IX
56- // TODO - since allowing commiting an obsolete state is counter-productive in the first place?
57- if commit_record. slot > delegation_metadata. last_update_external_slot {
58- // Load delegation record
59- let mut delegation_record_data = delegation_record_account. try_borrow_mut_data ( ) ?;
60- let delegation_record =
61- DelegationRecord :: try_from_bytes_with_discriminator_mut ( & mut delegation_record_data) ?;
62-
63- // TODO - We'll need to implement state validation
64- // TODO - fix: this logic should probably be done in either commit_state OR finalize IX, not both
65- verify_state (
66- validator,
67- delegation_record,
68- commit_record,
69- commit_state_account,
70- ) ?;
71-
72- // Check that the commit record is the right now
73- if !commit_record. account . eq ( delegated_account. key ) {
74- return Err ( DlpError :: InvalidDelegatedAccount . into ( ) ) ;
75- }
76- if !commit_record. identity . eq ( validator. key ) {
77- return Err ( DlpError :: InvalidReimbursementAccount . into ( ) ) ;
78- }
79-
80- // Settle accounts lamports
81- settle_lamports_balance (
82- delegated_account,
83- commit_state_account,
84- validator_fees_vault,
85- delegation_record. lamports ,
86- commit_record. lamports ,
87- ) ?;
88-
89- // Copying the new state to the delegated account
90- let commit_state_data = commit_state_account. try_borrow_data ( ) ?;
91- delegated_account. realloc ( commit_state_data. len ( ) , false ) ?;
92- let mut delegated_account_data = delegated_account. try_borrow_mut_data ( ) ?;
93- ( * delegated_account_data) . copy_from_slice ( & commit_state_data) ;
94-
95- delegation_metadata. last_update_external_slot = commit_record. slot ;
96- delegation_record. lamports = delegated_account. lamports ( ) ;
97- delegation_metadata. to_bytes_with_discriminator ( & mut delegation_metadata_data. as_mut ( ) ) ?;
98-
99- // Dropping references
100- drop ( commit_state_data) ;
101- drop ( delegated_account_data) ;
57+ verify_state (
58+ validator,
59+ delegation_record,
60+ commit_record,
61+ commit_state_account,
62+ ) ?;
63+
64+ // Check that the commit record is the right one
65+ if !commit_record. account . eq ( delegated_account. key ) {
66+ return Err ( DlpError :: InvalidDelegatedAccount . into ( ) ) ;
10267 }
68+ if !commit_record. identity . eq ( validator. key ) {
69+ return Err ( DlpError :: InvalidReimbursementAccount . into ( ) ) ;
70+ }
71+
72+ // Settle accounts lamports
73+ settle_lamports_balance (
74+ delegated_account,
75+ commit_state_account,
76+ validator_fees_vault,
77+ delegation_record. lamports ,
78+ commit_record. lamports ,
79+ ) ?;
80+
81+ // Update the delegation metadata
82+ delegation_metadata. last_update_external_slot = commit_record. slot ;
83+ delegation_metadata. to_bytes_with_discriminator ( & mut delegation_metadata_data. as_mut ( ) ) ?;
84+
85+ // Update the delegation record
86+ delegation_record. lamports = delegated_account. lamports ( ) ;
87+
88+ // Load commit state
89+ let commit_state_data = commit_state_account. try_borrow_data ( ) ?;
90+
91+ // Copying the new commit state to the delegated account
92+ delegated_account. realloc ( commit_state_data. len ( ) , false ) ?;
93+ let mut delegated_account_data = delegated_account. try_borrow_mut_data ( ) ?;
94+ ( * delegated_account_data) . copy_from_slice ( & commit_state_data) ;
10395
10496 // Drop remaining reference before closing accounts
10597 drop ( commit_record_data) ;
106- drop ( delegation_metadata_data ) ;
98+ drop ( commit_state_data ) ;
10799
108100 // Closing accounts
109101 close_pda ( commit_state_account, validator) ?;
0 commit comments