@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.
@@ -9470,6 +9470,7 @@ function accumulationOutputComparator(a, b) {
9470
9470
 
9471
9471
  ;// CONCATENATED MODULE: ./packages/jam/block/gp-constants.ts
9472
9472
 
9473
+
9473
9474
  /**
9474
9475
  * This file lists all of the constants defined in the GrayPaper appendix.
9475
9476
  *
@@ -9480,7 +9481,7 @@ function accumulationOutputComparator(a, b) {
9480
9481
  * here are only temporarily for convenience. When we figure out better names
9481
9482
  * and places for these this file will be eradicated.
9482
9483
  *
9483
- * https://graypaper.fluffylabs.dev/#/579bd12/413000413000
9484
+ * https://graypaper.fluffylabs.dev/#/ab2cdbd/442300442300?v=0.7.2
9484
9485
  */
9485
9486
  /** `G_I`: The gas allocated to invoke a work-package’s Is-Authorized logic. */
9486
9487
  const G_I = 50_000_000;
@@ -9496,8 +9497,8 @@ const S = 1024;
9496
9497
  const T = 128;
9497
9498
  /** `W_A`: The maximum size of is-authorized code in octets. */
9498
9499
  const W_A = 64_000;
9499
- /** `W_B`: The maximum size of an encoded work-package with extrinsic data and imports. */
9500
- const W_B = 13_794_305;
9500
+ /** `W_B`: The maximum size of the concatenated variable-size blobs, extrinsics and imported segments of a work-package, in octets */
9501
+ const W_B = Compatibility.isGreaterOrEqual(GpVersion.V0_7_2) ? 13_791_360 : 13_794_305;
9501
9502
  /** `W_C`: The maximum size of service code in octets. */
9502
9503
  const W_C = 4_000_000;
9503
9504
  /** `W_M`: The maximum number of imports in a work-package. */
@@ -10596,31 +10597,29 @@ var UpdatePreimageKind;
10596
10597
  * 3. Update `LookupHistory` with given value.
10597
10598
  */
10598
10599
  class UpdatePreimage {
10599
- serviceId;
10600
10600
  action;
10601
- constructor(serviceId, action) {
10602
- this.serviceId = serviceId;
10601
+ constructor(action) {
10603
10602
  this.action = action;
10604
10603
  }
10605
10604
  /** A preimage is provided. We should update the lookuphistory and add the preimage to db. */
10606
- static provide({ serviceId, preimage, slot, }) {
10607
- return new UpdatePreimage(serviceId, {
10605
+ static provide({ preimage, slot }) {
10606
+ return new UpdatePreimage({
10608
10607
  kind: UpdatePreimageKind.Provide,
10609
10608
  preimage,
10610
10609
  slot,
10611
10610
  });
10612
10611
  }
10613
10612
  /** The preimage should be removed completely from the database. */
10614
- static remove({ serviceId, hash, length }) {
10615
- return new UpdatePreimage(serviceId, {
10613
+ static remove({ hash, length }) {
10614
+ return new UpdatePreimage({
10616
10615
  kind: UpdatePreimageKind.Remove,
10617
10616
  hash,
10618
10617
  length,
10619
10618
  });
10620
10619
  }
10621
10620
  /** Update the lookup history of some preimage or add a new one (request). */
10622
- static updateOrAdd({ serviceId, lookupHistory }) {
10623
- return new UpdatePreimage(serviceId, {
10621
+ static updateOrAdd({ lookupHistory }) {
10622
+ return new UpdatePreimage({
10624
10623
  kind: UpdatePreimageKind.UpdateOrAdd,
10625
10624
  item: lookupHistory,
10626
10625
  });
@@ -10657,23 +10656,21 @@ var UpdateServiceKind;
10657
10656
  UpdateServiceKind[UpdateServiceKind["Create"] = 1] = "Create";
10658
10657
  })(UpdateServiceKind || (UpdateServiceKind = {}));
10659
10658
  /**
10660
- * Update service info of a particular `ServiceId` or create a new one.
10659
+ * Update service info or create a new one.
10661
10660
  */
10662
10661
  class UpdateService {
10663
- serviceId;
10664
10662
  action;
10665
- constructor(serviceId, action) {
10666
- this.serviceId = serviceId;
10663
+ constructor(action) {
10667
10664
  this.action = action;
10668
10665
  }
10669
- static update({ serviceId, serviceInfo }) {
10670
- return new UpdateService(serviceId, {
10666
+ static update({ serviceInfo }) {
10667
+ return new UpdateService({
10671
10668
  kind: UpdateServiceKind.Update,
10672
10669
  account: serviceInfo,
10673
10670
  });
10674
10671
  }
10675
- static create({ serviceId, serviceInfo, lookupHistory, }) {
10676
- return new UpdateService(serviceId, {
10672
+ static create({ serviceInfo, lookupHistory, }) {
10673
+ return new UpdateService({
10677
10674
  kind: UpdateServiceKind.Create,
10678
10675
  account: serviceInfo,
10679
10676
  lookupHistory,
@@ -10694,17 +10691,15 @@ var UpdateStorageKind;
10694
10691
  * Can either create/modify an entry or remove it.
10695
10692
  */
10696
10693
  class UpdateStorage {
10697
- serviceId;
10698
10694
  action;
10699
- constructor(serviceId, action) {
10700
- this.serviceId = serviceId;
10695
+ constructor(action) {
10701
10696
  this.action = action;
10702
10697
  }
10703
- static set({ serviceId, storage }) {
10704
- return new UpdateStorage(serviceId, { kind: UpdateStorageKind.Set, storage });
10698
+ static set({ storage }) {
10699
+ return new UpdateStorage({ kind: UpdateStorageKind.Set, storage });
10705
10700
  }
10706
- static remove({ serviceId, key }) {
10707
- return new UpdateStorage(serviceId, { kind: UpdateStorageKind.Remove, key });
10701
+ static remove({ key }) {
10702
+ return new UpdateStorage({ kind: UpdateStorageKind.Remove, key });
10708
10703
  }
10709
10704
  get key() {
10710
10705
  if (this.action.kind === UpdateStorageKind.Remove) {
@@ -10913,12 +10908,12 @@ class in_memory_state_InMemoryState extends WithDebug {
10913
10908
  * Modify the state and apply a single state update.
10914
10909
  */
10915
10910
  applyUpdate(update) {
10916
- const { servicesRemoved, servicesUpdates, preimages, storage, ...rest } = update;
10911
+ const { removed, created: _, updated, preimages, storage, ...rest } = update;
10917
10912
  // just assign all other variables
10918
10913
  Object.assign(this, rest);
10919
10914
  // and update the services state
10920
10915
  let result;
10921
- result = this.updateServices(servicesUpdates);
10916
+ result = this.updateServices(updated);
10922
10917
  if (result.isError) {
10923
10918
  return result;
10924
10919
  }
@@ -10930,7 +10925,7 @@ class in_memory_state_InMemoryState extends WithDebug {
10930
10925
  if (result.isError) {
10931
10926
  return result;
10932
10927
  }
10933
- this.removeServices(servicesRemoved);
10928
+ this.removeServices(removed);
10934
10929
  return result_Result.ok(result_OK);
10935
10930
  }
10936
10931
  removeServices(servicesRemoved) {
@@ -10939,87 +10934,100 @@ class in_memory_state_InMemoryState extends WithDebug {
10939
10934
  this.services.delete(serviceId);
10940
10935
  }
10941
10936
  }
10942
- updateStorage(storage) {
10943
- for (const { serviceId, action } of storage ?? []) {
10944
- const { kind } = action;
10945
- const service = this.services.get(serviceId);
10946
- if (service === undefined) {
10947
- return result_Result.error(in_memory_state_UpdateError.NoService, `Attempting to update storage of non-existing service: ${serviceId}`);
10948
- }
10949
- if (kind === UpdateStorageKind.Set) {
10950
- const { key, value } = action.storage;
10951
- service.data.storage.set(key.toString(), StorageItem.create({ key, value }));
10952
- }
10953
- else if (kind === UpdateStorageKind.Remove) {
10954
- const { key } = action;
10955
- debug_check `
10937
+ updateStorage(storageUpdates) {
10938
+ if (storageUpdates === undefined) {
10939
+ return result_Result.ok(result_OK);
10940
+ }
10941
+ for (const [serviceId, updates] of storageUpdates.entries()) {
10942
+ for (const update of updates) {
10943
+ const { kind } = update.action;
10944
+ const service = this.services.get(serviceId);
10945
+ if (service === undefined) {
10946
+ return result_Result.error(in_memory_state_UpdateError.NoService, `Attempting to update storage of non-existing service: ${serviceId}`);
10947
+ }
10948
+ if (kind === UpdateStorageKind.Set) {
10949
+ const { key, value } = update.action.storage;
10950
+ service.data.storage.set(key.toString(), StorageItem.create({ key, value }));
10951
+ }
10952
+ else if (kind === UpdateStorageKind.Remove) {
10953
+ const { key } = update.action;
10954
+ debug_check `
10956
10955
  ${service.data.storage.has(key.toString())}
10957
- Attempting to remove non-existing storage item at ${serviceId}: ${action.key}
10956
+ Attempting to remove non-existing storage item at ${serviceId}: ${update.action.key}
10958
10957
  `;
10959
- service.data.storage.delete(key.toString());
10960
- }
10961
- else {
10962
- debug_assertNever(kind);
10958
+ service.data.storage.delete(key.toString());
10959
+ }
10960
+ else {
10961
+ debug_assertNever(kind);
10962
+ }
10963
10963
  }
10964
10964
  }
10965
10965
  return result_Result.ok(result_OK);
10966
10966
  }
10967
- updatePreimages(preimages) {
10968
- for (const { serviceId, action } of preimages ?? []) {
10967
+ updatePreimages(preimagesUpdates) {
10968
+ if (preimagesUpdates === undefined) {
10969
+ return result_Result.ok(result_OK);
10970
+ }
10971
+ for (const [serviceId, updates] of preimagesUpdates.entries()) {
10969
10972
  const service = this.services.get(serviceId);
10970
10973
  if (service === undefined) {
10971
10974
  return result_Result.error(in_memory_state_UpdateError.NoService, `Attempting to update preimage of non-existing service: ${serviceId}`);
10972
10975
  }
10973
- const { kind } = action;
10974
- if (kind === UpdatePreimageKind.Provide) {
10975
- const { preimage, slot } = action;
10976
- if (service.data.preimages.has(preimage.hash)) {
10977
- return result_Result.error(in_memory_state_UpdateError.PreimageExists, `Overwriting existing preimage at ${serviceId}: ${preimage}`);
10978
- }
10979
- service.data.preimages.set(preimage.hash, preimage);
10980
- if (slot !== null) {
10981
- const lookupHistory = service.data.lookupHistory.get(preimage.hash);
10982
- const length = numbers_tryAsU32(preimage.blob.length);
10983
- const lookup = new LookupHistoryItem(preimage.hash, length, tryAsLookupHistorySlots([slot]));
10984
- if (lookupHistory === undefined) {
10985
- // no lookup history for that preimage at all (edge case, should be requested)
10986
- service.data.lookupHistory.set(preimage.hash, [lookup]);
10976
+ for (const update of updates) {
10977
+ const { kind } = update.action;
10978
+ if (kind === UpdatePreimageKind.Provide) {
10979
+ const { preimage, slot } = update.action;
10980
+ if (service.data.preimages.has(preimage.hash)) {
10981
+ return result_Result.error(in_memory_state_UpdateError.PreimageExists, `Overwriting existing preimage at ${serviceId}: ${preimage}`);
10982
+ }
10983
+ service.data.preimages.set(preimage.hash, preimage);
10984
+ if (slot !== null) {
10985
+ const lookupHistory = service.data.lookupHistory.get(preimage.hash);
10986
+ const length = numbers_tryAsU32(preimage.blob.length);
10987
+ const lookup = new LookupHistoryItem(preimage.hash, length, tryAsLookupHistorySlots([slot]));
10988
+ if (lookupHistory === undefined) {
10989
+ // no lookup history for that preimage at all (edge case, should be requested)
10990
+ service.data.lookupHistory.set(preimage.hash, [lookup]);
10991
+ }
10992
+ else {
10993
+ // insert or replace exiting entry
10994
+ const index = lookupHistory.map((x) => x.length).indexOf(length);
10995
+ lookupHistory.splice(index, index === -1 ? 0 : 1, lookup);
10996
+ }
10987
10997
  }
10988
- else {
10989
- // insert or replace exiting entry
10990
- const index = lookupHistory.map((x) => x.length).indexOf(length);
10991
- lookupHistory.splice(index, index === -1 ? 0 : 1, lookup);
10998
+ }
10999
+ else if (kind === UpdatePreimageKind.Remove) {
11000
+ const { hash, length } = update.action;
11001
+ service.data.preimages.delete(hash);
11002
+ const history = service.data.lookupHistory.get(hash) ?? [];
11003
+ const idx = history.map((x) => x.length).indexOf(length);
11004
+ if (idx !== -1) {
11005
+ history.splice(idx, 1);
10992
11006
  }
10993
11007
  }
10994
- }
10995
- else if (kind === UpdatePreimageKind.Remove) {
10996
- const { hash, length } = action;
10997
- service.data.preimages.delete(hash);
10998
- const history = service.data.lookupHistory.get(hash) ?? [];
10999
- const idx = history.map((x) => x.length).indexOf(length);
11000
- if (idx !== -1) {
11001
- history.splice(idx, 1);
11008
+ else if (kind === UpdatePreimageKind.UpdateOrAdd) {
11009
+ const { item } = update.action;
11010
+ const history = service.data.lookupHistory.get(item.hash) ?? [];
11011
+ const existingIdx = history.map((x) => x.length).indexOf(item.length);
11012
+ const removeCount = existingIdx === -1 ? 0 : 1;
11013
+ history.splice(existingIdx, removeCount, item);
11014
+ service.data.lookupHistory.set(item.hash, history);
11015
+ }
11016
+ else {
11017
+ debug_assertNever(kind);
11002
11018
  }
11003
- }
11004
- else if (kind === UpdatePreimageKind.UpdateOrAdd) {
11005
- const { item } = action;
11006
- const history = service.data.lookupHistory.get(item.hash) ?? [];
11007
- const existingIdx = history.map((x) => x.length).indexOf(item.length);
11008
- const removeCount = existingIdx === -1 ? 0 : 1;
11009
- history.splice(existingIdx, removeCount, item);
11010
- service.data.lookupHistory.set(item.hash, history);
11011
- }
11012
- else {
11013
- debug_assertNever(kind);
11014
11019
  }
11015
11020
  }
11016
11021
  return result_Result.ok(result_OK);
11017
11022
  }
11018
11023
  updateServices(servicesUpdates) {
11019
- for (const { serviceId, action } of servicesUpdates ?? []) {
11020
- const { kind, account } = action;
11024
+ if (servicesUpdates === undefined) {
11025
+ return result_Result.ok(result_OK);
11026
+ }
11027
+ for (const [serviceId, update] of servicesUpdates.entries()) {
11028
+ const { kind, account } = update.action;
11021
11029
  if (kind === UpdateServiceKind.Create) {
11022
- const { lookupHistory } = action;
11030
+ const { lookupHistory } = update.action;
11023
11031
  if (this.services.has(serviceId)) {
11024
11032
  return result_Result.error(in_memory_state_UpdateError.DuplicateService, `${serviceId} already exists!`);
11025
11033
  }
@@ -12280,7 +12288,6 @@ function getKeccakTrieHasher(hasher) {
12280
12288
 
12281
12289
 
12282
12290
 
12283
-
12284
12291
  /** What should be done with that key? */
12285
12292
  var StateEntryUpdateAction;
12286
12293
  (function (StateEntryUpdateAction) {
@@ -12296,76 +12303,88 @@ function* serializeStateUpdate(spec, blake2b, update) {
12296
12303
  yield* serializeBasicKeys(spec, update);
12297
12304
  const encode = (codec, val) => encoder_Encoder.encodeObject(codec, val, spec);
12298
12305
  // then let's proceed with service updates
12299
- yield* serializeServiceUpdates(update.servicesUpdates, encode, blake2b);
12306
+ yield* serializeServiceUpdates(update.updated, encode, blake2b);
12300
12307
  yield* serializePreimages(update.preimages, encode, blake2b);
12301
12308
  yield* serializeStorage(update.storage, blake2b);
12302
- yield* serializeRemovedServices(update.servicesRemoved);
12309
+ yield* serializeRemovedServices(update.removed);
12303
12310
  }
12304
12311
  function* serializeRemovedServices(servicesRemoved) {
12305
- for (const serviceId of servicesRemoved ?? []) {
12312
+ if (servicesRemoved === undefined) {
12313
+ return;
12314
+ }
12315
+ for (const serviceId of servicesRemoved) {
12306
12316
  // TODO [ToDr] what about all data associated with a service?
12307
12317
  const codec = serialize_serialize.serviceData(serviceId);
12308
12318
  yield [StateEntryUpdateAction.Remove, codec.key, EMPTY_BLOB];
12309
12319
  }
12310
12320
  }
12311
- function* serializeStorage(storage, blake2b) {
12312
- for (const { action, serviceId } of storage ?? []) {
12313
- switch (action.kind) {
12314
- case UpdateStorageKind.Set: {
12315
- const key = action.storage.key;
12316
- const codec = serialize_serialize.serviceStorage(blake2b, serviceId, key);
12317
- yield [StateEntryUpdateAction.Insert, codec.key, action.storage.value];
12318
- break;
12319
- }
12320
- case UpdateStorageKind.Remove: {
12321
- const key = action.key;
12322
- const codec = serialize_serialize.serviceStorage(blake2b, serviceId, key);
12323
- yield [StateEntryUpdateAction.Remove, codec.key, EMPTY_BLOB];
12324
- break;
12321
+ function* serializeStorage(storageUpdates, blake2b) {
12322
+ if (storageUpdates === undefined) {
12323
+ return;
12324
+ }
12325
+ for (const [serviceId, updates] of storageUpdates.entries()) {
12326
+ for (const { action } of updates) {
12327
+ switch (action.kind) {
12328
+ case UpdateStorageKind.Set: {
12329
+ const key = action.storage.key;
12330
+ const codec = serialize_serialize.serviceStorage(blake2b, serviceId, key);
12331
+ yield [StateEntryUpdateAction.Insert, codec.key, action.storage.value];
12332
+ break;
12333
+ }
12334
+ case UpdateStorageKind.Remove: {
12335
+ const key = action.key;
12336
+ const codec = serialize_serialize.serviceStorage(blake2b, serviceId, key);
12337
+ yield [StateEntryUpdateAction.Remove, codec.key, EMPTY_BLOB];
12338
+ break;
12339
+ }
12325
12340
  }
12326
- default:
12327
- debug_assertNever(action);
12328
12341
  }
12329
12342
  }
12330
12343
  }
12331
- function* serializePreimages(preimages, encode, blake2b) {
12332
- for (const { action, serviceId } of preimages ?? []) {
12333
- switch (action.kind) {
12334
- case UpdatePreimageKind.Provide: {
12335
- const { hash, blob } = action.preimage;
12336
- const codec = serialize_serialize.servicePreimages(blake2b, serviceId, hash);
12337
- yield [StateEntryUpdateAction.Insert, codec.key, blob];
12338
- if (action.slot !== null) {
12339
- const codec2 = serialize_serialize.serviceLookupHistory(blake2b, serviceId, hash, numbers_tryAsU32(blob.length));
12340
- yield [
12341
- StateEntryUpdateAction.Insert,
12342
- codec2.key,
12343
- encode(codec2.Codec, tryAsLookupHistorySlots([action.slot])),
12344
- ];
12344
+ function* serializePreimages(preimagesUpdates, encode, blake2b) {
12345
+ if (preimagesUpdates === undefined) {
12346
+ return;
12347
+ }
12348
+ for (const [serviceId, updates] of preimagesUpdates.entries()) {
12349
+ for (const { action } of updates) {
12350
+ switch (action.kind) {
12351
+ case UpdatePreimageKind.Provide: {
12352
+ const { hash, blob } = action.preimage;
12353
+ const codec = serialize_serialize.servicePreimages(blake2b, serviceId, hash);
12354
+ yield [StateEntryUpdateAction.Insert, codec.key, blob];
12355
+ if (action.slot !== null) {
12356
+ const codec2 = serialize_serialize.serviceLookupHistory(blake2b, serviceId, hash, numbers_tryAsU32(blob.length));
12357
+ yield [
12358
+ StateEntryUpdateAction.Insert,
12359
+ codec2.key,
12360
+ encode(codec2.Codec, tryAsLookupHistorySlots([action.slot])),
12361
+ ];
12362
+ }
12363
+ break;
12364
+ }
12365
+ case UpdatePreimageKind.UpdateOrAdd: {
12366
+ const { hash, length, slots } = action.item;
12367
+ const codec = serialize_serialize.serviceLookupHistory(blake2b, serviceId, hash, length);
12368
+ yield [StateEntryUpdateAction.Insert, codec.key, encode(codec.Codec, slots)];
12369
+ break;
12370
+ }
12371
+ case UpdatePreimageKind.Remove: {
12372
+ const { hash, length } = action;
12373
+ const codec = serialize_serialize.servicePreimages(blake2b, serviceId, hash);
12374
+ yield [StateEntryUpdateAction.Remove, codec.key, EMPTY_BLOB];
12375
+ const codec2 = serialize_serialize.serviceLookupHistory(blake2b, serviceId, hash, length);
12376
+ yield [StateEntryUpdateAction.Remove, codec2.key, EMPTY_BLOB];
12377
+ break;
12345
12378
  }
12346
- break;
12347
- }
12348
- case UpdatePreimageKind.UpdateOrAdd: {
12349
- const { hash, length, slots } = action.item;
12350
- const codec = serialize_serialize.serviceLookupHistory(blake2b, serviceId, hash, length);
12351
- yield [StateEntryUpdateAction.Insert, codec.key, encode(codec.Codec, slots)];
12352
- break;
12353
- }
12354
- case UpdatePreimageKind.Remove: {
12355
- const { hash, length } = action;
12356
- const codec = serialize_serialize.servicePreimages(blake2b, serviceId, hash);
12357
- yield [StateEntryUpdateAction.Remove, codec.key, EMPTY_BLOB];
12358
- const codec2 = serialize_serialize.serviceLookupHistory(blake2b, serviceId, hash, length);
12359
- yield [StateEntryUpdateAction.Remove, codec2.key, EMPTY_BLOB];
12360
- break;
12361
12379
  }
12362
- default:
12363
- debug_assertNever(action);
12364
12380
  }
12365
12381
  }
12366
12382
  }
12367
12383
  function* serializeServiceUpdates(servicesUpdates, encode, blake2b) {
12368
- for (const { action, serviceId } of servicesUpdates ?? []) {
12384
+ if (servicesUpdates === undefined) {
12385
+ return;
12386
+ }
12387
+ for (const [serviceId, { action }] of servicesUpdates.entries()) {
12369
12388
  // new service being created or updated
12370
12389
  const codec = serialize_serialize.serviceData(serviceId);
12371
12390
  yield [StateEntryUpdateAction.Insert, codec.key, encode(codec.Codec, action.account)];
@@ -14101,7 +14120,7 @@ class Preimages {
14101
14120
  }
14102
14121
  }
14103
14122
  const { preimages, slot } = input;
14104
- const pendingChanges = [];
14123
+ const pendingChanges = new Map();
14105
14124
  // select preimages for integration
14106
14125
  for (const preimage of preimages) {
14107
14126
  const { requester, blob } = preimage;
@@ -14118,11 +14137,12 @@ class Preimages {
14118
14137
  return result_Result.error(PreimagesErrorCode.PreimageUnneeded);
14119
14138
  }
14120
14139
  // https://graypaper.fluffylabs.dev/#/5f542d7/18c00018f300
14121
- pendingChanges.push(UpdatePreimage.provide({
14122
- serviceId: requester,
14140
+ const updates = pendingChanges.get(requester) ?? [];
14141
+ updates.push(UpdatePreimage.provide({
14123
14142
  preimage: PreimageItem.create({ hash, blob }),
14124
14143
  slot,
14125
14144
  }));
14145
+ pendingChanges.set(requester, updates);
14126
14146
  }
14127
14147
  return result_Result.ok({
14128
14148
  preimages: pendingChanges,
@@ -15280,6 +15300,14 @@ async function getRootHash(yieldedRoots) {
15280
15300
 
15281
15301
 
15282
15302
  const InsufficientFundsError = "insufficient funds";
15303
+ /** Deep clone of a map with array. */
15304
+ function deepCloneMapWithArray(map) {
15305
+ const cloned = [];
15306
+ for (const [k, v] of map.entries()) {
15307
+ cloned.push([k, v.slice()]);
15308
+ }
15309
+ return new Map(cloned);
15310
+ }
15283
15311
  /**
15284
15312
  * State updates that currently accumulating service produced.
15285
15313
  *
@@ -15309,10 +15337,11 @@ class AccumulationStateUpdate {
15309
15337
  /** Create new empty state update. */
15310
15338
  static empty() {
15311
15339
  return new AccumulationStateUpdate({
15312
- servicesUpdates: [],
15313
- servicesRemoved: [],
15314
- preimages: [],
15315
- storage: [],
15340
+ created: [],
15341
+ updated: new Map(),
15342
+ removed: [],
15343
+ preimages: new Map(),
15344
+ storage: new Map(),
15316
15345
  }, []);
15317
15346
  }
15318
15347
  /** Create a state update with some existing, yet uncommited services updates. */
@@ -15324,10 +15353,13 @@ class AccumulationStateUpdate {
15324
15353
  /** Create a copy of another `StateUpdate`. Used by checkpoints. */
15325
15354
  static copyFrom(from) {
15326
15355
  const serviceUpdates = {
15327
- servicesUpdates: [...from.services.servicesUpdates],
15328
- servicesRemoved: [...from.services.servicesRemoved],
15329
- preimages: [...from.services.preimages],
15330
- storage: [...from.services.storage],
15356
+ // shallow copy
15357
+ created: [...from.services.created],
15358
+ updated: new Map(from.services.updated),
15359
+ removed: [...from.services.removed],
15360
+ // deep copy
15361
+ preimages: deepCloneMapWithArray(from.services.preimages),
15362
+ storage: deepCloneMapWithArray(from.services.storage),
15331
15363
  };
15332
15364
  const transfers = [...from.transfers];
15333
15365
  const update = new AccumulationStateUpdate(serviceUpdates, transfers, new Map(from.yieldedRoots));
@@ -15375,9 +15407,9 @@ class PartiallyUpdatedState {
15375
15407
  if (destination === null) {
15376
15408
  return null;
15377
15409
  }
15378
- const maybeNewService = this.stateUpdate.services.servicesUpdates.find((update) => update.serviceId === destination);
15379
- if (maybeNewService !== undefined) {
15380
- return maybeNewService.action.account;
15410
+ const maybeUpdatedServiceInfo = this.stateUpdate.services.updated.get(destination);
15411
+ if (maybeUpdatedServiceInfo !== undefined) {
15412
+ return maybeUpdatedServiceInfo.action.account;
15381
15413
  }
15382
15414
  const maybeService = this.state.getService(destination);
15383
15415
  if (maybeService === null) {
@@ -15386,7 +15418,8 @@ class PartiallyUpdatedState {
15386
15418
  return maybeService.getInfo();
15387
15419
  }
15388
15420
  getStorage(serviceId, rawKey) {
15389
- const item = this.stateUpdate.services.storage.find((x) => x.serviceId === serviceId && x.key.isEqualTo(rawKey));
15421
+ const storages = this.stateUpdate.services.storage.get(serviceId) ?? [];
15422
+ const item = storages.find((x) => x.key.isEqualTo(rawKey));
15390
15423
  if (item !== undefined) {
15391
15424
  return item.value;
15392
15425
  }
@@ -15401,10 +15434,11 @@ class PartiallyUpdatedState {
15401
15434
  * the existence in `preimages` map.
15402
15435
  */
15403
15436
  hasPreimage(serviceId, hash) {
15404
- const providedPreimage = this.stateUpdate.services.preimages.find(
15437
+ const preimages = this.stateUpdate.services.preimages.get(serviceId) ?? [];
15438
+ const providedPreimage = preimages.find(
15405
15439
  // we ignore the action here, since if there is <any> update on that
15406
15440
  // hash it means it has to exist, right?
15407
- (p) => p.serviceId === serviceId && p.hash.isEqualTo(hash));
15441
+ (p) => p.hash.isEqualTo(hash));
15408
15442
  if (providedPreimage !== undefined) {
15409
15443
  return true;
15410
15444
  }
@@ -15417,7 +15451,8 @@ class PartiallyUpdatedState {
15417
15451
  }
15418
15452
  getPreimage(serviceId, hash) {
15419
15453
  // TODO [ToDr] Should we verify availability here?
15420
- const freshlyProvided = this.stateUpdate.services.preimages.find((x) => x.serviceId === serviceId && x.hash.isEqualTo(hash));
15454
+ const preimages = this.stateUpdate.services.preimages.get(serviceId) ?? [];
15455
+ const freshlyProvided = preimages.find((x) => x.hash.isEqualTo(hash));
15421
15456
  if (freshlyProvided !== undefined && freshlyProvided.action.kind === UpdatePreimageKind.Provide) {
15422
15457
  return freshlyProvided.action.preimage.blob;
15423
15458
  }
@@ -15426,10 +15461,11 @@ class PartiallyUpdatedState {
15426
15461
  }
15427
15462
  /** Get status of a preimage of current service taking into account any updates. */
15428
15463
  getLookupHistory(currentTimeslot, serviceId, hash, length) {
15464
+ const preimages = this.stateUpdate.services.preimages.get(serviceId) ?? [];
15429
15465
  // TODO [ToDr] This is most likely wrong. We may have `provide` and `remove` within
15430
15466
  // the same state update. We should however switch to proper "updated state"
15431
15467
  // representation soon.
15432
- const updatedPreimage = this.stateUpdate.services.preimages.findLast((update) => update.serviceId === serviceId && update.hash.isEqualTo(hash) && BigInt(update.length) === length);
15468
+ const updatedPreimage = preimages.findLast((update) => update.hash.isEqualTo(hash) && BigInt(update.length) === length);
15433
15469
  const stateFallback = () => {
15434
15470
  // fallback to state lookup
15435
15471
  const service = this.state.getService(serviceId);
@@ -15466,14 +15502,15 @@ class PartiallyUpdatedState {
15466
15502
  /* State update functions. */
15467
15503
  updateStorage(serviceId, key, value) {
15468
15504
  const update = value === null
15469
- ? UpdateStorage.remove({ serviceId, key })
15505
+ ? UpdateStorage.remove({ key })
15470
15506
  : UpdateStorage.set({
15471
- serviceId,
15472
15507
  storage: StorageItem.create({ key, value }),
15473
15508
  });
15474
- const index = this.stateUpdate.services.storage.findIndex((x) => x.serviceId === update.serviceId && x.key.isEqualTo(key));
15509
+ const storages = this.stateUpdate.services.storage.get(serviceId) ?? [];
15510
+ const index = storages.findIndex((x) => x.key.isEqualTo(key));
15475
15511
  const count = index === -1 ? 0 : 1;
15476
- this.stateUpdate.services.storage.splice(index, count, update);
15512
+ storages.splice(index, count, update);
15513
+ this.stateUpdate.services.storage.set(serviceId, storages);
15477
15514
  }
15478
15515
  /**
15479
15516
  * Update a preimage.
@@ -15481,8 +15518,10 @@ class PartiallyUpdatedState {
15481
15518
  * Note we store all previous entries as well, since there might be a sequence of:
15482
15519
  * `provide` -> `remove` and both should update the end state somehow.
15483
15520
  */
15484
- updatePreimage(newUpdate) {
15485
- this.stateUpdate.services.preimages.push(newUpdate);
15521
+ updatePreimage(serviceId, newUpdate) {
15522
+ const updatePreimages = this.stateUpdate.services.preimages.get(serviceId) ?? [];
15523
+ updatePreimages.push(newUpdate);
15524
+ this.stateUpdate.services.preimages.set(serviceId, updatePreimages);
15486
15525
  }
15487
15526
  updateServiceStorageUtilisation(serviceId, items, bytes, serviceInfo) {
15488
15527
  debug_check `${items >= 0} storageUtilisationCount has to be a positive number, got: ${items}`;
@@ -15506,20 +15545,23 @@ class PartiallyUpdatedState {
15506
15545
  return result_Result.ok(result_OK);
15507
15546
  }
15508
15547
  updateServiceInfo(serviceId, newInfo) {
15509
- const idx = this.stateUpdate.services.servicesUpdates.findIndex((x) => x.serviceId === serviceId);
15510
- const toRemove = idx === -1 ? 0 : 1;
15511
- const existingItem = this.stateUpdate.services.servicesUpdates[idx];
15512
- if (existingItem?.action.kind === UpdateServiceKind.Create) {
15513
- this.stateUpdate.services.servicesUpdates.splice(idx, toRemove, UpdateService.create({
15514
- serviceId,
15548
+ const existingUpdate = this.stateUpdate.services.updated.get(serviceId);
15549
+ if (existingUpdate?.action.kind === UpdateServiceKind.Create) {
15550
+ this.stateUpdate.services.updated.set(serviceId, UpdateService.create({
15515
15551
  serviceInfo: newInfo,
15516
- lookupHistory: existingItem.action.lookupHistory,
15552
+ lookupHistory: existingUpdate.action.lookupHistory,
15517
15553
  }));
15518
15554
  return;
15519
15555
  }
15520
- this.stateUpdate.services.servicesUpdates.splice(idx, toRemove, UpdateService.update({
15521
- serviceId,
15556
+ this.stateUpdate.services.updated.set(serviceId, UpdateService.update({
15557
+ serviceInfo: newInfo,
15558
+ }));
15559
+ }
15560
+ createService(serviceId, newInfo, newLookupHistory) {
15561
+ this.stateUpdate.services.created.push(serviceId);
15562
+ this.stateUpdate.services.updated.set(serviceId, UpdateService.create({
15522
15563
  serviceInfo: newInfo,
15564
+ lookupHistory: newLookupHistory,
15523
15565
  }));
15524
15566
  }
15525
15567
  getPrivilegedServices() {
@@ -19884,15 +19926,13 @@ class AccumulateExternalities {
19884
19926
  const clampedLength = clampU64ToU32(length);
19885
19927
  if (existingPreimage === null) {
19886
19928
  // https://graypaper.fluffylabs.dev/#/9a08063/38a60038a600?v=0.6.6
19887
- this.updatedState.updatePreimage(UpdatePreimage.updateOrAdd({
19888
- serviceId: this.currentServiceId,
19929
+ this.updatedState.updatePreimage(this.currentServiceId, UpdatePreimage.updateOrAdd({
19889
19930
  lookupHistory: new LookupHistoryItem(hash, clampedLength, tryAsLookupHistorySlots([])),
19890
19931
  }));
19891
19932
  }
19892
19933
  else {
19893
19934
  /** https://graypaper.fluffylabs.dev/#/9a08063/38ca0038ca00?v=0.6.6 */
19894
- this.updatedState.updatePreimage(UpdatePreimage.updateOrAdd({
19895
- serviceId: this.currentServiceId,
19935
+ this.updatedState.updatePreimage(this.currentServiceId, UpdatePreimage.updateOrAdd({
19896
19936
  lookupHistory: new LookupHistoryItem(hash, clampedLength, tryAsLookupHistorySlots([...existingPreimage.slots, this.currentTimeslot])),
19897
19937
  }));
19898
19938
  }
@@ -19917,8 +19957,7 @@ class AccumulateExternalities {
19917
19957
  if (res.isError) {
19918
19958
  return result_Result.error(ForgetPreimageError.StorageUtilisationError);
19919
19959
  }
19920
- this.updatedState.updatePreimage(UpdatePreimage.remove({
19921
- serviceId,
19960
+ this.updatedState.updatePreimage(serviceId, UpdatePreimage.remove({
19922
19961
  hash: status.hash,
19923
19962
  length: status.length,
19924
19963
  }));
@@ -19933,8 +19972,7 @@ class AccumulateExternalities {
19933
19972
  if (res.isError) {
19934
19973
  return result_Result.error(ForgetPreimageError.StorageUtilisationError);
19935
19974
  }
19936
- this.updatedState.updatePreimage(UpdatePreimage.remove({
19937
- serviceId,
19975
+ this.updatedState.updatePreimage(serviceId, UpdatePreimage.remove({
19938
19976
  hash: status.hash,
19939
19977
  length: status.length,
19940
19978
  }));
@@ -19944,8 +19982,7 @@ class AccumulateExternalities {
19944
19982
  }
19945
19983
  // https://graypaper.fluffylabs.dev/#/9a08063/38c80138c801?v=0.6.6
19946
19984
  if (s.status === PreimageStatusKind.Available) {
19947
- this.updatedState.updatePreimage(UpdatePreimage.updateOrAdd({
19948
- serviceId,
19985
+ this.updatedState.updatePreimage(serviceId, UpdatePreimage.updateOrAdd({
19949
19986
  lookupHistory: new LookupHistoryItem(status.hash, status.length, tryAsLookupHistorySlots([s.data[0], t])),
19950
19987
  }));
19951
19988
  return result_Result.ok(result_OK);
@@ -19954,8 +19991,7 @@ class AccumulateExternalities {
19954
19991
  if (s.status === PreimageStatusKind.Reavailable) {
19955
19992
  const y = s.data[1];
19956
19993
  if (y < t - this.chainSpec.preimageExpungePeriod) {
19957
- this.updatedState.updatePreimage(UpdatePreimage.updateOrAdd({
19958
- serviceId,
19994
+ this.updatedState.updatePreimage(serviceId, UpdatePreimage.updateOrAdd({
19959
19995
  lookupHistory: new LookupHistoryItem(status.hash, status.length, tryAsLookupHistorySlots([s.data[2], t])),
19960
19996
  }));
19961
19997
  return result_Result.ok(result_OK);
@@ -20046,11 +20082,7 @@ class AccumulateExternalities {
20046
20082
  }
20047
20083
  // add the new service with selected ID
20048
20084
  // https://graypaper.fluffylabs.dev/#/ab2cdbd/36be0336c003?v=0.7.2
20049
- this.updatedState.stateUpdate.services.servicesUpdates.push(UpdateService.create({
20050
- serviceId: newServiceId,
20051
- serviceInfo: newAccount,
20052
- lookupHistory: newLookupItem,
20053
- }));
20085
+ this.updatedState.createService(newServiceId, newAccount, newLookupItem);
20054
20086
  // update the balance of current service
20055
20087
  // https://graypaper.fluffylabs.dev/#/ab2cdbd/36c20336c403?v=0.7.2
20056
20088
  this.updatedState.updateServiceInfo(this.currentServiceId, updatedCurrentAccount);
@@ -20061,12 +20093,8 @@ class AccumulateExternalities {
20061
20093
  }
20062
20094
  const newServiceId = this.nextNewServiceId;
20063
20095
  // add the new service
20064
- // https://graypaper.fluffylabs.dev/#/ab2cdbd/36e70336e903?v=0.7.2
20065
- this.updatedState.stateUpdate.services.servicesUpdates.push(UpdateService.create({
20066
- serviceId: newServiceId,
20067
- serviceInfo: newAccount,
20068
- lookupHistory: newLookupItem,
20069
- }));
20096
+ // https://graypaper.fluffylabs.dev/#/7e6ff6a/36cb0236cb02?v=0.6.7
20097
+ this.updatedState.createService(newServiceId, newAccount, newLookupItem);
20070
20098
  // update the balance of current service
20071
20099
  // https://graypaper.fluffylabs.dev/#/ab2cdbd/36ec0336ee03?v=0.7.2
20072
20100
  this.updatedState.updateServiceInfo(this.currentServiceId, updatedCurrentAccount);
@@ -20213,8 +20241,7 @@ class AccumulateExternalities {
20213
20241
  return result_Result.error(ProvidePreimageError.AlreadyProvided);
20214
20242
  }
20215
20243
  // setting up the new preimage
20216
- this.updatedState.updatePreimage(UpdatePreimage.provide({
20217
- serviceId,
20244
+ this.updatedState.updatePreimage(serviceId, UpdatePreimage.provide({
20218
20245
  preimage: PreimageItem.create({
20219
20246
  hash: preimageHash,
20220
20247
  blob: preimage,
@@ -20258,11 +20285,13 @@ class AccumulateExternalities {
20258
20285
  balance: newBalance.value,
20259
20286
  }));
20260
20287
  // and finally add an ejected service.
20261
- this.updatedState.stateUpdate.services.servicesRemoved.push(destination);
20288
+ this.updatedState.stateUpdate.services.removed.push(destination);
20262
20289
  // take care of the code preimage and its lookup history
20263
20290
  // Safe, because we know the preimage is valid, and it's the code of the service, which is bounded by maximal service code size anyway (much smaller than 2**32 bytes).
20264
20291
  const preimageLength = numbers_tryAsU32(Number(l));
20265
- this.updatedState.stateUpdate.services.preimages.push(UpdatePreimage.remove({ serviceId: destination, hash: previousCodeHash, length: preimageLength }));
20292
+ const preimages = this.updatedState.stateUpdate.services.preimages.get(destination) ?? [];
20293
+ preimages.push(UpdatePreimage.remove({ hash: previousCodeHash, length: preimageLength }));
20294
+ this.updatedState.stateUpdate.services.preimages.set(destination, preimages);
20266
20295
  return result_Result.ok(result_OK);
20267
20296
  }
20268
20297
  read(serviceId, rawKey) {
@@ -21151,9 +21180,6 @@ class HostCallMemory {
21151
21180
  }
21152
21181
  return this.memory.loadInto(result, tryAsMemoryIndex(Number(startAddress)));
21153
21182
  }
21154
- getMemory() {
21155
- return this.memory;
21156
- }
21157
21183
  }
21158
21184
 
21159
21185
  ;// CONCATENATED MODULE: ./packages/core/pvm-host-calls/host-call-registers.ts
@@ -23359,19 +23385,16 @@ class Accumulate {
23359
23385
  const gasLimit = tryAsServiceGas(this.chainSpec.maxBlockGas > calculatedGasLimit ? this.chainSpec.maxBlockGas : calculatedGasLimit);
23360
23386
  return tryAsServiceGas(gasLimit);
23361
23387
  }
23362
- hasDuplicatedServicesCreated(updateServices) {
23363
- const createdServiceIds = new Set();
23364
- for (const update of updateServices) {
23365
- if (update.action.kind === UpdateServiceKind.Create) {
23366
- const serviceId = update.serviceId;
23367
- if (createdServiceIds.has(serviceId)) {
23368
- accumulate_logger.log `Duplicated Service creation detected ${serviceId}. Block is invalid.`;
23369
- return true;
23370
- }
23371
- createdServiceIds.add(serviceId);
23372
- }
23373
- }
23374
- return false;
23388
+ /**
23389
+ * Detects the very unlikely situation where multiple services are created with the same ID.
23390
+ *
23391
+ * https://graypaper.fluffylabs.dev/#/ab2cdbd/30f20330f403?v=0.7.2
23392
+ *
23393
+ * NOTE: This is public only for testing purposes and should not be used outside of accumulation.
23394
+ */
23395
+ hasDuplicatedServiceIdCreated(createdIds) {
23396
+ const uniqueIds = new Set(createdIds);
23397
+ return uniqueIds.size !== createdIds.length;
23375
23398
  }
23376
23399
  async transition({ reports, slot, entropy }) {
23377
23400
  const statistics = new Map();
@@ -23393,7 +23416,8 @@ class Accumulate {
23393
23416
  const accumulated = accumulatableReports.subview(0, accumulatedReports);
23394
23417
  const { services, yieldedRoots, transfers, validatorsData, privilegedServices, authorizationQueues, ...stateUpdateRest } = state;
23395
23418
  assertEmpty(stateUpdateRest);
23396
- if (this.hasDuplicatedServicesCreated(services.servicesUpdates)) {
23419
+ if (this.hasDuplicatedServiceIdCreated(services.created)) {
23420
+ accumulate_logger.trace `Duplicated Service creation detected. Block is invalid.`;
23397
23421
  return result_Result.error(ACCUMULATION_ERROR);
23398
23422
  }
23399
23423
  const accStateUpdate = this.getAccumulationStateUpdate(accumulated.toArray(), toAccumulateLater, slot, Array.from(statistics.keys()), services);
@@ -25083,6 +25107,16 @@ class OnChain {
25083
25107
  });
25084
25108
  const { statistics, ...statisticsRest } = statisticsUpdate;
25085
25109
  assertEmpty(statisticsRest);
25110
+ // Concat accumulatePreimages updates with preimages
25111
+ for (const [serviceId, accPreimageUpdates] of accumulatePreimages.entries()) {
25112
+ const preimagesUpdates = preimages.get(serviceId);
25113
+ if (preimagesUpdates === undefined) {
25114
+ preimages.set(serviceId, accPreimageUpdates);
25115
+ }
25116
+ else {
25117
+ preimages.set(serviceId, preimagesUpdates.concat(accPreimageUpdates));
25118
+ }
25119
+ }
25086
25120
  return result_Result.ok({
25087
25121
  ...(maybeAuthorizationQueues !== undefined ? { authQueues: maybeAuthorizationQueues } : {}),
25088
25122
  ...(maybeDesignatedValidatorData !== undefined ? { designatedValidatorData: maybeDesignatedValidatorData } : {}),
@@ -25104,7 +25138,7 @@ class OnChain {
25104
25138
  recentlyAccumulated,
25105
25139
  accumulationOutputLog,
25106
25140
  ...servicesUpdate,
25107
- preimages: preimages.concat(accumulatePreimages),
25141
+ preimages,
25108
25142
  });
25109
25143
  }
25110
25144
  getUsedAuthorizerHashes(guarantees) {