Skip to content

V2 remove redundancy & optimize gas usage#37

Merged
nolnol3 merged 8 commits intodevelopfrom
v2-remove-redundancy
Jan 22, 2026
Merged

V2 remove redundancy & optimize gas usage#37
nolnol3 merged 8 commits intodevelopfrom
v2-remove-redundancy

Conversation

@nolnol3
Copy link
Copy Markdown
Member

@nolnol3 nolnol3 commented Jan 22, 2026

V2 remove redundancy & optimize gas usage

## Summary
Reduce external calls to mitigate ~45,000 gas overhead per CALL/STATICCALL
introduced by Arbitrum Nitro v3.9.0 Multi-Constraint Pricing.

## Optimizations Applied
- #1: Remove duplicate Verifier.getTokenFeeInfo() calls in Billing.sol
- #2: Consolidate Verifier.paymentRecipient() fetch in _processVerifiedDelivery()
- #3: Combine unlockForVerification + payFromCoordinator into single
      unlockAndPayForVerification() function in Router.sol
- #5: Add Wallet.getSpenderInfo() to combine 3 external calls into 1
- Bytecode optimization: Remove duplicate memory function variants
  to fit Router within EIP-170 24KB limit with optimizer_runs=1,000,000
Gas optimization for Arbitrum Nitro v3.9.0 Multi-Constraint Pricing.

Changes:
- Add verifierFee field to Commitment struct to cache verifier fee
- Billing._startBilling(): Store verifierFee when creating commitment
- Billing._processVerifiedDelivery(): Use cached verifierFee instead of
  calling verifier.getTokenFeeInfo() again (saves ~45k gas)
- Coordinator.getCommitment(): Fetch verifierFee for commitment reconstruction
- Router.sendRequest(): Delegate to Coordinator.getCommitment() for
  idempotent requests (reduces Router bytecode size)
- Update CommitmentUtils to accept verifierFee parameter
- Update JavaScript commitment.js to include verifierFee field

Benchmark results (reportComputeResult):
- 64B payload: 677,920 -> 633,023 gas (-6.6%)
- 256B payload: 675,412 -> 623,791 gas (-7.6%)
- 512B payload: 688,157 -> 639,367 gas (-7.1%)
- Average savings: ~50,000 gas per call
- #5: Remove redundant redundancyCount[requestId] = 0 assignment
  (mapping defaults to 0, saves ~2.1k gas SSTORE)

- #6: Remove redundant isValidWallet() check in _markRequestInFlight
  (wallet already validated in createComputeSubscription)

- #7: Replace require strings with custom errors in Router.sol
  (CoordinatorNotFound, WalletFactoryAlreadySet, InvalidWalletFactoryAddress)

- #8: Pass client address as parameter to avoid SLOAD
  (saves ~2.1k gas by not reading subscription.client twice)
BREAKING CHANGE: This commit removes the `redundancy` parameter from the
subscription system, changing from a multi-node response model to a
single-node response per request.

Key changes:
- Remove `redundancy` field from ComputeSubscription and Commitment types
- Simplify RequestLock struct in Wallet.sol (remove redundancy/paidCount)
- Remove nodeResponded mapping from Coordinator (use commitment existence)
- Update ComputeDelivered event from 6 to 5 parameters (remove interval)
- Simplify lockForRequest from 5 to 4 parameters
- Remove numRedundantDeliveries from fulfill() calls
- Update EIP-712 type hash (remove redundancy from signed data)

Gas savings: ~36,350 gas per request/response cycle

Test results: 155 passed, 8 skipped
- 3 skipped in DelegateeComputeTest (delegated flow bug)
- 4 skipped in ComputeVerifierTest (pre-existing)
- 1 skipped in SubscriptionBatchReaderTest (commitment sync bug)
Bug 1 - Delegated flow duplicate requests:
- Add completion check in createSubscriptionDelegatee
- Revert with SubscriptionCompleted if subscription is cancelled or fulfilled

Bug 2 - Commitment sync on cancellation:
- Call Coordinator.cancelRequest() in _cancelSubscriptionHelper
- Ensures Router and Coordinator commitments stay in sync

Test results: 158 passed, 5 skipped (Verifier-related)
- Update threshold from 1KB to 256 bytes to match production config
- Add PayloadData detection in RAW_DATA handling for agent script
- Fix output URI type: external input -> external output (S3 URL)
- Add support for data:;base64, short format
- Add TEST_UPLOAD_THRESHOLD and TEST_STORAGE_TYPE environment variables

Result: ~13.5% gas reduction for reportComputeResult with external inputs
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 86313afe32

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

- Remove uint16 redundancy field from Commitment tuple encoding
- Update createSubscription calls to match new contract signature
- Add verifierFee field to buildCommitment function
- Fix hash mismatch between off-chain and on-chain commitment encoding

This fixes InvalidCommitment revert when submitting results to Coordinator.
@nolnol3 nolnol3 merged commit 3e44186 into develop Jan 22, 2026
2 checks passed
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.

1 participant