Skip to content

Commit 3432395

Browse files
committed
chore: add rustfmt.toml with some sensible format settings
1 parent e1b40fd commit 3432395

File tree

94 files changed

+2477
-1278
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

94 files changed

+2477
-1278
lines changed

Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
build:
2+
@cargo build-sbf
3+
4+
test:
5+
RUST_LOG=off cargo test-sbf --features unit_test_config
6+
7+
fmt:
8+
cargo +nightly fmt

rustfmt.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
max_width = 80
2+
edition = "2021"
3+
imports_granularity = "Crate"
4+
group_imports = "StdExternalCrate"

src/account_size_class.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ const MB: u32 = 1024 * KB;
1313
// That is because each *_size_budget() function includes this constant, so callers must subtract (N-1) instances
1414
// when combining multiple instructions.
1515
//
16-
pub const DLP_PROGRAM_DATA_SIZE_CLASS: AccountSizeClass = AccountSizeClass::Dynamic(350 * KB);
16+
pub const DLP_PROGRAM_DATA_SIZE_CLASS: AccountSizeClass =
17+
AccountSizeClass::Dynamic(350 * KB);
1718

1819
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
1920
pub enum AccountSizeClass {

src/args/commit_state.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use bytemuck::{Pod, Zeroable};
21
use std::mem::size_of;
32

43
use borsh::{BorshDeserialize, BorshSerialize};
4+
use bytemuck::{Pod, Zeroable};
55

66
use crate::args::{ArgsWithBuffer, Boolean};
77

@@ -35,7 +35,8 @@ pub struct CommitFinalizeArgs {
3535
pub reserved_padding: [u8; 3],
3636
}
3737

38-
pub type CommitFinalizeArgsWithBuffer<'a> = ArgsWithBuffer<'a, CommitFinalizeArgs>;
38+
pub type CommitFinalizeArgsWithBuffer<'a> =
39+
ArgsWithBuffer<'a, CommitFinalizeArgs>;
3940

4041
#[derive(Default, Debug, BorshSerialize, BorshDeserialize)]
4142
pub struct CommitStateArgs {

src/consts.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use pinocchio::Address;
2-
use solana_program::pubkey;
3-
use solana_program::pubkey::Pubkey;
2+
use solana_program::{pubkey, pubkey::Pubkey};
43

54
/// The delegation session fees (extracted in percentage from the delegation PDAs rent on closure).
65
pub const RENT_FEES_PERCENTAGE: u8 = 10;
@@ -15,7 +14,8 @@ pub const COMMIT_FEE_LAMPORTS: u64 = 100_000;
1514
pub const SESSION_FEE_LAMPORTS: u64 = 300_000;
1615

1716
/// The discriminator for the external undelegate instruction.
18-
pub const EXTERNAL_UNDELEGATE_DISCRIMINATOR: [u8; 8] = [196, 28, 41, 206, 48, 37, 51, 167];
17+
pub const EXTERNAL_UNDELEGATE_DISCRIMINATOR: [u8; 8] =
18+
[196, 28, 41, 206, 48, 37, 51, 167];
1919

2020
/// The program ID of the delegation program.
2121
pub const DELEGATION_PROGRAM_ID: Pubkey = crate::id();
@@ -32,11 +32,13 @@ pub const DEFAULT_VALIDATOR_IDENTITY: Pubkey =
3232
/// The broadcast identity marks an account as undelegatable.
3333
/// Validators treat it as always delegatable, which is safe since such accounts
3434
/// cannot be committed or delegated
35-
pub const BROADCAST_IDENTITY: Pubkey = pubkey!("Broadcast1111111111111111111111111111111111");
35+
pub const BROADCAST_IDENTITY: Pubkey =
36+
pubkey!("Broadcast1111111111111111111111111111111111");
3637

37-
pub const BPF_LOADER_UPGRADEABLE_ID: Address = Address::new_from_array(
38-
const_crypto::bs58::decode_pubkey("BPFLoaderUpgradeab1e11111111111111111111111"),
39-
);
38+
pub const BPF_LOADER_UPGRADEABLE_ID: Address =
39+
Address::new_from_array(const_crypto::bs58::decode_pubkey(
40+
"BPFLoaderUpgradeab1e11111111111111111111111",
41+
));
4042

4143
pub const DELEGATION_PROGRAM_DATA_ID: Address = Address::new_from_array(
4244
const_crypto::ed25519::derive_program_address(

src/diff/algorithm.rs

Lines changed: 50 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,11 @@ use std::cmp::{min, Ordering};
33
use pinocchio::error::ProgramError;
44
use rkyv::util::AlignedVec;
55

6-
use crate::error::DlpError;
7-
86
use super::{
9-
DiffSet, OffsetInData, SizeChanged, SIZE_OF_CHANGED_LEN, SIZE_OF_NUM_OFFSET_PAIRS,
10-
SIZE_OF_SINGLE_OFFSET_PAIR,
7+
DiffSet, OffsetInData, SizeChanged, SIZE_OF_CHANGED_LEN,
8+
SIZE_OF_NUM_OFFSET_PAIRS, SIZE_OF_SINGLE_OFFSET_PAIR,
119
};
12-
13-
use crate::{require_eq, require_le};
10+
use crate::{error::DlpError, require_eq, require_le};
1411

1512
///
1613
/// Compute diff between original and changed.
@@ -191,7 +188,10 @@ pub fn compute_diff(original: &[u8], changed: &[u8]) -> AlignedVec {
191188
/// - None means NO change
192189
/// - Some(size_changed) means the data size has changed and size_changed indicates
193190
/// whether it has expanded or shrunk.
194-
pub fn detect_size_change(original: &[u8], diffset: &DiffSet<'_>) -> Option<SizeChanged> {
191+
pub fn detect_size_change(
192+
original: &[u8],
193+
diffset: &DiffSet<'_>,
194+
) -> Option<SizeChanged> {
195195
match diffset.changed_len().cmp(&original.len()) {
196196
Ordering::Less => Some(SizeChanged::Shrunk(diffset.changed_len())),
197197
Ordering::Greater => Some(SizeChanged::Expanded(diffset.changed_len())),
@@ -203,7 +203,10 @@ pub fn detect_size_change(original: &[u8], diffset: &DiffSet<'_>) -> Option<Size
203203
///
204204
/// Precondition:
205205
/// - original.len() must be equal to the length encoded in the diff.
206-
pub fn apply_diff_in_place(original: &mut [u8], diffset: &DiffSet<'_>) -> Result<(), ProgramError> {
206+
pub fn apply_diff_in_place(
207+
original: &mut [u8],
208+
diffset: &DiffSet<'_>,
209+
) -> Result<(), ProgramError> {
207210
if let Some(_layout) = detect_size_change(original, diffset) {
208211
return Err(ProgramError::InvalidInstructionData);
209212
}
@@ -212,7 +215,10 @@ pub fn apply_diff_in_place(original: &mut [u8], diffset: &DiffSet<'_>) -> Result
212215

213216
/// This function creates a copy of original, possibly extending or shrinking it,
214217
/// and then applies the diff to it, before returning it.
215-
pub fn apply_diff_copy(original: &[u8], diffset: &DiffSet<'_>) -> Result<Vec<u8>, ProgramError> {
218+
pub fn apply_diff_copy(
219+
original: &[u8],
220+
diffset: &DiffSet<'_>,
221+
) -> Result<Vec<u8>, ProgramError> {
216222
Ok(match detect_size_change(original, diffset) {
217223
Some(SizeChanged::Expanded(new_size)) => {
218224
let mut applied = Vec::with_capacity(new_size);
@@ -273,7 +279,8 @@ pub fn merge_diff_copy<'a>(
273279
if write_index < start {
274280
require_le!(start, original.len(), DlpError::InvalidDiff);
275281
// copy the unchanged bytes
276-
destination[write_index..start].copy_from_slice(&original[write_index..start]);
282+
destination[write_index..start]
283+
.copy_from_slice(&original[write_index..start]);
277284
}
278285

279286
destination[start..end].copy_from_slice(diff_segment);
@@ -319,12 +326,14 @@ pub fn merge_diff_copy<'a>(
319326
if destination.len() <= original.len() {
320327
// case: all n bytes come from original
321328
let dest_len = destination.len();
322-
destination[write_index..].copy_from_slice(&original[write_index..dest_len]);
329+
destination[write_index..]
330+
.copy_from_slice(&original[write_index..dest_len]);
323331
dest_len
324332
} else if write_index < original.len() {
325333
// case: some bytes come from original and the rest are "assumed" to be
326334
// zero-initialized (by the caller).
327-
destination[write_index..original.len()].copy_from_slice(&original[write_index..]);
335+
destination[write_index..original.len()]
336+
.copy_from_slice(&original[write_index..]);
328337
original.len()
329338
} else {
330339
// case: all N bytes are "assumed" to be zero-initialized (by the caller).
@@ -344,7 +353,10 @@ pub fn merge_diff_copy<'a>(
344353
}
345354

346355
// private function that does the actual work.
347-
fn apply_diff_impl(original: &mut [u8], diffset: &DiffSet<'_>) -> Result<(), ProgramError> {
356+
fn apply_diff_impl(
357+
original: &mut [u8],
358+
diffset: &DiffSet<'_>,
359+
) -> Result<(), ProgramError> {
348360
for item in diffset.iter() {
349361
let (diff_segment, offset_range) = item?;
350362
original[offset_range].copy_from_slice(diff_segment);
@@ -361,7 +373,10 @@ mod tests {
361373
Rng, RngCore, SeedableRng,
362374
};
363375

364-
use crate::{apply_diff_copy, apply_diff_in_place, compute_diff, merge_diff_copy, DiffSet};
376+
use crate::{
377+
apply_diff_copy, apply_diff_in_place, compute_diff, merge_diff_copy,
378+
DiffSet,
379+
};
365380

366381
#[test]
367382
fn test_no_change() {
@@ -395,8 +410,9 @@ mod tests {
395410
// 2 (u32)
396411
expected_diff.extend_from_slice(&2u32.to_le_bytes());
397412
} else {
398-
expected_diff
399-
.extend_from_slice(&(2u32 + additional_changes.len() as u32).to_le_bytes());
413+
expected_diff.extend_from_slice(
414+
&(2u32 + additional_changes.len() as u32).to_le_bytes(),
415+
);
400416
}
401417

402418
// -- offsets
@@ -450,13 +466,15 @@ mod tests {
450466
assert_eq!(actual_diff.len(), 4 + 4 + 8 + 8 + (4 + 8));
451467
assert_eq!(actual_diff.as_slice(), expected_diff.as_slice());
452468

453-
let expected_changed = apply_diff_copy(&original, &actual_diffset).unwrap();
469+
let expected_changed =
470+
apply_diff_copy(&original, &actual_diffset).unwrap();
454471

455472
assert_eq!(changed.as_slice(), expected_changed.as_slice());
456473

457474
let expected_changed = {
458475
let mut destination = vec![255; original.len()];
459-
merge_diff_copy(&mut destination, &original, &actual_diffset).unwrap();
476+
merge_diff_copy(&mut destination, &original, &actual_diffset)
477+
.unwrap();
460478
destination
461479
};
462480

@@ -492,7 +510,8 @@ mod tests {
492510

493511
let expected_changed = {
494512
let mut destination = vec![255; CHANGED_LEN];
495-
merge_diff_copy(&mut destination, &original, &actual_diffset).unwrap();
513+
merge_diff_copy(&mut destination, &original, &actual_diffset)
514+
.unwrap();
496515
destination
497516
};
498517

@@ -525,14 +544,17 @@ mod tests {
525544
//
526545
// TODO (snawaz): we could optimize compute_diff to not include the zero bytes which are
527546
// part of the expansion.
528-
let expected_diff = get_example_expected_diff(CHANGED_LEN, vec![(100, &[0; 20])]);
547+
let expected_diff =
548+
get_example_expected_diff(CHANGED_LEN, vec![(100, &[0; 20])]);
529549

530550
assert_eq!(actual_diff.len(), 4 + 4 + (8 + 8) + (4 + 8) + (4 + 4 + 20));
531551
assert_eq!(actual_diff.as_slice(), expected_diff.as_slice());
532552

533553
let expected_changed = {
534554
let mut destination = vec![255; CHANGED_LEN];
535-
let unwritten = merge_diff_copy(&mut destination, &original, &actual_diffset).unwrap();
555+
let unwritten =
556+
merge_diff_copy(&mut destination, &original, &actual_diffset)
557+
.unwrap();
536558

537559
// TODO (snawaz): unwritten == &mut [], is because currently the expanded bytes are part of the diff.
538560
// Once compute_diff is optimized further, written must be &mut [0; 20].
@@ -587,7 +609,8 @@ mod tests {
587609
}
588610
}
589611
// println!("{diff_offset}, {diff_end} => {diff_len}");
590-
slices.push((diff_offset, copy[diff_offset..diff_end].to_vec()));
612+
slices
613+
.push((diff_offset, copy[diff_offset..diff_end].to_vec()));
591614

592615
offset_range = (diff_end + 8)..(diff_end + 8 + slab);
593616
}
@@ -606,7 +629,9 @@ mod tests {
606629
let mut offset_in_diff = 0u32;
607630
for (offset_in_account, slice) in slices.iter() {
608631
diff.extend_from_slice(&offset_in_diff.to_le_bytes());
609-
diff.extend_from_slice(&(*offset_in_account as u32).to_le_bytes());
632+
diff.extend_from_slice(
633+
&(*offset_in_account as u32).to_le_bytes(),
634+
);
610635
offset_in_diff += slice.len() as u32;
611636
}
612637

@@ -634,7 +659,8 @@ mod tests {
634659

635660
let expected_changed = {
636661
let mut destination = vec![255; original.len()];
637-
merge_diff_copy(&mut destination, &original, &actual_diffset).unwrap();
662+
merge_diff_copy(&mut destination, &original, &actual_diffset)
663+
.unwrap();
638664
destination
639665
};
640666

src/diff/types.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ impl<'a> DiffSet<'a> {
9292
// - raw_pairs aligned to 4-byte
9393
// - raw_pairs is big enough to hold both changed_len and segments_count
9494
this.offset_pairs = unsafe {
95-
let raw_pairs = buf.add(SIZE_OF_CHANGED_LEN + SIZE_OF_NUM_OFFSET_PAIRS)
95+
let raw_pairs = buf
96+
.add(SIZE_OF_CHANGED_LEN + SIZE_OF_NUM_OFFSET_PAIRS)
9697
as *const OffsetPair;
9798
slice::from_raw_parts(raw_pairs, segments_count)
9899
};
@@ -103,7 +104,9 @@ impl<'a> DiffSet<'a> {
103104
Ok(this)
104105
}
105106

106-
pub fn try_new_from_borsh_vec(vec_buffer: &'a [u8]) -> Result<Self, ProgramError> {
107+
pub fn try_new_from_borsh_vec(
108+
vec_buffer: &'a [u8],
109+
) -> Result<Self, ProgramError> {
107110
if vec_buffer.len() < 4 {
108111
return Err(ProgramError::InvalidInstructionData);
109112
}
@@ -164,9 +167,10 @@ impl<'a> DiffSet<'a> {
164167
return Err(DlpError::InvalidDiff.into());
165168
}
166169

167-
let segment = &self.concat_diff[segment_begin as usize..segment_end as usize];
168-
let range =
169-
offset_in_data as usize..(offset_in_data + segment_end - segment_begin) as usize;
170+
let segment =
171+
&self.concat_diff[segment_begin as usize..segment_end as usize];
172+
let range = offset_in_data as usize
173+
..(offset_in_data + segment_end - segment_begin) as usize;
170174

171175
if range.end > self.changed_len() {
172176
return Err(DlpError::InvalidDiff.into());
@@ -178,7 +182,8 @@ impl<'a> DiffSet<'a> {
178182
/// Iterates diff segments
179183
pub fn iter(
180184
&self,
181-
) -> impl Iterator<Item = Result<(&'a [u8], OffsetInData), ProgramError>> + '_ {
185+
) -> impl Iterator<Item = Result<(&'a [u8], OffsetInData), ProgramError>> + '_
186+
{
182187
(0..self.segments_count).map(|index| {
183188
self.diff_segment_at(index)
184189
.map(|val| val.expect("impossible: index can never be greater than segments_count"))

src/entrypoint.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
use crate::{error::DlpError, fast_process_instruction, slow_process_instruction};
2-
31
use solana_program::entrypoint;
42

3+
use crate::{
4+
error::DlpError, fast_process_instruction, slow_process_instruction,
5+
};
6+
57
entrypoint::custom_heap_default!();
68
entrypoint::custom_panic_default!();
79

@@ -15,8 +17,9 @@ pub unsafe extern "C" fn entrypoint(input: *mut u8) -> u64 {
1517
core::mem::MaybeUninit::<pinocchio::AccountView>::uninit();
1618
let mut accounts = [UNINIT; { pinocchio::MAX_TX_ACCOUNTS }];
1719

18-
let (program_id, count, data) =
19-
pinocchio::entrypoint::deserialize::<{ pinocchio::MAX_TX_ACCOUNTS }>(input, &mut accounts);
20+
let (program_id, count, data) = pinocchio::entrypoint::deserialize::<
21+
{ pinocchio::MAX_TX_ACCOUNTS },
22+
>(input, &mut accounts);
2023

2124
match fast_process_instruction(
2225
program_id,
@@ -25,7 +28,10 @@ pub unsafe extern "C" fn entrypoint(input: *mut u8) -> u64 {
2528
) {
2629
Some(Ok(())) => pinocchio::SUCCESS,
2730
Some(Err(error)) => {
28-
pinocchio_log::log!("fast_process_instruction: {}", error.to_str::<DlpError>());
31+
pinocchio_log::log!(
32+
"fast_process_instruction: {}",
33+
error.to_str::<DlpError>()
34+
);
2935
error.into()
3036
}
3137

@@ -40,7 +46,8 @@ pub unsafe extern "C" fn entrypoint(input: *mut u8) -> u64 {
4046
/// function name is slow_entrypoint() as opposed to entrypoint() because this is a fallback
4147
/// entrypoint (a slow one).
4248
pub unsafe fn slow_entrypoint(input: *mut u8) -> u64 {
43-
let (program_id, accounts, instruction_data) = unsafe { entrypoint::deserialize(input) };
49+
let (program_id, accounts, instruction_data) =
50+
unsafe { entrypoint::deserialize(input) };
4451
match slow_process_instruction(program_id, &accounts, instruction_data) {
4552
Ok(()) => entrypoint::SUCCESS,
4653
Err(error) => {

src/error.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,19 @@ use strum::IntoStaticStr;
44
use thiserror::Error;
55

66
pub const INVALID_ESCROW_PDA: &str = "invalid escrow pda in CallHandler";
7-
pub const INVALID_ESCROW_OWNER: &str = "escrow can not be delegated in CallHandler";
7+
pub const INVALID_ESCROW_OWNER: &str =
8+
"escrow can not be delegated in CallHandler";
89

910
#[derive(
10-
Debug, Error, Clone, Copy, PartialEq, Eq, IntoPrimitive, TryFromPrimitive, IntoStaticStr,
11+
Debug,
12+
Error,
13+
Clone,
14+
Copy,
15+
PartialEq,
16+
Eq,
17+
IntoPrimitive,
18+
TryFromPrimitive,
19+
IntoStaticStr,
1120
)]
1221
#[repr(u32)]
1322
pub enum DlpError {

0 commit comments

Comments
 (0)