@typeberry/jam 0.2.0-c3df163 → 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
  }
@@ -10171,31 +10177,29 @@ var UpdatePreimageKind;
10171
10177
  * 3. Update `LookupHistory` with given value.
10172
10178
  */
10173
10179
  class state_update_UpdatePreimage {
10174
- serviceId;
10175
10180
  action;
10176
- constructor(serviceId, action) {
10177
- this.serviceId = serviceId;
10181
+ constructor(action) {
10178
10182
  this.action = action;
10179
10183
  }
10180
10184
  /** A preimage is provided. We should update the lookuphistory and add the preimage to db. */
10181
- static provide({ serviceId, preimage, slot, }) {
10182
- return new state_update_UpdatePreimage(serviceId, {
10185
+ static provide({ preimage, slot }) {
10186
+ return new state_update_UpdatePreimage({
10183
10187
  kind: UpdatePreimageKind.Provide,
10184
10188
  preimage,
10185
10189
  slot,
10186
10190
  });
10187
10191
  }
10188
10192
  /** The preimage should be removed completely from the database. */
10189
- static remove({ serviceId, hash, length }) {
10190
- return new state_update_UpdatePreimage(serviceId, {
10193
+ static remove({ hash, length }) {
10194
+ return new state_update_UpdatePreimage({
10191
10195
  kind: UpdatePreimageKind.Remove,
10192
10196
  hash,
10193
10197
  length,
10194
10198
  });
10195
10199
  }
10196
10200
  /** Update the lookup history of some preimage or add a new one (request). */
10197
- static updateOrAdd({ serviceId, lookupHistory }) {
10198
- return new state_update_UpdatePreimage(serviceId, {
10201
+ static updateOrAdd({ lookupHistory }) {
10202
+ return new state_update_UpdatePreimage({
10199
10203
  kind: UpdatePreimageKind.UpdateOrAdd,
10200
10204
  item: lookupHistory,
10201
10205
  });
@@ -10232,23 +10236,21 @@ var UpdateServiceKind;
10232
10236
  UpdateServiceKind[UpdateServiceKind["Create"] = 1] = "Create";
10233
10237
  })(UpdateServiceKind || (UpdateServiceKind = {}));
10234
10238
  /**
10235
- * Update service info of a particular `ServiceId` or create a new one.
10239
+ * Update service info or create a new one.
10236
10240
  */
10237
10241
  class UpdateService {
10238
- serviceId;
10239
10242
  action;
10240
- constructor(serviceId, action) {
10241
- this.serviceId = serviceId;
10243
+ constructor(action) {
10242
10244
  this.action = action;
10243
10245
  }
10244
- static update({ serviceId, serviceInfo }) {
10245
- return new UpdateService(serviceId, {
10246
+ static update({ serviceInfo }) {
10247
+ return new UpdateService({
10246
10248
  kind: UpdateServiceKind.Update,
10247
10249
  account: serviceInfo,
10248
10250
  });
10249
10251
  }
10250
- static create({ serviceId, serviceInfo, lookupHistory, }) {
10251
- return new UpdateService(serviceId, {
10252
+ static create({ serviceInfo, lookupHistory, }) {
10253
+ return new UpdateService({
10252
10254
  kind: UpdateServiceKind.Create,
10253
10255
  account: serviceInfo,
10254
10256
  lookupHistory,
@@ -10269,17 +10271,15 @@ var UpdateStorageKind;
10269
10271
  * Can either create/modify an entry or remove it.
10270
10272
  */
10271
10273
  class UpdateStorage {
10272
- serviceId;
10273
10274
  action;
10274
- constructor(serviceId, action) {
10275
- this.serviceId = serviceId;
10275
+ constructor(action) {
10276
10276
  this.action = action;
10277
10277
  }
10278
- static set({ serviceId, storage }) {
10279
- return new UpdateStorage(serviceId, { kind: UpdateStorageKind.Set, storage });
10278
+ static set({ storage }) {
10279
+ return new UpdateStorage({ kind: UpdateStorageKind.Set, storage });
10280
10280
  }
10281
- static remove({ serviceId, key }) {
10282
- return new UpdateStorage(serviceId, { kind: UpdateStorageKind.Remove, key });
10281
+ static remove({ key }) {
10282
+ return new UpdateStorage({ kind: UpdateStorageKind.Remove, key });
10283
10283
  }
10284
10284
  get key() {
10285
10285
  if (this.action.kind === UpdateStorageKind.Remove) {
@@ -10488,12 +10488,12 @@ class in_memory_state_InMemoryState extends WithDebug {
10488
10488
  * Modify the state and apply a single state update.
10489
10489
  */
10490
10490
  applyUpdate(update) {
10491
- const { servicesRemoved, servicesUpdates, preimages, storage, ...rest } = update;
10491
+ const { removed, created: _, updated, preimages, storage, ...rest } = update;
10492
10492
  // just assign all other variables
10493
10493
  Object.assign(this, rest);
10494
10494
  // and update the services state
10495
10495
  let result;
10496
- result = this.updateServices(servicesUpdates);
10496
+ result = this.updateServices(updated);
10497
10497
  if (result.isError) {
10498
10498
  return result;
10499
10499
  }
@@ -10505,7 +10505,7 @@ class in_memory_state_InMemoryState extends WithDebug {
10505
10505
  if (result.isError) {
10506
10506
  return result;
10507
10507
  }
10508
- this.removeServices(servicesRemoved);
10508
+ this.removeServices(removed);
10509
10509
  return result_Result.ok(result_OK);
10510
10510
  }
10511
10511
  removeServices(servicesRemoved) {
@@ -10514,89 +10514,102 @@ class in_memory_state_InMemoryState extends WithDebug {
10514
10514
  this.services.delete(serviceId);
10515
10515
  }
10516
10516
  }
10517
- updateStorage(storage) {
10518
- for (const { serviceId, action } of storage ?? []) {
10519
- const { kind } = action;
10520
- const service = this.services.get(serviceId);
10521
- if (service === undefined) {
10522
- return result_Result.error(in_memory_state_UpdateError.NoService, `Attempting to update storage of non-existing service: ${serviceId}`);
10523
- }
10524
- if (kind === UpdateStorageKind.Set) {
10525
- const { key, value } = action.storage;
10526
- service.data.storage.set(key.toString(), StorageItem.create({ key, value }));
10527
- }
10528
- else if (kind === UpdateStorageKind.Remove) {
10529
- const { key } = action;
10530
- 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 `
10531
10535
  ${service.data.storage.has(key.toString())}
10532
- Attempting to remove non-existing storage item at ${serviceId}: ${action.key}
10536
+ Attempting to remove non-existing storage item at ${serviceId}: ${update.action.key}
10533
10537
  `;
10534
- service.data.storage.delete(key.toString());
10535
- }
10536
- else {
10537
- debug_assertNever(kind);
10538
+ service.data.storage.delete(key.toString());
10539
+ }
10540
+ else {
10541
+ debug_assertNever(kind);
10542
+ }
10538
10543
  }
10539
10544
  }
10540
10545
  return result_Result.ok(result_OK);
10541
10546
  }
10542
- updatePreimages(preimages) {
10543
- 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()) {
10544
10552
  const service = this.services.get(serviceId);
10545
10553
  if (service === undefined) {
10546
- return result_Result.error(in_memory_state_UpdateError.NoService, `Attempting to update preimage of non-existing service: ${serviceId}`);
10547
- }
10548
- const { kind } = action;
10549
- if (kind === UpdatePreimageKind.Provide) {
10550
- const { preimage, slot } = action;
10551
- if (service.data.preimages.has(preimage.hash)) {
10552
- return result_Result.error(in_memory_state_UpdateError.PreimageExists, `Overwriting existing preimage at ${serviceId}: ${preimage}`);
10553
- }
10554
- service.data.preimages.set(preimage.hash, preimage);
10555
- if (slot !== null) {
10556
- const lookupHistory = service.data.lookupHistory.get(preimage.hash);
10557
- const length = numbers_tryAsU32(preimage.blob.length);
10558
- const lookup = new service_LookupHistoryItem(preimage.hash, length, tryAsLookupHistorySlots([slot]));
10559
- if (lookupHistory === undefined) {
10560
- // no lookup history for that preimage at all (edge case, should be requested)
10561
- 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}`);
10562
10562
  }
10563
- else {
10564
- // insert or replace exiting entry
10565
- const index = lookupHistory.map((x) => x.length).indexOf(length);
10566
- 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
+ }
10567
10577
  }
10568
10578
  }
10569
- }
10570
- else if (kind === UpdatePreimageKind.Remove) {
10571
- const { hash, length } = action;
10572
- service.data.preimages.delete(hash);
10573
- const history = service.data.lookupHistory.get(hash) ?? [];
10574
- const idx = history.map((x) => x.length).indexOf(length);
10575
- if (idx !== -1) {
10576
- 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);
10577
10598
  }
10578
- }
10579
- else if (kind === UpdatePreimageKind.UpdateOrAdd) {
10580
- const { item } = action;
10581
- const history = service.data.lookupHistory.get(item.hash) ?? [];
10582
- const existingIdx = history.map((x) => x.length).indexOf(item.length);
10583
- const removeCount = existingIdx === -1 ? 0 : 1;
10584
- history.splice(existingIdx, removeCount, item);
10585
- service.data.lookupHistory.set(item.hash, history);
10586
- }
10587
- else {
10588
- debug_assertNever(kind);
10589
10599
  }
10590
10600
  }
10591
10601
  return result_Result.ok(result_OK);
10592
10602
  }
10593
10603
  updateServices(servicesUpdates) {
10594
- for (const { serviceId, action } of servicesUpdates ?? []) {
10595
- 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;
10596
10609
  if (kind === UpdateServiceKind.Create) {
10597
- const { lookupHistory } = action;
10610
+ const { lookupHistory } = update.action;
10598
10611
  if (this.services.has(serviceId)) {
10599
- 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!`);
10600
10613
  }
10601
10614
  this.services.set(serviceId, new InMemoryService(serviceId, {
10602
10615
  info: account,
@@ -10608,7 +10621,7 @@ class in_memory_state_InMemoryState extends WithDebug {
10608
10621
  else if (kind === UpdateServiceKind.Update) {
10609
10622
  const existingService = this.services.get(serviceId);
10610
10623
  if (existingService === undefined) {
10611
- 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.`);
10612
10625
  }
10613
10626
  existingService.data.info = account;
10614
10627
  }
@@ -11855,7 +11868,6 @@ function getKeccakTrieHasher(hasher) {
11855
11868
 
11856
11869
 
11857
11870
 
11858
-
11859
11871
  /** What should be done with that key? */
11860
11872
  var StateEntryUpdateAction;
11861
11873
  (function (StateEntryUpdateAction) {
@@ -11871,76 +11883,88 @@ function* serializeStateUpdate(spec, blake2b, update) {
11871
11883
  yield* serializeBasicKeys(spec, update);
11872
11884
  const encode = (codec, val) => encoder_Encoder.encodeObject(codec, val, spec);
11873
11885
  // then let's proceed with service updates
11874
- yield* serializeServiceUpdates(update.servicesUpdates, encode, blake2b);
11886
+ yield* serializeServiceUpdates(update.updated, encode, blake2b);
11875
11887
  yield* serializePreimages(update.preimages, encode, blake2b);
11876
11888
  yield* serializeStorage(update.storage, blake2b);
11877
- yield* serializeRemovedServices(update.servicesRemoved);
11889
+ yield* serializeRemovedServices(update.removed);
11878
11890
  }
11879
11891
  function* serializeRemovedServices(servicesRemoved) {
11880
- for (const serviceId of servicesRemoved ?? []) {
11892
+ if (servicesRemoved === undefined) {
11893
+ return;
11894
+ }
11895
+ for (const serviceId of servicesRemoved) {
11881
11896
  // TODO [ToDr] what about all data associated with a service?
11882
11897
  const codec = serialize_serialize.serviceData(serviceId);
11883
11898
  yield [StateEntryUpdateAction.Remove, codec.key, EMPTY_BLOB];
11884
11899
  }
11885
11900
  }
11886
- function* serializeStorage(storage, blake2b) {
11887
- for (const { action, serviceId } of storage ?? []) {
11888
- switch (action.kind) {
11889
- case UpdateStorageKind.Set: {
11890
- const key = action.storage.key;
11891
- const codec = serialize_serialize.serviceStorage(blake2b, serviceId, key);
11892
- yield [StateEntryUpdateAction.Insert, codec.key, action.storage.value];
11893
- break;
11894
- }
11895
- case UpdateStorageKind.Remove: {
11896
- const key = action.key;
11897
- const codec = serialize_serialize.serviceStorage(blake2b, serviceId, key);
11898
- yield [StateEntryUpdateAction.Remove, codec.key, EMPTY_BLOB];
11899
- 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
+ }
11900
11920
  }
11901
- default:
11902
- debug_assertNever(action);
11903
11921
  }
11904
11922
  }
11905
11923
  }
11906
- function* serializePreimages(preimages, encode, blake2b) {
11907
- for (const { action, serviceId } of preimages ?? []) {
11908
- switch (action.kind) {
11909
- case UpdatePreimageKind.Provide: {
11910
- const { hash, blob } = action.preimage;
11911
- const codec = serialize_serialize.servicePreimages(blake2b, serviceId, hash);
11912
- yield [StateEntryUpdateAction.Insert, codec.key, blob];
11913
- if (action.slot !== null) {
11914
- const codec2 = serialize_serialize.serviceLookupHistory(blake2b, serviceId, hash, numbers_tryAsU32(blob.length));
11915
- yield [
11916
- StateEntryUpdateAction.Insert,
11917
- codec2.key,
11918
- encode(codec2.Codec, tryAsLookupHistorySlots([action.slot])),
11919
- ];
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;
11920
11958
  }
11921
- break;
11922
- }
11923
- case UpdatePreimageKind.UpdateOrAdd: {
11924
- const { hash, length, slots } = action.item;
11925
- const codec = serialize_serialize.serviceLookupHistory(blake2b, serviceId, hash, length);
11926
- yield [StateEntryUpdateAction.Insert, codec.key, encode(codec.Codec, slots)];
11927
- break;
11928
- }
11929
- case UpdatePreimageKind.Remove: {
11930
- const { hash, length } = action;
11931
- const codec = serialize_serialize.servicePreimages(blake2b, serviceId, hash);
11932
- yield [StateEntryUpdateAction.Remove, codec.key, EMPTY_BLOB];
11933
- const codec2 = serialize_serialize.serviceLookupHistory(blake2b, serviceId, hash, length);
11934
- yield [StateEntryUpdateAction.Remove, codec2.key, EMPTY_BLOB];
11935
- break;
11936
11959
  }
11937
- default:
11938
- debug_assertNever(action);
11939
11960
  }
11940
11961
  }
11941
11962
  }
11942
11963
  function* serializeServiceUpdates(servicesUpdates, encode, blake2b) {
11943
- for (const { action, serviceId } of servicesUpdates ?? []) {
11964
+ if (servicesUpdates === undefined) {
11965
+ return;
11966
+ }
11967
+ for (const [serviceId, { action }] of servicesUpdates.entries()) {
11944
11968
  // new service being created or updated
11945
11969
  const codec = serialize_serialize.serviceData(serviceId);
11946
11970
  yield [StateEntryUpdateAction.Insert, codec.key, encode(codec.Codec, action.account)];
@@ -12227,13 +12251,13 @@ class LeafDb {
12227
12251
  */
12228
12252
  static fromLeavesBlob(blob, db) {
12229
12253
  if (blob.length % TRIE_NODE_BYTES !== 0) {
12230
- 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}`);
12231
12255
  }
12232
12256
  const leaves = SortedSet.fromArray(leafComparator, []);
12233
12257
  for (const nodeData of blob.chunks(TRIE_NODE_BYTES)) {
12234
12258
  const node = new TrieNode(nodeData.raw);
12235
12259
  if (node.getNodeType() === NodeType.Branch) {
12236
- return result_Result.error(LeafDbError.InvalidLeafData, `Branch node detected: ${nodeData}`);
12260
+ return result_Result.error(LeafDbError.InvalidLeafData, () => `Branch node detected: ${nodeData}`);
12237
12261
  }
12238
12262
  leaves.insert(node.asLeafNode());
12239
12263
  }
@@ -12952,7 +12976,7 @@ class LmdbStates {
12952
12976
  }
12953
12977
  catch (e) {
12954
12978
  logger.error `${e}`;
12955
- return result_Result.error(StateUpdateError.Commit);
12979
+ return result_Result.error(StateUpdateError.Commit, () => `Failed to commit state update: ${e}`);
12956
12980
  }
12957
12981
  return result_Result.ok(result_OK);
12958
12982
  }
@@ -13751,32 +13775,33 @@ class Preimages {
13751
13775
  }
13752
13776
  if (prevPreimage.requester > currPreimage.requester ||
13753
13777
  currPreimage.blob.compare(prevPreimage.blob).isLessOrEqual()) {
13754
- return Result.error(PreimagesErrorCode.PreimagesNotSortedUnique);
13778
+ return Result.error(PreimagesErrorCode.PreimagesNotSortedUnique, () => `Preimages not sorted/unique at index ${i}`);
13755
13779
  }
13756
13780
  }
13757
13781
  const { preimages, slot } = input;
13758
- const pendingChanges = [];
13782
+ const pendingChanges = new Map();
13759
13783
  // select preimages for integration
13760
13784
  for (const preimage of preimages) {
13761
13785
  const { requester, blob } = preimage;
13762
13786
  const hash = this.blake2b.hashBytes(blob).asOpaque();
13763
13787
  const service = this.state.getService(requester);
13764
13788
  if (service === null) {
13765
- return Result.error(PreimagesErrorCode.AccountNotFound);
13789
+ return Result.error(PreimagesErrorCode.AccountNotFound, () => `Service not found: ${requester}`);
13766
13790
  }
13767
13791
  const hasPreimage = service.hasPreimage(hash);
13768
13792
  const slots = service.getLookupHistory(hash, tryAsU32(blob.length));
13769
13793
  // https://graypaper.fluffylabs.dev/#/5f542d7/181800181900
13770
13794
  // https://graypaper.fluffylabs.dev/#/5f542d7/116f0011a500
13771
13795
  if (hasPreimage || slots === null || !LookupHistoryItem.isRequested(slots)) {
13772
- 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)}`);
13773
13797
  }
13774
13798
  // https://graypaper.fluffylabs.dev/#/5f542d7/18c00018f300
13775
- pendingChanges.push(UpdatePreimage.provide({
13776
- serviceId: requester,
13799
+ const updates = pendingChanges.get(requester) ?? [];
13800
+ updates.push(UpdatePreimage.provide({
13777
13801
  preimage: PreimageItem.create({ hash, blob }),
13778
13802
  slot,
13779
13803
  }));
13804
+ pendingChanges.set(requester, updates);
13780
13805
  }
13781
13806
  return Result.ok({
13782
13807
  preimages: pendingChanges,