Skip to content

Commit

Permalink
vm-mock: add current slot mock
Browse files Browse the repository at this point in the history
  • Loading branch information
peterjah committed Sep 19, 2024
1 parent 01baac3 commit 69bb9f4
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
8 changes: 7 additions & 1 deletion assembly/__tests__/env-helpers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { env } from '../env';
import { Address } from '../std';
import { addAddressToLedger } from '../vm-mock';
import { addAddressToLedger, mockCurrentSlot } from '../vm-mock';

const testAddress = new Address(
'AU12E6N5BFAdC2wyiBV6VJjqkWhpz1kLVp2XpbRdSnL1mKjCWT6oR',
Expand All @@ -17,6 +17,12 @@ describe('Testing env coins related functions', () => {
expect(env.currentThread()).toBe(1);
});

test('current slot', () => {
mockCurrentSlot(1234, 8);
expect(env.currentPeriod()).toBe(1234);
expect(env.currentThread()).toBe(8);
});

test('setBytecode', () => {
env.setBytecode(new StaticArray(0));
const byteCode = env.getBytecode().toString();
Expand Down
3 changes: 3 additions & 0 deletions assembly/vm-mock/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,6 @@ export declare function mockSetChainId(value: number): void;
*/
@external("massa", "assembly_script_set_origin_operation_id")
export declare function mockOriginOperationId(opId: string): void;

@external("massa", "assembly_script_set_slot")
export declare function mockCurrentSlot(period: u64, thread: u8): void;
13 changes: 11 additions & 2 deletions vm-mock/vm.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ let adminContext = false;

let webModule;

let currentSlot = { period: 0n, thread: 1 };

const scCallMockStack = [];
let callCoins = 0n; // Default value, coins for a call
let spentCoins = 0n; // Coins spent during the call
Expand Down Expand Up @@ -698,11 +700,18 @@ export default function createMockedABI(
},

assembly_script_get_current_period() {
return BigInt(0);
return currentSlot.period;
},

assembly_script_get_current_thread() {
return 1;
return currentSlot.thread;
},

assembly_script_set_slot(period, thread) {
currentSlot = {
period: BigInt(period),
thread,
};
},

assembly_script_set_bytecode(bytecodePtr) {
Expand Down

0 comments on commit 69bb9f4

Please sign in to comment.