Skip to content

Commit 1361f9f

Browse files
committed
feat: add AccountSizeClass and compute sizes for 4 instructions
1 parent d58eb95 commit 1361f9f

File tree

9 files changed

+173
-0
lines changed

9 files changed

+173
-0
lines changed

src/account_size_class.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#[derive(Copy, Clone, Debug)]
2+
pub enum AccountSizeClass {
3+
// account size <= 256 B
4+
//
5+
// - sysvars (rent, clock, epoch schedule)
6+
// - SPL token account and ATA
7+
// - SPL token mint
8+
// - delegation_metadata_pda
9+
// - delegation_record_pda
10+
// - commit_record_pda
11+
// - program_config_pda
12+
// - validator
13+
// - fees_vault_pda
14+
Tiny,
15+
16+
// account size <= 1 KB
17+
//
18+
// - sysvars (recent blockhash)
19+
Small,
20+
21+
// account size <= 8 KB
22+
//
23+
// - sysvars (instructions)
24+
Medium,
25+
26+
// account size <= 64 KB
27+
//
28+
// - sysvars (state history)
29+
Large,
30+
31+
// account size <= 256 KB
32+
//
33+
ExtraLarge,
34+
35+
// account size <= 1 MB
36+
Huge,
37+
38+
// any legal value
39+
Dynamic(u32),
40+
}
41+
42+
impl AccountSizeClass {
43+
pub const fn size_budget(self) -> u32 {
44+
const KB: u32 = 1024;
45+
const MB: u32 = 1024 * KB;
46+
47+
match self {
48+
Self::Tiny => 256,
49+
Self::Small => KB,
50+
Self::Medium => 8 * KB,
51+
Self::Large => 64 * KB,
52+
Self::ExtraLarge => 256 * KB,
53+
//Self::ExtraLarge => 5 * MB,
54+
Self::Huge => MB,
55+
Self::Dynamic(n) => n,
56+
}
57+
}
58+
}
59+
60+
pub fn total_size_budget(classes: &[AccountSizeClass]) -> u32 {
61+
classes.iter().map(|f| f.size_budget()).sum()
62+
}

src/instruction_builder/commit_diff.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use crate::pda::{
1010
delegation_metadata_pda_from_delegated_account, delegation_record_pda_from_delegated_account,
1111
program_config_from_program_id, validator_fees_vault_pda_from_validator,
1212
};
13+
use crate::{total_size_budget, AccountSizeClass};
1314

1415
/// Builds a commit state instruction.
1516
/// See [crate::processor::fast::process_commit_diff] for docs.
@@ -43,3 +44,17 @@ pub fn commit_diff(
4344
data: [DlpDiscriminator::CommitDiff.to_vec(), commit_args].concat(),
4445
}
4546
}
47+
48+
pub fn commit_diff_size_budget(delegated_account: AccountSizeClass) -> u32 {
49+
total_size_budget(&[
50+
AccountSizeClass::Tiny, // validator
51+
delegated_account, // delegated_account
52+
delegated_account, // commit_state_pda
53+
AccountSizeClass::Tiny, // commit_record_pda
54+
AccountSizeClass::Tiny, // delegation_record_pda
55+
AccountSizeClass::Tiny, // delegation_metadata_pda
56+
AccountSizeClass::Tiny, // validator_fees_vault_pda
57+
AccountSizeClass::Tiny, // program_config_pda
58+
AccountSizeClass::Tiny, // system_program
59+
])
60+
}

src/instruction_builder/commit_diff_from_buffer.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use crate::pda::{
1010
delegation_metadata_pda_from_delegated_account, delegation_record_pda_from_delegated_account,
1111
program_config_from_program_id, validator_fees_vault_pda_from_validator,
1212
};
13+
use crate::{total_size_budget, AccountSizeClass};
1314

1415
/// Builds a commit state from buffer instruction.
1516
/// See [crate::processor::process_commit_diff_from_buffer] for docs.
@@ -45,3 +46,18 @@ pub fn commit_diff_from_buffer(
4546
data: [DlpDiscriminator::CommitDiffFromBuffer.to_vec(), commit_args].concat(),
4647
}
4748
}
49+
50+
pub fn commit_diff_from_buffer_size_budget(delegated_account: AccountSizeClass) -> u32 {
51+
total_size_budget(&[
52+
AccountSizeClass::Tiny, // validator
53+
delegated_account, // delegated_account
54+
delegated_account, // commit_state_pda
55+
AccountSizeClass::Tiny, // commit_record_pda
56+
AccountSizeClass::Tiny, // delegation_record_pda
57+
AccountSizeClass::Tiny, // delegation_metadata_pda
58+
delegated_account, // commit_state_buffer
59+
AccountSizeClass::Tiny, // validator_fees_vault_pda
60+
AccountSizeClass::Tiny, // program_config_pda
61+
AccountSizeClass::Tiny, // system_program
62+
])
63+
}

src/instruction_builder/commit_state.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use crate::pda::{
1010
delegation_metadata_pda_from_delegated_account, delegation_record_pda_from_delegated_account,
1111
program_config_from_program_id, validator_fees_vault_pda_from_validator,
1212
};
13+
use crate::{total_size_budget, AccountSizeClass};
1314

1415
/// Builds a commit state instruction.
1516
/// See [crate::processor::process_commit_state] for docs.
@@ -43,3 +44,17 @@ pub fn commit_state(
4344
data: [DlpDiscriminator::CommitState.to_vec(), commit_args].concat(),
4445
}
4546
}
47+
48+
pub fn commit_size_budget(delegated_account: AccountSizeClass) -> u32 {
49+
total_size_budget(&[
50+
AccountSizeClass::Tiny, // validator
51+
delegated_account, // delegated_account
52+
delegated_account, // commit_state_pda
53+
AccountSizeClass::Tiny, // commit_record_pda
54+
AccountSizeClass::Tiny, // delegation_record_pda
55+
AccountSizeClass::Tiny, // delegation_metadata_pda
56+
AccountSizeClass::Tiny, // validator_fees_vault_pda
57+
AccountSizeClass::Tiny, // program_config_pda
58+
AccountSizeClass::Tiny, // system_program
59+
])
60+
}

src/instruction_builder/commit_state_from_buffer.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use crate::pda::{
1010
delegation_metadata_pda_from_delegated_account, delegation_record_pda_from_delegated_account,
1111
program_config_from_program_id, validator_fees_vault_pda_from_validator,
1212
};
13+
use crate::{total_size_budget, AccountSizeClass};
1314

1415
/// Builds a commit state from buffer instruction.
1516
/// See [crate::processor::process_commit_state_from_buffer] for docs.
@@ -49,3 +50,18 @@ pub fn commit_state_from_buffer(
4950
.concat(),
5051
}
5152
}
53+
54+
pub fn commit_state_from_buffer_size_budget(delegated_account: AccountSizeClass) -> u32 {
55+
total_size_budget(&[
56+
AccountSizeClass::Tiny, // validator
57+
delegated_account, // delegated_account
58+
delegated_account, // commit_state_pda
59+
AccountSizeClass::Tiny, // commit_record_pda
60+
AccountSizeClass::Tiny, // delegation_record_pda
61+
AccountSizeClass::Tiny, // delegation_metadata_pda
62+
delegated_account, // commit_state_buffer
63+
AccountSizeClass::Tiny, // validator_fees_vault_pda
64+
AccountSizeClass::Tiny, // program_config_pda
65+
AccountSizeClass::Tiny, // system_program
66+
])
67+
}

src/instruction_builder/delegate.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::pda::{
99
delegate_buffer_pda_from_delegated_account_and_owner_program,
1010
delegation_metadata_pda_from_delegated_account, delegation_record_pda_from_delegated_account,
1111
};
12+
use crate::{total_size_budget, AccountSizeClass};
1213

1314
/// Builds a delegate instruction
1415
/// See [crate::processor::process_delegate] for docs.
@@ -41,3 +42,15 @@ pub fn delegate(
4142
data,
4243
}
4344
}
45+
46+
pub fn delegate_size_budget(delegated_account: AccountSizeClass) -> u32 {
47+
total_size_budget(&[
48+
AccountSizeClass::Tiny, // payer
49+
delegated_account, // delegated_account
50+
AccountSizeClass::Tiny, // owner
51+
delegated_account, // delegate_buffer_pda
52+
AccountSizeClass::Tiny, // delegation_record_pda
53+
AccountSizeClass::Tiny, // delegation_metadata_pda
54+
AccountSizeClass::Tiny, // system_program
55+
])
56+
}

src/instruction_builder/finalize.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use crate::pda::{
88
delegation_metadata_pda_from_delegated_account, delegation_record_pda_from_delegated_account,
99
validator_fees_vault_pda_from_validator,
1010
};
11+
use crate::{total_size_budget, AccountSizeClass};
1112

1213
/// Builds a finalize state instruction.
1314
/// See [crate::processor::process_finalize] for docs.
@@ -33,3 +34,16 @@ pub fn finalize(validator: Pubkey, delegated_account: Pubkey) -> Instruction {
3334
data: DlpDiscriminator::Finalize.to_vec(),
3435
}
3536
}
37+
38+
pub fn finalize_size_budget(delegated_account: AccountSizeClass) -> u32 {
39+
total_size_budget(&[
40+
AccountSizeClass::Tiny, // validator
41+
delegated_account, // delegated_account
42+
delegated_account, // commit_state_pda
43+
AccountSizeClass::Tiny, // commit_record_pda
44+
AccountSizeClass::Tiny, // delegation_record_pda
45+
AccountSizeClass::Tiny, // delegation_metadata_pda
46+
AccountSizeClass::Tiny, // validator_fees_vault_pda
47+
AccountSizeClass::Tiny, // system_program
48+
])
49+
}

src/instruction_builder/undelegate.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::pda::{
99
fees_vault_pda, undelegate_buffer_pda_from_delegated_account,
1010
validator_fees_vault_pda_from_validator,
1111
};
12+
use crate::{total_size_budget, AccountSizeClass};
1213

1314
/// Builds an undelegate instruction.
1415
/// See [crate::processor::process_undelegate] for docs.
@@ -46,3 +47,20 @@ pub fn undelegate(
4647
data: DlpDiscriminator::Undelegate.to_vec(),
4748
}
4849
}
50+
51+
pub fn undelegate_size_budget(delegated_account: AccountSizeClass) -> u32 {
52+
total_size_budget(&[
53+
AccountSizeClass::Tiny, // validator
54+
delegated_account, // delegated_account
55+
AccountSizeClass::Tiny, // owner_program
56+
delegated_account, // undelegate_buffer_pda
57+
delegated_account, // commit_state_pda
58+
AccountSizeClass::Tiny, // commit_record_pda
59+
AccountSizeClass::Tiny, // delegation_record_pda
60+
AccountSizeClass::Tiny, // delegation_metadata_pda
61+
AccountSizeClass::Tiny, // rent_reimbursement
62+
AccountSizeClass::Tiny, // fees_vault_pda
63+
AccountSizeClass::Tiny, // validator_fees_vault_pda
64+
AccountSizeClass::Tiny, // system_program
65+
])
66+
}

src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ pub mod instruction_builder;
3131
pub mod pda;
3232
pub mod state;
3333

34+
mod account_size_class;
35+
36+
pub use account_size_class::*;
37+
3438
#[cfg(not(feature = "sdk"))]
3539
mod diff;
3640
#[cfg(not(feature = "sdk"))]

0 commit comments

Comments
 (0)