Repository navigation
Expand file tree
/
Copy pathcommit_finalize_from_buffer.rs
More file actions
158 lines (150 loc) · 4.49 KB
/
Copy pathcommit_finalize_from_buffer.rs
File metadata and controls
158 lines (150 loc) · 4.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
use delegation_program_interface as dpi;
use dpi::{
error::DlpError, pda::STATE_BUFFER_SEED, CommitFinalizeFromBufferArgs,
CommitPayloadKind, StateBuffer,
};
use pinocchio::{
address::address_eq, error::ProgramError, AccountView, ProgramResult,
};
use wheels::{
layout::Decodable, require_eq, require_eq_keys, require_n_accounts,
};
use crate::processor::internal::{
apply_commit_finalize, CommitFinalizeInput, CommitStateUpdate,
};
/// Commit and apply one finalized StateBuffer for a Direct cluster.
///
/// Accounts:
/// 0: `[signer, writable]` current validator and lamport settlement account
/// 1: `[writable]` delegated account
/// 2: `[writable]` DelegationState PDA
/// 3: `[]` finalized StateBuffer PDA
/// 4: `[]` DelegationCluster PDA
/// 5: `[]` system program, required by system CPI
#[inline(never)]
pub fn process_commit_finalize_from_buffer(
accounts: &[AccountView],
data: &[u8],
) -> ProgramResult {
let [
current_validator, // force multi-line
delegated_account,
delegation_state,
state_buffer,
delegation_cluster,
_system_program,
] = require_n_accounts!(accounts, 6);
let args = CommitFinalizeFromBufferArgs::decode(data)?;
let state_buffer_data = state_buffer.try_borrow()?;
let buffer = StateBuffer::decode(state_buffer_data.as_ref())?;
validate_state_buffer(
current_validator,
delegated_account,
state_buffer,
&buffer,
args.commit_seq(),
)?;
let state_update = match args.payload_kind() {
value if value == CommitPayloadKind::FullData.value() => {
CommitStateUpdate::FullData(buffer.payload().as_slice())
}
value if value == CommitPayloadKind::Diff.value() => {
CommitStateUpdate::Diff(dpi::diff::DiffSet::try_new(
buffer.payload().as_slice(),
)?)
}
_ => return Err(ProgramError::InvalidInstructionData),
};
apply_commit_finalize(
current_validator,
delegated_account,
delegation_state,
delegation_cluster,
CommitFinalizeInput {
commit_seq: args.commit_seq(),
lamports: args.lamports(),
owner: args.owner(),
allow_undelegation: args.allow_undelegation(),
state_update,
},
)
}
fn validate_state_buffer(
current_validator: &AccountView,
delegated_account: &AccountView,
state_buffer: &AccountView,
buffer: &dpi::StateBufferView<'_>,
commit_seq: u64,
) -> ProgramResult {
let commit_seq_bytes = commit_seq.to_le_bytes();
let bump = [buffer.bump()];
dpi::require_initialized_pda!(
state_buffer,
&[
STATE_BUFFER_SEED,
delegated_account.address().as_ref(),
&commit_seq_bytes,
current_validator.address().as_ref(),
&bump,
],
&crate::fast::ID,
false
);
require_eq!(
&buffer.discriminator(),
&StateBuffer::DISCRIMINATOR,
ProgramError::InvalidAccountData
);
require_eq_keys!(
buffer.authority(),
current_validator.address(),
DlpError::InvalidAuthority
);
require_eq_keys!(
buffer.account_pubkey(),
delegated_account.address(),
DlpError::InvalidDelegatedAccount
);
require_eq!(
buffer.commit_seq(),
commit_seq,
ProgramError::InvalidInstructionData
);
require_eq!(
buffer.finalized(),
true,
ProgramError::InvalidInstructionData
);
require_eq!(
&buffer.payload_alignment_padding(),
&[0; 2],
ProgramError::InvalidAccountData
);
require_eq!(
buffer.payload().len(),
buffer.total_len() as usize,
ProgramError::InvalidInstructionData
);
require_eq!(
buffer.payload().capacity(),
buffer.total_len() as usize,
ProgramError::InvalidInstructionData
);
require_eq!(
state_buffer.data_len(),
StateBuffer::data_len_from_payload_capacity(
buffer.payload().capacity()
)?,
ProgramError::InvalidAccountData
);
require_eq!(
buffer.data_hash(),
&account_data_hash(buffer.payload().as_slice()),
ProgramError::InvalidInstructionData
);
Ok(())
}
fn account_data_hash(data: &[u8]) -> [u8; 32] {
solana_sha256_hasher::hashv(&[b"magicblock.account_data.v1", data])
.to_bytes()
}