diff --git a/assembly/__tests__/vm-mock.spec.ts b/assembly/__tests__/vm-mock.spec.ts index 89ae8ea6..a0745d0a 100644 --- a/assembly/__tests__/vm-mock.spec.ts +++ b/assembly/__tests__/vm-mock.spec.ts @@ -10,10 +10,12 @@ import { keccak256, isEvmSignatureValid, evmGetPubkeyFromSignature, + getOriginOperationId, } from '../std'; import { changeCallStack, resetStorage } from '../vm-mock/storage'; import { mockAdminContext, + mockOriginOperationId, mockSetChainId, setDeployContext, setLocalContext, @@ -305,3 +307,21 @@ describe('Testing mocked Chain id', () => { expect(env.chainId()).toBe(9_000_000); }); }); + +describe('Testing mocked origin operation id', () => { + const mockedOpId = 'imAniceOpId'; + beforeEach(() => { + mockOriginOperationId(mockedOpId); + }); + + it('operation Id is mocked', () => { + const opId = getOriginOperationId(); + expect(opId).toBe(mockedOpId); + }); + + it('operation Id mock is unset', () => { + mockOriginOperationId(''); + const opId = getOriginOperationId(); + expect(opId).not.toBe(mockedOpId); + }); +}); diff --git a/assembly/vm-mock/env.ts b/assembly/vm-mock/env.ts index 00d78983..cd905501 100644 --- a/assembly/vm-mock/env.ts +++ b/assembly/vm-mock/env.ts @@ -168,3 +168,24 @@ export declare function setLocalContext(address?: string): void; @external("massa", "assembly_script_set_chain_id") export declare function mockSetChainId(value: number): void; + +/** + * Set mock for origin operation Id. + * + * @remarks + * This function is used to mock the origin operation Id for test purpose. + * If an empty string is passed, the origin operation Id will be reset to the default value (random generated). + * Don't forget to reset the mock after the test. + * + * @example + * ```typescript + * test('mock origin opId', () => { + * const opId = 123; + * mockOriginOperationId(opId); + * const res = getOriginOperationId()(); + * expect(res).toBe(opId); + * }); + * ``` + */ +@external("massa", "assembly_script_set_origin_operation_id") +export declare function mockOriginOperationId(opId: string): void; diff --git a/vm-mock/vm.js b/vm-mock/vm.js index 71630acd..1bfcc079 100644 --- a/vm-mock/vm.js +++ b/vm-mock/vm.js @@ -10,6 +10,8 @@ import sha3 from 'js-sha3'; let callerAddress = 'AU12UBnqTHDQALpocVBnkPNy7y5CndUJQTLutaVDDFgMJcq5kQiKq'; let contractAddress = 'AS12BqZEQ6sByhRLyEuf0YbQmcF2PsDdkNNG1akBJu9XcjZA1eT'; +let mockedOriginOpId = ''; + /** * return a random string * @@ -801,7 +803,15 @@ export default function createMockedABI( ); }, + assembly_script_set_origin_operation_id(dataPtr) { + const opId = ptrToString(dataPtr); + mockedOriginOpId = opId; + }, + assembly_script_get_origin_operation_id() { + if(mockedOriginOpId !== '') { + return newString(mockedOriginOpId); + } return newString(generateRandOpId()); },