@upcoming/bee-js 9.8.0 → 9.9.1

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/mjs/bee.js CHANGED
@@ -1217,23 +1217,28 @@ export class Bee {
1217
1217
  const depth = getDepthForSize(size, encryption, erasureCodeLevel);
1218
1218
  const chainState = await this.getChainState(options);
1219
1219
  const depthDelta = depth - batch.depth;
1220
- const multiplier = depthDelta === 0 ? 1n : 2n ** BigInt(depthDelta);
1221
- const targetAmount = (BigInt(batch.amount) + getAmountForDuration(duration, chainState.currentPrice, this.network === 'gnosis' ? 5 : 15)) * multiplier;
1222
- const amountDelta = targetAmount - BigInt(batch.amount);
1220
+ const multiplier = depthDelta <= 0 ? 1n : 2n ** BigInt(depthDelta);
1221
+ const blockTime = this.network === 'gnosis' ? 5 : 15;
1222
+ const additionalAmount = getAmountForDuration(duration, chainState.currentPrice, blockTime);
1223
+ const currentAmount = getAmountForDuration(batch.duration, chainState.currentPrice, blockTime);
1224
+ const targetAmount = duration.isZero() ? currentAmount * multiplier : currentAmount + additionalAmount * multiplier;
1225
+ const amountDelta = targetAmount - currentAmount;
1223
1226
  const transactionId = await this.topUpBatch(batch.batchID, amountDelta, options);
1224
- if (depthDelta) {
1227
+ if (depthDelta > 0) {
1225
1228
  return this.diluteBatch(batch.batchID, depth, options);
1226
1229
  }
1227
1230
  return transactionId;
1228
1231
  }
1229
1232
  async extendStorageSize(postageBatchId, size, options, encryption, erasureCodeLevel) {
1233
+ const chainState = await this.getChainState(options);
1230
1234
  const batch = await this.getPostageBatch(postageBatchId, options);
1231
1235
  const depth = getDepthForSize(size, encryption, erasureCodeLevel);
1232
1236
  const delta = depth - batch.depth;
1233
1237
  if (delta <= 0) {
1234
1238
  throw new BeeArgumentError('New depth has to be greater than the original depth', depth);
1235
1239
  }
1236
- await this.topUpBatch(batch.batchID, BigInt(batch.amount) * 2n ** BigInt(delta - 1) + 1n, options);
1240
+ const currentAmount = getAmountForDuration(batch.duration, chainState.currentPrice, this.network === 'gnosis' ? 5 : 15);
1241
+ await this.topUpBatch(batch.batchID, currentAmount * (2n ** BigInt(delta) - 1n) + 1n, options);
1237
1242
  return this.diluteBatch(batch.batchID, depth, options);
1238
1243
  }
1239
1244
  async extendStorageDuration(postageBatchId, duration, options) {
@@ -1245,22 +1250,26 @@ export class Bee {
1245
1250
  async getExtensionCost(postageBatchId, size, duration, options, encryption, erasureCodeLevel) {
1246
1251
  const batch = await this.getPostageBatch(postageBatchId, options);
1247
1252
  const chainState = await this.getChainState(options);
1248
- const amount = getAmountForDuration(duration, chainState.currentPrice, this.network === 'gnosis' ? 5 : 15);
1253
+ const blockTime = this.network === 'gnosis' ? 5 : 15;
1254
+ const amount = duration.isZero() ? 0n : getAmountForDuration(duration, chainState.currentPrice, blockTime);
1249
1255
  const depth = getDepthForSize(size, encryption, erasureCodeLevel);
1250
- const currentValue = getStampCost(batch.depth, batch.amount);
1251
- const newValue = getStampCost(depth, amount);
1252
- return newValue.minus(currentValue);
1256
+ const currentAmount = getAmountForDuration(batch.duration, chainState.currentPrice, blockTime);
1257
+ const currentCost = getStampCost(batch.depth, currentAmount);
1258
+ const newCost = getStampCost(depth, currentAmount + amount);
1259
+ return newCost.minus(currentCost);
1253
1260
  }
1254
1261
  async getSizeExtensionCost(postageBatchId, size, options, encryption, erasureCodeLevel) {
1255
1262
  const batch = await this.getPostageBatch(postageBatchId, options);
1263
+ const chainState = await this.getChainState(options);
1256
1264
  const depth = getDepthForSize(size, encryption, erasureCodeLevel);
1257
1265
  const delta = depth - batch.depth;
1258
1266
  if (delta <= 0) {
1259
1267
  throw new BeeArgumentError('New depth has to be greater than the original depth', depth);
1260
1268
  }
1261
- const currentPaid = getStampCost(batch.depth, batch.amount);
1262
- const newPaid = getStampCost(depth, batch.amount);
1263
- return newPaid.minus(currentPaid);
1269
+ const currentAmount = getAmountForDuration(batch.duration, chainState.currentPrice, this.network === 'gnosis' ? 5 : 15);
1270
+ const currentCost = getStampCost(batch.depth, currentAmount);
1271
+ const newCost = getStampCost(depth, currentAmount);
1272
+ return newCost.minus(currentCost);
1264
1273
  }
1265
1274
  async getDurationExtensionCost(postageBatchId, duration, options) {
1266
1275
  const batch = await this.getPostageBatch(postageBatchId, options);
@@ -1,6 +1,8 @@
1
1
  import { Binary, MerkleTree, Optional, Uint8ArrayReader } from 'cafe-utility';
2
+ import _debug from 'debug';
2
3
  import { NULL_ADDRESS } from "../index.js";
3
4
  import { Reference } from "../utils/typed-bytes.js";
5
+ const debug = _debug('bee-js:manifest');
4
6
  const ENCODER = new TextEncoder();
5
7
  const DECODER = new TextDecoder();
6
8
  const TYPE_VALUE = 2;
@@ -60,6 +62,11 @@ export class Fork {
60
62
  data.push(new Uint8Array(30 - this.prefix.length));
61
63
  }
62
64
  data.push(this.node.selfAddress);
65
+ debug('marshalling fork', {
66
+ prefixLength: this.prefix.length,
67
+ prefix: DECODER.decode(this.prefix),
68
+ address: Binary.uint8ArrayToHex(this.node.selfAddress)
69
+ });
63
70
  if (this.node.metadata) {
64
71
  const metadataBytes = Binary.padEndToMultiple(new Uint8Array([0x00, 0x00, ...ENCODER.encode(JSON.stringify(this.node.metadata))]), 32, 0x0a);
65
72
  const metadataLengthBytes = Binary.numberToUint16(metadataBytes.length - 2, 'BE');
@@ -72,8 +79,17 @@ export class Fork {
72
79
  const type = Binary.uint8ToNumber(reader.read(1));
73
80
  const prefixLength = Binary.uint8ToNumber(reader.read(1));
74
81
  const prefix = reader.read(prefixLength);
75
- reader.read(30 - prefixLength);
82
+ if (prefixLength < 30) {
83
+ reader.read(30 - prefixLength);
84
+ }
76
85
  const selfAddress = reader.read(addressLength);
86
+ debug('unmarshalling fork', {
87
+ type,
88
+ prefixLength,
89
+ prefix: DECODER.decode(prefix),
90
+ addressLength,
91
+ address: Binary.uint8ArrayToHex(selfAddress)
92
+ });
77
93
  let metadata = undefined;
78
94
  if (isType(type, TYPE_WITH_METADATA)) {
79
95
  const metadataLength = Binary.uint16ToNumber(reader.read(2), 'BE');
@@ -210,7 +226,7 @@ export class MantarayNode {
210
226
  throw new Error('MantarayNode#unmarshal invalid version hash');
211
227
  }
212
228
  const targetAddressLength = Binary.uint8ToNumber(reader.read(1));
213
- const targetAddress = targetAddressLength === 0 ? NULL_ADDRESS : reader.read(targetAddressLength);
229
+ const targetAddress = targetAddressLength ? reader.read(targetAddressLength) : NULL_ADDRESS;
214
230
  const node = new MantarayNode({
215
231
  selfAddress,
216
232
  targetAddress,
@@ -219,7 +235,7 @@ export class MantarayNode {
219
235
  const forkBitmap = reader.read(32);
220
236
  for (let i = 0; i < 256; i++) {
221
237
  if (Binary.getBit(forkBitmap, i, 'LE')) {
222
- const newFork = Fork.unmarshal(reader, targetAddressLength);
238
+ const newFork = Fork.unmarshal(reader, selfAddress.length);
223
239
  node.forks.set(i, newFork);
224
240
  newFork.node.parent = node;
225
241
  }
@@ -232,6 +248,10 @@ export class MantarayNode {
232
248
  addFork(path, reference, metadata) {
233
249
  this.selfAddress = null;
234
250
  path = path instanceof Uint8Array ? path : ENCODER.encode(path);
251
+ debug('adding fork', {
252
+ path: DECODER.decode(path),
253
+ reference: new Reference(reference).represent()
254
+ });
235
255
  // TODO: this should not be ignored
236
256
  // eslint-disable-next-line @typescript-eslint/no-this-alias
237
257
  let tip = this;
@@ -2,8 +2,8 @@ import { Dates } from 'cafe-utility';
2
2
  export class Duration {
3
3
  constructor(seconds) {
4
4
  this.seconds = Math.ceil(seconds);
5
- if (seconds <= 0) {
6
- throw Error('Duration must be greater than 0');
5
+ if (seconds < 0) {
6
+ throw Error('Duration cannot be negative');
7
7
  }
8
8
  }
9
9
  static fromMilliseconds(milliseconds) {
@@ -48,4 +48,8 @@ export class Duration {
48
48
  represent() {
49
49
  return Dates.secondsToHumanTime(this.seconds);
50
50
  }
51
- }
51
+ isZero() {
52
+ return this.seconds === 0;
53
+ }
54
+ }
55
+ Duration.ZERO = new Duration(0);
@@ -1,6 +1,8 @@
1
1
  import axios from 'axios';
2
2
  import { Dates, Objects, Strings, System } from 'cafe-utility';
3
+ import _debug from 'debug';
3
4
  import { BeeResponseError } from "../index.js";
5
+ const debug = _debug('bee-js:http');
4
6
  const {
5
7
  AxiosError
6
8
  } = axios;
@@ -37,6 +39,12 @@ export async function http(options, config) {
37
39
  let failedAttempts = 0;
38
40
  while (failedAttempts < MAX_FAILED_ATTEMPTS) {
39
41
  try {
42
+ debug(`${requestConfig.method || 'get'} ${Strings.joinUrl([requestConfig.baseURL, requestConfig.url])}`, {
43
+ headers: {
44
+ ...requestConfig.headers
45
+ },
46
+ params: requestConfig.params
47
+ });
40
48
  maybeRunOnRequestHook(options, requestConfig);
41
49
  const response = await axios(requestConfig);
42
50
  return response;
@@ -1,5 +1,6 @@
1
1
  export declare class Duration {
2
2
  private seconds;
3
+ static ZERO: Duration;
3
4
  private constructor();
4
5
  static fromMilliseconds(milliseconds: number): Duration;
5
6
  static fromSeconds(seconds: number): Duration;
@@ -15,4 +16,5 @@ export declare class Duration {
15
16
  toYears(): number;
16
17
  toEndDate(startDate?: Date): Date;
17
18
  represent(): string;
19
+ isZero(): boolean;
18
20
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@upcoming/bee-js",
3
- "version": "9.8.0",
3
+ "version": "9.9.1",
4
4
  "description": "Javascript client for Bee",
5
5
  "keywords": [
6
6
  "bee",
@@ -63,6 +63,7 @@
63
63
  "dependencies": {
64
64
  "axios": "^0.30.0",
65
65
  "cafe-utility": "^31.0.0",
66
+ "debug": "^4.4.1",
66
67
  "isomorphic-ws": "^4.0.1",
67
68
  "semver": "^7.3.5",
68
69
  "ws": "^8.7.0"
@@ -78,6 +79,7 @@
78
79
  "@commitlint/config-conventional": "^17.4.2",
79
80
  "@jest/types": "^29.6.3",
80
81
  "@naholyr/cross-env": "^1.0.0",
82
+ "@types/debug": "^4.1.12",
81
83
  "@types/jest": "^29.5.13",
82
84
  "@types/node": "^18.11.11",
83
85
  "@types/semver": "^7.3.9",