Skip to content

Fix realloc message packer for exact multiples of the realloc limit - #2071

Merged
lorisleiva merged 1 commit into
mainfrom
09-18-fix_realloc_message_packer_for_exact_multiples_of_the_realloc_limit
Sep 18, 2026
Merged

lorisleiva merged 1 commit into
mainfrom
09-18-fix_realloc_message_packer_for_exact_multiples_of_the_realloc_limit

Conversation

@lorisleiva

Copy link
Copy Markdown
Member

This PR fixes getReallocMessagePackerInstructionPlan emitting a 0-byte instruction as its last chunk whenever totalSize is an exact multiple of the 10,240-byte realloc limit. The size of the last instruction was computed as totalSize % REALLOC_LIMIT, which is zero in that case, so the account ended up one full chunk short of the requested size:

totalSize   before            after
10_240      [0]               [10240]
20_480      [10240, 0]        [10240, 10240]
15_000      [10240, 4760]     [10240, 4760]   (unchanged)
0           []                []              (unchanged)

The chunk sizes are now derived by walking the remaining size down in steps of at most REALLOC_LIMIT:

const instructions: Instruction[] = [];
for (let remaining = totalSize; remaining > 0; remaining -= REALLOC_LIMIT) {
    instructions.push(getInstruction(Math.min(REALLOC_LIMIT, remaining)));
}

Tests cover a single full chunk, two full chunks and a zero total size alongside the existing partial-chunk case.

This PR fixes `getReallocMessagePackerInstructionPlan` emitting a 0-byte instruction as its last chunk whenever `totalSize` is an exact multiple of the 10,240-byte realloc limit. The size of the last instruction was computed as `totalSize % REALLOC_LIMIT`, which is zero in that case, so the account ended up one full chunk short of the requested size:

```
totalSize   before            after
10_240      [0]               [10240]
20_480      [10240, 0]        [10240, 10240]
15_000      [10240, 4760]     [10240, 4760]   (unchanged)
0           []                []              (unchanged)
```

The chunk sizes are now derived by walking the remaining size down in steps of at most `REALLOC_LIMIT`:

```ts
const instructions: Instruction[] = [];
for (let remaining = totalSize; remaining > 0; remaining -= REALLOC_LIMIT) {
    instructions.push(getInstruction(Math.min(REALLOC_LIMIT, remaining)));
}
```

Tests cover a single full chunk, two full chunks and a zero total size alongside the existing partial-chunk case.
@changeset-bot

changeset-bot Bot commented Sep 18, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: faa047a

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 48 packages
Name Type
@solana/instruction-plans Patch
@solana/kit Patch
@solana/plugin-interfaces Patch
@solana/program-client-core Patch
@solana/react Patch
@solana/accounts Patch
@solana/addresses Patch
@solana/assertions Patch
@solana/codecs-core Patch
@solana/codecs-data-structures Patch
@solana/codecs-numbers Patch
@solana/codecs-strings Patch
@solana/codecs Patch
@solana/compat Patch
@solana/errors Patch
@solana/fast-stable-stringify Patch
@solana/fixed-points Patch
@solana/functional Patch
@solana/instructions Patch
@solana/keys Patch
@solana/nominal-types Patch
@solana/offchain-messages Patch
@solana/options Patch
@solana/plugin-core Patch
@solana/programs Patch
@solana/promises Patch
@solana/rpc-api Patch
@solana/rpc-graphql Patch
@solana/rpc-parsed-types Patch
@solana/rpc-spec-types Patch
@solana/rpc-spec Patch
@solana/rpc-subscriptions-api Patch
@solana/rpc-subscriptions-channel-websocket Patch
@solana/rpc-subscriptions-spec Patch
@solana/rpc-subscriptions Patch
@solana/rpc-transformers Patch
@solana/rpc-transport-http Patch
@solana/rpc-types Patch
@solana/rpc Patch
@solana/signers Patch
@solana/subscribable Patch
@solana/sysvars Patch
@solana/transaction-confirmation Patch
@solana/transaction-introspection Patch
@solana/transaction-messages Patch
@solana/transactions Patch
@solana/wallet-account-signer Patch
@solana/webcrypto-ed25519-polyfill Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link
Copy Markdown
Member Author

This stack of pull requests is managed by Graphite. Learn more about stacking.

@bundlemon

bundlemon Bot commented Sep 18, 2026

Copy link
Copy Markdown

BundleMon

Files updated (4)
Status Path Size Limits
✅ @solana/kit production bundle
kit/dist/index.production.min.js
57.45KB (-11B -0.02%) -
✅ instruction-plans/dist/index.browser.mjs
7.31KB (-39B -0.52%) -
✅ instruction-plans/dist/index.native.mjs
7.3KB (-39B -0.52%) -
✅ instruction-plans/dist/index.node.mjs
7.3KB (-40B -0.53%) -
Unchanged files (146)
Status Path Size Limits
✅ errors/dist/index.node.mjs
22.16KB -
✅ errors/dist/index.browser.mjs
22.14KB -
✅ errors/dist/index.native.mjs
22.14KB -
✅ rpc-graphql/dist/index.browser.mjs
18.87KB -
✅ rpc-graphql/dist/index.native.mjs
18.87KB -
✅ rpc-graphql/dist/index.node.mjs
18.86KB -
✅ wallet-account-signer/dist/index.node.mjs
18.81KB -
✅ wallet-account-signer/dist/index.native.mjs
18.79KB -
✅ wallet-account-signer/dist/index.browser.mjs
18.79KB -
✅ transaction-messages/dist/index.browser.mjs
11.84KB -
✅ transaction-messages/dist/index.native.mjs
11.83KB -
✅ transaction-messages/dist/index.node.mjs
11.83KB -
✅ react/dist/index.browser.mjs
5.32KB -
✅ react/dist/index.native.mjs
5.32KB -
✅ react/dist/index.node.mjs
5.32KB -
✅ codecs-data-structures/dist/index.browser.mjs
5.31KB -
✅ codecs-data-structures/dist/index.native.mjs
5.31KB -
✅ codecs-data-structures/dist/index.node.mjs
5.31KB -
✅ offchain-messages/dist/index.browser.mjs
5.25KB -
✅ offchain-messages/dist/index.native.mjs
5.24KB -
✅ offchain-messages/dist/index.node.mjs
5.24KB -
✅ fixed-points/dist/index.browser.mjs
5.08KB -
✅ fixed-points/dist/index.native.mjs
5.07KB -
✅ fixed-points/dist/index.node.mjs
5.07KB -
✅ kit/dist/index.browser.mjs
4.69KB -
✅ kit/dist/index.native.mjs
4.69KB -
✅ kit/dist/index.node.mjs
4.69KB -
✅ transactions/dist/index.browser.mjs
4.05KB -
✅ transactions/dist/index.native.mjs
4.05KB -
✅ transactions/dist/index.node.mjs
4.05KB -
✅ codecs-core/dist/index.browser.mjs
3.81KB -
✅ codecs-core/dist/index.native.mjs
3.81KB -
✅ codecs-core/dist/index.node.mjs
3.81KB -
✅ webcrypto-ed25519-polyfill/dist/index.node.mj
s
3.61KB -
✅ webcrypto-ed25519-polyfill/dist/index.browser
.mjs
3.59KB -
✅ webcrypto-ed25519-polyfill/dist/index.native.
mjs
3.57KB -
✅ signers/dist/index.browser.mjs
3.5KB -
✅ signers/dist/index.native.mjs
3.5KB -
✅ signers/dist/index.node.mjs
3.5KB -
✅ rpc-subscriptions/dist/index.browser.mjs
3.37KB -
✅ rpc-subscriptions/dist/index.node.mjs
3.34KB -
✅ rpc-subscriptions/dist/index.native.mjs
3.31KB -
✅ codecs-strings/dist/index.browser.mjs
3.29KB -
✅ codecs-strings/dist/index.node.mjs
3.17KB -
✅ codecs-strings/dist/index.native.mjs
3.13KB -
✅ subscribable/dist/index.node.mjs
3.13KB -
✅ rpc-transformers/dist/index.browser.mjs
3.1KB -
✅ rpc-transformers/dist/index.native.mjs
3.1KB -
✅ rpc-transformers/dist/index.node.mjs
3.1KB -
✅ keys/dist/index.node.mjs
3.06KB -
✅ subscribable/dist/index.native.mjs
3.06KB -
✅ subscribable/dist/index.browser.mjs
3.05KB -
✅ addresses/dist/index.browser.mjs
2.93KB -
✅ addresses/dist/index.native.mjs
2.92KB -
✅ addresses/dist/index.node.mjs
2.92KB -
✅ keys/dist/index.browser.mjs
2.85KB -
✅ keys/dist/index.native.mjs
2.85KB -
✅ transaction-introspection/dist/index.browser.
mjs
2.73KB -
✅ transaction-introspection/dist/index.native.m
js
2.73KB -
✅ transaction-introspection/dist/index.node.mjs
2.73KB -
✅ transaction-confirmation/dist/index.node.mjs
2.42KB -
✅ transaction-confirmation/dist/index.native.mj
s
2.37KB -
✅ sysvars/dist/index.browser.mjs
2.37KB -
✅ sysvars/dist/index.native.mjs
2.37KB -
✅ sysvars/dist/index.node.mjs
2.37KB -
✅ transaction-confirmation/dist/index.browser.m
js
2.36KB -
✅ rpc-subscriptions-spec/dist/index.node.mjs
2.33KB -
✅ rpc-subscriptions-spec/dist/index.native.mjs
2.29KB -
✅ rpc-subscriptions-spec/dist/index.browser.mjs
2.29KB -
✅ codecs-numbers/dist/index.browser.mjs
2.18KB -
✅ codecs-numbers/dist/index.native.mjs
2.18KB -
✅ codecs-numbers/dist/index.node.mjs
2.18KB -
✅ rpc/dist/index.node.mjs
1.94KB -
✅ rpc-transport-http/dist/index.browser.mjs
1.9KB -
✅ rpc-types/dist/index.browser.mjs
1.9KB -
✅ rpc-types/dist/index.native.mjs
1.9KB -
✅ rpc-types/dist/index.node.mjs
1.9KB -
✅ rpc-transport-http/dist/index.native.mjs
1.9KB -
✅ rpc/dist/index.native.mjs
1.8KB -
✅ rpc/dist/index.browser.mjs
1.79KB -
✅ rpc-transport-http/dist/index.node.mjs
1.71KB -
✅ rpc-spec-types/dist/index.browser.mjs
1.54KB -
✅ rpc-spec-types/dist/index.native.mjs
1.54KB -
✅ rpc-spec-types/dist/index.node.mjs
1.54KB -
✅ program-client-core/dist/index.browser.mjs
1.39KB -
✅ program-client-core/dist/index.native.mjs
1.39KB -
✅ program-client-core/dist/index.node.mjs
1.39KB -
✅ rpc-subscriptions-channel-websocket/dist/inde
x.node.mjs
1.33KB -
✅ rpc-subscriptions-channel-websocket/dist/inde
x.native.mjs
1.27KB -
✅ rpc-subscriptions-channel-websocket/dist/inde
x.browser.mjs
1.26KB -
✅ plugin-core/dist/index.browser.mjs
1.18KB -
✅ plugin-core/dist/index.native.mjs
1.18KB -
✅ plugin-core/dist/index.node.mjs
1.18KB -
✅ options/dist/index.browser.mjs
1.18KB -
✅ options/dist/index.native.mjs
1.18KB -
✅ options/dist/index.node.mjs
1.17KB -
✅ accounts/dist/index.browser.mjs
1.17KB -
✅ accounts/dist/index.native.mjs
1.17KB -
✅ accounts/dist/index.node.mjs
1.16KB -
✅ rpc-api/dist/index.browser.mjs
1.08KB -
✅ rpc-api/dist/index.native.mjs
1.08KB -
✅ rpc-api/dist/index.node.mjs
1.08KB -
✅ compat/dist/index.browser.mjs
969B -
✅ compat/dist/index.native.mjs
968B -
✅ compat/dist/index.node.mjs
966B -
✅ rpc-spec/dist/index.browser.mjs
928B -
✅ rpc-spec/dist/index.native.mjs
928B -
✅ rpc-spec/dist/index.node.mjs
926B -
✅ promises/dist/index.native.mjs
841B -
✅ promises/dist/index.node.mjs
840B -
✅ promises/dist/index.browser.mjs
839B -
✅ rpc-subscriptions-api/dist/index.browser.mjs
810B -
✅ rpc-subscriptions-api/dist/index.native.mjs
808B -
✅ rpc-subscriptions-api/dist/index.node.mjs
807B -
✅ assertions/dist/index.browser.mjs
783B -
✅ instructions/dist/index.browser.mjs
771B -
✅ instructions/dist/index.native.mjs
770B -
✅ instructions/dist/index.node.mjs
768B -
✅ fast-stable-stringify/dist/index.browser.mjs
726B -
✅ fast-stable-stringify/dist/index.native.mjs
725B -
✅ assertions/dist/index.native.mjs
724B -
✅ fast-stable-stringify/dist/index.node.mjs
724B -
✅ assertions/dist/index.node.mjs
723B -
✅ programs/dist/index.browser.mjs
329B -
✅ programs/dist/index.native.mjs
327B -
✅ programs/dist/index.node.mjs
325B -
✅ fs-impl/dist/index.browser.mjs
245B -
✅ event-target-impl/dist/index.node.mjs
230B -
✅ functional/dist/index.browser.mjs
154B -
✅ functional/dist/index.native.mjs
152B -
✅ text-encoding-impl/dist/index.native.mjs
152B -
✅ functional/dist/index.node.mjs
151B -
✅ codecs/dist/index.browser.mjs
145B -
✅ codecs/dist/index.native.mjs
144B -
✅ codecs/dist/index.node.mjs
142B -
✅ event-target-impl/dist/index.browser.mjs
133B -
✅ ws-impl/dist/index.node.mjs
131B -
✅ text-encoding-impl/dist/index.browser.mjs
122B -
✅ fs-impl/dist/index.node.mjs
120B -
✅ text-encoding-impl/dist/index.node.mjs
119B -
✅ ws-impl/dist/index.browser.mjs
113B -
✅ crypto-impl/dist/index.node.mjs
111B -
✅ crypto-impl/dist/index.browser.mjs
109B -
✅ rpc-parsed-types/dist/index.browser.mjs
66B -
✅ rpc-parsed-types/dist/index.native.mjs
65B -
✅ rpc-parsed-types/dist/index.node.mjs
63B -

Total files change -129B -0.02%

Final result: ✅

View report in BundleMon website ➡️


Current branch size history | Target branch size history

@lorisleiva

Copy link
Copy Markdown
Member Author

@trevor-cortex

@trevor-cortex trevor-cortex left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Summary

Fixes an off-by-one-chunk bug in getReallocMessagePackerInstructionPlan: when totalSize was an exact multiple of REALLOC_LIMIT, totalSize % REALLOC_LIMIT evaluated to 0 for the last instruction, so the plan emitted a useless 0-byte realloc and left the account one full chunk short. The ceil/% arithmetic is replaced with a simple loop that walks remaining down by REALLOC_LIMIT and emits min(REALLOC_LIMIT, remaining) per step. The docblock is updated to match, and three tests cover the previously-broken cases (single full chunk, two full chunks, zero size) alongside the existing partial-chunk test. A patch changeset is included for @solana/instruction-plans.

Verification

I traced the new loop by hand:

totalSize emitted sizes
0 []
10_240 [10240]
15_000 [10240, 4760]
20_480 [10240, 10240]

All match the PR description. The zero-size test relies on getMessagePackerInstructionPlanFromInstructions([]) behaving sensibly — I checked, and done() is 0 >= 0 === true while packMessageToCapacity hits the ALREADY_COMPLETE guard first, so the assertions are correct.

One incidental improvement worth noting (not a concern): the old code would have thrown a RangeError from new Array(-1) for a sufficiently negative totalSize (e.g. -20_000, since Math.ceil(-1.95) === -1). The loop now returns an empty plan for any non-positive totalSize, which is a more graceful failure mode for an input that's nonsensical anyway.

Notes for other reviewers

  • The loop is the entire behavioural change; the rest is docs, tests and the changeset. Nothing else in the file is touched.
  • patch is the right bump per CLAUDE.md (bug fix), and the changeset targets the correct package.
  • Tests follow the same shape as the existing getReallocMessagePackerInstructionPlan tests in the file (same createInstruction/message helpers, same toStrictEqual on instructions), so nothing new to learn there.

LGTM — I'd approve, but my token lacks approve rights on this repo, so posting as a comment.

@lorisleiva
lorisleiva marked this pull request as ready for review September 18, 2026 08:45

@mcintyre94 mcintyre94 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Good spot!

@lorisleiva
lorisleiva added this pull request to the merge queue Sep 18, 2026
Merged via the queue into main with commit 75653e9 Sep 18, 2026
16 checks passed
@lorisleiva
lorisleiva deleted the 09-18-fix_realloc_message_packer_for_exact_multiples_of_the_realloc_limit branch September 18, 2026 11:11
@github-actions

Copy link
Copy Markdown
Contributor

🔎💬 Inkeep AI search and chat service is syncing content for source 'Solana Kit Docs'

This was referenced Sep 18, 2026
@github-actions

github-actions Bot commented Oct 5, 2026

Copy link
Copy Markdown
Contributor

Because there has been no activity on this PR for 14 days since it was merged, it has been automatically locked. Please open a new issue if it requires a follow up.

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Oct 5, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants