Skip to content

Commit 494c0a5

Browse files
fix: allow fetching stamps with custom effective size calculations (#1105)
* fix: allow fetching stamps with custom effective size calculations * docs: jsdocs * test: update test coverage * test: update test coverage * refactor: use a method-based approach * chore: remove dead jsdocs * test: update test coverage * test: update test coverage --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 15a59e4 commit 494c0a5

4 files changed

Lines changed: 53 additions & 7 deletions

File tree

src/bee.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2350,17 +2350,26 @@ export class Bee {
23502350
*
23512351
* @param postageBatchId Batch ID
23522352
* @param requestOptions Options for making requests, such as timeouts, custom HTTP agents, headers, etc.
2353+
* @param encryption Assume that uploads with this postage batch are encrypted, which skews the capacity.
2354+
* @param erasureCodeLevel Assume that uploads with this postage batch are erasure coded, which skews the capacity.
23532355
*
23542356
* @see [Bee docs - Keep your data alive / Postage stamps](https://docs.ethswarm.org/docs/develop/access-the-swarm/introduction/#keep-your-data-alive)
23552357
* @see [Bee Debug API reference - `GET /stamps/${id}`](https://docs.ethswarm.org/api/#tag/Postage-Stamps/paths/~1stamps~1%7Bbatch_id%7D/get)
23562358
*/
23572359
async getPostageBatch(
23582360
postageBatchId: BatchId | Uint8Array | string,
23592361
requestOptions?: BeeRequestOptions,
2362+
encryption?: boolean,
2363+
erasureCodeLevel?: RedundancyLevel,
23602364
): Promise<PostageBatch> {
23612365
postageBatchId = new BatchId(postageBatchId)
23622366

2363-
return stamps.getPostageBatch(this.getRequestOptionsForCall(requestOptions), postageBatchId)
2367+
return stamps.getPostageBatch(
2368+
this.getRequestOptionsForCall(requestOptions),
2369+
postageBatchId,
2370+
encryption,
2371+
erasureCodeLevel,
2372+
)
23642373
}
23652374

23662375
/**

src/modules/debug/stamps.ts

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type {
66
PostageBatch,
77
PostageBatchBuckets,
88
PostageBatchOptions,
9+
RedundancyLevel,
910
} from '../../types'
1011
import { Duration } from '../../utils/duration'
1112
import { http } from '../../utils/http'
@@ -58,6 +59,8 @@ export async function getAllPostageBatches(requestOptions: BeeRequestOptions): P
5859
const batchTTL = normalizeBatchTTL(Types.asNumber(x.batchTTL, { name: 'batchTTL' }))
5960
const duration = Duration.fromSeconds(batchTTL)
6061

62+
const effectiveBytes = getStampEffectiveBytes(depth)
63+
6164
return {
6265
batchID: new BatchId(Types.asString(x.batchID, { name: 'batchID' })),
6366
utilization,
@@ -70,17 +73,29 @@ export async function getAllPostageBatches(requestOptions: BeeRequestOptions): P
7073
immutableFlag: Types.asBoolean(x.immutableFlag, { name: 'immutableFlag' }),
7174
usage,
7275
usageText: `${Math.round(usage * 100)}%`,
73-
size: Size.fromBytes(getStampEffectiveBytes(depth)),
74-
remainingSize: Size.fromBytes(Math.ceil(getStampEffectiveBytes(depth) * (1 - usage))),
76+
size: Size.fromBytes(effectiveBytes),
77+
remainingSize: Size.fromBytes(Math.ceil(effectiveBytes * (1 - usage))),
7578
theoreticalSize: Size.fromBytes(getStampTheoreticalBytes(depth)),
7679
duration,
80+
calculateSize(encryption, redundancyLevel) {
81+
const effectiveBytes = getStampEffectiveBytes(this.depth, encryption, redundancyLevel)
82+
83+
return Size.fromBytes(effectiveBytes)
84+
},
85+
calculateRemainingSize(encryption, redundancyLevel) {
86+
const effectiveBytes = getStampEffectiveBytes(this.depth, encryption, redundancyLevel)
87+
88+
return Size.fromBytes(Math.ceil(effectiveBytes * (1 - this.usage)))
89+
},
7790
}
7891
})
7992
}
8093

8194
export async function getPostageBatch(
8295
requestOptions: BeeRequestOptions,
8396
postageBatchId: BatchId,
97+
encryption?: boolean,
98+
erasureCodeLevel?: RedundancyLevel,
8499
): Promise<PostageBatch> {
85100
const response = await http<unknown>(requestOptions, {
86101
method: 'get',
@@ -97,6 +112,8 @@ export async function getPostageBatch(
97112
const batchTTL = normalizeBatchTTL(Types.asNumber(body.batchTTL, { name: 'batchTTL' }))
98113
const duration = Duration.fromSeconds(batchTTL)
99114

115+
const effectiveBytes = getStampEffectiveBytes(depth, encryption, erasureCodeLevel)
116+
100117
return {
101118
batchID: new BatchId(Types.asString(body.batchID, { name: 'batchID' })),
102119
utilization,
@@ -109,10 +126,20 @@ export async function getPostageBatch(
109126
immutableFlag: Types.asBoolean(body.immutableFlag, { name: 'immutableFlag' }),
110127
usage,
111128
usageText: `${Math.round(usage * 100)}%`,
112-
size: Size.fromBytes(getStampEffectiveBytes(depth)),
113-
remainingSize: Size.fromBytes(Math.ceil(getStampEffectiveBytes(depth) * (1 - usage))),
129+
size: Size.fromBytes(effectiveBytes),
130+
remainingSize: Size.fromBytes(Math.ceil(effectiveBytes * (1 - usage))),
114131
theoreticalSize: Size.fromBytes(getStampTheoreticalBytes(depth)),
115132
duration,
133+
calculateSize(encryption, redundancyLevel) {
134+
const effectiveBytes = getStampEffectiveBytes(depth, encryption, redundancyLevel)
135+
136+
return Size.fromBytes(effectiveBytes)
137+
},
138+
calculateRemainingSize(encryption, redundancyLevel) {
139+
const effectiveBytes = getStampEffectiveBytes(depth, encryption, redundancyLevel)
140+
141+
return Size.fromBytes(Math.ceil(effectiveBytes * (1 - usage)))
142+
},
116143
}
117144
}
118145

src/types/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,16 @@ export interface PostageBatch {
544544
* Theoretical size in bytes
545545
*/
546546
theoreticalSize: Size
547+
/**
548+
* Calculates the effective size of data that can be uploaded with this postage batch
549+
* based on whether encryption is used and the desired redundancy level.
550+
*/
551+
calculateSize(encryption: boolean, redundancyLevel: RedundancyLevel): Size
552+
/**
553+
* Calculates the remaining size of data that can be uploaded with this postage batch
554+
* based on whether encryption is used and the desired redundancy level.
555+
*/
556+
calculateRemainingSize(encryption: boolean, redundancyLevel: RedundancyLevel): Size
547557
}
548558

549559
export interface BatchBucket {

test/coverage/coverage-summary.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{"total": {"lines":{"total":2468,"covered":2026,"skipped":0,"pct":82.09},"statements":{"total":2511,"covered":2065,"skipped":0,"pct":82.23},"functions":{"total":597,"covered":478,"skipped":0,"pct":80.06},"branches":{"total":563,"covered":356,"skipped":0,"pct":63.23},"branchesTrue":{"total":0,"covered":0,"skipped":0,"pct":100}}
1+
{"total": {"lines":{"total":2478,"covered":2028,"skipped":0,"pct":81.84},"statements":{"total":2521,"covered":2067,"skipped":0,"pct":81.99},"functions":{"total":601,"covered":478,"skipped":0,"pct":79.53},"branches":{"total":563,"covered":356,"skipped":0,"pct":63.23},"branchesTrue":{"total":0,"covered":0,"skipped":0,"pct":100}}
22
,"/home/runner/work/bee-js/bee-js/src/bee-dev.ts": {"lines":{"total":14,"covered":5,"skipped":0,"pct":35.71},"functions":{"total":2,"covered":0,"skipped":0,"pct":0},"statements":{"total":14,"covered":5,"skipped":0,"pct":35.71},"branches":{"total":0,"covered":0,"skipped":0,"pct":100}}
33
,"/home/runner/work/bee-js/bee-js/src/bee.ts": {"lines":{"total":445,"covered":370,"skipped":0,"pct":83.14},"functions":{"total":127,"covered":104,"skipped":0,"pct":81.88},"statements":{"total":448,"covered":373,"skipped":0,"pct":83.25},"branches":{"total":115,"covered":67,"skipped":0,"pct":58.26}}
44
,"/home/runner/work/bee-js/bee-js/src/index.ts": {"lines":{"total":16,"covered":16,"skipped":0,"pct":100},"functions":{"total":10,"covered":7,"skipped":0,"pct":70},"statements":{"total":25,"covered":25,"skipped":0,"pct":100},"branches":{"total":0,"covered":0,"skipped":0,"pct":100}}
@@ -28,7 +28,7 @@
2828
,"/home/runner/work/bee-js/bee-js/src/modules/debug/connectivity.ts": {"lines":{"total":35,"covered":28,"skipped":0,"pct":80},"functions":{"total":11,"covered":7,"skipped":0,"pct":63.63},"statements":{"total":36,"covered":29,"skipped":0,"pct":80.55},"branches":{"total":2,"covered":2,"skipped":0,"pct":100}}
2929
,"/home/runner/work/bee-js/bee-js/src/modules/debug/settlements.ts": {"lines":{"total":18,"covered":18,"skipped":0,"pct":100},"functions":{"total":4,"covered":4,"skipped":0,"pct":100},"statements":{"total":18,"covered":18,"skipped":0,"pct":100},"branches":{"total":0,"covered":0,"skipped":0,"pct":100}}
3030
,"/home/runner/work/bee-js/bee-js/src/modules/debug/stake.ts": {"lines":{"total":32,"covered":21,"skipped":0,"pct":65.62},"functions":{"total":6,"covered":3,"skipped":0,"pct":50},"statements":{"total":32,"covered":21,"skipped":0,"pct":65.62},"branches":{"total":0,"covered":0,"skipped":0,"pct":100}}
31-
,"/home/runner/work/bee-js/bee-js/src/modules/debug/stamps.ts": {"lines":{"total":60,"covered":58,"skipped":0,"pct":96.66},"functions":{"total":13,"covered":13,"skipped":0,"pct":100},"statements":{"total":63,"covered":61,"skipped":0,"pct":96.82},"branches":{"total":2,"covered":0,"skipped":0,"pct":0}}
31+
,"/home/runner/work/bee-js/bee-js/src/modules/debug/stamps.ts": {"lines":{"total":70,"covered":60,"skipped":0,"pct":85.71},"functions":{"total":17,"covered":13,"skipped":0,"pct":76.47},"statements":{"total":73,"covered":63,"skipped":0,"pct":86.3},"branches":{"total":2,"covered":0,"skipped":0,"pct":0}}
3232
,"/home/runner/work/bee-js/bee-js/src/modules/debug/states.ts": {"lines":{"total":29,"covered":24,"skipped":0,"pct":82.75},"functions":{"total":5,"covered":4,"skipped":0,"pct":80},"statements":{"total":29,"covered":24,"skipped":0,"pct":82.75},"branches":{"total":0,"covered":0,"skipped":0,"pct":100}}
3333
,"/home/runner/work/bee-js/bee-js/src/modules/debug/status.ts": {"lines":{"total":36,"covered":34,"skipped":0,"pct":94.44},"functions":{"total":7,"covered":6,"skipped":0,"pct":85.71},"statements":{"total":36,"covered":34,"skipped":0,"pct":94.44},"branches":{"total":0,"covered":0,"skipped":0,"pct":100}}
3434
,"/home/runner/work/bee-js/bee-js/src/modules/debug/transactions.ts": {"lines":{"total":27,"covered":18,"skipped":0,"pct":66.66},"functions":{"total":5,"covered":3,"skipped":0,"pct":60},"statements":{"total":27,"covered":18,"skipped":0,"pct":66.66},"branches":{"total":1,"covered":0,"skipped":0,"pct":0}}

0 commit comments

Comments
 (0)