Repository navigation
Expand file tree
/
Copy pathcommit_finalize_from_buffer.rs
More file actions
106 lines (100 loc) · 3.39 KB
/
Copy pathcommit_finalize_from_buffer.rs
File metadata and controls
106 lines (100 loc) · 3.39 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
use solana_program::{
instruction::{AccountMeta, Instruction},
pubkey::Pubkey,
};
use solana_sdk_ids::system_program;
use wheels::{layout::Encodable, DataLayoutError};
use crate::{
compat::{Compatize, Modernize},
pda::{delegation_state_pda, state_buffer_pda},
CommitFinalizeFromBufferArgs, DlpV2Instruction, PrepareCommitFinalizeArgs,
};
/// Builds a direct-cluster commit that consumes a finalized StateBuffer.
pub fn commit_finalize_from_buffer(
current_validator: Pubkey,
delegation_cluster: Pubkey,
delegated_account: Pubkey,
args: CommitFinalizeFromBufferArgs,
) -> Result<Instruction, DataLayoutError> {
Ok(Instruction {
program_id: crate::id().modernize(),
accounts: vec![
AccountMeta::new(current_validator, true),
AccountMeta::new(delegated_account, false),
AccountMeta::new(
delegation_state_pda(&delegated_account.compatize())
.modernize(),
false,
),
AccountMeta::new_readonly(
state_buffer_pda(
&delegated_account.compatize(),
args.commit_seq,
¤t_validator.compatize(),
)
.modernize(),
false,
),
AccountMeta::new_readonly(delegation_cluster, false),
AccountMeta::new_readonly(system_program::id(), false),
],
data: [
DlpV2Instruction::CommitFinalizeFromBuffer.to_vec(),
args.encode()?,
]
.concat(),
})
}
/// Builds one growth step to run before finalizing a large account update.
pub fn prepare_commit_finalize(
current_validator: Pubkey,
delegation_cluster: Pubkey,
delegated_account: Pubkey,
args: PrepareCommitFinalizeArgs,
) -> Result<Instruction, DataLayoutError> {
Ok(Instruction {
program_id: crate::id().modernize(),
accounts: vec![
AccountMeta::new(current_validator, true),
AccountMeta::new(delegated_account, false),
AccountMeta::new_readonly(
delegation_state_pda(&delegated_account.compatize())
.modernize(),
false,
),
AccountMeta::new_readonly(delegation_cluster, false),
AccountMeta::new_readonly(system_program::id(), false),
],
data: [
DlpV2Instruction::PrepareCommitFinalize.to_vec(),
args.encode()?,
]
.concat(),
})
}
/// Builds every growth step needed before finalizing `target_data_len`.
pub fn prepare_commit_finalize_instructions(
current_validator: Pubkey,
delegation_cluster: Pubkey,
delegated_account: Pubkey,
commit_seq: u64,
current_data_len: usize,
target_data_len: u32,
) -> Result<Vec<Instruction>, DataLayoutError> {
let growth = (target_data_len as usize).saturating_sub(current_data_len);
let step_count =
growth.div_ceil(crate::StateBuffer::MAX_ACCOUNT_DATA_GROWTH_PER_WRITE);
let mut instructions = Vec::with_capacity(step_count);
for _ in 0..step_count {
instructions.push(prepare_commit_finalize(
current_validator,
delegation_cluster,
delegated_account,
PrepareCommitFinalizeArgs {
commit_seq,
target_data_len,
},
)?);
}
Ok(instructions)
}