@typeberry/lib 0.1.2-8c85275 → 0.1.2-a90b655

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,
@@ -7254,7 +7261,7 @@ class Block extends WithDebug {
7254
7261
  }
7255
7262
  }
7256
7263
 
7257
- var index$j = /*#__PURE__*/Object.freeze({
7264
+ var index$l = /*#__PURE__*/Object.freeze({
7258
7265
  __proto__: null,
7259
7266
  Block: Block,
7260
7267
  EpochMarker: EpochMarker,
@@ -7509,7 +7516,7 @@ var json;
7509
7516
  json.object = object;
7510
7517
  })(json || (json = {}));
7511
7518
 
7512
- var index$i = /*#__PURE__*/Object.freeze({
7519
+ var index$k = /*#__PURE__*/Object.freeze({
7513
7520
  __proto__: null,
7514
7521
  get json () { return json; },
7515
7522
  parseFromJson: parseFromJson
@@ -7779,7 +7786,7 @@ const blockFromJson = (spec) => json.object({
7779
7786
  extrinsic: getExtrinsicFromJson(spec),
7780
7787
  }, ({ header, extrinsic }) => Block.create({ header, extrinsic }));
7781
7788
 
7782
- var index$h = /*#__PURE__*/Object.freeze({
7789
+ var index$j = /*#__PURE__*/Object.freeze({
7783
7790
  __proto__: null,
7784
7791
  blockFromJson: blockFromJson,
7785
7792
  disputesExtrinsicFromJson: disputesExtrinsicFromJson,
@@ -8277,7 +8284,7 @@ class Logger {
8277
8284
  }
8278
8285
  }
8279
8286
 
8280
- var index$g = /*#__PURE__*/Object.freeze({
8287
+ var index$i = /*#__PURE__*/Object.freeze({
8281
8288
  __proto__: null,
8282
8289
  get Level () { return Level; },
8283
8290
  Logger: Logger,
@@ -8300,7 +8307,7 @@ class AuthorshipOptions {
8300
8307
  }
8301
8308
  }
8302
8309
 
8303
- const logger$4 = Logger.new(undefined, "config");
8310
+ const logger$5 = Logger.new(undefined, "config");
8304
8311
  /** Development config. Will accept unsealed blocks for now. */
8305
8312
  const DEV_CONFIG = "dev";
8306
8313
  /** Default config file. */
@@ -8359,15 +8366,15 @@ class NodeConfiguration {
8359
8366
  }
8360
8367
  function loadConfig(configPath) {
8361
8368
  if (configPath === DEFAULT_CONFIG) {
8362
- logger$4.log `🔧 Loading DEFAULT config`;
8369
+ logger$5.log `🔧 Loading DEFAULT config`;
8363
8370
  return parseFromJson(configs.default, NodeConfiguration.fromJson);
8364
8371
  }
8365
8372
  if (configPath === DEV_CONFIG) {
8366
- logger$4.log `🔧 Loading DEV config`;
8373
+ logger$5.log `🔧 Loading DEV config`;
8367
8374
  return parseFromJson(configs.dev, NodeConfiguration.fromJson);
8368
8375
  }
8369
8376
  try {
8370
- logger$4.log `🔧 Loading config from ${configPath}`;
8377
+ logger$5.log `🔧 Loading config from ${configPath}`;
8371
8378
  const configFile = fs.readFileSync(configPath, "utf8");
8372
8379
  const parsed = JSON.parse(configFile);
8373
8380
  return parseFromJson(parsed, NodeConfiguration.fromJson);
@@ -8377,7 +8384,7 @@ function loadConfig(configPath) {
8377
8384
  }
8378
8385
  }
8379
8386
 
8380
- var index$f = /*#__PURE__*/Object.freeze({
8387
+ var index$h = /*#__PURE__*/Object.freeze({
8381
8388
  __proto__: null,
8382
8389
  DEFAULT_CONFIG: DEFAULT_CONFIG,
8383
8390
  DEV_CONFIG: DEV_CONFIG,
@@ -10043,7 +10050,7 @@ const serviceDataCodec = codec$1.dictionary(codec$1.u32.asOpaque(), serviceEntri
10043
10050
  sortKeys: (a, b) => a - b,
10044
10051
  });
10045
10052
 
10046
- var index$e = /*#__PURE__*/Object.freeze({
10053
+ var index$g = /*#__PURE__*/Object.freeze({
10047
10054
  __proto__: null,
10048
10055
  AccumulationOutput: AccumulationOutput,
10049
10056
  AutoAccumulate: AutoAccumulate,
@@ -11053,7 +11060,7 @@ const bitLookup = [
11053
11060
  [0b00000000, 8],
11054
11061
  ];
11055
11062
 
11056
- var index$d = /*#__PURE__*/Object.freeze({
11063
+ var index$f = /*#__PURE__*/Object.freeze({
11057
11064
  __proto__: null,
11058
11065
  BranchNode: BranchNode,
11059
11066
  InMemoryTrie: InMemoryTrie,
@@ -11399,7 +11406,7 @@ function loadState(spec, entries) {
11399
11406
  * hashmap of `key -> value` entries.
11400
11407
  */
11401
11408
 
11402
- var index$c = /*#__PURE__*/Object.freeze({
11409
+ var index$e = /*#__PURE__*/Object.freeze({
11403
11410
  __proto__: null,
11404
11411
  SerializedService: SerializedService,
11405
11412
  SerializedState: SerializedState,
@@ -11685,7 +11692,7 @@ class InMemoryStates {
11685
11692
  }
11686
11693
  }
11687
11694
 
11688
- var index$b = /*#__PURE__*/Object.freeze({
11695
+ var index$d = /*#__PURE__*/Object.freeze({
11689
11696
  __proto__: null,
11690
11697
  InMemoryBlocks: InMemoryBlocks,
11691
11698
  InMemoryStates: InMemoryStates,
@@ -12047,7 +12054,7 @@ const initEc = async () => {
12047
12054
  await init.reedSolomon();
12048
12055
  };
12049
12056
 
12050
- var index$a = /*#__PURE__*/Object.freeze({
12057
+ var index$c = /*#__PURE__*/Object.freeze({
12051
12058
  __proto__: null,
12052
12059
  N_CHUNKS_REDUNDANCY: N_CHUNKS_REDUNDANCY,
12053
12060
  N_CHUNKS_REQUIRED: N_CHUNKS_REQUIRED,
@@ -12071,6 +12078,439 @@ var index$a = /*#__PURE__*/Object.freeze({
12071
12078
  unzip: unzip
12072
12079
  });
12073
12080
 
12081
+ /**
12082
+ * Version ::= SEQUENCE {
12083
+ * major INTEGER (0..255),
12084
+ * minor INTEGER (0..255),
12085
+ * patch INTEGER (0..255)
12086
+ * }
12087
+ */
12088
+ class Version extends WithDebug {
12089
+ major;
12090
+ minor;
12091
+ patch;
12092
+ static Codec = codec$1.Class(Version, {
12093
+ major: codec$1.u8,
12094
+ minor: codec$1.u8,
12095
+ patch: codec$1.u8,
12096
+ });
12097
+ static tryFromString(str) {
12098
+ const parse = (v) => tryAsU8(Number(v));
12099
+ try {
12100
+ const [major, minor, patch] = str.trim().split(".").map(parse);
12101
+ return Version.create({
12102
+ major,
12103
+ minor,
12104
+ patch,
12105
+ });
12106
+ }
12107
+ catch (e) {
12108
+ throw new Error(`Unable to parse ${str} as Version: ${e}`);
12109
+ }
12110
+ }
12111
+ static create({ major, minor, patch }) {
12112
+ return new Version(major, minor, patch);
12113
+ }
12114
+ constructor(major, minor, patch) {
12115
+ super();
12116
+ this.major = major;
12117
+ this.minor = minor;
12118
+ this.patch = patch;
12119
+ }
12120
+ }
12121
+ /**
12122
+ * Fuzzer Protocol V1
12123
+ * Reference: https://github.com/davxy/jam-conformance/blob/main/fuzz-proto/fuzz.asn
12124
+ */
12125
+ // Feature bit constants
12126
+ var Features;
12127
+ (function (Features) {
12128
+ Features[Features["Ancestry"] = 1] = "Ancestry";
12129
+ Features[Features["Fork"] = 2] = "Fork";
12130
+ Features[Features["Reserved"] = 2147483648] = "Reserved";
12131
+ })(Features || (Features = {}));
12132
+ /**
12133
+ * PeerInfo ::= SEQUENCE {
12134
+ * fuzz-version U8,
12135
+ * features Features,
12136
+ * jam-version Version,
12137
+ * app-version Version,
12138
+ * name UTF8String
12139
+ * }
12140
+ */
12141
+ class PeerInfo extends WithDebug {
12142
+ fuzzVersion;
12143
+ features;
12144
+ jamVersion;
12145
+ appVersion;
12146
+ name;
12147
+ static Codec = codec$1.Class(PeerInfo, {
12148
+ fuzzVersion: codec$1.u8,
12149
+ features: codec$1.u32,
12150
+ jamVersion: Version.Codec,
12151
+ appVersion: Version.Codec,
12152
+ name: codec$1.string,
12153
+ });
12154
+ static create({ fuzzVersion, features, appVersion, jamVersion, name }) {
12155
+ return new PeerInfo(fuzzVersion, features, jamVersion, appVersion, name);
12156
+ }
12157
+ constructor(fuzzVersion, features, jamVersion, appVersion, name) {
12158
+ super();
12159
+ this.fuzzVersion = fuzzVersion;
12160
+ this.features = features;
12161
+ this.jamVersion = jamVersion;
12162
+ this.appVersion = appVersion;
12163
+ this.name = name;
12164
+ }
12165
+ }
12166
+ /**
12167
+ * AncestryItem ::= SEQUENCE {
12168
+ * slot TimeSlot,
12169
+ * header-hash HeaderHash
12170
+ * }
12171
+ */
12172
+ class AncestryItem extends WithDebug {
12173
+ slot;
12174
+ headerHash;
12175
+ static Codec = codec$1.Class(AncestryItem, {
12176
+ slot: codec$1.u32.asOpaque(),
12177
+ headerHash: codec$1.bytes(HASH_SIZE).asOpaque(),
12178
+ });
12179
+ static create({ slot, headerHash }) {
12180
+ return new AncestryItem(slot, headerHash);
12181
+ }
12182
+ constructor(slot, headerHash) {
12183
+ super();
12184
+ this.slot = slot;
12185
+ this.headerHash = headerHash;
12186
+ }
12187
+ }
12188
+ /**
12189
+ * KeyValue ::= SEQUENCE {
12190
+ * key TrieKey,
12191
+ * value OCTET STRING
12192
+ * }
12193
+ */
12194
+ class KeyValue extends WithDebug {
12195
+ key;
12196
+ value;
12197
+ static Codec = codec$1.Class(KeyValue, {
12198
+ key: codec$1.bytes(TRUNCATED_HASH_SIZE),
12199
+ value: codec$1.blob,
12200
+ });
12201
+ static create({ key, value }) {
12202
+ return new KeyValue(key, value);
12203
+ }
12204
+ constructor(key, value) {
12205
+ super();
12206
+ this.key = key;
12207
+ this.value = value;
12208
+ }
12209
+ }
12210
+ /** State ::= SEQUENCE OF KeyValue */
12211
+ const stateCodec = codec$1.sequenceVarLen(KeyValue.Codec);
12212
+ /**
12213
+ * Ancestry ::= SEQUENCE (SIZE(0..24)) OF AncestryItem
12214
+ * Empty when `feature-ancestry` is not supported by both parties
12215
+ */
12216
+ const ancestryCodec = codec$1.sequenceVarLen(AncestryItem.Codec, {
12217
+ minLength: 0,
12218
+ maxLength: 24,
12219
+ });
12220
+ /**
12221
+ * Initialize ::= SEQUENCE {
12222
+ * header Header,
12223
+ * keyvals State,
12224
+ * ancestry Ancestry
12225
+ * }
12226
+ */
12227
+ class Initialize extends WithDebug {
12228
+ header;
12229
+ keyvals;
12230
+ ancestry;
12231
+ static Codec = codec$1.Class(Initialize, {
12232
+ header: Header.Codec,
12233
+ keyvals: stateCodec,
12234
+ ancestry: ancestryCodec,
12235
+ });
12236
+ static create({ header, keyvals, ancestry }) {
12237
+ return new Initialize(header, keyvals, ancestry);
12238
+ }
12239
+ constructor(header, keyvals, ancestry) {
12240
+ super();
12241
+ this.header = header;
12242
+ this.keyvals = keyvals;
12243
+ this.ancestry = ancestry;
12244
+ }
12245
+ }
12246
+ /** GetState ::= HeaderHash */
12247
+ const getStateCodec = codec$1.bytes(HASH_SIZE).asOpaque();
12248
+ /** StateRoot ::= StateRootHash */
12249
+ const stateRootCodec = codec$1.bytes(HASH_SIZE).asOpaque();
12250
+ /** Error ::= UTF8String */
12251
+ class ErrorMessage extends WithDebug {
12252
+ message;
12253
+ static Codec = codec$1.Class(ErrorMessage, {
12254
+ message: codec$1.string,
12255
+ });
12256
+ static create({ message }) {
12257
+ return new ErrorMessage(message);
12258
+ }
12259
+ constructor(message) {
12260
+ super();
12261
+ this.message = message;
12262
+ }
12263
+ }
12264
+ /** Message choice type tags */
12265
+ var MessageType;
12266
+ (function (MessageType) {
12267
+ MessageType[MessageType["PeerInfo"] = 0] = "PeerInfo";
12268
+ MessageType[MessageType["Initialize"] = 1] = "Initialize";
12269
+ MessageType[MessageType["StateRoot"] = 2] = "StateRoot";
12270
+ MessageType[MessageType["ImportBlock"] = 3] = "ImportBlock";
12271
+ MessageType[MessageType["GetState"] = 4] = "GetState";
12272
+ MessageType[MessageType["State"] = 5] = "State";
12273
+ MessageType[MessageType["Error"] = 255] = "Error";
12274
+ })(MessageType || (MessageType = {}));
12275
+ /**
12276
+ * Message ::= CHOICE {
12277
+ * peer-info [0] PeerInfo,
12278
+ * initialize [1] Initialize,
12279
+ * state-root [2] StateRoot,
12280
+ * import-block [3] ImportBlock,
12281
+ * get-state [4] GetState,
12282
+ * state [5] State,
12283
+ * error [255] Error
12284
+ * }
12285
+ */
12286
+ const messageCodec = codec$1.custom({
12287
+ name: "Message",
12288
+ sizeHint: { bytes: 1, isExact: false },
12289
+ }, (e, msg) => {
12290
+ e.i8(msg.type);
12291
+ switch (msg.type) {
12292
+ case MessageType.PeerInfo:
12293
+ PeerInfo.Codec.encode(e, msg.value);
12294
+ break;
12295
+ case MessageType.Initialize:
12296
+ Initialize.Codec.encode(e, msg.value);
12297
+ break;
12298
+ case MessageType.StateRoot:
12299
+ stateRootCodec.encode(e, msg.value);
12300
+ break;
12301
+ case MessageType.ImportBlock:
12302
+ Block.Codec.View.encode(e, msg.value);
12303
+ break;
12304
+ case MessageType.GetState:
12305
+ getStateCodec.encode(e, msg.value);
12306
+ break;
12307
+ case MessageType.State:
12308
+ stateCodec.encode(e, msg.value);
12309
+ break;
12310
+ case MessageType.Error:
12311
+ ErrorMessage.Codec.encode(e, msg.value);
12312
+ break;
12313
+ default:
12314
+ throw new Error(`Unknown message type: ${msg}`);
12315
+ }
12316
+ }, (d) => {
12317
+ const type = d.u8();
12318
+ switch (type) {
12319
+ case MessageType.PeerInfo:
12320
+ return { type: MessageType.PeerInfo, value: PeerInfo.Codec.decode(d) };
12321
+ case MessageType.Initialize:
12322
+ return { type: MessageType.Initialize, value: Initialize.Codec.decode(d) };
12323
+ case MessageType.StateRoot:
12324
+ return { type: MessageType.StateRoot, value: stateRootCodec.decode(d) };
12325
+ case MessageType.ImportBlock:
12326
+ return { type: MessageType.ImportBlock, value: Block.Codec.View.decode(d) };
12327
+ case MessageType.GetState:
12328
+ return { type: MessageType.GetState, value: getStateCodec.decode(d) };
12329
+ case MessageType.State:
12330
+ return { type: MessageType.State, value: stateCodec.decode(d) };
12331
+ case MessageType.Error:
12332
+ return { type: MessageType.Error, value: ErrorMessage.Codec.decode(d) };
12333
+ default:
12334
+ throw new Error(`Unknown message type: ${type}`);
12335
+ }
12336
+ }, (s) => {
12337
+ const type = s.decoder.u8();
12338
+ switch (type) {
12339
+ case MessageType.PeerInfo:
12340
+ PeerInfo.Codec.View.skip(s);
12341
+ break;
12342
+ case MessageType.Initialize:
12343
+ Initialize.Codec.View.skip(s);
12344
+ break;
12345
+ case MessageType.StateRoot:
12346
+ stateRootCodec.View.skip(s);
12347
+ break;
12348
+ case MessageType.ImportBlock:
12349
+ Block.Codec.View.skip(s);
12350
+ break;
12351
+ case MessageType.GetState:
12352
+ getStateCodec.View.skip(s);
12353
+ break;
12354
+ case MessageType.State:
12355
+ stateCodec.View.skip(s);
12356
+ break;
12357
+ case MessageType.Error:
12358
+ ErrorMessage.Codec.View.skip(s);
12359
+ break;
12360
+ default:
12361
+ throw new Error(`Unknown message type: ${type}`);
12362
+ }
12363
+ });
12364
+
12365
+ const logger$4 = Logger.new(undefined, "ext-ipc-fuzz-v1");
12366
+ class FuzzTarget {
12367
+ msgHandler;
12368
+ sender;
12369
+ spec;
12370
+ sessionFeatures = 0;
12371
+ constructor(msgHandler, sender, spec) {
12372
+ this.msgHandler = msgHandler;
12373
+ this.sender = sender;
12374
+ this.spec = spec;
12375
+ }
12376
+ async onSocketMessage(msg) {
12377
+ // attempt to decode the messsage
12378
+ try {
12379
+ const message = Decoder.decodeObject(messageCodec, msg, this.spec);
12380
+ logger$4.log `[${message.type}] incoming message`;
12381
+ await this.processAndRespond(message);
12382
+ }
12383
+ catch (e) {
12384
+ logger$4.error `Error while processing fuzz v1 message: ${e}`;
12385
+ logger$4.error `${e}`;
12386
+ if (e instanceof Error) {
12387
+ logger$4.error `${e.stack ?? ""}`;
12388
+ }
12389
+ this.sender.close();
12390
+ }
12391
+ }
12392
+ async processAndRespond(message) {
12393
+ let response = null;
12394
+ switch (message.type) {
12395
+ case MessageType.PeerInfo: {
12396
+ // only support V1
12397
+ if (message.value.fuzzVersion !== 1) {
12398
+ logger$4.warn `Unsupported fuzzer protocol version: ${message.value.fuzzVersion}. Closing`;
12399
+ this.sender.close();
12400
+ return;
12401
+ }
12402
+ // Handle handshake
12403
+ const ourPeerInfo = await this.msgHandler.getPeerInfo(message.value);
12404
+ // Calculate session features (intersection of both peer features)
12405
+ this.sessionFeatures = message.value.features & ourPeerInfo.features;
12406
+ logger$4.info `Handshake completed. Shared features: 0b${this.sessionFeatures.toString(2)}`;
12407
+ logger$4.log `Feature ancestry: ${(this.sessionFeatures & Features.Ancestry) !== 0}`;
12408
+ logger$4.log `Feature fork: ${(this.sessionFeatures & Features.Fork) !== 0}`;
12409
+ response = {
12410
+ type: MessageType.PeerInfo,
12411
+ value: ourPeerInfo,
12412
+ };
12413
+ break;
12414
+ }
12415
+ case MessageType.Initialize: {
12416
+ const stateRoot = await this.msgHandler.initialize(message.value);
12417
+ response = {
12418
+ type: MessageType.StateRoot,
12419
+ value: stateRoot,
12420
+ };
12421
+ break;
12422
+ }
12423
+ case MessageType.ImportBlock: {
12424
+ const result = await this.msgHandler.importBlock(message.value);
12425
+ if (result.isOk) {
12426
+ response = {
12427
+ type: MessageType.StateRoot,
12428
+ value: result.ok,
12429
+ };
12430
+ }
12431
+ else {
12432
+ response = {
12433
+ type: MessageType.Error,
12434
+ value: result.error,
12435
+ };
12436
+ }
12437
+ break;
12438
+ }
12439
+ case MessageType.GetState: {
12440
+ const state = await this.msgHandler.getSerializedState(message.value);
12441
+ response = {
12442
+ type: MessageType.State,
12443
+ value: state,
12444
+ };
12445
+ break;
12446
+ }
12447
+ case MessageType.StateRoot: {
12448
+ logger$4.log `--> Received unexpected 'StateRoot' message from the fuzzer. Closing.`;
12449
+ this.sender.close();
12450
+ return;
12451
+ }
12452
+ case MessageType.State: {
12453
+ logger$4.log `--> Received unexpected 'State' message from the fuzzer. Closing.`;
12454
+ this.sender.close();
12455
+ return;
12456
+ }
12457
+ case MessageType.Error: {
12458
+ logger$4.log `--> Received unexpected 'Error' message from the fuzzer. Closing.`;
12459
+ this.sender.close();
12460
+ return;
12461
+ }
12462
+ default: {
12463
+ logger$4.log `--> Received unexpected message type ${JSON.stringify(message)} from the fuzzer. Closing.`;
12464
+ this.sender.close();
12465
+ try {
12466
+ assertNever(message);
12467
+ }
12468
+ catch {
12469
+ return;
12470
+ }
12471
+ }
12472
+ }
12473
+ if (response !== null) {
12474
+ logger$4.trace `<-- responding with: ${response.type}`;
12475
+ const encoded = Encoder.encodeObject(messageCodec, response, this.spec);
12476
+ this.sender.send(encoded);
12477
+ }
12478
+ else {
12479
+ logger$4.warn `<-- no response generated for: ${message.type}`;
12480
+ }
12481
+ }
12482
+ onClose({ error }) {
12483
+ logger$4.log `Closing the v1 handler. Reason: ${error !== undefined ? error.message : "close"}.`;
12484
+ }
12485
+ /** Check if a specific feature is enabled in the session */
12486
+ hasFeature(feature) {
12487
+ return (this.sessionFeatures & feature) !== 0;
12488
+ }
12489
+ }
12490
+
12491
+ var index$b = /*#__PURE__*/Object.freeze({
12492
+ __proto__: null,
12493
+ AncestryItem: AncestryItem,
12494
+ ErrorMessage: ErrorMessage,
12495
+ get Features () { return Features; },
12496
+ FuzzTarget: FuzzTarget,
12497
+ Initialize: Initialize,
12498
+ KeyValue: KeyValue,
12499
+ get MessageType () { return MessageType; },
12500
+ PeerInfo: PeerInfo,
12501
+ Version: Version,
12502
+ ancestryCodec: ancestryCodec,
12503
+ getStateCodec: getStateCodec,
12504
+ messageCodec: messageCodec,
12505
+ stateCodec: stateCodec,
12506
+ stateRootCodec: stateRootCodec
12507
+ });
12508
+
12509
+ var index$a = /*#__PURE__*/Object.freeze({
12510
+ __proto__: null,
12511
+ v1: index$b
12512
+ });
12513
+
12074
12514
  /** Size of the transfer memo. */
12075
12515
  const TRANSFER_MEMO_BYTES = W_T;
12076
12516
  /**
@@ -16901,8 +17341,8 @@ var index$3 = /*#__PURE__*/Object.freeze({
16901
17341
  asOpaqueType: asOpaqueType,
16902
17342
  assertEmpty: assertEmpty,
16903
17343
  assertNever: assertNever,
16904
- block: index$j,
16905
- bytes: index$q,
17344
+ block: index$l,
17345
+ bytes: index$s,
16906
17346
  check: check,
16907
17347
  clampU64ToU32: clampU64ToU32,
16908
17348
  createResults: createResults,
@@ -16910,13 +17350,13 @@ var index$3 = /*#__PURE__*/Object.freeze({
16910
17350
  extractCodeAndMetadata: extractCodeAndMetadata,
16911
17351
  getServiceId: getServiceId,
16912
17352
  getServiceIdOrCurrent: getServiceIdOrCurrent,
16913
- hash: index$n,
17353
+ hash: index$p,
16914
17354
  inspect: inspect,
16915
17355
  instructionArgumentTypeMap: instructionArgumentTypeMap,
16916
17356
  interpreter: index$7,
16917
17357
  isBrowser: isBrowser,
16918
17358
  measure: measure,
16919
- numbers: index$p,
17359
+ numbers: index$r,
16920
17360
  resultToString: resultToString,
16921
17361
  seeThrough: seeThrough,
16922
17362
  slotsToPreimageStatus: slotsToPreimageStatus,
@@ -17630,32 +18070,33 @@ var index = /*#__PURE__*/Object.freeze({
17630
18070
  WorkPackageExecutor: WorkPackageExecutor
17631
18071
  });
17632
18072
 
17633
- exports.block = index$j;
17634
- exports.block_json = index$h;
17635
- exports.bytes = index$q;
17636
- exports.codec = index$o;
17637
- exports.collections = index$l;
17638
- exports.config = index$k;
17639
- exports.config_node = index$f;
17640
- exports.crypto = index$m;
17641
- exports.database = index$b;
17642
- exports.erasure_coding = index$a;
17643
- exports.hash = index$n;
18073
+ exports.block = index$l;
18074
+ exports.block_json = index$j;
18075
+ exports.bytes = index$s;
18076
+ exports.codec = index$q;
18077
+ exports.collections = index$n;
18078
+ exports.config = index$m;
18079
+ exports.config_node = index$h;
18080
+ exports.crypto = index$o;
18081
+ exports.database = index$d;
18082
+ exports.erasure_coding = index$c;
18083
+ exports.fuzz_proto = index$a;
18084
+ exports.hash = index$p;
17644
18085
  exports.jam_host_calls = index$9;
17645
- exports.json_parser = index$i;
17646
- exports.logger = index$g;
18086
+ exports.json_parser = index$k;
18087
+ exports.logger = index$i;
17647
18088
  exports.mmr = index$8;
17648
- exports.numbers = index$p;
17649
- exports.ordering = index$r;
18089
+ exports.numbers = index$r;
18090
+ exports.ordering = index$t;
17650
18091
  exports.pvm = index$3;
17651
18092
  exports.pvm_host_calls = index$6;
17652
18093
  exports.pvm_interpreter = index$7;
17653
18094
  exports.pvm_program = index$4;
17654
18095
  exports.pvm_spi_decoder = index$5;
17655
18096
  exports.shuffling = index$2;
17656
- exports.state = index$e;
18097
+ exports.state = index$g;
17657
18098
  exports.state_json = index$1;
17658
- exports.state_merkleization = index$c;
18099
+ exports.state_merkleization = index$e;
17659
18100
  exports.transition = index;
17660
- exports.trie = index$d;
17661
- exports.utils = index$s;
18101
+ exports.trie = index$f;
18102
+ exports.utils = index$u;