You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

169 lines
4.8 KiB
Solidity

//SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.0;
import "hardhat/console.sol";
import "./IFileOperator.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
2 years ago
import "evm-large-storage/contracts/LargeStorageManager.sol";
// import "evm-large-storage/contracts/W3RC3.sol";
contract Git3 is LargeStorageManager {
struct refInfo {
bytes20 hash;
uint96 index;
}
struct refData {
bytes20 hash;
2 years ago
bytes name;
}
mapping(bytes => address) public repoNameToOwner;
2 years ago
mapping(bytes => refInfo) public nameToRefInfo; // dev => {hash: 0x1234..., index: 1 }
bytes[] public refs; // [main, dev, test, staging]
function _convertRefInfo(
refInfo memory info
) internal view returns (refData memory res) {
res.hash = info.hash;
res.name = refs[info.index];
}
constructor() LargeStorageManager(0) {}
2 years ago
modifier onlyOwner(bytes memory repoName) {
2 years ago
require(repoNameToOwner[repoName] == msg.sender, "only owner");
_;
}
function download(
bytes memory repoName,
bytes memory path
) external view returns (bytes memory, bool) {
2 years ago
// call flat directory(FD)
2 years ago
return _get(keccak256(bytes.concat(repoName, "/", path)));
2 years ago
}
2 years ago
function _createRepo(bytes memory repoName) internal {
if (repoNameToOwner[repoName] == address(0)) {
repoNameToOwner[repoName] = msg.sender;
} else {
require(repoNameToOwner[repoName] == msg.sender, "only owner");
}
}
2 years ago
function upload(
bytes memory repoName,
bytes memory path,
bytes calldata data
) external payable {
_createRepo(repoName);
_putChunkFromCalldata(
keccak256(bytes.concat(repoName, "/", path)),
0,
data,
msg.value
);
}
function uploadChunk(
bytes memory repoName,
bytes memory path,
uint256 chunkId,
bytes calldata data
2 years ago
) external payable {
_createRepo(repoName);
_putChunkFromCalldata(
keccak256(bytes.concat(repoName, "/", path)),
chunkId,
data,
msg.value
);
}
2 years ago
function remove(
bytes memory repoName,
bytes memory path
) external onlyOwner(repoName) {
// The actually process of remove will remove all the chunks
2 years ago
_remove(keccak256(bytes.concat(repoName, "/", path)), 0);
}
2 years ago
function size(
bytes memory repoName,
bytes memory name
) external view returns (uint256, uint256) {
return _size(keccak256(bytes.concat(repoName, "/", name)));
}
2 years ago
function countChunks(
bytes memory repoName,
bytes memory name
) external view returns (uint256) {
return _countChunks(keccak256(bytes.concat(repoName, "/", name)));
}
function listRefs() public view returns (refData[] memory list) {
2 years ago
// todo: Differentiate all refs corresponding to a repo
list = new refData[](refs.length);
for (uint index = 0; index < refs.length; index++) {
list[index] = _convertRefInfo(nameToRefInfo[refs[index]]);
}
}
2 years ago
function setRef(
bytes memory repoName,
bytes memory name,
bytes20 refHash
) public {
bytes memory fullName = bytes.concat(repoName, "/", name);
// only execute `sload` once to reduce gas consumption
refInfo memory srs;
2 years ago
srs = nameToRefInfo[fullName];
uint256 refsLen = refs.length;
2 years ago
_createRepo(repoName);
if (srs.hash == bytes20(0)) {
// store refHash for the first time
require(
refsLen <= uint256(uint96(int96(-1))),
"Refs exceed valid length"
);
2 years ago
nameToRefInfo[fullName].hash = refHash;
nameToRefInfo[fullName].index = uint96(refsLen);
2 years ago
refs.push(fullName);
} else {
// only update refHash
2 years ago
nameToRefInfo[fullName].hash = refHash;
}
}
2 years ago
function delRef(
bytes memory repoName,
bytes memory name
) public onlyOwner(repoName) {
bytes memory fullName = bytes.concat(repoName, "/", name);
// only execute `sload` once to reduce gas consumption
refInfo memory srs;
2 years ago
srs = nameToRefInfo[fullName];
uint256 refsLen = refs.length;
2 years ago
require(
srs.hash != bytes20(0),
"Reference of this name does not exist"
);
require(srs.index < refsLen, "System Error: Invalid index");
if (srs.index < refsLen - 1) {
refs[srs.index] = refs[refsLen - 1];
nameToRefInfo[refs[refsLen - 1]].index = srs.index;
}
refs.pop();
2 years ago
delete nameToRefInfo[fullName];
}
}