Skip to content

Correct storage cost and fee calculations - #946

Open
hugomrdias wants to merge 1 commit into
masterfrom
hugomrdias/audit-costs-and-fee-calculations
Open

Correct storage cost and fee calculations#946
hugomrdias wants to merge 1 commit into
masterfrom
hugomrdias/audit-costs-and-fee-calculations

Conversation

@hugomrdias

Copy link
Copy Markdown
Member

Summary

  • Audit and correct warm-storage cost, deposit, lockup, reserve, and upload-fee calculations
  • Add PDP dataset size and leaf-count helpers plus related exports and mocks
  • Update multi-context storage cost handling and refresh storage documentation
  • Expand unit test coverage across core calculations and storage services

Testing

  • Added and updated unit tests for cost calculations, dataset sizing, PDP helpers, and storage workflows

@hugomrdias
hugomrdias requested review from a team, BigLep, jennijuju, rjan90 and rvagg as code owners August 27, 2026 14:21
@github-project-automation github-project-automation Bot moved this to 📌 Triage in FOC Aug 27, 2026
@pkg-pr-new

pkg-pr-new Bot commented Aug 27, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/@filoz/synapse-core@946
npm i https://pkg.pr.new/@filoz/synapse-react@946
npm i https://pkg.pr.new/@filoz/synapse-sdk@946

commit: 0c729da

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Aug 27, 2026

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
synapse-dev 0c729da Commit Preview URL

Branch Preview URL
Aug 27 2026, 02:37 PM

@hugomrdias hugomrdias added this to the 202608 Contract Release milestone Aug 27, 2026
@BigLep BigLep moved this from 📌 Triage to 🔎 Awaiting review in FOC Aug 27, 2026
@BigLep
BigLep requested a lite review from Copilot August 27, 2026 18:56

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR refactors warm-storage pricing and funding calculations to match on-chain behavior more closely by switching from raw byte totals to PDP leaf-count based sizing and by modeling one-time fees as lifecycle-reserve-funded rather than directly deposit-funded.

Changes:

  • Replace dataSize with pieceSizes across SDK/core APIs to correctly model per-piece PDP rounding and per-operation fees.
  • Add PDP sizing helpers (raw size ↔ leaf count, leaf count → FWSS-priced bytes) plus a shared calculateUploadCosts pure utility used by both single- and multi-context flows.
  • Update mocks, unit tests, and docs to reflect the new proving fee, lifecycle reserve behavior, and existing-data-set state requirements.

Reviewed changes

Copilot reviewed 42 out of 42 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
utils/example-storage-e2e.js Update prepare-cost input to pieceSizes for multi-file batching flow
packages/synapse-sdk/src/types.ts Update public SDK types: PrepareOptions.pieceSizes, enrich DataSetInfo with reserve state
packages/synapse-sdk/src/test/warm-storage-service.test.ts Align non-existent data set behavior expectations (null)
packages/synapse-sdk/src/test/calculate-multi-context-costs.test.ts Expand multi-context cost tests for leaf-count sizing + reserve replenishment
packages/synapse-sdk/src/storage/manager.ts Rework multi-context cost aggregation to use leaf counts + lifecycle reserve state and delegate arithmetic to core utility
packages/synapse-core/test/pdp-size.test.ts Add tests for new PDP size utilities
packages/synapse-core/test/get-upload-costs.test.ts Update upload-cost tests for pieceSizes, leaf-based pricing, and reserve-aware deposits
packages/synapse-core/test/get-pieces.test.ts Update dataset mocks to include new reserve-related fields
packages/synapse-core/test/get-data-sets-by-id.test.ts Add test coverage for new getDataSetsById helper
packages/synapse-core/test/get-data-set.test.ts Update “missing dataset” expectation to null
packages/synapse-core/test/get-data-set-leaf-counts.test.ts Replace dataset-size helper tests with leaf-count map helper tests
packages/synapse-core/test/get-account-total-storage-size.test.ts Update total storage size to use FWSS-priced bytes derived from leaf counts
packages/synapse-core/test/calculate-upload-fees.test.ts Update upload-fee tests to depend on pieceSizes validation and pricing
packages/synapse-core/test/calculate-upload-costs.test.ts Add direct tests for the new calculateUploadCosts pure utility
packages/synapse-core/test/calculate-lifecycle-reserve-funding.test.ts Add tests for lifecycle reserve creation/replenishment math
packages/synapse-core/test/calculate-effective-rate.test.ts Update proving fee constant in rate tests
packages/synapse-core/test/calculate-deposit-needed.test.ts Update deposit-needed tests for reserve-funded fees and leaf-count sizing
packages/synapse-core/test/calculate-additional-lockup-required.test.ts Update lockup tests to use leaf-count conversions and pieceSizes
packages/synapse-core/src/warm-storage/types.ts Extend DataSetInfo to include reserve balance and pending one-time payments
packages/synapse-core/src/warm-storage/index.ts Export new warm-storage helpers (calculateLifecycleReserveFunding, getDataSetsById)
packages/synapse-core/src/warm-storage/get-upload-costs.ts Delegate cost arithmetic to calculateUploadCosts; require existing-data-set state
packages/synapse-core/src/warm-storage/get-data-sets-by-id.ts Add multicall helper to fetch datasets indexed by ID (missing -> null)
packages/synapse-core/src/warm-storage/get-data-set.ts Refactor parsing and standardize missing dataset sentinel to null via parseGetDataSet
packages/synapse-core/src/warm-storage/get-account-total-storage-size.ts Sum FWSS-priced approximate sizes using leaf-count conversion
packages/synapse-core/src/warm-storage/calculate-upload-fees.ts Change fee calculation inputs to pieceSizes; simplify fee model to per-call base + per-piece
packages/synapse-core/src/warm-storage/calculate-lifecycle-reserve-funding.ts New helper to model reserve funding/replenishment and final reserve balance
packages/synapse-core/src/warm-storage/calculate-effective-rate.ts Clarify that rate inputs are FWSS-priced aggregate sizes derived from leaf counts
packages/synapse-core/src/warm-storage/calculate-deposit-needed.ts Adapt legacy deposit helper to the shared calculateUploadCosts utility
packages/synapse-core/src/warm-storage/calculate-additional-lockup-required.ts Switch delta computation to leaf-count aggregation + FWSS pricing conversion
packages/synapse-core/src/utils/pdp-size.ts New PDP sizing utilities + pieceSizes validation
packages/synapse-core/src/utils/index.ts Export new upload-cost utility and PDP sizing utilities
packages/synapse-core/src/utils/constants.ts Update leaf-size comment to direct callers to leafCountToRawSize() conversion
packages/synapse-core/src/utils/calculate-upload-costs.ts New shared pure upload cost + funding calculator (single + multi-context)
packages/synapse-core/src/pdp-verifier/index.ts Export new getDataSetLeafCounts helper and stop exporting deprecated size helper
packages/synapse-core/src/pdp-verifier/get-dataset-size.ts Remove deprecated dataset-size helper (bytes) in favor of leaf-count based APIs
packages/synapse-core/src/pdp-verifier/get-data-set-leaf-counts.ts New multicall leaf-count map helper with dedupe + “not live -> 0n” handling
packages/synapse-core/src/mocks/jsonrpc/index.ts Update pricing/fee/lockup mock values (proving fee + lifecycle reserve, etc.)
docs/src/content/docs/introduction/about.mdx Refresh proving fee and lifecycle reserve narrative
docs/src/content/docs/getting-started/index.mdx Update code samples to use pieceSizes
docs/src/content/docs/developer-guides/storage/upload-pipeline.mdx Update prepare example to pieceSizes
docs/src/content/docs/developer-guides/storage/storage-costs.mdx Update pricing/fees tables and add leaf-count sizing explanation
docs/src/content/docs/cookbooks/payments-and-storage.mdx Update cookbook pricing and lifecycle reserve mechanics

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +41 to +50
export function validatePieceSizes(pieceSizes: readonly bigint[]): void {
if (pieceSizes.length === 0) {
throw new ValidationError('pieceSizes must contain at least one piece')
}
for (const size of pieceSizes) {
if (size <= 0n) {
throw new ValidationError('pieceSizes must contain only positive byte sizes')
}
}
}
Comment on lines +26 to +28
* The length of `pieceSizes` determines the per-piece fee and the calculation
* includes one add-pieces base fee. Execution batch limits are intentionally
* not enforced here because they are independent of fee calculation.
Comment on lines 78 to +81
console.log('\n--- Preparing Account ---')
const { costs, transaction } = await synapse.storage.prepare({ dataSize: BigInt(totalSize) })
const { costs, transaction } = await synapse.storage.prepare({
pieceSizes: files.map((file) => BigInt(file.length)),
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: 🔎 Awaiting review

3 participants