fix(lang): Include system program AccountInfo in CPI invoke calls#4258
Open
awneesht wants to merge 1 commit intosolana-foundation:masterfrom
Open
fix(lang): Include system program AccountInfo in CPI invoke calls#4258awneesht wants to merge 1 commit intosolana-foundation:masterfrom
awneesht wants to merge 1 commit intosolana-foundation:masterfrom
Conversation
…lana-foundation#4132) When realloc or init constraints are used in a program invoked via CPI, the generated system program calls (transfer, create_account, allocate, assign) omitted the system program's AccountInfo from invoke, causing "sum of account balances before and after instruction do not match" at CPI depth 2+. Replace wrapper calls with direct invoke/invoke_signed that include system_program.to_account_info() in all codegen paths.
|
Someone is attempting to deploy a commit to the Solana Foundation Team on Vercel. A member of the Team first needs to authorize it. |
Author
|
@jamie-osec Would you be able to review this PR when you get a chance? Thanks! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #4132
Root cause: Anchor's codegen for
reallocandinitconstraints used system program wrapper functions (transfer,create_account,allocate,assign) that calledinvoke_signedwithout including the system program'sAccountInfo. This works at CPI depth 1 (runtime resolves from transaction accounts) but fails at depth 2+ with"sum of account balances before and after instruction do not match".Fix: Replace all wrapper calls in
constraints.rsandidl.rswith directinvoke/invoke_signedthat includesystem_program.to_account_info()in the account infos array. Usesinvoke(notinvoke_signed) for transfer operations where the payer is already aSigner, avoiding unnecessary signer seed lookups. This also eliminates heap allocations fromCpiContext/struct construction.Files changed:
lang/syn/src/codegen/accounts/constraints.rs— Fixedgenerate_constraint_realloc()andgenerate_create_account()(coversrealloc,init,init_if_neededfor all account types)lang/syn/src/codegen/program/idl.rs— Fixed__idl_resize_accounttests/realloc-cpi-bug/— New reproduction test with caller/callee CPI patternTest plan
tests/realloc-cpi-bug/— 5/5 passing (direct realloc, CPI realloc grow, CPI shrink, CPI grow-after-shrink)tests/realloc/— 5/5 passing (no regressions)tests/cpi-returns/— 11/11 passing (verifiesinitcodegen path)cargo test -p anchor-syn— passes