-
Notifications
You must be signed in to change notification settings - Fork 3
/
mockVM.ts
495 lines (435 loc) · 16.1 KB
/
mockVM.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
import { Protobuf } from "as-proto";
import { System } from "../systemCalls";
import { system_calls, chain, protocol, authority, value } from '@koinos/proto-as';
import { StringBytes } from "./stringBytes";
export namespace MockVM {
export const METADATA_SPACE = new chain.object_space(true, null, 0);
export class MockAuthority {
autorization_type: authority.authorization_type;
account: Uint8Array;
authorized: bool;
constructor(autorization_type: authority.authorization_type, account: Uint8Array, authorized: bool) {
this.autorization_type = autorization_type;
this.account = account;
this.authorized = authorized;
}
}
/**
* Set entry point that will be used when calling System.getEntryPoint()
* @param { u32 } entryPoint entry point to set
* @example
* ```ts
* MockVM.setEntryPoint(0xc3ab8ff1);
*
* const entryPoint = System.getEntryPoint();
* System.log('entryPoint: ' + entryPoint.toString());
* ```
*/
export function setEntryPoint(entryPoint: u32): void {
const entryPointObj = new value.value_type();
entryPointObj.int32_value = entryPoint;
System.putObject(METADATA_SPACE, 'entry_point', entryPointObj, value.value_type.encode);
}
/**
* Set contract arguments that will be used when calling System.getContractArguments()
* @param { Uint8Array } contractArguments contract arguments to set
* @example
* ```ts
* let contractArguments = StringBytes.stringToBytes('myArgs');
* MockVM.setContractArguments(contractArguments);
*
* contractArguments = System.getContractArguments();
* System.log('contractArguments: ' + (StringBytes.bytesToString(contractArguments)!));
* ```
*/
export function setContractArguments(contractArguments: Uint8Array): void {
System.putBytes(METADATA_SPACE, 'contract_arguments', contractArguments);
}
/**
* Set contract id that will be used when calling System.getContractArguments()
* @param { Uint8Array } contractId contract id to set
* @example
* ```ts
* let contractId = Base58.decode('1DQzuCcTKacbs9GGScRTU1Hc8BsyARTPqe');
* MockVM.setContractId(contractId);
*
* contractId = System.getContractId();
* System.log('contractId: ' + Base58.encode(contractId));
* ```
*/
export function setContractId(contractId: Uint8Array): void {
System.putBytes(METADATA_SPACE, 'contract_id', contractId);
}
export function setChainId(chainId: Uint8Array): void {
System.putBytes(METADATA_SPACE, 'chain_id', chainId);
}
/**
* Set head info that will be used when calling System.getHeadInfo()
* @param { chain.head_info } headInfo head info to set
* @example
* ```ts
* let headInfo = new chain.head_info();
* headInfo.head_block_time = 123456789;
* headInfo.last_irreversible_block = 3;
*
* MockVM.setHeadInfo(headInfo);
*
* headInfo = System.getHeadInfo();
* System.log('headInfo.head_block_time: ' + headInfo.head_block_time.toString());
* ```
*/
export function setHeadInfo(headInfo: chain.head_info): void {
System.putObject(METADATA_SPACE, 'head_info', headInfo, chain.head_info.encode);
}
/**
* Set entry point that will be used when calling System.getEntryPoint()
* @param { u64 } lastIrreversibleBlock last irreversible block height to set
* @example
* ```ts
* MockVM.setLastIrreversibleBlock(987654321);
*
* const lastIrreversibleBlock = System.getLastIrreversibleBlock();
* System.log('lastIrreversibleBlock: ' + lastIrreversibleBlock.toString());
* ```
*/
export function setLastIrreversibleBlock(lastIrreversibleBlock: u64): void {
const lastIrreversibleBlockObj = new value.value_type();
lastIrreversibleBlockObj.uint64_value = lastIrreversibleBlock;
System.putObject(METADATA_SPACE, 'last_irreversible_block', lastIrreversibleBlockObj, value.value_type.encode);
}
/**
* Set caller data that will be used when calling System.getCaller()
* @param { chain.caller_data } callerData caller data to set
* @example
* ```ts
* let callerData = new chain.caller_data(Base58.decode('1DQzuCcTKacbs9GGScRTU1Hc8BsyARTPqe'), chain.privilege.user_mode);
*
* MockVM.setCaller(callerData);
*
* callerData = System.getCaller();
*
* System.log('callerData.caller_privilege: ' + callerData.caller_privilege.toString());
* if (callerData.caller) {
* System.log('callerData.caller (b58): ' + Base58.encode(callerData.caller!));
* }
* ```
*/
export function setCaller(callerData: chain.caller_data): void {
System.putObject(METADATA_SPACE, 'caller', callerData, chain.caller_data.encode);
}
/**
* Set transaction that will be used when calling System.getTransaction() and System.getTransactionField(...)
* @param { protocol.transaction } transaction transaction to set
* @example
* ```ts
* let transaction = new protocol.transaction();
* transaction.id = StringBytes.stringToBytes("0x12345");
*
* MockVM.setTransaction(transaction);
*
* transaction = System.getTransaction();
*
* System.log("transaction.id: " + (StringBytes.bytesToString((transaction.id)!)!));
*
* let txField = System.getTransactionField('id');
* if (txField) {
* System.log("transaction.id: " + (StringBytes.bytesToString((txField.bytes_value) as Uint8Array) as string));
* }
* ```
*/
export function setTransaction(transaction: protocol.transaction): void {
System.putObject(METADATA_SPACE, 'transaction', transaction, protocol.transaction.encode);
}
/**
* Set operation that will be used when calling System.getOperation()
* @param { protocol.operation } operation operation to set
* @example
* ```ts
* let setOperation = new protocol.operation();
* setOperation.set_system_contract = new protocol.set_system_contract_operation(Base58.decode('1DQzuCcTKacbs9GGScRTU1Hc8BsyARTPqe'), true);
*
* MockVM.setOperation(setOperation);
*
* const getOperation = System.getOperation();
* ...
* ```
*/
export function setOperation(operation: protocol.operation): void {
System.putObject(METADATA_SPACE, 'operation', operation, protocol.operation.encode);
}
/**
* Set block that will be used when calling System.getBlock() and System.getBlockField(...)
* @param { protocol.block } block block to set
* @example
* ```ts
* let block = new protocol.block();
* block.id = StringBytes.stringToBytes("0x12345");
*
* MockVM.setBlock(block);
*
* block = System.getBlock();
*
* System.log("block.id: " + (StringBytes.bytesToString((block.id) as Uint8Array) as string));
*
* let blField = System.getBlockField('id');
* if (blField) {
* System.log("block.id: " + (StringBytes.bytesToString((blField.bytes_value) as Uint8Array) as string));
* }
* ```
*/
export function setBlock(block: protocol.block): void {
System.putObject(METADATA_SPACE, 'block', block, protocol.block.encode);
}
/**
* Set authorities that will be used when calling System.requireAuthority(...)
* @param { MockAuthority[] } authorities authorities to set
* @example
* ```ts
* const account = Base58.decode('1DQzuCcTKacbs9GGScRTU1Hc8BsyARTPqe');
* const auth1 = new MockVM.MockAuthority(authority.authorization_type.contract_call, account, true);
* const auth2 = new MockVM.MockAuthority(authority.authorization_type.contract_upload, account, false)
*
* MockVM.setAuthorities([auth1, auth2]);
*
* System.requireAuthority(authority.authorization_type.contract_call, account);
* ```
*/
export function setAuthorities(authorities: MockAuthority[]): void {
const authoritiesListType = new value.list_type();
for (let index = 0; index < authorities.length; index++) {
const auth = authorities[index];
const authorityValueType = new value.value_type();
authorityValueType.bool_value = auth.authorized;
authorityValueType.bytes_value = auth.account;
authorityValueType.int32_value = auth.autorization_type;
authoritiesListType.values.push(authorityValueType);
}
System.putObject(METADATA_SPACE, 'authority', authoritiesListType, value.list_type.encode);
}
/**
* Set system authority that will be used when calling System.requireSystemAuthority(...)
* @param { bool } authorized
* ```ts
* MockVM.setSystemAuthority(true);
*
* System.requireSystemAuthority();
* ```
*/
export function setSystemAuthority(authorized: bool): void {
const systemAuthValueType = new value.value_type();
systemAuthValueType.bool_value = authorized;
System.putObject(METADATA_SPACE, 'system_authority', systemAuthValueType, value.value_type.encode);
}
/**
* Set results that will be used when calling System.verifyVRFProof(...)
* @param { Uint8Array[] } verifyVRFProofResults The results are FIFO, so the first System.verifyVRFPRoof(...) used in your code will use the first result you set in callContractResults, the second System.callContract(...) will get the second result, etc...
* @example
* ```ts
MockVM.setVerifyVRFProofResults([false, true]);
let callRes = System.verifyVRFProof(pubKey, proof, hash, messgae);
if (callRes) {
// Will execute
}
let callRes = System.verifyVRFProof(pubKey, proof, hash, messgae);
if (callRes) {
// Will not execute
}
* ```
*/
export function setVerifyVRFProofResults(verifyVRFProofResults: bool[]): void {
const verifyVRFProofResultListType = new value.list_type();
for (let index = 0; index < verifyVRFProofResults.length; index++) {
const callVerifyVRFProofValueType = new value.value_type();
callVerifyVRFProofValueType.bool_value = verifyVRFProofResults[index];
verifyVRFProofResultListType.values.push(callVerifyVRFProofValueType);
}
System.putObject(METADATA_SPACE, 'verify_vrf', verifyVRFProofResultListType, value.list_type.encode);
}
/**
* Set call contract results that will be used when calling System.callContract(...)
* @param { Uint8Array[] } callContractResults The results are FIFO, so the first System.callContract(...) used in your code will use the first result you set in callContractResults, the second System.callContract(...) will get the second result, etc...
* @example
* ```ts
* const callContractResults = new value.list_type();
const callContractRes1 = Base64.decode('res1');
const callContractRes2 = Base64.decode('res2');
MockVM.setCallContractResults([callContractRes1, callContractRes2]);
let callRes = System.callContract(contract_id, 1, new Uint8Array(0));
if (callRes) {
System.log('callRes1: ' + (Base64.encode(callRes as Uint8Array) as string));
}
callRes = System.callContract(contract_id, 1, new Uint8Array(0));
if (callRes) {
System.log('callRes2: ' + (Base64.encode(callRes as Uint8Array) as string));
}
* ```
*/
export function setCallContractResults(callContractResults: system_calls.exit_arguments[]): void {
const callContractResultListType = new value.list_type();
for (let index = 0; index < callContractResults.length; index++) {
const callContractResultValueType = new value.value_type();
callContractResultValueType.bytes_value = Protobuf.encode(callContractResults[index], system_calls.exit_arguments.encode);
callContractResultListType.values.push(callContractResultValueType);
}
System.putObject(METADATA_SPACE, 'call_contract_results', callContractResultListType, value.list_type.encode);
}
/**
* Get contract result set when calling System.exit()
* @returns { Uint8Array | null }
* @example
* ```ts
* System.setContractResult(Base64.decode('res1'));
*
* const contractRes = MockVM.getContractResult();
*
* if (contractRes) {
* System.log('contractRes: ' + (Base64.encode(contractRes as Uint8Array) as string));
* }
* ```
*/
export function getContractResult(): Uint8Array | null {
const bytes = System.getBytes(METADATA_SPACE, 'contract_result');
return bytes;
}
/**
* Get error message string after a VM error
* @returns { Uint8Array | null }
* @example
* ```ts
* const errorMessage = MockVM.getErrorMessage();
* ```
*/
export function getErrorMessage(): String | null {
const bytes = System.getBytes(METADATA_SPACE, 'error_message');
return StringBytes.bytesToString(bytes);
}
/**
* Get exit code set when calling System.exitContract(...)
* @returns { string[] }
* @example
* ```ts
* System.exit(0);
*
* const exitCode = MockVM.getExitCode();
*
* if (exitCode) {
* System.log('exitCode: ' + exitCode.toString());
* }
* ```
*/
export function getExitCode(): i32 {
const bytes = System.getBytes(METADATA_SPACE, 'exit_code');
if (bytes) {
const valueType = Protobuf.decode<value.value_type>(bytes, value.value_type.decode);
return valueType.int32_value;
}
return -1;
}
/**
* Get logs set when calling System.log()
* @returns { string[] }
* @example
* ```ts
* System.log('log 1');
* System.log('log 2');
*
* const logs = MockVM.getLogs();
*
* for (let index = 0; index < logs.length; index++) {
* const log = logs;
* }
* ```
*/
export function getLogs(): string[] {
const logs: string[] = [];
const logsBytes = System.getBytes(METADATA_SPACE, 'logs');
if (logsBytes) {
const logsListType = Protobuf.decode<value.list_type>(logsBytes, value.list_type.decode);
for (let index = 0; index < logsListType.values.length; index++) {
const log = logsListType.values[index];
logs.push(log.string_value!);
}
}
return logs;
}
/**
* Remove the current logs
* @example
* ```ts
* System.log('log 1');
* System.log('log 2');
* MockVM.clearLogs();
* System.log('log 3');
* System.log('log 4');
* console.log(MockVM.getLogs().join(","));
* // log 3,log 4
* ```
*/
export function clearLogs(): void {
System.putBytes(METADATA_SPACE, 'logs', new Uint8Array(0));
}
/**
* Get logs set when calling System.log()
* @returns { string[] }
* @example
* ```ts
* System.Event('my-event-1', Base64.decode('event data'), [Base58.decode('1DQzuCcTKacbs9GGScRTU1Hc8BsyARTPqe')]);
* System.Event('my-event-2', Base64.decode('event data 2'), [Base58.decode('1DQzuCcTKacbs9GGScRTU1Hc8BsyARTPqe')]);
*
* const events = MockVM.getEvents();
*
* for (let index = 0; index < events.length; index++) {
* const event = events[index];
*
* System.log(event.name)
* System.log(event.data.toString())
*
* event.impacted.forEach(acct => {
* System.log(Base58.encode(acct));
* });
* }
* ```
*/
export function getEvents(): system_calls.event_arguments[] {
const events: system_calls.event_arguments[] = [];
const eventsBytes = System.getBytes(METADATA_SPACE, 'events');
if (eventsBytes) {
const eventsListType = Protobuf.decode<value.list_type>(eventsBytes, value.list_type.decode);
for (let index = 0; index < eventsListType.values.length; index++) {
const eventBytes = eventsListType.values[index];
events.push(Protobuf.decode<system_calls.event_arguments>(eventBytes.bytes_value!, system_calls.event_arguments.decode));
}
}
return events;
}
/**
* Reset the MockVM database
* @example
* ```ts
* MockVM.reset();
* ```
*/
export function reset(): void {
System.putBytes(METADATA_SPACE, 'reset', new Uint8Array(0));
}
/**
* Rrestore the backup made via MockVM.commitTransaction()
* @example
* ```ts
* MockVM.rollbackTransaction();
* ```
*/
export function rollbackTransaction(): void {
System.putBytes(METADATA_SPACE, 'rollback_transaction', new Uint8Array(0));
}
/**
* Backup the database so that it can be rolledback to the backedup state if the transaction reverts
* @example
* ```ts
* MockVM.commitTransaction();
* ```
*/
export function commitTransaction(): void {
System.putBytes(METADATA_SPACE, 'commit_transaction', new Uint8Array(0));
}
}