@typeberry/jam 0.2.0-74f246e → 0.2.0-8017bfd

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.
@@ -9044,6 +9044,7 @@ function accumulationOutputComparator(a, b) {
9044
9044
 
9045
9045
  ;// CONCATENATED MODULE: ./packages/jam/block/gp-constants.ts
9046
9046
 
9047
+
9047
9048
  /**
9048
9049
  * This file lists all of the constants defined in the GrayPaper appendix.
9049
9050
  *
@@ -9054,7 +9055,7 @@ function accumulationOutputComparator(a, b) {
9054
9055
  * here are only temporarily for convenience. When we figure out better names
9055
9056
  * and places for these this file will be eradicated.
9056
9057
  *
9057
- * https://graypaper.fluffylabs.dev/#/579bd12/413000413000
9058
+ * https://graypaper.fluffylabs.dev/#/ab2cdbd/442300442300?v=0.7.2
9058
9059
  */
9059
9060
  /** `G_I`: The gas allocated to invoke a work-package’s Is-Authorized logic. */
9060
9061
  const G_I = 50_000_000;
@@ -9070,8 +9071,8 @@ const S = 1024;
9070
9071
  const T = 128;
9071
9072
  /** `W_A`: The maximum size of is-authorized code in octets. */
9072
9073
  const W_A = 64_000;
9073
- /** `W_B`: The maximum size of an encoded work-package with extrinsic data and imports. */
9074
- const W_B = 13_794_305;
9074
+ /** `W_B`: The maximum size of the concatenated variable-size blobs, extrinsics and imported segments of a work-package, in octets */
9075
+ const W_B = Compatibility.isGreaterOrEqual(GpVersion.V0_7_2) ? 13_791_360 : 13_794_305;
9075
9076
  /** `W_C`: The maximum size of service code in octets. */
9076
9077
  const W_C = 4_000_000;
9077
9078
  /** `W_M`: The maximum number of imports in a work-package. */
@@ -10170,31 +10171,29 @@ var UpdatePreimageKind;
10170
10171
  * 3. Update `LookupHistory` with given value.
10171
10172
  */
10172
10173
  class state_update_UpdatePreimage {
10173
- serviceId;
10174
10174
  action;
10175
- constructor(serviceId, action) {
10176
- this.serviceId = serviceId;
10175
+ constructor(action) {
10177
10176
  this.action = action;
10178
10177
  }
10179
10178
  /** A preimage is provided. We should update the lookuphistory and add the preimage to db. */
10180
- static provide({ serviceId, preimage, slot, }) {
10181
- return new state_update_UpdatePreimage(serviceId, {
10179
+ static provide({ preimage, slot }) {
10180
+ return new state_update_UpdatePreimage({
10182
10181
  kind: UpdatePreimageKind.Provide,
10183
10182
  preimage,
10184
10183
  slot,
10185
10184
  });
10186
10185
  }
10187
10186
  /** The preimage should be removed completely from the database. */
10188
- static remove({ serviceId, hash, length }) {
10189
- return new state_update_UpdatePreimage(serviceId, {
10187
+ static remove({ hash, length }) {
10188
+ return new state_update_UpdatePreimage({
10190
10189
  kind: UpdatePreimageKind.Remove,
10191
10190
  hash,
10192
10191
  length,
10193
10192
  });
10194
10193
  }
10195
10194
  /** Update the lookup history of some preimage or add a new one (request). */
10196
- static updateOrAdd({ serviceId, lookupHistory }) {
10197
- return new state_update_UpdatePreimage(serviceId, {
10195
+ static updateOrAdd({ lookupHistory }) {
10196
+ return new state_update_UpdatePreimage({
10198
10197
  kind: UpdatePreimageKind.UpdateOrAdd,
10199
10198
  item: lookupHistory,
10200
10199
  });
@@ -10231,23 +10230,21 @@ var UpdateServiceKind;
10231
10230
  UpdateServiceKind[UpdateServiceKind["Create"] = 1] = "Create";
10232
10231
  })(UpdateServiceKind || (UpdateServiceKind = {}));
10233
10232
  /**
10234
- * Update service info of a particular `ServiceId` or create a new one.
10233
+ * Update service info or create a new one.
10235
10234
  */
10236
10235
  class UpdateService {
10237
- serviceId;
10238
10236
  action;
10239
- constructor(serviceId, action) {
10240
- this.serviceId = serviceId;
10237
+ constructor(action) {
10241
10238
  this.action = action;
10242
10239
  }
10243
- static update({ serviceId, serviceInfo }) {
10244
- return new UpdateService(serviceId, {
10240
+ static update({ serviceInfo }) {
10241
+ return new UpdateService({
10245
10242
  kind: UpdateServiceKind.Update,
10246
10243
  account: serviceInfo,
10247
10244
  });
10248
10245
  }
10249
- static create({ serviceId, serviceInfo, lookupHistory, }) {
10250
- return new UpdateService(serviceId, {
10246
+ static create({ serviceInfo, lookupHistory, }) {
10247
+ return new UpdateService({
10251
10248
  kind: UpdateServiceKind.Create,
10252
10249
  account: serviceInfo,
10253
10250
  lookupHistory,
@@ -10268,17 +10265,15 @@ var UpdateStorageKind;
10268
10265
  * Can either create/modify an entry or remove it.
10269
10266
  */
10270
10267
  class UpdateStorage {
10271
- serviceId;
10272
10268
  action;
10273
- constructor(serviceId, action) {
10274
- this.serviceId = serviceId;
10269
+ constructor(action) {
10275
10270
  this.action = action;
10276
10271
  }
10277
- static set({ serviceId, storage }) {
10278
- return new UpdateStorage(serviceId, { kind: UpdateStorageKind.Set, storage });
10272
+ static set({ storage }) {
10273
+ return new UpdateStorage({ kind: UpdateStorageKind.Set, storage });
10279
10274
  }
10280
- static remove({ serviceId, key }) {
10281
- return new UpdateStorage(serviceId, { kind: UpdateStorageKind.Remove, key });
10275
+ static remove({ key }) {
10276
+ return new UpdateStorage({ kind: UpdateStorageKind.Remove, key });
10282
10277
  }
10283
10278
  get key() {
10284
10279
  if (this.action.kind === UpdateStorageKind.Remove) {
@@ -10487,12 +10482,12 @@ class in_memory_state_InMemoryState extends WithDebug {
10487
10482
  * Modify the state and apply a single state update.
10488
10483
  */
10489
10484
  applyUpdate(update) {
10490
- const { servicesRemoved, servicesUpdates, preimages, storage, ...rest } = update;
10485
+ const { removed, created: _, updated, preimages, storage, ...rest } = update;
10491
10486
  // just assign all other variables
10492
10487
  Object.assign(this, rest);
10493
10488
  // and update the services state
10494
10489
  let result;
10495
- result = this.updateServices(servicesUpdates);
10490
+ result = this.updateServices(updated);
10496
10491
  if (result.isError) {
10497
10492
  return result;
10498
10493
  }
@@ -10504,7 +10499,7 @@ class in_memory_state_InMemoryState extends WithDebug {
10504
10499
  if (result.isError) {
10505
10500
  return result;
10506
10501
  }
10507
- this.removeServices(servicesRemoved);
10502
+ this.removeServices(removed);
10508
10503
  return result_Result.ok(result_OK);
10509
10504
  }
10510
10505
  removeServices(servicesRemoved) {
@@ -10513,87 +10508,100 @@ class in_memory_state_InMemoryState extends WithDebug {
10513
10508
  this.services.delete(serviceId);
10514
10509
  }
10515
10510
  }
10516
- updateStorage(storage) {
10517
- for (const { serviceId, action } of storage ?? []) {
10518
- const { kind } = action;
10519
- const service = this.services.get(serviceId);
10520
- if (service === undefined) {
10521
- return result_Result.error(in_memory_state_UpdateError.NoService, `Attempting to update storage of non-existing service: ${serviceId}`);
10522
- }
10523
- if (kind === UpdateStorageKind.Set) {
10524
- const { key, value } = action.storage;
10525
- service.data.storage.set(key.toString(), StorageItem.create({ key, value }));
10526
- }
10527
- else if (kind === UpdateStorageKind.Remove) {
10528
- const { key } = action;
10529
- debug_check `
10511
+ updateStorage(storageUpdates) {
10512
+ if (storageUpdates === undefined) {
10513
+ return result_Result.ok(result_OK);
10514
+ }
10515
+ for (const [serviceId, updates] of storageUpdates.entries()) {
10516
+ for (const update of updates) {
10517
+ const { kind } = update.action;
10518
+ const service = this.services.get(serviceId);
10519
+ if (service === undefined) {
10520
+ return result_Result.error(in_memory_state_UpdateError.NoService, `Attempting to update storage of non-existing service: ${serviceId}`);
10521
+ }
10522
+ if (kind === UpdateStorageKind.Set) {
10523
+ const { key, value } = update.action.storage;
10524
+ service.data.storage.set(key.toString(), StorageItem.create({ key, value }));
10525
+ }
10526
+ else if (kind === UpdateStorageKind.Remove) {
10527
+ const { key } = update.action;
10528
+ debug_check `
10530
10529
  ${service.data.storage.has(key.toString())}
10531
- Attempting to remove non-existing storage item at ${serviceId}: ${action.key}
10530
+ Attempting to remove non-existing storage item at ${serviceId}: ${update.action.key}
10532
10531
  `;
10533
- service.data.storage.delete(key.toString());
10534
- }
10535
- else {
10536
- debug_assertNever(kind);
10532
+ service.data.storage.delete(key.toString());
10533
+ }
10534
+ else {
10535
+ debug_assertNever(kind);
10536
+ }
10537
10537
  }
10538
10538
  }
10539
10539
  return result_Result.ok(result_OK);
10540
10540
  }
10541
- updatePreimages(preimages) {
10542
- for (const { serviceId, action } of preimages ?? []) {
10541
+ updatePreimages(preimagesUpdates) {
10542
+ if (preimagesUpdates === undefined) {
10543
+ return result_Result.ok(result_OK);
10544
+ }
10545
+ for (const [serviceId, updates] of preimagesUpdates.entries()) {
10543
10546
  const service = this.services.get(serviceId);
10544
10547
  if (service === undefined) {
10545
10548
  return result_Result.error(in_memory_state_UpdateError.NoService, `Attempting to update preimage of non-existing service: ${serviceId}`);
10546
10549
  }
10547
- const { kind } = action;
10548
- if (kind === UpdatePreimageKind.Provide) {
10549
- const { preimage, slot } = action;
10550
- if (service.data.preimages.has(preimage.hash)) {
10551
- return result_Result.error(in_memory_state_UpdateError.PreimageExists, `Overwriting existing preimage at ${serviceId}: ${preimage}`);
10552
- }
10553
- service.data.preimages.set(preimage.hash, preimage);
10554
- if (slot !== null) {
10555
- const lookupHistory = service.data.lookupHistory.get(preimage.hash);
10556
- const length = numbers_tryAsU32(preimage.blob.length);
10557
- const lookup = new service_LookupHistoryItem(preimage.hash, length, tryAsLookupHistorySlots([slot]));
10558
- if (lookupHistory === undefined) {
10559
- // no lookup history for that preimage at all (edge case, should be requested)
10560
- service.data.lookupHistory.set(preimage.hash, [lookup]);
10550
+ for (const update of updates) {
10551
+ const { kind } = update.action;
10552
+ if (kind === UpdatePreimageKind.Provide) {
10553
+ const { preimage, slot } = update.action;
10554
+ if (service.data.preimages.has(preimage.hash)) {
10555
+ return result_Result.error(in_memory_state_UpdateError.PreimageExists, `Overwriting existing preimage at ${serviceId}: ${preimage}`);
10561
10556
  }
10562
- else {
10563
- // insert or replace exiting entry
10564
- const index = lookupHistory.map((x) => x.length).indexOf(length);
10565
- lookupHistory.splice(index, index === -1 ? 0 : 1, lookup);
10557
+ service.data.preimages.set(preimage.hash, preimage);
10558
+ if (slot !== null) {
10559
+ const lookupHistory = service.data.lookupHistory.get(preimage.hash);
10560
+ const length = numbers_tryAsU32(preimage.blob.length);
10561
+ const lookup = new service_LookupHistoryItem(preimage.hash, length, tryAsLookupHistorySlots([slot]));
10562
+ if (lookupHistory === undefined) {
10563
+ // no lookup history for that preimage at all (edge case, should be requested)
10564
+ service.data.lookupHistory.set(preimage.hash, [lookup]);
10565
+ }
10566
+ else {
10567
+ // insert or replace exiting entry
10568
+ const index = lookupHistory.map((x) => x.length).indexOf(length);
10569
+ lookupHistory.splice(index, index === -1 ? 0 : 1, lookup);
10570
+ }
10566
10571
  }
10567
10572
  }
10568
- }
10569
- else if (kind === UpdatePreimageKind.Remove) {
10570
- const { hash, length } = action;
10571
- service.data.preimages.delete(hash);
10572
- const history = service.data.lookupHistory.get(hash) ?? [];
10573
- const idx = history.map((x) => x.length).indexOf(length);
10574
- if (idx !== -1) {
10575
- history.splice(idx, 1);
10573
+ else if (kind === UpdatePreimageKind.Remove) {
10574
+ const { hash, length } = update.action;
10575
+ service.data.preimages.delete(hash);
10576
+ const history = service.data.lookupHistory.get(hash) ?? [];
10577
+ const idx = history.map((x) => x.length).indexOf(length);
10578
+ if (idx !== -1) {
10579
+ history.splice(idx, 1);
10580
+ }
10581
+ }
10582
+ else if (kind === UpdatePreimageKind.UpdateOrAdd) {
10583
+ const { item } = update.action;
10584
+ const history = service.data.lookupHistory.get(item.hash) ?? [];
10585
+ const existingIdx = history.map((x) => x.length).indexOf(item.length);
10586
+ const removeCount = existingIdx === -1 ? 0 : 1;
10587
+ history.splice(existingIdx, removeCount, item);
10588
+ service.data.lookupHistory.set(item.hash, history);
10589
+ }
10590
+ else {
10591
+ debug_assertNever(kind);
10576
10592
  }
10577
- }
10578
- else if (kind === UpdatePreimageKind.UpdateOrAdd) {
10579
- const { item } = action;
10580
- const history = service.data.lookupHistory.get(item.hash) ?? [];
10581
- const existingIdx = history.map((x) => x.length).indexOf(item.length);
10582
- const removeCount = existingIdx === -1 ? 0 : 1;
10583
- history.splice(existingIdx, removeCount, item);
10584
- service.data.lookupHistory.set(item.hash, history);
10585
- }
10586
- else {
10587
- debug_assertNever(kind);
10588
10593
  }
10589
10594
  }
10590
10595
  return result_Result.ok(result_OK);
10591
10596
  }
10592
10597
  updateServices(servicesUpdates) {
10593
- for (const { serviceId, action } of servicesUpdates ?? []) {
10594
- const { kind, account } = action;
10598
+ if (servicesUpdates === undefined) {
10599
+ return result_Result.ok(result_OK);
10600
+ }
10601
+ for (const [serviceId, update] of servicesUpdates.entries()) {
10602
+ const { kind, account } = update.action;
10595
10603
  if (kind === UpdateServiceKind.Create) {
10596
- const { lookupHistory } = action;
10604
+ const { lookupHistory } = update.action;
10597
10605
  if (this.services.has(serviceId)) {
10598
10606
  return result_Result.error(in_memory_state_UpdateError.DuplicateService, `${serviceId} already exists!`);
10599
10607
  }
@@ -11854,7 +11862,6 @@ function getKeccakTrieHasher(hasher) {
11854
11862
 
11855
11863
 
11856
11864
 
11857
-
11858
11865
  /** What should be done with that key? */
11859
11866
  var StateEntryUpdateAction;
11860
11867
  (function (StateEntryUpdateAction) {
@@ -11870,76 +11877,88 @@ function* serializeStateUpdate(spec, blake2b, update) {
11870
11877
  yield* serializeBasicKeys(spec, update);
11871
11878
  const encode = (codec, val) => encoder_Encoder.encodeObject(codec, val, spec);
11872
11879
  // then let's proceed with service updates
11873
- yield* serializeServiceUpdates(update.servicesUpdates, encode, blake2b);
11880
+ yield* serializeServiceUpdates(update.updated, encode, blake2b);
11874
11881
  yield* serializePreimages(update.preimages, encode, blake2b);
11875
11882
  yield* serializeStorage(update.storage, blake2b);
11876
- yield* serializeRemovedServices(update.servicesRemoved);
11883
+ yield* serializeRemovedServices(update.removed);
11877
11884
  }
11878
11885
  function* serializeRemovedServices(servicesRemoved) {
11879
- for (const serviceId of servicesRemoved ?? []) {
11886
+ if (servicesRemoved === undefined) {
11887
+ return;
11888
+ }
11889
+ for (const serviceId of servicesRemoved) {
11880
11890
  // TODO [ToDr] what about all data associated with a service?
11881
11891
  const codec = serialize_serialize.serviceData(serviceId);
11882
11892
  yield [StateEntryUpdateAction.Remove, codec.key, EMPTY_BLOB];
11883
11893
  }
11884
11894
  }
11885
- function* serializeStorage(storage, blake2b) {
11886
- for (const { action, serviceId } of storage ?? []) {
11887
- switch (action.kind) {
11888
- case UpdateStorageKind.Set: {
11889
- const key = action.storage.key;
11890
- const codec = serialize_serialize.serviceStorage(blake2b, serviceId, key);
11891
- yield [StateEntryUpdateAction.Insert, codec.key, action.storage.value];
11892
- break;
11893
- }
11894
- case UpdateStorageKind.Remove: {
11895
- const key = action.key;
11896
- const codec = serialize_serialize.serviceStorage(blake2b, serviceId, key);
11897
- yield [StateEntryUpdateAction.Remove, codec.key, EMPTY_BLOB];
11898
- break;
11895
+ function* serializeStorage(storageUpdates, blake2b) {
11896
+ if (storageUpdates === undefined) {
11897
+ return;
11898
+ }
11899
+ for (const [serviceId, updates] of storageUpdates.entries()) {
11900
+ for (const { action } of updates) {
11901
+ switch (action.kind) {
11902
+ case UpdateStorageKind.Set: {
11903
+ const key = action.storage.key;
11904
+ const codec = serialize_serialize.serviceStorage(blake2b, serviceId, key);
11905
+ yield [StateEntryUpdateAction.Insert, codec.key, action.storage.value];
11906
+ break;
11907
+ }
11908
+ case UpdateStorageKind.Remove: {
11909
+ const key = action.key;
11910
+ const codec = serialize_serialize.serviceStorage(blake2b, serviceId, key);
11911
+ yield [StateEntryUpdateAction.Remove, codec.key, EMPTY_BLOB];
11912
+ break;
11913
+ }
11899
11914
  }
11900
- default:
11901
- debug_assertNever(action);
11902
11915
  }
11903
11916
  }
11904
11917
  }
11905
- function* serializePreimages(preimages, encode, blake2b) {
11906
- for (const { action, serviceId } of preimages ?? []) {
11907
- switch (action.kind) {
11908
- case UpdatePreimageKind.Provide: {
11909
- const { hash, blob } = action.preimage;
11910
- const codec = serialize_serialize.servicePreimages(blake2b, serviceId, hash);
11911
- yield [StateEntryUpdateAction.Insert, codec.key, blob];
11912
- if (action.slot !== null) {
11913
- const codec2 = serialize_serialize.serviceLookupHistory(blake2b, serviceId, hash, numbers_tryAsU32(blob.length));
11914
- yield [
11915
- StateEntryUpdateAction.Insert,
11916
- codec2.key,
11917
- encode(codec2.Codec, tryAsLookupHistorySlots([action.slot])),
11918
- ];
11918
+ function* serializePreimages(preimagesUpdates, encode, blake2b) {
11919
+ if (preimagesUpdates === undefined) {
11920
+ return;
11921
+ }
11922
+ for (const [serviceId, updates] of preimagesUpdates.entries()) {
11923
+ for (const { action } of updates) {
11924
+ switch (action.kind) {
11925
+ case UpdatePreimageKind.Provide: {
11926
+ const { hash, blob } = action.preimage;
11927
+ const codec = serialize_serialize.servicePreimages(blake2b, serviceId, hash);
11928
+ yield [StateEntryUpdateAction.Insert, codec.key, blob];
11929
+ if (action.slot !== null) {
11930
+ const codec2 = serialize_serialize.serviceLookupHistory(blake2b, serviceId, hash, numbers_tryAsU32(blob.length));
11931
+ yield [
11932
+ StateEntryUpdateAction.Insert,
11933
+ codec2.key,
11934
+ encode(codec2.Codec, tryAsLookupHistorySlots([action.slot])),
11935
+ ];
11936
+ }
11937
+ break;
11938
+ }
11939
+ case UpdatePreimageKind.UpdateOrAdd: {
11940
+ const { hash, length, slots } = action.item;
11941
+ const codec = serialize_serialize.serviceLookupHistory(blake2b, serviceId, hash, length);
11942
+ yield [StateEntryUpdateAction.Insert, codec.key, encode(codec.Codec, slots)];
11943
+ break;
11944
+ }
11945
+ case UpdatePreimageKind.Remove: {
11946
+ const { hash, length } = action;
11947
+ const codec = serialize_serialize.servicePreimages(blake2b, serviceId, hash);
11948
+ yield [StateEntryUpdateAction.Remove, codec.key, EMPTY_BLOB];
11949
+ const codec2 = serialize_serialize.serviceLookupHistory(blake2b, serviceId, hash, length);
11950
+ yield [StateEntryUpdateAction.Remove, codec2.key, EMPTY_BLOB];
11951
+ break;
11919
11952
  }
11920
- break;
11921
- }
11922
- case UpdatePreimageKind.UpdateOrAdd: {
11923
- const { hash, length, slots } = action.item;
11924
- const codec = serialize_serialize.serviceLookupHistory(blake2b, serviceId, hash, length);
11925
- yield [StateEntryUpdateAction.Insert, codec.key, encode(codec.Codec, slots)];
11926
- break;
11927
- }
11928
- case UpdatePreimageKind.Remove: {
11929
- const { hash, length } = action;
11930
- const codec = serialize_serialize.servicePreimages(blake2b, serviceId, hash);
11931
- yield [StateEntryUpdateAction.Remove, codec.key, EMPTY_BLOB];
11932
- const codec2 = serialize_serialize.serviceLookupHistory(blake2b, serviceId, hash, length);
11933
- yield [StateEntryUpdateAction.Remove, codec2.key, EMPTY_BLOB];
11934
- break;
11935
11953
  }
11936
- default:
11937
- debug_assertNever(action);
11938
11954
  }
11939
11955
  }
11940
11956
  }
11941
11957
  function* serializeServiceUpdates(servicesUpdates, encode, blake2b) {
11942
- for (const { action, serviceId } of servicesUpdates ?? []) {
11958
+ if (servicesUpdates === undefined) {
11959
+ return;
11960
+ }
11961
+ for (const [serviceId, { action }] of servicesUpdates.entries()) {
11943
11962
  // new service being created or updated
11944
11963
  const codec = serialize_serialize.serviceData(serviceId);
11945
11964
  yield [StateEntryUpdateAction.Insert, codec.key, encode(codec.Codec, action.account)];
@@ -13754,7 +13773,7 @@ class Preimages {
13754
13773
  }
13755
13774
  }
13756
13775
  const { preimages, slot } = input;
13757
- const pendingChanges = [];
13776
+ const pendingChanges = new Map();
13758
13777
  // select preimages for integration
13759
13778
  for (const preimage of preimages) {
13760
13779
  const { requester, blob } = preimage;
@@ -13771,11 +13790,12 @@ class Preimages {
13771
13790
  return Result.error(PreimagesErrorCode.PreimageUnneeded);
13772
13791
  }
13773
13792
  // https://graypaper.fluffylabs.dev/#/5f542d7/18c00018f300
13774
- pendingChanges.push(UpdatePreimage.provide({
13775
- serviceId: requester,
13793
+ const updates = pendingChanges.get(requester) ?? [];
13794
+ updates.push(UpdatePreimage.provide({
13776
13795
  preimage: PreimageItem.create({ hash, blob }),
13777
13796
  slot,
13778
13797
  }));
13798
+ pendingChanges.set(requester, updates);
13779
13799
  }
13780
13800
  return Result.ok({
13781
13801
  preimages: pendingChanges,