Skip to content

Commit

Permalink
Merge branch 'main' into 215-execution-related-functions
Browse files Browse the repository at this point in the history
  • Loading branch information
gregLibert authored Apr 13, 2023
2 parents 5c0c05c + 50be47d commit c782a7c
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
56 changes: 56 additions & 0 deletions assembly/__tests__/env-opdatastore.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { env } from '../env/index';

import { changeCallStack, resetStorage } from '../vm-mock/storage';
import { Args, bytesToString, stringToBytes } from '@massalabs/as-types';

describe('Testing mocked Operation Datastore', () => {
beforeAll(() => {
resetStorage();
});

afterEach(() => {
resetStorage();
});

test('hasOpKey - key not exists', () => {
expect(env.hasOpKey(stringToBytes('key1'))).toBeNull();
});

test('getOpKey - key exists', () => {
env.set(stringToBytes('key1'), stringToBytes('value1'));
expect(env.hasOpKey(stringToBytes('key1'))).not.toBeNull();
});

test('getOpData - key not exists', () => {
expect(env.getOpData(stringToBytes('key1'))).toBeNull();
});

test('getOpData - key exists', () => {
env.set(stringToBytes('key1'), stringToBytes('value1'));
expect(env.getOpData(stringToBytes('key1'))).not.toBeNull();
});

test('getOpKeys - no keys', () => {
const expected: StaticArray<u8> = [0, 0, 0, 0];
expect(env.getOpKeys()).toStrictEqual(expected);
});

test('getOpKeys - keys found', () => {
env.set(stringToBytes('key1'), stringToBytes('value1'));
env.set(stringToBytes('key2'), stringToBytes('value2'));
env.set(stringToBytes('key3'), stringToBytes('value3'));

expect(env.getOpKeys()).not.toBeNull();
});

test('getOpKeys - expected specific keys ', () => {
env.set(stringToBytes('key1'), stringToBytes('value1'));
env.set(stringToBytes('key2'), stringToBytes('value2'));
env.set(stringToBytes('key3'), stringToBytes('value3'));
const expected: StaticArray<u8> = [
3, 0, 0, 0, 4, 107, 101, 121, 49, 4, 107, 101, 121, 50, 4, 107, 101, 121,
51,
];
expect(env.getOpKeys()).toStrictEqual(expected);
});
});
26 changes: 26 additions & 0 deletions vm-mock/vm.js
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,31 @@ export default function createMockedABI(

assembly_script_get_remaining_gas() {
return BigInt(1000000000000000000);

assembly_script_has_op_key(kPtr) {
const k = ptrToUint8ArrayString(kPtr);
const addressStorage = ledger.get(contractAddress).storage;

if (!addressStorage.has(k)) return newArrayBuffer();

return newArrayBuffer(addressStorage.get(k));
},

assembly_script_get_op_keys() {
const addressStorage = ledger.get(contractAddress).storage;
const keysArr = Array.from(addressStorage.keys());
const keys = serializeKeys(keysArr);

return newArrayBuffer(keys);
},

assembly_script_get_op_data(kPtr) {
const k = ptrToUint8ArrayString(kPtr);
const addressStorage = ledger.get(contractAddress).storage;

if (!addressStorage.has(k)) return newArrayBuffer();

return newArrayBuffer(addressStorage.get(k));
},

assembly_script_get_owned_addresses() {
Expand Down Expand Up @@ -417,6 +442,7 @@ export default function createMockedABI(
`sendMessage: function ${calledFunction} will be called in ${address}`,
);
},

},
};

Expand Down

0 comments on commit c782a7c

Please sign in to comment.