@typeberry/lib 0.1.2-ef67dce → 0.1.3-707962d

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 +494 -49
  2. package/index.d.ts +1209 -653
  3. package/index.js +475 -31
  4. package/package.json +1 -1
package/index.cjs CHANGED
@@ -563,7 +563,7 @@ function isResult(x) {
563
563
  * as an afterthought.
564
564
  */
565
565
 
566
- var index$s = /*#__PURE__*/Object.freeze({
566
+ var index$u = /*#__PURE__*/Object.freeze({
567
567
  __proto__: null,
568
568
  get CURRENT_SUITE () { return CURRENT_SUITE; },
569
569
  get CURRENT_VERSION () { return CURRENT_VERSION; },
@@ -717,7 +717,7 @@ class Ordering {
717
717
  }
718
718
  }
719
719
 
720
- var index$r = /*#__PURE__*/Object.freeze({
720
+ var index$t = /*#__PURE__*/Object.freeze({
721
721
  __proto__: null,
722
722
  Ordering: Ordering
723
723
  });
@@ -968,7 +968,7 @@ function u8ArraySameLengthEqual(self, other) {
968
968
  }
969
969
  const bytesBlobComparator = (a, b) => a.compare(b);
970
970
 
971
- var index$q = /*#__PURE__*/Object.freeze({
971
+ var index$s = /*#__PURE__*/Object.freeze({
972
972
  __proto__: null,
973
973
  BitVec: BitVec,
974
974
  Bytes: Bytes,
@@ -1070,7 +1070,7 @@ const minU64 = (a, ...values) => values.reduce((min, value) => (value > min ? mi
1070
1070
  /** Get the biggest value between U64 a and values given as input parameters. */
1071
1071
  const maxU64 = (a, ...values) => values.reduce((max, value) => (value < max ? max : value), a);
1072
1072
 
1073
- var index$p = /*#__PURE__*/Object.freeze({
1073
+ var index$r = /*#__PURE__*/Object.freeze({
1074
1074
  __proto__: null,
1075
1075
  isU16: isU16,
1076
1076
  isU32: isU32,
@@ -1947,13 +1947,15 @@ function validateLength(range, length, context) {
1947
1947
 
1948
1948
  /** A caching wrapper for either object or sequence item. */
1949
1949
  class ViewField {
1950
+ name;
1950
1951
  getView;
1951
1952
  getValue;
1952
1953
  getEncoded;
1953
1954
  cachedValue;
1954
1955
  cachedView;
1955
1956
  cachedBlob;
1956
- constructor(getView, getValue, getEncoded) {
1957
+ constructor(name, getView, getValue, getEncoded) {
1958
+ this.name = name;
1957
1959
  this.getView = getView;
1958
1960
  this.getValue = getValue;
1959
1961
  this.getEncoded = getEncoded;
@@ -1979,6 +1981,9 @@ class ViewField {
1979
1981
  }
1980
1982
  return this.cachedBlob;
1981
1983
  }
1984
+ toString() {
1985
+ return `ViewField<${this.name}>`;
1986
+ }
1982
1987
  }
1983
1988
  /**
1984
1989
  * A base class for all the lazy views.
@@ -2053,7 +2058,7 @@ class ObjectView {
2053
2058
  const fieldDecoder = skipper.decoder.clone();
2054
2059
  const field = this.descriptorsKeys[i];
2055
2060
  const type = this.descriptors[field];
2056
- lastItem = new ViewField(() => type.View.decode(fieldDecoder.clone()), () => type.decode(fieldDecoder.clone()), () => type.skipEncoded(fieldDecoder.clone()));
2061
+ lastItem = new ViewField(`${this.toString()}.${String(field)}`, () => type.View.decode(fieldDecoder.clone()), () => type.decode(fieldDecoder.clone()), () => type.skipEncoded(fieldDecoder.clone()));
2057
2062
  // skip the field
2058
2063
  type.skip(skipper);
2059
2064
  // cache data
@@ -2065,6 +2070,9 @@ class ObjectView {
2065
2070
  }
2066
2071
  return lastItem;
2067
2072
  }
2073
+ toString() {
2074
+ return `View<${this.materializedConstructor.name}>(cache: ${this.cache.size})`;
2075
+ }
2068
2076
  }
2069
2077
  /**
2070
2078
  * A lazy-evaluated decoder of a sequence.
@@ -2153,7 +2161,7 @@ class SequenceView {
2153
2161
  // create new cached prop
2154
2162
  const fieldDecoder = skipper.decoder.clone();
2155
2163
  const type = this.descriptor;
2156
- lastItem = new ViewField(() => type.View.decode(fieldDecoder.clone()), () => type.decode(fieldDecoder.clone()), () => type.skipEncoded(fieldDecoder.clone()));
2164
+ lastItem = new ViewField(`${this.toString()}[${index}]`, () => type.View.decode(fieldDecoder.clone()), () => type.decode(fieldDecoder.clone()), () => type.skipEncoded(fieldDecoder.clone()));
2157
2165
  // skip the field
2158
2166
  type.skip(skipper);
2159
2167
  // cache data
@@ -2165,6 +2173,9 @@ class SequenceView {
2165
2173
  }
2166
2174
  return lastItem;
2167
2175
  }
2176
+ toString() {
2177
+ return `SequenceView<${this.descriptor.name}>(cache: ${this.cache.size})`;
2178
+ }
2168
2179
  }
2169
2180
 
2170
2181
  /**
@@ -2480,7 +2491,7 @@ function sequenceViewFixLen(type, { fixedLength }) {
2480
2491
  }, skipper);
2481
2492
  }
2482
2493
 
2483
- var index$o = /*#__PURE__*/Object.freeze({
2494
+ var index$q = /*#__PURE__*/Object.freeze({
2484
2495
  __proto__: null,
2485
2496
  Decoder: Decoder,
2486
2497
  Descriptor: Descriptor,
@@ -4919,7 +4930,7 @@ var keccak = /*#__PURE__*/Object.freeze({
4919
4930
  hashBlobs: hashBlobs
4920
4931
  });
4921
4932
 
4922
- var index$n = /*#__PURE__*/Object.freeze({
4933
+ var index$p = /*#__PURE__*/Object.freeze({
4923
4934
  __proto__: null,
4924
4935
  HASH_SIZE: HASH_SIZE,
4925
4936
  PageAllocator: PageAllocator,
@@ -4985,7 +4996,7 @@ var keyDerivation = /*#__PURE__*/Object.freeze({
4985
4996
  trivialSeed: trivialSeed
4986
4997
  });
4987
4998
 
4988
- var index$m = /*#__PURE__*/Object.freeze({
4999
+ var index$o = /*#__PURE__*/Object.freeze({
4989
5000
  __proto__: null,
4990
5001
  BANDERSNATCH_KEY_BYTES: BANDERSNATCH_KEY_BYTES,
4991
5002
  BANDERSNATCH_PROOF_BYTES: BANDERSNATCH_PROOF_BYTES,
@@ -5589,7 +5600,7 @@ class TruncatedHashDictionary {
5589
5600
  }
5590
5601
  }
5591
5602
 
5592
- var index$l = /*#__PURE__*/Object.freeze({
5603
+ var index$n = /*#__PURE__*/Object.freeze({
5593
5604
  __proto__: null,
5594
5605
  FixedSizeArray: FixedSizeArray,
5595
5606
  HashDictionary: HashDictionary,
@@ -5784,7 +5795,7 @@ class Bootnode {
5784
5795
  }
5785
5796
  }
5786
5797
 
5787
- var index$k = /*#__PURE__*/Object.freeze({
5798
+ var index$m = /*#__PURE__*/Object.freeze({
5788
5799
  __proto__: null,
5789
5800
  Bootnode: Bootnode,
5790
5801
  ChainSpec: ChainSpec,
@@ -7279,7 +7290,7 @@ function emptyBlock(slot = tryAsTimeSlot(0)) {
7279
7290
  });
7280
7291
  }
7281
7292
 
7282
- var index$j = /*#__PURE__*/Object.freeze({
7293
+ var index$l = /*#__PURE__*/Object.freeze({
7283
7294
  __proto__: null,
7284
7295
  Block: Block,
7285
7296
  EpochMarker: EpochMarker,
@@ -7535,7 +7546,7 @@ var json;
7535
7546
  json.object = object;
7536
7547
  })(json || (json = {}));
7537
7548
 
7538
- var index$i = /*#__PURE__*/Object.freeze({
7549
+ var index$k = /*#__PURE__*/Object.freeze({
7539
7550
  __proto__: null,
7540
7551
  get json () { return json; },
7541
7552
  parseFromJson: parseFromJson
@@ -7805,7 +7816,7 @@ const blockFromJson = (spec) => json.object({
7805
7816
  extrinsic: getExtrinsicFromJson(spec),
7806
7817
  }, ({ header, extrinsic }) => Block.create({ header, extrinsic }));
7807
7818
 
7808
- var index$h = /*#__PURE__*/Object.freeze({
7819
+ var index$j = /*#__PURE__*/Object.freeze({
7809
7820
  __proto__: null,
7810
7821
  blockFromJson: blockFromJson,
7811
7822
  disputesExtrinsicFromJson: disputesExtrinsicFromJson,
@@ -8303,7 +8314,7 @@ class Logger {
8303
8314
  }
8304
8315
  }
8305
8316
 
8306
- var index$g = /*#__PURE__*/Object.freeze({
8317
+ var index$i = /*#__PURE__*/Object.freeze({
8307
8318
  __proto__: null,
8308
8319
  get Level () { return Level; },
8309
8320
  Logger: Logger,
@@ -8326,7 +8337,7 @@ class AuthorshipOptions {
8326
8337
  }
8327
8338
  }
8328
8339
 
8329
- const logger$4 = Logger.new(undefined, "config");
8340
+ const logger$5 = Logger.new(undefined, "config");
8330
8341
  /** Development config. Will accept unsealed blocks for now. */
8331
8342
  const DEV_CONFIG = "dev";
8332
8343
  /** Default config file. */
@@ -8385,15 +8396,15 @@ class NodeConfiguration {
8385
8396
  }
8386
8397
  function loadConfig(configPath) {
8387
8398
  if (configPath === DEFAULT_CONFIG) {
8388
- logger$4.log `🔧 Loading DEFAULT config`;
8399
+ logger$5.log `🔧 Loading DEFAULT config`;
8389
8400
  return parseFromJson(configs.default, NodeConfiguration.fromJson);
8390
8401
  }
8391
8402
  if (configPath === DEV_CONFIG) {
8392
- logger$4.log `🔧 Loading DEV config`;
8403
+ logger$5.log `🔧 Loading DEV config`;
8393
8404
  return parseFromJson(configs.dev, NodeConfiguration.fromJson);
8394
8405
  }
8395
8406
  try {
8396
- logger$4.log `🔧 Loading config from ${configPath}`;
8407
+ logger$5.log `🔧 Loading config from ${configPath}`;
8397
8408
  const configFile = fs.readFileSync(configPath, "utf8");
8398
8409
  const parsed = JSON.parse(configFile);
8399
8410
  return parseFromJson(parsed, NodeConfiguration.fromJson);
@@ -8403,7 +8414,7 @@ function loadConfig(configPath) {
8403
8414
  }
8404
8415
  }
8405
8416
 
8406
- var index$f = /*#__PURE__*/Object.freeze({
8417
+ var index$h = /*#__PURE__*/Object.freeze({
8407
8418
  __proto__: null,
8408
8419
  DEFAULT_CONFIG: DEFAULT_CONFIG,
8409
8420
  DEV_CONFIG: DEV_CONFIG,
@@ -10069,7 +10080,7 @@ const serviceDataCodec = codec$1.dictionary(codec$1.u32.asOpaque(), serviceEntri
10069
10080
  sortKeys: (a, b) => a - b,
10070
10081
  });
10071
10082
 
10072
- var index$e = /*#__PURE__*/Object.freeze({
10083
+ var index$g = /*#__PURE__*/Object.freeze({
10073
10084
  __proto__: null,
10074
10085
  AccumulationOutput: AccumulationOutput,
10075
10086
  AutoAccumulate: AutoAccumulate,
@@ -11079,7 +11090,7 @@ const bitLookup = [
11079
11090
  [0b00000000, 8],
11080
11091
  ];
11081
11092
 
11082
- var index$d = /*#__PURE__*/Object.freeze({
11093
+ var index$f = /*#__PURE__*/Object.freeze({
11083
11094
  __proto__: null,
11084
11095
  BranchNode: BranchNode,
11085
11096
  InMemoryTrie: InMemoryTrie,
@@ -11425,7 +11436,7 @@ function loadState(spec, entries) {
11425
11436
  * hashmap of `key -> value` entries.
11426
11437
  */
11427
11438
 
11428
- var index$c = /*#__PURE__*/Object.freeze({
11439
+ var index$e = /*#__PURE__*/Object.freeze({
11429
11440
  __proto__: null,
11430
11441
  SerializedService: SerializedService,
11431
11442
  SerializedState: SerializedState,
@@ -11711,7 +11722,7 @@ class InMemoryStates {
11711
11722
  }
11712
11723
  }
11713
11724
 
11714
- var index$b = /*#__PURE__*/Object.freeze({
11725
+ var index$d = /*#__PURE__*/Object.freeze({
11715
11726
  __proto__: null,
11716
11727
  InMemoryBlocks: InMemoryBlocks,
11717
11728
  InMemoryStates: InMemoryStates,
@@ -12073,7 +12084,7 @@ const initEc = async () => {
12073
12084
  await init.reedSolomon();
12074
12085
  };
12075
12086
 
12076
- var index$a = /*#__PURE__*/Object.freeze({
12087
+ var index$c = /*#__PURE__*/Object.freeze({
12077
12088
  __proto__: null,
12078
12089
  N_CHUNKS_REDUNDANCY: N_CHUNKS_REDUNDANCY,
12079
12090
  N_CHUNKS_REQUIRED: N_CHUNKS_REQUIRED,
@@ -12097,6 +12108,439 @@ var index$a = /*#__PURE__*/Object.freeze({
12097
12108
  unzip: unzip
12098
12109
  });
12099
12110
 
12111
+ /**
12112
+ * Version ::= SEQUENCE {
12113
+ * major INTEGER (0..255),
12114
+ * minor INTEGER (0..255),
12115
+ * patch INTEGER (0..255)
12116
+ * }
12117
+ */
12118
+ class Version extends WithDebug {
12119
+ major;
12120
+ minor;
12121
+ patch;
12122
+ static Codec = codec$1.Class(Version, {
12123
+ major: codec$1.u8,
12124
+ minor: codec$1.u8,
12125
+ patch: codec$1.u8,
12126
+ });
12127
+ static tryFromString(str) {
12128
+ const parse = (v) => tryAsU8(Number(v));
12129
+ try {
12130
+ const [major, minor, patch] = str.trim().split(".").map(parse);
12131
+ return Version.create({
12132
+ major,
12133
+ minor,
12134
+ patch,
12135
+ });
12136
+ }
12137
+ catch (e) {
12138
+ throw new Error(`Unable to parse ${str} as Version: ${e}`);
12139
+ }
12140
+ }
12141
+ static create({ major, minor, patch }) {
12142
+ return new Version(major, minor, patch);
12143
+ }
12144
+ constructor(major, minor, patch) {
12145
+ super();
12146
+ this.major = major;
12147
+ this.minor = minor;
12148
+ this.patch = patch;
12149
+ }
12150
+ }
12151
+ /**
12152
+ * Fuzzer Protocol V1
12153
+ * Reference: https://github.com/davxy/jam-conformance/blob/main/fuzz-proto/fuzz.asn
12154
+ */
12155
+ // Feature bit constants
12156
+ var Features;
12157
+ (function (Features) {
12158
+ Features[Features["Ancestry"] = 1] = "Ancestry";
12159
+ Features[Features["Fork"] = 2] = "Fork";
12160
+ Features[Features["Reserved"] = 2147483648] = "Reserved";
12161
+ })(Features || (Features = {}));
12162
+ /**
12163
+ * PeerInfo ::= SEQUENCE {
12164
+ * fuzz-version U8,
12165
+ * features Features,
12166
+ * jam-version Version,
12167
+ * app-version Version,
12168
+ * name UTF8String
12169
+ * }
12170
+ */
12171
+ class PeerInfo extends WithDebug {
12172
+ fuzzVersion;
12173
+ features;
12174
+ jamVersion;
12175
+ appVersion;
12176
+ name;
12177
+ static Codec = codec$1.Class(PeerInfo, {
12178
+ fuzzVersion: codec$1.u8,
12179
+ features: codec$1.u32,
12180
+ jamVersion: Version.Codec,
12181
+ appVersion: Version.Codec,
12182
+ name: codec$1.string,
12183
+ });
12184
+ static create({ fuzzVersion, features, appVersion, jamVersion, name }) {
12185
+ return new PeerInfo(fuzzVersion, features, jamVersion, appVersion, name);
12186
+ }
12187
+ constructor(fuzzVersion, features, jamVersion, appVersion, name) {
12188
+ super();
12189
+ this.fuzzVersion = fuzzVersion;
12190
+ this.features = features;
12191
+ this.jamVersion = jamVersion;
12192
+ this.appVersion = appVersion;
12193
+ this.name = name;
12194
+ }
12195
+ }
12196
+ /**
12197
+ * AncestryItem ::= SEQUENCE {
12198
+ * slot TimeSlot,
12199
+ * header-hash HeaderHash
12200
+ * }
12201
+ */
12202
+ class AncestryItem extends WithDebug {
12203
+ slot;
12204
+ headerHash;
12205
+ static Codec = codec$1.Class(AncestryItem, {
12206
+ slot: codec$1.u32.asOpaque(),
12207
+ headerHash: codec$1.bytes(HASH_SIZE).asOpaque(),
12208
+ });
12209
+ static create({ slot, headerHash }) {
12210
+ return new AncestryItem(slot, headerHash);
12211
+ }
12212
+ constructor(slot, headerHash) {
12213
+ super();
12214
+ this.slot = slot;
12215
+ this.headerHash = headerHash;
12216
+ }
12217
+ }
12218
+ /**
12219
+ * KeyValue ::= SEQUENCE {
12220
+ * key TrieKey,
12221
+ * value OCTET STRING
12222
+ * }
12223
+ */
12224
+ class KeyValue extends WithDebug {
12225
+ key;
12226
+ value;
12227
+ static Codec = codec$1.Class(KeyValue, {
12228
+ key: codec$1.bytes(TRUNCATED_HASH_SIZE),
12229
+ value: codec$1.blob,
12230
+ });
12231
+ static create({ key, value }) {
12232
+ return new KeyValue(key, value);
12233
+ }
12234
+ constructor(key, value) {
12235
+ super();
12236
+ this.key = key;
12237
+ this.value = value;
12238
+ }
12239
+ }
12240
+ /** State ::= SEQUENCE OF KeyValue */
12241
+ const stateCodec = codec$1.sequenceVarLen(KeyValue.Codec);
12242
+ /**
12243
+ * Ancestry ::= SEQUENCE (SIZE(0..24)) OF AncestryItem
12244
+ * Empty when `feature-ancestry` is not supported by both parties
12245
+ */
12246
+ const ancestryCodec = codec$1.sequenceVarLen(AncestryItem.Codec, {
12247
+ minLength: 0,
12248
+ maxLength: 24,
12249
+ });
12250
+ /**
12251
+ * Initialize ::= SEQUENCE {
12252
+ * header Header,
12253
+ * keyvals State,
12254
+ * ancestry Ancestry
12255
+ * }
12256
+ */
12257
+ class Initialize extends WithDebug {
12258
+ header;
12259
+ keyvals;
12260
+ ancestry;
12261
+ static Codec = codec$1.Class(Initialize, {
12262
+ header: Header.Codec,
12263
+ keyvals: stateCodec,
12264
+ ancestry: ancestryCodec,
12265
+ });
12266
+ static create({ header, keyvals, ancestry }) {
12267
+ return new Initialize(header, keyvals, ancestry);
12268
+ }
12269
+ constructor(header, keyvals, ancestry) {
12270
+ super();
12271
+ this.header = header;
12272
+ this.keyvals = keyvals;
12273
+ this.ancestry = ancestry;
12274
+ }
12275
+ }
12276
+ /** GetState ::= HeaderHash */
12277
+ const getStateCodec = codec$1.bytes(HASH_SIZE).asOpaque();
12278
+ /** StateRoot ::= StateRootHash */
12279
+ const stateRootCodec = codec$1.bytes(HASH_SIZE).asOpaque();
12280
+ /** Error ::= UTF8String */
12281
+ class ErrorMessage extends WithDebug {
12282
+ message;
12283
+ static Codec = codec$1.Class(ErrorMessage, {
12284
+ message: codec$1.string,
12285
+ });
12286
+ static create({ message }) {
12287
+ return new ErrorMessage(message);
12288
+ }
12289
+ constructor(message) {
12290
+ super();
12291
+ this.message = message;
12292
+ }
12293
+ }
12294
+ /** Message choice type tags */
12295
+ var MessageType;
12296
+ (function (MessageType) {
12297
+ MessageType[MessageType["PeerInfo"] = 0] = "PeerInfo";
12298
+ MessageType[MessageType["Initialize"] = 1] = "Initialize";
12299
+ MessageType[MessageType["StateRoot"] = 2] = "StateRoot";
12300
+ MessageType[MessageType["ImportBlock"] = 3] = "ImportBlock";
12301
+ MessageType[MessageType["GetState"] = 4] = "GetState";
12302
+ MessageType[MessageType["State"] = 5] = "State";
12303
+ MessageType[MessageType["Error"] = 255] = "Error";
12304
+ })(MessageType || (MessageType = {}));
12305
+ /**
12306
+ * Message ::= CHOICE {
12307
+ * peer-info [0] PeerInfo,
12308
+ * initialize [1] Initialize,
12309
+ * state-root [2] StateRoot,
12310
+ * import-block [3] ImportBlock,
12311
+ * get-state [4] GetState,
12312
+ * state [5] State,
12313
+ * error [255] Error
12314
+ * }
12315
+ */
12316
+ const messageCodec = codec$1.custom({
12317
+ name: "Message",
12318
+ sizeHint: { bytes: 1, isExact: false },
12319
+ }, (e, msg) => {
12320
+ e.i8(msg.type);
12321
+ switch (msg.type) {
12322
+ case MessageType.PeerInfo:
12323
+ PeerInfo.Codec.encode(e, msg.value);
12324
+ break;
12325
+ case MessageType.Initialize:
12326
+ Initialize.Codec.encode(e, msg.value);
12327
+ break;
12328
+ case MessageType.StateRoot:
12329
+ stateRootCodec.encode(e, msg.value);
12330
+ break;
12331
+ case MessageType.ImportBlock:
12332
+ Block.Codec.View.encode(e, msg.value);
12333
+ break;
12334
+ case MessageType.GetState:
12335
+ getStateCodec.encode(e, msg.value);
12336
+ break;
12337
+ case MessageType.State:
12338
+ stateCodec.encode(e, msg.value);
12339
+ break;
12340
+ case MessageType.Error:
12341
+ ErrorMessage.Codec.encode(e, msg.value);
12342
+ break;
12343
+ default:
12344
+ throw new Error(`Unknown message type: ${msg}`);
12345
+ }
12346
+ }, (d) => {
12347
+ const type = d.u8();
12348
+ switch (type) {
12349
+ case MessageType.PeerInfo:
12350
+ return { type: MessageType.PeerInfo, value: PeerInfo.Codec.decode(d) };
12351
+ case MessageType.Initialize:
12352
+ return { type: MessageType.Initialize, value: Initialize.Codec.decode(d) };
12353
+ case MessageType.StateRoot:
12354
+ return { type: MessageType.StateRoot, value: stateRootCodec.decode(d) };
12355
+ case MessageType.ImportBlock:
12356
+ return { type: MessageType.ImportBlock, value: Block.Codec.View.decode(d) };
12357
+ case MessageType.GetState:
12358
+ return { type: MessageType.GetState, value: getStateCodec.decode(d) };
12359
+ case MessageType.State:
12360
+ return { type: MessageType.State, value: stateCodec.decode(d) };
12361
+ case MessageType.Error:
12362
+ return { type: MessageType.Error, value: ErrorMessage.Codec.decode(d) };
12363
+ default:
12364
+ throw new Error(`Unknown message type: ${type}`);
12365
+ }
12366
+ }, (s) => {
12367
+ const type = s.decoder.u8();
12368
+ switch (type) {
12369
+ case MessageType.PeerInfo:
12370
+ PeerInfo.Codec.View.skip(s);
12371
+ break;
12372
+ case MessageType.Initialize:
12373
+ Initialize.Codec.View.skip(s);
12374
+ break;
12375
+ case MessageType.StateRoot:
12376
+ stateRootCodec.View.skip(s);
12377
+ break;
12378
+ case MessageType.ImportBlock:
12379
+ Block.Codec.View.skip(s);
12380
+ break;
12381
+ case MessageType.GetState:
12382
+ getStateCodec.View.skip(s);
12383
+ break;
12384
+ case MessageType.State:
12385
+ stateCodec.View.skip(s);
12386
+ break;
12387
+ case MessageType.Error:
12388
+ ErrorMessage.Codec.View.skip(s);
12389
+ break;
12390
+ default:
12391
+ throw new Error(`Unknown message type: ${type}`);
12392
+ }
12393
+ });
12394
+
12395
+ const logger$4 = Logger.new(undefined, "ext-ipc-fuzz-v1");
12396
+ class FuzzTarget {
12397
+ msgHandler;
12398
+ sender;
12399
+ spec;
12400
+ sessionFeatures = 0;
12401
+ constructor(msgHandler, sender, spec) {
12402
+ this.msgHandler = msgHandler;
12403
+ this.sender = sender;
12404
+ this.spec = spec;
12405
+ }
12406
+ async onSocketMessage(msg) {
12407
+ // attempt to decode the messsage
12408
+ try {
12409
+ const message = Decoder.decodeObject(messageCodec, msg, this.spec);
12410
+ logger$4.log `[${message.type}] incoming message`;
12411
+ await this.processAndRespond(message);
12412
+ }
12413
+ catch (e) {
12414
+ logger$4.error `Error while processing fuzz v1 message: ${e}`;
12415
+ logger$4.error `${e}`;
12416
+ if (e instanceof Error) {
12417
+ logger$4.error `${e.stack ?? ""}`;
12418
+ }
12419
+ this.sender.close();
12420
+ }
12421
+ }
12422
+ async processAndRespond(message) {
12423
+ let response = null;
12424
+ switch (message.type) {
12425
+ case MessageType.PeerInfo: {
12426
+ // only support V1
12427
+ if (message.value.fuzzVersion !== 1) {
12428
+ logger$4.warn `Unsupported fuzzer protocol version: ${message.value.fuzzVersion}. Closing`;
12429
+ this.sender.close();
12430
+ return;
12431
+ }
12432
+ // Handle handshake
12433
+ const ourPeerInfo = await this.msgHandler.getPeerInfo(message.value);
12434
+ // Calculate session features (intersection of both peer features)
12435
+ this.sessionFeatures = message.value.features & ourPeerInfo.features;
12436
+ logger$4.info `Handshake completed. Shared features: 0b${this.sessionFeatures.toString(2)}`;
12437
+ logger$4.log `Feature ancestry: ${(this.sessionFeatures & Features.Ancestry) !== 0}`;
12438
+ logger$4.log `Feature fork: ${(this.sessionFeatures & Features.Fork) !== 0}`;
12439
+ response = {
12440
+ type: MessageType.PeerInfo,
12441
+ value: ourPeerInfo,
12442
+ };
12443
+ break;
12444
+ }
12445
+ case MessageType.Initialize: {
12446
+ const stateRoot = await this.msgHandler.initialize(message.value);
12447
+ response = {
12448
+ type: MessageType.StateRoot,
12449
+ value: stateRoot,
12450
+ };
12451
+ break;
12452
+ }
12453
+ case MessageType.ImportBlock: {
12454
+ const result = await this.msgHandler.importBlock(message.value);
12455
+ if (result.isOk) {
12456
+ response = {
12457
+ type: MessageType.StateRoot,
12458
+ value: result.ok,
12459
+ };
12460
+ }
12461
+ else {
12462
+ response = {
12463
+ type: MessageType.Error,
12464
+ value: result.error,
12465
+ };
12466
+ }
12467
+ break;
12468
+ }
12469
+ case MessageType.GetState: {
12470
+ const state = await this.msgHandler.getSerializedState(message.value);
12471
+ response = {
12472
+ type: MessageType.State,
12473
+ value: state,
12474
+ };
12475
+ break;
12476
+ }
12477
+ case MessageType.StateRoot: {
12478
+ logger$4.log `--> Received unexpected 'StateRoot' message from the fuzzer. Closing.`;
12479
+ this.sender.close();
12480
+ return;
12481
+ }
12482
+ case MessageType.State: {
12483
+ logger$4.log `--> Received unexpected 'State' message from the fuzzer. Closing.`;
12484
+ this.sender.close();
12485
+ return;
12486
+ }
12487
+ case MessageType.Error: {
12488
+ logger$4.log `--> Received unexpected 'Error' message from the fuzzer. Closing.`;
12489
+ this.sender.close();
12490
+ return;
12491
+ }
12492
+ default: {
12493
+ logger$4.log `--> Received unexpected message type ${JSON.stringify(message)} from the fuzzer. Closing.`;
12494
+ this.sender.close();
12495
+ try {
12496
+ assertNever(message);
12497
+ }
12498
+ catch {
12499
+ return;
12500
+ }
12501
+ }
12502
+ }
12503
+ if (response !== null) {
12504
+ logger$4.trace `<-- responding with: ${response.type}`;
12505
+ const encoded = Encoder.encodeObject(messageCodec, response, this.spec);
12506
+ this.sender.send(encoded);
12507
+ }
12508
+ else {
12509
+ logger$4.warn `<-- no response generated for: ${message.type}`;
12510
+ }
12511
+ }
12512
+ onClose({ error }) {
12513
+ logger$4.log `Closing the v1 handler. Reason: ${error !== undefined ? error.message : "close"}.`;
12514
+ }
12515
+ /** Check if a specific feature is enabled in the session */
12516
+ hasFeature(feature) {
12517
+ return (this.sessionFeatures & feature) !== 0;
12518
+ }
12519
+ }
12520
+
12521
+ var index$b = /*#__PURE__*/Object.freeze({
12522
+ __proto__: null,
12523
+ AncestryItem: AncestryItem,
12524
+ ErrorMessage: ErrorMessage,
12525
+ get Features () { return Features; },
12526
+ FuzzTarget: FuzzTarget,
12527
+ Initialize: Initialize,
12528
+ KeyValue: KeyValue,
12529
+ get MessageType () { return MessageType; },
12530
+ PeerInfo: PeerInfo,
12531
+ Version: Version,
12532
+ ancestryCodec: ancestryCodec,
12533
+ getStateCodec: getStateCodec,
12534
+ messageCodec: messageCodec,
12535
+ stateCodec: stateCodec,
12536
+ stateRootCodec: stateRootCodec
12537
+ });
12538
+
12539
+ var index$a = /*#__PURE__*/Object.freeze({
12540
+ __proto__: null,
12541
+ v1: index$b
12542
+ });
12543
+
12100
12544
  /** Size of the transfer memo. */
12101
12545
  const TRANSFER_MEMO_BYTES = W_T;
12102
12546
  /**
@@ -16939,8 +17383,8 @@ var index$3 = /*#__PURE__*/Object.freeze({
16939
17383
  asOpaqueType: asOpaqueType,
16940
17384
  assertEmpty: assertEmpty,
16941
17385
  assertNever: assertNever,
16942
- block: index$j,
16943
- bytes: index$q,
17386
+ block: index$l,
17387
+ bytes: index$s,
16944
17388
  check: check,
16945
17389
  clampU64ToU32: clampU64ToU32,
16946
17390
  createResults: createResults,
@@ -16948,13 +17392,13 @@ var index$3 = /*#__PURE__*/Object.freeze({
16948
17392
  extractCodeAndMetadata: extractCodeAndMetadata,
16949
17393
  getServiceId: getServiceId,
16950
17394
  getServiceIdOrCurrent: getServiceIdOrCurrent,
16951
- hash: index$n,
17395
+ hash: index$p,
16952
17396
  inspect: inspect,
16953
17397
  instructionArgumentTypeMap: instructionArgumentTypeMap,
16954
17398
  interpreter: index$7,
16955
17399
  isBrowser: isBrowser,
16956
17400
  measure: measure,
16957
- numbers: index$p,
17401
+ numbers: index$r,
16958
17402
  resultToString: resultToString,
16959
17403
  seeThrough: seeThrough,
16960
17404
  slotsToPreimageStatus: slotsToPreimageStatus,
@@ -17668,32 +18112,33 @@ var index = /*#__PURE__*/Object.freeze({
17668
18112
  WorkPackageExecutor: WorkPackageExecutor
17669
18113
  });
17670
18114
 
17671
- exports.block = index$j;
17672
- exports.block_json = index$h;
17673
- exports.bytes = index$q;
17674
- exports.codec = index$o;
17675
- exports.collections = index$l;
17676
- exports.config = index$k;
17677
- exports.config_node = index$f;
17678
- exports.crypto = index$m;
17679
- exports.database = index$b;
17680
- exports.erasure_coding = index$a;
17681
- exports.hash = index$n;
18115
+ exports.block = index$l;
18116
+ exports.block_json = index$j;
18117
+ exports.bytes = index$s;
18118
+ exports.codec = index$q;
18119
+ exports.collections = index$n;
18120
+ exports.config = index$m;
18121
+ exports.config_node = index$h;
18122
+ exports.crypto = index$o;
18123
+ exports.database = index$d;
18124
+ exports.erasure_coding = index$c;
18125
+ exports.fuzz_proto = index$a;
18126
+ exports.hash = index$p;
17682
18127
  exports.jam_host_calls = index$9;
17683
- exports.json_parser = index$i;
17684
- exports.logger = index$g;
18128
+ exports.json_parser = index$k;
18129
+ exports.logger = index$i;
17685
18130
  exports.mmr = index$8;
17686
- exports.numbers = index$p;
17687
- exports.ordering = index$r;
18131
+ exports.numbers = index$r;
18132
+ exports.ordering = index$t;
17688
18133
  exports.pvm = index$3;
17689
18134
  exports.pvm_host_calls = index$6;
17690
18135
  exports.pvm_interpreter = index$7;
17691
18136
  exports.pvm_program = index$4;
17692
18137
  exports.pvm_spi_decoder = index$5;
17693
18138
  exports.shuffling = index$2;
17694
- exports.state = index$e;
18139
+ exports.state = index$g;
17695
18140
  exports.state_json = index$1;
17696
- exports.state_merkleization = index$c;
18141
+ exports.state_merkleization = index$e;
17697
18142
  exports.transition = index;
17698
- exports.trie = index$d;
17699
- exports.utils = index$s;
18143
+ exports.trie = index$f;
18144
+ exports.utils = index$u;