Skip to content

refactor: use requires to assert and automatically log richer information on failure#65

Open
snawaz wants to merge 3 commits intosnawaz/documentfrom
snawaz/requires
Open

refactor: use requires to assert and automatically log richer information on failure#65
snawaz wants to merge 3 commits intosnawaz/documentfrom
snawaz/requires

Conversation

@snawaz
Copy link
Copy Markdown
Contributor

@snawaz snawaz commented Apr 11, 2026

  • No breaking changes
  • Standardized requires macros that not only logs values automatically but the failing expression as well which will be very useful in debugging.
  • Enforce uniformity, such as accounts destructuring is always the first line in each processor.

Question:

  • Are we passing more accounts than needed by processors?
    • Based on the existing code, I had to use require_n_accounts_with_ignored!() but I want to use the stricter form require_n_accounts!().

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 11, 2026

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 600530fb-9f07-43b9-be7d-4d4ee4d02215

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch snawaz/requires

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor Author

snawaz commented Apr 11, 2026

@snawaz snawaz requested a review from GabrielePicco April 13, 2026 11:21
Copy link
Copy Markdown
Contributor

@Dodecahedr0x Dodecahedr0x left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

require_n_accounts is more restrictive than the current implem. I don't thinks it's a bad thing (tx data is already a limit) but it's also unnecessary IMO in this PR

Plus small suggestions

@@ -0,0 +1,240 @@
///
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like requires are part of the program more than the API

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

api crate has code as well, that validates and returns error. we should use requires there as well, else we'll see only errors like InvalidAccountData in the logs without any context.

#[macro_export]
macro_rules! require {
($cond:expr, $error:expr) => {{
if !$cond {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

they can all use core::hiint::unlikely to improve perfs (tried it locally, up to 600 CU gains on some instructions, requires nightly)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I could use that. But 600 CU is very unlikely (pun not intended), because if usually consumes 4 to 6 CU.

);

assert_owner!(rent_pda_info, &pinocchio_system::ID);
require!(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assert_owner uses the more efficient address_eq instead of the internal == of owned_by. Could introduce require_owner or use require_keys_eq(unsafe { x.owner() }, y)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. I think pinocchio developers overlooked it when implementing owned_by function.

let [
queue_info, // force multi-line
_system_program_info,
] = require_n_accounts!(accounts, 2);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no equivalent

owner_info, // force multi-line
ephemeral_ata_info,
recipient_info,
] = require_n_accounts!(accounts, 3);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no equivalent

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I discussed these cases with @GabrielePicco and so the earlier form [a, b, c, ..] is made stricter and this is equivalent to [a, b, c] that ensures we do not unecessarily pass more accounts than used by the instruction, to keep the tx-size minimal.

lamports_pda_info,
payer_info,
destination_info,
] = require_n_accounts!(accounts, 4);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

contradicts the len() >= 6 below

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eagle eyes! 👁️

It seems we do not have any test for this ix because tests pass!

I used require_n_accounts_with_ignored(accounts, 4) now, that ignores additonal accounts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants