@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.
@@ -9770,7 +9770,7 @@ function hashComparator(a, b) {
9770
9770
 
9771
9771
  ;// CONCATENATED MODULE: ./packages/core/pvm-interpreter/ops/math-consts.ts
9772
9772
  const MAX_VALUE = 4294967295;
9773
- const math_consts_MAX_VALUE_U64 = (/* unused pure expression or super */ null && (2n ** 63n));
9773
+ const math_consts_MAX_VALUE_U64 = 2n ** 63n;
9774
9774
  const MIN_VALUE = -(2 ** 31);
9775
9775
  const MAX_SHIFT_U32 = 32;
9776
9776
  const MAX_SHIFT_U64 = 64n;
@@ -15806,17 +15806,16 @@ class FetchExternalities {
15806
15806
 
15807
15807
 
15808
15808
 
15809
+
15809
15810
  class AccumulateDataItem {
15810
15811
  operands;
15811
15812
  reportsLength;
15812
- gasCost;
15813
- constructor(operands, reportsLength, gasCost) {
15813
+ constructor(operands, reportsLength) {
15814
15814
  this.operands = operands;
15815
15815
  this.reportsLength = reportsLength;
15816
- this.gasCost = gasCost;
15817
15816
  }
15818
15817
  static empty() {
15819
- return new AccumulateDataItem([], numbers_tryAsU32(0), tryAsServiceGas(0n));
15818
+ return new AccumulateDataItem([], numbers_tryAsU32(0));
15820
15819
  }
15821
15820
  }
15822
15821
  /**
@@ -15828,21 +15827,39 @@ class AccumulateDataItem {
15828
15827
  class AccumulateData {
15829
15828
  reportsDataByServiceId;
15830
15829
  transfersByServiceId;
15831
- autoAccumulateServicesByServiceId;
15830
+ gasLimitByServiceId;
15832
15831
  serviceIds;
15833
15832
  constructor(reports, transfers, autoAccumulateServices) {
15834
- const { autoAccumulateServicesByServiceId, serviceIds: serviceIdsFromAutoAccumulate } = this.transformAutoAccumulateServices(autoAccumulateServices);
15835
- this.autoAccumulateServicesByServiceId = autoAccumulateServicesByServiceId;
15836
- const { reportsDataByServiceId, serviceIds: serviceIdsFromReports } = this.transformReports(reports);
15833
+ const { serviceIds: serviceIdsFromAutoAccumulate, gasLimitByServiceId: autoAccumulateGasLimitByServiceId } = this.transformAutoAccumulateServices(autoAccumulateServices);
15834
+ const { reportsDataByServiceId, serviceIds: serviceIdsFromReports, gasLimitByServiceId: reportsGasLimitByServiceId, } = this.transformReports(reports);
15837
15835
  this.reportsDataByServiceId = reportsDataByServiceId;
15838
- const { transfersByServiceId, serviceIds: serviceIdsFromTransfers } = this.transformTransfers(transfers);
15836
+ const { transfersByServiceId, serviceIds: serviceIdsFromTransfers, gasLimitByServiceId: transfersGasLimitByServiceId, } = this.transformTransfers(transfers);
15839
15837
  this.transfersByServiceId = transfersByServiceId;
15840
15838
  /**
15841
15839
  * Merge service ids from reports, auto-accumulate services and transfers.
15842
15840
  *
15843
- * https://graypaper.fluffylabs.dev/#/68eaa1f/175f01175f01?v=0.6.4
15841
+ * https://graypaper.fluffylabs.dev/#/ab2cdbd/173803174b03?v=0.7.2
15844
15842
  */
15845
15843
  this.serviceIds = this.mergeServiceIds(serviceIdsFromReports, serviceIdsFromAutoAccumulate, serviceIdsFromTransfers);
15844
+ /**
15845
+ * Merge gas limits from reports, auto-accumulate services and transfers.
15846
+ *
15847
+ * https://graypaper.fluffylabs.dev/#/ab2cdbd/182001183701?v=0.7.2
15848
+ */
15849
+ this.gasLimitByServiceId = this.mergeGasLimitByServiceId(this.serviceIds, autoAccumulateGasLimitByServiceId, reportsGasLimitByServiceId, transfersGasLimitByServiceId);
15850
+ }
15851
+ /**
15852
+ * Calculate the gas limit implied by the selected deferred-transfers, work-reports and gas-privileges.
15853
+ *
15854
+ * https://graypaper.fluffylabs.dev/#/ab2cdbd/182001183701?v=0.7.2
15855
+ */
15856
+ mergeGasLimitByServiceId(serviceIds, ...gasLimitByServiceIdMaps) {
15857
+ const gasByServiceId = new Map();
15858
+ for (const serviceId of serviceIds) {
15859
+ const { overflow, value } = sumU64(...gasLimitByServiceIdMaps.map((map) => map.get(serviceId) ?? tryAsServiceGas(0)));
15860
+ gasByServiceId.set(serviceId, tryAsServiceGas(overflow ? math_consts_MAX_VALUE_U64 : value));
15861
+ }
15862
+ return gasByServiceId;
15846
15863
  }
15847
15864
  /** Merge two sets of service ids */
15848
15865
  mergeServiceIds(...sources) {
@@ -15854,50 +15871,74 @@ class AccumulateData {
15854
15871
  }
15855
15872
  return Array.from(merged);
15856
15873
  }
15874
+ /**
15875
+ * Transform the list of pending transfers into:
15876
+ * - map: transfers by service id
15877
+ * - map: gas limit by service id
15878
+ * - set: service ids
15879
+ */
15857
15880
  transformTransfers(transfersToTransform) {
15858
15881
  const transfersByServiceId = new Map();
15859
15882
  const serviceIds = new Set();
15883
+ const gasLimitByServiceId = new Map();
15860
15884
  for (const transfer of transfersToTransform) {
15861
15885
  const serviceId = transfer.destination;
15862
15886
  const transfers = transfersByServiceId.get(serviceId) ?? [];
15887
+ const gas = gasLimitByServiceId.get(serviceId) ?? tryAsServiceGas(0n);
15888
+ const { value, overflow } = sumU64(gas, transfer.gas);
15889
+ gasLimitByServiceId.set(serviceId, tryAsServiceGas(overflow ? math_consts_MAX_VALUE_U64 : value));
15863
15890
  transfers.push(transfer);
15864
15891
  transfersByServiceId.set(serviceId, transfers);
15865
15892
  serviceIds.add(serviceId);
15866
15893
  }
15867
- return { transfersByServiceId, serviceIds };
15894
+ return { transfersByServiceId, serviceIds, gasLimitByServiceId };
15868
15895
  }
15869
- /** Transform the list of auto-accumulate services into a map by service id. */
15896
+ /**
15897
+ * Transform the list of auto accumulate services into:
15898
+ * - map: gas limit by service id
15899
+ * - set: service ids
15900
+ */
15870
15901
  transformAutoAccumulateServices(autoAccumulateServices) {
15871
15902
  const serviceIds = new Set();
15872
- const autoAccumulateServicesByServiceId = new Map();
15903
+ const gasLimitByServiceId = new Map();
15873
15904
  for (const autoAccumulate of autoAccumulateServices) {
15874
- autoAccumulateServicesByServiceId.set(autoAccumulate.service, autoAccumulate);
15905
+ gasLimitByServiceId.set(autoAccumulate.service, autoAccumulate.gasLimit);
15875
15906
  serviceIds.add(autoAccumulate.service);
15876
15907
  }
15877
- return { autoAccumulateServicesByServiceId, serviceIds };
15908
+ return { serviceIds, gasLimitByServiceId };
15878
15909
  }
15879
15910
  /**
15880
15911
  * A function that transform reports into a list of operands and data needed for statistics (gas cost and reports length).
15881
15912
  */
15913
+ /**
15914
+ * Transform the list of reports into:
15915
+ * - map: AccumulateDataItem by service id
15916
+ * - map: gas limit by service id
15917
+ * - set: service ids
15918
+ */
15882
15919
  transformReports(reports) {
15883
15920
  const reportsDataByServiceId = new Map();
15921
+ const gasLimitByServiceId = new Map();
15884
15922
  const serviceIds = new Set();
15885
15923
  for (const report of reports) {
15886
15924
  for (const result of report.results) {
15887
15925
  const serviceId = result.serviceId;
15888
15926
  serviceIds.add(serviceId);
15889
15927
  const item = reportsDataByServiceId.get(serviceId) ?? AccumulateDataItem.empty();
15928
+ const gas = gasLimitByServiceId.get(serviceId) ?? tryAsServiceGas(0n);
15929
+ const { value, overflow } = sumU64(gas, result.gas);
15930
+ const newGas = tryAsServiceGas(overflow ? math_consts_MAX_VALUE_U64 : value);
15931
+ gasLimitByServiceId.set(serviceId, newGas);
15890
15932
  /**
15891
15933
  * We count the report results and gas cost for each service to update service statistics.
15892
15934
  *
15893
- * https://graypaper.fluffylabs.dev/#/68eaa1f/171e04174a04?v=0.6.4
15935
+ * https://graypaper.fluffylabs.dev/#/ab2cdbd/180504182604?v=0.7.2
15894
15936
  */
15895
15937
  item.reportsLength = numbers_tryAsU32(item.reportsLength + 1);
15896
- item.gasCost = tryAsServiceGas(item.gasCost + result.gas);
15897
15938
  /**
15898
15939
  * Transform report into an operand
15899
15940
  *
15900
- * https://graypaper.fluffylabs.dev/#/68eaa1f/17bf02176f03?v=0.6.4
15941
+ * https://graypaper.fluffylabs.dev/#/ab2cdbd/185901181402?v=0.7.2
15901
15942
  */
15902
15943
  item.operands.push(Operand.new({
15903
15944
  gas: result.gas, // g
@@ -15911,19 +15952,7 @@ class AccumulateData {
15911
15952
  reportsDataByServiceId.set(serviceId, item);
15912
15953
  }
15913
15954
  }
15914
- /**
15915
- * Add initial gas cost - it is `U(f_s, 0)` from this formula:
15916
- *
15917
- * https://graypaper.fluffylabs.dev/#/68eaa1f/17b00217b002?v=0.6.4
15918
- */
15919
- for (const serviceId of serviceIds) {
15920
- const item = reportsDataByServiceId.get(serviceId) ?? null;
15921
- const autoAccumulateService = this.autoAccumulateServicesByServiceId.get(serviceId) ?? null;
15922
- if (item !== null && autoAccumulateService !== null) {
15923
- item.gasCost = tryAsServiceGas(item.gasCost + autoAccumulateService.gasLimit);
15924
- }
15925
- }
15926
- return { reportsDataByServiceId, serviceIds };
15955
+ return { reportsDataByServiceId, serviceIds, gasLimitByServiceId };
15927
15956
  }
15928
15957
  /** Returns the list of operands for a given service id */
15929
15958
  getOperands(serviceId) {
@@ -15937,14 +15966,14 @@ class AccumulateData {
15937
15966
  getReportsLength(serviceId) {
15938
15967
  return this.reportsDataByServiceId.get(serviceId)?.reportsLength ?? numbers_tryAsU32(0);
15939
15968
  }
15940
- /** Returns the gas cost for a given service id */
15941
- getGasCost(serviceId) {
15942
- return this.reportsDataByServiceId.get(serviceId)?.gasCost ?? tryAsServiceGas(0n);
15969
+ /** Returns the gas limit for a given service id */
15970
+ getGasLimit(serviceId) {
15971
+ return this.gasLimitByServiceId.get(serviceId) ?? tryAsServiceGas(0n);
15943
15972
  }
15944
15973
  /**
15945
15974
  * Returns a list of service ids that should be accumulated.
15946
15975
  *
15947
- * https://graypaper.fluffylabs.dev/#/68eaa1f/175f01175f01?v=0.6.4
15976
+ * https://graypaper.fluffylabs.dev/#/ab2cdbd/173803174a03?v=0.7.2
15948
15977
  */
15949
15978
  getServiceIds() {
15950
15979
  return this.serviceIds;
@@ -22475,6 +22504,7 @@ class PvmExecutor {
22475
22504
 
22476
22505
 
22477
22506
 
22507
+
22478
22508
  const ACCUMULATION_ERROR = "duplicate service created";
22479
22509
  var PvmInvocationError;
22480
22510
  (function (PvmInvocationError) {
@@ -22675,8 +22705,15 @@ class Accumulate {
22675
22705
  const { gasCost, state: stateAfterParallelAcc, ...rest } = await this.accumulateInParallel(accumulateData, slot, entropy, statistics, stateUpdate);
22676
22706
  const newTransfers = stateAfterParallelAcc.takeTransfers();
22677
22707
  assertEmpty(rest);
22708
+ /**
22709
+ * Gas limit from transfers is added to the next round of accumulation
22710
+ *
22711
+ * https://graypaper.fluffylabs.dev/#/ab2cdbd/172b02172b02?v=0.7.2
22712
+ */
22713
+ const transfersGas = transfers.map((t) => t.gas);
22714
+ const { value: newGasLimit, overflow } = sumU64(tryAsServiceGas(gasLimit - gasCost), ...transfersGas);
22678
22715
  // NOTE [ToDr] recursive invocation
22679
- const { accumulatedReports, gasCost: seqGasCost, state, ...seqRest } = await this.accumulateSequentially(tryAsServiceGas(gasLimit - gasCost), reportsToAccumulateSequentially, newTransfers, slot, entropy, statistics, stateAfterParallelAcc, []);
22716
+ const { accumulatedReports, gasCost: seqGasCost, state, ...seqRest } = await this.accumulateSequentially(tryAsServiceGas(overflow ? math_consts_MAX_VALUE_U64 : newGasLimit), reportsToAccumulateSequentially, newTransfers, slot, entropy, statistics, stateAfterParallelAcc, []);
22680
22717
  assertEmpty(seqRest);
22681
22718
  return {
22682
22719
  accumulatedReports: numbers_tryAsU32(i + accumulatedReports),
@@ -22702,7 +22739,7 @@ class Accumulate {
22702
22739
  for (const serviceId of serviceIds) {
22703
22740
  const checkpoint = AccumulationStateUpdate.copyFrom(currentState);
22704
22741
  const operands = accumulateData.getOperands(serviceId);
22705
- const { consumedGas, stateUpdate } = await this.accumulateSingleService(serviceId, accumulateData.getTransfers(serviceId), operands, accumulateData.getGasCost(serviceId), slot, entropy, currentState);
22742
+ const { consumedGas, stateUpdate } = await this.accumulateSingleService(serviceId, accumulateData.getTransfers(serviceId), operands, accumulateData.getGasLimit(serviceId), slot, entropy, currentState);
22706
22743
  gasCost = tryAsServiceGas(gasCost + consumedGas);
22707
22744
  // https://graypaper.fluffylabs.dev/#/ab2cdbd/193b05193b05?v=0.7.2
22708
22745
  const serviceStatistics = statistics.get(serviceId) ?? { count: numbers_tryAsU32(0), gasUsed: tryAsServiceGas(0) };
@@ -22722,7 +22759,7 @@ class Accumulate {
22722
22759
  // Since serviceIds already contains newV, this service gets accumulated twice.
22723
22760
  // To avoid double-counting, we skip stats and gas cost tracking here.
22724
22761
  // We need this accumulation to get the correct `delegator`
22725
- const { stateUpdate } = await this.accumulateSingleService(newV, [], accumulateData.getOperands(newV), accumulateData.getGasCost(newV), slot, entropy, checkpoint);
22762
+ const { stateUpdate } = await this.accumulateSingleService(newV, [], accumulateData.getOperands(newV), accumulateData.getGasLimit(newV), slot, entropy, checkpoint);
22726
22763
  const correctV = stateUpdate?.privilegedServices?.delegator ?? this.state.privilegedServices.delegator;
22727
22764
  currentState.privilegedServices = PrivilegedServices.create({
22728
22765
  ...currentState.privilegedServices,
@@ -24260,7 +24297,6 @@ class Statistics {
24260
24297
 
24261
24298
 
24262
24299
 
24263
-
24264
24300
  class DbHeaderChain {
24265
24301
  blocks;
24266
24302
  constructor(blocks) {
@@ -24331,7 +24367,7 @@ class OnChain {
24331
24367
  // chapter 13: https://graypaper.fluffylabs.dev/#/68eaa1f/18b60118b601?v=0.6.4
24332
24368
  statistics;
24333
24369
  isReadyForNextEpoch = Promise.resolve(false);
24334
- constructor(chainSpec, state, blocks, hasher, pvm = PvmBackend.BuiltIn) {
24370
+ constructor(chainSpec, state, blocks, hasher, pvm) {
24335
24371
  this.chainSpec = chainSpec;
24336
24372
  this.state = state;
24337
24373
  this.hasher = hasher;