@typeberry/convert 0.1.2-726ffce6 → 0.1.2-b99f2bee

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 +167 -55
  2. package/index.js.map +1 -1
  3. package/package.json +1 -1
package/index.js CHANGED
@@ -4477,7 +4477,7 @@ function assert (t, m) {
4477
4477
 
4478
4478
  /***/ }),
4479
4479
 
4480
- /***/ 405:
4480
+ /***/ 786:
4481
4481
  /***/ ((module, __unused_webpack_exports, __nccwpck_require__) => {
4482
4482
 
4483
4483
  module.exports = __nccwpck_require__.p + "6b655f8772c01b768329.js?bandersnatch_bg.wasm";
@@ -4605,8 +4605,6 @@ module.exports = __nccwpck_require__.p + "ccf8ada94096a8f232f5.js?reed_solomon_w
4605
4605
  /************************************************************************/
4606
4606
  var __webpack_exports__ = {};
4607
4607
 
4608
- ;// CONCATENATED MODULE: external "node:url"
4609
- const external_node_url_namespaceObject = __WEBPACK_EXTERNAL_createRequire(import.meta.url)("node:url");
4610
4608
  ;// CONCATENATED MODULE: ./packages/core/logger/options.ts
4611
4609
  var Level;
4612
4610
  (function (Level) {
@@ -7599,7 +7597,7 @@ async function __wbg_init$2(module_or_path) {
7599
7597
  if (wasm$2 !== void 0) return wasm$2;
7600
7598
  if (typeof module_or_path !== "undefined") if (Object.getPrototypeOf(module_or_path) === Object.prototype) ({module_or_path} = module_or_path);
7601
7599
  else console.warn("using deprecated parameters for the initialization function; pass a single object instead");
7602
- if (typeof module_or_path === "undefined") module_or_path = new URL(/* asset import */ __nccwpck_require__(405), __nccwpck_require__.b);
7600
+ if (typeof module_or_path === "undefined") module_or_path = new URL(/* asset import */ __nccwpck_require__(786), __nccwpck_require__.b);
7603
7601
  const imports = __wbg_get_imports$2();
7604
7602
  if (typeof module_or_path === "string" || typeof Request === "function" && module_or_path instanceof Request || typeof URL === "function" && module_or_path instanceof URL) module_or_path = fetch(module_or_path);
7605
7603
  __wbg_init_memory$2(imports);
@@ -26811,6 +26809,12 @@ async function runPvmTest(testContent) {
26811
26809
 
26812
26810
 
26813
26811
 
26812
+ function looseType(output) {
26813
+ return {
26814
+ value: output.value,
26815
+ encode: output.encode,
26816
+ };
26817
+ }
26814
26818
  const SUPPORTED_TYPES = [
26815
26819
  {
26816
26820
  name: "block",
@@ -26828,8 +26832,12 @@ const SUPPORTED_TYPES = [
26828
26832
  run(spec, data, option) {
26829
26833
  const header = data;
26830
26834
  if (option === "as-hash") {
26831
- return hashBytes(encoder_Encoder.encodeObject(header_Header.Codec, header, spec));
26835
+ return looseType({
26836
+ value: hashBytes(encoder_Encoder.encodeObject(header_Header.Codec, header, spec)),
26837
+ encode: descriptors_codec.bytes(hash_HASH_SIZE),
26838
+ });
26832
26839
  }
26840
+ throw new Error(`Invalid processing option: ${option}`);
26833
26841
  },
26834
26842
  },
26835
26843
  },
@@ -26873,11 +26881,17 @@ const SUPPORTED_TYPES = [
26873
26881
  run(spec, data, option) {
26874
26882
  const state = data;
26875
26883
  if (option === "as-entries") {
26876
- return Object.fromEntries(state_entries_StateEntries.serializeInMemory(spec, state));
26884
+ return looseType({
26885
+ value: Object.fromEntries(state_entries_StateEntries.serializeInMemory(spec, state)),
26886
+ });
26877
26887
  }
26878
26888
  if (option === "as-root-hash") {
26879
- return state_entries_StateEntries.serializeInMemory(spec, state).getRootHash();
26889
+ return looseType({
26890
+ value: state_entries_StateEntries.serializeInMemory(spec, state).getRootHash(),
26891
+ encode: descriptors_codec.bytes(hash_HASH_SIZE),
26892
+ });
26880
26893
  }
26894
+ throw new Error(`Invalid processing option: ${option}`);
26881
26895
  },
26882
26896
  },
26883
26897
  },
@@ -26887,19 +26901,39 @@ const SUPPORTED_TYPES = [
26887
26901
  decode: StateTransitionGenesis.Codec,
26888
26902
  json: () => StateTransitionGenesis.fromJson,
26889
26903
  process: {
26890
- options: ["as-state", "as-jip4"],
26904
+ options: ["as-state", "as-jip4", "as-fuzz-message"],
26891
26905
  run(spec, data, option) {
26892
26906
  const test = data;
26893
26907
  if (option === "as-state") {
26894
- return stateFromKeyvals(spec, test.state);
26908
+ return looseType({
26909
+ value: stateFromKeyvals(spec, test.state),
26910
+ });
26895
26911
  }
26896
26912
  if (option === "as-jip4") {
26897
26913
  const genesisState = new Map(test.state.keyvals.map((x) => [x.key, x.value]));
26898
- return JipChainSpec.create({
26899
- genesisHeader: encoder_Encoder.encodeObject(header_Header.Codec, test.header, spec),
26900
- genesisState,
26914
+ return looseType({
26915
+ value: JipChainSpec.create({
26916
+ genesisHeader: encoder_Encoder.encodeObject(header_Header.Codec, test.header, spec),
26917
+ genesisState,
26918
+ }),
26919
+ });
26920
+ }
26921
+ if (option === "as-fuzz-message") {
26922
+ const init = Initialize.create({
26923
+ header: test.header,
26924
+ keyvals: test.state.keyvals,
26925
+ ancestry: [],
26926
+ });
26927
+ const msg = {
26928
+ type: types_MessageType.Initialize,
26929
+ value: init,
26930
+ };
26931
+ return looseType({
26932
+ value: msg,
26933
+ encode: types_messageCodec,
26901
26934
  });
26902
26935
  }
26936
+ throw new Error(`Invalid processing option: ${option}`);
26903
26937
  },
26904
26938
  },
26905
26939
  },
@@ -26909,15 +26943,32 @@ const SUPPORTED_TYPES = [
26909
26943
  decode: StateTransition.Codec,
26910
26944
  json: () => StateTransition.fromJson,
26911
26945
  process: {
26912
- options: ["as-pre-state", "as-post-state"],
26946
+ options: ["as-pre-state", "as-post-state", "as-fuzz-message"],
26913
26947
  run(spec, data, option) {
26914
26948
  const test = data;
26915
26949
  if (option === "as-pre-state") {
26916
- return stateFromKeyvals(spec, test.pre_state);
26950
+ return looseType({
26951
+ value: stateFromKeyvals(spec, test.pre_state),
26952
+ });
26917
26953
  }
26918
26954
  if (option === "as-post-state") {
26919
- return stateFromKeyvals(spec, test.post_state);
26955
+ return looseType({
26956
+ value: stateFromKeyvals(spec, test.post_state),
26957
+ });
26920
26958
  }
26959
+ if (option === "as-fuzz-message") {
26960
+ const encoded = encoder_Encoder.encodeObject(block_Block.Codec, test.block, spec);
26961
+ const blockView = decoder_Decoder.decodeObject(block_Block.Codec.View, encoded, spec);
26962
+ const msg = {
26963
+ type: types_MessageType.ImportBlock,
26964
+ value: blockView,
26965
+ };
26966
+ return looseType({
26967
+ value: msg,
26968
+ encode: types_messageCodec,
26969
+ });
26970
+ }
26971
+ throw new Error(`Invalid processing option: ${option}`);
26921
26972
  },
26922
26973
  },
26923
26974
  },
@@ -26940,7 +26991,7 @@ const HELP = `
26940
26991
  @typeberry/convert ${package_namespaceObject.rE} by Fluffy Labs.
26941
26992
 
26942
26993
  Usage:
26943
- @typeberry/convert [options] <bin-hex-or-json-input-file> <type> [process] [output-format]
26994
+ @typeberry/convert [options] <bin-hex-or-json-input-file> <type> [process] [output-format] [output-file]
26944
26995
 
26945
26996
  Attempts to read provided input file as 'type' and output in requested 'output-format'.
26946
26997
  For some 'type's it's additionally possible to process the data before outputting it.
@@ -26949,6 +27000,7 @@ The input type is detected from file extension ('.bin', '.hex' or '.json').
26949
27000
  Example usage:
26950
27001
  @typeberry/convert ./genesis-header.json header to-hex
26951
27002
  @typeberry/convert ./state-snapshot.json state-dump as-entries to-json
27003
+ @typeberry/convert ./state-snapshot.json stf-vector as-fuzz-message to-bin msg0.bin
26952
27004
 
26953
27005
  Options:
26954
27006
  --flavor - chain spec flavor, either 'full' or 'tiny'.
@@ -26958,6 +27010,7 @@ Output formats:
26958
27010
  to-print - Print the object to the console
26959
27011
  to-json - JSON format (when supported)
26960
27012
  to-hex - JAM-codec hex-encoded string (when supported)
27013
+ to-bin - JAM-codec binary data (when supported)
26961
27014
  to-repl - Start a JavaScript REPL with the data loaded into a variable
26962
27015
 
26963
27016
  Input types:
@@ -26980,6 +27033,7 @@ var OutputFormat;
26980
27033
  OutputFormat["Print"] = "to-print";
26981
27034
  OutputFormat["Json"] = "to-json";
26982
27035
  OutputFormat["Hex"] = "to-hex";
27036
+ OutputFormat["Bin"] = "to-bin";
26983
27037
  OutputFormat["Repl"] = "to-repl";
26984
27038
  })(OutputFormat || (OutputFormat = {}));
26985
27039
  function parseArgs(cliInput, withRelPath) {
@@ -27001,14 +27055,16 @@ function parseArgs(cliInput, withRelPath) {
27001
27055
  const type = parseType(args._.shift());
27002
27056
  const maybeProcess = args._.shift();
27003
27057
  const maybeOutputFormat = args._.shift();
27058
+ const maybeDestination = args._.shift();
27004
27059
  assertNoMoreArgs(args);
27005
- const { process, format } = getProcessAndOutput(type, maybeProcess, maybeOutputFormat);
27060
+ const { process, format, destination } = getProcessFormatAndDestination(type, maybeProcess, maybeOutputFormat, maybeDestination);
27006
27061
  return {
27007
27062
  flavor: chainSpec.flavor,
27008
27063
  type,
27009
27064
  process,
27010
27065
  inputPath: withRelPath(input),
27011
27066
  outputFormat: format,
27067
+ destination,
27012
27068
  };
27013
27069
  }
27014
27070
  function parseType(type) {
@@ -27034,10 +27090,21 @@ function parseOutputFormat(output) {
27034
27090
  return OutputFormat.Json;
27035
27091
  case OutputFormat.Repl:
27036
27092
  return OutputFormat.Repl;
27093
+ case OutputFormat.Bin:
27094
+ return OutputFormat.Bin;
27037
27095
  default:
27038
27096
  throw new Error(`Invalid output format: '${output}'.`);
27039
27097
  }
27040
27098
  }
27099
+ function parseProcess(processOptions, maybeProcess) {
27100
+ if (maybeProcess === undefined) {
27101
+ return null;
27102
+ }
27103
+ if (!processOptions.includes(maybeProcess)) {
27104
+ throw new Error(`Incorrect processing option: ${maybeProcess}. Expected one of: ${processOptions}.`);
27105
+ }
27106
+ return maybeProcess;
27107
+ }
27041
27108
  // TODO [ToDr] Consider sharing that?
27042
27109
  function parseOption(args, option, parser, defaultValue) {
27043
27110
  if (args[option] === undefined) {
@@ -27073,33 +27140,64 @@ function assertNoMoreArgs(args) {
27073
27140
  throw new Error(`Unrecognized options: '${keysLeft}'`);
27074
27141
  }
27075
27142
  }
27076
- function getProcessAndOutput(type, maybeProcess, maybeOutputFormat) {
27143
+ function getProcessFormatAndDestination(type, maybeProcess, maybeOutputFormat, maybeDestination) {
27144
+ const defaultProcess = "";
27077
27145
  const defaultFormat = parseOutputFormat(undefined);
27078
- const options = type.process?.options ?? [];
27079
- // we have both options, so we expect them in the right order.
27080
- if (maybeProcess !== undefined && maybeOutputFormat !== undefined) {
27146
+ const processOptions = type.process?.options ?? [];
27147
+ // we have all three so it must be in order
27148
+ if (maybeProcess !== undefined && maybeOutputFormat !== undefined && maybeDestination !== undefined) {
27081
27149
  const format = parseOutputFormat(maybeOutputFormat);
27082
- if (!options.includes(maybeProcess)) {
27083
- throw new Error(`Incorrect processing option: ${maybeProcess}. Expected one of: ${options}.`);
27084
- }
27085
- return { process: maybeProcess, format };
27150
+ const process = parseProcess(processOptions, maybeProcess) ?? defaultProcess;
27151
+ const destination = maybeDestination;
27152
+ throwIfDumpNotSupported(format, destination);
27153
+ return { process, format, destination };
27154
+ }
27155
+ // we have either:
27156
+ // 1. process + format
27157
+ // 2. format + destination
27158
+ if (maybeProcess !== undefined && maybeOutputFormat !== undefined) {
27159
+ // we've got processing first, so easy-peasy
27160
+ if (processOptions.includes(maybeProcess)) {
27161
+ const format = parseOutputFormat(maybeOutputFormat);
27162
+ throwIfDumpNotSupported(format, null);
27163
+ return { process: maybeProcess, format, destination: null };
27164
+ }
27165
+ // first one has to be format then.
27166
+ const format = parseOutputFormat(maybeProcess);
27167
+ const destination = maybeOutputFormat;
27168
+ throwIfDumpNotSupported(format, destination);
27169
+ return { process: defaultProcess, format, destination };
27086
27170
  }
27087
27171
  // only one parameter, but it can be either output or processing.
27172
+ const destination = null;
27088
27173
  if (maybeProcess !== undefined) {
27089
- if (options.includes(maybeProcess)) {
27090
- return { process: maybeProcess, format: defaultFormat };
27174
+ if (processOptions.includes(maybeProcess)) {
27175
+ return { process: maybeProcess, format: defaultFormat, destination };
27091
27176
  }
27092
27177
  // now it should be output format, but we want to give a better error message,
27093
27178
  // if user mispelled processing.
27094
27179
  try {
27095
27180
  const format = parseOutputFormat(maybeProcess);
27096
- return { process: "", format };
27181
+ throwIfDumpNotSupported(format, destination);
27182
+ return { process: defaultProcess, format, destination };
27097
27183
  }
27098
27184
  catch {
27099
27185
  throw new Error(`'${maybeProcess}' is neither output format nor processing parameter.`);
27100
27186
  }
27101
27187
  }
27102
- return { process: "", format: defaultFormat };
27188
+ return { process: defaultProcess, format: defaultFormat, destination };
27189
+ }
27190
+ function throwIfDumpNotSupported(format, destination) {
27191
+ if (destination !== null) {
27192
+ if (format === OutputFormat.Print || format === OutputFormat.Repl) {
27193
+ throw new Error(`Dumping to file is not supported for ${format}`);
27194
+ }
27195
+ }
27196
+ else {
27197
+ if (format === OutputFormat.Bin) {
27198
+ throw new Error(`${format} requires destination file`);
27199
+ }
27200
+ }
27103
27201
  }
27104
27202
 
27105
27203
  // EXTERNAL MODULE: ./node_modules/json-bigint-patch/dist/index.js
@@ -27159,9 +27257,13 @@ function loadInputFile(file, withRelPath) {
27159
27257
  throw new Error("Input file format unsupported.");
27160
27258
  }
27161
27259
  function dumpOutput(spec, data, type, outputFormat, args, withRelPath) {
27260
+ const { destination } = args;
27261
+ const dump = destination !== null
27262
+ ? (v) => external_node_fs_default().writeFileSync(withRelPath(destination), v)
27263
+ : (v) => console.info(v);
27162
27264
  switch (outputFormat) {
27163
27265
  case OutputFormat.Print: {
27164
- console.info(`${debug_inspect(data)}`);
27266
+ dump(`${debug_inspect(data)}`);
27165
27267
  return;
27166
27268
  }
27167
27269
  case OutputFormat.Hex: {
@@ -27169,13 +27271,24 @@ function dumpOutput(spec, data, type, outputFormat, args, withRelPath) {
27169
27271
  throw new Error(`${type.name} does not support encoding to JAM codec.`);
27170
27272
  }
27171
27273
  const encoded = encoder_Encoder.encodeObject(type.encode, data, spec);
27172
- console.info(`${encoded}`);
27274
+ dump(`${encoded}`);
27275
+ return;
27276
+ }
27277
+ case OutputFormat.Bin: {
27278
+ if (type.encode === undefined) {
27279
+ throw new Error(`${type.name} does not support encoding to JAM codec.`);
27280
+ }
27281
+ if (destination === null) {
27282
+ throw new Error(`${OutputFormat.Bin} requires destination file.`);
27283
+ }
27284
+ const encoded = encoder_Encoder.encodeObject(type.encode, data, spec);
27285
+ dump(encoded.raw);
27173
27286
  return;
27174
27287
  }
27175
27288
  case OutputFormat.Json: {
27176
27289
  // TODO [ToDr] this will probably not work for all cases,
27177
27290
  // but for now may be good enough.
27178
- console.info(toJson(data));
27291
+ dump(toJson(data));
27179
27292
  return;
27180
27293
  }
27181
27294
  case OutputFormat.Repl: {
@@ -27252,12 +27365,14 @@ function processOutput(spec, data, type, process) {
27252
27365
  if (type.process === undefined || !type.process.options.includes(process)) {
27253
27366
  throw new Error(`Unsupported processing: '${process}' for '${type.name}'`);
27254
27367
  }
27368
+ const processed = type.process.run(spec, data, process);
27255
27369
  return {
27256
- processed: type.process.run(spec, data, process),
27370
+ processed: processed.value,
27257
27371
  type: {
27258
27372
  ...type,
27259
- // disable encoding, since it won't match
27260
- encode: undefined,
27373
+ name: `${type.name}(${process})`,
27374
+ // use encoding from processed type
27375
+ encode: processed.encode,
27261
27376
  },
27262
27377
  };
27263
27378
  }
@@ -27294,26 +27409,23 @@ function loadAndProcessDataFile(file, withRelPath, flavor, decodeType, process)
27294
27409
 
27295
27410
 
27296
27411
 
27297
-
27298
- if (import.meta.url === (0,external_node_url_namespaceObject.pathToFileURL)(process.argv[1]).href) {
27299
- Logger.configureAll(process.env.JAM_LOG ?? "", Level.LOG);
27300
- const withRelPath = workspacePathFix(`${import.meta.dirname}/../..`);
27301
- let args;
27302
- try {
27303
- args = parseArgs(process.argv.slice(2), withRelPath);
27304
- }
27305
- catch (e) {
27306
- console.error(`\n${e}\n`);
27307
- console.info(HELP);
27308
- process.exit(1);
27309
- }
27310
- try {
27311
- main(args, withRelPath);
27312
- }
27313
- catch (e) {
27314
- console.error(`${e}`);
27315
- process.exit(-1);
27316
- }
27412
+ Logger.configureAll(process.env.JAM_LOG ?? "", Level.LOG);
27413
+ const withRelPath = workspacePathFix(`${import.meta.dirname}/../..`);
27414
+ let args;
27415
+ try {
27416
+ args = parseArgs(process.argv.slice(2), withRelPath);
27417
+ }
27418
+ catch (e) {
27419
+ console.error(`\n${e}\n`);
27420
+ console.info(HELP);
27421
+ process.exit(1);
27422
+ }
27423
+ try {
27424
+ main(args, withRelPath);
27425
+ }
27426
+ catch (e) {
27427
+ console.error(`${e}`);
27428
+ process.exit(-1);
27317
27429
  }
27318
27430
 
27319
27431