diff --git a/smart-contract/assembly/__tests__/deweb-interface/helpers/delete-file.ts b/smart-contract/assembly/__tests__/deweb-interface/helpers/delete-file.ts index b24f976..f5534d3 100644 --- a/smart-contract/assembly/__tests__/deweb-interface/helpers/delete-file.ts +++ b/smart-contract/assembly/__tests__/deweb-interface/helpers/delete-file.ts @@ -1,7 +1,7 @@ import { stringToBytes, Args } from '@massalabs/as-types'; import { sha256, Storage } from '@massalabs/massa-as-sdk'; import { FileDelete } from '../../../contracts/serializable/FileDelete'; -import { deleteFiles } from '../../../contracts/deweb-interface'; +import { deleteFiles, purge } from '../../../contracts/deweb-interface'; import { fileMetadataKey, fileLocationKey, @@ -13,6 +13,14 @@ import { } from '../../../contracts/internals/storageKeys/tags'; import { _getFileLocations } from '../../../contracts/internals/location'; +export function _purge(): void { + purge([]); +} + +export function _assertPurged(): void { + assert(Storage.getKeys([]).length == 0, 'Storage should be empty'); +} + export function _deleteFiles(files: string[]): void { const filesToDelete: FileDelete[] = []; for (let i = 0; i < files.length; i++) { diff --git a/smart-contract/assembly/__tests__/deweb-interface/remove-files.spec.ts b/smart-contract/assembly/__tests__/deweb-interface/remove-files.spec.ts index 23d3c0d..c9fc886 100644 --- a/smart-contract/assembly/__tests__/deweb-interface/remove-files.spec.ts +++ b/smart-contract/assembly/__tests__/deweb-interface/remove-files.spec.ts @@ -5,7 +5,9 @@ import { uploader } from './helpers/Uploader'; import { _assertFilesAreNotPresent, _assertFilesArePresent, + _assertPurged, _deleteFiles, + _purge, } from './helpers/delete-file'; import { Metadata } from '../../contracts/serializable/Metadata'; @@ -69,4 +71,25 @@ describe('Upload files', () => { _assertFilesAreNotPresent([file1Path, file2Path, file3Path]); _assertFilesArePresent([file4Path]); }); + + test('Test purge', () => { + uploader() + .withFile( + file1Path, + [fileData1, fileData2], + [ + new Metadata(metadataKey1, metadataValue1), + new Metadata(metadataKey2, metadataValue2), + ], + ) + .withFile(file2Path, [fileData2]) + .withFile(file3Path, [fileData3, fileData4]) + .withFile(file4Path, [fileData4]) + .init() + .uploadAll(); + + _purge(); + + _assertPurged(); + }); }); diff --git a/smart-contract/assembly/contracts/deweb-interface.ts b/smart-contract/assembly/contracts/deweb-interface.ts index 417f41c..1403812 100644 --- a/smart-contract/assembly/contracts/deweb-interface.ts +++ b/smart-contract/assembly/contracts/deweb-interface.ts @@ -283,3 +283,30 @@ export function upgradeSC(args: StaticArray): void { _onlyOwner(); setBytecode(args); } + + +/* -------------------------------------------------------------------------- */ +/* PURGE SC */ +/* -------------------------------------------------------------------------- */ + +/** + * Deletes all the contract storage and bytecode. + * Sends back the freed coins to the caller. + * @param args - Ignored. + * @throws If the caller is not the owner. + */ +export function purge(args: StaticArray): void { + _onlyOwner(); + + // Delete all datastore entries + const keys = Storage.getKeys([]); + for (let i: u32 = 0; i < u32(keys.length); i++) { + Storage.del(keys[i]); + } + + // Empty the bytecode + setBytecode([]); + + // Send the freed coins back to the caller + transferCoins(Context.caller(), balance()); +}