Skip to content

fix: consolidate CRANK_KEYPAIR into a singleton to reduce secretKey heap allocations - #355

Open
Ayomisco wants to merge 1 commit into
dcccrypto:mainfrom
Ayomisco:fix/keeper-8-keypair-singleton
Open

fix: consolidate CRANK_KEYPAIR into a singleton to reduce secretKey heap allocations#355
Ayomisco wants to merge 1 commit into
dcccrypto:mainfrom
Ayomisco:fix/keeper-8-keypair-singleton

Conversation

@Ayomisco

@Ayomisco Ayomisco commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

Closes #354

What

Add src/lib/keypair-singleton.ts and replace all four loadKeypair(process.env.CRANK_KEYPAIR) call sites with getKeeperKeypair().

Why

loadKeypair was called at module scope in index.ts, in the field initializers of CrankService and LiquidationService, and once more in env-guards.ts. That created four separate Uint8Array allocations holding the ed25519 secret key, each persisting in the heap for the process lifetime.

Deployment platforms log stdout and stderr permanently. Any logger that serializes a service instance with JSON.stringify encodes secretKey: Uint8Array as a plain JSON object and sends it to the log stream.

Changes

  • New file: src/lib/keypair-singleton.ts
    • Loads on first call, overwrites then deletes process.env.CRANK_KEYPAIR, returns the same instance on every subsequent call.
  • src/index.ts: replaced loadKeypair(...) with getKeeperKeypair(), removed loadKeypair from import.
  • src/services/crank.ts: replaced field initializer with a private getter that calls getKeeperKeypair(), removed loadKeypair from import.
  • src/services/liquidation.ts: same getter pattern, removed loadKeypair from import.

…eap allocations

loadKeypair(process.env.CRANK_KEYPAIR) was called four times at startup:
once in env-guards.ts (validation), once in index.ts (module scope), once in
CrankService (field initializer), and once in LiquidationService (field initializer).
This created four separate 64-byte Uint8Array allocations containing the ed25519
secret key, all persisting for the process lifetime.

Introduce src/lib/keypair-singleton.ts with a getKeeperKeypair() function that
parses and caches the keypair on first call, then overwrites and deletes
process.env.CRANK_KEYPAIR so the raw secret string is not readable by later code,
heap snapshots, or accidental logger.info(process.env) calls.

The env-guards.ts validation call is unchanged — it runs before getKeeperKeypair()
is first invoked, so it still reads the env var successfully.
@coderabbitai

coderabbitai Bot commented Jun 26, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@Ayomisco, we couldn't start this review because you've reached your PR review rate limit.

More reviews will be available in 57 minutes and 5 seconds. Learn how PR review limits work.

Your organization has used up its prepaid credits, and credit purchases are no longer available. Enable the review add-on in the billing tab to keep reviews running — you're only billed for reviews past your plan's rate limits ($0.25/file).

⌛ How to resolve this issue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based credits.

🚦 How do rate limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 06fba5b8-154b-4eaf-8518-c693609f86bb

📥 Commits

Reviewing files that changed from the base of the PR and between 8ee810d and 0ec5e6c.

📒 Files selected for processing (4)
  • src/index.ts
  • src/lib/keypair-singleton.ts
  • src/services/crank.ts
  • src/services/liquidation.ts
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

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.

[CRITICAL] CRANK_KEYPAIR parsed into 4 separate heap allocations with no secret-zeroing

1 participant