@typeberry/jam 0.4.0-203a18d → 0.4.0-248b604

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.js CHANGED
@@ -24314,7 +24314,11 @@ var TestSuite;
24314
24314
  })(TestSuite || (TestSuite = {}));
24315
24315
  const ALL_VERSIONS_IN_ORDER = [GpVersion.V0_6_7, GpVersion.V0_7_0, GpVersion.V0_7_1, GpVersion.V0_7_2];
24316
24316
  const DEFAULT_SUITE = TestSuite.W3F_DAVXY;
24317
- const DEFAULT_VERSION = GpVersion.V0_7_2;
24317
+ /**
24318
+ * Current version is set to track the jam-conformance testing.
24319
+ * Since we are currently at 0.7.1 not 0.7.2, we set our default version accordingly.
24320
+ */
24321
+ const DEFAULT_VERSION = GpVersion.V0_7_1;
24318
24322
  const env = typeof process === "undefined" ? {} : process.env;
24319
24323
  let CURRENT_VERSION = parseCurrentVersion(env.GP_VERSION) ?? DEFAULT_VERSION;
24320
24324
  let CURRENT_SUITE = parseCurrentSuite(env.TEST_SUITE) ?? DEFAULT_SUITE;
@@ -24373,8 +24377,8 @@ class Compatibility {
24373
24377
  /**
24374
24378
  * Allows selecting different values for different Gray Paper versions from one record.
24375
24379
  *
24376
- * @param fallback The default value to return if no value is found for the current.
24377
- * @param record A record mapping versions to values, checking if the version is greater or equal to the current version.
24380
+ * fallback The default value to return if no value is found for the current.
24381
+ * versions A record mapping versions to values, checking if the version is greater or equal to the current version.
24378
24382
  * @returns The value for the current version, or the default value.
24379
24383
  */
24380
24384
  static selectIfGreaterOrEqual({ fallback, versions, }) {
@@ -24537,7 +24541,7 @@ const workspacePathFix = dev_env.NODE_ENV === "development"
24537
24541
 
24538
24542
  ;// CONCATENATED MODULE: ./packages/core/utils/opaque.ts
24539
24543
  /**
24540
- * @fileoverview `Opaque<Type, Token>` constructs a unique type which is a subset of Type with a
24544
+ * `Opaque<Type, Token>` constructs a unique type which is a subset of Type with a
24541
24545
  * specified unique token Token. It means that base type cannot be assigned to unique type by accident.
24542
24546
  * Good examples of opaque types include:
24543
24547
  * - JWTs or other tokens - these are special kinds of string used for authorization purposes.
@@ -35445,7 +35449,6 @@ class LeafNode {
35445
35449
  /**
35446
35450
  * Get the byte length of embedded value.
35447
35451
  *
35448
- * @remark
35449
35452
  * Note in case this node only contains hash this is going to be 0.
35450
35453
  */
35451
35454
  getValueLength() {
@@ -35456,7 +35459,6 @@ class LeafNode {
35456
35459
  /**
35457
35460
  * Returns the embedded value.
35458
35461
  *
35459
- * @remark
35460
35462
  * Note that this is going to be empty for a regular leaf node (i.e. containing a hash).
35461
35463
  */
35462
35464
  getValue() {
@@ -35466,7 +35468,6 @@ class LeafNode {
35466
35468
  /**
35467
35469
  * Returns contained value hash.
35468
35470
  *
35469
- * @remark
35470
35471
  * Note that for embedded value this is going to be full 0-padded 32 bytes.
35471
35472
  */
35472
35473
  getValueHash() {
@@ -39149,7 +39150,7 @@ const gas_tryAsSmallGas = (v) => opaque_asOpaqueType(numbers_tryAsU32(v));
39149
39150
  /** Attempt to convert given number into U64 gas representation. */
39150
39151
  const tryAsBigGas = (v) => opaque_asOpaqueType(numbers_tryAsU64(v));
39151
39152
  /** Attempt to convert given number into gas. */
39152
- const tryAsGas = (v) => typeof v === "number" && v < 2 ** 32 ? gas_tryAsSmallGas(v) : tryAsBigGas(v);
39153
+ const gas_tryAsGas = (v) => typeof v === "number" && v < 2 ** 32 ? gas_tryAsSmallGas(v) : tryAsBigGas(v);
39153
39154
 
39154
39155
  ;// CONCATENATED MODULE: ./packages/core/pvm-interface/memory.ts
39155
39156
 
@@ -39432,7 +39433,7 @@ const tryAsRegisterIndex = (index) => {
39432
39433
  debug_check `${index >= 0 && index < registers_NO_OF_REGISTERS} Incorrect register index: ${index}!`;
39433
39434
  return opaque_asOpaqueType(index);
39434
39435
  };
39435
- class Registers {
39436
+ class registers_Registers {
39436
39437
  bytes;
39437
39438
  asSigned;
39438
39439
  asUnsigned;
@@ -39451,7 +39452,7 @@ class Registers {
39451
39452
  }
39452
39453
  static fromBytes(bytes) {
39453
39454
  debug_check `${bytes.length === registers_NO_OF_REGISTERS << REGISTER_SIZE_SHIFT} Invalid size of registers array.`;
39454
- return new Registers(bytes);
39455
+ return new registers_Registers(bytes);
39455
39456
  }
39456
39457
  getBytesAsLittleEndian(index, len) {
39457
39458
  const offset = index << REGISTER_SIZE_SHIFT;
@@ -39765,49 +39766,10 @@ class NoopMissing {
39765
39766
  }
39766
39767
  }
39767
39768
 
39768
- ;// CONCATENATED MODULE: ./packages/core/pvm-interpreter/gas.ts
39769
-
39770
-
39771
- /** Create a new gas counter instance depending on the gas value. */
39772
- function gasCounter(gas) {
39773
- return new GasCounterU64(numbers_tryAsU64(gas));
39774
- }
39775
- class GasCounterU64 {
39776
- gas;
39777
- initialGas;
39778
- constructor(gas) {
39779
- this.gas = gas;
39780
- this.initialGas = tryAsGas(gas);
39781
- }
39782
- set(g) {
39783
- this.gas = numbers_tryAsU64(g);
39784
- }
39785
- get() {
39786
- return tryAsGas(this.gas);
39787
- }
39788
- sub(g) {
39789
- const result = this.gas - numbers_tryAsU64(g);
39790
- if (result >= 0n) {
39791
- this.gas = numbers_tryAsU64(result);
39792
- return false;
39793
- }
39794
- this.gas = numbers_tryAsU64(0n);
39795
- return true;
39796
- }
39797
- used() {
39798
- const gasConsumed = numbers_tryAsU64(this.initialGas) - this.gas;
39799
- // In we have less than zero left we assume that all gas has been consumed.
39800
- if (gasConsumed < 0) {
39801
- return this.initialGas;
39802
- }
39803
- return tryAsGas(gasConsumed);
39804
- }
39805
- }
39806
-
39807
39769
  ;// CONCATENATED MODULE: ./packages/core/pvm-interpreter/memory/memory-index.ts
39808
39770
 
39809
39771
 
39810
- const tryAsMemoryIndex = (index) => {
39772
+ const memory_index_tryAsMemoryIndex = (index) => {
39811
39773
  debug_check `${index >= 0 && index <= MAX_MEMORY_INDEX} Incorrect memory index: ${index}!`;
39812
39774
  return opaque_asOpaqueType(index);
39813
39775
  };
@@ -39821,25 +39783,25 @@ const tryAsSbrkIndex = (index) => {
39821
39783
 
39822
39784
  const memory_consts_PAGE_SIZE_SHIFT = 12;
39823
39785
  // PAGE_SIZE has to be a power of 2
39824
- const PAGE_SIZE = 1 << memory_consts_PAGE_SIZE_SHIFT;
39786
+ const memory_consts_PAGE_SIZE = 1 << memory_consts_PAGE_SIZE_SHIFT;
39825
39787
  const MIN_ALLOCATION_SHIFT = (() => {
39826
39788
  const MIN_ALLOCATION_SHIFT = 7;
39827
39789
  debug_check `${MIN_ALLOCATION_SHIFT >= 0 && MIN_ALLOCATION_SHIFT < memory_consts_PAGE_SIZE_SHIFT} incorrect minimal allocation shift`;
39828
39790
  return MIN_ALLOCATION_SHIFT;
39829
39791
  })();
39830
- const MIN_ALLOCATION_LENGTH = PAGE_SIZE >> MIN_ALLOCATION_SHIFT;
39831
- const LAST_PAGE_NUMBER = (MEMORY_SIZE - PAGE_SIZE) / PAGE_SIZE;
39792
+ const MIN_ALLOCATION_LENGTH = memory_consts_PAGE_SIZE >> MIN_ALLOCATION_SHIFT;
39793
+ const LAST_PAGE_NUMBER = (MEMORY_SIZE - memory_consts_PAGE_SIZE) / memory_consts_PAGE_SIZE;
39832
39794
  /** https://graypaper.fluffylabs.dev/#/68eaa1f/35a60235a602?v=0.6.4 */
39833
39795
  const RESERVED_NUMBER_OF_PAGES = 16;
39834
39796
  /** https://graypaper.fluffylabs.dev/#/68eaa1f/35a60235a602?v=0.6.4 */
39835
- const MAX_NUMBER_OF_PAGES = MEMORY_SIZE / PAGE_SIZE;
39797
+ const MAX_NUMBER_OF_PAGES = MEMORY_SIZE / memory_consts_PAGE_SIZE;
39836
39798
 
39837
39799
  ;// CONCATENATED MODULE: ./packages/core/pvm-interpreter/memory/pages/page-utils.ts
39838
39800
 
39839
39801
 
39840
39802
  /** Ensure that given memory `index` is within `[0...PAGE_SIZE)` and can be used to index a page */
39841
39803
  const tryAsPageIndex = (index) => {
39842
- debug_check `${index >= 0 && index < PAGE_SIZE}, Incorect page index: ${index}!`;
39804
+ debug_check `${index >= 0 && index < memory_consts_PAGE_SIZE}, Incorect page index: ${index}!`;
39843
39805
  return opaque_asOpaqueType(index);
39844
39806
  };
39845
39807
  /** Ensure that given `index` represents an index of one of the pages. */
@@ -39867,17 +39829,17 @@ function getNextPageNumber(pageNumber) {
39867
39829
 
39868
39830
 
39869
39831
  function alignToPageSize(length) {
39870
- return PAGE_SIZE * Math.ceil(length / PAGE_SIZE);
39832
+ return memory_consts_PAGE_SIZE * Math.ceil(length / memory_consts_PAGE_SIZE);
39871
39833
  }
39872
39834
  function getPageNumber(address) {
39873
39835
  return tryAsPageNumber(address >>> memory_consts_PAGE_SIZE_SHIFT);
39874
39836
  }
39875
39837
  function getStartPageIndex(address) {
39876
- return tryAsMemoryIndex((address >>> memory_consts_PAGE_SIZE_SHIFT) << memory_consts_PAGE_SIZE_SHIFT);
39838
+ return memory_index_tryAsMemoryIndex((address >>> memory_consts_PAGE_SIZE_SHIFT) << memory_consts_PAGE_SIZE_SHIFT);
39877
39839
  }
39878
39840
  function getStartPageIndexFromPageNumber(pageNumber) {
39879
39841
  // >>> 0 is needed to avoid changing sign of the number
39880
- return tryAsMemoryIndex((pageNumber << memory_consts_PAGE_SIZE_SHIFT) >>> 0);
39842
+ return memory_index_tryAsMemoryIndex((pageNumber << memory_consts_PAGE_SIZE_SHIFT) >>> 0);
39881
39843
  }
39882
39844
 
39883
39845
  ;// CONCATENATED MODULE: ./packages/core/pvm-interpreter/memory/errors.ts
@@ -39899,7 +39861,7 @@ class PageFault {
39899
39861
  return new PageFault(numbers_tryAsU32(startPageIndex), isAccessFault);
39900
39862
  }
39901
39863
  static fromMemoryIndex(maybeMemoryIndex, isAccessFault = false) {
39902
- const memoryIndex = tryAsMemoryIndex(maybeMemoryIndex % MEMORY_SIZE);
39864
+ const memoryIndex = memory_index_tryAsMemoryIndex(maybeMemoryIndex % MEMORY_SIZE);
39903
39865
  const startPageIndex = getStartPageIndex(memoryIndex);
39904
39866
  return new PageFault(numbers_tryAsU32(startPageIndex), isAccessFault);
39905
39867
  }
@@ -39978,9 +39940,9 @@ class MemoryRange {
39978
39940
  constructor(start, length) {
39979
39941
  this.start = start;
39980
39942
  this.length = length;
39981
- this.end = tryAsMemoryIndex((this.start + this.length) % MEMORY_SIZE);
39943
+ this.end = memory_index_tryAsMemoryIndex((this.start + this.length) % MEMORY_SIZE);
39982
39944
  if (length > 0) {
39983
- this.lastIndex = tryAsMemoryIndex((this.end - 1 + MEMORY_SIZE) % MEMORY_SIZE);
39945
+ this.lastIndex = memory_index_tryAsMemoryIndex((this.end - 1 + MEMORY_SIZE) % MEMORY_SIZE);
39984
39946
  }
39985
39947
  }
39986
39948
  /** Creates a memory range from given starting point and length */
@@ -40023,7 +39985,7 @@ class MemoryRange {
40023
39985
  *
40024
39986
  * it should be in `memory-consts` but it cannot be there because of circular dependency
40025
39987
  */
40026
- const RESERVED_MEMORY_RANGE = MemoryRange.fromStartAndLength(tryAsMemoryIndex(0), RESERVED_NUMBER_OF_PAGES * PAGE_SIZE);
39988
+ const RESERVED_MEMORY_RANGE = MemoryRange.fromStartAndLength(memory_index_tryAsMemoryIndex(0), RESERVED_NUMBER_OF_PAGES * memory_consts_PAGE_SIZE);
40027
39989
 
40028
39990
  ;// CONCATENATED MODULE: ./packages/core/pvm-interpreter/memory/page-range.ts
40029
39991
 
@@ -40061,7 +40023,7 @@ class PageRange {
40061
40023
  // lastIndex is not null because we just ensured that the range is not empty
40062
40024
  const pageWithLastIndex = getPageNumber(range.lastIndex ?? range.end);
40063
40025
  const endPage = getNextPageNumber(pageWithLastIndex);
40064
- if ((startPage === endPage || startPage === pageWithLastIndex) && range.length > PAGE_SIZE) {
40026
+ if ((startPage === endPage || startPage === pageWithLastIndex) && range.length > memory_consts_PAGE_SIZE) {
40065
40027
  // full range
40066
40028
  return new PageRange(startPage, MAX_NUMBER_OF_PAGES);
40067
40029
  }
@@ -40125,8 +40087,8 @@ class ReadablePage extends MemoryPage {
40125
40087
  }
40126
40088
  loadInto(result, startIndex, length) {
40127
40089
  const endIndex = startIndex + length;
40128
- if (endIndex > PAGE_SIZE) {
40129
- return result_Result.error(PageFault.fromMemoryIndex(this.start + PAGE_SIZE), () => `Page fault: read beyond page boundary at ${this.start + PAGE_SIZE}`);
40090
+ if (endIndex > memory_consts_PAGE_SIZE) {
40091
+ return result_Result.error(PageFault.fromMemoryIndex(this.start + memory_consts_PAGE_SIZE), () => `Page fault: read beyond page boundary at ${this.start + memory_consts_PAGE_SIZE}`);
40130
40092
  }
40131
40093
  const bytes = this.data.subarray(startIndex, endIndex);
40132
40094
  // we zero the bytes, since data might not yet be initialized at `endIndex`.
@@ -40159,8 +40121,8 @@ class WriteablePage extends MemoryPage {
40159
40121
  constructor(pageNumber, initialData) {
40160
40122
  super(pageNumber);
40161
40123
  const dataLength = initialData?.length ?? 0;
40162
- const initialPageLength = Math.min(PAGE_SIZE, Math.max(dataLength, MIN_ALLOCATION_LENGTH));
40163
- this.buffer = new ArrayBuffer(initialPageLength, { maxByteLength: PAGE_SIZE });
40124
+ const initialPageLength = Math.min(memory_consts_PAGE_SIZE, Math.max(dataLength, MIN_ALLOCATION_LENGTH));
40125
+ this.buffer = new ArrayBuffer(initialPageLength, { maxByteLength: memory_consts_PAGE_SIZE });
40164
40126
  this.view = new Uint8Array(this.buffer);
40165
40127
  if (initialData !== undefined) {
40166
40128
  this.view.set(initialData);
@@ -40168,8 +40130,8 @@ class WriteablePage extends MemoryPage {
40168
40130
  }
40169
40131
  loadInto(result, startIndex, length) {
40170
40132
  const endIndex = startIndex + length;
40171
- if (endIndex > PAGE_SIZE) {
40172
- return result_Result.error(PageFault.fromMemoryIndex(this.start + PAGE_SIZE), () => `Page fault: read beyond page boundary at ${this.start + PAGE_SIZE}`);
40133
+ if (endIndex > memory_consts_PAGE_SIZE) {
40134
+ return result_Result.error(PageFault.fromMemoryIndex(this.start + memory_consts_PAGE_SIZE), () => `Page fault: read beyond page boundary at ${this.start + memory_consts_PAGE_SIZE}`);
40173
40135
  }
40174
40136
  const bytes = this.view.subarray(startIndex, endIndex);
40175
40137
  // we zero the bytes, since the view might not yet be initialized at `endIndex`.
@@ -40178,16 +40140,16 @@ class WriteablePage extends MemoryPage {
40178
40140
  return result_Result.ok(result_OK);
40179
40141
  }
40180
40142
  storeFrom(startIndex, bytes) {
40181
- if (this.buffer.byteLength < startIndex + bytes.length && this.buffer.byteLength < PAGE_SIZE) {
40182
- const newLength = Math.min(PAGE_SIZE, Math.max(MIN_ALLOCATION_LENGTH, startIndex + bytes.length));
40143
+ if (this.buffer.byteLength < startIndex + bytes.length && this.buffer.byteLength < memory_consts_PAGE_SIZE) {
40144
+ const newLength = Math.min(memory_consts_PAGE_SIZE, Math.max(MIN_ALLOCATION_LENGTH, startIndex + bytes.length));
40183
40145
  this.buffer.resize(newLength);
40184
40146
  }
40185
40147
  this.view.set(bytes, startIndex);
40186
40148
  return result_Result.ok(result_OK);
40187
40149
  }
40188
40150
  setData(pageIndex, data) {
40189
- if (this.buffer.byteLength < pageIndex + data.length && this.buffer.byteLength < PAGE_SIZE) {
40190
- const newLength = Math.min(PAGE_SIZE, Math.max(MIN_ALLOCATION_LENGTH, pageIndex + data.length));
40151
+ if (this.buffer.byteLength < pageIndex + data.length && this.buffer.byteLength < memory_consts_PAGE_SIZE) {
40152
+ const newLength = Math.min(memory_consts_PAGE_SIZE, Math.max(MIN_ALLOCATION_LENGTH, pageIndex + data.length));
40191
40153
  this.buffer.resize(newLength);
40192
40154
  }
40193
40155
  this.view.set(data, pageIndex);
@@ -40238,10 +40200,10 @@ class Memory {
40238
40200
  this.memory = memory;
40239
40201
  }
40240
40202
  store(address, bytes) {
40241
- return this.storeFrom(tryAsMemoryIndex(address), bytes);
40203
+ return this.storeFrom(memory_index_tryAsMemoryIndex(address), bytes);
40242
40204
  }
40243
40205
  read(address, output) {
40244
- return this.loadInto(output, tryAsMemoryIndex(address));
40206
+ return this.loadInto(output, memory_index_tryAsMemoryIndex(address));
40245
40207
  }
40246
40208
  reset() {
40247
40209
  this.sbrkIndex = tryAsSbrkIndex(RESERVED_MEMORY_RANGE.end);
@@ -40268,8 +40230,8 @@ class Memory {
40268
40230
  let currentPosition = address;
40269
40231
  let bytesLeft = bytes.length;
40270
40232
  for (const page of pages) {
40271
- const pageStartIndex = tryAsPageIndex(currentPosition % PAGE_SIZE);
40272
- const bytesToWrite = Math.min(PAGE_SIZE - pageStartIndex, bytesLeft);
40233
+ const pageStartIndex = tryAsPageIndex(currentPosition % memory_consts_PAGE_SIZE);
40234
+ const bytesToWrite = Math.min(memory_consts_PAGE_SIZE - pageStartIndex, bytesLeft);
40273
40235
  const sourceStartIndex = currentPosition - address;
40274
40236
  const source = bytes.subarray(sourceStartIndex, sourceStartIndex + bytesToWrite);
40275
40237
  page.storeFrom(pageStartIndex, source);
@@ -40318,8 +40280,8 @@ class Memory {
40318
40280
  let currentPosition = startAddress;
40319
40281
  let bytesLeft = result.length;
40320
40282
  for (const page of pages) {
40321
- const pageStartIndex = tryAsPageIndex(currentPosition % PAGE_SIZE);
40322
- const bytesToRead = Math.min(PAGE_SIZE - pageStartIndex, bytesLeft);
40283
+ const pageStartIndex = tryAsPageIndex(currentPosition % memory_consts_PAGE_SIZE);
40284
+ const bytesToRead = Math.min(memory_consts_PAGE_SIZE - pageStartIndex, bytesLeft);
40323
40285
  const destinationStartIndex = currentPosition - startAddress;
40324
40286
  const destination = result.subarray(destinationStartIndex);
40325
40287
  page.loadInto(destination, pageStartIndex, bytesToRead);
@@ -40346,7 +40308,7 @@ class Memory {
40346
40308
  const newSbrkIndex = tryAsSbrkIndex(alignToPageSize(newVirtualSbrkIndex));
40347
40309
  // TODO [MaSi]: `getPageNumber` works incorrectly for SbrkIndex. Sbrk index should be changed to MemoryIndex
40348
40310
  const firstPageNumber = getPageNumber(currentSbrkIndex);
40349
- const pagesToAllocate = (newSbrkIndex - currentSbrkIndex) / PAGE_SIZE;
40311
+ const pagesToAllocate = (newSbrkIndex - currentSbrkIndex) / memory_consts_PAGE_SIZE;
40350
40312
  const rangeToAllocate = PageRange.fromStartAndLength(firstPageNumber, pagesToAllocate);
40351
40313
  for (const pageNumber of rangeToAllocate) {
40352
40314
  const page = new WriteablePage(pageNumber);
@@ -40401,8 +40363,8 @@ class MemoryBuilder {
40401
40363
  setReadablePages(start, end, data = new Uint8Array()) {
40402
40364
  this.ensureNotFinalized();
40403
40365
  debug_check `${start < end} end has to be bigger than start`;
40404
- debug_check `${start % PAGE_SIZE === 0} start needs to be a multiple of page size (${PAGE_SIZE})`;
40405
- debug_check `${end % PAGE_SIZE === 0} end needs to be a multiple of page size (${PAGE_SIZE})`;
40366
+ debug_check `${start % memory_consts_PAGE_SIZE === 0} start needs to be a multiple of page size (${memory_consts_PAGE_SIZE})`;
40367
+ debug_check `${end % memory_consts_PAGE_SIZE === 0} end needs to be a multiple of page size (${memory_consts_PAGE_SIZE})`;
40406
40368
  debug_check `${data.length <= end - start} the initial data is longer than address range`;
40407
40369
  const length = end - start;
40408
40370
  const range = MemoryRange.fromStartAndLength(start, length);
@@ -40411,7 +40373,7 @@ class MemoryBuilder {
40411
40373
  const noOfPages = pages.length;
40412
40374
  for (let i = 0; i < noOfPages; i++) {
40413
40375
  const pageNumber = pages[i];
40414
- const dataChunk = data.subarray(i * PAGE_SIZE, (i + 1) * PAGE_SIZE);
40376
+ const dataChunk = data.subarray(i * memory_consts_PAGE_SIZE, (i + 1) * memory_consts_PAGE_SIZE);
40415
40377
  const page = new ReadablePage(pageNumber, dataChunk);
40416
40378
  this.initialMemory.set(pageNumber, page);
40417
40379
  }
@@ -40429,8 +40391,8 @@ class MemoryBuilder {
40429
40391
  setWriteablePages(start, end, data = new Uint8Array()) {
40430
40392
  this.ensureNotFinalized();
40431
40393
  debug_check `${start < end} end has to be bigger than start`;
40432
- debug_check `${start % PAGE_SIZE === 0} start needs to be a multiple of page size (${PAGE_SIZE})`;
40433
- debug_check `${end % PAGE_SIZE === 0} end needs to be a multiple of page size (${PAGE_SIZE})`;
40394
+ debug_check `${start % memory_consts_PAGE_SIZE === 0} start needs to be a multiple of page size (${memory_consts_PAGE_SIZE})`;
40395
+ debug_check `${end % memory_consts_PAGE_SIZE === 0} end needs to be a multiple of page size (${memory_consts_PAGE_SIZE})`;
40434
40396
  debug_check `${data.length <= end - start} the initial data is longer than address range`;
40435
40397
  const length = end - start;
40436
40398
  const range = MemoryRange.fromStartAndLength(start, length);
@@ -40439,7 +40401,7 @@ class MemoryBuilder {
40439
40401
  const noOfPages = pages.length;
40440
40402
  for (let i = 0; i < noOfPages; i++) {
40441
40403
  const pageNumber = pages[i];
40442
- const dataChunk = data.subarray(i * PAGE_SIZE, (i + 1) * PAGE_SIZE);
40404
+ const dataChunk = data.subarray(i * memory_consts_PAGE_SIZE, (i + 1) * memory_consts_PAGE_SIZE);
40443
40405
  const page = new WriteablePage(pageNumber, dataChunk);
40444
40406
  this.initialMemory.set(pageNumber, page);
40445
40407
  }
@@ -40451,8 +40413,8 @@ class MemoryBuilder {
40451
40413
  */
40452
40414
  setData(start, data) {
40453
40415
  this.ensureNotFinalized();
40454
- const pageOffset = start % PAGE_SIZE;
40455
- const remainingSpaceOnPage = PAGE_SIZE - pageOffset;
40416
+ const pageOffset = start % memory_consts_PAGE_SIZE;
40417
+ const remainingSpaceOnPage = memory_consts_PAGE_SIZE - pageOffset;
40456
40418
  debug_check `${data.length <= remainingSpaceOnPage} The data has to fit into a single page.`;
40457
40419
  const length = data.length;
40458
40420
  const range = MemoryRange.fromStartAndLength(start, length);
@@ -40643,27 +40605,27 @@ class Program {
40643
40605
  static fromSpi(blob, args, hasMetadata) {
40644
40606
  const { code: spiCode, metadata } = hasMetadata ? extractCodeAndMetadata(blob) : { code: blob };
40645
40607
  const { code, memory: rawMemory, registers } = decodeStandardProgram(spiCode, args);
40646
- const regs = new Registers();
40608
+ const regs = new registers_Registers();
40647
40609
  regs.copyFrom(registers);
40648
40610
  const memoryBuilder = new MemoryBuilder();
40649
40611
  for (const { start, end, data } of rawMemory.readable) {
40650
- const startIndex = tryAsMemoryIndex(start);
40651
- const endIndex = tryAsMemoryIndex(end);
40612
+ const startIndex = memory_index_tryAsMemoryIndex(start);
40613
+ const endIndex = memory_index_tryAsMemoryIndex(end);
40652
40614
  memoryBuilder.setReadablePages(startIndex, endIndex, data ?? new Uint8Array());
40653
40615
  }
40654
40616
  for (const { start, end, data } of rawMemory.writeable) {
40655
- const startIndex = tryAsMemoryIndex(start);
40656
- const endIndex = tryAsMemoryIndex(end);
40617
+ const startIndex = memory_index_tryAsMemoryIndex(start);
40618
+ const endIndex = memory_index_tryAsMemoryIndex(end);
40657
40619
  memoryBuilder.setWriteablePages(startIndex, endIndex, data ?? new Uint8Array());
40658
40620
  }
40659
- const heapStart = tryAsMemoryIndex(rawMemory.sbrkIndex);
40621
+ const heapStart = memory_index_tryAsMemoryIndex(rawMemory.sbrkIndex);
40660
40622
  const heapEnd = tryAsSbrkIndex(rawMemory.heapEnd);
40661
40623
  const memory = memoryBuilder.finalize(heapStart, heapEnd);
40662
40624
  return new Program(code, regs, memory, metadata);
40663
40625
  }
40664
40626
  static fromGeneric(blob, hasMetadata) {
40665
40627
  const { code, metadata } = hasMetadata ? extractCodeAndMetadata(blob) : { code: blob };
40666
- const regs = new Registers();
40628
+ const regs = new registers_Registers();
40667
40629
  const memory = new Memory();
40668
40630
  return new Program(code, regs, memory, metadata);
40669
40631
  }
@@ -41678,6 +41640,45 @@ class BasicBlocks {
41678
41640
  ;// CONCATENATED MODULE: ./packages/core/pvm-interpreter/basic-blocks/index.ts
41679
41641
 
41680
41642
 
41643
+ ;// CONCATENATED MODULE: ./packages/core/pvm-interpreter/gas.ts
41644
+
41645
+
41646
+ /** Create a new gas counter instance depending on the gas value. */
41647
+ function gasCounter(gas) {
41648
+ return new GasCounterU64(numbers_tryAsU64(gas));
41649
+ }
41650
+ class GasCounterU64 {
41651
+ gas;
41652
+ initialGas;
41653
+ constructor(gas) {
41654
+ this.gas = gas;
41655
+ this.initialGas = gas_tryAsGas(gas);
41656
+ }
41657
+ set(g) {
41658
+ this.gas = numbers_tryAsU64(g);
41659
+ }
41660
+ get() {
41661
+ return gas_tryAsGas(this.gas);
41662
+ }
41663
+ sub(g) {
41664
+ const result = this.gas - numbers_tryAsU64(g);
41665
+ if (result >= 0n) {
41666
+ this.gas = numbers_tryAsU64(result);
41667
+ return false;
41668
+ }
41669
+ this.gas = numbers_tryAsU64(0n);
41670
+ return true;
41671
+ }
41672
+ used() {
41673
+ const gasConsumed = numbers_tryAsU64(this.initialGas) - this.gas;
41674
+ // In we have less than zero left we assume that all gas has been consumed.
41675
+ if (gasConsumed < 0) {
41676
+ return this.initialGas;
41677
+ }
41678
+ return gas_tryAsGas(gasConsumed);
41679
+ }
41680
+ }
41681
+
41681
41682
  ;// CONCATENATED MODULE: ./packages/core/pvm-interpreter/instruction-gas-map.ts
41682
41683
 
41683
41684
 
@@ -42254,7 +42255,7 @@ class LoadOps {
42254
42255
  }
42255
42256
  loadNumber(address, registerIndex, numberLength) {
42256
42257
  const registerBytes = this.regs.getBytesAsLittleEndian(registerIndex, REG_SIZE_BYTES);
42257
- const loadResult = this.memory.loadInto(registerBytes.subarray(0, numberLength), tryAsMemoryIndex(address));
42258
+ const loadResult = this.memory.loadInto(registerBytes.subarray(0, numberLength), memory_index_tryAsMemoryIndex(address));
42258
42259
  if (loadResult.isError) {
42259
42260
  if (loadResult.error.isAccessFault) {
42260
42261
  this.instructionResult.status = pvm_interpreter_result_Result.FAULT_ACCESS;
@@ -42270,7 +42271,7 @@ class LoadOps {
42270
42271
  loadSignedNumber(address, registerIndex, numberLength) {
42271
42272
  // load all bytes from register to correctly handle the sign.
42272
42273
  const registerBytes = this.regs.getBytesAsLittleEndian(registerIndex, REG_SIZE_BYTES);
42273
- const loadResult = this.memory.loadInto(registerBytes.subarray(0, numberLength), tryAsMemoryIndex(address));
42274
+ const loadResult = this.memory.loadInto(registerBytes.subarray(0, numberLength), memory_index_tryAsMemoryIndex(address));
42274
42275
  if (loadResult.isError) {
42275
42276
  if (loadResult.error.isAccessFault) {
42276
42277
  this.instructionResult.status = pvm_interpreter_result_Result.FAULT_ACCESS;
@@ -42692,7 +42693,7 @@ class StoreOps {
42692
42693
  this.store(address, secondImmediateDecoder.getExtendedBytesAsLittleEndian());
42693
42694
  }
42694
42695
  store(address, bytes) {
42695
- const storeResult = this.memory.storeFrom(tryAsMemoryIndex(address), bytes);
42696
+ const storeResult = this.memory.storeFrom(memory_index_tryAsMemoryIndex(address), bytes);
42696
42697
  if (storeResult.isOk) {
42697
42698
  return;
42698
42699
  }
@@ -42701,7 +42702,7 @@ class StoreOps {
42701
42702
  }
42702
42703
  else {
42703
42704
  this.instructionResult.status = pvm_interpreter_result_Result.FAULT;
42704
- this.instructionResult.exitParam = getStartPageIndex(tryAsMemoryIndex(storeResult.error.address));
42705
+ this.instructionResult.exitParam = getStartPageIndex(memory_index_tryAsMemoryIndex(storeResult.error.address));
42705
42706
  }
42706
42707
  }
42707
42708
  }
@@ -43500,11 +43501,11 @@ class ProgramDecoder {
43500
43501
 
43501
43502
 
43502
43503
  const interpreter_logger = logger_Logger.new(import.meta.filename, "pvm");
43503
- class Interpreter {
43504
+ class interpreter_Interpreter {
43504
43505
  useSbrkGas;
43505
- registers = new Registers();
43506
+ registers = new registers_Registers();
43506
43507
  memory = new Memory();
43507
- gas = gasCounter(tryAsGas(0));
43508
+ gas = gasCounter(gas_tryAsGas(0));
43508
43509
  code = new Uint8Array();
43509
43510
  mask = Mask.empty();
43510
43511
  pc = 0;
@@ -43638,8 +43639,8 @@ class Interpreter {
43638
43639
  break;
43639
43640
  case ArgumentType.TWO_REGISTERS:
43640
43641
  if (this.useSbrkGas && currentInstruction === Instruction.SBRK) {
43641
- const calculateSbrkCost = (length) => (alignToPageSize(length) / PAGE_SIZE) * 16;
43642
- const underflow = this.gas.sub(tryAsGas(calculateSbrkCost(this.registers.getLowerU32(argsResult.firstRegisterIndex))));
43642
+ const calculateSbrkCost = (length) => (alignToPageSize(length) / memory_consts_PAGE_SIZE) * 16;
43643
+ const underflow = this.gas.sub(gas_tryAsGas(calculateSbrkCost(this.registers.getLowerU32(argsResult.firstRegisterIndex))));
43643
43644
  if (underflow) {
43644
43645
  this.status = status_Status.OOG;
43645
43646
  return this.status;
@@ -43734,12 +43735,88 @@ class Interpreter {
43734
43735
  }
43735
43736
  }
43736
43737
 
43738
+ ;// CONCATENATED MODULE: ./packages/core/pvm-interpreter/debugger-adapter.ts
43739
+
43740
+
43741
+
43742
+
43743
+
43744
+
43745
+ class DebuggerAdapter {
43746
+ pvm;
43747
+ constructor(useSbrkGas = false) {
43748
+ this.pvm = new Interpreter({ useSbrkGas });
43749
+ }
43750
+ resetGeneric(rawProgram, flatRegisters, initialGas) {
43751
+ this.pvm.resetGeneric(rawProgram, 0, tryAsGas(initialGas), new Registers(flatRegisters));
43752
+ }
43753
+ reset(rawProgram, pc, gas, maybeRegisters, maybeMemory) {
43754
+ this.pvm.resetGeneric(rawProgram, pc, tryAsGas(gas), maybeRegisters, maybeMemory);
43755
+ }
43756
+ getPageDump(pageNumber) {
43757
+ const page = this.pvm.getMemoryPage(pageNumber);
43758
+ if (page === null) {
43759
+ // page wasn't allocated so we return an empty page
43760
+ return safeAllocUint8Array(PAGE_SIZE);
43761
+ }
43762
+ if (page.length === PAGE_SIZE) {
43763
+ // page was allocated and has a proper size so we can simply return it
43764
+ return page;
43765
+ }
43766
+ // page was allocated but it is shorter than PAGE_SIZE so we have to extend it
43767
+ const fullPage = safeAllocUint8Array(PAGE_SIZE);
43768
+ fullPage.set(page);
43769
+ return fullPage;
43770
+ }
43771
+ setMemory(address, value) {
43772
+ this.pvm.memory.storeFrom(tryAsMemoryIndex(address), value);
43773
+ }
43774
+ getExitArg() {
43775
+ return this.pvm.getExitParam() ?? 0;
43776
+ }
43777
+ getStatus() {
43778
+ return this.pvm.getStatus();
43779
+ }
43780
+ nextStep() {
43781
+ return this.pvm.nextStep() === Status.OK;
43782
+ }
43783
+ nSteps(steps) {
43784
+ check `${steps >>> 0 > 0} Expected a positive integer got ${steps}`;
43785
+ for (let i = 0; i < steps; i++) {
43786
+ const isOk = this.nextStep();
43787
+ if (!isOk) {
43788
+ return false;
43789
+ }
43790
+ }
43791
+ return true;
43792
+ }
43793
+ getRegisters() {
43794
+ return this.pvm.registers.getAllU64();
43795
+ }
43796
+ setRegisters(registers) {
43797
+ this.pvm.registers.copyFrom(new Registers(registers));
43798
+ }
43799
+ getProgramCounter() {
43800
+ return this.pvm.getPC();
43801
+ }
43802
+ setNextProgramCounter(nextPc) {
43803
+ this.pvm.setNextPC(nextPc);
43804
+ }
43805
+ getGasLeft() {
43806
+ return BigInt(this.pvm.gas.get());
43807
+ }
43808
+ setGasLeft(gas) {
43809
+ this.pvm.gas.set(tryAsGas(gas));
43810
+ }
43811
+ }
43812
+
43737
43813
  ;// CONCATENATED MODULE: ./packages/core/pvm-interpreter/index.ts
43738
43814
 
43739
43815
 
43740
43816
 
43741
43817
 
43742
43818
 
43819
+
43743
43820
  ;// CONCATENATED MODULE: ./node_modules/@fluffylabs/anan-as/build/debug-raw.js
43744
43821
  async function instantiate(module, imports = {}) {
43745
43822
  const adaptedImports = {
@@ -44193,12 +44270,12 @@ class AnanasMemory {
44193
44270
  }
44194
44271
  class AnanasGasCounter {
44195
44272
  instance;
44196
- initialGas = tryAsGas(0n);
44273
+ initialGas = gas_tryAsGas(0n);
44197
44274
  constructor(instance) {
44198
44275
  this.instance = instance;
44199
44276
  }
44200
44277
  get() {
44201
- return tryAsGas(this.instance.getGasLeft());
44278
+ return gas_tryAsGas(this.instance.getGasLeft());
44202
44279
  }
44203
44280
  set(g) {
44204
44281
  this.instance.setGasLeft(BigInt(g));
@@ -44303,7 +44380,7 @@ class InterpreterInstanceManager {
44303
44380
  const instances = [];
44304
44381
  switch (interpreter) {
44305
44382
  case PvmBackend.BuiltIn:
44306
- instances.push(new Interpreter({
44383
+ instances.push(new interpreter_Interpreter({
44307
44384
  useSbrkGas: false,
44308
44385
  }));
44309
44386
  break;
@@ -46759,7 +46836,7 @@ class Transfer {
46759
46836
  */
46760
46837
  basicGasCost = Compatibility.isGreaterOrEqual(GpVersion.V0_7_2)
46761
46838
  ? gas_tryAsSmallGas(10)
46762
- : (regs) => tryAsGas(10n + regs.get(TRANSFER_GAS_FEE_REG));
46839
+ : (regs) => gas_tryAsGas(10n + regs.get(TRANSFER_GAS_FEE_REG));
46763
46840
  tracedRegisters = traceRegisters(transfer_IN_OUT_REG, AMOUNT_REG, TRANSFER_GAS_FEE_REG, MEMO_START_REG);
46764
46841
  constructor(currentServiceId, partialState) {
46765
46842
  this.currentServiceId = currentServiceId;
@@ -46787,7 +46864,7 @@ class Transfer {
46787
46864
  if (transferResult.isOk) {
46788
46865
  if (Compatibility.isGreaterOrEqual(GpVersion.V0_7_2)) {
46789
46866
  // substracting value `t`
46790
- const underflow = gas.sub(tryAsGas(transferGasFee));
46867
+ const underflow = gas.sub(gas_tryAsGas(transferGasFee));
46791
46868
  if (underflow) {
46792
46869
  return PvmExecution.OOG;
46793
46870
  }
@@ -47532,7 +47609,7 @@ class Accumulate {
47532
47609
  serviceId,
47533
47610
  argsLength: numbers_tryAsU32(transfers.length + operands.length),
47534
47611
  });
47535
- const result = await executor.run(invocationArgs, tryAsGas(gas));
47612
+ const result = await executor.run(invocationArgs, gas_tryAsGas(gas));
47536
47613
  const [newState, checkpoint] = partialState.getStateUpdates();
47537
47614
  /**
47538
47615
  * PVM invocation returned and error so we return the checkpoint
@@ -47936,7 +48013,7 @@ class DeferredTransfers {
47936
48013
  partiallyUpdatedState.updateServiceInfo(serviceId, newInfo);
47937
48014
  const partialState = new AccumulateExternalities(this.chainSpec, this.blake2b, partiallyUpdatedState, serviceId, serviceId, timeslot);
47938
48015
  const fetchExternalities = FetchExternalities.createForOnTransfer({ entropy, transfers }, this.chainSpec);
47939
- let consumedGas = tryAsGas(0);
48016
+ let consumedGas = gas_tryAsGas(0);
47940
48017
  const hasTransfers = transfers.length > 0;
47941
48018
  const isCodeCorrect = code !== null && code.length <= W_C;
47942
48019
  if (!hasTransfers || !isCodeCorrect) {
@@ -47954,7 +48031,7 @@ class DeferredTransfers {
47954
48031
  const executor = await PvmExecutor.createOnTransferExecutor(serviceId, code, { partialState, fetchExternalities }, this.pvm);
47955
48032
  const args = encoder_Encoder.encodeObject(deferred_transfers_ARGS_CODEC, { timeslot, serviceId, transfersLength: numbers_tryAsU32(transfers.length) }, this.chainSpec);
47956
48033
  const gas = transfers.reduce((acc, item) => acc + item.gas, 0n);
47957
- consumedGas = (await executor.run(args, tryAsGas(gas))).consumedGas;
48034
+ consumedGas = (await executor.run(args, gas_tryAsGas(gas))).consumedGas;
47958
48035
  }
47959
48036
  transferStatistics.set(serviceId, { count: numbers_tryAsU32(transfers.length), gasUsed: tryAsServiceGas(consumedGas) });
47960
48037
  const [updatedState] = partialState.getStateUpdates();