@typeberry/lib 0.1.1 → 0.1.2-3178190

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 +152 -102
  2. package/index.d.ts +92 -56
  3. package/index.js +152 -102
  4. package/package.json +1 -1
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,29 +18,35 @@ 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 env = typeof process === "undefined" ? {} : process.env;
21
+ const ALL_VERSIONS_IN_ORDER = [GpVersion.V0_6_7, GpVersion.V0_7_0, GpVersion.V0_7_1, GpVersion.V0_7_2];
22
+ const env$1 = typeof process === "undefined" ? {} : process.env;
22
23
  const DEFAULT_VERSION = GpVersion.V0_7_0;
23
- let CURRENT_VERSION = parseCurrentVersion(env.GP_VERSION) ?? DEFAULT_VERSION;
24
- let CURRENT_SUITE = parseCurrentSuite(env.TEST_SUITE) ?? DEFAULT_SUITE;
24
+ let CURRENT_VERSION = parseCurrentVersion(env$1.GP_VERSION) ?? DEFAULT_VERSION;
25
+ let CURRENT_SUITE = parseCurrentSuite(env$1.TEST_SUITE) ?? DEFAULT_SUITE;
25
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) {
@@ -200,6 +207,33 @@ class WithDebug {
200
207
  }
201
208
  }
202
209
 
210
+ const env = typeof process === "undefined" ? {} : process.env;
211
+ /**
212
+ * The function will produce relative path resolver that is adjusted
213
+ * for package location within the workspace.
214
+ *
215
+ * Example:
216
+ * $ npm start -w @typeberry/jam
217
+ *
218
+ * The above command will run `./bin/jam/index.js`, however we would
219
+ * still want relative paths to be resolved according to top-level workspace
220
+ * directory.
221
+ *
222
+ * So the caller, passes the absolute workspace path as argument and get's
223
+ * a function that can properly resolve relative paths.
224
+ *
225
+ * NOTE: the translation happens only for development build! When
226
+ * we build a single library from our project, we no longer mangle the paths.
227
+ */
228
+ const workspacePathFix = env.NODE_ENV === "development"
229
+ ? (workspacePath) => (p) => {
230
+ if (p.startsWith("/")) {
231
+ return p;
232
+ }
233
+ return `${workspacePath}/${p}`;
234
+ }
235
+ : () => (p) => p;
236
+
203
237
  /**
204
238
  * @fileoverview `Opaque<Type, Token>` constructs a unique type which is a subset of Type with a
205
239
  * specified unique token Token. It means that base type cannot be assigned to unique type by accident.
@@ -552,7 +586,8 @@ var index$s = /*#__PURE__*/Object.freeze({
552
586
  isBrowser: isBrowser,
553
587
  measure: measure,
554
588
  resultToString: resultToString,
555
- seeThrough: seeThrough
589
+ seeThrough: seeThrough,
590
+ workspacePathFix: workspacePathFix
556
591
  });
557
592
 
558
593
  /**
@@ -8012,6 +8047,23 @@ function parseLevel(lvl) {
8012
8047
  }
8013
8048
 
8014
8049
  // biome-ignore-all lint/suspicious/noConsole: logger
8050
+ function print(level, levelAndName, strings, data) {
8051
+ if (level < levelAndName[0]) {
8052
+ return;
8053
+ }
8054
+ const lvlText = Level[level].padEnd(5);
8055
+ const val = strings.map((v, idx) => `${v}${idx < data.length ? data[idx] : ""}`);
8056
+ const msg = `${lvlText} [${levelAndName[1]}] ${val.join("")}`;
8057
+ if (level === Level.WARN) {
8058
+ console.warn(msg);
8059
+ }
8060
+ else if (level === Level.ERROR) {
8061
+ console.error(msg);
8062
+ }
8063
+ else {
8064
+ console.info(msg);
8065
+ }
8066
+ }
8015
8067
  /** An optimized logger that ignores `TRACE`, `DEBUG` and `LOG` messages.
8016
8068
  *
8017
8069
  * Use the `create` method to instantiate the right instance of a more specialized logger.
@@ -8042,109 +8094,91 @@ class ConsoleTransport {
8042
8094
  constructor(options) {
8043
8095
  this.options = options;
8044
8096
  }
8045
- insane(_moduleName, _val) {
8097
+ insane(_levelAndName, _strings, _data) {
8046
8098
  /* no-op */
8047
8099
  }
8048
- trace(_moduleName, _val) {
8100
+ trace(_levelAndName, _strings, _data) {
8049
8101
  /* no-op */
8050
8102
  }
8051
- log(_moduleName, _val) {
8103
+ log(_levelAndName, _strings, _data) {
8052
8104
  /* no-op */
8053
8105
  }
8054
- info(_moduleName, _val) {
8106
+ info(_levelAndName, _strings, _data) {
8055
8107
  /* no-op */
8056
8108
  }
8057
- warn(moduleName, val) {
8058
- this.push(Level.WARN, moduleName, val);
8109
+ warn(levelAndName, strings, data) {
8110
+ print(Level.WARN, levelAndName, strings, data);
8059
8111
  }
8060
- error(moduleName, val) {
8061
- this.push(Level.ERROR, moduleName, val);
8062
- }
8063
- push(level, moduleName, val) {
8064
- const shortModule = moduleName.replace(this.options.workingDir, "");
8065
- const configuredLevel = findLevel(this.options, moduleName);
8066
- const lvlText = Level[level].padEnd(5);
8067
- if (level < configuredLevel) {
8068
- return;
8069
- }
8070
- const msg = `${lvlText} [${shortModule}] ${val}`;
8071
- if (level === Level.WARN) {
8072
- console.warn(msg);
8073
- }
8074
- else if (level === Level.ERROR) {
8075
- console.error(msg);
8076
- }
8077
- else {
8078
- console.info(msg);
8079
- }
8112
+ error(levelAndName, strings, data) {
8113
+ print(Level.ERROR, levelAndName, strings, data);
8080
8114
  }
8081
8115
  }
8082
8116
  /**
8083
8117
  * Insane version of console logger - supports insane level.
8084
8118
  */
8085
8119
  class InsaneConsoleLogger extends ConsoleTransport {
8086
- insane(moduleName, val) {
8087
- this.push(Level.INSANE, moduleName, val);
8120
+ insane(levelAndName, strings, data) {
8121
+ print(Level.INSANE, levelAndName, strings, data);
8088
8122
  }
8089
- trace(moduleName, val) {
8090
- this.push(Level.TRACE, moduleName, val);
8123
+ trace(levelAndName, strings, data) {
8124
+ print(Level.TRACE, levelAndName, strings, data);
8091
8125
  }
8092
- log(moduleName, val) {
8093
- this.push(Level.LOG, moduleName, val);
8126
+ log(levelAndName, strings, data) {
8127
+ print(Level.LOG, levelAndName, strings, data);
8094
8128
  }
8095
- info(moduleName, val) {
8096
- this.push(Level.INFO, moduleName, val);
8129
+ info(levelAndName, strings, data) {
8130
+ print(Level.INFO, levelAndName, strings, data);
8097
8131
  }
8098
8132
  }
8099
8133
  /**
8100
8134
  * A basic version of console logger - printing everything.
8101
8135
  */
8102
8136
  class TraceConsoleTransport extends ConsoleTransport {
8103
- insane(_moduleName, _val) {
8137
+ insane(_levelAndName, _strings, _data) {
8104
8138
  /* no-op */
8105
8139
  }
8106
- trace(moduleName, val) {
8107
- this.push(Level.TRACE, moduleName, val);
8140
+ trace(levelAndName, strings, data) {
8141
+ print(Level.TRACE, levelAndName, strings, data);
8108
8142
  }
8109
- log(moduleName, val) {
8110
- this.push(Level.LOG, moduleName, val);
8143
+ log(levelAndName, strings, data) {
8144
+ print(Level.LOG, levelAndName, strings, data);
8111
8145
  }
8112
- info(moduleName, val) {
8113
- this.push(Level.INFO, moduleName, val);
8146
+ info(levelAndName, strings, data) {
8147
+ print(Level.INFO, levelAndName, strings, data);
8114
8148
  }
8115
8149
  }
8116
8150
  /**
8117
8151
  * An optimized version of the logger - completely ignores `TRACE` level calls.
8118
8152
  */
8119
8153
  class LogConsoleTransport extends ConsoleTransport {
8120
- insane(_moduleName, _val) {
8154
+ insane(_levelAndName, _strings, _data) {
8121
8155
  /* no-op */
8122
8156
  }
8123
- trace(_moduleName, _val) {
8157
+ trace(_levelAndName, _strings, _data) {
8124
8158
  /* no-op */
8125
8159
  }
8126
- log(moduleName, val) {
8127
- this.push(Level.LOG, moduleName, val);
8160
+ log(levelAndName, strings, data) {
8161
+ print(Level.LOG, levelAndName, strings, data);
8128
8162
  }
8129
- info(moduleName, val) {
8130
- this.push(Level.INFO, moduleName, val);
8163
+ info(levelAndName, strings, data) {
8164
+ print(Level.INFO, levelAndName, strings, data);
8131
8165
  }
8132
8166
  }
8133
8167
  /**
8134
8168
  * An optimized version of the logger - completely ignores `TRACE` & `DEBUG` level calls.
8135
8169
  */
8136
8170
  class InfoConsoleTransport extends ConsoleTransport {
8137
- insane(_moduleName, _val) {
8171
+ insane(_levelAndName, _strings, _data) {
8138
8172
  /* no-op */
8139
8173
  }
8140
- trace(_moduleName, _val) {
8174
+ trace(_levelAndName, _strings, _data) {
8141
8175
  /* no-op */
8142
8176
  }
8143
- log(_moduleName, _val) {
8177
+ log(_levelAndName, _strings, _data) {
8144
8178
  /* no-op */
8145
8179
  }
8146
- info(moduleName, val) {
8147
- this.push(Level.INFO, moduleName, val);
8180
+ info(levelAndName, strings, data) {
8181
+ print(Level.INFO, levelAndName, strings, data);
8148
8182
  }
8149
8183
  }
8150
8184
 
@@ -8177,11 +8211,6 @@ class Logger {
8177
8211
  const module = moduleName ?? fName;
8178
8212
  return new Logger(module.padStart(8, " "), GLOBAL_CONFIG);
8179
8213
  }
8180
- /**
8181
- * Return currently configured level for given module. */
8182
- static getLevel(moduleName) {
8183
- return findLevel(GLOBAL_CONFIG.options, moduleName);
8184
- }
8185
8214
  /**
8186
8215
  * Global configuration of all loggers.
8187
8216
  *
@@ -8212,33 +8241,46 @@ class Logger {
8212
8241
  const options = parseLoggerOptions(input, defaultLevel, workingDir);
8213
8242
  Logger.configureAllFromOptions(options);
8214
8243
  }
8244
+ cachedLevelAndName;
8215
8245
  constructor(moduleName, config) {
8216
8246
  this.moduleName = moduleName;
8217
8247
  this.config = config;
8218
8248
  }
8249
+ /** Return currently configured level for given module. */
8250
+ getLevel() {
8251
+ return this.getLevelAndName()[0];
8252
+ }
8253
+ getLevelAndName() {
8254
+ if (this.cachedLevelAndName === undefined) {
8255
+ const level = findLevel(this.config.options, this.moduleName);
8256
+ const shortName = this.moduleName.replace(this.config.options.workingDir, "");
8257
+ this.cachedLevelAndName = [level, shortName];
8258
+ }
8259
+ return this.cachedLevelAndName;
8260
+ }
8219
8261
  /** Log a message with `INSANE` level. */
8220
- insane(val) {
8221
- this.config.transport.insane(this.moduleName, val);
8262
+ insane(strings, ...data) {
8263
+ this.config.transport.insane(this.getLevelAndName(), strings, data);
8222
8264
  }
8223
8265
  /** Log a message with `TRACE` level. */
8224
- trace(val) {
8225
- this.config.transport.trace(this.moduleName, val);
8266
+ trace(strings, ...data) {
8267
+ this.config.transport.trace(this.getLevelAndName(), strings, data);
8226
8268
  }
8227
8269
  /** Log a message with `DEBUG`/`LOG` level. */
8228
- log(val) {
8229
- this.config.transport.log(this.moduleName, val);
8270
+ log(strings, ...data) {
8271
+ this.config.transport.log(this.getLevelAndName(), strings, data);
8230
8272
  }
8231
8273
  /** Log a message with `INFO` level. */
8232
- info(val) {
8233
- this.config.transport.info(this.moduleName, val);
8274
+ info(strings, ...data) {
8275
+ this.config.transport.info(this.getLevelAndName(), strings, data);
8234
8276
  }
8235
8277
  /** Log a message with `WARN` level. */
8236
- warn(val) {
8237
- this.config.transport.warn(this.moduleName, val);
8278
+ warn(strings, ...data) {
8279
+ this.config.transport.warn(this.getLevelAndName(), strings, data);
8238
8280
  }
8239
8281
  /** Log a message with `ERROR` level. */
8240
- error(val) {
8241
- this.config.transport.error(this.moduleName, val);
8282
+ error(strings, ...data) {
8283
+ this.config.transport.error(this.getLevelAndName(), strings, data);
8242
8284
  }
8243
8285
  }
8244
8286
 
@@ -8265,7 +8307,7 @@ class AuthorshipOptions {
8265
8307
  }
8266
8308
  }
8267
8309
 
8268
- const logger$3 = Logger.new(undefined, "config");
8310
+ const logger$4 = Logger.new(undefined, "config");
8269
8311
  /** Development config. Will accept unsealed blocks for now. */
8270
8312
  const DEV_CONFIG = "dev";
8271
8313
  /** Default config file. */
@@ -8324,15 +8366,15 @@ class NodeConfiguration {
8324
8366
  }
8325
8367
  function loadConfig(configPath) {
8326
8368
  if (configPath === DEFAULT_CONFIG) {
8327
- logger$3.log("🔧 Loading DEFAULT config");
8369
+ logger$4.log `🔧 Loading DEFAULT config`;
8328
8370
  return parseFromJson(configs.default, NodeConfiguration.fromJson);
8329
8371
  }
8330
8372
  if (configPath === DEV_CONFIG) {
8331
- logger$3.log("🔧 Loading DEV config");
8373
+ logger$4.log `🔧 Loading DEV config`;
8332
8374
  return parseFromJson(configs.dev, NodeConfiguration.fromJson);
8333
8375
  }
8334
8376
  try {
8335
- logger$3.log(`🔧 Loading config from ${configPath}`);
8377
+ logger$4.log `🔧 Loading config from ${configPath}`;
8336
8378
  const configFile = fs.readFileSync(configPath, "utf8");
8337
8379
  const parsed = JSON.parse(configFile);
8338
8380
  return parseFromJson(parsed, NodeConfiguration.fromJson);
@@ -12362,16 +12404,12 @@ class PartiallyUpdatedState {
12362
12404
  *
12363
12405
  * NOTE the info may be updated compared to what is in the state.
12364
12406
  *
12365
- * Takes into account newly created services as well.
12407
+ * Takes into account ejected and newly created services as well.
12366
12408
  */
12367
12409
  getServiceInfo(destination) {
12368
12410
  if (destination === null) {
12369
12411
  return null;
12370
12412
  }
12371
- const isEjected = this.stateUpdate.services.servicesRemoved.some((x) => x === destination);
12372
- if (isEjected) {
12373
- return null;
12374
- }
12375
12413
  const maybeNewService = this.stateUpdate.services.servicesUpdates.find((update) => update.serviceId === destination);
12376
12414
  if (maybeNewService !== undefined) {
12377
12415
  return maybeNewService.action.account;
@@ -14087,6 +14125,17 @@ class PageRange {
14087
14125
  }
14088
14126
  return new PageRange(start, length);
14089
14127
  }
14128
+ /** Returns true if the page range is wrapped (`start` >= `end`) and is not empty */
14129
+ isWrapped() {
14130
+ return this.start >= this.end && !this.isEmpty();
14131
+ }
14132
+ /** Checks if given page number is within the range */
14133
+ isInRange(page) {
14134
+ if (this.isWrapped()) {
14135
+ return page >= this.start || page < this.end;
14136
+ }
14137
+ return page >= this.start && page < this.end;
14138
+ }
14090
14139
  /** Checks if a range is empty (`length === 0`) */
14091
14140
  isEmpty() {
14092
14141
  return this.length === 0;
@@ -14194,7 +14243,7 @@ var AccessType;
14194
14243
  AccessType[AccessType["READ"] = 0] = "READ";
14195
14244
  AccessType[AccessType["WRITE"] = 1] = "WRITE";
14196
14245
  })(AccessType || (AccessType = {}));
14197
- // const logger = Logger.new(import.meta.filename, "pvm:mem");
14246
+ const logger$3 = Logger.new(undefined, "pvm:mem");
14198
14247
  class Memory {
14199
14248
  sbrkIndex;
14200
14249
  virtualSbrkIndex;
@@ -14225,7 +14274,7 @@ class Memory {
14225
14274
  if (bytes.length === 0) {
14226
14275
  return Result$1.ok(OK);
14227
14276
  }
14228
- // logger.insane(`MEM[${address}] <- ${BytesBlob.blobFrom(bytes)}`);
14277
+ logger$3.insane `MEM[${address}] <- ${BytesBlob.blobFrom(bytes)}`;
14229
14278
  const pagesResult = this.getPages(address, bytes.length, AccessType.WRITE);
14230
14279
  if (pagesResult.isError) {
14231
14280
  return Result$1.error(pagesResult.error);
@@ -14292,7 +14341,7 @@ class Memory {
14292
14341
  currentPosition += bytesToRead;
14293
14342
  bytesLeft -= bytesToRead;
14294
14343
  }
14295
- // logger.insane(`MEM[${startAddress}] => ${BytesBlob.blobFrom(result)}`);
14344
+ logger$3.insane `MEM[${startAddress}] => ${BytesBlob.blobFrom(result)}`;
14296
14345
  return Result$1.ok(OK);
14297
14346
  }
14298
14347
  sbrk(length) {
@@ -14427,10 +14476,11 @@ class MemoryBuilder {
14427
14476
  startHeapIndex (${startHeapIndex}) has to be less than or equal to endHeapIndex (${endHeapIndex})
14428
14477
  `;
14429
14478
  this.ensureNotFinalized();
14430
- const range = MemoryRange.fromStartAndLength(startHeapIndex, endHeapIndex - startHeapIndex);
14431
- const pages = PageRange.fromMemoryRange(range);
14432
- for (const pageNumber of pages) {
14433
- if (this.initialMemory.has(pageNumber)) {
14479
+ const heapRange = MemoryRange.fromStartAndLength(startHeapIndex, endHeapIndex - startHeapIndex);
14480
+ const heapPagesRange = PageRange.fromMemoryRange(heapRange);
14481
+ const initializedPageNumbers = Array.from(this.initialMemory.keys());
14482
+ for (const pageNumber of initializedPageNumbers) {
14483
+ if (heapPagesRange.isInRange(pageNumber)) {
14434
14484
  throw new IncorrectSbrkIndex();
14435
14485
  }
14436
14486
  }
@@ -16086,7 +16136,7 @@ class ProgramDecoder {
16086
16136
  return Result$1.ok(new ProgramDecoder(program));
16087
16137
  }
16088
16138
  catch (e) {
16089
- logger$2.error(`Invalid program: ${e}`);
16139
+ logger$2.error `Invalid program: ${e}`;
16090
16140
  return Result$1.error(ProgramDecoderError.InvalidProgramError);
16091
16141
  }
16092
16142
  }
@@ -16211,7 +16261,7 @@ class Interpreter {
16211
16261
  const argsType = instructionArgumentTypeMap[currentInstruction] ?? ArgumentType.NO_ARGUMENTS;
16212
16262
  const argsResult = this.argsDecodingResults[argsType];
16213
16263
  this.argsDecoder.fillArgs(this.pc, argsResult);
16214
- logger$1.insane(`[PC: ${this.pc}] ${Instruction[currentInstruction]}`);
16264
+ logger$1.insane `[PC: ${this.pc}] ${Instruction[currentInstruction]}`;
16215
16265
  if (!isValidInstruction) {
16216
16266
  this.instructionResult.status = Result.PANIC;
16217
16267
  }
@@ -16283,7 +16333,7 @@ class Interpreter {
16283
16333
  this.status = Status.HOST;
16284
16334
  break;
16285
16335
  }
16286
- logger$1.insane(`[PC: ${this.pc}] Status: ${Result[this.instructionResult.status]}`);
16336
+ logger$1.insane `[PC: ${this.pc}] Status: ${Result[this.instructionResult.status]}`;
16287
16337
  return this.status;
16288
16338
  }
16289
16339
  this.pc = this.instructionResult.nextPc;
@@ -16517,7 +16567,7 @@ class HostCallsManager {
16517
16567
  return `r${idx}=${value} (0x${value.toString(16)})`;
16518
16568
  })
16519
16569
  .join(", ");
16520
- logger.insane(`[${currentServiceId}] ${context} ${name}${requested}. Gas: ${gas}. Regs: ${registerValues}.`);
16570
+ logger.insane `[${currentServiceId}] ${context} ${name}${requested}. Gas: ${gas}. Regs: ${registerValues}.`;
16521
16571
  }
16522
16572
  }
16523
16573
 
package/index.d.ts CHANGED
@@ -2,6 +2,7 @@ declare enum GpVersion {
2
2
  V0_6_7 = "0.6.7",
3
3
  V0_7_0 = "0.7.0",
4
4
  V0_7_1 = "0.7.1-preview",
5
+ V0_7_2 = "0.7.2-preview",
5
6
  }
6
7
 
7
8
  declare enum TestSuite {
@@ -11,9 +12,7 @@ declare enum TestSuite {
11
12
 
12
13
  declare const DEFAULT_SUITE = TestSuite.W3F_DAVXY;
13
14
 
14
- declare const ALL_VERSIONS_IN_ORDER = [GpVersion.V0_6_7, GpVersion.V0_7_0, GpVersion.V0_7_1];
15
-
16
- declare const env = typeof process === "undefined" ? {} : process.env;
15
+ declare const ALL_VERSIONS_IN_ORDER = [GpVersion.V0_6_7, GpVersion.V0_7_0, GpVersion.V0_7_1, GpVersion.V0_7_2];
17
16
  declare const DEFAULT_VERSION = GpVersion.V0_7_0;
18
17
  declare let CURRENT_VERSION = parseCurrentVersion(env.GP_VERSION) ?? DEFAULT_VERSION;
19
18
  declare let CURRENT_SUITE = parseCurrentSuite(env.TEST_SUITE) ?? DEFAULT_SUITE;
@@ -22,24 +21,30 @@ declare function parseCurrentVersion(env?: string): GpVersion | undefined {
22
21
  if (env === undefined) {
23
22
  return undefined;
24
23
  }
25
- const version = env as GpVersion;
26
- if (!Object.values(GpVersion).includes(version)) {
27
- throw new Error(
28
- `Configured environment variable GP_VERSION is unknown: '${env}'. Use one of: ${ALL_VERSIONS_IN_ORDER}`,
29
- );
24
+ switch (env) {
25
+ case GpVersion.V0_6_7:
26
+ case GpVersion.V0_7_0:
27
+ case GpVersion.V0_7_1:
28
+ case GpVersion.V0_7_2:
29
+ return env;
30
+ default:
31
+ throw new Error(
32
+ `Configured environment variable GP_VERSION is unknown: '${env}'. Use one of: ${ALL_VERSIONS_IN_ORDER}`,
33
+ );
30
34
  }
31
- return version;
32
35
  }
33
36
 
34
37
  declare function parseCurrentSuite(env?: string): TestSuite | undefined {
35
38
  if (env === undefined) return undefined;
36
- const val = env as TestSuite;
37
- if (!Object.values(TestSuite).includes(val)) {
38
- throw new Error(
39
- `Configured environment variable TEST_SUITE is unknown: '${env}'. Use one of: ${Object.values(TestSuite)}`,
40
- );
39
+ switch (env) {
40
+ case TestSuite.W3F_DAVXY:
41
+ case TestSuite.JAMDUNA:
42
+ return env;
43
+ default:
44
+ throw new Error(
45
+ `Configured environment variable TEST_SUITE is unknown: '${env}'. Use one of: ${Object.values(TestSuite)}`,
46
+ );
41
47
  }
42
- return val;
43
48
  }
44
49
 
45
50
  declare class Compatibility {
@@ -235,6 +240,33 @@ declare abstract class WithDebug {
235
240
  }
236
241
  }
237
242
 
243
+ /**
244
+ * The function will produce relative path resolver that is adjusted
245
+ * for package location within the workspace.
246
+ *
247
+ * Example:
248
+ * $ npm start -w @typeberry/jam
249
+ *
250
+ * The above command will run `./bin/jam/index.js`, however we would
251
+ * still want relative paths to be resolved according to top-level workspace
252
+ * directory.
253
+ *
254
+ * So the caller, passes the absolute workspace path as argument and get's
255
+ * a function that can properly resolve relative paths.
256
+ *
257
+ * NOTE: the translation happens only for development build! When
258
+ * we build a single library from our project, we no longer mangle the paths.
259
+ */
260
+ declare const workspacePathFix =
261
+ env.NODE_ENV === "development"
262
+ ? (workspacePath: string) => (p: string) => {
263
+ if (p.startsWith("/")) {
264
+ return p;
265
+ }
266
+ return `${workspacePath}/${p}`;
267
+ }
268
+ : () => (p: string) => p;
269
+
238
270
  /**
239
271
  * @fileoverview `Opaque<Type, Token>` constructs a unique type which is a subset of Type with a
240
272
  * specified unique token Token. It means that base type cannot be assigned to unique type by accident.
@@ -712,7 +744,6 @@ declare const index$s_assertNever: typeof assertNever;
712
744
  declare const index$s_callCompareFunction: typeof callCompareFunction;
713
745
  declare const index$s_check: typeof check;
714
746
  declare const index$s_deepEqual: typeof deepEqual;
715
- declare const index$s_env: typeof env;
716
747
  declare const index$s_getAllKeysSorted: typeof getAllKeysSorted;
717
748
  declare const index$s_inspect: typeof inspect;
718
749
  declare const index$s_isBrowser: typeof isBrowser;
@@ -726,8 +757,9 @@ declare const index$s_parseCurrentVersion: typeof parseCurrentVersion;
726
757
  declare const index$s_resultToString: typeof resultToString;
727
758
  declare const index$s_seeThrough: typeof seeThrough;
728
759
  declare const index$s_trimStack: typeof trimStack;
760
+ declare const index$s_workspacePathFix: typeof workspacePathFix;
729
761
  declare namespace index$s {
730
- export { index$s_ALL_VERSIONS_IN_ORDER as ALL_VERSIONS_IN_ORDER, index$s_CURRENT_SUITE as CURRENT_SUITE, index$s_CURRENT_VERSION as CURRENT_VERSION, index$s_Compatibility as Compatibility, index$s_DEFAULT_SUITE as DEFAULT_SUITE, index$s_DEFAULT_VERSION as DEFAULT_VERSION, index$s_ErrorsCollector as ErrorsCollector, index$s_GpVersion as GpVersion, Result$2 as Result, index$s_RichTaggedError as RichTaggedError, index$s_TEST_COMPARE_USING as TEST_COMPARE_USING, index$s_TestSuite as TestSuite, index$s_WithDebug as WithDebug, index$s___OPAQUE_TYPE__ as __OPAQUE_TYPE__, index$s_asOpaqueType as asOpaqueType, index$s_assertEmpty as assertEmpty, index$s_assertNever as assertNever, index$s_callCompareFunction as callCompareFunction, index$s_check as check, index$s_deepEqual as deepEqual, index$s_env as env, index$s_getAllKeysSorted as getAllKeysSorted, index$s_inspect as inspect, index$s_isBrowser as isBrowser, index$s_isResult as isResult, index$s_isTaggedError as isTaggedError, index$s_maybeTaggedErrorToString as maybeTaggedErrorToString, index$s_measure as measure, index$s_oomWarningPrinted as oomWarningPrinted, index$s_parseCurrentSuite as parseCurrentSuite, index$s_parseCurrentVersion as parseCurrentVersion, index$s_resultToString as resultToString, index$s_seeThrough as seeThrough, index$s_trimStack as trimStack };
762
+ export { index$s_ALL_VERSIONS_IN_ORDER as ALL_VERSIONS_IN_ORDER, index$s_CURRENT_SUITE as CURRENT_SUITE, index$s_CURRENT_VERSION as CURRENT_VERSION, index$s_Compatibility as Compatibility, index$s_DEFAULT_SUITE as DEFAULT_SUITE, index$s_DEFAULT_VERSION as DEFAULT_VERSION, index$s_ErrorsCollector as ErrorsCollector, index$s_GpVersion as GpVersion, Result$2 as Result, index$s_RichTaggedError as RichTaggedError, index$s_TEST_COMPARE_USING as TEST_COMPARE_USING, index$s_TestSuite as TestSuite, index$s_WithDebug as WithDebug, index$s___OPAQUE_TYPE__ as __OPAQUE_TYPE__, index$s_asOpaqueType as asOpaqueType, index$s_assertEmpty as assertEmpty, index$s_assertNever as assertNever, index$s_callCompareFunction as callCompareFunction, index$s_check as check, index$s_deepEqual as deepEqual, index$s_getAllKeysSorted as getAllKeysSorted, index$s_inspect as inspect, index$s_isBrowser as isBrowser, index$s_isResult as isResult, index$s_isTaggedError as isTaggedError, index$s_maybeTaggedErrorToString as maybeTaggedErrorToString, index$s_measure as measure, index$s_oomWarningPrinted as oomWarningPrinted, index$s_parseCurrentSuite as parseCurrentSuite, index$s_parseCurrentVersion as parseCurrentVersion, index$s_resultToString as resultToString, index$s_seeThrough as seeThrough, index$s_trimStack as trimStack, index$s_workspacePathFix as workspacePathFix };
731
763
  export type { index$s_DeepEqualOptions as DeepEqualOptions, index$s_EnumMapping as EnumMapping, index$s_ErrorResult as ErrorResult, index$s_OK as OK, index$s_OkResult as OkResult, index$s_Opaque as Opaque, index$s_StringLiteral as StringLiteral, index$s_TaggedError as TaggedError, index$s_TokenOf as TokenOf, index$s_Uninstantiable as Uninstantiable, index$s_WithOpaque as WithOpaque };
732
764
  }
733
765
 
@@ -7928,12 +7960,6 @@ declare class Logger {
7928
7960
  return new Logger(module.padStart(8, " "), GLOBAL_CONFIG);
7929
7961
  }
7930
7962
 
7931
- /**
7932
- * Return currently configured level for given module. */
7933
- static getLevel(moduleName: string): Level {
7934
- return findLevel(GLOBAL_CONFIG.options, moduleName);
7935
- }
7936
-
7937
7963
  /**
7938
7964
  * Global configuration of all loggers.
7939
7965
  *
@@ -7968,39 +7994,55 @@ declare class Logger {
7968
7994
  Logger.configureAllFromOptions(options);
7969
7995
  }
7970
7996
 
7997
+ private cachedLevelAndName?: readonly [Level, string];
7998
+
7971
7999
  private constructor(
7972
8000
  private readonly moduleName: string,
7973
8001
  private readonly config: typeof GLOBAL_CONFIG,
7974
8002
  ) {}
7975
8003
 
8004
+ /** Return currently configured level for given module. */
8005
+ getLevel(): Level {
8006
+ return this.getLevelAndName()[0];
8007
+ }
8008
+
8009
+ private getLevelAndName(): readonly [Level, string] {
8010
+ if (this.cachedLevelAndName === undefined) {
8011
+ const level = findLevel(this.config.options, this.moduleName);
8012
+ const shortName = this.moduleName.replace(this.config.options.workingDir, "");
8013
+ this.cachedLevelAndName = [level, shortName];
8014
+ }
8015
+ return this.cachedLevelAndName;
8016
+ }
8017
+
7976
8018
  /** Log a message with `INSANE` level. */
7977
- insane(val: string) {
7978
- this.config.transport.insane(this.moduleName, val);
8019
+ insane(strings: TemplateStringsArray, ...data: unknown[]) {
8020
+ this.config.transport.insane(this.getLevelAndName(), strings, data);
7979
8021
  }
7980
8022
 
7981
8023
  /** Log a message with `TRACE` level. */
7982
- trace(val: string) {
7983
- this.config.transport.trace(this.moduleName, val);
8024
+ trace(strings: TemplateStringsArray, ...data: unknown[]) {
8025
+ this.config.transport.trace(this.getLevelAndName(), strings, data);
7984
8026
  }
7985
8027
 
7986
8028
  /** Log a message with `DEBUG`/`LOG` level. */
7987
- log(val: string) {
7988
- this.config.transport.log(this.moduleName, val);
8029
+ log(strings: TemplateStringsArray, ...data: unknown[]) {
8030
+ this.config.transport.log(this.getLevelAndName(), strings, data);
7989
8031
  }
7990
8032
 
7991
8033
  /** Log a message with `INFO` level. */
7992
- info(val: string) {
7993
- this.config.transport.info(this.moduleName, val);
8034
+ info(strings: TemplateStringsArray, ...data: unknown[]) {
8035
+ this.config.transport.info(this.getLevelAndName(), strings, data);
7994
8036
  }
7995
8037
 
7996
8038
  /** Log a message with `WARN` level. */
7997
- warn(val: string) {
7998
- this.config.transport.warn(this.moduleName, val);
8039
+ warn(strings: TemplateStringsArray, ...data: unknown[]) {
8040
+ this.config.transport.warn(this.getLevelAndName(), strings, data);
7999
8041
  }
8000
8042
 
8001
8043
  /** Log a message with `ERROR` level. */
8002
- error(val: string) {
8003
- this.config.transport.error(this.moduleName, val);
8044
+ error(strings: TemplateStringsArray, ...data: unknown[]) {
8045
+ this.config.transport.error(this.getLevelAndName(), strings, data);
8004
8046
  }
8005
8047
  }
8006
8048
 
@@ -8099,17 +8141,17 @@ declare class NodeConfiguration {
8099
8141
 
8100
8142
  declare function loadConfig(configPath: string): NodeConfiguration {
8101
8143
  if (configPath === DEFAULT_CONFIG) {
8102
- logger.log("🔧 Loading DEFAULT config");
8144
+ logger.log`🔧 Loading DEFAULT config`;
8103
8145
  return parseFromJson(configs.default, NodeConfiguration.fromJson);
8104
8146
  }
8105
8147
 
8106
8148
  if (configPath === DEV_CONFIG) {
8107
- logger.log("🔧 Loading DEV config");
8149
+ logger.log`🔧 Loading DEV config`;
8108
8150
  return parseFromJson(configs.dev, NodeConfiguration.fromJson);
8109
8151
  }
8110
8152
 
8111
8153
  try {
8112
- logger.log(`🔧 Loading config from ${configPath}`);
8154
+ logger.log`🔧 Loading config from ${configPath}`;
8113
8155
  const configFile = fs.readFileSync(configPath, "utf8");
8114
8156
  const parsed = JSON.parse(configFile);
8115
8157
  return parseFromJson(parsed, NodeConfiguration.fromJson);
@@ -14446,8 +14488,6 @@ declare enum AccessType {
14446
14488
  WRITE = 1,
14447
14489
  }
14448
14490
 
14449
- // const logger = Logger.new(import.meta.filename, "pvm:mem");
14450
-
14451
14491
  declare class Memory {
14452
14492
  static fromInitialMemory(initialMemoryState: InitialMemoryState) {
14453
14493
  return new Memory(
@@ -14484,7 +14524,7 @@ declare class Memory {
14484
14524
  return Result.ok(OK);
14485
14525
  }
14486
14526
 
14487
- // logger.insane(`MEM[${address}] <- ${BytesBlob.blobFrom(bytes)}`);
14527
+ logger.insane`MEM[${address}] <- ${BytesBlob.blobFrom(bytes)}`;
14488
14528
  const pagesResult = this.getPages(address, bytes.length, AccessType.WRITE);
14489
14529
 
14490
14530
  if (pagesResult.isError) {
@@ -14573,7 +14613,7 @@ declare class Memory {
14573
14613
  bytesLeft -= bytesToRead;
14574
14614
  }
14575
14615
 
14576
- // logger.insane(`MEM[${startAddress}] => ${BytesBlob.blobFrom(result)}`);
14616
+ logger.insane`MEM[${startAddress}] => ${BytesBlob.blobFrom(result)}`;
14577
14617
  return Result.ok(OK);
14578
14618
  }
14579
14619
 
@@ -14740,11 +14780,12 @@ declare class MemoryBuilder {
14740
14780
  `;
14741
14781
  this.ensureNotFinalized();
14742
14782
 
14743
- const range = MemoryRange.fromStartAndLength(startHeapIndex, endHeapIndex - startHeapIndex);
14744
- const pages = PageRange.fromMemoryRange(range);
14783
+ const heapRange = MemoryRange.fromStartAndLength(startHeapIndex, endHeapIndex - startHeapIndex);
14784
+ const heapPagesRange = PageRange.fromMemoryRange(heapRange);
14785
+ const initializedPageNumbers = Array.from(this.initialMemory.keys());
14745
14786
 
14746
- for (const pageNumber of pages) {
14747
- if (this.initialMemory.has(pageNumber)) {
14787
+ for (const pageNumber of initializedPageNumbers) {
14788
+ if (heapPagesRange.isInRange(pageNumber)) {
14748
14789
  throw new IncorrectSbrkIndex();
14749
14790
  }
14750
14791
  }
@@ -16536,7 +16577,7 @@ declare class ProgramDecoder {
16536
16577
  try {
16537
16578
  return Result.ok(new ProgramDecoder(program));
16538
16579
  } catch (e) {
16539
- logger.error(`Invalid program: ${e}`);
16580
+ logger.error`Invalid program: ${e}`;
16540
16581
  return Result.error(ProgramDecoderError.InvalidProgramError);
16541
16582
  }
16542
16583
  }
@@ -16698,7 +16739,7 @@ declare class Interpreter {
16698
16739
  const argsResult = this.argsDecodingResults[argsType];
16699
16740
  this.argsDecoder.fillArgs(this.pc, argsResult);
16700
16741
 
16701
- logger.insane(`[PC: ${this.pc}] ${Instruction[currentInstruction]}`);
16742
+ logger.insane`[PC: ${this.pc}] ${Instruction[currentInstruction]}`;
16702
16743
 
16703
16744
  if (!isValidInstruction) {
16704
16745
  this.instructionResult.status = Result.PANIC;
@@ -16775,7 +16816,7 @@ declare class Interpreter {
16775
16816
  this.status = Status.HOST;
16776
16817
  break;
16777
16818
  }
16778
- logger.insane(`[PC: ${this.pc}] Status: ${Result[this.instructionResult.status]}`);
16819
+ logger.insane`[PC: ${this.pc}] Status: ${Result[this.instructionResult.status]}`;
16779
16820
  return this.status;
16780
16821
  }
16781
16822
 
@@ -17117,18 +17158,13 @@ declare class PartiallyUpdatedState<T extends StateSlice = StateSlice> {
17117
17158
  *
17118
17159
  * NOTE the info may be updated compared to what is in the state.
17119
17160
  *
17120
- * Takes into account newly created services as well.
17161
+ * Takes into account ejected and newly created services as well.
17121
17162
  */
17122
17163
  getServiceInfo(destination: ServiceId | null): ServiceAccountInfo | null {
17123
17164
  if (destination === null) {
17124
17165
  return null;
17125
17166
  }
17126
17167
 
17127
- const isEjected = this.stateUpdate.services.servicesRemoved.some((x) => x === destination);
17128
- if (isEjected) {
17129
- return null;
17130
- }
17131
-
17132
17168
  const maybeNewService = this.stateUpdate.services.servicesUpdates.find(
17133
17169
  (update) => update.serviceId === destination,
17134
17170
  );
@@ -17514,7 +17550,7 @@ declare class HostCallsManager {
17514
17550
  return `r${idx}=${value} (0x${value.toString(16)})`;
17515
17551
  })
17516
17552
  .join(", ");
17517
- logger.insane(`[${currentServiceId}] ${context} ${name}${requested}. Gas: ${gas}. Regs: ${registerValues}.`);
17553
+ logger.insane`[${currentServiceId}] ${context} ${name}${requested}. Gas: ${gas}. Regs: ${registerValues}.`;
17518
17554
  }
17519
17555
  }
17520
17556
 
package/index.js CHANGED
@@ -7,6 +7,7 @@ var GpVersion;
7
7
  GpVersion["V0_6_7"] = "0.6.7";
8
8
  GpVersion["V0_7_0"] = "0.7.0";
9
9
  GpVersion["V0_7_1"] = "0.7.1-preview";
10
+ GpVersion["V0_7_2"] = "0.7.2-preview";
10
11
  })(GpVersion || (GpVersion = {}));
11
12
  var TestSuite;
12
13
  (function (TestSuite) {
@@ -14,29 +15,35 @@ var TestSuite;
14
15
  TestSuite["JAMDUNA"] = "jamduna";
15
16
  })(TestSuite || (TestSuite = {}));
16
17
  const DEFAULT_SUITE = TestSuite.W3F_DAVXY;
17
- const ALL_VERSIONS_IN_ORDER = [GpVersion.V0_6_7, GpVersion.V0_7_0, GpVersion.V0_7_1];
18
- const env = typeof process === "undefined" ? {} : process.env;
18
+ const ALL_VERSIONS_IN_ORDER = [GpVersion.V0_6_7, GpVersion.V0_7_0, GpVersion.V0_7_1, GpVersion.V0_7_2];
19
+ const env$1 = typeof process === "undefined" ? {} : process.env;
19
20
  const DEFAULT_VERSION = GpVersion.V0_7_0;
20
- let CURRENT_VERSION = parseCurrentVersion(env.GP_VERSION) ?? DEFAULT_VERSION;
21
- let CURRENT_SUITE = parseCurrentSuite(env.TEST_SUITE) ?? DEFAULT_SUITE;
21
+ let CURRENT_VERSION = parseCurrentVersion(env$1.GP_VERSION) ?? DEFAULT_VERSION;
22
+ let CURRENT_SUITE = parseCurrentSuite(env$1.TEST_SUITE) ?? DEFAULT_SUITE;
22
23
  function parseCurrentVersion(env) {
23
24
  if (env === undefined) {
24
25
  return undefined;
25
26
  }
26
- const version = env;
27
- if (!Object.values(GpVersion).includes(version)) {
28
- throw new Error(`Configured environment variable GP_VERSION is unknown: '${env}'. Use one of: ${ALL_VERSIONS_IN_ORDER}`);
27
+ switch (env) {
28
+ case GpVersion.V0_6_7:
29
+ case GpVersion.V0_7_0:
30
+ case GpVersion.V0_7_1:
31
+ case GpVersion.V0_7_2:
32
+ return env;
33
+ default:
34
+ throw new Error(`Configured environment variable GP_VERSION is unknown: '${env}'. Use one of: ${ALL_VERSIONS_IN_ORDER}`);
29
35
  }
30
- return version;
31
36
  }
32
37
  function parseCurrentSuite(env) {
33
38
  if (env === undefined)
34
39
  return undefined;
35
- const val = env;
36
- if (!Object.values(TestSuite).includes(val)) {
37
- throw new Error(`Configured environment variable TEST_SUITE is unknown: '${env}'. Use one of: ${Object.values(TestSuite)}`);
40
+ switch (env) {
41
+ case TestSuite.W3F_DAVXY:
42
+ case TestSuite.JAMDUNA:
43
+ return env;
44
+ default:
45
+ throw new Error(`Configured environment variable TEST_SUITE is unknown: '${env}'. Use one of: ${Object.values(TestSuite)}`);
38
46
  }
39
- return val;
40
47
  }
41
48
  class Compatibility {
42
49
  static override(version) {
@@ -197,6 +204,33 @@ class WithDebug {
197
204
  }
198
205
  }
199
206
 
207
+ const env = typeof process === "undefined" ? {} : process.env;
208
+ /**
209
+ * The function will produce relative path resolver that is adjusted
210
+ * for package location within the workspace.
211
+ *
212
+ * Example:
213
+ * $ npm start -w @typeberry/jam
214
+ *
215
+ * The above command will run `./bin/jam/index.js`, however we would
216
+ * still want relative paths to be resolved according to top-level workspace
217
+ * directory.
218
+ *
219
+ * So the caller, passes the absolute workspace path as argument and get's
220
+ * a function that can properly resolve relative paths.
221
+ *
222
+ * NOTE: the translation happens only for development build! When
223
+ * we build a single library from our project, we no longer mangle the paths.
224
+ */
225
+ const workspacePathFix = env.NODE_ENV === "development"
226
+ ? (workspacePath) => (p) => {
227
+ if (p.startsWith("/")) {
228
+ return p;
229
+ }
230
+ return `${workspacePath}/${p}`;
231
+ }
232
+ : () => (p) => p;
233
+
200
234
  /**
201
235
  * @fileoverview `Opaque<Type, Token>` constructs a unique type which is a subset of Type with a
202
236
  * specified unique token Token. It means that base type cannot be assigned to unique type by accident.
@@ -549,7 +583,8 @@ var index$s = /*#__PURE__*/Object.freeze({
549
583
  isBrowser: isBrowser,
550
584
  measure: measure,
551
585
  resultToString: resultToString,
552
- seeThrough: seeThrough
586
+ seeThrough: seeThrough,
587
+ workspacePathFix: workspacePathFix
553
588
  });
554
589
 
555
590
  /**
@@ -8009,6 +8044,23 @@ function parseLevel(lvl) {
8009
8044
  }
8010
8045
 
8011
8046
  // biome-ignore-all lint/suspicious/noConsole: logger
8047
+ function print(level, levelAndName, strings, data) {
8048
+ if (level < levelAndName[0]) {
8049
+ return;
8050
+ }
8051
+ const lvlText = Level[level].padEnd(5);
8052
+ const val = strings.map((v, idx) => `${v}${idx < data.length ? data[idx] : ""}`);
8053
+ const msg = `${lvlText} [${levelAndName[1]}] ${val.join("")}`;
8054
+ if (level === Level.WARN) {
8055
+ console.warn(msg);
8056
+ }
8057
+ else if (level === Level.ERROR) {
8058
+ console.error(msg);
8059
+ }
8060
+ else {
8061
+ console.info(msg);
8062
+ }
8063
+ }
8012
8064
  /** An optimized logger that ignores `TRACE`, `DEBUG` and `LOG` messages.
8013
8065
  *
8014
8066
  * Use the `create` method to instantiate the right instance of a more specialized logger.
@@ -8039,109 +8091,91 @@ class ConsoleTransport {
8039
8091
  constructor(options) {
8040
8092
  this.options = options;
8041
8093
  }
8042
- insane(_moduleName, _val) {
8094
+ insane(_levelAndName, _strings, _data) {
8043
8095
  /* no-op */
8044
8096
  }
8045
- trace(_moduleName, _val) {
8097
+ trace(_levelAndName, _strings, _data) {
8046
8098
  /* no-op */
8047
8099
  }
8048
- log(_moduleName, _val) {
8100
+ log(_levelAndName, _strings, _data) {
8049
8101
  /* no-op */
8050
8102
  }
8051
- info(_moduleName, _val) {
8103
+ info(_levelAndName, _strings, _data) {
8052
8104
  /* no-op */
8053
8105
  }
8054
- warn(moduleName, val) {
8055
- this.push(Level.WARN, moduleName, val);
8106
+ warn(levelAndName, strings, data) {
8107
+ print(Level.WARN, levelAndName, strings, data);
8056
8108
  }
8057
- error(moduleName, val) {
8058
- this.push(Level.ERROR, moduleName, val);
8059
- }
8060
- push(level, moduleName, val) {
8061
- const shortModule = moduleName.replace(this.options.workingDir, "");
8062
- const configuredLevel = findLevel(this.options, moduleName);
8063
- const lvlText = Level[level].padEnd(5);
8064
- if (level < configuredLevel) {
8065
- return;
8066
- }
8067
- const msg = `${lvlText} [${shortModule}] ${val}`;
8068
- if (level === Level.WARN) {
8069
- console.warn(msg);
8070
- }
8071
- else if (level === Level.ERROR) {
8072
- console.error(msg);
8073
- }
8074
- else {
8075
- console.info(msg);
8076
- }
8109
+ error(levelAndName, strings, data) {
8110
+ print(Level.ERROR, levelAndName, strings, data);
8077
8111
  }
8078
8112
  }
8079
8113
  /**
8080
8114
  * Insane version of console logger - supports insane level.
8081
8115
  */
8082
8116
  class InsaneConsoleLogger extends ConsoleTransport {
8083
- insane(moduleName, val) {
8084
- this.push(Level.INSANE, moduleName, val);
8117
+ insane(levelAndName, strings, data) {
8118
+ print(Level.INSANE, levelAndName, strings, data);
8085
8119
  }
8086
- trace(moduleName, val) {
8087
- this.push(Level.TRACE, moduleName, val);
8120
+ trace(levelAndName, strings, data) {
8121
+ print(Level.TRACE, levelAndName, strings, data);
8088
8122
  }
8089
- log(moduleName, val) {
8090
- this.push(Level.LOG, moduleName, val);
8123
+ log(levelAndName, strings, data) {
8124
+ print(Level.LOG, levelAndName, strings, data);
8091
8125
  }
8092
- info(moduleName, val) {
8093
- this.push(Level.INFO, moduleName, val);
8126
+ info(levelAndName, strings, data) {
8127
+ print(Level.INFO, levelAndName, strings, data);
8094
8128
  }
8095
8129
  }
8096
8130
  /**
8097
8131
  * A basic version of console logger - printing everything.
8098
8132
  */
8099
8133
  class TraceConsoleTransport extends ConsoleTransport {
8100
- insane(_moduleName, _val) {
8134
+ insane(_levelAndName, _strings, _data) {
8101
8135
  /* no-op */
8102
8136
  }
8103
- trace(moduleName, val) {
8104
- this.push(Level.TRACE, moduleName, val);
8137
+ trace(levelAndName, strings, data) {
8138
+ print(Level.TRACE, levelAndName, strings, data);
8105
8139
  }
8106
- log(moduleName, val) {
8107
- this.push(Level.LOG, moduleName, val);
8140
+ log(levelAndName, strings, data) {
8141
+ print(Level.LOG, levelAndName, strings, data);
8108
8142
  }
8109
- info(moduleName, val) {
8110
- this.push(Level.INFO, moduleName, val);
8143
+ info(levelAndName, strings, data) {
8144
+ print(Level.INFO, levelAndName, strings, data);
8111
8145
  }
8112
8146
  }
8113
8147
  /**
8114
8148
  * An optimized version of the logger - completely ignores `TRACE` level calls.
8115
8149
  */
8116
8150
  class LogConsoleTransport extends ConsoleTransport {
8117
- insane(_moduleName, _val) {
8151
+ insane(_levelAndName, _strings, _data) {
8118
8152
  /* no-op */
8119
8153
  }
8120
- trace(_moduleName, _val) {
8154
+ trace(_levelAndName, _strings, _data) {
8121
8155
  /* no-op */
8122
8156
  }
8123
- log(moduleName, val) {
8124
- this.push(Level.LOG, moduleName, val);
8157
+ log(levelAndName, strings, data) {
8158
+ print(Level.LOG, levelAndName, strings, data);
8125
8159
  }
8126
- info(moduleName, val) {
8127
- this.push(Level.INFO, moduleName, val);
8160
+ info(levelAndName, strings, data) {
8161
+ print(Level.INFO, levelAndName, strings, data);
8128
8162
  }
8129
8163
  }
8130
8164
  /**
8131
8165
  * An optimized version of the logger - completely ignores `TRACE` & `DEBUG` level calls.
8132
8166
  */
8133
8167
  class InfoConsoleTransport extends ConsoleTransport {
8134
- insane(_moduleName, _val) {
8168
+ insane(_levelAndName, _strings, _data) {
8135
8169
  /* no-op */
8136
8170
  }
8137
- trace(_moduleName, _val) {
8171
+ trace(_levelAndName, _strings, _data) {
8138
8172
  /* no-op */
8139
8173
  }
8140
- log(_moduleName, _val) {
8174
+ log(_levelAndName, _strings, _data) {
8141
8175
  /* no-op */
8142
8176
  }
8143
- info(moduleName, val) {
8144
- this.push(Level.INFO, moduleName, val);
8177
+ info(levelAndName, strings, data) {
8178
+ print(Level.INFO, levelAndName, strings, data);
8145
8179
  }
8146
8180
  }
8147
8181
 
@@ -8174,11 +8208,6 @@ class Logger {
8174
8208
  const module = moduleName ?? fName;
8175
8209
  return new Logger(module.padStart(8, " "), GLOBAL_CONFIG);
8176
8210
  }
8177
- /**
8178
- * Return currently configured level for given module. */
8179
- static getLevel(moduleName) {
8180
- return findLevel(GLOBAL_CONFIG.options, moduleName);
8181
- }
8182
8211
  /**
8183
8212
  * Global configuration of all loggers.
8184
8213
  *
@@ -8209,33 +8238,46 @@ class Logger {
8209
8238
  const options = parseLoggerOptions(input, defaultLevel, workingDir);
8210
8239
  Logger.configureAllFromOptions(options);
8211
8240
  }
8241
+ cachedLevelAndName;
8212
8242
  constructor(moduleName, config) {
8213
8243
  this.moduleName = moduleName;
8214
8244
  this.config = config;
8215
8245
  }
8246
+ /** Return currently configured level for given module. */
8247
+ getLevel() {
8248
+ return this.getLevelAndName()[0];
8249
+ }
8250
+ getLevelAndName() {
8251
+ if (this.cachedLevelAndName === undefined) {
8252
+ const level = findLevel(this.config.options, this.moduleName);
8253
+ const shortName = this.moduleName.replace(this.config.options.workingDir, "");
8254
+ this.cachedLevelAndName = [level, shortName];
8255
+ }
8256
+ return this.cachedLevelAndName;
8257
+ }
8216
8258
  /** Log a message with `INSANE` level. */
8217
- insane(val) {
8218
- this.config.transport.insane(this.moduleName, val);
8259
+ insane(strings, ...data) {
8260
+ this.config.transport.insane(this.getLevelAndName(), strings, data);
8219
8261
  }
8220
8262
  /** Log a message with `TRACE` level. */
8221
- trace(val) {
8222
- this.config.transport.trace(this.moduleName, val);
8263
+ trace(strings, ...data) {
8264
+ this.config.transport.trace(this.getLevelAndName(), strings, data);
8223
8265
  }
8224
8266
  /** Log a message with `DEBUG`/`LOG` level. */
8225
- log(val) {
8226
- this.config.transport.log(this.moduleName, val);
8267
+ log(strings, ...data) {
8268
+ this.config.transport.log(this.getLevelAndName(), strings, data);
8227
8269
  }
8228
8270
  /** Log a message with `INFO` level. */
8229
- info(val) {
8230
- this.config.transport.info(this.moduleName, val);
8271
+ info(strings, ...data) {
8272
+ this.config.transport.info(this.getLevelAndName(), strings, data);
8231
8273
  }
8232
8274
  /** Log a message with `WARN` level. */
8233
- warn(val) {
8234
- this.config.transport.warn(this.moduleName, val);
8275
+ warn(strings, ...data) {
8276
+ this.config.transport.warn(this.getLevelAndName(), strings, data);
8235
8277
  }
8236
8278
  /** Log a message with `ERROR` level. */
8237
- error(val) {
8238
- this.config.transport.error(this.moduleName, val);
8279
+ error(strings, ...data) {
8280
+ this.config.transport.error(this.getLevelAndName(), strings, data);
8239
8281
  }
8240
8282
  }
8241
8283
 
@@ -8262,7 +8304,7 @@ class AuthorshipOptions {
8262
8304
  }
8263
8305
  }
8264
8306
 
8265
- const logger$3 = Logger.new(import.meta.filename, "config");
8307
+ const logger$4 = Logger.new(import.meta.filename, "config");
8266
8308
  /** Development config. Will accept unsealed blocks for now. */
8267
8309
  const DEV_CONFIG = "dev";
8268
8310
  /** Default config file. */
@@ -8321,15 +8363,15 @@ class NodeConfiguration {
8321
8363
  }
8322
8364
  function loadConfig(configPath) {
8323
8365
  if (configPath === DEFAULT_CONFIG) {
8324
- logger$3.log("🔧 Loading DEFAULT config");
8366
+ logger$4.log `🔧 Loading DEFAULT config`;
8325
8367
  return parseFromJson(configs.default, NodeConfiguration.fromJson);
8326
8368
  }
8327
8369
  if (configPath === DEV_CONFIG) {
8328
- logger$3.log("🔧 Loading DEV config");
8370
+ logger$4.log `🔧 Loading DEV config`;
8329
8371
  return parseFromJson(configs.dev, NodeConfiguration.fromJson);
8330
8372
  }
8331
8373
  try {
8332
- logger$3.log(`🔧 Loading config from ${configPath}`);
8374
+ logger$4.log `🔧 Loading config from ${configPath}`;
8333
8375
  const configFile = fs.readFileSync(configPath, "utf8");
8334
8376
  const parsed = JSON.parse(configFile);
8335
8377
  return parseFromJson(parsed, NodeConfiguration.fromJson);
@@ -12359,16 +12401,12 @@ class PartiallyUpdatedState {
12359
12401
  *
12360
12402
  * NOTE the info may be updated compared to what is in the state.
12361
12403
  *
12362
- * Takes into account newly created services as well.
12404
+ * Takes into account ejected and newly created services as well.
12363
12405
  */
12364
12406
  getServiceInfo(destination) {
12365
12407
  if (destination === null) {
12366
12408
  return null;
12367
12409
  }
12368
- const isEjected = this.stateUpdate.services.servicesRemoved.some((x) => x === destination);
12369
- if (isEjected) {
12370
- return null;
12371
- }
12372
12410
  const maybeNewService = this.stateUpdate.services.servicesUpdates.find((update) => update.serviceId === destination);
12373
12411
  if (maybeNewService !== undefined) {
12374
12412
  return maybeNewService.action.account;
@@ -14084,6 +14122,17 @@ class PageRange {
14084
14122
  }
14085
14123
  return new PageRange(start, length);
14086
14124
  }
14125
+ /** Returns true if the page range is wrapped (`start` >= `end`) and is not empty */
14126
+ isWrapped() {
14127
+ return this.start >= this.end && !this.isEmpty();
14128
+ }
14129
+ /** Checks if given page number is within the range */
14130
+ isInRange(page) {
14131
+ if (this.isWrapped()) {
14132
+ return page >= this.start || page < this.end;
14133
+ }
14134
+ return page >= this.start && page < this.end;
14135
+ }
14087
14136
  /** Checks if a range is empty (`length === 0`) */
14088
14137
  isEmpty() {
14089
14138
  return this.length === 0;
@@ -14191,7 +14240,7 @@ var AccessType;
14191
14240
  AccessType[AccessType["READ"] = 0] = "READ";
14192
14241
  AccessType[AccessType["WRITE"] = 1] = "WRITE";
14193
14242
  })(AccessType || (AccessType = {}));
14194
- // const logger = Logger.new(import.meta.filename, "pvm:mem");
14243
+ const logger$3 = Logger.new(import.meta.filename, "pvm:mem");
14195
14244
  class Memory {
14196
14245
  sbrkIndex;
14197
14246
  virtualSbrkIndex;
@@ -14222,7 +14271,7 @@ class Memory {
14222
14271
  if (bytes.length === 0) {
14223
14272
  return Result$1.ok(OK);
14224
14273
  }
14225
- // logger.insane(`MEM[${address}] <- ${BytesBlob.blobFrom(bytes)}`);
14274
+ logger$3.insane `MEM[${address}] <- ${BytesBlob.blobFrom(bytes)}`;
14226
14275
  const pagesResult = this.getPages(address, bytes.length, AccessType.WRITE);
14227
14276
  if (pagesResult.isError) {
14228
14277
  return Result$1.error(pagesResult.error);
@@ -14289,7 +14338,7 @@ class Memory {
14289
14338
  currentPosition += bytesToRead;
14290
14339
  bytesLeft -= bytesToRead;
14291
14340
  }
14292
- // logger.insane(`MEM[${startAddress}] => ${BytesBlob.blobFrom(result)}`);
14341
+ logger$3.insane `MEM[${startAddress}] => ${BytesBlob.blobFrom(result)}`;
14293
14342
  return Result$1.ok(OK);
14294
14343
  }
14295
14344
  sbrk(length) {
@@ -14424,10 +14473,11 @@ class MemoryBuilder {
14424
14473
  startHeapIndex (${startHeapIndex}) has to be less than or equal to endHeapIndex (${endHeapIndex})
14425
14474
  `;
14426
14475
  this.ensureNotFinalized();
14427
- const range = MemoryRange.fromStartAndLength(startHeapIndex, endHeapIndex - startHeapIndex);
14428
- const pages = PageRange.fromMemoryRange(range);
14429
- for (const pageNumber of pages) {
14430
- if (this.initialMemory.has(pageNumber)) {
14476
+ const heapRange = MemoryRange.fromStartAndLength(startHeapIndex, endHeapIndex - startHeapIndex);
14477
+ const heapPagesRange = PageRange.fromMemoryRange(heapRange);
14478
+ const initializedPageNumbers = Array.from(this.initialMemory.keys());
14479
+ for (const pageNumber of initializedPageNumbers) {
14480
+ if (heapPagesRange.isInRange(pageNumber)) {
14431
14481
  throw new IncorrectSbrkIndex();
14432
14482
  }
14433
14483
  }
@@ -16083,7 +16133,7 @@ class ProgramDecoder {
16083
16133
  return Result$1.ok(new ProgramDecoder(program));
16084
16134
  }
16085
16135
  catch (e) {
16086
- logger$2.error(`Invalid program: ${e}`);
16136
+ logger$2.error `Invalid program: ${e}`;
16087
16137
  return Result$1.error(ProgramDecoderError.InvalidProgramError);
16088
16138
  }
16089
16139
  }
@@ -16208,7 +16258,7 @@ class Interpreter {
16208
16258
  const argsType = instructionArgumentTypeMap[currentInstruction] ?? ArgumentType.NO_ARGUMENTS;
16209
16259
  const argsResult = this.argsDecodingResults[argsType];
16210
16260
  this.argsDecoder.fillArgs(this.pc, argsResult);
16211
- logger$1.insane(`[PC: ${this.pc}] ${Instruction[currentInstruction]}`);
16261
+ logger$1.insane `[PC: ${this.pc}] ${Instruction[currentInstruction]}`;
16212
16262
  if (!isValidInstruction) {
16213
16263
  this.instructionResult.status = Result.PANIC;
16214
16264
  }
@@ -16280,7 +16330,7 @@ class Interpreter {
16280
16330
  this.status = Status.HOST;
16281
16331
  break;
16282
16332
  }
16283
- logger$1.insane(`[PC: ${this.pc}] Status: ${Result[this.instructionResult.status]}`);
16333
+ logger$1.insane `[PC: ${this.pc}] Status: ${Result[this.instructionResult.status]}`;
16284
16334
  return this.status;
16285
16335
  }
16286
16336
  this.pc = this.instructionResult.nextPc;
@@ -16514,7 +16564,7 @@ class HostCallsManager {
16514
16564
  return `r${idx}=${value} (0x${value.toString(16)})`;
16515
16565
  })
16516
16566
  .join(", ");
16517
- logger.insane(`[${currentServiceId}] ${context} ${name}${requested}. Gas: ${gas}. Regs: ${registerValues}.`);
16567
+ logger.insane `[${currentServiceId}] ${context} ${name}${requested}. Gas: ${gas}. Regs: ${registerValues}.`;
16518
16568
  }
16519
16569
  }
16520
16570
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@typeberry/lib",
3
- "version": "0.1.1",
3
+ "version": "0.1.2-3178190",
4
4
  "main": "index.js",
5
5
  "author": "Fluffy Labs",
6
6
  "license": "MPL-2.0",