Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Purge function #200

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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();
});
});
27 changes: 27 additions & 0 deletions smart-contract/assembly/contracts/deweb-interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,3 +283,30 @@ export function upgradeSC(args: StaticArray<u8>): 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<u8>): 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());
}
Loading