@ethersphere/bee-js 9.1.2 → 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/dist/cjs/utils/size.js +6 -0
- package/dist/cjs/utils/stamps.js +33 -33
- package/dist/index.browser.min.js +1 -1
- package/dist/index.browser.min.js.map +1 -1
- package/dist/mjs/utils/size.js +6 -0
- package/dist/mjs/utils/stamps.js +16 -22
- package/dist/types/utils/size.d.ts +2 -0
- package/package.json +1 -1
package/dist/cjs/utils/size.js
CHANGED
|
@@ -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
|
}
|
package/dist/cjs/utils/stamps.js
CHANGED
|
@@ -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
|
-
[
|
|
51
|
-
[
|
|
52
|
-
[
|
|
53
|
-
[
|
|
54
|
-
[
|
|
55
|
-
[
|
|
56
|
-
[
|
|
57
|
-
[
|
|
58
|
-
[
|
|
59
|
-
[
|
|
60
|
-
[
|
|
61
|
-
[
|
|
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 <
|
|
65
|
+
if (depth < 17) {
|
|
73
66
|
return 0;
|
|
74
67
|
}
|
|
75
|
-
const
|
|
76
|
-
|
|
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 =
|
|
81
|
+
for (let i = 17; i < 35; i++) {
|
|
82
82
|
map.set(i, getStampEffectiveBytes(i));
|
|
83
83
|
}
|
|
84
84
|
return map;
|
|
@@ -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.
|
|
125
|
+
if (size.toBytes() <= sizeBreakpoint * 1000 * 1000 * 1000) {
|
|
126
126
|
return depth;
|
|
127
127
|
}
|
|
128
128
|
}
|
|
129
|
-
return
|
|
129
|
+
return 35;
|
|
130
130
|
}
|
|
131
131
|
exports.getDepthForSize = getDepthForSize;
|
|
132
132
|
function convertEnvelopeToMarshaledStamp(envelope) {
|