@ethersphere/bee-js 9.1.1 → 9.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -374,6 +374,8 @@ After making changes, link the package to your project by running `npm link` in
374
374
 
375
375
  ### Test
376
376
 
377
+ [Code coverage](https://bah5acgza26tlmya36bdiu5cdfs3hh22hqthkhfv6cvq2ugxqrv5aw267ydlq.bzz.limo/)
378
+
377
379
  Tests are currently run against a mainnet Bee nodes. This is temporary and this section will be revised in the future.
378
380
 
379
381
  ## License
package/dist/cjs/bee.js CHANGED
@@ -1150,7 +1150,7 @@ class Bee {
1150
1150
  throw new error_1.BeeArgumentError(`Depth has to be between ${types_1.STAMPS_DEPTH_MIN}..${types_1.STAMPS_DEPTH_MAX}`, depth);
1151
1151
  }
1152
1152
  const chainState = await this.getChainState();
1153
- const minimumAmount = BigInt(chainState.currentPrice) * 17280n;
1153
+ const minimumAmount = BigInt(chainState.currentPrice) * 17280n + 1n;
1154
1154
  if (BigInt(amountString) < minimumAmount) {
1155
1155
  throw new error_1.BeeArgumentError(`Amount has to be at least ${minimumAmount} (1 day at current price ${chainState.currentPrice})`, amountString);
1156
1156
  }
@@ -1182,7 +1182,7 @@ class Bee {
1182
1182
  if (delta <= 0) {
1183
1183
  throw new error_1.BeeArgumentError('New depth has to be greater than the original depth', depth);
1184
1184
  }
1185
- await this.topUpBatch(batch.batchID, BigInt(batch.amount) * 2n ** BigInt(delta - 1), options);
1185
+ await this.topUpBatch(batch.batchID, BigInt(batch.amount) * 2n ** BigInt(delta - 1) + 1n, options);
1186
1186
  return this.diluteBatch(batch.batchID, depth, options);
1187
1187
  }
1188
1188
  async extendStorageDuration(postageBatchId, duration, options) {
@@ -19,6 +19,12 @@ class Size {
19
19
  static fromBytes(bytes) {
20
20
  return new Size(bytes);
21
21
  }
22
+ static fromKilobytes(kilobytes) {
23
+ return new Size(kilobytes * 1000);
24
+ }
25
+ static fromMegabytes(megabytes) {
26
+ return new Size(megabytes * 1000 * 1000);
27
+ }
22
28
  static fromGigabytes(gigabytes) {
23
29
  return new Size(gigabytes * 1000 * 1000 * 1000);
24
30
  }
@@ -6,6 +6,7 @@ const bytes_1 = require("./bytes");
6
6
  const duration_1 = require("./duration");
7
7
  const tokens_1 = require("./tokens");
8
8
  const type_1 = require("./type");
9
+ const MAX_UTILIZATION = 0.9;
9
10
  /**
10
11
  * Utility function that calculates usage of postage batch based on its utilization, depth and bucket depth.
11
12
  *
@@ -30,35 +31,27 @@ function getStampTheoreticalBytes(depth) {
30
31
  exports.getStampTheoreticalBytes = getStampTheoreticalBytes;
31
32
  /**
32
33
  * Based on https://docs.ethswarm.org/docs/learn/technology/contracts/postage-stamp/#effective-utilisation-table
34
+ * Optimised for encrypted, medium erasure coding
33
35
  */
34
- const utilisationRateMap = {
35
- 22: 0.2867,
36
- 23: 0.4956,
37
- 24: 0.6433,
38
- 25: 0.7478,
39
- 26: 0.8217,
40
- 27: 0.8739,
41
- 28: 0.9108,
42
- 29: 0.9369,
43
- 30: 0.9554,
44
- 31: 0.9685,
45
- 32: 0.9777,
46
- 33: 0.9842,
47
- 34: 0.9889,
48
- };
49
36
  const effectiveSizeBreakpoints = [
50
- [22, 4.93],
51
- [23, 17.03],
52
- [24, 44.21],
53
- [25, 102.78],
54
- [26, 225.87],
55
- [27, 480.44],
56
- [28, 1001.44],
57
- [29, 2060.27],
58
- [30, 4201.9],
59
- [31, 8519.02],
60
- [32, 17199.89],
61
- [33, 34628.46],
37
+ [17, 0.00004089],
38
+ [18, 0.00609],
39
+ [19, 0.10249],
40
+ [20, 0.62891],
41
+ [21, 2.38],
42
+ [22, 7.07],
43
+ [23, 18.24],
44
+ [24, 43.04],
45
+ [25, 96.5],
46
+ [26, 208.52],
47
+ [27, 435.98],
48
+ [28, 908.81],
49
+ [29, 1870],
50
+ [30, 3810],
51
+ [31, 7730],
52
+ [32, 15610],
53
+ [33, 31430],
54
+ [34, 63150],
62
55
  ];
63
56
  /**
64
57
  * Utility function that calculates the effective size of a postage batch based on its depth.
@@ -69,16 +62,23 @@ const effectiveSizeBreakpoints = [
69
62
  * @returns {number} The effective size of the postage batch in bytes.
70
63
  */
71
64
  function getStampEffectiveBytes(depth) {
72
- if (depth < 22) {
65
+ if (depth < 17) {
73
66
  return 0;
74
67
  }
75
- const utilRate = utilisationRateMap[depth] ?? 0.99;
76
- return Math.ceil(getStampTheoreticalBytes(depth) * utilRate);
68
+ const breakpoint = effectiveSizeBreakpoints.find(([d, size]) => {
69
+ if (depth === d) {
70
+ return size;
71
+ }
72
+ });
73
+ if (breakpoint) {
74
+ return breakpoint[1] * 1000 * 1000 * 1000;
75
+ }
76
+ return Math.ceil(getStampTheoreticalBytes(depth) * MAX_UTILIZATION);
77
77
  }
78
78
  exports.getStampEffectiveBytes = getStampEffectiveBytes;
79
79
  function getStampEffectiveBytesBreakpoints() {
80
80
  const map = new Map();
81
- for (let i = 22; i < 35; i++) {
81
+ for (let i = 17; i < 35; i++) {
82
82
  map.set(i, getStampEffectiveBytes(i));
83
83
  }
84
84
  return map;
@@ -111,7 +111,7 @@ exports.getStampDuration = getStampDuration;
111
111
  * @param blockTime The block time in seconds.
112
112
  */
113
113
  function getAmountForDuration(duration, pricePerBlock, blockTime) {
114
- return (BigInt(duration.toSeconds()) / BigInt(blockTime)) * BigInt(pricePerBlock);
114
+ return (BigInt(duration.toSeconds()) / BigInt(blockTime)) * BigInt(pricePerBlock) + 1n;
115
115
  }
116
116
  exports.getAmountForDuration = getAmountForDuration;
117
117
  /**
@@ -122,11 +122,11 @@ exports.getAmountForDuration = getAmountForDuration;
122
122
  */
123
123
  function getDepthForSize(size) {
124
124
  for (const [depth, sizeBreakpoint] of effectiveSizeBreakpoints) {
125
- if (size.toGigabytes() <= sizeBreakpoint) {
125
+ if (size.toBytes() <= sizeBreakpoint * 1000 * 1000 * 1000) {
126
126
  return depth;
127
127
  }
128
128
  }
129
- return 34;
129
+ return 35;
130
130
  }
131
131
  exports.getDepthForSize = getDepthForSize;
132
132
  function convertEnvelopeToMarshaledStamp(envelope) {