@typeberry/lib 0.2.0-74f246e → 0.2.0-adde0dd

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.
Files changed (4) hide show
  1. package/index.cjs +241 -200
  2. package/index.d.ts +1014 -953
  3. package/index.js +241 -200
  4. package/package.json +1 -1
package/index.cjs CHANGED
@@ -304,7 +304,7 @@ function resultToString(res) {
304
304
  if (res.isOk) {
305
305
  return `OK: ${typeof res.ok === "symbol" ? res.ok.toString() : res.ok}`;
306
306
  }
307
- return `${res.details}\nError: ${maybeTaggedErrorToString(res.error)}`;
307
+ return `${res.details()}\nError: ${maybeTaggedErrorToString(res.error)}`;
308
308
  }
309
309
  /** An indication of two possible outcomes returned from a function. */
310
310
  const Result$1 = {
@@ -318,7 +318,7 @@ const Result$1 = {
318
318
  };
319
319
  },
320
320
  /** Create new [`Result`] with `Error` status. */
321
- error: (error, details = "") => {
321
+ error: (error, details) => {
322
322
  check `${error !== undefined} 'Error' type cannot be undefined.`;
323
323
  return {
324
324
  isOk: false,
@@ -431,7 +431,7 @@ function deepEqual(actual, expected, { context = [], errorsCollector, ignore = [
431
431
  }
432
432
  if (actual.isError && expected.isError) {
433
433
  deepEqual(actual.error, expected.error, { context: ctx.concat(["error"]), errorsCollector: errors, ignore });
434
- deepEqual(actual.details, expected.details, {
434
+ deepEqual(actual.details(), expected.details(), {
435
435
  context: ctx.concat(["details"]),
436
436
  errorsCollector: errors,
437
437
  // display details when error does not match
@@ -8072,6 +8072,8 @@ function accumulationOutputComparator(a, b) {
8072
8072
  const O = 8;
8073
8073
  /** `Q`: The number of items in the authorizations queue. */
8074
8074
  const Q = 80;
8075
+ /** `W_B`: The maximum size of the concatenated variable-size blobs, extrinsics and imported segments of a work-package, in octets */
8076
+ Compatibility.isGreaterOrEqual(GpVersion.V0_7_2) ? 13_791_360 : 13_794_305;
8075
8077
  /** `W_T`: The size of a transfer memo in octets. */
8076
8078
  const W_T = 128;
8077
8079
  /**
@@ -9062,31 +9064,29 @@ var UpdatePreimageKind;
9062
9064
  * 3. Update `LookupHistory` with given value.
9063
9065
  */
9064
9066
  class UpdatePreimage {
9065
- serviceId;
9066
9067
  action;
9067
- constructor(serviceId, action) {
9068
- this.serviceId = serviceId;
9068
+ constructor(action) {
9069
9069
  this.action = action;
9070
9070
  }
9071
9071
  /** A preimage is provided. We should update the lookuphistory and add the preimage to db. */
9072
- static provide({ serviceId, preimage, slot, }) {
9073
- return new UpdatePreimage(serviceId, {
9072
+ static provide({ preimage, slot }) {
9073
+ return new UpdatePreimage({
9074
9074
  kind: UpdatePreimageKind.Provide,
9075
9075
  preimage,
9076
9076
  slot,
9077
9077
  });
9078
9078
  }
9079
9079
  /** The preimage should be removed completely from the database. */
9080
- static remove({ serviceId, hash, length }) {
9081
- return new UpdatePreimage(serviceId, {
9080
+ static remove({ hash, length }) {
9081
+ return new UpdatePreimage({
9082
9082
  kind: UpdatePreimageKind.Remove,
9083
9083
  hash,
9084
9084
  length,
9085
9085
  });
9086
9086
  }
9087
9087
  /** Update the lookup history of some preimage or add a new one (request). */
9088
- static updateOrAdd({ serviceId, lookupHistory }) {
9089
- return new UpdatePreimage(serviceId, {
9088
+ static updateOrAdd({ lookupHistory }) {
9089
+ return new UpdatePreimage({
9090
9090
  kind: UpdatePreimageKind.UpdateOrAdd,
9091
9091
  item: lookupHistory,
9092
9092
  });
@@ -9123,23 +9123,21 @@ var UpdateServiceKind;
9123
9123
  UpdateServiceKind[UpdateServiceKind["Create"] = 1] = "Create";
9124
9124
  })(UpdateServiceKind || (UpdateServiceKind = {}));
9125
9125
  /**
9126
- * Update service info of a particular `ServiceId` or create a new one.
9126
+ * Update service info or create a new one.
9127
9127
  */
9128
9128
  class UpdateService {
9129
- serviceId;
9130
9129
  action;
9131
- constructor(serviceId, action) {
9132
- this.serviceId = serviceId;
9130
+ constructor(action) {
9133
9131
  this.action = action;
9134
9132
  }
9135
- static update({ serviceId, serviceInfo }) {
9136
- return new UpdateService(serviceId, {
9133
+ static update({ serviceInfo }) {
9134
+ return new UpdateService({
9137
9135
  kind: UpdateServiceKind.Update,
9138
9136
  account: serviceInfo,
9139
9137
  });
9140
9138
  }
9141
- static create({ serviceId, serviceInfo, lookupHistory, }) {
9142
- return new UpdateService(serviceId, {
9139
+ static create({ serviceInfo, lookupHistory, }) {
9140
+ return new UpdateService({
9143
9141
  kind: UpdateServiceKind.Create,
9144
9142
  account: serviceInfo,
9145
9143
  lookupHistory,
@@ -9160,17 +9158,15 @@ var UpdateStorageKind;
9160
9158
  * Can either create/modify an entry or remove it.
9161
9159
  */
9162
9160
  class UpdateStorage {
9163
- serviceId;
9164
9161
  action;
9165
- constructor(serviceId, action) {
9166
- this.serviceId = serviceId;
9162
+ constructor(action) {
9167
9163
  this.action = action;
9168
9164
  }
9169
- static set({ serviceId, storage }) {
9170
- return new UpdateStorage(serviceId, { kind: UpdateStorageKind.Set, storage });
9165
+ static set({ storage }) {
9166
+ return new UpdateStorage({ kind: UpdateStorageKind.Set, storage });
9171
9167
  }
9172
- static remove({ serviceId, key }) {
9173
- return new UpdateStorage(serviceId, { kind: UpdateStorageKind.Remove, key });
9168
+ static remove({ key }) {
9169
+ return new UpdateStorage({ kind: UpdateStorageKind.Remove, key });
9174
9170
  }
9175
9171
  get key() {
9176
9172
  if (this.action.kind === UpdateStorageKind.Remove) {
@@ -9355,12 +9351,12 @@ class InMemoryState extends WithDebug {
9355
9351
  * Modify the state and apply a single state update.
9356
9352
  */
9357
9353
  applyUpdate(update) {
9358
- const { servicesRemoved, servicesUpdates, preimages, storage, ...rest } = update;
9354
+ const { removed, created: _, updated, preimages, storage, ...rest } = update;
9359
9355
  // just assign all other variables
9360
9356
  Object.assign(this, rest);
9361
9357
  // and update the services state
9362
9358
  let result;
9363
- result = this.updateServices(servicesUpdates);
9359
+ result = this.updateServices(updated);
9364
9360
  if (result.isError) {
9365
9361
  return result;
9366
9362
  }
@@ -9372,7 +9368,7 @@ class InMemoryState extends WithDebug {
9372
9368
  if (result.isError) {
9373
9369
  return result;
9374
9370
  }
9375
- this.removeServices(servicesRemoved);
9371
+ this.removeServices(removed);
9376
9372
  return Result$1.ok(OK);
9377
9373
  }
9378
9374
  removeServices(servicesRemoved) {
@@ -9381,89 +9377,102 @@ class InMemoryState extends WithDebug {
9381
9377
  this.services.delete(serviceId);
9382
9378
  }
9383
9379
  }
9384
- updateStorage(storage) {
9385
- for (const { serviceId, action } of storage ?? []) {
9386
- const { kind } = action;
9387
- const service = this.services.get(serviceId);
9388
- if (service === undefined) {
9389
- return Result$1.error(UpdateError.NoService, `Attempting to update storage of non-existing service: ${serviceId}`);
9390
- }
9391
- if (kind === UpdateStorageKind.Set) {
9392
- const { key, value } = action.storage;
9393
- service.data.storage.set(key.toString(), StorageItem.create({ key, value }));
9394
- }
9395
- else if (kind === UpdateStorageKind.Remove) {
9396
- const { key } = action;
9397
- check `
9380
+ updateStorage(storageUpdates) {
9381
+ if (storageUpdates === undefined) {
9382
+ return Result$1.ok(OK);
9383
+ }
9384
+ for (const [serviceId, updates] of storageUpdates.entries()) {
9385
+ for (const update of updates) {
9386
+ const { kind } = update.action;
9387
+ const service = this.services.get(serviceId);
9388
+ if (service === undefined) {
9389
+ return Result$1.error(UpdateError.NoService, () => `Attempting to update storage of non-existing service: ${serviceId}`);
9390
+ }
9391
+ if (kind === UpdateStorageKind.Set) {
9392
+ const { key, value } = update.action.storage;
9393
+ service.data.storage.set(key.toString(), StorageItem.create({ key, value }));
9394
+ }
9395
+ else if (kind === UpdateStorageKind.Remove) {
9396
+ const { key } = update.action;
9397
+ check `
9398
9398
  ${service.data.storage.has(key.toString())}
9399
- Attempting to remove non-existing storage item at ${serviceId}: ${action.key}
9399
+ Attempting to remove non-existing storage item at ${serviceId}: ${update.action.key}
9400
9400
  `;
9401
- service.data.storage.delete(key.toString());
9402
- }
9403
- else {
9404
- assertNever(kind);
9401
+ service.data.storage.delete(key.toString());
9402
+ }
9403
+ else {
9404
+ assertNever(kind);
9405
+ }
9405
9406
  }
9406
9407
  }
9407
9408
  return Result$1.ok(OK);
9408
9409
  }
9409
- updatePreimages(preimages) {
9410
- for (const { serviceId, action } of preimages ?? []) {
9410
+ updatePreimages(preimagesUpdates) {
9411
+ if (preimagesUpdates === undefined) {
9412
+ return Result$1.ok(OK);
9413
+ }
9414
+ for (const [serviceId, updates] of preimagesUpdates.entries()) {
9411
9415
  const service = this.services.get(serviceId);
9412
9416
  if (service === undefined) {
9413
- return Result$1.error(UpdateError.NoService, `Attempting to update preimage of non-existing service: ${serviceId}`);
9417
+ return Result$1.error(UpdateError.NoService, () => `Attempting to update preimage of non-existing service: ${serviceId}`);
9414
9418
  }
9415
- const { kind } = action;
9416
- if (kind === UpdatePreimageKind.Provide) {
9417
- const { preimage, slot } = action;
9418
- if (service.data.preimages.has(preimage.hash)) {
9419
- return Result$1.error(UpdateError.PreimageExists, `Overwriting existing preimage at ${serviceId}: ${preimage}`);
9420
- }
9421
- service.data.preimages.set(preimage.hash, preimage);
9422
- if (slot !== null) {
9423
- const lookupHistory = service.data.lookupHistory.get(preimage.hash);
9424
- const length = tryAsU32(preimage.blob.length);
9425
- const lookup = new LookupHistoryItem(preimage.hash, length, tryAsLookupHistorySlots([slot]));
9426
- if (lookupHistory === undefined) {
9427
- // no lookup history for that preimage at all (edge case, should be requested)
9428
- service.data.lookupHistory.set(preimage.hash, [lookup]);
9419
+ for (const update of updates) {
9420
+ const { kind } = update.action;
9421
+ if (kind === UpdatePreimageKind.Provide) {
9422
+ const { preimage, slot } = update.action;
9423
+ if (service.data.preimages.has(preimage.hash)) {
9424
+ return Result$1.error(UpdateError.PreimageExists, () => `Overwriting existing preimage at ${serviceId}: ${preimage}`);
9429
9425
  }
9430
- else {
9431
- // insert or replace exiting entry
9432
- const index = lookupHistory.map((x) => x.length).indexOf(length);
9433
- lookupHistory.splice(index, index === -1 ? 0 : 1, lookup);
9426
+ service.data.preimages.set(preimage.hash, preimage);
9427
+ if (slot !== null) {
9428
+ const lookupHistory = service.data.lookupHistory.get(preimage.hash);
9429
+ const length = tryAsU32(preimage.blob.length);
9430
+ const lookup = new LookupHistoryItem(preimage.hash, length, tryAsLookupHistorySlots([slot]));
9431
+ if (lookupHistory === undefined) {
9432
+ // no lookup history for that preimage at all (edge case, should be requested)
9433
+ service.data.lookupHistory.set(preimage.hash, [lookup]);
9434
+ }
9435
+ else {
9436
+ // insert or replace exiting entry
9437
+ const index = lookupHistory.map((x) => x.length).indexOf(length);
9438
+ lookupHistory.splice(index, index === -1 ? 0 : 1, lookup);
9439
+ }
9434
9440
  }
9435
9441
  }
9436
- }
9437
- else if (kind === UpdatePreimageKind.Remove) {
9438
- const { hash, length } = action;
9439
- service.data.preimages.delete(hash);
9440
- const history = service.data.lookupHistory.get(hash) ?? [];
9441
- const idx = history.map((x) => x.length).indexOf(length);
9442
- if (idx !== -1) {
9443
- history.splice(idx, 1);
9442
+ else if (kind === UpdatePreimageKind.Remove) {
9443
+ const { hash, length } = update.action;
9444
+ service.data.preimages.delete(hash);
9445
+ const history = service.data.lookupHistory.get(hash) ?? [];
9446
+ const idx = history.map((x) => x.length).indexOf(length);
9447
+ if (idx !== -1) {
9448
+ history.splice(idx, 1);
9449
+ }
9450
+ }
9451
+ else if (kind === UpdatePreimageKind.UpdateOrAdd) {
9452
+ const { item } = update.action;
9453
+ const history = service.data.lookupHistory.get(item.hash) ?? [];
9454
+ const existingIdx = history.map((x) => x.length).indexOf(item.length);
9455
+ const removeCount = existingIdx === -1 ? 0 : 1;
9456
+ history.splice(existingIdx, removeCount, item);
9457
+ service.data.lookupHistory.set(item.hash, history);
9458
+ }
9459
+ else {
9460
+ assertNever(kind);
9444
9461
  }
9445
- }
9446
- else if (kind === UpdatePreimageKind.UpdateOrAdd) {
9447
- const { item } = action;
9448
- const history = service.data.lookupHistory.get(item.hash) ?? [];
9449
- const existingIdx = history.map((x) => x.length).indexOf(item.length);
9450
- const removeCount = existingIdx === -1 ? 0 : 1;
9451
- history.splice(existingIdx, removeCount, item);
9452
- service.data.lookupHistory.set(item.hash, history);
9453
- }
9454
- else {
9455
- assertNever(kind);
9456
9462
  }
9457
9463
  }
9458
9464
  return Result$1.ok(OK);
9459
9465
  }
9460
9466
  updateServices(servicesUpdates) {
9461
- for (const { serviceId, action } of servicesUpdates ?? []) {
9462
- const { kind, account } = action;
9467
+ if (servicesUpdates === undefined) {
9468
+ return Result$1.ok(OK);
9469
+ }
9470
+ for (const [serviceId, update] of servicesUpdates.entries()) {
9471
+ const { kind, account } = update.action;
9463
9472
  if (kind === UpdateServiceKind.Create) {
9464
- const { lookupHistory } = action;
9473
+ const { lookupHistory } = update.action;
9465
9474
  if (this.services.has(serviceId)) {
9466
- return Result$1.error(UpdateError.DuplicateService, `${serviceId} already exists!`);
9475
+ return Result$1.error(UpdateError.DuplicateService, () => `${serviceId} already exists!`);
9467
9476
  }
9468
9477
  this.services.set(serviceId, new InMemoryService(serviceId, {
9469
9478
  info: account,
@@ -9475,7 +9484,7 @@ class InMemoryState extends WithDebug {
9475
9484
  else if (kind === UpdateServiceKind.Update) {
9476
9485
  const existingService = this.services.get(serviceId);
9477
9486
  if (existingService === undefined) {
9478
- return Result$1.error(UpdateError.NoService, `Cannot update ${serviceId} because it does not exist.`);
9487
+ return Result$1.error(UpdateError.NoService, () => `Cannot update ${serviceId} because it does not exist.`);
9479
9488
  }
9480
9489
  existingService.data.info = account;
9481
9490
  }
@@ -10729,76 +10738,88 @@ function* serializeStateUpdate(spec, blake2b, update) {
10729
10738
  yield* serializeBasicKeys(spec, update);
10730
10739
  const encode = (codec, val) => Encoder.encodeObject(codec, val, spec);
10731
10740
  // then let's proceed with service updates
10732
- yield* serializeServiceUpdates(update.servicesUpdates, encode, blake2b);
10741
+ yield* serializeServiceUpdates(update.updated, encode, blake2b);
10733
10742
  yield* serializePreimages(update.preimages, encode, blake2b);
10734
10743
  yield* serializeStorage(update.storage, blake2b);
10735
- yield* serializeRemovedServices(update.servicesRemoved);
10744
+ yield* serializeRemovedServices(update.removed);
10736
10745
  }
10737
10746
  function* serializeRemovedServices(servicesRemoved) {
10738
- for (const serviceId of servicesRemoved ?? []) {
10747
+ if (servicesRemoved === undefined) {
10748
+ return;
10749
+ }
10750
+ for (const serviceId of servicesRemoved) {
10739
10751
  // TODO [ToDr] what about all data associated with a service?
10740
10752
  const codec = serialize.serviceData(serviceId);
10741
10753
  yield [StateEntryUpdateAction.Remove, codec.key, EMPTY_BLOB];
10742
10754
  }
10743
10755
  }
10744
- function* serializeStorage(storage, blake2b) {
10745
- for (const { action, serviceId } of storage ?? []) {
10746
- switch (action.kind) {
10747
- case UpdateStorageKind.Set: {
10748
- const key = action.storage.key;
10749
- const codec = serialize.serviceStorage(blake2b, serviceId, key);
10750
- yield [StateEntryUpdateAction.Insert, codec.key, action.storage.value];
10751
- break;
10752
- }
10753
- case UpdateStorageKind.Remove: {
10754
- const key = action.key;
10755
- const codec = serialize.serviceStorage(blake2b, serviceId, key);
10756
- yield [StateEntryUpdateAction.Remove, codec.key, EMPTY_BLOB];
10757
- break;
10756
+ function* serializeStorage(storageUpdates, blake2b) {
10757
+ if (storageUpdates === undefined) {
10758
+ return;
10759
+ }
10760
+ for (const [serviceId, updates] of storageUpdates.entries()) {
10761
+ for (const { action } of updates) {
10762
+ switch (action.kind) {
10763
+ case UpdateStorageKind.Set: {
10764
+ const key = action.storage.key;
10765
+ const codec = serialize.serviceStorage(blake2b, serviceId, key);
10766
+ yield [StateEntryUpdateAction.Insert, codec.key, action.storage.value];
10767
+ break;
10768
+ }
10769
+ case UpdateStorageKind.Remove: {
10770
+ const key = action.key;
10771
+ const codec = serialize.serviceStorage(blake2b, serviceId, key);
10772
+ yield [StateEntryUpdateAction.Remove, codec.key, EMPTY_BLOB];
10773
+ break;
10774
+ }
10758
10775
  }
10759
- default:
10760
- assertNever(action);
10761
10776
  }
10762
10777
  }
10763
10778
  }
10764
- function* serializePreimages(preimages, encode, blake2b) {
10765
- for (const { action, serviceId } of preimages ?? []) {
10766
- switch (action.kind) {
10767
- case UpdatePreimageKind.Provide: {
10768
- const { hash, blob } = action.preimage;
10769
- const codec = serialize.servicePreimages(blake2b, serviceId, hash);
10770
- yield [StateEntryUpdateAction.Insert, codec.key, blob];
10771
- if (action.slot !== null) {
10772
- const codec2 = serialize.serviceLookupHistory(blake2b, serviceId, hash, tryAsU32(blob.length));
10773
- yield [
10774
- StateEntryUpdateAction.Insert,
10775
- codec2.key,
10776
- encode(codec2.Codec, tryAsLookupHistorySlots([action.slot])),
10777
- ];
10779
+ function* serializePreimages(preimagesUpdates, encode, blake2b) {
10780
+ if (preimagesUpdates === undefined) {
10781
+ return;
10782
+ }
10783
+ for (const [serviceId, updates] of preimagesUpdates.entries()) {
10784
+ for (const { action } of updates) {
10785
+ switch (action.kind) {
10786
+ case UpdatePreimageKind.Provide: {
10787
+ const { hash, blob } = action.preimage;
10788
+ const codec = serialize.servicePreimages(blake2b, serviceId, hash);
10789
+ yield [StateEntryUpdateAction.Insert, codec.key, blob];
10790
+ if (action.slot !== null) {
10791
+ const codec2 = serialize.serviceLookupHistory(blake2b, serviceId, hash, tryAsU32(blob.length));
10792
+ yield [
10793
+ StateEntryUpdateAction.Insert,
10794
+ codec2.key,
10795
+ encode(codec2.Codec, tryAsLookupHistorySlots([action.slot])),
10796
+ ];
10797
+ }
10798
+ break;
10799
+ }
10800
+ case UpdatePreimageKind.UpdateOrAdd: {
10801
+ const { hash, length, slots } = action.item;
10802
+ const codec = serialize.serviceLookupHistory(blake2b, serviceId, hash, length);
10803
+ yield [StateEntryUpdateAction.Insert, codec.key, encode(codec.Codec, slots)];
10804
+ break;
10805
+ }
10806
+ case UpdatePreimageKind.Remove: {
10807
+ const { hash, length } = action;
10808
+ const codec = serialize.servicePreimages(blake2b, serviceId, hash);
10809
+ yield [StateEntryUpdateAction.Remove, codec.key, EMPTY_BLOB];
10810
+ const codec2 = serialize.serviceLookupHistory(blake2b, serviceId, hash, length);
10811
+ yield [StateEntryUpdateAction.Remove, codec2.key, EMPTY_BLOB];
10812
+ break;
10778
10813
  }
10779
- break;
10780
- }
10781
- case UpdatePreimageKind.UpdateOrAdd: {
10782
- const { hash, length, slots } = action.item;
10783
- const codec = serialize.serviceLookupHistory(blake2b, serviceId, hash, length);
10784
- yield [StateEntryUpdateAction.Insert, codec.key, encode(codec.Codec, slots)];
10785
- break;
10786
- }
10787
- case UpdatePreimageKind.Remove: {
10788
- const { hash, length } = action;
10789
- const codec = serialize.servicePreimages(blake2b, serviceId, hash);
10790
- yield [StateEntryUpdateAction.Remove, codec.key, EMPTY_BLOB];
10791
- const codec2 = serialize.serviceLookupHistory(blake2b, serviceId, hash, length);
10792
- yield [StateEntryUpdateAction.Remove, codec2.key, EMPTY_BLOB];
10793
- break;
10794
10814
  }
10795
- default:
10796
- assertNever(action);
10797
10815
  }
10798
10816
  }
10799
10817
  }
10800
10818
  function* serializeServiceUpdates(servicesUpdates, encode, blake2b) {
10801
- for (const { action, serviceId } of servicesUpdates ?? []) {
10819
+ if (servicesUpdates === undefined) {
10820
+ return;
10821
+ }
10822
+ for (const [serviceId, { action }] of servicesUpdates.entries()) {
10802
10823
  // new service being created or updated
10803
10824
  const codec = serialize.serviceData(serviceId);
10804
10825
  yield [StateEntryUpdateAction.Insert, codec.key, encode(codec.Codec, action.account)];
@@ -11074,13 +11095,13 @@ class LeafDb {
11074
11095
  */
11075
11096
  static fromLeavesBlob(blob, db) {
11076
11097
  if (blob.length % TRIE_NODE_BYTES !== 0) {
11077
- return Result$1.error(LeafDbError.InvalidLeafData, `${blob.length} is not a multiply of ${TRIE_NODE_BYTES}: ${blob}`);
11098
+ return Result$1.error(LeafDbError.InvalidLeafData, () => `${blob.length} is not a multiply of ${TRIE_NODE_BYTES}: ${blob}`);
11078
11099
  }
11079
11100
  const leaves = SortedSet.fromArray(leafComparator, []);
11080
11101
  for (const nodeData of blob.chunks(TRIE_NODE_BYTES)) {
11081
11102
  const node = new TrieNode(nodeData.raw);
11082
11103
  if (node.getNodeType() === NodeType.Branch) {
11083
- return Result$1.error(LeafDbError.InvalidLeafData, `Branch node detected: ${nodeData}`);
11104
+ return Result$1.error(LeafDbError.InvalidLeafData, () => `Branch node detected: ${nodeData}`);
11084
11105
  }
11085
11106
  leaves.insert(node.asLeafNode());
11086
11107
  }
@@ -12395,6 +12416,14 @@ const NoMachineError = Symbol("Machine index not found.");
12395
12416
  const SegmentExportError = Symbol("Too many segments already exported.");
12396
12417
 
12397
12418
  const InsufficientFundsError = "insufficient funds";
12419
+ /** Deep clone of a map with array. */
12420
+ function deepCloneMapWithArray(map) {
12421
+ const cloned = [];
12422
+ for (const [k, v] of map.entries()) {
12423
+ cloned.push([k, v.slice()]);
12424
+ }
12425
+ return new Map(cloned);
12426
+ }
12398
12427
  /**
12399
12428
  * State updates that currently accumulating service produced.
12400
12429
  *
@@ -12424,10 +12453,11 @@ class AccumulationStateUpdate {
12424
12453
  /** Create new empty state update. */
12425
12454
  static empty() {
12426
12455
  return new AccumulationStateUpdate({
12427
- servicesUpdates: [],
12428
- servicesRemoved: [],
12429
- preimages: [],
12430
- storage: [],
12456
+ created: [],
12457
+ updated: new Map(),
12458
+ removed: [],
12459
+ preimages: new Map(),
12460
+ storage: new Map(),
12431
12461
  }, []);
12432
12462
  }
12433
12463
  /** Create a state update with some existing, yet uncommited services updates. */
@@ -12439,10 +12469,13 @@ class AccumulationStateUpdate {
12439
12469
  /** Create a copy of another `StateUpdate`. Used by checkpoints. */
12440
12470
  static copyFrom(from) {
12441
12471
  const serviceUpdates = {
12442
- servicesUpdates: [...from.services.servicesUpdates],
12443
- servicesRemoved: [...from.services.servicesRemoved],
12444
- preimages: [...from.services.preimages],
12445
- storage: [...from.services.storage],
12472
+ // shallow copy
12473
+ created: [...from.services.created],
12474
+ updated: new Map(from.services.updated),
12475
+ removed: [...from.services.removed],
12476
+ // deep copy
12477
+ preimages: deepCloneMapWithArray(from.services.preimages),
12478
+ storage: deepCloneMapWithArray(from.services.storage),
12446
12479
  };
12447
12480
  const transfers = [...from.transfers];
12448
12481
  const update = new AccumulationStateUpdate(serviceUpdates, transfers, new Map(from.yieldedRoots));
@@ -12490,9 +12523,9 @@ class PartiallyUpdatedState {
12490
12523
  if (destination === null) {
12491
12524
  return null;
12492
12525
  }
12493
- const maybeNewService = this.stateUpdate.services.servicesUpdates.find((update) => update.serviceId === destination);
12494
- if (maybeNewService !== undefined) {
12495
- return maybeNewService.action.account;
12526
+ const maybeUpdatedServiceInfo = this.stateUpdate.services.updated.get(destination);
12527
+ if (maybeUpdatedServiceInfo !== undefined) {
12528
+ return maybeUpdatedServiceInfo.action.account;
12496
12529
  }
12497
12530
  const maybeService = this.state.getService(destination);
12498
12531
  if (maybeService === null) {
@@ -12501,7 +12534,8 @@ class PartiallyUpdatedState {
12501
12534
  return maybeService.getInfo();
12502
12535
  }
12503
12536
  getStorage(serviceId, rawKey) {
12504
- const item = this.stateUpdate.services.storage.find((x) => x.serviceId === serviceId && x.key.isEqualTo(rawKey));
12537
+ const storages = this.stateUpdate.services.storage.get(serviceId) ?? [];
12538
+ const item = storages.find((x) => x.key.isEqualTo(rawKey));
12505
12539
  if (item !== undefined) {
12506
12540
  return item.value;
12507
12541
  }
@@ -12516,10 +12550,11 @@ class PartiallyUpdatedState {
12516
12550
  * the existence in `preimages` map.
12517
12551
  */
12518
12552
  hasPreimage(serviceId, hash) {
12519
- const providedPreimage = this.stateUpdate.services.preimages.find(
12553
+ const preimages = this.stateUpdate.services.preimages.get(serviceId) ?? [];
12554
+ const providedPreimage = preimages.find(
12520
12555
  // we ignore the action here, since if there is <any> update on that
12521
12556
  // hash it means it has to exist, right?
12522
- (p) => p.serviceId === serviceId && p.hash.isEqualTo(hash));
12557
+ (p) => p.hash.isEqualTo(hash));
12523
12558
  if (providedPreimage !== undefined) {
12524
12559
  return true;
12525
12560
  }
@@ -12532,7 +12567,8 @@ class PartiallyUpdatedState {
12532
12567
  }
12533
12568
  getPreimage(serviceId, hash) {
12534
12569
  // TODO [ToDr] Should we verify availability here?
12535
- const freshlyProvided = this.stateUpdate.services.preimages.find((x) => x.serviceId === serviceId && x.hash.isEqualTo(hash));
12570
+ const preimages = this.stateUpdate.services.preimages.get(serviceId) ?? [];
12571
+ const freshlyProvided = preimages.find((x) => x.hash.isEqualTo(hash));
12536
12572
  if (freshlyProvided !== undefined && freshlyProvided.action.kind === UpdatePreimageKind.Provide) {
12537
12573
  return freshlyProvided.action.preimage.blob;
12538
12574
  }
@@ -12541,10 +12577,11 @@ class PartiallyUpdatedState {
12541
12577
  }
12542
12578
  /** Get status of a preimage of current service taking into account any updates. */
12543
12579
  getLookupHistory(currentTimeslot, serviceId, hash, length) {
12580
+ const preimages = this.stateUpdate.services.preimages.get(serviceId) ?? [];
12544
12581
  // TODO [ToDr] This is most likely wrong. We may have `provide` and `remove` within
12545
12582
  // the same state update. We should however switch to proper "updated state"
12546
12583
  // representation soon.
12547
- const updatedPreimage = this.stateUpdate.services.preimages.findLast((update) => update.serviceId === serviceId && update.hash.isEqualTo(hash) && BigInt(update.length) === length);
12584
+ const updatedPreimage = preimages.findLast((update) => update.hash.isEqualTo(hash) && BigInt(update.length) === length);
12548
12585
  const stateFallback = () => {
12549
12586
  // fallback to state lookup
12550
12587
  const service = this.state.getService(serviceId);
@@ -12581,14 +12618,15 @@ class PartiallyUpdatedState {
12581
12618
  /* State update functions. */
12582
12619
  updateStorage(serviceId, key, value) {
12583
12620
  const update = value === null
12584
- ? UpdateStorage.remove({ serviceId, key })
12621
+ ? UpdateStorage.remove({ key })
12585
12622
  : UpdateStorage.set({
12586
- serviceId,
12587
12623
  storage: StorageItem.create({ key, value }),
12588
12624
  });
12589
- const index = this.stateUpdate.services.storage.findIndex((x) => x.serviceId === update.serviceId && x.key.isEqualTo(key));
12625
+ const storages = this.stateUpdate.services.storage.get(serviceId) ?? [];
12626
+ const index = storages.findIndex((x) => x.key.isEqualTo(key));
12590
12627
  const count = index === -1 ? 0 : 1;
12591
- this.stateUpdate.services.storage.splice(index, count, update);
12628
+ storages.splice(index, count, update);
12629
+ this.stateUpdate.services.storage.set(serviceId, storages);
12592
12630
  }
12593
12631
  /**
12594
12632
  * Update a preimage.
@@ -12596,8 +12634,10 @@ class PartiallyUpdatedState {
12596
12634
  * Note we store all previous entries as well, since there might be a sequence of:
12597
12635
  * `provide` -> `remove` and both should update the end state somehow.
12598
12636
  */
12599
- updatePreimage(newUpdate) {
12600
- this.stateUpdate.services.preimages.push(newUpdate);
12637
+ updatePreimage(serviceId, newUpdate) {
12638
+ const updatePreimages = this.stateUpdate.services.preimages.get(serviceId) ?? [];
12639
+ updatePreimages.push(newUpdate);
12640
+ this.stateUpdate.services.preimages.set(serviceId, updatePreimages);
12601
12641
  }
12602
12642
  updateServiceStorageUtilisation(serviceId, items, bytes, serviceInfo) {
12603
12643
  check `${items >= 0} storageUtilisationCount has to be a positive number, got: ${items}`;
@@ -12606,11 +12646,11 @@ class PartiallyUpdatedState {
12606
12646
  const overflowBytes = !isU64(bytes);
12607
12647
  // TODO [ToDr] this is not specified in GP, but it seems sensible.
12608
12648
  if (overflowItems || overflowBytes) {
12609
- return Result$1.error(InsufficientFundsError);
12649
+ return Result$1.error(InsufficientFundsError, () => `Storage utilisation overflow: items=${overflowItems}, bytes=${overflowBytes}`);
12610
12650
  }
12611
12651
  const thresholdBalance = ServiceAccountInfo.calculateThresholdBalance(items, bytes, serviceInfo.gratisStorage);
12612
12652
  if (serviceInfo.balance < thresholdBalance) {
12613
- return Result$1.error(InsufficientFundsError);
12653
+ return Result$1.error(InsufficientFundsError, () => `Service balance (${serviceInfo.balance}) below threshold (${thresholdBalance})`);
12614
12654
  }
12615
12655
  // Update service info with new details.
12616
12656
  this.updateServiceInfo(serviceId, ServiceAccountInfo.create({
@@ -12621,20 +12661,23 @@ class PartiallyUpdatedState {
12621
12661
  return Result$1.ok(OK);
12622
12662
  }
12623
12663
  updateServiceInfo(serviceId, newInfo) {
12624
- const idx = this.stateUpdate.services.servicesUpdates.findIndex((x) => x.serviceId === serviceId);
12625
- const toRemove = idx === -1 ? 0 : 1;
12626
- const existingItem = this.stateUpdate.services.servicesUpdates[idx];
12627
- if (existingItem?.action.kind === UpdateServiceKind.Create) {
12628
- this.stateUpdate.services.servicesUpdates.splice(idx, toRemove, UpdateService.create({
12629
- serviceId,
12664
+ const existingUpdate = this.stateUpdate.services.updated.get(serviceId);
12665
+ if (existingUpdate?.action.kind === UpdateServiceKind.Create) {
12666
+ this.stateUpdate.services.updated.set(serviceId, UpdateService.create({
12630
12667
  serviceInfo: newInfo,
12631
- lookupHistory: existingItem.action.lookupHistory,
12668
+ lookupHistory: existingUpdate.action.lookupHistory,
12632
12669
  }));
12633
12670
  return;
12634
12671
  }
12635
- this.stateUpdate.services.servicesUpdates.splice(idx, toRemove, UpdateService.update({
12636
- serviceId,
12672
+ this.stateUpdate.services.updated.set(serviceId, UpdateService.update({
12673
+ serviceInfo: newInfo,
12674
+ }));
12675
+ }
12676
+ createService(serviceId, newInfo, newLookupHistory) {
12677
+ this.stateUpdate.services.created.push(serviceId);
12678
+ this.stateUpdate.services.updated.set(serviceId, UpdateService.create({
12637
12679
  serviceInfo: newInfo,
12680
+ lookupHistory: newLookupHistory,
12638
12681
  }));
12639
12682
  }
12640
12683
  getPrivilegedServices() {
@@ -14255,7 +14298,7 @@ class ReadablePage extends MemoryPage {
14255
14298
  loadInto(result, startIndex, length) {
14256
14299
  const endIndex = startIndex + length;
14257
14300
  if (endIndex > PAGE_SIZE$1) {
14258
- return Result$1.error(PageFault.fromMemoryIndex(this.start + PAGE_SIZE$1));
14301
+ return Result$1.error(PageFault.fromMemoryIndex(this.start + PAGE_SIZE$1), () => `Page fault: read beyond page boundary at ${this.start + PAGE_SIZE$1}`);
14259
14302
  }
14260
14303
  const bytes = this.data.subarray(startIndex, endIndex);
14261
14304
  // we zero the bytes, since data might not yet be initialized at `endIndex`.
@@ -14264,7 +14307,7 @@ class ReadablePage extends MemoryPage {
14264
14307
  return Result$1.ok(OK);
14265
14308
  }
14266
14309
  storeFrom(_address, _data) {
14267
- return Result$1.error(PageFault.fromMemoryIndex(this.start, true));
14310
+ return Result$1.error(PageFault.fromMemoryIndex(this.start, true), () => `Page fault: attempted to write to read-only page at ${this.start}`);
14268
14311
  }
14269
14312
  setData(pageIndex, data) {
14270
14313
  this.data.set(data, pageIndex);
@@ -14293,7 +14336,7 @@ class WriteablePage extends MemoryPage {
14293
14336
  loadInto(result, startIndex, length) {
14294
14337
  const endIndex = startIndex + length;
14295
14338
  if (endIndex > PAGE_SIZE$1) {
14296
- return Result$1.error(PageFault.fromMemoryIndex(this.start + PAGE_SIZE$1));
14339
+ return Result$1.error(PageFault.fromMemoryIndex(this.start + PAGE_SIZE$1), () => `Page fault: read beyond page boundary at ${this.start + PAGE_SIZE$1}`);
14297
14340
  }
14298
14341
  const bytes = this.view.subarray(startIndex, endIndex);
14299
14342
  // we zero the bytes, since the view might not yet be initialized at `endIndex`.
@@ -14363,7 +14406,7 @@ class Memory {
14363
14406
  logger$3.insane `MEM[${address}] <- ${BytesBlob.blobFrom(bytes)}`;
14364
14407
  const pagesResult = this.getPages(address, bytes.length, AccessType.WRITE);
14365
14408
  if (pagesResult.isError) {
14366
- return Result$1.error(pagesResult.error);
14409
+ return Result$1.error(pagesResult.error, pagesResult.details);
14367
14410
  }
14368
14411
  const pages = pagesResult.ok;
14369
14412
  let currentPosition = address;
@@ -14388,14 +14431,14 @@ class Memory {
14388
14431
  const pages = [];
14389
14432
  for (const pageNumber of pageRange) {
14390
14433
  if (pageNumber < RESERVED_NUMBER_OF_PAGES) {
14391
- return Result$1.error(PageFault.fromPageNumber(pageNumber, true));
14434
+ return Result$1.error(PageFault.fromPageNumber(pageNumber, true), () => `Page fault: attempted to access reserved page ${pageNumber}`);
14392
14435
  }
14393
14436
  const page = this.memory.get(pageNumber);
14394
14437
  if (page === undefined) {
14395
- return Result$1.error(PageFault.fromPageNumber(pageNumber));
14438
+ return Result$1.error(PageFault.fromPageNumber(pageNumber), () => `Page fault: page ${pageNumber} not allocated`);
14396
14439
  }
14397
14440
  if (accessType === AccessType.WRITE && !page.isWriteable()) {
14398
- return Result$1.error(PageFault.fromPageNumber(pageNumber, true));
14441
+ return Result$1.error(PageFault.fromPageNumber(pageNumber, true), () => `Page fault: attempted to write to read-only page ${pageNumber}`);
14399
14442
  }
14400
14443
  pages.push(page);
14401
14444
  }
@@ -14413,7 +14456,7 @@ class Memory {
14413
14456
  }
14414
14457
  const pagesResult = this.getPages(startAddress, result.length, AccessType.READ);
14415
14458
  if (pagesResult.isError) {
14416
- return Result$1.error(pagesResult.error);
14459
+ return Result$1.error(pagesResult.error, pagesResult.details);
14417
14460
  }
14418
14461
  const pages = pagesResult.ok;
14419
14462
  let currentPosition = startAddress;
@@ -16218,7 +16261,7 @@ class ProgramDecoder {
16218
16261
  }
16219
16262
  catch (e) {
16220
16263
  logger$2.error `Invalid program: ${e}`;
16221
- return Result$1.error(ProgramDecoderError.InvalidProgramError);
16264
+ return Result$1.error(ProgramDecoderError.InvalidProgramError, () => `Program decoder error: ${e}`);
16222
16265
  }
16223
16266
  }
16224
16267
  }
@@ -16499,7 +16542,7 @@ class HostCallMemory {
16499
16542
  return Result$1.ok(OK);
16500
16543
  }
16501
16544
  if (address + tryAsU64(bytes.length) > MEMORY_SIZE) {
16502
- return Result$1.error(new OutOfBounds());
16545
+ return Result$1.error(new OutOfBounds(), () => `Memory access out of bounds: address ${address} + length ${bytes.length} exceeds memory size`);
16503
16546
  }
16504
16547
  return this.memory.storeFrom(tryAsMemoryIndex(Number(address)), bytes);
16505
16548
  }
@@ -16508,13 +16551,10 @@ class HostCallMemory {
16508
16551
  return Result$1.ok(OK);
16509
16552
  }
16510
16553
  if (startAddress + tryAsU64(result.length) > MEMORY_SIZE) {
16511
- return Result$1.error(new OutOfBounds());
16554
+ return Result$1.error(new OutOfBounds(), () => `Memory access out of bounds: address ${startAddress} + length ${result.length} exceeds memory size`);
16512
16555
  }
16513
16556
  return this.memory.loadInto(result, tryAsMemoryIndex(Number(startAddress)));
16514
16557
  }
16515
- getMemory() {
16516
- return this.memory;
16517
- }
16518
16558
  }
16519
16559
 
16520
16560
  class HostCallRegisters {
@@ -17602,32 +17642,33 @@ class Preimages {
17602
17642
  }
17603
17643
  if (prevPreimage.requester > currPreimage.requester ||
17604
17644
  currPreimage.blob.compare(prevPreimage.blob).isLessOrEqual()) {
17605
- return Result$1.error(PreimagesErrorCode.PreimagesNotSortedUnique);
17645
+ return Result$1.error(PreimagesErrorCode.PreimagesNotSortedUnique, () => `Preimages not sorted/unique at index ${i}`);
17606
17646
  }
17607
17647
  }
17608
17648
  const { preimages, slot } = input;
17609
- const pendingChanges = [];
17649
+ const pendingChanges = new Map();
17610
17650
  // select preimages for integration
17611
17651
  for (const preimage of preimages) {
17612
17652
  const { requester, blob } = preimage;
17613
17653
  const hash = this.blake2b.hashBytes(blob).asOpaque();
17614
17654
  const service = this.state.getService(requester);
17615
17655
  if (service === null) {
17616
- return Result$1.error(PreimagesErrorCode.AccountNotFound);
17656
+ return Result$1.error(PreimagesErrorCode.AccountNotFound, () => `Service not found: ${requester}`);
17617
17657
  }
17618
17658
  const hasPreimage = service.hasPreimage(hash);
17619
17659
  const slots = service.getLookupHistory(hash, tryAsU32(blob.length));
17620
17660
  // https://graypaper.fluffylabs.dev/#/5f542d7/181800181900
17621
17661
  // https://graypaper.fluffylabs.dev/#/5f542d7/116f0011a500
17622
17662
  if (hasPreimage || slots === null || !LookupHistoryItem.isRequested(slots)) {
17623
- return Result$1.error(PreimagesErrorCode.PreimageUnneeded);
17663
+ return Result$1.error(PreimagesErrorCode.PreimageUnneeded, () => `Preimage unneeded: requester=${requester}, hash=${hash}, hasPreimage=${hasPreimage}, isRequested=${slots !== null && LookupHistoryItem.isRequested(slots)}`);
17624
17664
  }
17625
17665
  // https://graypaper.fluffylabs.dev/#/5f542d7/18c00018f300
17626
- pendingChanges.push(UpdatePreimage.provide({
17627
- serviceId: requester,
17666
+ const updates = pendingChanges.get(requester) ?? [];
17667
+ updates.push(UpdatePreimage.provide({
17628
17668
  preimage: PreimageItem.create({ hash, blob }),
17629
17669
  slot,
17630
17670
  }));
17671
+ pendingChanges.set(requester, updates);
17631
17672
  }
17632
17673
  return Result$1.ok({
17633
17674
  preimages: pendingChanges,