-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
267 additions
and
215 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 2 additions & 7 deletions
9
smart-contracts/assembly/contracts/utils/__tests__/ownership.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
smart-contracts/assembly/contracts/utils/ownership-internal.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { | ||
Context, | ||
Storage, | ||
createEvent, | ||
generateEvent, | ||
} from '@massalabs/massa-as-sdk'; | ||
|
||
export const OWNER_KEY = 'OWNER'; | ||
|
||
export const CHANGE_OWNER_EVENT_NAME = 'CHANGE_OWNER'; | ||
|
||
/** | ||
* Sets the contract owner. This function is to be called from a smart contract. | ||
* | ||
* @param newOwner - The address of the new contract owner. | ||
* | ||
* Emits a CHANGE_OWNER event upon successful execution. | ||
*/ | ||
export function _setOwner(newOwner: string): void { | ||
if (Storage.has(OWNER_KEY)) { | ||
_onlyOwner(); | ||
} | ||
Storage.set(OWNER_KEY, newOwner); | ||
|
||
generateEvent(createEvent(CHANGE_OWNER_EVENT_NAME, [newOwner])); | ||
} | ||
|
||
/** | ||
* Checks if the given account is the owner of the contract. | ||
* | ||
* @param account - The address of the account to check. | ||
* @returns true if the account is the owner, false otherwise. | ||
*/ | ||
export function _isOwner(account: string): bool { | ||
if (!Storage.has(OWNER_KEY)) { | ||
return false; | ||
} | ||
return account === Storage.get(OWNER_KEY); | ||
} | ||
|
||
/** | ||
* Check if the caller is the contract owner. | ||
* | ||
* @throws Will throw an error if the caller is not the owner or if the owner is not set. | ||
*/ | ||
export function _onlyOwner(): void { | ||
assert(Storage.has(OWNER_KEY), 'Owner is not set'); | ||
const owner = Storage.get(OWNER_KEY); | ||
assert(Context.caller().toString() === owner, 'Caller is not the owner'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 84 additions & 21 deletions
105
smart-contracts/assembly/contracts/website-deployer/__tests__/websiteDeployer.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,93 @@ | ||
import { Args } from '@massalabs/as-types'; | ||
import { Storage } from '@massalabs/massa-as-sdk'; | ||
import { Args, i32ToBytes, unwrapStaticArray } from '@massalabs/as-types'; | ||
import { | ||
Storage, | ||
resetStorage, | ||
setDeployContext, | ||
} from '@massalabs/massa-as-sdk'; | ||
import { | ||
NB_CHUNKS_KEY, | ||
appendBytesToWebsite, | ||
initializeWebsite, | ||
keyExpectedNbChunks, | ||
} from '../websiteDeployer'; | ||
constructor, | ||
deleteWebsite, | ||
} from '../websiteStorer'; | ||
import { isOwner } from '../../utils'; | ||
|
||
const user = 'AU12UBnqTHDQALpocVBnkPNy7y5CndUJQTLutaVDDFgMJcq5kQiKq'; | ||
|
||
describe('website deployer tests', () => { | ||
test('initializeWebsite', () => { | ||
// execute | ||
const value: u64 = 52; | ||
beforeAll(() => { | ||
resetStorage(); | ||
setDeployContext(user); | ||
|
||
constructor([]); | ||
}); | ||
|
||
test('owner is set', () => { | ||
expect(isOwner(new Args().add(user).serialize())).toBeTruthy(); | ||
}); | ||
|
||
throws('delete fails if website not created', () => { | ||
const chunkId = i32(0); | ||
const data = new Uint8Array(100); | ||
data.fill(0xf); | ||
deleteWebsite(new Args().add(chunkId).add(data).serialize()); | ||
}); | ||
|
||
test('upload a website', () => { | ||
const nbChunks = 20; | ||
|
||
initializeWebsite(new Args().add(value).serialize()); | ||
// assert | ||
expect(Storage.get(keyExpectedNbChunks).nextU64().unwrap()).toBe(value); | ||
for (let chunkId: i32 = 0; chunkId < nbChunks; chunkId++) { | ||
const data = new Uint8Array(100); | ||
data.fill(chunkId & 0xf); | ||
const argsAppend = new Args().add(chunkId).add(data).serialize(); | ||
appendBytesToWebsite(argsAppend); | ||
|
||
expect(Storage.get(NB_CHUNKS_KEY)).toStrictEqual(i32ToBytes(chunkId + 1)); | ||
expect(Storage.get(i32ToBytes(chunkId))).toStrictEqual( | ||
unwrapStaticArray(data), | ||
); | ||
} | ||
|
||
expect(Storage.get(NB_CHUNKS_KEY)).toStrictEqual(i32ToBytes(nbChunks)); | ||
}); | ||
|
||
test('edit a website (upload missed chunks)', () => { | ||
const nbChunks = 15; | ||
for (let chunkId: i32 = 0; chunkId < nbChunks; chunkId++) { | ||
const data = new Uint8Array(100); | ||
data.fill(chunkId & 0xf); | ||
const argsAppend = new Args().add(chunkId).add(data).serialize(); | ||
appendBytesToWebsite(argsAppend); | ||
|
||
expect(Storage.get(i32ToBytes(chunkId))).toStrictEqual( | ||
unwrapStaticArray(data), | ||
); | ||
} | ||
}); | ||
|
||
test('append to website', () => { | ||
const chunkId = u64(5); | ||
const want = new Uint8Array(2); | ||
want.set([0, 1]); | ||
const MASSA_WEB_CHUNKS = `massa_web_${chunkId}`; | ||
const MASSA_WEB_CHUNKS_ARR = new Args().add(MASSA_WEB_CHUNKS).serialize(); | ||
const argsAppend = new Args().add(chunkId).add(want).serialize(); | ||
appendBytesToWebsite(argsAppend); | ||
const got = new Args(Storage.get(MASSA_WEB_CHUNKS_ARR)).nextUint8Array(); | ||
expect(got.unwrap()).toStrictEqual(want); | ||
test('delete website', () => { | ||
deleteWebsite([]); | ||
expect(Storage.has(NB_CHUNKS_KEY)).toBeFalsy(); | ||
const nbChunks = 20; | ||
for (let chunkId: i32 = 0; chunkId < nbChunks; chunkId++) { | ||
expect(Storage.has(i32ToBytes(chunkId))).toBeFalsy(); | ||
} | ||
}); | ||
|
||
test('update website', () => { | ||
const nbChunks = 9; | ||
for (let chunkId: i32 = 0; chunkId < nbChunks; chunkId++) { | ||
const data = new Uint8Array(100); | ||
data.fill(chunkId & 0xf); | ||
const argsAppend = new Args().add(chunkId).add(data).serialize(); | ||
appendBytesToWebsite(argsAppend); | ||
|
||
expect(Storage.get(NB_CHUNKS_KEY)).toStrictEqual(i32ToBytes(chunkId + 1)); | ||
expect(Storage.get(i32ToBytes(chunkId))).toStrictEqual( | ||
unwrapStaticArray(data), | ||
); | ||
} | ||
|
||
expect(Storage.get(NB_CHUNKS_KEY)).toStrictEqual(i32ToBytes(nbChunks)); | ||
}); | ||
}); |
24 changes: 0 additions & 24 deletions
24
smart-contracts/assembly/contracts/website-deployer/main-websiteDeployer.ts
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.