@btc-vision/btc-runtime 1.0.16 → 1.0.18

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.0.16",
3
+ "version": "1.0.18",
4
4
  "description": "Bitcoin Smart Contract Runtime",
5
5
  "main": "btc/index.ts",
6
6
  "types": "btc/index.ts",
@@ -306,7 +306,9 @@ export abstract class OP_20 extends OP_NET implements IOP_20 {
306
306
  if (allowed < value) throw new Revert(`Insufficient allowance ${allowed} < ${value}`);
307
307
 
308
308
  const newAllowance: u256 = SafeMath.sub(allowed, value);
309
- fromAllowanceMap.set(from, newAllowance);
309
+ fromAllowanceMap.set(spender, newAllowance);
310
+
311
+ this.allowanceMap.set(from, fromAllowanceMap);
310
312
 
311
313
  this._unsafeTransferFrom(from, to, value);
312
314
 
@@ -8,7 +8,6 @@ import { encodePointerHash } from '../math/abi';
8
8
  import { BytesWriter } from '../buffer/BytesWriter';
9
9
  import { MAX_EVENTS, NetEvent } from '../events/NetEvent';
10
10
  import { Potential } from '../lang/Definitions';
11
- import { Map } from '../generic/Map';
12
11
  import { OP_NET } from '../contracts/OP_NET';
13
12
  import { PointerStorage } from '../types';
14
13
  import {
@@ -21,6 +20,7 @@ import {
21
20
  storePointer,
22
21
  } from './global';
23
22
  import { DeployContractResponse } from '../interfaces/DeployContractResponse';
23
+ import { MapU256 } from '../generic/MapU256';
24
24
 
25
25
  export * from '../env/global';
26
26
 
@@ -28,7 +28,7 @@ export * from '../env/global';
28
28
  export class BlockchainEnvironment {
29
29
  private static readonly runtimeException: string = 'RuntimeException';
30
30
 
31
- private storage: PointerStorage = new Map();
31
+ private storage: PointerStorage = new MapU256();
32
32
  private events: NetEvent[] = [];
33
33
 
34
34
  private _callee: PotentialAddress = null;
@@ -226,14 +226,8 @@ export class BlockchainEnvironment {
226
226
  const pointerHash: MemorySlotPointer = encodePointerHash(pointer, subPointer);
227
227
  this.ensureStorageAtPointer(pointerHash, defaultValue);
228
228
 
229
- // maybe find a better way for this
230
- const allKeys: u256[] = this.storage.keys();
231
- for (let i: i32 = 0; i < allKeys.length; i++) {
232
- const v: u256 = allKeys[i];
233
-
234
- if (u256.eq(v, pointerHash)) {
235
- return this.storage.get(v);
236
- }
229
+ if (this.storage.has(pointerHash)) {
230
+ return this.storage.get(pointerHash);
237
231
  }
238
232
 
239
233
  return defaultValue;
@@ -242,7 +236,8 @@ export class BlockchainEnvironment {
242
236
  public hasStorageAt(pointer: u16, subPointer: MemorySlotPointer): bool {
243
237
  // We mark zero as the default value for the storage, if something is 0, the storage slot get deleted or is non-existent
244
238
  const val: u256 = this.getStorageAt(pointer, subPointer, u256.Zero);
245
- return val != u256.Zero;
239
+
240
+ return u256.ne(val, u256.Zero);
246
241
  }
247
242
 
248
243
  public setStorageAt(
@@ -272,17 +267,6 @@ export class BlockchainEnvironment {
272
267
  }
273
268
 
274
269
  private _internalSetStorageAt(pointerHash: u256, value: MemorySlotData<u256>): void {
275
- const keys: u256[] = this.storage.keys();
276
-
277
- // Delete the old value, there is a bug with u256 and maps.
278
- for (let i = 0; i < keys.length; i++) {
279
- const key = keys[i];
280
-
281
- if (u256.eq(key, pointerHash)) {
282
- this.storage.delete(key);
283
- }
284
- }
285
-
286
270
  this.storage.set(pointerHash, value);
287
271
 
288
272
  const writer: BytesWriter = new BytesWriter();
@@ -294,14 +278,8 @@ export class BlockchainEnvironment {
294
278
  }
295
279
 
296
280
  private hasPointerStorageHash(pointer: MemorySlotPointer): bool {
297
- const keys = this.storage.keys();
298
-
299
- for (let i = 0; i < keys.length; i++) {
300
- const key = keys[i];
301
-
302
- if (u256.eq(key, pointer)) {
303
- return true;
304
- }
281
+ if (this.storage.has(pointer)) {
282
+ return true;
305
283
  }
306
284
 
307
285
  // we attempt to load the requested pointer.
@@ -1,8 +1,8 @@
1
1
  import { Revert } from '../types/Revert';
2
2
 
3
3
  export class Map<K, V> {
4
- private _keys: K[] = [];
5
- private _values: V[] = [];
4
+ protected _keys: K[] = [];
5
+ protected _values: V[] = [];
6
6
 
7
7
  public get size(): i32 {
8
8
  return this._keys.length;
@@ -17,7 +17,7 @@ export class Map<K, V> {
17
17
  }
18
18
 
19
19
  public set(key: K, value: V): void {
20
- const index: i32 = this._keys.indexOf(key);
20
+ const index: i32 = this.indexOf(key);
21
21
  if (index == -1) {
22
22
  this._keys.push(key);
23
23
  this._values.push(value);
@@ -26,8 +26,12 @@ export class Map<K, V> {
26
26
  }
27
27
  }
28
28
 
29
+ public indexOf(key: K): i32 {
30
+ return this._keys.indexOf(key);
31
+ }
32
+
29
33
  public get(key: K): V {
30
- const index: i32 = this._keys.indexOf(key);
34
+ const index: i32 = this.indexOf(key);
31
35
  if (index == -1) {
32
36
  throw new Revert('Key not found in map');
33
37
  }
@@ -35,11 +39,17 @@ export class Map<K, V> {
35
39
  }
36
40
 
37
41
  public has(key: K): bool {
38
- return this._keys.includes(key);
42
+ for (let i: i32 = 0; i < this._keys.length; i++) {
43
+ if (this._keys[i] == key) {
44
+ return true;
45
+ }
46
+ }
47
+
48
+ return false;
39
49
  }
40
50
 
41
51
  public delete(key: K): bool {
42
- const index: i32 = this._keys.indexOf(key);
52
+ const index: i32 = this.indexOf(key);
43
53
  if (index == -1) {
44
54
  return false;
45
55
  }
@@ -0,0 +1,58 @@
1
+ import { Revert } from '../types/Revert';
2
+ import { u256 } from 'as-bignum/assembly';
3
+ import { Map } from './Map';
4
+
5
+ export class MapU256 extends Map<u256, u256> {
6
+ public set(key: u256, value: u256): void {
7
+ const index: i32 = this._keys.indexOf(key);
8
+ if (index == -1) {
9
+ this._keys.push(key);
10
+ this._values.push(value);
11
+ } else {
12
+ this._values[index] = value;
13
+ }
14
+ }
15
+
16
+ public indexOf(pointerHash: u256): i32 {
17
+ // Delete the old value
18
+ for (let i: i32 = 0; i < this._keys.length; i++) {
19
+ const key = this._keys[i];
20
+
21
+ if (u256.eq(key, pointerHash)) {
22
+ return i;
23
+ }
24
+ }
25
+
26
+ return -1;
27
+ }
28
+
29
+ public has(key: u256): bool {
30
+ for (let i: i32 = 0; i < this._keys.length; i++) {
31
+ if (u256.eq(this._keys[i], key)) {
32
+ return true;
33
+ }
34
+ }
35
+
36
+ return false;
37
+ }
38
+
39
+ public get(key: u256): u256 {
40
+ const index: i32 = this.indexOf(key);
41
+ if (index == -1) {
42
+ throw new Revert('Key not found in map');
43
+ }
44
+ return this._values[index];
45
+ }
46
+
47
+ public delete(key: u256): bool {
48
+ const index: i32 = this.indexOf(key);
49
+ if (index == -1) {
50
+ return false;
51
+ }
52
+
53
+ this._keys.splice(index, 1);
54
+ this._values.splice(index, 1);
55
+
56
+ return true;
57
+ }
58
+ }