Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into feat/v2-piece-cid
Browse files Browse the repository at this point in the history
  • Loading branch information
Gozala committed Jul 19, 2023
2 parents c7d126d + a1006c0 commit 64989b3
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/aggregate.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { indexAreaStart } from './inclusion.js'

const NodeSize = BigInt(Node.Size)
const EntrySize = Number(Index.EntrySize)
export const MAX_CAPACITY = 2n ** BigInt(Tree.MAX_HEIGHT) * NodeSize
export const MAX_CAPACITY = Piece.PaddedSize.fromHeight(Tree.MAX_HEIGHT)

/**
* Default aggregate size (32GiB).
Expand Down
10 changes: 9 additions & 1 deletion src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,14 +177,22 @@ export interface PieceInfo {
size: PaddedPieceSize
}

export interface PieceInfoView extends PieceInfo {
export interface PieceView {
/**
* Commitment to the data segment (Merkle node which is the root of the
* subtree containing all the nodes making up the data segment)
*/
link: PieceLink

/**
* Height of the perfect binary merkle tree representing
* this piece.
*/
height: number
}

export interface PieceInfoView extends PieceInfo, PieceView {}

/**
* Represents a piece tree and underlying merkle tree.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/piece.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class PieceInfo {
this.height = height
}
get size() {
return 2n ** BigInt(this.height) * BigInt(NODE_SIZE)
return PaddedSize.fromHeight(this.height)
}
toJSON() {
return toJSON(this)
Expand Down
9 changes: 8 additions & 1 deletion src/piece/padded-size.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,11 @@ export const toUnpaddedSize = (size) => size - size / BYTES_PER_QUAD
*
* @param {API.PaddedPieceSize} size
*/
export const toHeight = (size) => log2Ceil(size / BYTES_PER_NODE)
export const toHeight = (size) => log2Ceil(size / NODE_SIZE)

/**
* Calculates the padded size of the piece tree from height.
*
* @param {number} height
*/
export const fromHeight = (height) => 2n ** BigInt(height) * NODE_SIZE
16 changes: 16 additions & 0 deletions test/piece-size.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,22 @@ export const testPieceSize = {
])
),

// to height - from height
...Object.fromEntries(
[128, 1024, 34359738368].map((size) => [
`PaddedSize.fromHeight(PaddedSize.toHeight(PaddedSize.from(${size})))) === ${size}`,
(assert) => {
assert.equal(
PaddedSize.fromHeight(
PaddedSize.toHeight(PaddedSize.from(size))
),
BigInt(size)
)
},
])
),


// throw
...Object.fromEntries(
[9, 128, 99453687, 1016 + 0x1000000].map((size) => [
Expand Down

0 comments on commit 64989b3

Please sign in to comment.