@btc-vision/btc-runtime 1.1.9 → 1.1.11
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
|
@@ -2,7 +2,6 @@ import { u256 } from 'as-bignum/assembly';
|
|
|
2
2
|
import { Address, ADDRESS_BYTE_LENGTH } from '../types/Address';
|
|
3
3
|
import { Selector } from '../math/abi';
|
|
4
4
|
import { BytesReader } from './BytesReader';
|
|
5
|
-
import { SelectorsMap } from '../universal/ABIRegistry';
|
|
6
5
|
import { Revert } from '../types/Revert';
|
|
7
6
|
import { Map } from '../generic/Map';
|
|
8
7
|
import { ArrayBuffer } from 'arraybuffer';
|
|
@@ -12,7 +11,7 @@ export class BytesWriter {
|
|
|
12
11
|
private currentOffset: u32 = 0;
|
|
13
12
|
private buffer: DataView;
|
|
14
13
|
|
|
15
|
-
constructor(length: i32
|
|
14
|
+
constructor(length: i32) {
|
|
16
15
|
const arrayBuffer = new ArrayBuffer(length);
|
|
17
16
|
|
|
18
17
|
this.buffer = new DataView(arrayBuffer);
|
|
@@ -125,20 +124,17 @@ export class BytesWriter {
|
|
|
125
124
|
this.writeString(value);
|
|
126
125
|
}
|
|
127
126
|
|
|
128
|
-
public
|
|
129
|
-
|
|
127
|
+
public writeAddressValueTupleMap(map: Map<Address, u256>): void {
|
|
128
|
+
if (map.size > 65535) throw new Revert('Map size is too large');
|
|
130
129
|
|
|
131
|
-
const
|
|
132
|
-
for (let i = 0; i < keys.length; i++) {
|
|
133
|
-
const key: u32 = keys[i] as u32;
|
|
134
|
-
const value = map.get(key);
|
|
130
|
+
/*const requiredSize: u32 = 2 + map.size * (ADDRESS_BYTE_LENGTH + 32);
|
|
135
131
|
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
132
|
+
if (this.buffer.byteLength < requiredSize) {
|
|
133
|
+
abort(
|
|
134
|
+
`This buffer is too small. Required size: ${requiredSize} - Current size: ${this.buffer.byteLength}`,
|
|
135
|
+
);
|
|
136
|
+
}*/
|
|
139
137
|
|
|
140
|
-
public writeAddressValueTupleMap(map: Map<Address, u256>): void {
|
|
141
|
-
if (map.size > 65535) throw new Revert('Map size is too large');
|
|
142
138
|
this.writeU16(u16(map.size));
|
|
143
139
|
|
|
144
140
|
const keys = map.keys();
|
|
@@ -154,9 +150,28 @@ export class BytesWriter {
|
|
|
154
150
|
public writeLimitedAddressBytesMap(map: Map<Address, Uint8Array[]>): void {
|
|
155
151
|
if (map.size > 8) throw new Revert('Too many contract called.'); // no more than 8 different contracts.
|
|
156
152
|
|
|
157
|
-
|
|
153
|
+
/*let requiredSize: u32 = 1 + (map.size * ADDRESS_BYTE_LENGTH + 1);
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
for (let i = 0; i < map.size; i++) {
|
|
157
|
+
const address: Address = keys[i];
|
|
158
|
+
const calls: Uint8Array[] = map.get(address) || [];
|
|
159
|
+
|
|
160
|
+
for (let j: i32 = 0; j < calls.length; j++) {
|
|
161
|
+
requiredSize += 4 + calls[j].length;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
if (this.buffer.byteLength < requiredSize) {
|
|
166
|
+
abort(
|
|
167
|
+
`This buffer is too small. Required size: ${requiredSize} - Current size: ${this.buffer.byteLength}`,
|
|
168
|
+
);
|
|
169
|
+
}*/
|
|
158
170
|
|
|
159
171
|
const keys: Address[] = map.keys();
|
|
172
|
+
|
|
173
|
+
this.writeU8(u8(map.size));
|
|
174
|
+
|
|
160
175
|
for (let i: i32 = 0; i < keys.length; i++) {
|
|
161
176
|
const address: Address = keys[i];
|
|
162
177
|
const calls: Uint8Array[] = map.get(address) || [];
|
|
@@ -243,17 +258,17 @@ export class BytesWriter {
|
|
|
243
258
|
}
|
|
244
259
|
|
|
245
260
|
private resize(size: u32): void {
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
const buf: Uint8Array = new Uint8Array(u32(this.buffer.byteLength) + size);
|
|
261
|
+
abort(
|
|
262
|
+
`Buffer is getting resized. This is very bad for performance. Expected size: ${this.buffer.byteLength + size} - Current size: ${this.buffer.byteLength}`,
|
|
263
|
+
);
|
|
264
|
+
|
|
265
|
+
/*const buf: Uint8Array = new Uint8Array(u32(this.buffer.byteLength) + size);
|
|
251
266
|
|
|
252
267
|
for (let i: i32 = 0; i < this.buffer.byteLength; i++) {
|
|
253
268
|
buf[i] = this.buffer.getUint8(i);
|
|
254
269
|
}
|
|
255
270
|
|
|
256
|
-
this.buffer = new DataView(buf.buffer)
|
|
271
|
+
this.buffer = new DataView(buf.buffer);*/
|
|
257
272
|
}
|
|
258
273
|
|
|
259
274
|
private getDefaultBuffer(length: i32 = 1): DataView {
|
|
@@ -210,11 +210,11 @@ export abstract class DeployableOP_20 extends OP_NET implements IOP_20 {
|
|
|
210
210
|
response.writeU8(this.decimals);
|
|
211
211
|
break;
|
|
212
212
|
case encodeSelector('name'):
|
|
213
|
-
response = new BytesWriter(this.name.length +
|
|
213
|
+
response = new BytesWriter(this.name.length + 2);
|
|
214
214
|
response.writeStringWithLength(this.name);
|
|
215
215
|
break;
|
|
216
216
|
case encodeSelector('symbol'):
|
|
217
|
-
response = new BytesWriter(this.symbol.length +
|
|
217
|
+
response = new BytesWriter(this.symbol.length + 2);
|
|
218
218
|
response.writeStringWithLength(this.symbol);
|
|
219
219
|
break;
|
|
220
220
|
case encodeSelector('totalSupply'):
|
|
@@ -168,7 +168,7 @@ export class BlockchainEnvironment {
|
|
|
168
168
|
throw this.error('Too many events');
|
|
169
169
|
}
|
|
170
170
|
|
|
171
|
-
const buffer: BytesWriter = new BytesWriter();
|
|
171
|
+
const buffer: BytesWriter = new BytesWriter(this.getEventSize());
|
|
172
172
|
buffer.writeU16(eventLength);
|
|
173
173
|
|
|
174
174
|
for (let i: u8 = 0; i < eventLength; i++) {
|
|
@@ -195,7 +195,7 @@ export class BlockchainEnvironment {
|
|
|
195
195
|
}
|
|
196
196
|
|
|
197
197
|
public deployContract(hash: u256, bytecode: Uint8Array): DeployContractResponse {
|
|
198
|
-
const writer = new BytesWriter();
|
|
198
|
+
const writer = new BytesWriter(32 + bytecode.length);
|
|
199
199
|
writer.writeU256(hash);
|
|
200
200
|
writer.writeBytes(bytecode);
|
|
201
201
|
|
|
@@ -260,16 +260,19 @@ export class BlockchainEnvironment {
|
|
|
260
260
|
this._internalSetStorageAt(pointerHash, value);
|
|
261
261
|
}
|
|
262
262
|
|
|
263
|
-
public getViewSelectors(): Uint8Array {
|
|
264
|
-
return ABIRegistry.getViewSelectors();
|
|
265
|
-
}
|
|
266
|
-
|
|
267
263
|
public getMethodSelectors(): Uint8Array {
|
|
268
264
|
return ABIRegistry.getMethodSelectors();
|
|
269
265
|
}
|
|
270
266
|
|
|
271
|
-
|
|
272
|
-
|
|
267
|
+
private getEventSize(): u32 {
|
|
268
|
+
let size: u32 = 2;
|
|
269
|
+
|
|
270
|
+
for (let i: u32 = 0; i < <u32>this.events.length; i++) {
|
|
271
|
+
const event: NetEvent = this.events[i];
|
|
272
|
+
size += 2 + event.eventType.length + 8 + event.length + 4;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
return size;
|
|
273
276
|
}
|
|
274
277
|
|
|
275
278
|
private createContractIfNotExists(): void {
|
|
@@ -1,23 +1,36 @@
|
|
|
1
1
|
import { BytesWriter } from '../buffer/BytesWriter';
|
|
2
|
+
import { Revert } from '../types/Revert';
|
|
2
3
|
|
|
3
4
|
export const MAX_EVENT_DATA_SIZE: u32 = 352; // 352 bytes max per event.
|
|
4
5
|
export const MAX_EVENTS: u16 = 1000; // 1000 events max per transactions.
|
|
5
6
|
|
|
6
7
|
export abstract class NetEvent {
|
|
8
|
+
private readonly buffer: Uint8Array;
|
|
9
|
+
|
|
7
10
|
protected constructor(
|
|
8
11
|
public readonly eventType: string,
|
|
9
12
|
protected data: BytesWriter,
|
|
10
|
-
) {
|
|
13
|
+
) {
|
|
14
|
+
if (data.bufferLength() > MAX_EVENT_DATA_SIZE) {
|
|
15
|
+
throw new Error('Event data length exceeds maximum length.');
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
this.buffer = data.getBuffer();
|
|
19
|
+
}
|
|
11
20
|
|
|
12
21
|
public get length(): u32 {
|
|
13
|
-
|
|
22
|
+
if (!this.buffer) {
|
|
23
|
+
throw new Revert('Buffer is not defined');
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return this.buffer.byteLength;
|
|
14
27
|
}
|
|
15
28
|
|
|
16
29
|
public getEventData(): Uint8Array {
|
|
17
|
-
if (this.
|
|
18
|
-
throw new
|
|
30
|
+
if (!this.buffer) {
|
|
31
|
+
throw new Revert('Buffer is not defined');
|
|
19
32
|
}
|
|
20
33
|
|
|
21
|
-
return this.
|
|
34
|
+
return this.buffer;
|
|
22
35
|
}
|
|
23
36
|
}
|
package/runtime/exports/index.ts
CHANGED
|
@@ -20,18 +20,10 @@ export function getEvents(): Uint8Array {
|
|
|
20
20
|
return Blockchain.getEvents();
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
-
export function getViewABI(): Uint8Array {
|
|
24
|
-
return Blockchain.getViewSelectors();
|
|
25
|
-
}
|
|
26
|
-
|
|
27
23
|
export function getMethodABI(): Uint8Array {
|
|
28
24
|
return Blockchain.getMethodSelectors();
|
|
29
25
|
}
|
|
30
26
|
|
|
31
|
-
export function getWriteMethods(): Uint8Array {
|
|
32
|
-
return Blockchain.getWriteMethods();
|
|
33
|
-
}
|
|
34
|
-
|
|
35
27
|
export function setEnvironment(data: Uint8Array): void {
|
|
36
28
|
Blockchain.setEnvironment(data);
|
|
37
29
|
}
|
|
@@ -1,72 +1,27 @@
|
|
|
1
1
|
import { encodeSelector, Selector } from '../math/abi';
|
|
2
2
|
import { BytesReader } from '../buffer/BytesReader';
|
|
3
3
|
import { BytesWriter } from '../buffer/BytesWriter';
|
|
4
|
-
import { Map } from '../generic/Map';
|
|
5
4
|
|
|
6
5
|
export type Calldata = NonNullable<BytesReader>;
|
|
7
|
-
export type SelectorsMap = Map<u32, Uint8Array>;
|
|
8
6
|
|
|
9
7
|
class ABIRegistryBase {
|
|
10
8
|
private methodMap: Selector[] = [];
|
|
11
|
-
private selectors: SelectorsMap = new Map();
|
|
12
|
-
|
|
13
|
-
private viewSelectors: Selector[] = [];
|
|
14
|
-
private allowedWriteMethods: Selector[] = [];
|
|
15
|
-
|
|
16
|
-
// Register properties with their selectors and handlers
|
|
17
|
-
public defineGetterSelector(name: string, canWrite: boolean): void {
|
|
18
|
-
const selector: Selector = encodeSelector(name);
|
|
19
|
-
|
|
20
|
-
const selectorWriter: BytesWriter = new BytesWriter();
|
|
21
|
-
selectorWriter.writeABISelector(name, selector);
|
|
22
|
-
|
|
23
|
-
if (!this.selectors.has(selector)) {
|
|
24
|
-
this.selectors.set(selector, selectorWriter.getBuffer());
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
if (canWrite) this.addToWriteMethods(selector);
|
|
28
|
-
|
|
29
|
-
if (!this.viewSelectors.includes(selector)) {
|
|
30
|
-
this.viewSelectors.push(selector);
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
public getViewSelectors(): Uint8Array {
|
|
35
|
-
const writer: BytesWriter = new BytesWriter();
|
|
36
|
-
writer.writeViewSelectorMap(this.selectors);
|
|
37
|
-
|
|
38
|
-
return writer.getBuffer();
|
|
39
|
-
}
|
|
40
9
|
|
|
41
10
|
public getMethodSelectors(): Uint8Array {
|
|
42
|
-
const writer: BytesWriter = new BytesWriter();
|
|
11
|
+
const writer: BytesWriter = new BytesWriter(2 + this.methodMap.length * 4);
|
|
43
12
|
writer.writeMethodSelectorsMap(this.methodMap);
|
|
44
13
|
|
|
45
14
|
return writer.getBuffer();
|
|
46
15
|
}
|
|
47
16
|
|
|
48
|
-
public getWriteMethods(): Uint8Array {
|
|
49
|
-
const writer: BytesWriter = new BytesWriter();
|
|
50
|
-
writer.writeMethodSelectorsMap(this.allowedWriteMethods);
|
|
51
|
-
|
|
52
|
-
return writer.getBuffer();
|
|
53
|
-
}
|
|
54
|
-
|
|
55
17
|
// Register methods with their selectors and handlers
|
|
56
|
-
public defineMethodSelector(name: string
|
|
18
|
+
public defineMethodSelector(name: string): void {
|
|
57
19
|
const selector: u32 = encodeSelector(name);
|
|
58
|
-
if (canWrite) this.addToWriteMethods(selector);
|
|
59
20
|
|
|
60
21
|
if (!this.methodMap.includes(selector)) {
|
|
61
22
|
this.methodMap.push(selector);
|
|
62
23
|
}
|
|
63
24
|
}
|
|
64
|
-
|
|
65
|
-
private addToWriteMethods(selector: Selector): void {
|
|
66
|
-
if (!this.allowedWriteMethods.includes(selector)) {
|
|
67
|
-
this.allowedWriteMethods.push(selector);
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
25
|
}
|
|
71
26
|
|
|
72
27
|
export const ABIRegistry = new ABIRegistryBase();
|