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 webstorer nonce #125

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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,11 +1,17 @@
import { Args, i32ToBytes, unwrapStaticArray } from '@massalabs/as-types';
import {
Args,
i32ToBytes,
u64ToBytes,
unwrapStaticArray,
} from '@massalabs/as-types';
import {
Storage,
resetStorage,
setDeployContext,
} from '@massalabs/massa-as-sdk';
import {
NB_CHUNKS_KEY,
NONCE_KEY,
appendBytesToWebsite,
constructor,
deleteWebsite,
Expand Down Expand Up @@ -49,6 +55,7 @@ describe('website deployer tests', () => {
}

expect(Storage.get(NB_CHUNKS_KEY)).toStrictEqual(i32ToBytes(nbChunks));
expect(Storage.get(NONCE_KEY)).toStrictEqual(u64ToBytes(0));
});

test('edit a website (upload missed chunks)', () => {
Expand All @@ -63,6 +70,8 @@ describe('website deployer tests', () => {
unwrapStaticArray(data),
);
}

expect(Storage.get(NONCE_KEY)).toStrictEqual(u64ToBytes(0));
});

test('delete website', () => {
Expand All @@ -72,6 +81,8 @@ describe('website deployer tests', () => {
for (let chunkId: i32 = 0; chunkId < nbChunks; chunkId++) {
expect(Storage.has(i32ToBytes(chunkId))).toBeFalsy();
}

expect(Storage.get(NONCE_KEY)).toStrictEqual(u64ToBytes(1));
});

test('update website', () => {
Expand All @@ -89,5 +100,6 @@ describe('website deployer tests', () => {
}

expect(Storage.get(NB_CHUNKS_KEY)).toStrictEqual(i32ToBytes(nbChunks));
expect(Storage.get(NONCE_KEY)).toStrictEqual(u64ToBytes(2));
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@ import { Storage, Context, generateEvent } from '@massalabs/massa-as-sdk';
import {
Args,
bytesToI32,
bytesToU64,
i32ToBytes,
stringToBytes,
u64ToBytes,
} from '@massalabs/as-types';
import { onlyOwner } from '../utils';
import { _setOwner } from '../utils/ownership-internal';
import { isDeployingContract } from '@massalabs/massa-as-sdk/assembly/std/context';

export const NB_CHUNKS_KEY = stringToBytes('NB_CHUNKS');
export const NONCE_KEY = stringToBytes('NONCE');

/**
* Constructor function that initializes the contract owner.
Expand Down Expand Up @@ -56,6 +59,14 @@ export function appendBytesToWebsite(binaryArgs: StaticArray<u8>): void {
Storage.set(NB_CHUNKS_KEY, i32ToBytes(chunkNb + 1));
}

// if we are uploading a new website version, increment the nonce
if (!totalChunks) {
let nonce: u64 = Storage.has(NONCE_KEY)
? bytesToU64(Storage.get(NONCE_KEY)) + 1
: 0;
Storage.set(NONCE_KEY, u64ToBytes(nonce));
}

generateEvent(
`Website chunk ${chunkNb} deployed to ${Context.callee().toString()}`,
);
Expand Down Expand Up @@ -84,5 +95,9 @@ export function deleteWebsite(_: StaticArray<u8>): void {
}
Storage.del(NB_CHUNKS_KEY);

// increment nonce
let nonce: u64 = bytesToU64(Storage.get(NONCE_KEY));
Storage.set(NONCE_KEY, u64ToBytes(++nonce));

generateEvent(`Website ${Context.callee().toString()} deleted successfully`);
}
Loading