@btc-vision/btc-runtime 1.5.3 → 1.5.4

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@btc-vision/btc-runtime",
3
- "version": "1.5.3",
3
+ "version": "1.5.4",
4
4
  "description": "Bitcoin Smart Contract Runtime",
5
5
  "main": "btc/index.ts",
6
6
  "scripts": {},
@@ -1,7 +1,6 @@
1
- import { BytesReader } from '../../buffer/BytesReader';
2
1
  import { BytesWriter } from '../../buffer/BytesWriter';
3
2
  import { Blockchain } from '../../env';
4
- import { addUint8ArraysBE, u64ToBE32Bytes } from '../../math/bytes';
3
+ import { addUint8ArraysBE, encodeBasePointer, readLengthAndStartIndex, u64ToBE32Bytes } from '../../math/bytes';
5
4
  import { Address } from '../../types/Address';
6
5
  import { Revert } from '../../types/Revert';
7
6
 
@@ -38,25 +37,15 @@ export class StoredAddressArray {
38
37
  `You must pass a 30 bytes sub-pointer. (AddressArray, got ${subPointer.length})`,
39
38
  );
40
39
 
41
- // Construct base pointer as a 32-byte array
42
- const writer = new BytesWriter(32);
43
- writer.writeU16(pointer);
44
- writer.writeBytes(subPointer);
40
+ const basePointer = encodeBasePointer(pointer, subPointer);
41
+ this.lengthPointer = Uint8Array.wrap(basePointer.buffer);
42
+ this.baseU256Pointer = basePointer;
45
43
 
46
- // We'll reuse the same bytes as the "base pointer" for offsets
47
- const baseU256Pointer = writer.getBuffer(); // 32 bytes
48
- // For length+startIndex, we'll use the same pointer
49
- const lengthPointer = Uint8Array.wrap(baseU256Pointer.buffer);
44
+ const storedLenStart = Blockchain.getStorageAt(basePointer);
45
+ const data = readLengthAndStartIndex(storedLenStart);
50
46
 
51
- // Load length + startIndex from storage (16 bytes: 8 for length, 8 for startIndex).
52
- const storedLengthAndStartIndex: Uint8Array = Blockchain.getStorageAt(lengthPointer);
53
-
54
- const reader = new BytesReader(storedLengthAndStartIndex);
55
- this._length = reader.readU64();
56
- this._startIndex = reader.readU64();
57
-
58
- this.lengthPointer = lengthPointer;
59
- this.baseU256Pointer = baseU256Pointer;
47
+ this._length = data[0];
48
+ this._startIndex = data[1];
60
49
  }
61
50
 
62
51
  /** Get an element by its global index. */
@@ -1,8 +1,15 @@
1
1
  import { BytesWriter } from '../../buffer/BytesWriter';
2
- import { BytesReader } from '../../buffer/BytesReader';
3
2
  import { Blockchain } from '../../env';
4
3
  import { Revert } from '../../types/Revert';
5
- import { addUint8ArraysBE, GET_EMPTY_BUFFER, getBit, setBit, u64ToBE32Bytes } from '../../math/bytes';
4
+ import {
5
+ addUint8ArraysBE,
6
+ encodeBasePointer,
7
+ GET_EMPTY_BUFFER,
8
+ getBit,
9
+ readLengthAndStartIndex,
10
+ setBit,
11
+ u64ToBE32Bytes,
12
+ } from '../../math/bytes';
6
13
 
7
14
  /**
8
15
  * @class StoredBooleanArray
@@ -42,22 +49,15 @@ export class StoredBooleanArray {
42
49
  ) {
43
50
  assert(subPtr.length <= 30, `You must pass a 30 bytes sub-pointer. (StoredBooleanArray, got ${subPtr.length})`);
44
51
 
45
- const writer = new BytesWriter(32);
46
- writer.writeU16(pointer);
47
- writer.writeBytes(subPtr);
48
-
49
- const basePtr = writer.getBuffer();
52
+ const basePointer = encodeBasePointer(pointer, subPtr);
53
+ this.lengthPointer = Uint8Array.wrap(basePointer.buffer);
54
+ this.basePointer = basePointer;
50
55
 
51
- this.basePointer = basePtr;
52
- this.lengthPointer = basePtr;
56
+ const storedLenStart = Blockchain.getStorageAt(basePointer);
57
+ const data = readLengthAndStartIndex(storedLenStart);
53
58
 
54
- const storedLenStart: Uint8Array = Blockchain.getStorageAt(
55
- this.lengthPointer,
56
- );
57
-
58
- const r = new BytesReader(storedLenStart);
59
- this._length = r.readU64();
60
- this._startIndex = r.readU64();
59
+ this._length = data[0];
60
+ this._startIndex = data[1];
61
61
  }
62
62
 
63
63
  // -------------- Public Accessors -------------- //
@@ -187,12 +187,11 @@ export class StoredBooleanArray {
187
187
 
188
188
  // 2) If length or startIndex changed, store them
189
189
  if (this._isChangedLength || this._isChangedStartIndex) {
190
- const w = new BytesWriter(16);
190
+ const w = new BytesWriter(32);
191
191
  w.writeU64(this._length);
192
192
  w.writeU64(this._startIndex);
193
193
 
194
- const data = w.getBuffer(); // 16 bytes
195
- // You could store 32 bytes if you prefer; the leftover bytes can be 0
194
+ const data = w.getBuffer();
196
195
  Blockchain.setStorageAt(this.lengthPointer, data);
197
196
 
198
197
  this._isChangedLength = false;
@@ -215,9 +214,7 @@ export class StoredBooleanArray {
215
214
  }
216
215
 
217
216
  // also reset length + startIndex in storage
218
- const writer = new BytesWriter(16);
219
- writer.writeU64(0);
220
- writer.writeU64(0);
217
+ const writer = new BytesWriter(32);
221
218
  Blockchain.setStorageAt(this.lengthPointer, writer.getBuffer());
222
219
 
223
220
  // reset in memory