@btc-vision/btc-runtime 1.5.1 → 1.5.2

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.
@@ -1,56 +0,0 @@
1
- import { Blockchain } from '../env';
2
- import { encodePointerUnknownLength } from '../math/abi';
3
- import { BytesWriter } from '../buffer/BytesWriter';
4
- import { EMPTY_BUFFER } from '../math/bytes';
5
-
6
- @final
7
- export class StringMemoryMap<K extends string> {
8
- public pointer: u16;
9
-
10
- constructor(
11
- pointer: u16,
12
- ) {
13
- this.pointer = pointer;
14
- }
15
-
16
- @inline
17
- public set(key: K, value: Uint8Array): this {
18
- const keyHash: Uint8Array = this.encodePointer(key);
19
- Blockchain.setStorageAt(keyHash, value);
20
-
21
- return this;
22
- }
23
-
24
- @inline
25
- public get(key: K): Uint8Array {
26
- const keyHash: Uint8Array = this.encodePointer(key);
27
-
28
- return Blockchain.getStorageAt(keyHash);
29
- }
30
-
31
- @inline
32
- public has(key: K): bool {
33
- const keyHash: Uint8Array = this.encodePointer(key);
34
-
35
- return Blockchain.hasStorageAt(keyHash);
36
- }
37
-
38
- @unsafe
39
- public delete(key: K): bool {
40
- this.set(key, EMPTY_BUFFER);
41
-
42
- return true;
43
- }
44
-
45
- @unsafe
46
- public clear(): void {
47
- throw new Error('Method not implemented.');
48
- }
49
-
50
- private encodePointer(key: K): Uint8Array {
51
- const writer = new BytesWriter(key.length);
52
- writer.writeString(key);
53
-
54
- return encodePointerUnknownLength(this.pointer, writer.getBuffer());
55
- }
56
- }
@@ -1,72 +0,0 @@
1
- import { Blockchain } from '../env';
2
- import { encodePointerUnknownLength } from '../math/abi';
3
- import { BytesWriter } from '../buffer/BytesWriter';
4
- import { u256 } from '@btc-vision/as-bignum/assembly';
5
-
6
- @final
7
- export class Uint8ArrayMerger {
8
- public parentKey: Uint8Array;
9
-
10
- public pointer: u16;
11
-
12
- constructor(
13
- parent: Uint8Array,
14
- pointer: u16,
15
- ) {
16
- this.pointer = pointer;
17
-
18
- this.parentKey = parent;
19
- }
20
-
21
- public setAsUint8Array(key: Uint8Array, value: Uint8Array): this {
22
- const keyHash: Uint8Array = this.getKeyHash(key);
23
- Blockchain.setStorageAt(keyHash, value);
24
-
25
- return this;
26
- }
27
-
28
- public set(key: Uint8Array, value: u256): this {
29
- return this.setAsUint8Array(key, value.toUint8Array(true));
30
- }
31
-
32
- public getAsUint8Array(key: Uint8Array): Uint8Array {
33
- const keyHash: Uint8Array = this.getKeyHash(key);
34
-
35
- return Blockchain.getStorageAt(keyHash);
36
- }
37
-
38
- public get(key: Uint8Array): u256 {
39
- const data = this.getAsUint8Array(key);
40
-
41
- return u256.fromUint8ArrayBE(data);
42
- }
43
-
44
- public has(key: Uint8Array): bool {
45
- const mergedKey: Uint8Array = this.getKeyHash(key);
46
-
47
- return Blockchain.hasStorageAt(mergedKey);
48
- }
49
-
50
- @unsafe
51
- public delete(_key: Uint8Array): bool {
52
- throw new Error('Method not implemented.');
53
- }
54
-
55
- @unsafe
56
- public clear(): void {
57
- throw new Error('Clear method not implemented.');
58
- }
59
-
60
- private getKeyHash(key: Uint8Array): Uint8Array {
61
- const writer: BytesWriter = new BytesWriter(key.byteLength + this.parentKey.byteLength);
62
-
63
- writer.writeBytes(this.parentKey);
64
- writer.writeBytes(key);
65
-
66
- return this.encodePointer(writer);
67
- }
68
-
69
- private encodePointer(writer: BytesWriter): Uint8Array {
70
- return encodePointerUnknownLength(this.pointer, writer.getBuffer());
71
- }
72
- }
@@ -1,113 +0,0 @@
1
- import { BytesReader } from '../buffer/BytesReader';
2
- import { BytesWriter } from '../buffer/BytesWriter';
3
- import { Blockchain } from '../env';
4
- import { encodePointer } from '../math/abi';
5
- import { Revert } from '../types/Revert';
6
-
7
- export const SERIALIZED_POINTER_LENGTH: u8 = 29;
8
-
9
- // Similar to a struct in Solidity. (Use in worst case scenario, consume a lot of gas)
10
- export abstract class Serializable {
11
- protected pointer: u16;
12
- protected subPointer: Uint8Array;
13
-
14
- protected constructor(pointer: u16, subPointer: Uint8Array) {
15
- if (subPointer.length !== SERIALIZED_POINTER_LENGTH) throw new Revert(`Sub pointer length must be ${SERIALIZED_POINTER_LENGTH} bytes.`);
16
-
17
- this.pointer = pointer;
18
- this.subPointer = subPointer;
19
- }
20
-
21
- // Max of 8160 bytes
22
- public abstract get chunkCount(): u8;
23
-
24
- public abstract writeToBuffer(): BytesWriter;
25
-
26
- public abstract readFromBuffer(reader: BytesReader): void;
27
-
28
- public abstract exists(chunk: Uint8Array, index: u8): boolean;
29
-
30
- public load(): boolean {
31
- const chunks: Uint8Array[] = [];
32
-
33
- for (let index: u8 = 0; index < this.chunkCount; index++) {
34
- const pointer = this.getPointer(this.subPointer, index);
35
- const chunk: Uint8Array = Blockchain.getStorageAt(pointer);
36
-
37
- if (!this.exists(chunk, index)) {
38
- return false;
39
- }
40
-
41
- chunks.push(chunk);
42
- }
43
-
44
- const reader = this.chunksToBytes(chunks);
45
- this.readFromBuffer(reader);
46
-
47
- return true;
48
- }
49
-
50
- public save(): void {
51
- const writer: BytesWriter = this.writeToBuffer();
52
- const buffer = writer.getBuffer();
53
- const chunks: Uint8Array[] = this.bytesToChunks(buffer);
54
-
55
- if (chunks.length !== this.chunkCount) {
56
- throw new Revert(
57
- 'Invalid chunk count, expected ' +
58
- this.chunkCount.toString() +
59
- ' but got ' +
60
- chunks.length.toString(),
61
- );
62
- }
63
-
64
- if (chunks.length > 255) {
65
- throw new Revert('Too many chunks to save. You may only write up to 8160 bytes per object.');
66
- }
67
-
68
- for (let index: u8 = 0; index < u8(chunks.length); index++) {
69
- Blockchain.setStorageAt(this.getPointer(this.subPointer, index), chunks[index]);
70
- }
71
- }
72
-
73
- protected bytesToChunks(buffer: Uint8Array): Uint8Array[] {
74
- const chunks: Uint8Array[] = [];
75
-
76
- for (let index: i32 = 0; index < buffer.byteLength; index += 32) {
77
- if (chunks.length === 256) {
78
- throw new Revert(`Too many chunks to save You may only write up to 8160 bytes per object.`);
79
- }
80
-
81
- const chunk = buffer.slice(index, index + 32);
82
- chunks.push(chunk);
83
- }
84
-
85
- return chunks;
86
- }
87
-
88
- protected chunksToBytes(chunks: Uint8Array[]): BytesReader {
89
- if (this.chunkCount > u8(255)) {
90
- throw new Revert(`Too many chunks received. You may only write up to 8160 bytes per object.`);
91
- }
92
-
93
- const buffer: Uint8Array = new Uint8Array(i32(this.chunkCount) * i32(32));
94
- let offset: i32 = 0;
95
-
96
- for (let indexChunk: i32 = 0; indexChunk < chunks.length; indexChunk++) {
97
- const bytes: Uint8Array = chunks[indexChunk];
98
- for (let indexByte: i32 = 0; indexByte < bytes.length; indexByte++) {
99
- buffer[offset++] = bytes[indexByte];
100
- }
101
- }
102
-
103
- return new BytesReader(buffer);
104
- }
105
-
106
- protected getPointer(subPointer: Uint8Array, index: u8): Uint8Array {
107
- const writer = new BytesWriter(30);
108
- writer.writeU8(index);
109
- writer.writeBytes(subPointer);
110
-
111
- return encodePointer(this.pointer, writer.getBuffer());
112
- }
113
- }