refactor: light-sdks detach account metas from account infos#1848
refactor: light-sdks detach account metas from account infos#1848ananas-block merged 3 commits intomainfrom
Conversation
|
Caution Review failedThe pull request is closed. WalkthroughThis update refactors the construction and validation of CPI account structures across the SDK, tests, and example programs. It replaces infallible constructors with fallible ones that return Changes
Sequence Diagram(s)sequenceDiagram
participant Caller
participant CpiAccounts
participant Error
Caller->>CpiAccounts: try_new_with_config(...)
alt Validation passes
CpiAccounts-->>Caller: Ok(CpiAccounts)
else Validation fails
CpiAccounts-->>Error: Return specific error variant
Error-->>Caller: Err(LightSdkTypesError)
end
sequenceDiagram
participant CpiAccounts
participant CpiInstructionConfig
participant get_account_metas_from_config
CpiAccounts->>CpiInstructionConfig: TryFrom<&CpiAccounts>
CpiInstructionConfig-->>get_account_metas_from_config: get_account_metas_from_config(config)
get_account_metas_from_config-->>CpiInstructionConfig: Vec<AccountMeta>
Possibly related PRs
Suggested reviewers
Poem
📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (1)
✨ Finishing Touches
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. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
6b12c7a to
12f5cfd
Compare
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
sdk-libs/sdk/src/cpi/accounts.rs (1)
26-134: Optimize performance by avoiding unnecessary clonesThe function logic is correct and well-documented, but there's a performance issue with cloning
light_system_program_metamultiple times.Consider this optimization to avoid repeated clones:
- // 8. Light System Program (readonly) - let light_system_program_meta = AccountMeta { - pubkey: Pubkey::from(LIGHT_SYSTEM_PROGRAM_ID), - is_signer: false, - is_writable: false, - }; + // 8. Light System Program (readonly) - reused for optional accounts + let light_system_program_pubkey = Pubkey::from(LIGHT_SYSTEM_PROGRAM_ID); + + let create_light_system_meta = || AccountMeta { + pubkey: light_system_program_pubkey, + is_signer: false, + is_writable: false, + }; // 9. Sol Pool PDA (writable) OR Light System Program (readonly) if let Some(sol_pool_pda_pubkey) = config.sol_pool_pda_pubkey { account_metas.push(AccountMeta { pubkey: sol_pool_pda_pubkey, is_signer: false, is_writable: true, }); } else { - account_metas.push(light_system_program_meta.clone()); + account_metas.push(create_light_system_meta()); } // 10. Sol Compression Recipient (writable) OR Light System Program (readonly) if let Some(sol_compression_recipient_pubkey) = config.sol_compression_recipient_pubkey { account_metas.push(AccountMeta { pubkey: sol_compression_recipient_pubkey, is_signer: false, is_writable: true, }); } else { - account_metas.push(light_system_program_meta.clone()); + account_metas.push(create_light_system_meta()); } // Skip to line 122... } else { - account_metas.push(light_system_program_meta); + account_metas.push(create_light_system_meta()); }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (1)
Cargo.lockis excluded by!**/*.lock
📒 Files selected for processing (16)
examples/anchor/token-escrow/src/escrow_with_compressed_pda/escrow.rs(1 hunks)examples/anchor/token-escrow/src/escrow_with_compressed_pda/withdrawal.rs(1 hunks)program-libs/account-checks/src/account_info/account_info_trait.rs(1 hunks)program-tests/create-address-test-program/src/lib.rs(2 hunks)program-tests/sdk-pinocchio-test/src/create_pda.rs(1 hunks)program-tests/sdk-pinocchio-test/src/update_pda.rs(1 hunks)program-tests/sdk-test/src/create_pda.rs(1 hunks)program-tests/sdk-test/src/update_pda.rs(1 hunks)sdk-libs/sdk-pinocchio/src/error.rs(4 hunks)sdk-libs/sdk-types/Cargo.toml(1 hunks)sdk-libs/sdk-types/src/constants.rs(2 hunks)sdk-libs/sdk-types/src/cpi_accounts.rs(2 hunks)sdk-libs/sdk-types/src/error.rs(3 hunks)sdk-libs/sdk/src/cpi/accounts.rs(1 hunks)sdk-libs/sdk/src/cpi/invoke.rs(6 hunks)sdk-libs/sdk/src/error.rs(4 hunks)
🚧 Files skipped from review as they are similar to previous changes (15)
- program-tests/sdk-pinocchio-test/src/create_pda.rs
- examples/anchor/token-escrow/src/escrow_with_compressed_pda/escrow.rs
- sdk-libs/sdk-types/Cargo.toml
- program-tests/sdk-test/src/create_pda.rs
- examples/anchor/token-escrow/src/escrow_with_compressed_pda/withdrawal.rs
- program-tests/sdk-test/src/update_pda.rs
- program-libs/account-checks/src/account_info/account_info_trait.rs
- program-tests/sdk-pinocchio-test/src/update_pda.rs
- program-tests/create-address-test-program/src/lib.rs
- sdk-libs/sdk/src/cpi/invoke.rs
- sdk-libs/sdk-types/src/constants.rs
- sdk-libs/sdk-types/src/error.rs
- sdk-libs/sdk/src/error.rs
- sdk-libs/sdk-types/src/cpi_accounts.rs
- sdk-libs/sdk-pinocchio/src/error.rs
⏰ Context from checks skipped due to timeout of 90000ms (23)
- GitHub Check: cli-v1
- GitHub Check: cli-v2
- GitHub Check: lint
- GitHub Check: system-programs (sdk-test-program, ["cargo-test-sbf -p sdk-test"])
- GitHub Check: system-programs (counter-test, ["cargo test-sbf -p counter"])
- GitHub Check: system-programs (sdk-anchor-test-program, ["cargo-test-sbf -p sdk-anchor-test", "cargo-test-sbf -...
- GitHub Check: system-programs (token-escrow-test, [ "cargo test-sbf -p token-escrow" ])
- GitHub Check: stateless-js-v1
- GitHub Check: Test program-libs-slow
- GitHub Check: Test concurrent-merkle-tree
- GitHub Check: Test batched-merkle-tree-simulate
- GitHub Check: Test sdk-libs
- GitHub Check: Test program-libs-fast
- GitHub Check: stateless-js-v2
- GitHub Check: system-programs (system-cpi-test, ["cargo-test-sbf -p system-cpi-test"])
- GitHub Check: system-programs (random-e2e-test, ["cargo-test-sbf -p e2e-test"])
- GitHub Check: system-programs (system-cpi-test-v2-other, ["cargo-test-sbf -p system-cpi-v2-test -- --skip funct...
- GitHub Check: system-programs (system-cpi-test-v2-functional, ["cargo-test-sbf -p system-cpi-v2-test -- functio...
- GitHub Check: system-programs (light-compressed-token, ["cargo-test-sbf -p compressed-token-test"])
- GitHub Check: system-programs (account-compression, ["cargo-test-sbf -p account-compression-test"])
- GitHub Check: system-programs (light-system-program, ["cargo-test-sbf -p system-test"])
- GitHub Check: system-programs (system-cpi-test-v2-event, ["cargo-test-sbf -p system-cpi-v2-test -- event::parse"])
- GitHub Check: system-programs (light-registry, ["cargo-test-sbf -p registry-test"])
🔇 Additional comments (3)
sdk-libs/sdk/src/cpi/accounts.rs (3)
2-6: LGTM: Clean import organizationThe additional imports are well-organized and necessary for the new functionality. The explicit import of constants improves code readability.
13-22: Well-designed configuration structThe
CpiInstructionConfigstruct provides a clean separation of configuration data from conversion logic. The use of optional pubkeys for conditional accounts is elegant and type-safe.
136-162: Robust error handling in conversion traitThe
TryFromimplementation correctly handles the conversion fromCpiAccountstoCpiInstructionConfigwith proper error propagation. The logic appropriately checks configuration flags before attempting to access optional accounts.
12f5cfd to
0ad4311
Compare
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
sdk-libs/sdk/src/cpi/accounts.rs (1)
26-134: Excellent implementation with detailed documentationThe function is well-structured with clear account ordering and comprehensive comments. The performance optimization using
Vec::with_capacityand proper handling of optional accounts with fallback tolight_system_program_metais good practice.Minor optimization opportunity: Consider reducing the repeated cloning of
light_system_program_meta:- // 8. Light System Program (readonly) - let light_system_program_meta = AccountMeta { - pubkey: Pubkey::from(LIGHT_SYSTEM_PROGRAM_ID), - is_signer: false, - is_writable: false, - }; + // 8. Light System Program (readonly) - reused as fallback + let light_system_program_meta = &AccountMeta { + pubkey: Pubkey::from(LIGHT_SYSTEM_PROGRAM_ID), + is_signer: false, + is_writable: false, + };Then use
*light_system_program_metainstead of.clone()calls.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (1)
Cargo.lockis excluded by!**/*.lock
📒 Files selected for processing (16)
examples/anchor/token-escrow/src/escrow_with_compressed_pda/escrow.rs(1 hunks)examples/anchor/token-escrow/src/escrow_with_compressed_pda/withdrawal.rs(1 hunks)program-libs/account-checks/src/account_info/account_info_trait.rs(1 hunks)program-tests/create-address-test-program/src/lib.rs(2 hunks)program-tests/sdk-pinocchio-test/src/create_pda.rs(1 hunks)program-tests/sdk-pinocchio-test/src/update_pda.rs(1 hunks)program-tests/sdk-test/src/create_pda.rs(1 hunks)program-tests/sdk-test/src/update_pda.rs(1 hunks)sdk-libs/sdk-pinocchio/src/error.rs(4 hunks)sdk-libs/sdk-types/Cargo.toml(1 hunks)sdk-libs/sdk-types/src/constants.rs(2 hunks)sdk-libs/sdk-types/src/cpi_accounts.rs(3 hunks)sdk-libs/sdk-types/src/error.rs(3 hunks)sdk-libs/sdk/src/cpi/accounts.rs(1 hunks)sdk-libs/sdk/src/cpi/invoke.rs(6 hunks)sdk-libs/sdk/src/error.rs(4 hunks)
🚧 Files skipped from review as they are similar to previous changes (15)
- sdk-libs/sdk-types/Cargo.toml
- program-tests/sdk-pinocchio-test/src/update_pda.rs
- program-tests/sdk-test/src/update_pda.rs
- program-libs/account-checks/src/account_info/account_info_trait.rs
- program-tests/sdk-test/src/create_pda.rs
- examples/anchor/token-escrow/src/escrow_with_compressed_pda/escrow.rs
- program-tests/sdk-pinocchio-test/src/create_pda.rs
- examples/anchor/token-escrow/src/escrow_with_compressed_pda/withdrawal.rs
- program-tests/create-address-test-program/src/lib.rs
- sdk-libs/sdk-types/src/constants.rs
- sdk-libs/sdk-types/src/error.rs
- sdk-libs/sdk/src/cpi/invoke.rs
- sdk-libs/sdk-types/src/cpi_accounts.rs
- sdk-libs/sdk-pinocchio/src/error.rs
- sdk-libs/sdk/src/error.rs
⏰ Context from checks skipped due to timeout of 90000ms (24)
- GitHub Check: Test program-libs-fast
- GitHub Check: Test program-libs-slow
- GitHub Check: Test batched-merkle-tree-simulate
- GitHub Check: Test concurrent-merkle-tree
- GitHub Check: Test sdk-libs
- GitHub Check: system-programs (system-cpi-test-v2-event, ["cargo-test-sbf -p system-cpi-v2-test -- event::parse"])
- GitHub Check: system-programs (system-cpi-test-v2-functional, ["cargo-test-sbf -p system-cpi-v2-test -- functio...
- GitHub Check: system-programs (system-cpi-test-v2-other, ["cargo-test-sbf -p system-cpi-v2-test -- --skip funct...
- GitHub Check: system-programs (account-compression, ["cargo-test-sbf -p account-compression-test"])
- GitHub Check: system-programs (random-e2e-test, ["cargo-test-sbf -p e2e-test"])
- GitHub Check: system-programs (system-cpi-test, ["cargo-test-sbf -p system-cpi-test"])
- GitHub Check: system-programs (light-compressed-token, ["cargo-test-sbf -p compressed-token-test"])
- GitHub Check: system-programs (light-system-program, ["cargo-test-sbf -p system-test"])
- GitHub Check: system-programs (light-registry, ["cargo-test-sbf -p registry-test"])
- GitHub Check: lint
- GitHub Check: stateless-js-v1
- GitHub Check: cli-v1
- GitHub Check: system-programs (sdk-test-program, ["cargo-test-sbf -p sdk-test"])
- GitHub Check: cli-v2
- GitHub Check: system-programs (counter-test, ["cargo test-sbf -p counter"])
- GitHub Check: system-programs (sdk-anchor-test-program, ["cargo-test-sbf -p sdk-anchor-test", "cargo-test-sbf -...
- GitHub Check: system-programs (token-escrow-test, [ "cargo test-sbf -p token-escrow" ])
- GitHub Check: stateless-js-v2
- GitHub Check: Analyze (javascript-typescript)
🔇 Additional comments (3)
sdk-libs/sdk/src/cpi/accounts.rs (3)
2-6: LGTM: Clean import organizationThe imports are well-organized and include all necessary constants and types for the refactored functionality.
13-22: LGTM: Well-designed configuration structThe
CpiInstructionConfigstruct appropriately separates configuration data from conversion logic. The use ofOption<Pubkey>for optional accounts and lifetime parameters for borrowed data is correct.
136-162: Verify consistency ofcpi_signeraccessI wasn’t able to locate the definition of
config()onCpiAccounts<'a, 'info>or its return type in the codebase. Please confirm that:
- The
config()method onCpiAccountsreturns a type defining both:
- A
cpi_signer()method (for retrieving the signer key)- A
cpi_signerfield (with a.program_idfor the invoking program)- Using the method for the signer key and the field for the program ID is intentional and correct
- If this mix is unintended, consider unifying the access pattern for clarity
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
sdk-libs/sdk/src/cpi/accounts.rs (1)
26-134: Improve vector capacity calculation for better performance.The vector capacity calculation
1 + SYSTEM_ACCOUNTS_LENmay not accurately reflect the actual number of accounts that will be added, especially considering optional accounts and variable-length packed accounts. This could lead to unnecessary reallocations.Consider calculating a more accurate initial capacity:
- let mut account_metas = Vec::with_capacity(1 + SYSTEM_ACCOUNTS_LEN); + let base_accounts = 1 + SYSTEM_ACCOUNTS_LEN; + let optional_accounts = [ + config.sol_pool_pda_pubkey.is_some(), + config.sol_compression_recipient_pubkey.is_some(), + config.cpi_context_pubkey.is_some() + ].iter().count(); + let mut account_metas = Vec::with_capacity(base_accounts + optional_accounts + config.packed_accounts.len());The rest of the function implementation is well-structured with clear comments and proper conditional logic for optional accounts.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
sdk-libs/sdk-types/src/cpi_accounts.rs(4 hunks)sdk-libs/sdk/src/cpi/accounts.rs(1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
- sdk-libs/sdk-types/src/cpi_accounts.rs
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (23)
- GitHub Check: system-programs (system-cpi-test-v2-event, ["cargo-test-sbf -p system-cpi-v2-test -- event::parse"])
- GitHub Check: system-programs (random-e2e-test, ["cargo-test-sbf -p e2e-test"])
- GitHub Check: cli-v1
- GitHub Check: system-programs (system-cpi-test-v2-functional, ["cargo-test-sbf -p system-cpi-v2-test -- functio...
- GitHub Check: system-programs (light-system-program, ["cargo-test-sbf -p system-test"])
- GitHub Check: system-programs (system-cpi-test-v2-other, ["cargo-test-sbf -p system-cpi-v2-test -- --skip funct...
- GitHub Check: system-programs (light-registry, ["cargo-test-sbf -p registry-test"])
- GitHub Check: system-programs (system-cpi-test, ["cargo-test-sbf -p system-cpi-test"])
- GitHub Check: system-programs (light-compressed-token, ["cargo-test-sbf -p compressed-token-test"])
- GitHub Check: lint
- GitHub Check: system-programs (account-compression, ["cargo-test-sbf -p account-compression-test"])
- GitHub Check: cli-v2
- GitHub Check: stateless-js-v2
- GitHub Check: stateless-js-v1
- GitHub Check: Test sdk-libs
- GitHub Check: system-programs (sdk-test-program, ["cargo-test-sbf -p sdk-test"])
- GitHub Check: system-programs (token-escrow-test, [ "cargo test-sbf -p token-escrow" ])
- GitHub Check: system-programs (sdk-anchor-test-program, ["cargo-test-sbf -p sdk-anchor-test", "cargo-test-sbf -...
- GitHub Check: system-programs (counter-test, ["cargo test-sbf -p counter"])
- GitHub Check: Test program-libs-slow
- GitHub Check: Test program-libs-fast
- GitHub Check: Test concurrent-merkle-tree
- GitHub Check: Test batched-merkle-tree-simulate
🔇 Additional comments (3)
sdk-libs/sdk/src/cpi/accounts.rs (3)
2-6: LGTM! Imports support the new CPI account configuration.The new imports are appropriate for the refactored CPI account handling functionality.
13-22: Well-designed configuration struct.The
CpiInstructionConfigstruct effectively separates configuration data from conversion logic. The use of lifetime parameters and optional pubkeys provides appropriate flexibility for different CPI scenarios.
136-162: Excellent TryFrom implementation with proper error handling.The conversion logic correctly extracts all necessary data from
CpiAccountsand handles optional accounts appropriately. The use of the?operator for error propagation andunwrap_or(&[])for safe defaults demonstrates good error handling practices.
Co-authored-by: Swen Schäferjohann <42959314+SwenSchaeferjohann@users.noreply.github.com>
Summary by CodeRabbit
New Features
Refactor
Chores
Style