@typeberry/convert 0.5.3 → 0.5.4

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 (3) hide show
  1. package/index.js +99 -66
  2. package/index.js.map +1 -1
  3. package/package.json +1 -1
package/index.js CHANGED
@@ -4956,7 +4956,7 @@ function isResult(x) {
4956
4956
  var minimist = __nccwpck_require__(595);
4957
4957
  var minimist_default = /*#__PURE__*/__nccwpck_require__.n(minimist);
4958
4958
  ;// CONCATENATED MODULE: ./bin/convert/package.json
4959
- const package_namespaceObject = {"rE":"0.5.3"};
4959
+ const package_namespaceObject = {"rE":"0.5.4"};
4960
4960
  ;// CONCATENATED MODULE: ./packages/core/bytes/bitvec.ts
4961
4961
 
4962
4962
  /**
@@ -5346,7 +5346,7 @@ const bytesBlobComparator = (a, b) => a.compare(b);
5346
5346
  const asTypedNumber = (v) => v;
5347
5347
  const MAX_VALUE_U8 = 0xff;
5348
5348
  const MAX_VALUE_U16 = 0xffff;
5349
- const MAX_VALUE_U32 = 0xffff_ffff;
5349
+ const numbers_MAX_VALUE_U32 = 0xffff_ffff;
5350
5350
  const MAX_VALUE_U64 = 0xffffffffffffffffn;
5351
5351
  /** Attempt to cast an input number into U8. */
5352
5352
  const tryAsU8 = (v) => {
@@ -5368,7 +5368,7 @@ const numbers_tryAsU32 = (v) => {
5368
5368
  return asTypedNumber(v);
5369
5369
  };
5370
5370
  /** Check if given number is a valid U32 number. */
5371
- const isU32 = (v) => (v & MAX_VALUE_U32) >>> 0 === v;
5371
+ const isU32 = (v) => (v & numbers_MAX_VALUE_U32) >>> 0 === v;
5372
5372
  /** Attempt to cast an input number into U64. */
5373
5373
  const numbers_tryAsU64 = (x) => {
5374
5374
  const v = BigInt(x);
@@ -7743,6 +7743,7 @@ const ZERO_HASH = bytes_Bytes.zero(hash_HASH_SIZE);
7743
7743
  class WithHash extends WithDebug {
7744
7744
  hash;
7745
7745
  data;
7746
+ // TODO [ToDr] use static method and make constructor private
7746
7747
  constructor(hash, data) {
7747
7748
  super();
7748
7749
  this.hash = hash;
@@ -9751,14 +9752,18 @@ class work_item_WorkItem extends WithDebug {
9751
9752
 
9752
9753
 
9753
9754
 
9754
- /** Verify the value is within the `WorkItemsCount` bounds. */
9755
+ /** Convert the value to `WorkItemsCount` bounds. */
9755
9756
  function tryAsWorkItemsCount(len) {
9756
9757
  debug_check `
9757
- ${len >= MIN_NUMBER_OF_WORK_ITEMS && len <= work_package_MAX_NUMBER_OF_WORK_ITEMS}
9758
+ ${isWorkItemsCount(len)}
9758
9759
  WorkItemsCount: Expected '${MIN_NUMBER_OF_WORK_ITEMS} <= count <= ${work_package_MAX_NUMBER_OF_WORK_ITEMS}' got ${len}
9759
9760
  `;
9760
9761
  return tryAsU8(len);
9761
9762
  }
9763
+ /** Verify the value is within the `WorkItemsCount` bounds. */
9764
+ function isWorkItemsCount(len) {
9765
+ return len >= MIN_NUMBER_OF_WORK_ITEMS && len <= work_package_MAX_NUMBER_OF_WORK_ITEMS;
9766
+ }
9762
9767
  /** Minimal number of work items in the work package or results in work report. */
9763
9768
  const MIN_NUMBER_OF_WORK_ITEMS = 1;
9764
9769
  /** `I`: Maximal number of work items in the work package or results in work report. */
@@ -9859,6 +9864,12 @@ class WorkExecResult extends WithDebug {
9859
9864
  }
9860
9865
  return { kind: x.kind };
9861
9866
  }, (x) => new WorkExecResult(x.kind, x.kind === WorkExecResultKind.ok ? x.okBlob : null));
9867
+ static ok(blob) {
9868
+ return new WorkExecResult(WorkExecResultKind.ok, blob);
9869
+ }
9870
+ static error(kind) {
9871
+ return new WorkExecResult(kind, null);
9872
+ }
9862
9873
  constructor(
9863
9874
  /** The execution result tag. */
9864
9875
  kind,
@@ -10949,22 +10960,22 @@ const workExecResultFromJson = json.object({
10949
10960
  }, (val) => {
10950
10961
  const { ok, out_of_gas, panic, bad_code, code_oversize, output_oversize } = val;
10951
10962
  if (ok !== undefined) {
10952
- return new WorkExecResult(numbers_tryAsU32(WorkExecResultKind.ok), ok);
10963
+ return WorkExecResult.ok(ok);
10953
10964
  }
10954
10965
  if (out_of_gas === null) {
10955
- return new WorkExecResult(numbers_tryAsU32(WorkExecResultKind.outOfGas));
10966
+ return WorkExecResult.error(WorkExecResultKind.outOfGas);
10956
10967
  }
10957
10968
  if (panic === null) {
10958
- return new WorkExecResult(numbers_tryAsU32(WorkExecResultKind.panic));
10969
+ return WorkExecResult.error(WorkExecResultKind.panic);
10959
10970
  }
10960
10971
  if (bad_code === null) {
10961
- return new WorkExecResult(numbers_tryAsU32(WorkExecResultKind.badCode));
10972
+ return WorkExecResult.error(WorkExecResultKind.badCode);
10962
10973
  }
10963
10974
  if (code_oversize === null) {
10964
- return new WorkExecResult(numbers_tryAsU32(WorkExecResultKind.codeOversize));
10975
+ return WorkExecResult.error(WorkExecResultKind.codeOversize);
10965
10976
  }
10966
10977
  if (output_oversize === null) {
10967
- return new WorkExecResult(numbers_tryAsU32(WorkExecResultKind.digestTooBig));
10978
+ return WorkExecResult.error(WorkExecResultKind.digestTooBig);
10968
10979
  }
10969
10980
  throw new Error("Invalid WorkExecResult");
10970
10981
  });
@@ -11162,6 +11173,13 @@ const blockFromJson = (spec) => json.object({
11162
11173
  header: headerFromJson,
11163
11174
  extrinsic: getExtrinsicFromJson(spec),
11164
11175
  }, ({ header, extrinsic }) => Block.create({ header, extrinsic }));
11176
+ const blockViewFromJson = (spec) => {
11177
+ const parseBlock = blockFromJson(spec);
11178
+ return json.fromAny((p) => {
11179
+ const block = parse_parseFromJson(p, parseBlock);
11180
+ return reencodeAsView(Block.Codec, block, spec);
11181
+ });
11182
+ };
11165
11183
 
11166
11184
  ;// CONCATENATED MODULE: ./packages/jam/block-json/index.ts
11167
11185
 
@@ -11656,13 +11674,13 @@ const getStateCodec = codec_codec.bytes(hash_HASH_SIZE).asOpaque();
11656
11674
  /** StateRoot ::= StateRootHash */
11657
11675
  const stateRootCodec = codec_codec.bytes(hash_HASH_SIZE).asOpaque();
11658
11676
  /** Error ::= UTF8String */
11659
- class ErrorMessage extends WithDebug {
11677
+ class types_ErrorMessage extends WithDebug {
11660
11678
  message;
11661
- static Codec = codec_codec.Class(ErrorMessage, {
11679
+ static Codec = codec_codec.Class(types_ErrorMessage, {
11662
11680
  message: codec_codec.string,
11663
11681
  });
11664
11682
  static create({ message }) {
11665
- return new ErrorMessage(message);
11683
+ return new types_ErrorMessage(message);
11666
11684
  }
11667
11685
  constructor(message) {
11668
11686
  super();
@@ -11716,7 +11734,7 @@ const types_messageCodec = codec_codec.custom({
11716
11734
  stateCodec.encode(e, msg.value);
11717
11735
  break;
11718
11736
  case types_MessageType.Error:
11719
- ErrorMessage.Codec.encode(e, msg.value);
11737
+ types_ErrorMessage.Codec.encode(e, msg.value);
11720
11738
  break;
11721
11739
  default:
11722
11740
  throw new Error(`Unknown message type: ${msg}`);
@@ -11737,7 +11755,7 @@ const types_messageCodec = codec_codec.custom({
11737
11755
  case types_MessageType.State:
11738
11756
  return { type: types_MessageType.State, value: stateCodec.decode(d) };
11739
11757
  case types_MessageType.Error:
11740
- return { type: types_MessageType.Error, value: ErrorMessage.Codec.decode(d) };
11758
+ return { type: types_MessageType.Error, value: types_ErrorMessage.Codec.decode(d) };
11741
11759
  default:
11742
11760
  throw new Error(`Unknown message type: ${type}`);
11743
11761
  }
@@ -11763,7 +11781,7 @@ const types_messageCodec = codec_codec.custom({
11763
11781
  stateCodec.View.skip(s);
11764
11782
  break;
11765
11783
  case types_MessageType.Error:
11766
- ErrorMessage.Codec.View.skip(s);
11784
+ types_ErrorMessage.Codec.View.skip(s);
11767
11785
  break;
11768
11786
  default:
11769
11787
  throw new Error(`Unknown message type: ${type}`);
@@ -11826,35 +11844,59 @@ class FuzzTarget {
11826
11844
  break;
11827
11845
  }
11828
11846
  case MessageType.Initialize: {
11829
- const stateRoot = await this.msgHandler.initialize(message.value);
11830
- response = {
11831
- type: MessageType.StateRoot,
11832
- value: stateRoot,
11833
- };
11834
- break;
11835
- }
11836
- case MessageType.ImportBlock: {
11837
- const result = await this.msgHandler.importBlock(message.value);
11838
- if (result.isOk) {
11847
+ try {
11848
+ const stateRoot = await this.msgHandler.initialize(message.value);
11839
11849
  response = {
11840
11850
  type: MessageType.StateRoot,
11841
- value: result.ok,
11851
+ value: stateRoot,
11842
11852
  };
11843
11853
  }
11844
- else {
11854
+ catch (e) {
11845
11855
  response = {
11846
11856
  type: MessageType.Error,
11847
- value: result.error,
11857
+ value: ErrorMessage.create({ message: `initialize error: ${e}` }),
11858
+ };
11859
+ }
11860
+ break;
11861
+ }
11862
+ case MessageType.ImportBlock: {
11863
+ try {
11864
+ const result = await this.msgHandler.importBlock(message.value);
11865
+ if (result.isOk) {
11866
+ response = {
11867
+ type: MessageType.StateRoot,
11868
+ value: result.ok,
11869
+ };
11870
+ }
11871
+ else {
11872
+ response = {
11873
+ type: MessageType.Error,
11874
+ value: result.error,
11875
+ };
11876
+ }
11877
+ }
11878
+ catch (e) {
11879
+ response = {
11880
+ type: MessageType.Error,
11881
+ value: ErrorMessage.create({ message: `importBlock error: ${e}` }),
11848
11882
  };
11849
11883
  }
11850
11884
  break;
11851
11885
  }
11852
11886
  case MessageType.GetState: {
11853
- const state = await this.msgHandler.getSerializedState(message.value);
11854
- response = {
11855
- type: MessageType.State,
11856
- value: state,
11857
- };
11887
+ try {
11888
+ const state = await this.msgHandler.getSerializedState(message.value);
11889
+ response = {
11890
+ type: MessageType.State,
11891
+ value: state,
11892
+ };
11893
+ }
11894
+ catch (e) {
11895
+ response = {
11896
+ type: MessageType.Error,
11897
+ value: ErrorMessage.create({ message: `getState error: ${e}` }),
11898
+ };
11899
+ }
11858
11900
  break;
11859
11901
  }
11860
11902
  case MessageType.StateRoot: {
@@ -12314,13 +12356,6 @@ function hashComparator(a, b) {
12314
12356
  return a.compare(b);
12315
12357
  }
12316
12358
 
12317
- ;// CONCATENATED MODULE: ./packages/core/pvm-interpreter/ops/math-consts.ts
12318
- const math_consts_MAX_VALUE = 4294967295;
12319
- const math_consts_MAX_VALUE_U64 = (/* unused pure expression or super */ null && (2n ** 63n));
12320
- const math_consts_MIN_VALUE = (/* unused pure expression or super */ null && (-(2 ** 31)));
12321
- const math_consts_MAX_SHIFT_U32 = 32;
12322
- const math_consts_MAX_SHIFT_U64 = 64n;
12323
-
12324
12359
  ;// CONCATENATED MODULE: ./packages/jam/state/recent-blocks.ts
12325
12360
 
12326
12361
 
@@ -13300,7 +13335,6 @@ class UpdateStorage {
13300
13335
 
13301
13336
 
13302
13337
 
13303
-
13304
13338
 
13305
13339
 
13306
13340
  var UpdateError;
@@ -13733,7 +13767,7 @@ class InMemoryState extends WithDebug {
13733
13767
  manager: tryAsServiceId(0),
13734
13768
  assigners: tryAsPerCore(new Array(spec.coresCount).fill(tryAsServiceId(0)), spec),
13735
13769
  delegator: tryAsServiceId(0),
13736
- registrar: tryAsServiceId(math_consts_MAX_VALUE),
13770
+ registrar: tryAsServiceId(numbers_MAX_VALUE_U32),
13737
13771
  autoAccumulateServices: new Map(),
13738
13772
  }),
13739
13773
  accumulationOutputLog: SortedArray.fromArray(accumulationOutputComparator, []),
@@ -16065,11 +16099,11 @@ class StateTransition {
16065
16099
  static fromJson = {
16066
16100
  pre_state: TestState.fromJson,
16067
16101
  post_state: TestState.fromJson,
16068
- block: blockFromJson(chain_spec_tinyChainSpec),
16102
+ block: blockViewFromJson(chain_spec_tinyChainSpec),
16069
16103
  };
16070
16104
  static Codec = codec_codec.object({
16071
16105
  pre_state: TestState.Codec,
16072
- block: Block.Codec,
16106
+ block: Block.Codec.View,
16073
16107
  post_state: TestState.Codec,
16074
16108
  });
16075
16109
  pre_state;
@@ -17893,22 +17927,22 @@ class memory_builder_MemoryBuilder {
17893
17927
  * Overflowing addition for two-complement representation of 32-bit signed numbers.
17894
17928
  */
17895
17929
  function math_utils_addWithOverflowU32(a, b) {
17896
- if (a > MAX_VALUE - b) {
17930
+ if (a > MAX_VALUE_U32 - b) {
17897
17931
  /**
17898
- * MAX_VALUE is equal to 2 ** 32 - 1
17899
- * MAX_VALUE - ( (MAX_VALUE - a) + (MAX_VALUE - b) ) - 1
17900
- * = MAX_VALUE - (2MAX_VALUE - a - b) -1
17901
- * = MAX_VALUE - 2MAX_VALUE + a + b - 1
17902
- * = a + b - MAX_VALUE - 1
17932
+ * MAX_VALUE_U32 is equal to 2 ** 32 - 1
17933
+ * MAX_VALUE_U32 - ( (MAX_VALUE_U32 - a) + (MAX_VALUE_U32 - b) ) - 1
17934
+ * = MAX_VALUE_U32 - (2MAX_VALUE_U32 - a - b) -1
17935
+ * = MAX_VALUE_U32 - 2MAX_VALUE_U32 + a + b - 1
17936
+ * = a + b - MAX_VALUE_U32 - 1
17903
17937
  * = a + b - 2 ** 32
17904
- * but we know that 2MAX_VALUE > a + b > MAX_VALUE so in this case:
17938
+ * but we know that 2MAX_VALUE_U32 > a + b > MAX_VALUE_U32 so in this case:
17905
17939
  * a + b - 2 ** 32 <=> (a + b) % 2 ** 32
17906
- * = (a + b) % (MAX_VALUE + 1)
17940
+ * = (a + b) % (MAX_VALUE_U32 + 1)
17907
17941
  */
17908
- const spaceToMaxA = MAX_VALUE - a;
17909
- const spaceToMaxB = MAX_VALUE - b;
17942
+ const spaceToMaxA = MAX_VALUE_U32 - a;
17943
+ const spaceToMaxB = MAX_VALUE_U32 - b;
17910
17944
  const overflowSum = spaceToMaxA + spaceToMaxB;
17911
- return MAX_VALUE - overflowSum - 1;
17945
+ return MAX_VALUE_U32 - overflowSum - 1;
17912
17946
  }
17913
17947
  return a + b;
17914
17948
  }
@@ -17923,7 +17957,7 @@ function math_utils_addWithOverflowU64(a, b) {
17923
17957
  */
17924
17958
  function math_utils_subU32(a, b) {
17925
17959
  if (b > a) {
17926
- return MAX_VALUE - b + a + 1;
17960
+ return MAX_VALUE_U32 - b + a + 1;
17927
17961
  }
17928
17962
  return a - b;
17929
17963
  }
@@ -18509,7 +18543,7 @@ class math_ops_MathOps {
18509
18543
  if (this.regs.getLowerU32(secondIndex) === 0) {
18510
18544
  this.regs.setU64(resultIndex, 2n ** 64n - 1n);
18511
18545
  }
18512
- else if (this.regs.getLowerI32(secondIndex) === -1 && this.regs.getLowerI32(firstIndex) === MIN_VALUE) {
18546
+ else if (this.regs.getLowerI32(secondIndex) === -1 && this.regs.getLowerI32(firstIndex) === MIN_VALUE_I32) {
18513
18547
  this.regs.setU64(resultIndex, signExtend32To64(this.regs.getLowerU32(firstIndex)));
18514
18548
  }
18515
18549
  else {
@@ -18547,7 +18581,7 @@ class math_ops_MathOps {
18547
18581
  if (this.regs.getLowerU32(secondIndex) === 0) {
18548
18582
  this.regs.setU64(resultIndex, BigInt(this.regs.getLowerI32(firstIndex)));
18549
18583
  }
18550
- else if (this.regs.getLowerI32(secondIndex) === -1 && this.regs.getLowerI32(firstIndex) === MIN_VALUE) {
18584
+ else if (this.regs.getLowerI32(secondIndex) === -1 && this.regs.getLowerI32(firstIndex) === MIN_VALUE_I32) {
18551
18585
  this.regs.setU64(resultIndex, 0n);
18552
18586
  }
18553
18587
  else {
@@ -20273,7 +20307,7 @@ const SUPPORTED_TYPES = [
20273
20307
  if (option === "as-block") {
20274
20308
  return looseType({
20275
20309
  encode: Block.Codec,
20276
- value: test.block,
20310
+ value: test.block.materialize(),
20277
20311
  });
20278
20312
  }
20279
20313
  if (option === "as-fuzz-message") {
@@ -20281,11 +20315,9 @@ const SUPPORTED_TYPES = [
20281
20315
  console.warn("⚠️ Warning: 'as-fuzz-message' is deprecated and will be removed in version 0.6.0. Use 'as-block-fuzz-message' instead.");
20282
20316
  }
20283
20317
  if (option === "as-block-fuzz-message" || option === "as-fuzz-message") {
20284
- const encoded = encoder_Encoder.encodeObject(Block.Codec, test.block, spec);
20285
- const blockView = decoder_Decoder.decodeObject(Block.Codec.View, encoded, spec);
20286
20318
  const msg = {
20287
20319
  type: types_MessageType.ImportBlock,
20288
- value: blockView,
20320
+ value: test.block,
20289
20321
  };
20290
20322
  return looseType({
20291
20323
  value: msg,
@@ -20293,13 +20325,14 @@ const SUPPORTED_TYPES = [
20293
20325
  });
20294
20326
  }
20295
20327
  if (option === "as-state-fuzz-message") {
20328
+ const blockHeader = test.block.header.materialize();
20296
20329
  const init = Initialize.create({
20297
20330
  header: header_Header.empty(),
20298
20331
  keyvals: test.pre_state.keyvals,
20299
20332
  ancestry: [
20300
20333
  AncestryItem.create({
20301
- headerHash: test.block.header.parentHeaderHash,
20302
- slot: common_tryAsTimeSlot(Math.max(0, test.block.header.timeSlotIndex - 1)),
20334
+ headerHash: blockHeader.parentHeaderHash,
20335
+ slot: common_tryAsTimeSlot(Math.max(0, blockHeader.timeSlotIndex - 1)),
20303
20336
  }),
20304
20337
  ],
20305
20338
  });