Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions program-libs/account-checks/docs/ACCOUNT_ITERATOR.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,17 +122,21 @@ fn next_option_mut(

### `remaining`
```rust
fn remaining(&self) -> Result<&'info [T], AccountError>
fn remaining(self) -> Result<&'info [T], AccountError>
```
- Returns all unprocessed accounts
- **Consumes the iterator** - cannot use iterator after calling this method
- **Error:** `NotEnoughAccountKeys` (20014) if iterator exhausted
- Use case: Getting all remaining accounts for dynamic processing

### `remaining_unchecked`
```rust
fn remaining_unchecked(&self) -> Result<&'info [T], AccountError>
fn remaining_unchecked(self) -> Result<&'info [T], AccountError>
```
- Returns remaining accounts or empty slice if exhausted
- Never errors
- **Consumes the iterator** - cannot use iterator after calling this method
- Never errors - returns empty slice if no accounts remaining
- Use case: Optional remaining accounts where empty is acceptable

## Status Methods

Expand Down
8 changes: 5 additions & 3 deletions program-libs/account-checks/src/account_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ impl<'info, T: AccountInfoTrait> AccountIterator<'info, T> {
/// Get all remaining accounts in the iterator.
#[inline(always)]
#[track_caller]
pub fn remaining(&self) -> Result<&'info [T], AccountError> {
pub fn remaining(self) -> Result<&'info [T], AccountError> {
if self.position >= self.accounts.len() {
#[cfg(feature = "std")]
{
Expand All @@ -182,10 +182,12 @@ impl<'info, T: AccountInfoTrait> AccountIterator<'info, T> {
Ok(&self.accounts[self.position..])
}

/// Get all remaining accounts in the iterator.
/// Get all remaining accounts in the iterator without validation.
///
/// Returns an empty slice if position is at or past the end.
#[inline(always)]
#[track_caller]
pub fn remaining_unchecked(&self) -> Result<&'info [T], AccountError> {
pub fn remaining_unchecked(self) -> Result<&'info [T], AccountError> {
if self.position >= self.accounts.len() {
Ok(&[])
} else {
Expand Down
42 changes: 21 additions & 21 deletions program-libs/account-checks/tests/account_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -627,51 +627,51 @@ fn test_boundary_position() {
assert_eq!(iter.position(), 2);
let remaining = iter.remaining().unwrap();
assert_eq!(remaining.len(), 1);

// Consume last
iter.next_account("last").unwrap();
assert!(iter.iterator_is_empty());
}

#[test]
fn test_multiple_remaining_calls() {
fn test_remaining_consumes_iterator() {
let accounts = create_pinocchio_accounts(4, false, false);
let mut iter = AccountIterator::new(&accounts);

iter.next_account("first").unwrap();

// Call remaining multiple times
let remaining1 = iter.remaining().unwrap();
let remaining2 = iter.remaining().unwrap();
let remaining3 = iter.remaining().unwrap();
// remaining() consumes the iterator
let remaining = iter.remaining().unwrap();

assert_eq!(remaining1.len(), 3);
assert_eq!(remaining2.len(), 3);
assert_eq!(remaining3.len(), 3);
assert_eq!(iter.position(), 1); // Position unchanged
assert_eq!(remaining.len(), 3);
// Iterator is consumed, cannot use it anymore
}

#[test]
fn test_remaining_unchecked_vs_remaining() {
// Test remaining() with accounts available
let accounts = create_pinocchio_accounts(2, false, false);
let mut iter = AccountIterator::new(&accounts);

// Both should work when accounts available
let iter = AccountIterator::new(&accounts);
let remaining1 = iter.remaining().unwrap();
assert_eq!(remaining1.len(), 2);

// Test remaining_unchecked() with accounts available
let accounts = create_pinocchio_accounts(2, false, false);
let iter = AccountIterator::new(&accounts);
let remaining2 = iter.remaining_unchecked().unwrap();
assert_eq!(remaining1.len(), remaining2.len());
assert_eq!(remaining2.len(), 2);

// Consume all
// Test remaining() when all consumed - should error
let accounts = create_pinocchio_accounts(2, false, false);
let mut iter = AccountIterator::new(&accounts);
iter.next_account("first").unwrap();
iter.next_account("second").unwrap();

// remaining() should error
assert_eq!(
get_error(iter.remaining()),
AccountError::NotEnoughAccountKeys
);

// remaining_unchecked() should return empty
// Test remaining_unchecked() when all consumed - should return empty
let accounts = create_pinocchio_accounts(2, false, false);
let mut iter = AccountIterator::new(&accounts);
iter.next_account("first").unwrap();
iter.next_account("second").unwrap();
let unchecked = iter.remaining_unchecked().unwrap();
assert_eq!(unchecked.len(), 0);
}
Expand Down
2 changes: 0 additions & 2 deletions programs/system/src/cpi_context/process_cpi_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,6 @@ pub fn copy_cpi_context_outputs(
}
bytes = inner_bytes;
}
// Debug assert TODO: remove pre mainnet deployment
assert_eq!(bytes.len(), 4);
}
Ok(())
}
Expand Down
Loading