@typeberry/jam 0.4.0-2473e55 → 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;
@@ -39146,7 +39150,7 @@ const gas_tryAsSmallGas = (v) => opaque_asOpaqueType(numbers_tryAsU32(v));
39146
39150
  /** Attempt to convert given number into U64 gas representation. */
39147
39151
  const tryAsBigGas = (v) => opaque_asOpaqueType(numbers_tryAsU64(v));
39148
39152
  /** Attempt to convert given number into gas. */
39149
- 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);
39150
39154
 
39151
39155
  ;// CONCATENATED MODULE: ./packages/core/pvm-interface/memory.ts
39152
39156
 
@@ -39429,7 +39433,7 @@ const tryAsRegisterIndex = (index) => {
39429
39433
  debug_check `${index >= 0 && index < registers_NO_OF_REGISTERS} Incorrect register index: ${index}!`;
39430
39434
  return opaque_asOpaqueType(index);
39431
39435
  };
39432
- class Registers {
39436
+ class registers_Registers {
39433
39437
  bytes;
39434
39438
  asSigned;
39435
39439
  asUnsigned;
@@ -39448,7 +39452,7 @@ class Registers {
39448
39452
  }
39449
39453
  static fromBytes(bytes) {
39450
39454
  debug_check `${bytes.length === registers_NO_OF_REGISTERS << REGISTER_SIZE_SHIFT} Invalid size of registers array.`;
39451
- return new Registers(bytes);
39455
+ return new registers_Registers(bytes);
39452
39456
  }
39453
39457
  getBytesAsLittleEndian(index, len) {
39454
39458
  const offset = index << REGISTER_SIZE_SHIFT;
@@ -39762,49 +39766,10 @@ class NoopMissing {
39762
39766
  }
39763
39767
  }
39764
39768
 
39765
- ;// CONCATENATED MODULE: ./packages/core/pvm-interpreter/gas.ts
39766
-
39767
-
39768
- /** Create a new gas counter instance depending on the gas value. */
39769
- function gasCounter(gas) {
39770
- return new GasCounterU64(numbers_tryAsU64(gas));
39771
- }
39772
- class GasCounterU64 {
39773
- gas;
39774
- initialGas;
39775
- constructor(gas) {
39776
- this.gas = gas;
39777
- this.initialGas = tryAsGas(gas);
39778
- }
39779
- set(g) {
39780
- this.gas = numbers_tryAsU64(g);
39781
- }
39782
- get() {
39783
- return tryAsGas(this.gas);
39784
- }
39785
- sub(g) {
39786
- const result = this.gas - numbers_tryAsU64(g);
39787
- if (result >= 0n) {
39788
- this.gas = numbers_tryAsU64(result);
39789
- return false;
39790
- }
39791
- this.gas = numbers_tryAsU64(0n);
39792
- return true;
39793
- }
39794
- used() {
39795
- const gasConsumed = numbers_tryAsU64(this.initialGas) - this.gas;
39796
- // In we have less than zero left we assume that all gas has been consumed.
39797
- if (gasConsumed < 0) {
39798
- return this.initialGas;
39799
- }
39800
- return tryAsGas(gasConsumed);
39801
- }
39802
- }
39803
-
39804
39769
  ;// CONCATENATED MODULE: ./packages/core/pvm-interpreter/memory/memory-index.ts
39805
39770
 
39806
39771
 
39807
- const tryAsMemoryIndex = (index) => {
39772
+ const memory_index_tryAsMemoryIndex = (index) => {
39808
39773
  debug_check `${index >= 0 && index <= MAX_MEMORY_INDEX} Incorrect memory index: ${index}!`;
39809
39774
  return opaque_asOpaqueType(index);
39810
39775
  };
@@ -39818,25 +39783,25 @@ const tryAsSbrkIndex = (index) => {
39818
39783
 
39819
39784
  const memory_consts_PAGE_SIZE_SHIFT = 12;
39820
39785
  // PAGE_SIZE has to be a power of 2
39821
- const PAGE_SIZE = 1 << memory_consts_PAGE_SIZE_SHIFT;
39786
+ const memory_consts_PAGE_SIZE = 1 << memory_consts_PAGE_SIZE_SHIFT;
39822
39787
  const MIN_ALLOCATION_SHIFT = (() => {
39823
39788
  const MIN_ALLOCATION_SHIFT = 7;
39824
39789
  debug_check `${MIN_ALLOCATION_SHIFT >= 0 && MIN_ALLOCATION_SHIFT < memory_consts_PAGE_SIZE_SHIFT} incorrect minimal allocation shift`;
39825
39790
  return MIN_ALLOCATION_SHIFT;
39826
39791
  })();
39827
- const MIN_ALLOCATION_LENGTH = PAGE_SIZE >> MIN_ALLOCATION_SHIFT;
39828
- 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;
39829
39794
  /** https://graypaper.fluffylabs.dev/#/68eaa1f/35a60235a602?v=0.6.4 */
39830
39795
  const RESERVED_NUMBER_OF_PAGES = 16;
39831
39796
  /** https://graypaper.fluffylabs.dev/#/68eaa1f/35a60235a602?v=0.6.4 */
39832
- const MAX_NUMBER_OF_PAGES = MEMORY_SIZE / PAGE_SIZE;
39797
+ const MAX_NUMBER_OF_PAGES = MEMORY_SIZE / memory_consts_PAGE_SIZE;
39833
39798
 
39834
39799
  ;// CONCATENATED MODULE: ./packages/core/pvm-interpreter/memory/pages/page-utils.ts
39835
39800
 
39836
39801
 
39837
39802
  /** Ensure that given memory `index` is within `[0...PAGE_SIZE)` and can be used to index a page */
39838
39803
  const tryAsPageIndex = (index) => {
39839
- 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}!`;
39840
39805
  return opaque_asOpaqueType(index);
39841
39806
  };
39842
39807
  /** Ensure that given `index` represents an index of one of the pages. */
@@ -39864,17 +39829,17 @@ function getNextPageNumber(pageNumber) {
39864
39829
 
39865
39830
 
39866
39831
  function alignToPageSize(length) {
39867
- return PAGE_SIZE * Math.ceil(length / PAGE_SIZE);
39832
+ return memory_consts_PAGE_SIZE * Math.ceil(length / memory_consts_PAGE_SIZE);
39868
39833
  }
39869
39834
  function getPageNumber(address) {
39870
39835
  return tryAsPageNumber(address >>> memory_consts_PAGE_SIZE_SHIFT);
39871
39836
  }
39872
39837
  function getStartPageIndex(address) {
39873
- 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);
39874
39839
  }
39875
39840
  function getStartPageIndexFromPageNumber(pageNumber) {
39876
39841
  // >>> 0 is needed to avoid changing sign of the number
39877
- return tryAsMemoryIndex((pageNumber << memory_consts_PAGE_SIZE_SHIFT) >>> 0);
39842
+ return memory_index_tryAsMemoryIndex((pageNumber << memory_consts_PAGE_SIZE_SHIFT) >>> 0);
39878
39843
  }
39879
39844
 
39880
39845
  ;// CONCATENATED MODULE: ./packages/core/pvm-interpreter/memory/errors.ts
@@ -39896,7 +39861,7 @@ class PageFault {
39896
39861
  return new PageFault(numbers_tryAsU32(startPageIndex), isAccessFault);
39897
39862
  }
39898
39863
  static fromMemoryIndex(maybeMemoryIndex, isAccessFault = false) {
39899
- const memoryIndex = tryAsMemoryIndex(maybeMemoryIndex % MEMORY_SIZE);
39864
+ const memoryIndex = memory_index_tryAsMemoryIndex(maybeMemoryIndex % MEMORY_SIZE);
39900
39865
  const startPageIndex = getStartPageIndex(memoryIndex);
39901
39866
  return new PageFault(numbers_tryAsU32(startPageIndex), isAccessFault);
39902
39867
  }
@@ -39975,9 +39940,9 @@ class MemoryRange {
39975
39940
  constructor(start, length) {
39976
39941
  this.start = start;
39977
39942
  this.length = length;
39978
- this.end = tryAsMemoryIndex((this.start + this.length) % MEMORY_SIZE);
39943
+ this.end = memory_index_tryAsMemoryIndex((this.start + this.length) % MEMORY_SIZE);
39979
39944
  if (length > 0) {
39980
- this.lastIndex = tryAsMemoryIndex((this.end - 1 + MEMORY_SIZE) % MEMORY_SIZE);
39945
+ this.lastIndex = memory_index_tryAsMemoryIndex((this.end - 1 + MEMORY_SIZE) % MEMORY_SIZE);
39981
39946
  }
39982
39947
  }
39983
39948
  /** Creates a memory range from given starting point and length */
@@ -40020,7 +39985,7 @@ class MemoryRange {
40020
39985
  *
40021
39986
  * it should be in `memory-consts` but it cannot be there because of circular dependency
40022
39987
  */
40023
- 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);
40024
39989
 
40025
39990
  ;// CONCATENATED MODULE: ./packages/core/pvm-interpreter/memory/page-range.ts
40026
39991
 
@@ -40058,7 +40023,7 @@ class PageRange {
40058
40023
  // lastIndex is not null because we just ensured that the range is not empty
40059
40024
  const pageWithLastIndex = getPageNumber(range.lastIndex ?? range.end);
40060
40025
  const endPage = getNextPageNumber(pageWithLastIndex);
40061
- if ((startPage === endPage || startPage === pageWithLastIndex) && range.length > PAGE_SIZE) {
40026
+ if ((startPage === endPage || startPage === pageWithLastIndex) && range.length > memory_consts_PAGE_SIZE) {
40062
40027
  // full range
40063
40028
  return new PageRange(startPage, MAX_NUMBER_OF_PAGES);
40064
40029
  }
@@ -40122,8 +40087,8 @@ class ReadablePage extends MemoryPage {
40122
40087
  }
40123
40088
  loadInto(result, startIndex, length) {
40124
40089
  const endIndex = startIndex + length;
40125
- if (endIndex > PAGE_SIZE) {
40126
- 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}`);
40127
40092
  }
40128
40093
  const bytes = this.data.subarray(startIndex, endIndex);
40129
40094
  // we zero the bytes, since data might not yet be initialized at `endIndex`.
@@ -40156,8 +40121,8 @@ class WriteablePage extends MemoryPage {
40156
40121
  constructor(pageNumber, initialData) {
40157
40122
  super(pageNumber);
40158
40123
  const dataLength = initialData?.length ?? 0;
40159
- const initialPageLength = Math.min(PAGE_SIZE, Math.max(dataLength, MIN_ALLOCATION_LENGTH));
40160
- 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 });
40161
40126
  this.view = new Uint8Array(this.buffer);
40162
40127
  if (initialData !== undefined) {
40163
40128
  this.view.set(initialData);
@@ -40165,8 +40130,8 @@ class WriteablePage extends MemoryPage {
40165
40130
  }
40166
40131
  loadInto(result, startIndex, length) {
40167
40132
  const endIndex = startIndex + length;
40168
- if (endIndex > PAGE_SIZE) {
40169
- 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}`);
40170
40135
  }
40171
40136
  const bytes = this.view.subarray(startIndex, endIndex);
40172
40137
  // we zero the bytes, since the view might not yet be initialized at `endIndex`.
@@ -40175,16 +40140,16 @@ class WriteablePage extends MemoryPage {
40175
40140
  return result_Result.ok(result_OK);
40176
40141
  }
40177
40142
  storeFrom(startIndex, bytes) {
40178
- if (this.buffer.byteLength < startIndex + bytes.length && this.buffer.byteLength < PAGE_SIZE) {
40179
- 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));
40180
40145
  this.buffer.resize(newLength);
40181
40146
  }
40182
40147
  this.view.set(bytes, startIndex);
40183
40148
  return result_Result.ok(result_OK);
40184
40149
  }
40185
40150
  setData(pageIndex, data) {
40186
- if (this.buffer.byteLength < pageIndex + data.length && this.buffer.byteLength < PAGE_SIZE) {
40187
- 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));
40188
40153
  this.buffer.resize(newLength);
40189
40154
  }
40190
40155
  this.view.set(data, pageIndex);
@@ -40235,10 +40200,10 @@ class Memory {
40235
40200
  this.memory = memory;
40236
40201
  }
40237
40202
  store(address, bytes) {
40238
- return this.storeFrom(tryAsMemoryIndex(address), bytes);
40203
+ return this.storeFrom(memory_index_tryAsMemoryIndex(address), bytes);
40239
40204
  }
40240
40205
  read(address, output) {
40241
- return this.loadInto(output, tryAsMemoryIndex(address));
40206
+ return this.loadInto(output, memory_index_tryAsMemoryIndex(address));
40242
40207
  }
40243
40208
  reset() {
40244
40209
  this.sbrkIndex = tryAsSbrkIndex(RESERVED_MEMORY_RANGE.end);
@@ -40265,8 +40230,8 @@ class Memory {
40265
40230
  let currentPosition = address;
40266
40231
  let bytesLeft = bytes.length;
40267
40232
  for (const page of pages) {
40268
- const pageStartIndex = tryAsPageIndex(currentPosition % PAGE_SIZE);
40269
- 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);
40270
40235
  const sourceStartIndex = currentPosition - address;
40271
40236
  const source = bytes.subarray(sourceStartIndex, sourceStartIndex + bytesToWrite);
40272
40237
  page.storeFrom(pageStartIndex, source);
@@ -40315,8 +40280,8 @@ class Memory {
40315
40280
  let currentPosition = startAddress;
40316
40281
  let bytesLeft = result.length;
40317
40282
  for (const page of pages) {
40318
- const pageStartIndex = tryAsPageIndex(currentPosition % PAGE_SIZE);
40319
- 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);
40320
40285
  const destinationStartIndex = currentPosition - startAddress;
40321
40286
  const destination = result.subarray(destinationStartIndex);
40322
40287
  page.loadInto(destination, pageStartIndex, bytesToRead);
@@ -40343,7 +40308,7 @@ class Memory {
40343
40308
  const newSbrkIndex = tryAsSbrkIndex(alignToPageSize(newVirtualSbrkIndex));
40344
40309
  // TODO [MaSi]: `getPageNumber` works incorrectly for SbrkIndex. Sbrk index should be changed to MemoryIndex
40345
40310
  const firstPageNumber = getPageNumber(currentSbrkIndex);
40346
- const pagesToAllocate = (newSbrkIndex - currentSbrkIndex) / PAGE_SIZE;
40311
+ const pagesToAllocate = (newSbrkIndex - currentSbrkIndex) / memory_consts_PAGE_SIZE;
40347
40312
  const rangeToAllocate = PageRange.fromStartAndLength(firstPageNumber, pagesToAllocate);
40348
40313
  for (const pageNumber of rangeToAllocate) {
40349
40314
  const page = new WriteablePage(pageNumber);
@@ -40398,8 +40363,8 @@ class MemoryBuilder {
40398
40363
  setReadablePages(start, end, data = new Uint8Array()) {
40399
40364
  this.ensureNotFinalized();
40400
40365
  debug_check `${start < end} end has to be bigger than start`;
40401
- debug_check `${start % PAGE_SIZE === 0} start needs to be a multiple of page size (${PAGE_SIZE})`;
40402
- 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})`;
40403
40368
  debug_check `${data.length <= end - start} the initial data is longer than address range`;
40404
40369
  const length = end - start;
40405
40370
  const range = MemoryRange.fromStartAndLength(start, length);
@@ -40408,7 +40373,7 @@ class MemoryBuilder {
40408
40373
  const noOfPages = pages.length;
40409
40374
  for (let i = 0; i < noOfPages; i++) {
40410
40375
  const pageNumber = pages[i];
40411
- 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);
40412
40377
  const page = new ReadablePage(pageNumber, dataChunk);
40413
40378
  this.initialMemory.set(pageNumber, page);
40414
40379
  }
@@ -40426,8 +40391,8 @@ class MemoryBuilder {
40426
40391
  setWriteablePages(start, end, data = new Uint8Array()) {
40427
40392
  this.ensureNotFinalized();
40428
40393
  debug_check `${start < end} end has to be bigger than start`;
40429
- debug_check `${start % PAGE_SIZE === 0} start needs to be a multiple of page size (${PAGE_SIZE})`;
40430
- 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})`;
40431
40396
  debug_check `${data.length <= end - start} the initial data is longer than address range`;
40432
40397
  const length = end - start;
40433
40398
  const range = MemoryRange.fromStartAndLength(start, length);
@@ -40436,7 +40401,7 @@ class MemoryBuilder {
40436
40401
  const noOfPages = pages.length;
40437
40402
  for (let i = 0; i < noOfPages; i++) {
40438
40403
  const pageNumber = pages[i];
40439
- 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);
40440
40405
  const page = new WriteablePage(pageNumber, dataChunk);
40441
40406
  this.initialMemory.set(pageNumber, page);
40442
40407
  }
@@ -40448,8 +40413,8 @@ class MemoryBuilder {
40448
40413
  */
40449
40414
  setData(start, data) {
40450
40415
  this.ensureNotFinalized();
40451
- const pageOffset = start % PAGE_SIZE;
40452
- const remainingSpaceOnPage = PAGE_SIZE - pageOffset;
40416
+ const pageOffset = start % memory_consts_PAGE_SIZE;
40417
+ const remainingSpaceOnPage = memory_consts_PAGE_SIZE - pageOffset;
40453
40418
  debug_check `${data.length <= remainingSpaceOnPage} The data has to fit into a single page.`;
40454
40419
  const length = data.length;
40455
40420
  const range = MemoryRange.fromStartAndLength(start, length);
@@ -40640,27 +40605,27 @@ class Program {
40640
40605
  static fromSpi(blob, args, hasMetadata) {
40641
40606
  const { code: spiCode, metadata } = hasMetadata ? extractCodeAndMetadata(blob) : { code: blob };
40642
40607
  const { code, memory: rawMemory, registers } = decodeStandardProgram(spiCode, args);
40643
- const regs = new Registers();
40608
+ const regs = new registers_Registers();
40644
40609
  regs.copyFrom(registers);
40645
40610
  const memoryBuilder = new MemoryBuilder();
40646
40611
  for (const { start, end, data } of rawMemory.readable) {
40647
- const startIndex = tryAsMemoryIndex(start);
40648
- const endIndex = tryAsMemoryIndex(end);
40612
+ const startIndex = memory_index_tryAsMemoryIndex(start);
40613
+ const endIndex = memory_index_tryAsMemoryIndex(end);
40649
40614
  memoryBuilder.setReadablePages(startIndex, endIndex, data ?? new Uint8Array());
40650
40615
  }
40651
40616
  for (const { start, end, data } of rawMemory.writeable) {
40652
- const startIndex = tryAsMemoryIndex(start);
40653
- const endIndex = tryAsMemoryIndex(end);
40617
+ const startIndex = memory_index_tryAsMemoryIndex(start);
40618
+ const endIndex = memory_index_tryAsMemoryIndex(end);
40654
40619
  memoryBuilder.setWriteablePages(startIndex, endIndex, data ?? new Uint8Array());
40655
40620
  }
40656
- const heapStart = tryAsMemoryIndex(rawMemory.sbrkIndex);
40621
+ const heapStart = memory_index_tryAsMemoryIndex(rawMemory.sbrkIndex);
40657
40622
  const heapEnd = tryAsSbrkIndex(rawMemory.heapEnd);
40658
40623
  const memory = memoryBuilder.finalize(heapStart, heapEnd);
40659
40624
  return new Program(code, regs, memory, metadata);
40660
40625
  }
40661
40626
  static fromGeneric(blob, hasMetadata) {
40662
40627
  const { code, metadata } = hasMetadata ? extractCodeAndMetadata(blob) : { code: blob };
40663
- const regs = new Registers();
40628
+ const regs = new registers_Registers();
40664
40629
  const memory = new Memory();
40665
40630
  return new Program(code, regs, memory, metadata);
40666
40631
  }
@@ -41675,6 +41640,45 @@ class BasicBlocks {
41675
41640
  ;// CONCATENATED MODULE: ./packages/core/pvm-interpreter/basic-blocks/index.ts
41676
41641
 
41677
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
+
41678
41682
  ;// CONCATENATED MODULE: ./packages/core/pvm-interpreter/instruction-gas-map.ts
41679
41683
 
41680
41684
 
@@ -42251,7 +42255,7 @@ class LoadOps {
42251
42255
  }
42252
42256
  loadNumber(address, registerIndex, numberLength) {
42253
42257
  const registerBytes = this.regs.getBytesAsLittleEndian(registerIndex, REG_SIZE_BYTES);
42254
- 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));
42255
42259
  if (loadResult.isError) {
42256
42260
  if (loadResult.error.isAccessFault) {
42257
42261
  this.instructionResult.status = pvm_interpreter_result_Result.FAULT_ACCESS;
@@ -42267,7 +42271,7 @@ class LoadOps {
42267
42271
  loadSignedNumber(address, registerIndex, numberLength) {
42268
42272
  // load all bytes from register to correctly handle the sign.
42269
42273
  const registerBytes = this.regs.getBytesAsLittleEndian(registerIndex, REG_SIZE_BYTES);
42270
- 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));
42271
42275
  if (loadResult.isError) {
42272
42276
  if (loadResult.error.isAccessFault) {
42273
42277
  this.instructionResult.status = pvm_interpreter_result_Result.FAULT_ACCESS;
@@ -42689,7 +42693,7 @@ class StoreOps {
42689
42693
  this.store(address, secondImmediateDecoder.getExtendedBytesAsLittleEndian());
42690
42694
  }
42691
42695
  store(address, bytes) {
42692
- const storeResult = this.memory.storeFrom(tryAsMemoryIndex(address), bytes);
42696
+ const storeResult = this.memory.storeFrom(memory_index_tryAsMemoryIndex(address), bytes);
42693
42697
  if (storeResult.isOk) {
42694
42698
  return;
42695
42699
  }
@@ -42698,7 +42702,7 @@ class StoreOps {
42698
42702
  }
42699
42703
  else {
42700
42704
  this.instructionResult.status = pvm_interpreter_result_Result.FAULT;
42701
- this.instructionResult.exitParam = getStartPageIndex(tryAsMemoryIndex(storeResult.error.address));
42705
+ this.instructionResult.exitParam = getStartPageIndex(memory_index_tryAsMemoryIndex(storeResult.error.address));
42702
42706
  }
42703
42707
  }
42704
42708
  }
@@ -43497,11 +43501,11 @@ class ProgramDecoder {
43497
43501
 
43498
43502
 
43499
43503
  const interpreter_logger = logger_Logger.new(import.meta.filename, "pvm");
43500
- class Interpreter {
43504
+ class interpreter_Interpreter {
43501
43505
  useSbrkGas;
43502
- registers = new Registers();
43506
+ registers = new registers_Registers();
43503
43507
  memory = new Memory();
43504
- gas = gasCounter(tryAsGas(0));
43508
+ gas = gasCounter(gas_tryAsGas(0));
43505
43509
  code = new Uint8Array();
43506
43510
  mask = Mask.empty();
43507
43511
  pc = 0;
@@ -43635,8 +43639,8 @@ class Interpreter {
43635
43639
  break;
43636
43640
  case ArgumentType.TWO_REGISTERS:
43637
43641
  if (this.useSbrkGas && currentInstruction === Instruction.SBRK) {
43638
- const calculateSbrkCost = (length) => (alignToPageSize(length) / PAGE_SIZE) * 16;
43639
- 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))));
43640
43644
  if (underflow) {
43641
43645
  this.status = status_Status.OOG;
43642
43646
  return this.status;
@@ -43731,12 +43735,88 @@ class Interpreter {
43731
43735
  }
43732
43736
  }
43733
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
+
43734
43813
  ;// CONCATENATED MODULE: ./packages/core/pvm-interpreter/index.ts
43735
43814
 
43736
43815
 
43737
43816
 
43738
43817
 
43739
43818
 
43819
+
43740
43820
  ;// CONCATENATED MODULE: ./node_modules/@fluffylabs/anan-as/build/debug-raw.js
43741
43821
  async function instantiate(module, imports = {}) {
43742
43822
  const adaptedImports = {
@@ -44190,12 +44270,12 @@ class AnanasMemory {
44190
44270
  }
44191
44271
  class AnanasGasCounter {
44192
44272
  instance;
44193
- initialGas = tryAsGas(0n);
44273
+ initialGas = gas_tryAsGas(0n);
44194
44274
  constructor(instance) {
44195
44275
  this.instance = instance;
44196
44276
  }
44197
44277
  get() {
44198
- return tryAsGas(this.instance.getGasLeft());
44278
+ return gas_tryAsGas(this.instance.getGasLeft());
44199
44279
  }
44200
44280
  set(g) {
44201
44281
  this.instance.setGasLeft(BigInt(g));
@@ -44300,7 +44380,7 @@ class InterpreterInstanceManager {
44300
44380
  const instances = [];
44301
44381
  switch (interpreter) {
44302
44382
  case PvmBackend.BuiltIn:
44303
- instances.push(new Interpreter({
44383
+ instances.push(new interpreter_Interpreter({
44304
44384
  useSbrkGas: false,
44305
44385
  }));
44306
44386
  break;
@@ -46756,7 +46836,7 @@ class Transfer {
46756
46836
  */
46757
46837
  basicGasCost = Compatibility.isGreaterOrEqual(GpVersion.V0_7_2)
46758
46838
  ? gas_tryAsSmallGas(10)
46759
- : (regs) => tryAsGas(10n + regs.get(TRANSFER_GAS_FEE_REG));
46839
+ : (regs) => gas_tryAsGas(10n + regs.get(TRANSFER_GAS_FEE_REG));
46760
46840
  tracedRegisters = traceRegisters(transfer_IN_OUT_REG, AMOUNT_REG, TRANSFER_GAS_FEE_REG, MEMO_START_REG);
46761
46841
  constructor(currentServiceId, partialState) {
46762
46842
  this.currentServiceId = currentServiceId;
@@ -46784,7 +46864,7 @@ class Transfer {
46784
46864
  if (transferResult.isOk) {
46785
46865
  if (Compatibility.isGreaterOrEqual(GpVersion.V0_7_2)) {
46786
46866
  // substracting value `t`
46787
- const underflow = gas.sub(tryAsGas(transferGasFee));
46867
+ const underflow = gas.sub(gas_tryAsGas(transferGasFee));
46788
46868
  if (underflow) {
46789
46869
  return PvmExecution.OOG;
46790
46870
  }
@@ -47529,7 +47609,7 @@ class Accumulate {
47529
47609
  serviceId,
47530
47610
  argsLength: numbers_tryAsU32(transfers.length + operands.length),
47531
47611
  });
47532
- const result = await executor.run(invocationArgs, tryAsGas(gas));
47612
+ const result = await executor.run(invocationArgs, gas_tryAsGas(gas));
47533
47613
  const [newState, checkpoint] = partialState.getStateUpdates();
47534
47614
  /**
47535
47615
  * PVM invocation returned and error so we return the checkpoint
@@ -47933,7 +48013,7 @@ class DeferredTransfers {
47933
48013
  partiallyUpdatedState.updateServiceInfo(serviceId, newInfo);
47934
48014
  const partialState = new AccumulateExternalities(this.chainSpec, this.blake2b, partiallyUpdatedState, serviceId, serviceId, timeslot);
47935
48015
  const fetchExternalities = FetchExternalities.createForOnTransfer({ entropy, transfers }, this.chainSpec);
47936
- let consumedGas = tryAsGas(0);
48016
+ let consumedGas = gas_tryAsGas(0);
47937
48017
  const hasTransfers = transfers.length > 0;
47938
48018
  const isCodeCorrect = code !== null && code.length <= W_C;
47939
48019
  if (!hasTransfers || !isCodeCorrect) {
@@ -47951,7 +48031,7 @@ class DeferredTransfers {
47951
48031
  const executor = await PvmExecutor.createOnTransferExecutor(serviceId, code, { partialState, fetchExternalities }, this.pvm);
47952
48032
  const args = encoder_Encoder.encodeObject(deferred_transfers_ARGS_CODEC, { timeslot, serviceId, transfersLength: numbers_tryAsU32(transfers.length) }, this.chainSpec);
47953
48033
  const gas = transfers.reduce((acc, item) => acc + item.gas, 0n);
47954
- consumedGas = (await executor.run(args, tryAsGas(gas))).consumedGas;
48034
+ consumedGas = (await executor.run(args, gas_tryAsGas(gas))).consumedGas;
47955
48035
  }
47956
48036
  transferStatistics.set(serviceId, { count: numbers_tryAsU32(transfers.length), gasUsed: tryAsServiceGas(consumedGas) });
47957
48037
  const [updatedState] = partialState.getStateUpdates();