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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/index.cjs +217 -176
  2. package/index.d.ts +972 -936
  3. package/index.js +217 -176
  4. package/package.json +1 -1
package/index.js CHANGED
@@ -8069,6 +8069,8 @@ function accumulationOutputComparator(a, b) {
8069
8069
  const O = 8;
8070
8070
  /** `Q`: The number of items in the authorizations queue. */
8071
8071
  const Q = 80;
8072
+ /** `W_B`: The maximum size of the concatenated variable-size blobs, extrinsics and imported segments of a work-package, in octets */
8073
+ Compatibility.isGreaterOrEqual(GpVersion.V0_7_2) ? 13_791_360 : 13_794_305;
8072
8074
  /** `W_T`: The size of a transfer memo in octets. */
8073
8075
  const W_T = 128;
8074
8076
  /**
@@ -9059,31 +9061,29 @@ var UpdatePreimageKind;
9059
9061
  * 3. Update `LookupHistory` with given value.
9060
9062
  */
9061
9063
  class UpdatePreimage {
9062
- serviceId;
9063
9064
  action;
9064
- constructor(serviceId, action) {
9065
- this.serviceId = serviceId;
9065
+ constructor(action) {
9066
9066
  this.action = action;
9067
9067
  }
9068
9068
  /** A preimage is provided. We should update the lookuphistory and add the preimage to db. */
9069
- static provide({ serviceId, preimage, slot, }) {
9070
- return new UpdatePreimage(serviceId, {
9069
+ static provide({ preimage, slot }) {
9070
+ return new UpdatePreimage({
9071
9071
  kind: UpdatePreimageKind.Provide,
9072
9072
  preimage,
9073
9073
  slot,
9074
9074
  });
9075
9075
  }
9076
9076
  /** The preimage should be removed completely from the database. */
9077
- static remove({ serviceId, hash, length }) {
9078
- return new UpdatePreimage(serviceId, {
9077
+ static remove({ hash, length }) {
9078
+ return new UpdatePreimage({
9079
9079
  kind: UpdatePreimageKind.Remove,
9080
9080
  hash,
9081
9081
  length,
9082
9082
  });
9083
9083
  }
9084
9084
  /** Update the lookup history of some preimage or add a new one (request). */
9085
- static updateOrAdd({ serviceId, lookupHistory }) {
9086
- return new UpdatePreimage(serviceId, {
9085
+ static updateOrAdd({ lookupHistory }) {
9086
+ return new UpdatePreimage({
9087
9087
  kind: UpdatePreimageKind.UpdateOrAdd,
9088
9088
  item: lookupHistory,
9089
9089
  });
@@ -9120,23 +9120,21 @@ var UpdateServiceKind;
9120
9120
  UpdateServiceKind[UpdateServiceKind["Create"] = 1] = "Create";
9121
9121
  })(UpdateServiceKind || (UpdateServiceKind = {}));
9122
9122
  /**
9123
- * Update service info of a particular `ServiceId` or create a new one.
9123
+ * Update service info or create a new one.
9124
9124
  */
9125
9125
  class UpdateService {
9126
- serviceId;
9127
9126
  action;
9128
- constructor(serviceId, action) {
9129
- this.serviceId = serviceId;
9127
+ constructor(action) {
9130
9128
  this.action = action;
9131
9129
  }
9132
- static update({ serviceId, serviceInfo }) {
9133
- return new UpdateService(serviceId, {
9130
+ static update({ serviceInfo }) {
9131
+ return new UpdateService({
9134
9132
  kind: UpdateServiceKind.Update,
9135
9133
  account: serviceInfo,
9136
9134
  });
9137
9135
  }
9138
- static create({ serviceId, serviceInfo, lookupHistory, }) {
9139
- return new UpdateService(serviceId, {
9136
+ static create({ serviceInfo, lookupHistory, }) {
9137
+ return new UpdateService({
9140
9138
  kind: UpdateServiceKind.Create,
9141
9139
  account: serviceInfo,
9142
9140
  lookupHistory,
@@ -9157,17 +9155,15 @@ var UpdateStorageKind;
9157
9155
  * Can either create/modify an entry or remove it.
9158
9156
  */
9159
9157
  class UpdateStorage {
9160
- serviceId;
9161
9158
  action;
9162
- constructor(serviceId, action) {
9163
- this.serviceId = serviceId;
9159
+ constructor(action) {
9164
9160
  this.action = action;
9165
9161
  }
9166
- static set({ serviceId, storage }) {
9167
- return new UpdateStorage(serviceId, { kind: UpdateStorageKind.Set, storage });
9162
+ static set({ storage }) {
9163
+ return new UpdateStorage({ kind: UpdateStorageKind.Set, storage });
9168
9164
  }
9169
- static remove({ serviceId, key }) {
9170
- return new UpdateStorage(serviceId, { kind: UpdateStorageKind.Remove, key });
9165
+ static remove({ key }) {
9166
+ return new UpdateStorage({ kind: UpdateStorageKind.Remove, key });
9171
9167
  }
9172
9168
  get key() {
9173
9169
  if (this.action.kind === UpdateStorageKind.Remove) {
@@ -9352,12 +9348,12 @@ class InMemoryState extends WithDebug {
9352
9348
  * Modify the state and apply a single state update.
9353
9349
  */
9354
9350
  applyUpdate(update) {
9355
- const { servicesRemoved, servicesUpdates, preimages, storage, ...rest } = update;
9351
+ const { removed, created: _, updated, preimages, storage, ...rest } = update;
9356
9352
  // just assign all other variables
9357
9353
  Object.assign(this, rest);
9358
9354
  // and update the services state
9359
9355
  let result;
9360
- result = this.updateServices(servicesUpdates);
9356
+ result = this.updateServices(updated);
9361
9357
  if (result.isError) {
9362
9358
  return result;
9363
9359
  }
@@ -9369,7 +9365,7 @@ class InMemoryState extends WithDebug {
9369
9365
  if (result.isError) {
9370
9366
  return result;
9371
9367
  }
9372
- this.removeServices(servicesRemoved);
9368
+ this.removeServices(removed);
9373
9369
  return Result$1.ok(OK);
9374
9370
  }
9375
9371
  removeServices(servicesRemoved) {
@@ -9378,87 +9374,100 @@ class InMemoryState extends WithDebug {
9378
9374
  this.services.delete(serviceId);
9379
9375
  }
9380
9376
  }
9381
- updateStorage(storage) {
9382
- for (const { serviceId, action } of storage ?? []) {
9383
- const { kind } = action;
9384
- const service = this.services.get(serviceId);
9385
- if (service === undefined) {
9386
- return Result$1.error(UpdateError.NoService, `Attempting to update storage of non-existing service: ${serviceId}`);
9387
- }
9388
- if (kind === UpdateStorageKind.Set) {
9389
- const { key, value } = action.storage;
9390
- service.data.storage.set(key.toString(), StorageItem.create({ key, value }));
9391
- }
9392
- else if (kind === UpdateStorageKind.Remove) {
9393
- const { key } = action;
9394
- check `
9377
+ updateStorage(storageUpdates) {
9378
+ if (storageUpdates === undefined) {
9379
+ return Result$1.ok(OK);
9380
+ }
9381
+ for (const [serviceId, updates] of storageUpdates.entries()) {
9382
+ for (const update of updates) {
9383
+ const { kind } = update.action;
9384
+ const service = this.services.get(serviceId);
9385
+ if (service === undefined) {
9386
+ return Result$1.error(UpdateError.NoService, `Attempting to update storage of non-existing service: ${serviceId}`);
9387
+ }
9388
+ if (kind === UpdateStorageKind.Set) {
9389
+ const { key, value } = update.action.storage;
9390
+ service.data.storage.set(key.toString(), StorageItem.create({ key, value }));
9391
+ }
9392
+ else if (kind === UpdateStorageKind.Remove) {
9393
+ const { key } = update.action;
9394
+ check `
9395
9395
  ${service.data.storage.has(key.toString())}
9396
- Attempting to remove non-existing storage item at ${serviceId}: ${action.key}
9396
+ Attempting to remove non-existing storage item at ${serviceId}: ${update.action.key}
9397
9397
  `;
9398
- service.data.storage.delete(key.toString());
9399
- }
9400
- else {
9401
- assertNever(kind);
9398
+ service.data.storage.delete(key.toString());
9399
+ }
9400
+ else {
9401
+ assertNever(kind);
9402
+ }
9402
9403
  }
9403
9404
  }
9404
9405
  return Result$1.ok(OK);
9405
9406
  }
9406
- updatePreimages(preimages) {
9407
- for (const { serviceId, action } of preimages ?? []) {
9407
+ updatePreimages(preimagesUpdates) {
9408
+ if (preimagesUpdates === undefined) {
9409
+ return Result$1.ok(OK);
9410
+ }
9411
+ for (const [serviceId, updates] of preimagesUpdates.entries()) {
9408
9412
  const service = this.services.get(serviceId);
9409
9413
  if (service === undefined) {
9410
9414
  return Result$1.error(UpdateError.NoService, `Attempting to update preimage of non-existing service: ${serviceId}`);
9411
9415
  }
9412
- const { kind } = action;
9413
- if (kind === UpdatePreimageKind.Provide) {
9414
- const { preimage, slot } = action;
9415
- if (service.data.preimages.has(preimage.hash)) {
9416
- return Result$1.error(UpdateError.PreimageExists, `Overwriting existing preimage at ${serviceId}: ${preimage}`);
9417
- }
9418
- service.data.preimages.set(preimage.hash, preimage);
9419
- if (slot !== null) {
9420
- const lookupHistory = service.data.lookupHistory.get(preimage.hash);
9421
- const length = tryAsU32(preimage.blob.length);
9422
- const lookup = new LookupHistoryItem(preimage.hash, length, tryAsLookupHistorySlots([slot]));
9423
- if (lookupHistory === undefined) {
9424
- // no lookup history for that preimage at all (edge case, should be requested)
9425
- service.data.lookupHistory.set(preimage.hash, [lookup]);
9416
+ for (const update of updates) {
9417
+ const { kind } = update.action;
9418
+ if (kind === UpdatePreimageKind.Provide) {
9419
+ const { preimage, slot } = update.action;
9420
+ if (service.data.preimages.has(preimage.hash)) {
9421
+ return Result$1.error(UpdateError.PreimageExists, `Overwriting existing preimage at ${serviceId}: ${preimage}`);
9426
9422
  }
9427
- else {
9428
- // insert or replace exiting entry
9429
- const index = lookupHistory.map((x) => x.length).indexOf(length);
9430
- lookupHistory.splice(index, index === -1 ? 0 : 1, lookup);
9423
+ service.data.preimages.set(preimage.hash, preimage);
9424
+ if (slot !== null) {
9425
+ const lookupHistory = service.data.lookupHistory.get(preimage.hash);
9426
+ const length = tryAsU32(preimage.blob.length);
9427
+ const lookup = new LookupHistoryItem(preimage.hash, length, tryAsLookupHistorySlots([slot]));
9428
+ if (lookupHistory === undefined) {
9429
+ // no lookup history for that preimage at all (edge case, should be requested)
9430
+ service.data.lookupHistory.set(preimage.hash, [lookup]);
9431
+ }
9432
+ else {
9433
+ // insert or replace exiting entry
9434
+ const index = lookupHistory.map((x) => x.length).indexOf(length);
9435
+ lookupHistory.splice(index, index === -1 ? 0 : 1, lookup);
9436
+ }
9431
9437
  }
9432
9438
  }
9433
- }
9434
- else if (kind === UpdatePreimageKind.Remove) {
9435
- const { hash, length } = action;
9436
- service.data.preimages.delete(hash);
9437
- const history = service.data.lookupHistory.get(hash) ?? [];
9438
- const idx = history.map((x) => x.length).indexOf(length);
9439
- if (idx !== -1) {
9440
- history.splice(idx, 1);
9439
+ else if (kind === UpdatePreimageKind.Remove) {
9440
+ const { hash, length } = update.action;
9441
+ service.data.preimages.delete(hash);
9442
+ const history = service.data.lookupHistory.get(hash) ?? [];
9443
+ const idx = history.map((x) => x.length).indexOf(length);
9444
+ if (idx !== -1) {
9445
+ history.splice(idx, 1);
9446
+ }
9447
+ }
9448
+ else if (kind === UpdatePreimageKind.UpdateOrAdd) {
9449
+ const { item } = update.action;
9450
+ const history = service.data.lookupHistory.get(item.hash) ?? [];
9451
+ const existingIdx = history.map((x) => x.length).indexOf(item.length);
9452
+ const removeCount = existingIdx === -1 ? 0 : 1;
9453
+ history.splice(existingIdx, removeCount, item);
9454
+ service.data.lookupHistory.set(item.hash, history);
9455
+ }
9456
+ else {
9457
+ assertNever(kind);
9441
9458
  }
9442
- }
9443
- else if (kind === UpdatePreimageKind.UpdateOrAdd) {
9444
- const { item } = action;
9445
- const history = service.data.lookupHistory.get(item.hash) ?? [];
9446
- const existingIdx = history.map((x) => x.length).indexOf(item.length);
9447
- const removeCount = existingIdx === -1 ? 0 : 1;
9448
- history.splice(existingIdx, removeCount, item);
9449
- service.data.lookupHistory.set(item.hash, history);
9450
- }
9451
- else {
9452
- assertNever(kind);
9453
9459
  }
9454
9460
  }
9455
9461
  return Result$1.ok(OK);
9456
9462
  }
9457
9463
  updateServices(servicesUpdates) {
9458
- for (const { serviceId, action } of servicesUpdates ?? []) {
9459
- const { kind, account } = action;
9464
+ if (servicesUpdates === undefined) {
9465
+ return Result$1.ok(OK);
9466
+ }
9467
+ for (const [serviceId, update] of servicesUpdates.entries()) {
9468
+ const { kind, account } = update.action;
9460
9469
  if (kind === UpdateServiceKind.Create) {
9461
- const { lookupHistory } = action;
9470
+ const { lookupHistory } = update.action;
9462
9471
  if (this.services.has(serviceId)) {
9463
9472
  return Result$1.error(UpdateError.DuplicateService, `${serviceId} already exists!`);
9464
9473
  }
@@ -10726,76 +10735,88 @@ function* serializeStateUpdate(spec, blake2b, update) {
10726
10735
  yield* serializeBasicKeys(spec, update);
10727
10736
  const encode = (codec, val) => Encoder.encodeObject(codec, val, spec);
10728
10737
  // then let's proceed with service updates
10729
- yield* serializeServiceUpdates(update.servicesUpdates, encode, blake2b);
10738
+ yield* serializeServiceUpdates(update.updated, encode, blake2b);
10730
10739
  yield* serializePreimages(update.preimages, encode, blake2b);
10731
10740
  yield* serializeStorage(update.storage, blake2b);
10732
- yield* serializeRemovedServices(update.servicesRemoved);
10741
+ yield* serializeRemovedServices(update.removed);
10733
10742
  }
10734
10743
  function* serializeRemovedServices(servicesRemoved) {
10735
- for (const serviceId of servicesRemoved ?? []) {
10744
+ if (servicesRemoved === undefined) {
10745
+ return;
10746
+ }
10747
+ for (const serviceId of servicesRemoved) {
10736
10748
  // TODO [ToDr] what about all data associated with a service?
10737
10749
  const codec = serialize.serviceData(serviceId);
10738
10750
  yield [StateEntryUpdateAction.Remove, codec.key, EMPTY_BLOB];
10739
10751
  }
10740
10752
  }
10741
- function* serializeStorage(storage, blake2b) {
10742
- for (const { action, serviceId } of storage ?? []) {
10743
- switch (action.kind) {
10744
- case UpdateStorageKind.Set: {
10745
- const key = action.storage.key;
10746
- const codec = serialize.serviceStorage(blake2b, serviceId, key);
10747
- yield [StateEntryUpdateAction.Insert, codec.key, action.storage.value];
10748
- break;
10749
- }
10750
- case UpdateStorageKind.Remove: {
10751
- const key = action.key;
10752
- const codec = serialize.serviceStorage(blake2b, serviceId, key);
10753
- yield [StateEntryUpdateAction.Remove, codec.key, EMPTY_BLOB];
10754
- break;
10753
+ function* serializeStorage(storageUpdates, blake2b) {
10754
+ if (storageUpdates === undefined) {
10755
+ return;
10756
+ }
10757
+ for (const [serviceId, updates] of storageUpdates.entries()) {
10758
+ for (const { action } of updates) {
10759
+ switch (action.kind) {
10760
+ case UpdateStorageKind.Set: {
10761
+ const key = action.storage.key;
10762
+ const codec = serialize.serviceStorage(blake2b, serviceId, key);
10763
+ yield [StateEntryUpdateAction.Insert, codec.key, action.storage.value];
10764
+ break;
10765
+ }
10766
+ case UpdateStorageKind.Remove: {
10767
+ const key = action.key;
10768
+ const codec = serialize.serviceStorage(blake2b, serviceId, key);
10769
+ yield [StateEntryUpdateAction.Remove, codec.key, EMPTY_BLOB];
10770
+ break;
10771
+ }
10755
10772
  }
10756
- default:
10757
- assertNever(action);
10758
10773
  }
10759
10774
  }
10760
10775
  }
10761
- function* serializePreimages(preimages, encode, blake2b) {
10762
- for (const { action, serviceId } of preimages ?? []) {
10763
- switch (action.kind) {
10764
- case UpdatePreimageKind.Provide: {
10765
- const { hash, blob } = action.preimage;
10766
- const codec = serialize.servicePreimages(blake2b, serviceId, hash);
10767
- yield [StateEntryUpdateAction.Insert, codec.key, blob];
10768
- if (action.slot !== null) {
10769
- const codec2 = serialize.serviceLookupHistory(blake2b, serviceId, hash, tryAsU32(blob.length));
10770
- yield [
10771
- StateEntryUpdateAction.Insert,
10772
- codec2.key,
10773
- encode(codec2.Codec, tryAsLookupHistorySlots([action.slot])),
10774
- ];
10776
+ function* serializePreimages(preimagesUpdates, encode, blake2b) {
10777
+ if (preimagesUpdates === undefined) {
10778
+ return;
10779
+ }
10780
+ for (const [serviceId, updates] of preimagesUpdates.entries()) {
10781
+ for (const { action } of updates) {
10782
+ switch (action.kind) {
10783
+ case UpdatePreimageKind.Provide: {
10784
+ const { hash, blob } = action.preimage;
10785
+ const codec = serialize.servicePreimages(blake2b, serviceId, hash);
10786
+ yield [StateEntryUpdateAction.Insert, codec.key, blob];
10787
+ if (action.slot !== null) {
10788
+ const codec2 = serialize.serviceLookupHistory(blake2b, serviceId, hash, tryAsU32(blob.length));
10789
+ yield [
10790
+ StateEntryUpdateAction.Insert,
10791
+ codec2.key,
10792
+ encode(codec2.Codec, tryAsLookupHistorySlots([action.slot])),
10793
+ ];
10794
+ }
10795
+ break;
10796
+ }
10797
+ case UpdatePreimageKind.UpdateOrAdd: {
10798
+ const { hash, length, slots } = action.item;
10799
+ const codec = serialize.serviceLookupHistory(blake2b, serviceId, hash, length);
10800
+ yield [StateEntryUpdateAction.Insert, codec.key, encode(codec.Codec, slots)];
10801
+ break;
10802
+ }
10803
+ case UpdatePreimageKind.Remove: {
10804
+ const { hash, length } = action;
10805
+ const codec = serialize.servicePreimages(blake2b, serviceId, hash);
10806
+ yield [StateEntryUpdateAction.Remove, codec.key, EMPTY_BLOB];
10807
+ const codec2 = serialize.serviceLookupHistory(blake2b, serviceId, hash, length);
10808
+ yield [StateEntryUpdateAction.Remove, codec2.key, EMPTY_BLOB];
10809
+ break;
10775
10810
  }
10776
- break;
10777
- }
10778
- case UpdatePreimageKind.UpdateOrAdd: {
10779
- const { hash, length, slots } = action.item;
10780
- const codec = serialize.serviceLookupHistory(blake2b, serviceId, hash, length);
10781
- yield [StateEntryUpdateAction.Insert, codec.key, encode(codec.Codec, slots)];
10782
- break;
10783
- }
10784
- case UpdatePreimageKind.Remove: {
10785
- const { hash, length } = action;
10786
- const codec = serialize.servicePreimages(blake2b, serviceId, hash);
10787
- yield [StateEntryUpdateAction.Remove, codec.key, EMPTY_BLOB];
10788
- const codec2 = serialize.serviceLookupHistory(blake2b, serviceId, hash, length);
10789
- yield [StateEntryUpdateAction.Remove, codec2.key, EMPTY_BLOB];
10790
- break;
10791
10811
  }
10792
- default:
10793
- assertNever(action);
10794
10812
  }
10795
10813
  }
10796
10814
  }
10797
10815
  function* serializeServiceUpdates(servicesUpdates, encode, blake2b) {
10798
- for (const { action, serviceId } of servicesUpdates ?? []) {
10816
+ if (servicesUpdates === undefined) {
10817
+ return;
10818
+ }
10819
+ for (const [serviceId, { action }] of servicesUpdates.entries()) {
10799
10820
  // new service being created or updated
10800
10821
  const codec = serialize.serviceData(serviceId);
10801
10822
  yield [StateEntryUpdateAction.Insert, codec.key, encode(codec.Codec, action.account)];
@@ -12392,6 +12413,14 @@ const NoMachineError = Symbol("Machine index not found.");
12392
12413
  const SegmentExportError = Symbol("Too many segments already exported.");
12393
12414
 
12394
12415
  const InsufficientFundsError = "insufficient funds";
12416
+ /** Deep clone of a map with array. */
12417
+ function deepCloneMapWithArray(map) {
12418
+ const cloned = [];
12419
+ for (const [k, v] of map.entries()) {
12420
+ cloned.push([k, v.slice()]);
12421
+ }
12422
+ return new Map(cloned);
12423
+ }
12395
12424
  /**
12396
12425
  * State updates that currently accumulating service produced.
12397
12426
  *
@@ -12421,10 +12450,11 @@ class AccumulationStateUpdate {
12421
12450
  /** Create new empty state update. */
12422
12451
  static empty() {
12423
12452
  return new AccumulationStateUpdate({
12424
- servicesUpdates: [],
12425
- servicesRemoved: [],
12426
- preimages: [],
12427
- storage: [],
12453
+ created: [],
12454
+ updated: new Map(),
12455
+ removed: [],
12456
+ preimages: new Map(),
12457
+ storage: new Map(),
12428
12458
  }, []);
12429
12459
  }
12430
12460
  /** Create a state update with some existing, yet uncommited services updates. */
@@ -12436,10 +12466,13 @@ class AccumulationStateUpdate {
12436
12466
  /** Create a copy of another `StateUpdate`. Used by checkpoints. */
12437
12467
  static copyFrom(from) {
12438
12468
  const serviceUpdates = {
12439
- servicesUpdates: [...from.services.servicesUpdates],
12440
- servicesRemoved: [...from.services.servicesRemoved],
12441
- preimages: [...from.services.preimages],
12442
- storage: [...from.services.storage],
12469
+ // shallow copy
12470
+ created: [...from.services.created],
12471
+ updated: new Map(from.services.updated),
12472
+ removed: [...from.services.removed],
12473
+ // deep copy
12474
+ preimages: deepCloneMapWithArray(from.services.preimages),
12475
+ storage: deepCloneMapWithArray(from.services.storage),
12443
12476
  };
12444
12477
  const transfers = [...from.transfers];
12445
12478
  const update = new AccumulationStateUpdate(serviceUpdates, transfers, new Map(from.yieldedRoots));
@@ -12487,9 +12520,9 @@ class PartiallyUpdatedState {
12487
12520
  if (destination === null) {
12488
12521
  return null;
12489
12522
  }
12490
- const maybeNewService = this.stateUpdate.services.servicesUpdates.find((update) => update.serviceId === destination);
12491
- if (maybeNewService !== undefined) {
12492
- return maybeNewService.action.account;
12523
+ const maybeUpdatedServiceInfo = this.stateUpdate.services.updated.get(destination);
12524
+ if (maybeUpdatedServiceInfo !== undefined) {
12525
+ return maybeUpdatedServiceInfo.action.account;
12493
12526
  }
12494
12527
  const maybeService = this.state.getService(destination);
12495
12528
  if (maybeService === null) {
@@ -12498,7 +12531,8 @@ class PartiallyUpdatedState {
12498
12531
  return maybeService.getInfo();
12499
12532
  }
12500
12533
  getStorage(serviceId, rawKey) {
12501
- const item = this.stateUpdate.services.storage.find((x) => x.serviceId === serviceId && x.key.isEqualTo(rawKey));
12534
+ const storages = this.stateUpdate.services.storage.get(serviceId) ?? [];
12535
+ const item = storages.find((x) => x.key.isEqualTo(rawKey));
12502
12536
  if (item !== undefined) {
12503
12537
  return item.value;
12504
12538
  }
@@ -12513,10 +12547,11 @@ class PartiallyUpdatedState {
12513
12547
  * the existence in `preimages` map.
12514
12548
  */
12515
12549
  hasPreimage(serviceId, hash) {
12516
- const providedPreimage = this.stateUpdate.services.preimages.find(
12550
+ const preimages = this.stateUpdate.services.preimages.get(serviceId) ?? [];
12551
+ const providedPreimage = preimages.find(
12517
12552
  // we ignore the action here, since if there is <any> update on that
12518
12553
  // hash it means it has to exist, right?
12519
- (p) => p.serviceId === serviceId && p.hash.isEqualTo(hash));
12554
+ (p) => p.hash.isEqualTo(hash));
12520
12555
  if (providedPreimage !== undefined) {
12521
12556
  return true;
12522
12557
  }
@@ -12529,7 +12564,8 @@ class PartiallyUpdatedState {
12529
12564
  }
12530
12565
  getPreimage(serviceId, hash) {
12531
12566
  // TODO [ToDr] Should we verify availability here?
12532
- const freshlyProvided = this.stateUpdate.services.preimages.find((x) => x.serviceId === serviceId && x.hash.isEqualTo(hash));
12567
+ const preimages = this.stateUpdate.services.preimages.get(serviceId) ?? [];
12568
+ const freshlyProvided = preimages.find((x) => x.hash.isEqualTo(hash));
12533
12569
  if (freshlyProvided !== undefined && freshlyProvided.action.kind === UpdatePreimageKind.Provide) {
12534
12570
  return freshlyProvided.action.preimage.blob;
12535
12571
  }
@@ -12538,10 +12574,11 @@ class PartiallyUpdatedState {
12538
12574
  }
12539
12575
  /** Get status of a preimage of current service taking into account any updates. */
12540
12576
  getLookupHistory(currentTimeslot, serviceId, hash, length) {
12577
+ const preimages = this.stateUpdate.services.preimages.get(serviceId) ?? [];
12541
12578
  // TODO [ToDr] This is most likely wrong. We may have `provide` and `remove` within
12542
12579
  // the same state update. We should however switch to proper "updated state"
12543
12580
  // representation soon.
12544
- const updatedPreimage = this.stateUpdate.services.preimages.findLast((update) => update.serviceId === serviceId && update.hash.isEqualTo(hash) && BigInt(update.length) === length);
12581
+ const updatedPreimage = preimages.findLast((update) => update.hash.isEqualTo(hash) && BigInt(update.length) === length);
12545
12582
  const stateFallback = () => {
12546
12583
  // fallback to state lookup
12547
12584
  const service = this.state.getService(serviceId);
@@ -12578,14 +12615,15 @@ class PartiallyUpdatedState {
12578
12615
  /* State update functions. */
12579
12616
  updateStorage(serviceId, key, value) {
12580
12617
  const update = value === null
12581
- ? UpdateStorage.remove({ serviceId, key })
12618
+ ? UpdateStorage.remove({ key })
12582
12619
  : UpdateStorage.set({
12583
- serviceId,
12584
12620
  storage: StorageItem.create({ key, value }),
12585
12621
  });
12586
- const index = this.stateUpdate.services.storage.findIndex((x) => x.serviceId === update.serviceId && x.key.isEqualTo(key));
12622
+ const storages = this.stateUpdate.services.storage.get(serviceId) ?? [];
12623
+ const index = storages.findIndex((x) => x.key.isEqualTo(key));
12587
12624
  const count = index === -1 ? 0 : 1;
12588
- this.stateUpdate.services.storage.splice(index, count, update);
12625
+ storages.splice(index, count, update);
12626
+ this.stateUpdate.services.storage.set(serviceId, storages);
12589
12627
  }
12590
12628
  /**
12591
12629
  * Update a preimage.
@@ -12593,8 +12631,10 @@ class PartiallyUpdatedState {
12593
12631
  * Note we store all previous entries as well, since there might be a sequence of:
12594
12632
  * `provide` -> `remove` and both should update the end state somehow.
12595
12633
  */
12596
- updatePreimage(newUpdate) {
12597
- this.stateUpdate.services.preimages.push(newUpdate);
12634
+ updatePreimage(serviceId, newUpdate) {
12635
+ const updatePreimages = this.stateUpdate.services.preimages.get(serviceId) ?? [];
12636
+ updatePreimages.push(newUpdate);
12637
+ this.stateUpdate.services.preimages.set(serviceId, updatePreimages);
12598
12638
  }
12599
12639
  updateServiceStorageUtilisation(serviceId, items, bytes, serviceInfo) {
12600
12640
  check `${items >= 0} storageUtilisationCount has to be a positive number, got: ${items}`;
@@ -12618,20 +12658,23 @@ class PartiallyUpdatedState {
12618
12658
  return Result$1.ok(OK);
12619
12659
  }
12620
12660
  updateServiceInfo(serviceId, newInfo) {
12621
- const idx = this.stateUpdate.services.servicesUpdates.findIndex((x) => x.serviceId === serviceId);
12622
- const toRemove = idx === -1 ? 0 : 1;
12623
- const existingItem = this.stateUpdate.services.servicesUpdates[idx];
12624
- if (existingItem?.action.kind === UpdateServiceKind.Create) {
12625
- this.stateUpdate.services.servicesUpdates.splice(idx, toRemove, UpdateService.create({
12626
- serviceId,
12661
+ const existingUpdate = this.stateUpdate.services.updated.get(serviceId);
12662
+ if (existingUpdate?.action.kind === UpdateServiceKind.Create) {
12663
+ this.stateUpdate.services.updated.set(serviceId, UpdateService.create({
12627
12664
  serviceInfo: newInfo,
12628
- lookupHistory: existingItem.action.lookupHistory,
12665
+ lookupHistory: existingUpdate.action.lookupHistory,
12629
12666
  }));
12630
12667
  return;
12631
12668
  }
12632
- this.stateUpdate.services.servicesUpdates.splice(idx, toRemove, UpdateService.update({
12633
- serviceId,
12669
+ this.stateUpdate.services.updated.set(serviceId, UpdateService.update({
12670
+ serviceInfo: newInfo,
12671
+ }));
12672
+ }
12673
+ createService(serviceId, newInfo, newLookupHistory) {
12674
+ this.stateUpdate.services.created.push(serviceId);
12675
+ this.stateUpdate.services.updated.set(serviceId, UpdateService.create({
12634
12676
  serviceInfo: newInfo,
12677
+ lookupHistory: newLookupHistory,
12635
12678
  }));
12636
12679
  }
12637
12680
  getPrivilegedServices() {
@@ -16509,9 +16552,6 @@ class HostCallMemory {
16509
16552
  }
16510
16553
  return this.memory.loadInto(result, tryAsMemoryIndex(Number(startAddress)));
16511
16554
  }
16512
- getMemory() {
16513
- return this.memory;
16514
- }
16515
16555
  }
16516
16556
 
16517
16557
  class HostCallRegisters {
@@ -17603,7 +17643,7 @@ class Preimages {
17603
17643
  }
17604
17644
  }
17605
17645
  const { preimages, slot } = input;
17606
- const pendingChanges = [];
17646
+ const pendingChanges = new Map();
17607
17647
  // select preimages for integration
17608
17648
  for (const preimage of preimages) {
17609
17649
  const { requester, blob } = preimage;
@@ -17620,11 +17660,12 @@ class Preimages {
17620
17660
  return Result$1.error(PreimagesErrorCode.PreimageUnneeded);
17621
17661
  }
17622
17662
  // https://graypaper.fluffylabs.dev/#/5f542d7/18c00018f300
17623
- pendingChanges.push(UpdatePreimage.provide({
17624
- serviceId: requester,
17663
+ const updates = pendingChanges.get(requester) ?? [];
17664
+ updates.push(UpdatePreimage.provide({
17625
17665
  preimage: PreimageItem.create({ hash, blob }),
17626
17666
  slot,
17627
17667
  }));
17668
+ pendingChanges.set(requester, updates);
17628
17669
  }
17629
17670
  return Result$1.ok({
17630
17671
  preimages: pendingChanges,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@typeberry/lib",
3
- "version": "0.2.0-74f246e",
3
+ "version": "0.2.0-8017bfd",
4
4
  "main": "index.js",
5
5
  "author": "Fluffy Labs",
6
6
  "license": "MPL-2.0",