Skip to content

Commit

Permalink
Merge pull request #9 from fxhash/feat/reuse-enum-for-type
Browse files Browse the repository at this point in the history
feat: use enum for type in checksum
  • Loading branch information
stevennevins authored Nov 29, 2023
2 parents 9178614 + 25e4934 commit 449906f
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 11 deletions.
8 changes: 6 additions & 2 deletions src/FileSystem.sol
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ contract FileSystem is IFileSystem {
bytes32[] memory hashedFilenames = hashFileNames(_fileNames);
directoryChecksum = keccak256(
bytes.concat(
DIRECTORY_TYPE,
bytes1(uint8(InodeType.Directory)),
keccak256(abi.encodePacked(hashedFilenames)),
keccak256(abi.encodePacked(_fileChecksums))
)
Expand All @@ -76,7 +76,11 @@ contract FileSystem is IFileSystem {
if (!IContentStore(CONTENT_STORE).checksumExists(_chunkPointers[i])) revert ChunkNotFound();
}
fileChecksum = keccak256(
bytes.concat(FILE_TYPE, keccak256(abi.encodePacked(_chunkPointers)), keccak256(_metadata))
bytes.concat(
bytes1(uint8(InodeType.File)),
keccak256(abi.encodePacked(_chunkPointers)),
keccak256(_metadata)
)
);
if (inodeExists(fileChecksum)) revert InodeAlreadyExists();
File memory newFile = File(_metadata, _chunkPointers);
Expand Down
4 changes: 0 additions & 4 deletions src/utils/Constants.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ pragma solidity 0.8.23;
CONSTANTS
//////////////////////////////////////////////////////////////////////////*/

// Metadata
bytes1 constant DIRECTORY_TYPE = bytes1(0x01);
bytes1 constant FILE_TYPE = bytes1(0x00);

// Characters
uint256 constant CHARACTER_LENGTH = 18;
string constant FORBIDDEN_CHARS = ":/?#[]@!$&'()*+,;=";
2 changes: 1 addition & 1 deletion test/FileSystem/CreateDirectory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ contract CreateDirectory is FileSystemTest {
hashedNames = fileSystem.hashFileNames(fileNames);
checksum = keccak256(
abi.encodePacked(
DIRECTORY_TYPE,
bytes1(uint8(InodeType.Directory)),
keccak256(abi.encodePacked(hashedNames)),
keccak256(abi.encodePacked(filePointers))
)
Expand Down
12 changes: 10 additions & 2 deletions test/FileSystem/CreateFile.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ contract CreateFile is FileSystemTest {
function test_CreateFile() public {
fileSystem.createFile(metadata, chunkChecksums);
checksum = keccak256(
abi.encodePacked(FILE_TYPE, keccak256(abi.encodePacked(chunkChecksums)), keccak256(metadata))
abi.encodePacked(
bytes1(uint8(InodeType.File)),
keccak256(abi.encodePacked(chunkChecksums)),
keccak256(metadata)
)
);
assertTrue(fileSystem.inodeExists(checksum));
}
Expand Down Expand Up @@ -53,7 +57,11 @@ contract CreateFile is FileSystemTest {
chunkChecksums.push(chunkChecksums[0]);
fileSystem.createFile(metadata, chunkChecksums);
checksum = keccak256(
abi.encodePacked(FILE_TYPE, keccak256(abi.encodePacked(chunkChecksums)), keccak256(metadata))
abi.encodePacked(
bytes1(uint8(InodeType.File)),
keccak256(abi.encodePacked(chunkChecksums)),
keccak256(metadata)
)
);
assertTrue(fileSystem.inodeExists(checksum));
}
Expand Down
2 changes: 1 addition & 1 deletion test/FileSystem/FileSystemTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import "forge-std/Test.sol";
import "src/utils/Constants.sol";

import {ContentStore} from "ethfs/packages/contracts/src/ContentStore.sol";
import {FileSystem} from "src/FileSystem.sol";
import {FileSystem, InodeType} from "src/FileSystem.sol";
import {BytesLib} from "test/utils/BytesLib.sol";

import {IContentStore} from "ethfs/packages/contracts/src/IContentStore.sol";
Expand Down
2 changes: 1 addition & 1 deletion test/FileSystem/ReadDirectory.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ contract ReadDirectory is FileSystemTest {
hashedFileNames = fileSystem.hashFileNames(fileNames);
checksum = keccak256(
abi.encodePacked(
DIRECTORY_TYPE,
bytes1(uint8(InodeType.Directory)),
keccak256(abi.encodePacked(hashedFileNames)),
keccak256(abi.encodePacked(filePointers))
)
Expand Down

0 comments on commit 449906f

Please sign in to comment.