Skip to content

Commit

Permalink
feat: padded size from height calc function (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gozala authored Jul 19, 2023
2 parents 61ee74d + dba6cd5 commit 47971cd
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 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
2 changes: 1 addition & 1 deletion src/piece.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class PieceInfo {
this.height = height
}
get size() {
return 2n ** BigInt(this.height) * BigInt(NodeSize)
return PaddedSize.fromHeight(this.height)
}
toJSON() {
return toJSON(this)
Expand Down
7 changes: 7 additions & 0 deletions src/piece/padded-size.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,10 @@ export const toUnpaddedSize = (size) => size - size / 128n
* @param {API.PaddedPieceSize} size
*/
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 47971cd

Please sign in to comment.