From 47f9e0dd2bcabae5d4748affd2a7793bd0b948e6 Mon Sep 17 00:00:00 2001 From: kaf-lamed-beyt Date: Tue, 18 Nov 2025 23:46:20 +0100 Subject: [PATCH 1/2] fix: pad rawSize for payment calculations calculateRequiredAllowances(), supposedly, is the entry point for all the calculate* functions in /core/payments. calling padSizeToPDPLeaves once in it, shares that converted with the rest. --- src/core/payments/constants.ts | 16 ++++++++++++++++ src/core/payments/index.ts | 4 +++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/core/payments/constants.ts b/src/core/payments/constants.ts index 6f044cc..023a8f8 100644 --- a/src/core/payments/constants.ts +++ b/src/core/payments/constants.ts @@ -51,3 +51,19 @@ export const BUFFER_DENOMINATOR = 10n */ export const STORAGE_SCALE_MAX = 10_000_000 export const STORAGE_SCALE_MAX_BI = BigInt(STORAGE_SCALE_MAX) + +/** PDP Leaf Size - the payment rate is based on `rawSize` bytes rounded up to the next multiple of 32. + * + * @see - https://github.com/FilOzone/synapse-sdk/issues/339#issue-3539254596 + */ +export const PDP_LEAF_SIZE = 32 + +/** + * Pad raw size to the next multiple of 32 bytes + * + * @param rawSizeBytes - The actual size in bytes + * @returns Padded size (next multiple of 32) + */ +export function padSizeToPDPLeaves(rawSizeBytes: number): number { + return Math.ceil(rawSizeBytes / PDP_LEAF_SIZE) * PDP_LEAF_SIZE +} diff --git a/src/core/payments/index.ts b/src/core/payments/index.ts index 2100abc..ecc76d6 100644 --- a/src/core/payments/index.ts +++ b/src/core/payments/index.ts @@ -26,6 +26,7 @@ import { MAX_LOCKUP_ALLOWANCE, MAX_RATE_ALLOWANCE, MIN_FIL_FOR_GAS, + padSizeToPDPLeaves, STORAGE_SCALE_MAX, STORAGE_SCALE_MAX_BI, USDFC_DECIMALS, @@ -826,7 +827,8 @@ export function calculateDepositCapacity( * @returns Required allowances for the piece */ export function calculateRequiredAllowances(pieceSizeBytes: number, pricePerTiBPerEpoch: bigint): StorageAllowances { - const storageTiB = pieceSizeBytes / Number(SIZE_CONSTANTS.TiB) + const paddedSizeBytes = padSizeToPDPLeaves(pieceSizeBytes) + const storageTiB = paddedSizeBytes / Number(SIZE_CONSTANTS.TiB) return calculateStorageAllowances(storageTiB, pricePerTiBPerEpoch) } From 8f45ca43b5045ec8682a7b1cc355bd47e503b4c8 Mon Sep 17 00:00:00 2001 From: kaf-lamed-beyt Date: Thu, 20 Nov 2025 22:17:26 +0100 Subject: [PATCH 2/2] chore: move padSizeToPDPLeave to utils.ts --- src/core/payments/constants.ts | 10 ---------- src/core/payments/index.ts | 2 +- src/core/payments/utils.ts | 11 +++++++++++ 3 files changed, 12 insertions(+), 11 deletions(-) create mode 100644 src/core/payments/utils.ts diff --git a/src/core/payments/constants.ts b/src/core/payments/constants.ts index 023a8f8..924cb5b 100644 --- a/src/core/payments/constants.ts +++ b/src/core/payments/constants.ts @@ -57,13 +57,3 @@ export const STORAGE_SCALE_MAX_BI = BigInt(STORAGE_SCALE_MAX) * @see - https://github.com/FilOzone/synapse-sdk/issues/339#issue-3539254596 */ export const PDP_LEAF_SIZE = 32 - -/** - * Pad raw size to the next multiple of 32 bytes - * - * @param rawSizeBytes - The actual size in bytes - * @returns Padded size (next multiple of 32) - */ -export function padSizeToPDPLeaves(rawSizeBytes: number): number { - return Math.ceil(rawSizeBytes / PDP_LEAF_SIZE) * PDP_LEAF_SIZE -} diff --git a/src/core/payments/index.ts b/src/core/payments/index.ts index ecc76d6..1e0354a 100644 --- a/src/core/payments/index.ts +++ b/src/core/payments/index.ts @@ -26,13 +26,13 @@ import { MAX_LOCKUP_ALLOWANCE, MAX_RATE_ALLOWANCE, MIN_FIL_FOR_GAS, - padSizeToPDPLeaves, STORAGE_SCALE_MAX, STORAGE_SCALE_MAX_BI, USDFC_DECIMALS, } from './constants.js' import { applyFloorPricing } from './floor-pricing.js' import type { PaymentStatus, ServiceApprovalStatus, StorageAllowances, StorageRunwaySummary } from './types.js' +import { padSizeToPDPLeaves } from './utils.js' // Re-export all constants export * from './constants.js' diff --git a/src/core/payments/utils.ts b/src/core/payments/utils.ts new file mode 100644 index 0000000..500a930 --- /dev/null +++ b/src/core/payments/utils.ts @@ -0,0 +1,11 @@ +import { PDP_LEAF_SIZE } from './constants.js' + +/** + * Pad raw size to the next multiple of 32 bytes + * + * @param rawSizeBytes - The actual size in bytes + * @returns Padded size (next multiple of 32) + */ +export function padSizeToPDPLeaves(rawSizeBytes: number): number { + return Math.ceil(rawSizeBytes / PDP_LEAF_SIZE) * PDP_LEAF_SIZE +}