Skip to content

Commit eea0f20

Browse files
committed
telemetry: add test for timestamp index full skip behavior
Unit test verifies that append_timestamp_index_entry returns Ok(()) without modifying account data when the index is at MAX_TIMESTAMP_INDEX_ENTRIES.
1 parent 9131a8c commit eea0f20

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed

smartcontract/programs/doublezero-telemetry/src/processors/telemetry/write_timestamp_index.rs

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,90 @@ pub fn append_timestamp_index_entry(
9797

9898
Ok(())
9999
}
100+
101+
#[cfg(test)]
102+
mod tests {
103+
use super::*;
104+
use crate::state::timestamp_index::TimestampIndexHeader;
105+
use solana_program::account_info::AccountInfo;
106+
107+
#[test]
108+
fn test_append_skips_when_full() {
109+
let program_id = Pubkey::new_unique();
110+
let samples_key = Pubkey::new_unique();
111+
112+
let (ts_pda, _) = derive_timestamp_index_pda(&program_id, &samples_key);
113+
114+
// Build a timestamp index header at max capacity.
115+
let header = TimestampIndexHeader {
116+
account_type: AccountType::TimestampIndex,
117+
samples_account_pk: samples_key,
118+
next_entry_index: MAX_TIMESTAMP_INDEX_ENTRIES as u32,
119+
_unused: [0u8; 64],
120+
};
121+
let mut ts_data = borsh::to_vec(&header).unwrap();
122+
// Pad with dummy entries so data_is_empty() returns false and
123+
// the account data is consistent with the header.
124+
ts_data.resize(
125+
TIMESTAMP_INDEX_HEADER_SIZE + MAX_TIMESTAMP_INDEX_ENTRIES * TIMESTAMP_INDEX_ENTRY_SIZE,
126+
0,
127+
);
128+
let ts_data_snapshot = ts_data.clone();
129+
130+
let mut ts_lamports = 1_000_000u64;
131+
let ts_account = AccountInfo::new(
132+
&ts_pda,
133+
false,
134+
true,
135+
&mut ts_lamports,
136+
&mut ts_data,
137+
&program_id,
138+
false,
139+
0,
140+
);
141+
142+
let mut samples_lamports = 1_000_000u64;
143+
let mut samples_data = vec![0u8; 1]; // non-empty placeholder
144+
let samples_account = AccountInfo::new(
145+
&samples_key,
146+
false,
147+
false,
148+
&mut samples_lamports,
149+
&mut samples_data,
150+
&program_id,
151+
false,
152+
0,
153+
);
154+
155+
let payer_key = Pubkey::new_unique();
156+
let mut payer_lamports = 10_000_000u64;
157+
let mut payer_data = vec![];
158+
let payer = AccountInfo::new(
159+
&payer_key,
160+
true,
161+
true,
162+
&mut payer_lamports,
163+
&mut payer_data,
164+
&solana_program::system_program::ID,
165+
false,
166+
0,
167+
);
168+
169+
let accounts = vec![ts_account.clone(), samples_account.clone(), payer.clone()];
170+
171+
// Appending should succeed (Ok) without modifying the account.
172+
let result = append_timestamp_index_entry(
173+
&program_id,
174+
&ts_account,
175+
&samples_account,
176+
&payer,
177+
&accounts,
178+
99999, // sample_index
179+
1_700_000_000_000_000,
180+
);
181+
assert!(result.is_ok());
182+
183+
// Account data should be unchanged — no new entry was appended.
184+
assert_eq!(*ts_account.data.borrow(), ts_data_snapshot);
185+
}
186+
}

0 commit comments

Comments
 (0)