Skip to content

Commit 4b2b28f

Browse files
fix: pad rawSize for payment calculations (#251)
* 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. * chore: move padSizeToPDPLeave to utils.ts
1 parent 5c1b0cb commit 4b2b28f

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

src/core/payments/constants.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,9 @@ export const BUFFER_DENOMINATOR = 10n
5151
*/
5252
export const STORAGE_SCALE_MAX = 10_000_000
5353
export const STORAGE_SCALE_MAX_BI = BigInt(STORAGE_SCALE_MAX)
54+
55+
/** PDP Leaf Size - the payment rate is based on `rawSize` bytes rounded up to the next multiple of 32.
56+
*
57+
* @see - https://github.com/FilOzone/synapse-sdk/issues/339#issue-3539254596
58+
*/
59+
export const PDP_LEAF_SIZE = 32

src/core/payments/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
} from './constants.js'
3333
import { applyFloorPricing } from './floor-pricing.js'
3434
import type { PaymentStatus, ServiceApprovalStatus, StorageAllowances, StorageRunwaySummary } from './types.js'
35+
import { padSizeToPDPLeaves } from './utils.js'
3536

3637
// Re-export all constants
3738
export * from './constants.js'
@@ -833,7 +834,8 @@ export function calculateDepositCapacity(
833834
* @returns Required allowances for the piece
834835
*/
835836
export function calculateRequiredAllowances(pieceSizeBytes: number, pricePerTiBPerEpoch: bigint): StorageAllowances {
836-
const storageTiB = pieceSizeBytes / Number(SIZE_CONSTANTS.TiB)
837+
const paddedSizeBytes = padSizeToPDPLeaves(pieceSizeBytes)
838+
const storageTiB = paddedSizeBytes / Number(SIZE_CONSTANTS.TiB)
837839
return calculateStorageAllowances(storageTiB, pricePerTiBPerEpoch)
838840
}
839841

src/core/payments/utils.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { PDP_LEAF_SIZE } from './constants.js'
2+
3+
/**
4+
* Pad raw size to the next multiple of 32 bytes
5+
*
6+
* @param rawSizeBytes - The actual size in bytes
7+
* @returns Padded size (next multiple of 32)
8+
*/
9+
export function padSizeToPDPLeaves(rawSizeBytes: number): number {
10+
return Math.ceil(rawSizeBytes / PDP_LEAF_SIZE) * PDP_LEAF_SIZE
11+
}

0 commit comments

Comments
 (0)