Skip to content

Commit

Permalink
add webstorer nonce
Browse files Browse the repository at this point in the history
  • Loading branch information
peterjah committed Sep 7, 2023
1 parent 155404f commit 4229146
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
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`);
}

0 comments on commit 4229146

Please sign in to comment.