Skip to content

refactor(basics): replace hand-rolled byte packing with kit codecs - #677

Open
amilz wants to merge 1 commit into
mainfrom
fix/kit-codecs-pinocchio-byte-packing
Open

refactor(basics): replace hand-rolled byte packing with kit codecs#677
amilz wants to merge 1 commit into
mainfrom
fix/kit-codecs-pinocchio-byte-packing

Conversation

@amilz

@amilz amilz commented Aug 6, 2026

Copy link
Copy Markdown
Collaborator

What changed

Replaced hand-rolled instruction/account byte packing in the basics/ TypeScript tests with @solana/kit codecs, matching the pattern already used by tokens/transfer-tokens/pinocchio/tests/test.ts.

Out goes Buffer.alloc + writeUInt8/writeUInt32LE/writeBigUInt64LE, Buffer.concat, DataView.setBigUint64, and manual fixed-width string padding/trimming. In come getStructEncoder/getStructDecoder composed from getU8, getU32, getU64, getArray, and fixEncoderSize/fixDecoderSize over getUtf8.

Where a test read its own program's account data by byte offset, it now decodes through a getStructDecoder that mirrors the on-chain layout (favorites, realloc, account-data, program-derived-addresses, close-account).

basics/counter/native additionally drops the bn.js / @types/bn.js dependencies — deserializeCounterAccount now returns a native bigint via getU64Decoder(), and tests/counter.test.ts compares against bigint literals. Lockfile regenerated.

Files

  • basics/transfer-sol/pinocchio/tests/test.ts
  • basics/transfer-sol/asm/tests/instruction.ts
  • basics/pda-rent-payer/pinocchio/tests/test.ts
  • basics/program-derived-addresses/pinocchio/tests/test.ts
  • basics/processing-instructions/pinocchio/tests/test.ts
  • basics/favorites/pinocchio/tests/test.ts
  • basics/realloc/pinocchio/tests/realloc.test.ts
  • basics/account-data/pinocchio/tests/index.test.ts
  • basics/close-account/pinocchio/tests/close-account.test.ts
  • basics/counter/native/ts/accounts/counter.ts
  • basics/counter/native/tests/counter.test.ts
  • basics/counter/native/package.json + pnpm-lock.yaml

Test evidence

npx tsc --noEmit clean in every touched package, and pnpm build-and-test (cargo build-sbf + mocha) green:

Package Result
basics/transfer-sol/pinocchio 2 passing
basics/pda-rent-payer/pinocchio 2 passing
basics/program-derived-addresses/pinocchio 4 passing
basics/processing-instructions/pinocchio 2 passing
basics/favorites/pinocchio 2 passing
basics/realloc/pinocchio 3 passing
basics/account-data/pinocchio 2 passing
basics/close-account/pinocchio 2 passing
basics/counter/native 2 passing
basics/transfer-sol/asm typecheck only — see below

basics/transfer-sol/asm builds with sbpf, which is not available in this environment (cargo install sbpf fails to compile), so its suite could not be executed. The change there is a one-line swap of new DataView(...).setBigUint64(0, lamports, true) for getU64Encoder().encode(lamports) — byte-identical little-endian u64 — and it typechecks clean.

prettier --check passes on all touched files.

Part of DEV-838

@amilz
amilz requested a review from dev-jodee as a code owner August 6, 2026 17:36
@linear

linear Bot commented Aug 6, 2026

Copy link
Copy Markdown

DEV-838

@amilz amilz self-assigned this Aug 6, 2026
@greptile-apps

greptile-apps Bot commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR replaces manual TypeScript byte packing and account decoding in the basics examples with @solana/kit codecs, and changes the native counter decoder from BN.js to native bigint.

  • Adds fixed-layout struct encoders and decoders matching the examples’ on-chain account and instruction layouts.
  • Removes BN.js and its type package from the native counter example.
  • Regenerates the counter package lockfile.

Confidence Score: 4/5

The code refactor is behaviorally sound, but the pull request must not merge until its commit is signed and verifiably attributed.

The examined codec layouts match their on-chain instruction and account formats, while the head commit lacks the verified signature required by the repository owner.

Files Needing Attention: Pull request commit metadata; no source file requires a behavioral correction.

Important Files Changed

Filename Overview
basics/account-data/pinocchio/tests/index.test.ts Replaces manual fixed-width address serialization and account decoding with a matching struct codec.
basics/favorites/pinocchio/tests/test.ts Uses fixed-size UTF-8 and array codecs for the existing favorites instruction and account layouts.
basics/realloc/pinocchio/tests/realloc.test.ts Replaces three manual layouts with codecs whose field order, sizes, and endianness match the program ABI.
basics/program-derived-addresses/pinocchio/tests/test.ts Encodes the six-byte create instruction and decodes the five-byte persisted account layout consistently.
basics/counter/native/ts/accounts/counter.ts Replaces BN.js decoding with a little-endian u64 bigint decoder matching the on-chain counter representation.
basics/counter/native/package.json Removes BN.js dependencies rendered unnecessary by native bigint decoding; the pull request commit does not satisfy the verified-signature requirement.
basics/transfer-sol/asm/tests/instruction.ts Replaces DataView little-endian u64 packing with the equivalent kit u64 encoder.

Comments Outside Diff (1)

  1. basics/counter/native/package.json, line 1 (link)

    P1 Commit signature is unverified

    The pull request’s only commit, 7fd415bfd4ce6cdb88281825bc9012bab59a3838, has no verified signature, so it does not satisfy the repository’s commit-provenance requirement and must not merge in its current form.

    Context Used: Request changes if the commits are not signed (ver... (source)

Reviews (1): Last reviewed commit: "refactor(basics): replace hand-rolled by..." | Re-trigger Greptile

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.

2 participants