Skip to content

Commit

Permalink
add mockOriginOperationId
Browse files Browse the repository at this point in the history
  • Loading branch information
peterjah committed Feb 28, 2024
1 parent f7e4d18 commit eb8ac1f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
20 changes: 20 additions & 0 deletions assembly/__tests__/vm-mock.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ import {
keccak256,
isEvmSignatureValid,
evmGetPubkeyFromSignature,
getOriginOperationId,
} from '../std';
import { changeCallStack, resetStorage } from '../vm-mock/storage';
import {
mockAdminContext,
mockOriginOperationId,
mockSetChainId,
setDeployContext,
setLocalContext,
Expand Down Expand Up @@ -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);
});
});
21 changes: 21 additions & 0 deletions assembly/vm-mock/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
10 changes: 10 additions & 0 deletions vm-mock/vm.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import sha3 from 'js-sha3';
let callerAddress = 'AU12UBnqTHDQALpocVBnkPNy7y5CndUJQTLutaVDDFgMJcq5kQiKq';
let contractAddress = 'AS12BqZEQ6sByhRLyEuf0YbQmcF2PsDdkNNG1akBJu9XcjZA1eT';

let mockedOriginOpId = '';

/**
* return a random string
*
Expand Down Expand Up @@ -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());
},

Expand Down

0 comments on commit eb8ac1f

Please sign in to comment.