Skip to content

Commit f3f89f0

Browse files
committed
continue vitest
1 parent 2695b79 commit f3f89f0

20 files changed

Lines changed: 424 additions & 31 deletions

src/v2/instruction.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ pub enum DlpInstruction {
1313
///
1414
Delegate = 101,
1515
DelegateWithAnyValidator = 102,
16+
DelegateInline = 103,
17+
DelegateInline32 = 104,
1618

1719
///
1820
/// Commit group: [111, 140] => 30 slots
Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
use std::ptr::read_unaligned;
2+
3+
use dlp::{
4+
pda::validator_fees_vault_pda_from_validator,
5+
pod_view::PodView,
6+
v2::{
7+
CommitFinalizeArgs, DelegationStateHeader, ValidatedDelegationBindings,
8+
},
9+
};
10+
use solana_program::{
11+
hash::Hash, native_token::LAMPORTS_PER_SOL, rent::Rent, system_program,
12+
};
13+
use solana_program_test::{
14+
BanksClient, BanksTransactionResultWithMetadata, ProgramTest,
15+
};
16+
use solana_sdk::{
17+
account::Account,
18+
compute_budget::ComputeBudgetInstruction,
19+
signature::{Keypair, Signer},
20+
transaction::Transaction,
21+
};
22+
23+
use crate::fixtures::{
24+
DEFAULT_COMMIT_FREQUENCY_MS, DEFAULT_DELEGATION_SLOT, DELEGATED_PDA_ID,
25+
DELEGATED_PDA_OWNER_ID, TEST_AUTHORITY,
26+
};
27+
28+
mod fixtures;
29+
30+
#[tokio::test]
31+
async fn test_v2_commit_finalize_inline_resize_perf() {
32+
// Setup
33+
const KB: usize = 1024;
34+
const ACCOUNT_SIZE: usize = 4 * KB;
35+
36+
let (banks, _, authority, blockhash) =
37+
setup_program_test_env(vec![0; ACCOUNT_SIZE]).await;
38+
let new_state: Vec<u8> = vec![1; ACCOUNT_SIZE + 0 * KB];
39+
40+
let new_account_balance = 1_000_000;
41+
42+
let ix = dlp::instruction_builder::v2_commit_finalize_inline_resize(
43+
authority.pubkey(),
44+
DELEGATED_PDA_ID,
45+
&mut CommitFinalizeArgs {
46+
commit_id: 1,
47+
allow_undelegation: true.into(),
48+
data_is_diff: false.into(),
49+
lamports: new_account_balance,
50+
reserved_padding: Default::default(),
51+
},
52+
&new_state,
53+
);
54+
let tx = Transaction::new_signed_with_payer(
55+
&[
56+
ComputeBudgetInstruction::set_compute_unit_limit(
57+
dlp::instruction_builder::MAX_CU_COMMIT_FINALIZE + 150, /* 150 for ComputeBudgetInstruction itself */
58+
),
59+
ix,
60+
],
61+
Some(&authority.pubkey()),
62+
&[&authority],
63+
blockhash,
64+
);
65+
66+
// execute CommitFinalize and validate CU performmace
67+
{
68+
let BanksTransactionResultWithMetadata {
69+
result: _,
70+
metadata,
71+
} = banks.process_transaction_with_metadata(tx).await.unwrap();
72+
73+
let metadata = metadata.unwrap();
74+
75+
assertables::assert_lt!(metadata.compute_units_consumed, 1150);
76+
77+
assert_eq!(
78+
metadata
79+
.log_messages
80+
.iter()
81+
.filter(|log| !log
82+
.contains("ComputeBudget111111111111111111111111111111"))
83+
.count(),
84+
3,
85+
"CommitFinalize must not log anything in OK scenario"
86+
);
87+
}
88+
89+
let delegated_account =
90+
banks.get_account(DELEGATED_PDA_ID).await.unwrap().unwrap();
91+
let state_size = unsafe {
92+
read_unaligned(delegated_account.data.as_ptr() as *const u32)
93+
} as usize;
94+
assert_eq!(
95+
delegated_account.data[4 + state_size..].len(),
96+
new_state.len()
97+
);
98+
assert_eq!(delegated_account.data[4 + state_size..], new_state);
99+
100+
// let delegation_metadata_account = banks
101+
// .get_account(pdas.delegation_metadata)
102+
// .await
103+
// .unwrap()
104+
// .unwrap();
105+
106+
// let delegation_metadata =
107+
// DelegationMetadata::try_from_bytes_with_discriminator(
108+
// &delegation_metadata_account.data,
109+
// )
110+
// .unwrap();
111+
112+
// assert_eq!(delegation_metadata.is_undelegatable, true);
113+
}
114+
115+
async fn setup_program_test_env(
116+
pda_data: Vec<u8>,
117+
) -> (BanksClient, Keypair, Keypair, Hash) {
118+
let mut program_test = ProgramTest::new("dlp", dlp::ID, None);
119+
program_test.prefer_bpf(true);
120+
121+
let validator_keypair = Keypair::from_bytes(&TEST_AUTHORITY).unwrap();
122+
123+
program_test.add_account(
124+
validator_keypair.pubkey(),
125+
Account {
126+
lamports: 10 * LAMPORTS_PER_SOL,
127+
data: vec![],
128+
owner: system_program::id(),
129+
executable: false,
130+
rent_epoch: 0,
131+
},
132+
);
133+
134+
let validator_fees_vault =
135+
validator_fees_vault_pda_from_validator(&validator_keypair.pubkey());
136+
let state_header = DelegationStateHeader {
137+
discriminator: DelegationStateHeader::DISCRIMINATOR,
138+
original_owner: DELEGATED_PDA_OWNER_ID.to_bytes().into(),
139+
delegation_slot: DEFAULT_DELEGATION_SLOT,
140+
original_lamports: Rent::default().minimum_balance(500),
141+
commit_frequency_ms: DEFAULT_COMMIT_FREQUENCY_MS,
142+
bindings: ValidatedDelegationBindings {
143+
delegated_account: DELEGATED_PDA_ID.to_bytes().into(),
144+
validator_as_authority: validator_keypair
145+
.pubkey()
146+
.to_bytes()
147+
.into(),
148+
validator_fees_vault: validator_fees_vault.to_bytes().into(),
149+
},
150+
last_commit_id: 0,
151+
rent_payer: validator_keypair.pubkey().to_bytes().into(),
152+
is_undelegatable: false.into(),
153+
reserved_padding0: Default::default(),
154+
};
155+
156+
// Setup a delegated PDA containing the delegation_state at the end
157+
program_test.add_account(
158+
DELEGATED_PDA_ID,
159+
Account {
160+
lamports: LAMPORTS_PER_SOL,
161+
data: {
162+
let state_buffer = vec![];
163+
let state = [state_header.to_bytes(), state_buffer].concat();
164+
let state_size = (state.len() as u32).to_le_bytes().to_vec();
165+
166+
[
167+
state_size, // size of the delegation state
168+
state, // delegation state (appended by Delegate ix)
169+
pda_data, // delegated pda
170+
]
171+
.concat()
172+
},
173+
owner: dlp::id(),
174+
executable: false,
175+
rent_epoch: 0,
176+
},
177+
);
178+
179+
// Setup the validator fees vault
180+
program_test.add_account(
181+
validator_fees_vault,
182+
Account {
183+
lamports: LAMPORTS_PER_SOL,
184+
data: vec![],
185+
owner: dlp::id(),
186+
executable: false,
187+
rent_epoch: 0,
188+
},
189+
);
190+
191+
let (banks, payer, blockhash) = program_test.start().await;
192+
(banks, payer, validator_keypair, blockhash)
193+
}
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
1-
all:
1+
2+
build:
3+
anchor build
4+
5+
test:
26
anchor test --skip-local-validator --skip-build --skip-deploy

tests/v2-integration/counter/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"license": "ISC",
33
"scripts": {
4+
"check": "tsc --noEmit",
45
"test": "vitest --run",
56
"lint:fix": "prettier */*.js \"*/**/*{.js,.ts}\" -w",
67
"lint": "prettier */*.js \"*/**/*{.js,.ts}\" --check"

tests/v2-integration/counter/programs/counter/src/lib.rs

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ pub mod counter {
1313

1414
/// Create the PDA and initialize counter = 0
1515
pub fn initialize(ctx: Context<Initialize>) -> Result<()> {
16+
msg!("user: {}", ctx.accounts.user.key);
1617
let counter = &mut ctx.accounts.counter;
1718
counter.value = 0;
1819
Ok(())
1920
}
2021

21-
/// Increment counter by 1
2222
pub fn increment(ctx: Context<Increment>) -> Result<()> {
2323
let counter = &mut ctx.accounts.counter;
2424

@@ -28,18 +28,21 @@ pub mod counter {
2828
Ok(())
2929
}
3030

31-
/// (Optional) Reset to zero (useful in tests)
32-
pub fn reset(ctx: Context<Increment>) -> Result<()> {
33-
let counter = &mut ctx.accounts.counter;
34-
counter.value = 0;
31+
pub fn delegate_counter(ctx: Context<DelegateCounter>) -> Result<()> {
32+
ctx.accounts
33+
.delegate_pda(
34+
&ctx.accounts.payer,
35+
&[],
36+
DelegateConfig {
37+
commit_frequency_ms: 1000,
38+
validator: None, //"mAGicPQYBMvcYveUZA5F5UNNwyHvfYh5xkLS2Fr1mev"),
39+
},
40+
)
41+
.unwrap();
3542
Ok(())
3643
}
3744
}
3845

39-
/* ================================
40-
Accounts
41-
================================ */
42-
4346
#[derive(Accounts)]
4447
pub struct Initialize<'info> {
4548
#[account(
@@ -78,10 +81,6 @@ pub struct Increment<'info> {
7881
pub user: Signer<'info>,
7982
}
8083

81-
/* ================================
82-
State
83-
================================ */
84-
8584
#[account]
8685
pub struct Counter {
8786
pub value: u64,
@@ -91,10 +90,6 @@ impl Counter {
9190
pub const SIZE: usize = 8; // u64
9291
}
9392

94-
/* ================================
95-
Errors
96-
================================ */
97-
9893
#[error_code]
9994
pub enum ErrorCode {
10095
#[msg("Counter overflow")]
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"pubkey": "LUzidNSiPNjYNkxZcUm5hYHwnWPwsUfh2US1cpWwaBm",
3+
"account": {
4+
"lamports": 20000000000,
5+
"data": [
6+
"",
7+
"base64"
8+
],
9+
"owner": "11111111111111111111111111111111",
10+
"executable": false,
11+
"rentEpoch": 18446744073709551615,
12+
"space": 0
13+
}
14+
}

tests/v2-integration/counter/scripts/accounts/memo_v1.json

Lines changed: 14 additions & 0 deletions
Large diffs are not rendered by default.

tests/v2-integration/counter/scripts/accounts/memo_v2.json

Lines changed: 14 additions & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)