@typeberry/jam 0.2.0-e767e74 → 0.2.0-f506473

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.
@@ -3170,7 +3170,7 @@ function resultToString(res) {
3170
3170
  if (res.isOk) {
3171
3171
  return `OK: ${typeof res.ok === "symbol" ? res.ok.toString() : res.ok}`;
3172
3172
  }
3173
- return `${res.details}\nError: ${maybeTaggedErrorToString(res.error)}`;
3173
+ return `${res.details()}\nError: ${maybeTaggedErrorToString(res.error)}`;
3174
3174
  }
3175
3175
  /** An indication of two possible outcomes returned from a function. */
3176
3176
  const result_Result = {
@@ -3184,7 +3184,7 @@ const result_Result = {
3184
3184
  };
3185
3185
  },
3186
3186
  /** Create new [`Result`] with `Error` status. */
3187
- error: (error, details = "") => {
3187
+ error: (error, details) => {
3188
3188
  debug_check `${error !== undefined} 'Error' type cannot be undefined.`;
3189
3189
  return {
3190
3190
  isOk: false,
@@ -3303,7 +3303,7 @@ function deepEqual(actual, expected, { context = [], errorsCollector, ignore = [
3303
3303
  }
3304
3304
  if (actual.isError && expected.isError) {
3305
3305
  deepEqual(actual.error, expected.error, { context: ctx.concat(["error"]), errorsCollector: errors, ignore });
3306
- deepEqual(actual.details, expected.details, {
3306
+ deepEqual(actual.details(), expected.details(), {
3307
3307
  context: ctx.concat(["details"]),
3308
3308
  errorsCollector: errors,
3309
3309
  // display details when error does not match
@@ -3956,8 +3956,8 @@ class decoder_Decoder {
3956
3956
  /**
3957
3957
  * Create a new [`Decoder`] instance given a raw array of bytes as a source.
3958
3958
  */
3959
- static fromBlob(source) {
3960
- return new decoder_Decoder(source);
3959
+ static fromBlob(source, context) {
3960
+ return new decoder_Decoder(source, undefined, context);
3961
3961
  }
3962
3962
  /**
3963
3963
  * Decode a single object from all of the source bytes.
@@ -4252,7 +4252,7 @@ class decoder_Decoder {
4252
4252
  ensureHasBytes(bytes) {
4253
4253
  debug_check `${bytes >= 0} Negative number of bytes given.`;
4254
4254
  if (this.offset + bytes > this.source.length) {
4255
- throw new Error(`Attempting to decode more data than there is left. Need ${bytes}, left: ${this.source.length - this.offset}.`);
4255
+ throw new EndOfDataError(`Attempting to decode more data than there is left. Need ${bytes}, left: ${this.source.length - this.offset}.`);
4256
4256
  }
4257
4257
  }
4258
4258
  }
@@ -4266,6 +4266,8 @@ function decodeVariableLengthExtraBytes(firstByte) {
4266
4266
  }
4267
4267
  return 0;
4268
4268
  }
4269
+ class EndOfDataError extends Error {
4270
+ }
4269
4271
 
4270
4272
  ;// CONCATENATED MODULE: ./packages/core/codec/skip.ts
4271
4273
 
@@ -5057,6 +5059,7 @@ class SequenceView {
5057
5059
 
5058
5060
 
5059
5061
 
5062
+
5060
5063
  /**
5061
5064
  * For sequences with unknown length we need to give some size hint.
5062
5065
  * TODO [ToDr] [opti] This value should be updated when we run some real-data bechmarks.
@@ -5306,6 +5309,9 @@ function forEachDescriptor(descriptors, f) {
5306
5309
  f(k, descriptors[k]);
5307
5310
  }
5308
5311
  catch (e) {
5312
+ if (e instanceof EndOfDataError) {
5313
+ throw new EndOfDataError(`${key}: ${e}`);
5314
+ }
5309
5315
  throw new Error(`${key}: ${e}`);
5310
5316
  }
5311
5317
  }
@@ -9044,6 +9050,7 @@ function accumulationOutputComparator(a, b) {
9044
9050
 
9045
9051
  ;// CONCATENATED MODULE: ./packages/jam/block/gp-constants.ts
9046
9052
 
9053
+
9047
9054
  /**
9048
9055
  * This file lists all of the constants defined in the GrayPaper appendix.
9049
9056
  *
@@ -9054,7 +9061,7 @@ function accumulationOutputComparator(a, b) {
9054
9061
  * here are only temporarily for convenience. When we figure out better names
9055
9062
  * and places for these this file will be eradicated.
9056
9063
  *
9057
- * https://graypaper.fluffylabs.dev/#/579bd12/413000413000
9064
+ * https://graypaper.fluffylabs.dev/#/ab2cdbd/442300442300?v=0.7.2
9058
9065
  */
9059
9066
  /** `G_I`: The gas allocated to invoke a work-package’s Is-Authorized logic. */
9060
9067
  const G_I = 50_000_000;
@@ -9070,8 +9077,8 @@ const S = 1024;
9070
9077
  const T = 128;
9071
9078
  /** `W_A`: The maximum size of is-authorized code in octets. */
9072
9079
  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;
9080
+ /** `W_B`: The maximum size of the concatenated variable-size blobs, extrinsics and imported segments of a work-package, in octets */
9081
+ const W_B = Compatibility.isGreaterOrEqual(GpVersion.V0_7_2) ? 13_791_360 : 13_794_305;
9075
9082
  /** `W_C`: The maximum size of service code in octets. */
9076
9083
  const W_C = 4_000_000;
9077
9084
  /** `W_M`: The maximum number of imports in a work-package. */
@@ -10170,31 +10177,29 @@ var UpdatePreimageKind;
10170
10177
  * 3. Update `LookupHistory` with given value.
10171
10178
  */
10172
10179
  class state_update_UpdatePreimage {
10173
- serviceId;
10174
10180
  action;
10175
- constructor(serviceId, action) {
10176
- this.serviceId = serviceId;
10181
+ constructor(action) {
10177
10182
  this.action = action;
10178
10183
  }
10179
10184
  /** 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, {
10185
+ static provide({ preimage, slot }) {
10186
+ return new state_update_UpdatePreimage({
10182
10187
  kind: UpdatePreimageKind.Provide,
10183
10188
  preimage,
10184
10189
  slot,
10185
10190
  });
10186
10191
  }
10187
10192
  /** The preimage should be removed completely from the database. */
10188
- static remove({ serviceId, hash, length }) {
10189
- return new state_update_UpdatePreimage(serviceId, {
10193
+ static remove({ hash, length }) {
10194
+ return new state_update_UpdatePreimage({
10190
10195
  kind: UpdatePreimageKind.Remove,
10191
10196
  hash,
10192
10197
  length,
10193
10198
  });
10194
10199
  }
10195
10200
  /** 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, {
10201
+ static updateOrAdd({ lookupHistory }) {
10202
+ return new state_update_UpdatePreimage({
10198
10203
  kind: UpdatePreimageKind.UpdateOrAdd,
10199
10204
  item: lookupHistory,
10200
10205
  });
@@ -10231,23 +10236,21 @@ var UpdateServiceKind;
10231
10236
  UpdateServiceKind[UpdateServiceKind["Create"] = 1] = "Create";
10232
10237
  })(UpdateServiceKind || (UpdateServiceKind = {}));
10233
10238
  /**
10234
- * Update service info of a particular `ServiceId` or create a new one.
10239
+ * Update service info or create a new one.
10235
10240
  */
10236
10241
  class UpdateService {
10237
- serviceId;
10238
10242
  action;
10239
- constructor(serviceId, action) {
10240
- this.serviceId = serviceId;
10243
+ constructor(action) {
10241
10244
  this.action = action;
10242
10245
  }
10243
- static update({ serviceId, serviceInfo }) {
10244
- return new UpdateService(serviceId, {
10246
+ static update({ serviceInfo }) {
10247
+ return new UpdateService({
10245
10248
  kind: UpdateServiceKind.Update,
10246
10249
  account: serviceInfo,
10247
10250
  });
10248
10251
  }
10249
- static create({ serviceId, serviceInfo, lookupHistory, }) {
10250
- return new UpdateService(serviceId, {
10252
+ static create({ serviceInfo, lookupHistory, }) {
10253
+ return new UpdateService({
10251
10254
  kind: UpdateServiceKind.Create,
10252
10255
  account: serviceInfo,
10253
10256
  lookupHistory,
@@ -10268,17 +10271,15 @@ var UpdateStorageKind;
10268
10271
  * Can either create/modify an entry or remove it.
10269
10272
  */
10270
10273
  class UpdateStorage {
10271
- serviceId;
10272
10274
  action;
10273
- constructor(serviceId, action) {
10274
- this.serviceId = serviceId;
10275
+ constructor(action) {
10275
10276
  this.action = action;
10276
10277
  }
10277
- static set({ serviceId, storage }) {
10278
- return new UpdateStorage(serviceId, { kind: UpdateStorageKind.Set, storage });
10278
+ static set({ storage }) {
10279
+ return new UpdateStorage({ kind: UpdateStorageKind.Set, storage });
10279
10280
  }
10280
- static remove({ serviceId, key }) {
10281
- return new UpdateStorage(serviceId, { kind: UpdateStorageKind.Remove, key });
10281
+ static remove({ key }) {
10282
+ return new UpdateStorage({ kind: UpdateStorageKind.Remove, key });
10282
10283
  }
10283
10284
  get key() {
10284
10285
  if (this.action.kind === UpdateStorageKind.Remove) {
@@ -10487,12 +10488,12 @@ class in_memory_state_InMemoryState extends WithDebug {
10487
10488
  * Modify the state and apply a single state update.
10488
10489
  */
10489
10490
  applyUpdate(update) {
10490
- const { servicesRemoved, servicesUpdates, preimages, storage, ...rest } = update;
10491
+ const { removed, created: _, updated, preimages, storage, ...rest } = update;
10491
10492
  // just assign all other variables
10492
10493
  Object.assign(this, rest);
10493
10494
  // and update the services state
10494
10495
  let result;
10495
- result = this.updateServices(servicesUpdates);
10496
+ result = this.updateServices(updated);
10496
10497
  if (result.isError) {
10497
10498
  return result;
10498
10499
  }
@@ -10504,7 +10505,7 @@ class in_memory_state_InMemoryState extends WithDebug {
10504
10505
  if (result.isError) {
10505
10506
  return result;
10506
10507
  }
10507
- this.removeServices(servicesRemoved);
10508
+ this.removeServices(removed);
10508
10509
  return result_Result.ok(result_OK);
10509
10510
  }
10510
10511
  removeServices(servicesRemoved) {
@@ -10513,89 +10514,102 @@ class in_memory_state_InMemoryState extends WithDebug {
10513
10514
  this.services.delete(serviceId);
10514
10515
  }
10515
10516
  }
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 `
10517
+ updateStorage(storageUpdates) {
10518
+ if (storageUpdates === undefined) {
10519
+ return result_Result.ok(result_OK);
10520
+ }
10521
+ for (const [serviceId, updates] of storageUpdates.entries()) {
10522
+ for (const update of updates) {
10523
+ const { kind } = update.action;
10524
+ const service = this.services.get(serviceId);
10525
+ if (service === undefined) {
10526
+ return result_Result.error(in_memory_state_UpdateError.NoService, () => `Attempting to update storage of non-existing service: ${serviceId}`);
10527
+ }
10528
+ if (kind === UpdateStorageKind.Set) {
10529
+ const { key, value } = update.action.storage;
10530
+ service.data.storage.set(key.toString(), StorageItem.create({ key, value }));
10531
+ }
10532
+ else if (kind === UpdateStorageKind.Remove) {
10533
+ const { key } = update.action;
10534
+ debug_check `
10530
10535
  ${service.data.storage.has(key.toString())}
10531
- Attempting to remove non-existing storage item at ${serviceId}: ${action.key}
10536
+ Attempting to remove non-existing storage item at ${serviceId}: ${update.action.key}
10532
10537
  `;
10533
- service.data.storage.delete(key.toString());
10534
- }
10535
- else {
10536
- debug_assertNever(kind);
10538
+ service.data.storage.delete(key.toString());
10539
+ }
10540
+ else {
10541
+ debug_assertNever(kind);
10542
+ }
10537
10543
  }
10538
10544
  }
10539
10545
  return result_Result.ok(result_OK);
10540
10546
  }
10541
- updatePreimages(preimages) {
10542
- for (const { serviceId, action } of preimages ?? []) {
10547
+ updatePreimages(preimagesUpdates) {
10548
+ if (preimagesUpdates === undefined) {
10549
+ return result_Result.ok(result_OK);
10550
+ }
10551
+ for (const [serviceId, updates] of preimagesUpdates.entries()) {
10543
10552
  const service = this.services.get(serviceId);
10544
10553
  if (service === undefined) {
10545
- return result_Result.error(in_memory_state_UpdateError.NoService, `Attempting to update preimage of non-existing service: ${serviceId}`);
10546
- }
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]);
10554
+ return result_Result.error(in_memory_state_UpdateError.NoService, () => `Attempting to update preimage of non-existing service: ${serviceId}`);
10555
+ }
10556
+ for (const update of updates) {
10557
+ const { kind } = update.action;
10558
+ if (kind === UpdatePreimageKind.Provide) {
10559
+ const { preimage, slot } = update.action;
10560
+ if (service.data.preimages.has(preimage.hash)) {
10561
+ return result_Result.error(in_memory_state_UpdateError.PreimageExists, () => `Overwriting existing preimage at ${serviceId}: ${preimage}`);
10561
10562
  }
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);
10563
+ service.data.preimages.set(preimage.hash, preimage);
10564
+ if (slot !== null) {
10565
+ const lookupHistory = service.data.lookupHistory.get(preimage.hash);
10566
+ const length = numbers_tryAsU32(preimage.blob.length);
10567
+ const lookup = new service_LookupHistoryItem(preimage.hash, length, tryAsLookupHistorySlots([slot]));
10568
+ if (lookupHistory === undefined) {
10569
+ // no lookup history for that preimage at all (edge case, should be requested)
10570
+ service.data.lookupHistory.set(preimage.hash, [lookup]);
10571
+ }
10572
+ else {
10573
+ // insert or replace exiting entry
10574
+ const index = lookupHistory.map((x) => x.length).indexOf(length);
10575
+ lookupHistory.splice(index, index === -1 ? 0 : 1, lookup);
10576
+ }
10566
10577
  }
10567
10578
  }
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);
10579
+ else if (kind === UpdatePreimageKind.Remove) {
10580
+ const { hash, length } = update.action;
10581
+ service.data.preimages.delete(hash);
10582
+ const history = service.data.lookupHistory.get(hash) ?? [];
10583
+ const idx = history.map((x) => x.length).indexOf(length);
10584
+ if (idx !== -1) {
10585
+ history.splice(idx, 1);
10586
+ }
10587
+ }
10588
+ else if (kind === UpdatePreimageKind.UpdateOrAdd) {
10589
+ const { item } = update.action;
10590
+ const history = service.data.lookupHistory.get(item.hash) ?? [];
10591
+ const existingIdx = history.map((x) => x.length).indexOf(item.length);
10592
+ const removeCount = existingIdx === -1 ? 0 : 1;
10593
+ history.splice(existingIdx, removeCount, item);
10594
+ service.data.lookupHistory.set(item.hash, history);
10595
+ }
10596
+ else {
10597
+ debug_assertNever(kind);
10576
10598
  }
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
10599
  }
10589
10600
  }
10590
10601
  return result_Result.ok(result_OK);
10591
10602
  }
10592
10603
  updateServices(servicesUpdates) {
10593
- for (const { serviceId, action } of servicesUpdates ?? []) {
10594
- const { kind, account } = action;
10604
+ if (servicesUpdates === undefined) {
10605
+ return result_Result.ok(result_OK);
10606
+ }
10607
+ for (const [serviceId, update] of servicesUpdates.entries()) {
10608
+ const { kind, account } = update.action;
10595
10609
  if (kind === UpdateServiceKind.Create) {
10596
- const { lookupHistory } = action;
10610
+ const { lookupHistory } = update.action;
10597
10611
  if (this.services.has(serviceId)) {
10598
- return result_Result.error(in_memory_state_UpdateError.DuplicateService, `${serviceId} already exists!`);
10612
+ return result_Result.error(in_memory_state_UpdateError.DuplicateService, () => `${serviceId} already exists!`);
10599
10613
  }
10600
10614
  this.services.set(serviceId, new InMemoryService(serviceId, {
10601
10615
  info: account,
@@ -10607,7 +10621,7 @@ class in_memory_state_InMemoryState extends WithDebug {
10607
10621
  else if (kind === UpdateServiceKind.Update) {
10608
10622
  const existingService = this.services.get(serviceId);
10609
10623
  if (existingService === undefined) {
10610
- return result_Result.error(in_memory_state_UpdateError.NoService, `Cannot update ${serviceId} because it does not exist.`);
10624
+ return result_Result.error(in_memory_state_UpdateError.NoService, () => `Cannot update ${serviceId} because it does not exist.`);
10611
10625
  }
10612
10626
  existingService.data.info = account;
10613
10627
  }
@@ -11854,7 +11868,6 @@ function getKeccakTrieHasher(hasher) {
11854
11868
 
11855
11869
 
11856
11870
 
11857
-
11858
11871
  /** What should be done with that key? */
11859
11872
  var StateEntryUpdateAction;
11860
11873
  (function (StateEntryUpdateAction) {
@@ -11870,76 +11883,88 @@ function* serializeStateUpdate(spec, blake2b, update) {
11870
11883
  yield* serializeBasicKeys(spec, update);
11871
11884
  const encode = (codec, val) => encoder_Encoder.encodeObject(codec, val, spec);
11872
11885
  // then let's proceed with service updates
11873
- yield* serializeServiceUpdates(update.servicesUpdates, encode, blake2b);
11886
+ yield* serializeServiceUpdates(update.updated, encode, blake2b);
11874
11887
  yield* serializePreimages(update.preimages, encode, blake2b);
11875
11888
  yield* serializeStorage(update.storage, blake2b);
11876
- yield* serializeRemovedServices(update.servicesRemoved);
11889
+ yield* serializeRemovedServices(update.removed);
11877
11890
  }
11878
11891
  function* serializeRemovedServices(servicesRemoved) {
11879
- for (const serviceId of servicesRemoved ?? []) {
11892
+ if (servicesRemoved === undefined) {
11893
+ return;
11894
+ }
11895
+ for (const serviceId of servicesRemoved) {
11880
11896
  // TODO [ToDr] what about all data associated with a service?
11881
11897
  const codec = serialize_serialize.serviceData(serviceId);
11882
11898
  yield [StateEntryUpdateAction.Remove, codec.key, EMPTY_BLOB];
11883
11899
  }
11884
11900
  }
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;
11901
+ function* serializeStorage(storageUpdates, blake2b) {
11902
+ if (storageUpdates === undefined) {
11903
+ return;
11904
+ }
11905
+ for (const [serviceId, updates] of storageUpdates.entries()) {
11906
+ for (const { action } of updates) {
11907
+ switch (action.kind) {
11908
+ case UpdateStorageKind.Set: {
11909
+ const key = action.storage.key;
11910
+ const codec = serialize_serialize.serviceStorage(blake2b, serviceId, key);
11911
+ yield [StateEntryUpdateAction.Insert, codec.key, action.storage.value];
11912
+ break;
11913
+ }
11914
+ case UpdateStorageKind.Remove: {
11915
+ const key = action.key;
11916
+ const codec = serialize_serialize.serviceStorage(blake2b, serviceId, key);
11917
+ yield [StateEntryUpdateAction.Remove, codec.key, EMPTY_BLOB];
11918
+ break;
11919
+ }
11899
11920
  }
11900
- default:
11901
- debug_assertNever(action);
11902
11921
  }
11903
11922
  }
11904
11923
  }
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
- ];
11924
+ function* serializePreimages(preimagesUpdates, encode, blake2b) {
11925
+ if (preimagesUpdates === undefined) {
11926
+ return;
11927
+ }
11928
+ for (const [serviceId, updates] of preimagesUpdates.entries()) {
11929
+ for (const { action } of updates) {
11930
+ switch (action.kind) {
11931
+ case UpdatePreimageKind.Provide: {
11932
+ const { hash, blob } = action.preimage;
11933
+ const codec = serialize_serialize.servicePreimages(blake2b, serviceId, hash);
11934
+ yield [StateEntryUpdateAction.Insert, codec.key, blob];
11935
+ if (action.slot !== null) {
11936
+ const codec2 = serialize_serialize.serviceLookupHistory(blake2b, serviceId, hash, numbers_tryAsU32(blob.length));
11937
+ yield [
11938
+ StateEntryUpdateAction.Insert,
11939
+ codec2.key,
11940
+ encode(codec2.Codec, tryAsLookupHistorySlots([action.slot])),
11941
+ ];
11942
+ }
11943
+ break;
11944
+ }
11945
+ case UpdatePreimageKind.UpdateOrAdd: {
11946
+ const { hash, length, slots } = action.item;
11947
+ const codec = serialize_serialize.serviceLookupHistory(blake2b, serviceId, hash, length);
11948
+ yield [StateEntryUpdateAction.Insert, codec.key, encode(codec.Codec, slots)];
11949
+ break;
11950
+ }
11951
+ case UpdatePreimageKind.Remove: {
11952
+ const { hash, length } = action;
11953
+ const codec = serialize_serialize.servicePreimages(blake2b, serviceId, hash);
11954
+ yield [StateEntryUpdateAction.Remove, codec.key, EMPTY_BLOB];
11955
+ const codec2 = serialize_serialize.serviceLookupHistory(blake2b, serviceId, hash, length);
11956
+ yield [StateEntryUpdateAction.Remove, codec2.key, EMPTY_BLOB];
11957
+ break;
11919
11958
  }
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
11959
  }
11936
- default:
11937
- debug_assertNever(action);
11938
11960
  }
11939
11961
  }
11940
11962
  }
11941
11963
  function* serializeServiceUpdates(servicesUpdates, encode, blake2b) {
11942
- for (const { action, serviceId } of servicesUpdates ?? []) {
11964
+ if (servicesUpdates === undefined) {
11965
+ return;
11966
+ }
11967
+ for (const [serviceId, { action }] of servicesUpdates.entries()) {
11943
11968
  // new service being created or updated
11944
11969
  const codec = serialize_serialize.serviceData(serviceId);
11945
11970
  yield [StateEntryUpdateAction.Insert, codec.key, encode(codec.Codec, action.account)];
@@ -12226,13 +12251,13 @@ class LeafDb {
12226
12251
  */
12227
12252
  static fromLeavesBlob(blob, db) {
12228
12253
  if (blob.length % TRIE_NODE_BYTES !== 0) {
12229
- return result_Result.error(LeafDbError.InvalidLeafData, `${blob.length} is not a multiply of ${TRIE_NODE_BYTES}: ${blob}`);
12254
+ return result_Result.error(LeafDbError.InvalidLeafData, () => `${blob.length} is not a multiply of ${TRIE_NODE_BYTES}: ${blob}`);
12230
12255
  }
12231
12256
  const leaves = SortedSet.fromArray(leafComparator, []);
12232
12257
  for (const nodeData of blob.chunks(TRIE_NODE_BYTES)) {
12233
12258
  const node = new TrieNode(nodeData.raw);
12234
12259
  if (node.getNodeType() === NodeType.Branch) {
12235
- return result_Result.error(LeafDbError.InvalidLeafData, `Branch node detected: ${nodeData}`);
12260
+ return result_Result.error(LeafDbError.InvalidLeafData, () => `Branch node detected: ${nodeData}`);
12236
12261
  }
12237
12262
  leaves.insert(node.asLeafNode());
12238
12263
  }
@@ -12951,7 +12976,7 @@ class LmdbStates {
12951
12976
  }
12952
12977
  catch (e) {
12953
12978
  logger.error `${e}`;
12954
- return result_Result.error(StateUpdateError.Commit);
12979
+ return result_Result.error(StateUpdateError.Commit, () => `Failed to commit state update: ${e}`);
12955
12980
  }
12956
12981
  return result_Result.ok(result_OK);
12957
12982
  }
@@ -13750,32 +13775,33 @@ class Preimages {
13750
13775
  }
13751
13776
  if (prevPreimage.requester > currPreimage.requester ||
13752
13777
  currPreimage.blob.compare(prevPreimage.blob).isLessOrEqual()) {
13753
- return Result.error(PreimagesErrorCode.PreimagesNotSortedUnique);
13778
+ return Result.error(PreimagesErrorCode.PreimagesNotSortedUnique, () => `Preimages not sorted/unique at index ${i}`);
13754
13779
  }
13755
13780
  }
13756
13781
  const { preimages, slot } = input;
13757
- const pendingChanges = [];
13782
+ const pendingChanges = new Map();
13758
13783
  // select preimages for integration
13759
13784
  for (const preimage of preimages) {
13760
13785
  const { requester, blob } = preimage;
13761
13786
  const hash = this.blake2b.hashBytes(blob).asOpaque();
13762
13787
  const service = this.state.getService(requester);
13763
13788
  if (service === null) {
13764
- return Result.error(PreimagesErrorCode.AccountNotFound);
13789
+ return Result.error(PreimagesErrorCode.AccountNotFound, () => `Service not found: ${requester}`);
13765
13790
  }
13766
13791
  const hasPreimage = service.hasPreimage(hash);
13767
13792
  const slots = service.getLookupHistory(hash, tryAsU32(blob.length));
13768
13793
  // https://graypaper.fluffylabs.dev/#/5f542d7/181800181900
13769
13794
  // https://graypaper.fluffylabs.dev/#/5f542d7/116f0011a500
13770
13795
  if (hasPreimage || slots === null || !LookupHistoryItem.isRequested(slots)) {
13771
- return Result.error(PreimagesErrorCode.PreimageUnneeded);
13796
+ return Result.error(PreimagesErrorCode.PreimageUnneeded, () => `Preimage unneeded: requester=${requester}, hash=${hash}, hasPreimage=${hasPreimage}, isRequested=${slots !== null && LookupHistoryItem.isRequested(slots)}`);
13772
13797
  }
13773
13798
  // https://graypaper.fluffylabs.dev/#/5f542d7/18c00018f300
13774
- pendingChanges.push(UpdatePreimage.provide({
13775
- serviceId: requester,
13799
+ const updates = pendingChanges.get(requester) ?? [];
13800
+ updates.push(UpdatePreimage.provide({
13776
13801
  preimage: PreimageItem.create({ hash, blob }),
13777
13802
  slot,
13778
13803
  }));
13804
+ pendingChanges.set(requester, updates);
13779
13805
  }
13780
13806
  return Result.ok({
13781
13807
  preimages: pendingChanges,