@typeberry/lib 0.1.1-e48de40 → 0.1.2-0416cb6

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.
package/index.cjs CHANGED
@@ -10,6 +10,7 @@ var GpVersion;
10
10
  GpVersion["V0_6_7"] = "0.6.7";
11
11
  GpVersion["V0_7_0"] = "0.7.0";
12
12
  GpVersion["V0_7_1"] = "0.7.1-preview";
13
+ GpVersion["V0_7_2"] = "0.7.2-preview";
13
14
  })(GpVersion || (GpVersion = {}));
14
15
  var TestSuite;
15
16
  (function (TestSuite) {
@@ -17,7 +18,7 @@ var TestSuite;
17
18
  TestSuite["JAMDUNA"] = "jamduna";
18
19
  })(TestSuite || (TestSuite = {}));
19
20
  const DEFAULT_SUITE = TestSuite.W3F_DAVXY;
20
- const ALL_VERSIONS_IN_ORDER = [GpVersion.V0_6_7, GpVersion.V0_7_0, GpVersion.V0_7_1];
21
+ const ALL_VERSIONS_IN_ORDER = [GpVersion.V0_6_7, GpVersion.V0_7_0, GpVersion.V0_7_1, GpVersion.V0_7_2];
21
22
  const env$1 = typeof process === "undefined" ? {} : process.env;
22
23
  const DEFAULT_VERSION = GpVersion.V0_7_0;
23
24
  let CURRENT_VERSION = parseCurrentVersion(env$1.GP_VERSION) ?? DEFAULT_VERSION;
@@ -26,20 +27,26 @@ function parseCurrentVersion(env) {
26
27
  if (env === undefined) {
27
28
  return undefined;
28
29
  }
29
- const version = env;
30
- if (!Object.values(GpVersion).includes(version)) {
31
- throw new Error(`Configured environment variable GP_VERSION is unknown: '${env}'. Use one of: ${ALL_VERSIONS_IN_ORDER}`);
30
+ switch (env) {
31
+ case GpVersion.V0_6_7:
32
+ case GpVersion.V0_7_0:
33
+ case GpVersion.V0_7_1:
34
+ case GpVersion.V0_7_2:
35
+ return env;
36
+ default:
37
+ throw new Error(`Configured environment variable GP_VERSION is unknown: '${env}'. Use one of: ${ALL_VERSIONS_IN_ORDER}`);
32
38
  }
33
- return version;
34
39
  }
35
40
  function parseCurrentSuite(env) {
36
41
  if (env === undefined)
37
42
  return undefined;
38
- const val = env;
39
- if (!Object.values(TestSuite).includes(val)) {
40
- throw new Error(`Configured environment variable TEST_SUITE is unknown: '${env}'. Use one of: ${Object.values(TestSuite)}`);
43
+ switch (env) {
44
+ case TestSuite.W3F_DAVXY:
45
+ case TestSuite.JAMDUNA:
46
+ return env;
47
+ default:
48
+ throw new Error(`Configured environment variable TEST_SUITE is unknown: '${env}'. Use one of: ${Object.values(TestSuite)}`);
41
49
  }
42
- return val;
43
50
  }
44
51
  class Compatibility {
45
52
  static override(version) {
@@ -556,7 +563,7 @@ function isResult(x) {
556
563
  * as an afterthought.
557
564
  */
558
565
 
559
- var index$s = /*#__PURE__*/Object.freeze({
566
+ var index$u = /*#__PURE__*/Object.freeze({
560
567
  __proto__: null,
561
568
  get CURRENT_SUITE () { return CURRENT_SUITE; },
562
569
  get CURRENT_VERSION () { return CURRENT_VERSION; },
@@ -710,7 +717,7 @@ class Ordering {
710
717
  }
711
718
  }
712
719
 
713
- var index$r = /*#__PURE__*/Object.freeze({
720
+ var index$t = /*#__PURE__*/Object.freeze({
714
721
  __proto__: null,
715
722
  Ordering: Ordering
716
723
  });
@@ -961,7 +968,7 @@ function u8ArraySameLengthEqual(self, other) {
961
968
  }
962
969
  const bytesBlobComparator = (a, b) => a.compare(b);
963
970
 
964
- var index$q = /*#__PURE__*/Object.freeze({
971
+ var index$s = /*#__PURE__*/Object.freeze({
965
972
  __proto__: null,
966
973
  BitVec: BitVec,
967
974
  Bytes: Bytes,
@@ -1063,7 +1070,7 @@ const minU64 = (a, ...values) => values.reduce((min, value) => (value > min ? mi
1063
1070
  /** Get the biggest value between U64 a and values given as input parameters. */
1064
1071
  const maxU64 = (a, ...values) => values.reduce((max, value) => (value < max ? max : value), a);
1065
1072
 
1066
- var index$p = /*#__PURE__*/Object.freeze({
1073
+ var index$r = /*#__PURE__*/Object.freeze({
1067
1074
  __proto__: null,
1068
1075
  isU16: isU16,
1069
1076
  isU32: isU32,
@@ -2473,7 +2480,7 @@ function sequenceViewFixLen(type, { fixedLength }) {
2473
2480
  }, skipper);
2474
2481
  }
2475
2482
 
2476
- var index$o = /*#__PURE__*/Object.freeze({
2483
+ var index$q = /*#__PURE__*/Object.freeze({
2477
2484
  __proto__: null,
2478
2485
  Decoder: Decoder,
2479
2486
  Descriptor: Descriptor,
@@ -4912,7 +4919,7 @@ var keccak = /*#__PURE__*/Object.freeze({
4912
4919
  hashBlobs: hashBlobs
4913
4920
  });
4914
4921
 
4915
- var index$n = /*#__PURE__*/Object.freeze({
4922
+ var index$p = /*#__PURE__*/Object.freeze({
4916
4923
  __proto__: null,
4917
4924
  HASH_SIZE: HASH_SIZE,
4918
4925
  PageAllocator: PageAllocator,
@@ -4978,7 +4985,7 @@ var keyDerivation = /*#__PURE__*/Object.freeze({
4978
4985
  trivialSeed: trivialSeed
4979
4986
  });
4980
4987
 
4981
- var index$m = /*#__PURE__*/Object.freeze({
4988
+ var index$o = /*#__PURE__*/Object.freeze({
4982
4989
  __proto__: null,
4983
4990
  BANDERSNATCH_KEY_BYTES: BANDERSNATCH_KEY_BYTES,
4984
4991
  BANDERSNATCH_PROOF_BYTES: BANDERSNATCH_PROOF_BYTES,
@@ -5582,7 +5589,7 @@ class TruncatedHashDictionary {
5582
5589
  }
5583
5590
  }
5584
5591
 
5585
- var index$l = /*#__PURE__*/Object.freeze({
5592
+ var index$n = /*#__PURE__*/Object.freeze({
5586
5593
  __proto__: null,
5587
5594
  FixedSizeArray: FixedSizeArray,
5588
5595
  HashDictionary: HashDictionary,
@@ -5777,7 +5784,7 @@ class Bootnode {
5777
5784
  }
5778
5785
  }
5779
5786
 
5780
- var index$k = /*#__PURE__*/Object.freeze({
5787
+ var index$m = /*#__PURE__*/Object.freeze({
5781
5788
  __proto__: null,
5782
5789
  Bootnode: Bootnode,
5783
5790
  ChainSpec: ChainSpec,
@@ -5950,6 +5957,40 @@ var assurances = /*#__PURE__*/Object.freeze({
5950
5957
  assurancesExtrinsicCodec: assurancesExtrinsicCodec
5951
5958
  });
5952
5959
 
5960
+ /** Attempt to convert a number into `TimeSlot`. */
5961
+ const tryAsTimeSlot = (v) => asOpaqueType(tryAsU32(v));
5962
+ /** Attempt to convert a number into `ValidatorIndex`. */
5963
+ const tryAsValidatorIndex = (v) => asOpaqueType(tryAsU16(v));
5964
+ /** Attempt to convert a number into `ServiceId`. */
5965
+ const tryAsServiceId = (v) => asOpaqueType(tryAsU32(v));
5966
+ const tryAsServiceGas = (v) => asOpaqueType(tryAsU64(v));
5967
+ /** Attempt to convert a number into `CoreIndex`. */
5968
+ const tryAsCoreIndex = (v) => asOpaqueType(tryAsU16(v));
5969
+ /** Attempt to convert a number into `Epoch`. */
5970
+ const tryAsEpoch = (v) => asOpaqueType(tryAsU32(v));
5971
+ function tryAsPerValidator(array, spec) {
5972
+ check `
5973
+ ${array.length === spec.validatorsCount}
5974
+ Invalid per-validator array length. Expected ${spec.validatorsCount}, got: ${array.length}
5975
+ `;
5976
+ return asKnownSize(array);
5977
+ }
5978
+ const codecPerValidator = (val) => codecWithContext((context) => {
5979
+ return codecKnownSizeArray(val, {
5980
+ fixedLength: context.validatorsCount,
5981
+ });
5982
+ });
5983
+ function tryAsPerEpochBlock(array, spec) {
5984
+ check `
5985
+ ${array.length === spec.epochLength}
5986
+ Invalid per-epoch-block array length. Expected ${spec.epochLength}, got: ${array.length}
5987
+ `;
5988
+ return asKnownSize(array);
5989
+ }
5990
+ const codecPerEpochBlock = (val) => codecWithContext((context) => {
5991
+ return codecKnownSizeArray(val, { fixedLength: context.epochLength });
5992
+ });
5993
+
5953
5994
  /**
5954
5995
  * Proof of signing a contradictory [`Judgement`] of a work report.
5955
5996
  */
@@ -6141,40 +6182,6 @@ var disputes = /*#__PURE__*/Object.freeze({
6141
6182
  Verdict: Verdict
6142
6183
  });
6143
6184
 
6144
- /** Attempt to convert a number into `TimeSlot`. */
6145
- const tryAsTimeSlot = (v) => asOpaqueType(tryAsU32(v));
6146
- /** Attempt to convert a number into `ValidatorIndex`. */
6147
- const tryAsValidatorIndex = (v) => asOpaqueType(tryAsU16(v));
6148
- /** Attempt to convert a number into `ServiceId`. */
6149
- const tryAsServiceId = (v) => asOpaqueType(tryAsU32(v));
6150
- const tryAsServiceGas = (v) => asOpaqueType(tryAsU64(v));
6151
- /** Attempt to convert a number into `CoreIndex`. */
6152
- const tryAsCoreIndex = (v) => asOpaqueType(tryAsU16(v));
6153
- /** Attempt to convert a number into `Epoch`. */
6154
- const tryAsEpoch = (v) => asOpaqueType(tryAsU32(v));
6155
- function tryAsPerValidator(array, spec) {
6156
- check `
6157
- ${array.length === spec.validatorsCount}
6158
- Invalid per-validator array length. Expected ${spec.validatorsCount}, got: ${array.length}
6159
- `;
6160
- return asKnownSize(array);
6161
- }
6162
- const codecPerValidator = (val) => codecWithContext((context) => {
6163
- return codecKnownSizeArray(val, {
6164
- fixedLength: context.validatorsCount,
6165
- });
6166
- });
6167
- function tryAsPerEpochBlock(array, spec) {
6168
- check `
6169
- ${array.length === spec.epochLength}
6170
- Invalid per-epoch-block array length. Expected ${spec.epochLength}, got: ${array.length}
6171
- `;
6172
- return asKnownSize(array);
6173
- }
6174
- const codecPerEpochBlock = (val) => codecWithContext((context) => {
6175
- return codecKnownSizeArray(val, { fixedLength: context.epochLength });
6176
- });
6177
-
6178
6185
  /**
6179
6186
  * Mapping between work package hash and root hash of it's exports.
6180
6187
  *
@@ -7253,8 +7260,26 @@ class Block extends WithDebug {
7253
7260
  this.extrinsic = extrinsic;
7254
7261
  }
7255
7262
  }
7263
+ function emptyBlock(slot = tryAsTimeSlot(0)) {
7264
+ const header = Header.empty();
7265
+ header.timeSlotIndex = slot;
7266
+ return Block.create({
7267
+ header,
7268
+ extrinsic: Extrinsic.create({
7269
+ tickets: asKnownSize([]),
7270
+ preimages: [],
7271
+ assurances: asKnownSize([]),
7272
+ guarantees: asKnownSize([]),
7273
+ disputes: {
7274
+ verdicts: [],
7275
+ culprits: [],
7276
+ faults: [],
7277
+ },
7278
+ }),
7279
+ });
7280
+ }
7256
7281
 
7257
- var index$j = /*#__PURE__*/Object.freeze({
7282
+ var index$l = /*#__PURE__*/Object.freeze({
7258
7283
  __proto__: null,
7259
7284
  Block: Block,
7260
7285
  EpochMarker: EpochMarker,
@@ -7269,6 +7294,7 @@ var index$j = /*#__PURE__*/Object.freeze({
7269
7294
  codecPerValidator: codecPerValidator,
7270
7295
  codecUtils: codec,
7271
7296
  disputes: disputes,
7297
+ emptyBlock: emptyBlock,
7272
7298
  encodeUnsealedHeader: encodeUnsealedHeader,
7273
7299
  guarantees: guarantees,
7274
7300
  headerViewWithHashCodec: headerViewWithHashCodec,
@@ -7509,7 +7535,7 @@ var json;
7509
7535
  json.object = object;
7510
7536
  })(json || (json = {}));
7511
7537
 
7512
- var index$i = /*#__PURE__*/Object.freeze({
7538
+ var index$k = /*#__PURE__*/Object.freeze({
7513
7539
  __proto__: null,
7514
7540
  get json () { return json; },
7515
7541
  parseFromJson: parseFromJson
@@ -7779,7 +7805,7 @@ const blockFromJson = (spec) => json.object({
7779
7805
  extrinsic: getExtrinsicFromJson(spec),
7780
7806
  }, ({ header, extrinsic }) => Block.create({ header, extrinsic }));
7781
7807
 
7782
- var index$h = /*#__PURE__*/Object.freeze({
7808
+ var index$j = /*#__PURE__*/Object.freeze({
7783
7809
  __proto__: null,
7784
7810
  blockFromJson: blockFromJson,
7785
7811
  disputesExtrinsicFromJson: disputesExtrinsicFromJson,
@@ -8045,8 +8071,8 @@ function print(level, levelAndName, strings, data) {
8045
8071
  return;
8046
8072
  }
8047
8073
  const lvlText = Level[level].padEnd(5);
8048
- const val = strings.map((v, idx) => `${v}${data[idx]}`);
8049
- const msg = `${lvlText} [${levelAndName[1]}] ${val}`;
8074
+ const val = strings.map((v, idx) => `${v}${idx < data.length ? data[idx] : ""}`);
8075
+ const msg = `${lvlText} [${levelAndName[1]}] ${val.join("")}`;
8050
8076
  if (level === Level.WARN) {
8051
8077
  console.warn(msg);
8052
8078
  }
@@ -8277,7 +8303,7 @@ class Logger {
8277
8303
  }
8278
8304
  }
8279
8305
 
8280
- var index$g = /*#__PURE__*/Object.freeze({
8306
+ var index$i = /*#__PURE__*/Object.freeze({
8281
8307
  __proto__: null,
8282
8308
  get Level () { return Level; },
8283
8309
  Logger: Logger,
@@ -8300,7 +8326,7 @@ class AuthorshipOptions {
8300
8326
  }
8301
8327
  }
8302
8328
 
8303
- const logger$4 = Logger.new(undefined, "config");
8329
+ const logger$5 = Logger.new(undefined, "config");
8304
8330
  /** Development config. Will accept unsealed blocks for now. */
8305
8331
  const DEV_CONFIG = "dev";
8306
8332
  /** Default config file. */
@@ -8359,15 +8385,15 @@ class NodeConfiguration {
8359
8385
  }
8360
8386
  function loadConfig(configPath) {
8361
8387
  if (configPath === DEFAULT_CONFIG) {
8362
- logger$4.log `🔧 Loading DEFAULT config`;
8388
+ logger$5.log `🔧 Loading DEFAULT config`;
8363
8389
  return parseFromJson(configs.default, NodeConfiguration.fromJson);
8364
8390
  }
8365
8391
  if (configPath === DEV_CONFIG) {
8366
- logger$4.log `🔧 Loading DEV config`;
8392
+ logger$5.log `🔧 Loading DEV config`;
8367
8393
  return parseFromJson(configs.dev, NodeConfiguration.fromJson);
8368
8394
  }
8369
8395
  try {
8370
- logger$4.log `🔧 Loading config from ${configPath}`;
8396
+ logger$5.log `🔧 Loading config from ${configPath}`;
8371
8397
  const configFile = fs.readFileSync(configPath, "utf8");
8372
8398
  const parsed = JSON.parse(configFile);
8373
8399
  return parseFromJson(parsed, NodeConfiguration.fromJson);
@@ -8377,7 +8403,7 @@ function loadConfig(configPath) {
8377
8403
  }
8378
8404
  }
8379
8405
 
8380
- var index$f = /*#__PURE__*/Object.freeze({
8406
+ var index$h = /*#__PURE__*/Object.freeze({
8381
8407
  __proto__: null,
8382
8408
  DEFAULT_CONFIG: DEFAULT_CONFIG,
8383
8409
  DEV_CONFIG: DEV_CONFIG,
@@ -10043,7 +10069,7 @@ const serviceDataCodec = codec$1.dictionary(codec$1.u32.asOpaque(), serviceEntri
10043
10069
  sortKeys: (a, b) => a - b,
10044
10070
  });
10045
10071
 
10046
- var index$e = /*#__PURE__*/Object.freeze({
10072
+ var index$g = /*#__PURE__*/Object.freeze({
10047
10073
  __proto__: null,
10048
10074
  AccumulationOutput: AccumulationOutput,
10049
10075
  AutoAccumulate: AutoAccumulate,
@@ -11053,7 +11079,7 @@ const bitLookup = [
11053
11079
  [0b00000000, 8],
11054
11080
  ];
11055
11081
 
11056
- var index$d = /*#__PURE__*/Object.freeze({
11082
+ var index$f = /*#__PURE__*/Object.freeze({
11057
11083
  __proto__: null,
11058
11084
  BranchNode: BranchNode,
11059
11085
  InMemoryTrie: InMemoryTrie,
@@ -11399,7 +11425,7 @@ function loadState(spec, entries) {
11399
11425
  * hashmap of `key -> value` entries.
11400
11426
  */
11401
11427
 
11402
- var index$c = /*#__PURE__*/Object.freeze({
11428
+ var index$e = /*#__PURE__*/Object.freeze({
11403
11429
  __proto__: null,
11404
11430
  SerializedService: SerializedService,
11405
11431
  SerializedState: SerializedState,
@@ -11685,7 +11711,7 @@ class InMemoryStates {
11685
11711
  }
11686
11712
  }
11687
11713
 
11688
- var index$b = /*#__PURE__*/Object.freeze({
11714
+ var index$d = /*#__PURE__*/Object.freeze({
11689
11715
  __proto__: null,
11690
11716
  InMemoryBlocks: InMemoryBlocks,
11691
11717
  InMemoryStates: InMemoryStates,
@@ -12047,7 +12073,7 @@ const initEc = async () => {
12047
12073
  await init.reedSolomon();
12048
12074
  };
12049
12075
 
12050
- var index$a = /*#__PURE__*/Object.freeze({
12076
+ var index$c = /*#__PURE__*/Object.freeze({
12051
12077
  __proto__: null,
12052
12078
  N_CHUNKS_REDUNDANCY: N_CHUNKS_REDUNDANCY,
12053
12079
  N_CHUNKS_REQUIRED: N_CHUNKS_REQUIRED,
@@ -12071,6 +12097,439 @@ var index$a = /*#__PURE__*/Object.freeze({
12071
12097
  unzip: unzip
12072
12098
  });
12073
12099
 
12100
+ /**
12101
+ * Version ::= SEQUENCE {
12102
+ * major INTEGER (0..255),
12103
+ * minor INTEGER (0..255),
12104
+ * patch INTEGER (0..255)
12105
+ * }
12106
+ */
12107
+ class Version extends WithDebug {
12108
+ major;
12109
+ minor;
12110
+ patch;
12111
+ static Codec = codec$1.Class(Version, {
12112
+ major: codec$1.u8,
12113
+ minor: codec$1.u8,
12114
+ patch: codec$1.u8,
12115
+ });
12116
+ static tryFromString(str) {
12117
+ const parse = (v) => tryAsU8(Number(v));
12118
+ try {
12119
+ const [major, minor, patch] = str.trim().split(".").map(parse);
12120
+ return Version.create({
12121
+ major,
12122
+ minor,
12123
+ patch,
12124
+ });
12125
+ }
12126
+ catch (e) {
12127
+ throw new Error(`Unable to parse ${str} as Version: ${e}`);
12128
+ }
12129
+ }
12130
+ static create({ major, minor, patch }) {
12131
+ return new Version(major, minor, patch);
12132
+ }
12133
+ constructor(major, minor, patch) {
12134
+ super();
12135
+ this.major = major;
12136
+ this.minor = minor;
12137
+ this.patch = patch;
12138
+ }
12139
+ }
12140
+ /**
12141
+ * Fuzzer Protocol V1
12142
+ * Reference: https://github.com/davxy/jam-conformance/blob/main/fuzz-proto/fuzz.asn
12143
+ */
12144
+ // Feature bit constants
12145
+ var Features;
12146
+ (function (Features) {
12147
+ Features[Features["Ancestry"] = 1] = "Ancestry";
12148
+ Features[Features["Fork"] = 2] = "Fork";
12149
+ Features[Features["Reserved"] = 2147483648] = "Reserved";
12150
+ })(Features || (Features = {}));
12151
+ /**
12152
+ * PeerInfo ::= SEQUENCE {
12153
+ * fuzz-version U8,
12154
+ * features Features,
12155
+ * jam-version Version,
12156
+ * app-version Version,
12157
+ * name UTF8String
12158
+ * }
12159
+ */
12160
+ class PeerInfo extends WithDebug {
12161
+ fuzzVersion;
12162
+ features;
12163
+ jamVersion;
12164
+ appVersion;
12165
+ name;
12166
+ static Codec = codec$1.Class(PeerInfo, {
12167
+ fuzzVersion: codec$1.u8,
12168
+ features: codec$1.u32,
12169
+ jamVersion: Version.Codec,
12170
+ appVersion: Version.Codec,
12171
+ name: codec$1.string,
12172
+ });
12173
+ static create({ fuzzVersion, features, appVersion, jamVersion, name }) {
12174
+ return new PeerInfo(fuzzVersion, features, jamVersion, appVersion, name);
12175
+ }
12176
+ constructor(fuzzVersion, features, jamVersion, appVersion, name) {
12177
+ super();
12178
+ this.fuzzVersion = fuzzVersion;
12179
+ this.features = features;
12180
+ this.jamVersion = jamVersion;
12181
+ this.appVersion = appVersion;
12182
+ this.name = name;
12183
+ }
12184
+ }
12185
+ /**
12186
+ * AncestryItem ::= SEQUENCE {
12187
+ * slot TimeSlot,
12188
+ * header-hash HeaderHash
12189
+ * }
12190
+ */
12191
+ class AncestryItem extends WithDebug {
12192
+ slot;
12193
+ headerHash;
12194
+ static Codec = codec$1.Class(AncestryItem, {
12195
+ slot: codec$1.u32.asOpaque(),
12196
+ headerHash: codec$1.bytes(HASH_SIZE).asOpaque(),
12197
+ });
12198
+ static create({ slot, headerHash }) {
12199
+ return new AncestryItem(slot, headerHash);
12200
+ }
12201
+ constructor(slot, headerHash) {
12202
+ super();
12203
+ this.slot = slot;
12204
+ this.headerHash = headerHash;
12205
+ }
12206
+ }
12207
+ /**
12208
+ * KeyValue ::= SEQUENCE {
12209
+ * key TrieKey,
12210
+ * value OCTET STRING
12211
+ * }
12212
+ */
12213
+ class KeyValue extends WithDebug {
12214
+ key;
12215
+ value;
12216
+ static Codec = codec$1.Class(KeyValue, {
12217
+ key: codec$1.bytes(TRUNCATED_HASH_SIZE),
12218
+ value: codec$1.blob,
12219
+ });
12220
+ static create({ key, value }) {
12221
+ return new KeyValue(key, value);
12222
+ }
12223
+ constructor(key, value) {
12224
+ super();
12225
+ this.key = key;
12226
+ this.value = value;
12227
+ }
12228
+ }
12229
+ /** State ::= SEQUENCE OF KeyValue */
12230
+ const stateCodec = codec$1.sequenceVarLen(KeyValue.Codec);
12231
+ /**
12232
+ * Ancestry ::= SEQUENCE (SIZE(0..24)) OF AncestryItem
12233
+ * Empty when `feature-ancestry` is not supported by both parties
12234
+ */
12235
+ const ancestryCodec = codec$1.sequenceVarLen(AncestryItem.Codec, {
12236
+ minLength: 0,
12237
+ maxLength: 24,
12238
+ });
12239
+ /**
12240
+ * Initialize ::= SEQUENCE {
12241
+ * header Header,
12242
+ * keyvals State,
12243
+ * ancestry Ancestry
12244
+ * }
12245
+ */
12246
+ class Initialize extends WithDebug {
12247
+ header;
12248
+ keyvals;
12249
+ ancestry;
12250
+ static Codec = codec$1.Class(Initialize, {
12251
+ header: Header.Codec,
12252
+ keyvals: stateCodec,
12253
+ ancestry: ancestryCodec,
12254
+ });
12255
+ static create({ header, keyvals, ancestry }) {
12256
+ return new Initialize(header, keyvals, ancestry);
12257
+ }
12258
+ constructor(header, keyvals, ancestry) {
12259
+ super();
12260
+ this.header = header;
12261
+ this.keyvals = keyvals;
12262
+ this.ancestry = ancestry;
12263
+ }
12264
+ }
12265
+ /** GetState ::= HeaderHash */
12266
+ const getStateCodec = codec$1.bytes(HASH_SIZE).asOpaque();
12267
+ /** StateRoot ::= StateRootHash */
12268
+ const stateRootCodec = codec$1.bytes(HASH_SIZE).asOpaque();
12269
+ /** Error ::= UTF8String */
12270
+ class ErrorMessage extends WithDebug {
12271
+ message;
12272
+ static Codec = codec$1.Class(ErrorMessage, {
12273
+ message: codec$1.string,
12274
+ });
12275
+ static create({ message }) {
12276
+ return new ErrorMessage(message);
12277
+ }
12278
+ constructor(message) {
12279
+ super();
12280
+ this.message = message;
12281
+ }
12282
+ }
12283
+ /** Message choice type tags */
12284
+ var MessageType;
12285
+ (function (MessageType) {
12286
+ MessageType[MessageType["PeerInfo"] = 0] = "PeerInfo";
12287
+ MessageType[MessageType["Initialize"] = 1] = "Initialize";
12288
+ MessageType[MessageType["StateRoot"] = 2] = "StateRoot";
12289
+ MessageType[MessageType["ImportBlock"] = 3] = "ImportBlock";
12290
+ MessageType[MessageType["GetState"] = 4] = "GetState";
12291
+ MessageType[MessageType["State"] = 5] = "State";
12292
+ MessageType[MessageType["Error"] = 255] = "Error";
12293
+ })(MessageType || (MessageType = {}));
12294
+ /**
12295
+ * Message ::= CHOICE {
12296
+ * peer-info [0] PeerInfo,
12297
+ * initialize [1] Initialize,
12298
+ * state-root [2] StateRoot,
12299
+ * import-block [3] ImportBlock,
12300
+ * get-state [4] GetState,
12301
+ * state [5] State,
12302
+ * error [255] Error
12303
+ * }
12304
+ */
12305
+ const messageCodec = codec$1.custom({
12306
+ name: "Message",
12307
+ sizeHint: { bytes: 1, isExact: false },
12308
+ }, (e, msg) => {
12309
+ e.i8(msg.type);
12310
+ switch (msg.type) {
12311
+ case MessageType.PeerInfo:
12312
+ PeerInfo.Codec.encode(e, msg.value);
12313
+ break;
12314
+ case MessageType.Initialize:
12315
+ Initialize.Codec.encode(e, msg.value);
12316
+ break;
12317
+ case MessageType.StateRoot:
12318
+ stateRootCodec.encode(e, msg.value);
12319
+ break;
12320
+ case MessageType.ImportBlock:
12321
+ Block.Codec.View.encode(e, msg.value);
12322
+ break;
12323
+ case MessageType.GetState:
12324
+ getStateCodec.encode(e, msg.value);
12325
+ break;
12326
+ case MessageType.State:
12327
+ stateCodec.encode(e, msg.value);
12328
+ break;
12329
+ case MessageType.Error:
12330
+ ErrorMessage.Codec.encode(e, msg.value);
12331
+ break;
12332
+ default:
12333
+ throw new Error(`Unknown message type: ${msg}`);
12334
+ }
12335
+ }, (d) => {
12336
+ const type = d.u8();
12337
+ switch (type) {
12338
+ case MessageType.PeerInfo:
12339
+ return { type: MessageType.PeerInfo, value: PeerInfo.Codec.decode(d) };
12340
+ case MessageType.Initialize:
12341
+ return { type: MessageType.Initialize, value: Initialize.Codec.decode(d) };
12342
+ case MessageType.StateRoot:
12343
+ return { type: MessageType.StateRoot, value: stateRootCodec.decode(d) };
12344
+ case MessageType.ImportBlock:
12345
+ return { type: MessageType.ImportBlock, value: Block.Codec.View.decode(d) };
12346
+ case MessageType.GetState:
12347
+ return { type: MessageType.GetState, value: getStateCodec.decode(d) };
12348
+ case MessageType.State:
12349
+ return { type: MessageType.State, value: stateCodec.decode(d) };
12350
+ case MessageType.Error:
12351
+ return { type: MessageType.Error, value: ErrorMessage.Codec.decode(d) };
12352
+ default:
12353
+ throw new Error(`Unknown message type: ${type}`);
12354
+ }
12355
+ }, (s) => {
12356
+ const type = s.decoder.u8();
12357
+ switch (type) {
12358
+ case MessageType.PeerInfo:
12359
+ PeerInfo.Codec.View.skip(s);
12360
+ break;
12361
+ case MessageType.Initialize:
12362
+ Initialize.Codec.View.skip(s);
12363
+ break;
12364
+ case MessageType.StateRoot:
12365
+ stateRootCodec.View.skip(s);
12366
+ break;
12367
+ case MessageType.ImportBlock:
12368
+ Block.Codec.View.skip(s);
12369
+ break;
12370
+ case MessageType.GetState:
12371
+ getStateCodec.View.skip(s);
12372
+ break;
12373
+ case MessageType.State:
12374
+ stateCodec.View.skip(s);
12375
+ break;
12376
+ case MessageType.Error:
12377
+ ErrorMessage.Codec.View.skip(s);
12378
+ break;
12379
+ default:
12380
+ throw new Error(`Unknown message type: ${type}`);
12381
+ }
12382
+ });
12383
+
12384
+ const logger$4 = Logger.new(undefined, "ext-ipc-fuzz-v1");
12385
+ class FuzzTarget {
12386
+ msgHandler;
12387
+ sender;
12388
+ spec;
12389
+ sessionFeatures = 0;
12390
+ constructor(msgHandler, sender, spec) {
12391
+ this.msgHandler = msgHandler;
12392
+ this.sender = sender;
12393
+ this.spec = spec;
12394
+ }
12395
+ async onSocketMessage(msg) {
12396
+ // attempt to decode the messsage
12397
+ try {
12398
+ const message = Decoder.decodeObject(messageCodec, msg, this.spec);
12399
+ logger$4.log `[${message.type}] incoming message`;
12400
+ await this.processAndRespond(message);
12401
+ }
12402
+ catch (e) {
12403
+ logger$4.error `Error while processing fuzz v1 message: ${e}`;
12404
+ logger$4.error `${e}`;
12405
+ if (e instanceof Error) {
12406
+ logger$4.error `${e.stack ?? ""}`;
12407
+ }
12408
+ this.sender.close();
12409
+ }
12410
+ }
12411
+ async processAndRespond(message) {
12412
+ let response = null;
12413
+ switch (message.type) {
12414
+ case MessageType.PeerInfo: {
12415
+ // only support V1
12416
+ if (message.value.fuzzVersion !== 1) {
12417
+ logger$4.warn `Unsupported fuzzer protocol version: ${message.value.fuzzVersion}. Closing`;
12418
+ this.sender.close();
12419
+ return;
12420
+ }
12421
+ // Handle handshake
12422
+ const ourPeerInfo = await this.msgHandler.getPeerInfo(message.value);
12423
+ // Calculate session features (intersection of both peer features)
12424
+ this.sessionFeatures = message.value.features & ourPeerInfo.features;
12425
+ logger$4.info `Handshake completed. Shared features: 0b${this.sessionFeatures.toString(2)}`;
12426
+ logger$4.log `Feature ancestry: ${(this.sessionFeatures & Features.Ancestry) !== 0}`;
12427
+ logger$4.log `Feature fork: ${(this.sessionFeatures & Features.Fork) !== 0}`;
12428
+ response = {
12429
+ type: MessageType.PeerInfo,
12430
+ value: ourPeerInfo,
12431
+ };
12432
+ break;
12433
+ }
12434
+ case MessageType.Initialize: {
12435
+ const stateRoot = await this.msgHandler.initialize(message.value);
12436
+ response = {
12437
+ type: MessageType.StateRoot,
12438
+ value: stateRoot,
12439
+ };
12440
+ break;
12441
+ }
12442
+ case MessageType.ImportBlock: {
12443
+ const result = await this.msgHandler.importBlock(message.value);
12444
+ if (result.isOk) {
12445
+ response = {
12446
+ type: MessageType.StateRoot,
12447
+ value: result.ok,
12448
+ };
12449
+ }
12450
+ else {
12451
+ response = {
12452
+ type: MessageType.Error,
12453
+ value: result.error,
12454
+ };
12455
+ }
12456
+ break;
12457
+ }
12458
+ case MessageType.GetState: {
12459
+ const state = await this.msgHandler.getSerializedState(message.value);
12460
+ response = {
12461
+ type: MessageType.State,
12462
+ value: state,
12463
+ };
12464
+ break;
12465
+ }
12466
+ case MessageType.StateRoot: {
12467
+ logger$4.log `--> Received unexpected 'StateRoot' message from the fuzzer. Closing.`;
12468
+ this.sender.close();
12469
+ return;
12470
+ }
12471
+ case MessageType.State: {
12472
+ logger$4.log `--> Received unexpected 'State' message from the fuzzer. Closing.`;
12473
+ this.sender.close();
12474
+ return;
12475
+ }
12476
+ case MessageType.Error: {
12477
+ logger$4.log `--> Received unexpected 'Error' message from the fuzzer. Closing.`;
12478
+ this.sender.close();
12479
+ return;
12480
+ }
12481
+ default: {
12482
+ logger$4.log `--> Received unexpected message type ${JSON.stringify(message)} from the fuzzer. Closing.`;
12483
+ this.sender.close();
12484
+ try {
12485
+ assertNever(message);
12486
+ }
12487
+ catch {
12488
+ return;
12489
+ }
12490
+ }
12491
+ }
12492
+ if (response !== null) {
12493
+ logger$4.trace `<-- responding with: ${response.type}`;
12494
+ const encoded = Encoder.encodeObject(messageCodec, response, this.spec);
12495
+ this.sender.send(encoded);
12496
+ }
12497
+ else {
12498
+ logger$4.warn `<-- no response generated for: ${message.type}`;
12499
+ }
12500
+ }
12501
+ onClose({ error }) {
12502
+ logger$4.log `Closing the v1 handler. Reason: ${error !== undefined ? error.message : "close"}.`;
12503
+ }
12504
+ /** Check if a specific feature is enabled in the session */
12505
+ hasFeature(feature) {
12506
+ return (this.sessionFeatures & feature) !== 0;
12507
+ }
12508
+ }
12509
+
12510
+ var index$b = /*#__PURE__*/Object.freeze({
12511
+ __proto__: null,
12512
+ AncestryItem: AncestryItem,
12513
+ ErrorMessage: ErrorMessage,
12514
+ get Features () { return Features; },
12515
+ FuzzTarget: FuzzTarget,
12516
+ Initialize: Initialize,
12517
+ KeyValue: KeyValue,
12518
+ get MessageType () { return MessageType; },
12519
+ PeerInfo: PeerInfo,
12520
+ Version: Version,
12521
+ ancestryCodec: ancestryCodec,
12522
+ getStateCodec: getStateCodec,
12523
+ messageCodec: messageCodec,
12524
+ stateCodec: stateCodec,
12525
+ stateRootCodec: stateRootCodec
12526
+ });
12527
+
12528
+ var index$a = /*#__PURE__*/Object.freeze({
12529
+ __proto__: null,
12530
+ v1: index$b
12531
+ });
12532
+
12074
12533
  /** Size of the transfer memo. */
12075
12534
  const TRANSFER_MEMO_BYTES = W_T;
12076
12535
  /**
@@ -12397,16 +12856,12 @@ class PartiallyUpdatedState {
12397
12856
  *
12398
12857
  * NOTE the info may be updated compared to what is in the state.
12399
12858
  *
12400
- * Takes into account newly created services as well.
12859
+ * Takes into account ejected and newly created services as well.
12401
12860
  */
12402
12861
  getServiceInfo(destination) {
12403
12862
  if (destination === null) {
12404
12863
  return null;
12405
12864
  }
12406
- const isEjected = this.stateUpdate.services.servicesRemoved.some((x) => x === destination);
12407
- if (isEjected) {
12408
- return null;
12409
- }
12410
12865
  const maybeNewService = this.stateUpdate.services.servicesUpdates.find((update) => update.serviceId === destination);
12411
12866
  if (maybeNewService !== undefined) {
12412
12867
  return maybeNewService.action.account;
@@ -14122,6 +14577,17 @@ class PageRange {
14122
14577
  }
14123
14578
  return new PageRange(start, length);
14124
14579
  }
14580
+ /** Returns true if the page range is wrapped (`start` >= `end`) and is not empty */
14581
+ isWrapped() {
14582
+ return this.start >= this.end && !this.isEmpty();
14583
+ }
14584
+ /** Checks if given page number is within the range */
14585
+ isInRange(page) {
14586
+ if (this.isWrapped()) {
14587
+ return page >= this.start || page < this.end;
14588
+ }
14589
+ return page >= this.start && page < this.end;
14590
+ }
14125
14591
  /** Checks if a range is empty (`length === 0`) */
14126
14592
  isEmpty() {
14127
14593
  return this.length === 0;
@@ -14462,10 +14928,11 @@ class MemoryBuilder {
14462
14928
  startHeapIndex (${startHeapIndex}) has to be less than or equal to endHeapIndex (${endHeapIndex})
14463
14929
  `;
14464
14930
  this.ensureNotFinalized();
14465
- const range = MemoryRange.fromStartAndLength(startHeapIndex, endHeapIndex - startHeapIndex);
14466
- const pages = PageRange.fromMemoryRange(range);
14467
- for (const pageNumber of pages) {
14468
- if (this.initialMemory.has(pageNumber)) {
14931
+ const heapRange = MemoryRange.fromStartAndLength(startHeapIndex, endHeapIndex - startHeapIndex);
14932
+ const heapPagesRange = PageRange.fromMemoryRange(heapRange);
14933
+ const initializedPageNumbers = Array.from(this.initialMemory.keys());
14934
+ for (const pageNumber of initializedPageNumbers) {
14935
+ if (heapPagesRange.isInRange(pageNumber)) {
14469
14936
  throw new IncorrectSbrkIndex();
14470
14937
  }
14471
14938
  }
@@ -16905,8 +17372,8 @@ var index$3 = /*#__PURE__*/Object.freeze({
16905
17372
  asOpaqueType: asOpaqueType,
16906
17373
  assertEmpty: assertEmpty,
16907
17374
  assertNever: assertNever,
16908
- block: index$j,
16909
- bytes: index$q,
17375
+ block: index$l,
17376
+ bytes: index$s,
16910
17377
  check: check,
16911
17378
  clampU64ToU32: clampU64ToU32,
16912
17379
  createResults: createResults,
@@ -16914,13 +17381,13 @@ var index$3 = /*#__PURE__*/Object.freeze({
16914
17381
  extractCodeAndMetadata: extractCodeAndMetadata,
16915
17382
  getServiceId: getServiceId,
16916
17383
  getServiceIdOrCurrent: getServiceIdOrCurrent,
16917
- hash: index$n,
17384
+ hash: index$p,
16918
17385
  inspect: inspect,
16919
17386
  instructionArgumentTypeMap: instructionArgumentTypeMap,
16920
17387
  interpreter: index$7,
16921
17388
  isBrowser: isBrowser,
16922
17389
  measure: measure,
16923
- numbers: index$p,
17390
+ numbers: index$r,
16924
17391
  resultToString: resultToString,
16925
17392
  seeThrough: seeThrough,
16926
17393
  slotsToPreimageStatus: slotsToPreimageStatus,
@@ -17634,32 +18101,33 @@ var index = /*#__PURE__*/Object.freeze({
17634
18101
  WorkPackageExecutor: WorkPackageExecutor
17635
18102
  });
17636
18103
 
17637
- exports.block = index$j;
17638
- exports.block_json = index$h;
17639
- exports.bytes = index$q;
17640
- exports.codec = index$o;
17641
- exports.collections = index$l;
17642
- exports.config = index$k;
17643
- exports.config_node = index$f;
17644
- exports.crypto = index$m;
17645
- exports.database = index$b;
17646
- exports.erasure_coding = index$a;
17647
- exports.hash = index$n;
18104
+ exports.block = index$l;
18105
+ exports.block_json = index$j;
18106
+ exports.bytes = index$s;
18107
+ exports.codec = index$q;
18108
+ exports.collections = index$n;
18109
+ exports.config = index$m;
18110
+ exports.config_node = index$h;
18111
+ exports.crypto = index$o;
18112
+ exports.database = index$d;
18113
+ exports.erasure_coding = index$c;
18114
+ exports.fuzz_proto = index$a;
18115
+ exports.hash = index$p;
17648
18116
  exports.jam_host_calls = index$9;
17649
- exports.json_parser = index$i;
17650
- exports.logger = index$g;
18117
+ exports.json_parser = index$k;
18118
+ exports.logger = index$i;
17651
18119
  exports.mmr = index$8;
17652
- exports.numbers = index$p;
17653
- exports.ordering = index$r;
18120
+ exports.numbers = index$r;
18121
+ exports.ordering = index$t;
17654
18122
  exports.pvm = index$3;
17655
18123
  exports.pvm_host_calls = index$6;
17656
18124
  exports.pvm_interpreter = index$7;
17657
18125
  exports.pvm_program = index$4;
17658
18126
  exports.pvm_spi_decoder = index$5;
17659
18127
  exports.shuffling = index$2;
17660
- exports.state = index$e;
18128
+ exports.state = index$g;
17661
18129
  exports.state_json = index$1;
17662
- exports.state_merkleization = index$c;
18130
+ exports.state_merkleization = index$e;
17663
18131
  exports.transition = index;
17664
- exports.trie = index$d;
17665
- exports.utils = index$s;
18132
+ exports.trie = index$f;
18133
+ exports.utils = index$u;