@typeberry/jam 0.2.0-a46bfc7 → 0.2.0-b525add

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/index.js CHANGED
@@ -33214,7 +33214,7 @@ function hashComparator(a, b) {
33214
33214
 
33215
33215
  ;// CONCATENATED MODULE: ./packages/core/pvm-interpreter/ops/math-consts.ts
33216
33216
  const MAX_VALUE = 4294967295;
33217
- const math_consts_MAX_VALUE_U64 = (/* unused pure expression or super */ null && (2n ** 63n));
33217
+ const math_consts_MAX_VALUE_U64 = 2n ** 63n;
33218
33218
  const MIN_VALUE = -(2 ** 31);
33219
33219
  const MAX_SHIFT_U32 = 32;
33220
33220
  const MAX_SHIFT_U64 = 64n;
@@ -40433,17 +40433,16 @@ class FetchExternalities {
40433
40433
 
40434
40434
 
40435
40435
 
40436
+
40436
40437
  class AccumulateDataItem {
40437
40438
  operands;
40438
40439
  reportsLength;
40439
- gasCost;
40440
- constructor(operands, reportsLength, gasCost) {
40440
+ constructor(operands, reportsLength) {
40441
40441
  this.operands = operands;
40442
40442
  this.reportsLength = reportsLength;
40443
- this.gasCost = gasCost;
40444
40443
  }
40445
40444
  static empty() {
40446
- return new AccumulateDataItem([], numbers_tryAsU32(0), tryAsServiceGas(0n));
40445
+ return new AccumulateDataItem([], numbers_tryAsU32(0));
40447
40446
  }
40448
40447
  }
40449
40448
  /**
@@ -40455,21 +40454,39 @@ class AccumulateDataItem {
40455
40454
  class AccumulateData {
40456
40455
  reportsDataByServiceId;
40457
40456
  transfersByServiceId;
40458
- autoAccumulateServicesByServiceId;
40457
+ gasLimitByServiceId;
40459
40458
  serviceIds;
40460
40459
  constructor(reports, transfers, autoAccumulateServices) {
40461
- const { autoAccumulateServicesByServiceId, serviceIds: serviceIdsFromAutoAccumulate } = this.transformAutoAccumulateServices(autoAccumulateServices);
40462
- this.autoAccumulateServicesByServiceId = autoAccumulateServicesByServiceId;
40463
- const { reportsDataByServiceId, serviceIds: serviceIdsFromReports } = this.transformReports(reports);
40460
+ const { serviceIds: serviceIdsFromAutoAccumulate, gasLimitByServiceId: autoAccumulateGasLimitByServiceId } = this.transformAutoAccumulateServices(autoAccumulateServices);
40461
+ const { reportsDataByServiceId, serviceIds: serviceIdsFromReports, gasLimitByServiceId: reportsGasLimitByServiceId, } = this.transformReports(reports);
40464
40462
  this.reportsDataByServiceId = reportsDataByServiceId;
40465
- const { transfersByServiceId, serviceIds: serviceIdsFromTransfers } = this.transformTransfers(transfers);
40463
+ const { transfersByServiceId, serviceIds: serviceIdsFromTransfers, gasLimitByServiceId: transfersGasLimitByServiceId, } = this.transformTransfers(transfers);
40466
40464
  this.transfersByServiceId = transfersByServiceId;
40467
40465
  /**
40468
40466
  * Merge service ids from reports, auto-accumulate services and transfers.
40469
40467
  *
40470
- * https://graypaper.fluffylabs.dev/#/68eaa1f/175f01175f01?v=0.6.4
40468
+ * https://graypaper.fluffylabs.dev/#/ab2cdbd/173803174b03?v=0.7.2
40471
40469
  */
40472
40470
  this.serviceIds = this.mergeServiceIds(serviceIdsFromReports, serviceIdsFromAutoAccumulate, serviceIdsFromTransfers);
40471
+ /**
40472
+ * Merge gas limits from reports, auto-accumulate services and transfers.
40473
+ *
40474
+ * https://graypaper.fluffylabs.dev/#/ab2cdbd/182001183701?v=0.7.2
40475
+ */
40476
+ this.gasLimitByServiceId = this.mergeGasLimitByServiceId(this.serviceIds, autoAccumulateGasLimitByServiceId, reportsGasLimitByServiceId, transfersGasLimitByServiceId);
40477
+ }
40478
+ /**
40479
+ * Calculate the gas limit implied by the selected deferred-transfers, work-reports and gas-privileges.
40480
+ *
40481
+ * https://graypaper.fluffylabs.dev/#/ab2cdbd/182001183701?v=0.7.2
40482
+ */
40483
+ mergeGasLimitByServiceId(serviceIds, ...gasLimitByServiceIdMaps) {
40484
+ const gasByServiceId = new Map();
40485
+ for (const serviceId of serviceIds) {
40486
+ const { overflow, value } = sumU64(...gasLimitByServiceIdMaps.map((map) => map.get(serviceId) ?? tryAsServiceGas(0)));
40487
+ gasByServiceId.set(serviceId, tryAsServiceGas(overflow ? math_consts_MAX_VALUE_U64 : value));
40488
+ }
40489
+ return gasByServiceId;
40473
40490
  }
40474
40491
  /** Merge two sets of service ids */
40475
40492
  mergeServiceIds(...sources) {
@@ -40481,50 +40498,74 @@ class AccumulateData {
40481
40498
  }
40482
40499
  return Array.from(merged);
40483
40500
  }
40501
+ /**
40502
+ * Transform the list of pending transfers into:
40503
+ * - map: transfers by service id
40504
+ * - map: gas limit by service id
40505
+ * - set: service ids
40506
+ */
40484
40507
  transformTransfers(transfersToTransform) {
40485
40508
  const transfersByServiceId = new Map();
40486
40509
  const serviceIds = new Set();
40510
+ const gasLimitByServiceId = new Map();
40487
40511
  for (const transfer of transfersToTransform) {
40488
40512
  const serviceId = transfer.destination;
40489
40513
  const transfers = transfersByServiceId.get(serviceId) ?? [];
40514
+ const gas = gasLimitByServiceId.get(serviceId) ?? tryAsServiceGas(0n);
40515
+ const { value, overflow } = sumU64(gas, transfer.gas);
40516
+ gasLimitByServiceId.set(serviceId, tryAsServiceGas(overflow ? math_consts_MAX_VALUE_U64 : value));
40490
40517
  transfers.push(transfer);
40491
40518
  transfersByServiceId.set(serviceId, transfers);
40492
40519
  serviceIds.add(serviceId);
40493
40520
  }
40494
- return { transfersByServiceId, serviceIds };
40521
+ return { transfersByServiceId, serviceIds, gasLimitByServiceId };
40495
40522
  }
40496
- /** Transform the list of auto-accumulate services into a map by service id. */
40523
+ /**
40524
+ * Transform the list of auto accumulate services into:
40525
+ * - map: gas limit by service id
40526
+ * - set: service ids
40527
+ */
40497
40528
  transformAutoAccumulateServices(autoAccumulateServices) {
40498
40529
  const serviceIds = new Set();
40499
- const autoAccumulateServicesByServiceId = new Map();
40530
+ const gasLimitByServiceId = new Map();
40500
40531
  for (const autoAccumulate of autoAccumulateServices) {
40501
- autoAccumulateServicesByServiceId.set(autoAccumulate.service, autoAccumulate);
40532
+ gasLimitByServiceId.set(autoAccumulate.service, autoAccumulate.gasLimit);
40502
40533
  serviceIds.add(autoAccumulate.service);
40503
40534
  }
40504
- return { autoAccumulateServicesByServiceId, serviceIds };
40535
+ return { serviceIds, gasLimitByServiceId };
40505
40536
  }
40506
40537
  /**
40507
40538
  * A function that transform reports into a list of operands and data needed for statistics (gas cost and reports length).
40508
40539
  */
40540
+ /**
40541
+ * Transform the list of reports into:
40542
+ * - map: AccumulateDataItem by service id
40543
+ * - map: gas limit by service id
40544
+ * - set: service ids
40545
+ */
40509
40546
  transformReports(reports) {
40510
40547
  const reportsDataByServiceId = new Map();
40548
+ const gasLimitByServiceId = new Map();
40511
40549
  const serviceIds = new Set();
40512
40550
  for (const report of reports) {
40513
40551
  for (const result of report.results) {
40514
40552
  const serviceId = result.serviceId;
40515
40553
  serviceIds.add(serviceId);
40516
40554
  const item = reportsDataByServiceId.get(serviceId) ?? AccumulateDataItem.empty();
40555
+ const gas = gasLimitByServiceId.get(serviceId) ?? tryAsServiceGas(0n);
40556
+ const { value, overflow } = sumU64(gas, result.gas);
40557
+ const newGas = tryAsServiceGas(overflow ? math_consts_MAX_VALUE_U64 : value);
40558
+ gasLimitByServiceId.set(serviceId, newGas);
40517
40559
  /**
40518
40560
  * We count the report results and gas cost for each service to update service statistics.
40519
40561
  *
40520
- * https://graypaper.fluffylabs.dev/#/68eaa1f/171e04174a04?v=0.6.4
40562
+ * https://graypaper.fluffylabs.dev/#/ab2cdbd/180504182604?v=0.7.2
40521
40563
  */
40522
40564
  item.reportsLength = numbers_tryAsU32(item.reportsLength + 1);
40523
- item.gasCost = tryAsServiceGas(item.gasCost + result.gas);
40524
40565
  /**
40525
40566
  * Transform report into an operand
40526
40567
  *
40527
- * https://graypaper.fluffylabs.dev/#/68eaa1f/17bf02176f03?v=0.6.4
40568
+ * https://graypaper.fluffylabs.dev/#/ab2cdbd/185901181402?v=0.7.2
40528
40569
  */
40529
40570
  item.operands.push(Operand.new({
40530
40571
  gas: result.gas, // g
@@ -40538,19 +40579,7 @@ class AccumulateData {
40538
40579
  reportsDataByServiceId.set(serviceId, item);
40539
40580
  }
40540
40581
  }
40541
- /**
40542
- * Add initial gas cost - it is `U(f_s, 0)` from this formula:
40543
- *
40544
- * https://graypaper.fluffylabs.dev/#/68eaa1f/17b00217b002?v=0.6.4
40545
- */
40546
- for (const serviceId of serviceIds) {
40547
- const item = reportsDataByServiceId.get(serviceId) ?? null;
40548
- const autoAccumulateService = this.autoAccumulateServicesByServiceId.get(serviceId) ?? null;
40549
- if (item !== null && autoAccumulateService !== null) {
40550
- item.gasCost = tryAsServiceGas(item.gasCost + autoAccumulateService.gasLimit);
40551
- }
40552
- }
40553
- return { reportsDataByServiceId, serviceIds };
40582
+ return { reportsDataByServiceId, serviceIds, gasLimitByServiceId };
40554
40583
  }
40555
40584
  /** Returns the list of operands for a given service id */
40556
40585
  getOperands(serviceId) {
@@ -40564,14 +40593,14 @@ class AccumulateData {
40564
40593
  getReportsLength(serviceId) {
40565
40594
  return this.reportsDataByServiceId.get(serviceId)?.reportsLength ?? numbers_tryAsU32(0);
40566
40595
  }
40567
- /** Returns the gas cost for a given service id */
40568
- getGasCost(serviceId) {
40569
- return this.reportsDataByServiceId.get(serviceId)?.gasCost ?? tryAsServiceGas(0n);
40596
+ /** Returns the gas limit for a given service id */
40597
+ getGasLimit(serviceId) {
40598
+ return this.gasLimitByServiceId.get(serviceId) ?? tryAsServiceGas(0n);
40570
40599
  }
40571
40600
  /**
40572
40601
  * Returns a list of service ids that should be accumulated.
40573
40602
  *
40574
- * https://graypaper.fluffylabs.dev/#/68eaa1f/175f01175f01?v=0.6.4
40603
+ * https://graypaper.fluffylabs.dev/#/ab2cdbd/173803174a03?v=0.7.2
40575
40604
  */
40576
40605
  getServiceIds() {
40577
40606
  return this.serviceIds;
@@ -47100,6 +47129,7 @@ class PvmExecutor {
47100
47129
 
47101
47130
 
47102
47131
 
47132
+
47103
47133
  const ACCUMULATION_ERROR = "duplicate service created";
47104
47134
  var PvmInvocationError;
47105
47135
  (function (PvmInvocationError) {
@@ -47300,8 +47330,15 @@ class Accumulate {
47300
47330
  const { gasCost, state: stateAfterParallelAcc, ...rest } = await this.accumulateInParallel(accumulateData, slot, entropy, statistics, stateUpdate);
47301
47331
  const newTransfers = stateAfterParallelAcc.takeTransfers();
47302
47332
  assertEmpty(rest);
47333
+ /**
47334
+ * Gas limit from transfers is added to the next round of accumulation
47335
+ *
47336
+ * https://graypaper.fluffylabs.dev/#/ab2cdbd/172b02172b02?v=0.7.2
47337
+ */
47338
+ const transfersGas = transfers.map((t) => t.gas);
47339
+ const { value: newGasLimit, overflow } = sumU64(tryAsServiceGas(gasLimit - gasCost), ...transfersGas);
47303
47340
  // NOTE [ToDr] recursive invocation
47304
- const { accumulatedReports, gasCost: seqGasCost, state, ...seqRest } = await this.accumulateSequentially(tryAsServiceGas(gasLimit - gasCost), reportsToAccumulateSequentially, newTransfers, slot, entropy, statistics, stateAfterParallelAcc, []);
47341
+ const { accumulatedReports, gasCost: seqGasCost, state, ...seqRest } = await this.accumulateSequentially(tryAsServiceGas(overflow ? math_consts_MAX_VALUE_U64 : newGasLimit), reportsToAccumulateSequentially, newTransfers, slot, entropy, statistics, stateAfterParallelAcc, []);
47305
47342
  assertEmpty(seqRest);
47306
47343
  return {
47307
47344
  accumulatedReports: numbers_tryAsU32(i + accumulatedReports),
@@ -47327,7 +47364,7 @@ class Accumulate {
47327
47364
  for (const serviceId of serviceIds) {
47328
47365
  const checkpoint = AccumulationStateUpdate.copyFrom(currentState);
47329
47366
  const operands = accumulateData.getOperands(serviceId);
47330
- const { consumedGas, stateUpdate } = await this.accumulateSingleService(serviceId, accumulateData.getTransfers(serviceId), operands, accumulateData.getGasCost(serviceId), slot, entropy, currentState);
47367
+ const { consumedGas, stateUpdate } = await this.accumulateSingleService(serviceId, accumulateData.getTransfers(serviceId), operands, accumulateData.getGasLimit(serviceId), slot, entropy, currentState);
47331
47368
  gasCost = tryAsServiceGas(gasCost + consumedGas);
47332
47369
  // https://graypaper.fluffylabs.dev/#/ab2cdbd/193b05193b05?v=0.7.2
47333
47370
  const serviceStatistics = statistics.get(serviceId) ?? { count: numbers_tryAsU32(0), gasUsed: tryAsServiceGas(0) };
@@ -47347,7 +47384,7 @@ class Accumulate {
47347
47384
  // Since serviceIds already contains newV, this service gets accumulated twice.
47348
47385
  // To avoid double-counting, we skip stats and gas cost tracking here.
47349
47386
  // We need this accumulation to get the correct `delegator`
47350
- const { stateUpdate } = await this.accumulateSingleService(newV, [], accumulateData.getOperands(newV), accumulateData.getGasCost(newV), slot, entropy, checkpoint);
47387
+ const { stateUpdate } = await this.accumulateSingleService(newV, [], accumulateData.getOperands(newV), accumulateData.getGasLimit(newV), slot, entropy, checkpoint);
47351
47388
  const correctV = stateUpdate?.privilegedServices?.delegator ?? this.state.privilegedServices.delegator;
47352
47389
  currentState.privilegedServices = PrivilegedServices.create({
47353
47390
  ...currentState.privilegedServices,
@@ -48885,7 +48922,6 @@ class Statistics {
48885
48922
 
48886
48923
 
48887
48924
 
48888
-
48889
48925
  class DbHeaderChain {
48890
48926
  blocks;
48891
48927
  constructor(blocks) {
@@ -48956,7 +48992,7 @@ class OnChain {
48956
48992
  // chapter 13: https://graypaper.fluffylabs.dev/#/68eaa1f/18b60118b601?v=0.6.4
48957
48993
  statistics;
48958
48994
  isReadyForNextEpoch = Promise.resolve(false);
48959
- constructor(chainSpec, state, blocks, hasher, pvm = PvmBackend.BuiltIn) {
48995
+ constructor(chainSpec, state, blocks, hasher, pvm) {
48960
48996
  this.chainSpec = chainSpec;
48961
48997
  this.state = state;
48962
48998
  this.hasher = hasher;