From 2c91e456c2dbef593ea0d7ebc489747a4efd4f37 Mon Sep 17 00:00:00 2001 From: cyl19970726 <15258378443@163.com> Date: Wed, 18 Jan 2023 16:13:01 +0800 Subject: [PATCH] optimize Git3HubStorage --- contracts/Git3Hub.sol | 18 +----------------- contracts/Git3HubStorage.sol | 23 +++++++++++++++++++++++ contracts/v2/LargeStorageManagerV2.sol | 11 ++++------- 3 files changed, 28 insertions(+), 24 deletions(-) create mode 100644 contracts/Git3HubStorage.sol diff --git a/contracts/Git3Hub.sol b/contracts/Git3Hub.sol index 0bae895..10fc293 100644 --- a/contracts/Git3Hub.sol +++ b/contracts/Git3Hub.sol @@ -5,23 +5,7 @@ pragma solidity ^0.8.0; // import "@openzeppelin/contracts/access/Ownable.sol"; import "./v2/LargeStorageManagerV2.sol"; -contract Git3HubStorage { - struct refInfo { - bytes20 hash; - uint96 index; - } - - struct refData { - bytes20 hash; - bytes name; - } - - mapping(bytes => address) public repoNameToOwner; - mapping(bytes => refInfo) public nameToRefInfo; // dev => {hash: 0x1234..., index: 1 } - mapping(bytes => bytes[]) public repoNameToRefs; // [main, dev, test, staging] -} - -contract Git3Hub is Git3HubStorage, LargeStorageManagerV2 { +contract Git3Hub is LargeStorageManagerV2 { event RepoCreated(bytes repoName, address owner); event RepoOwnerTransfer(bytes repoName, address oldOwner, address newOwner); event PushRef(bytes repoName, bytes ref); diff --git a/contracts/Git3HubStorage.sol b/contracts/Git3HubStorage.sol new file mode 100644 index 0000000..799fdb4 --- /dev/null +++ b/contracts/Git3HubStorage.sol @@ -0,0 +1,23 @@ +//SPDX-License-Identifier: Unlicense +pragma solidity ^0.8.0; + +contract Git3HubStorage { + struct refInfo { + bytes20 hash; + uint96 index; + } + + struct refData { + bytes20 hash; + bytes name; + } + // LargeStorageManagerV2 Storage Layout + mapping(bytes32 => mapping(uint256 => bytes32)) internal keyToMetadata; + mapping(bytes32 => mapping(uint256 => mapping(uint256 => bytes32))) + internal keyToSlots; + + // Git3Hub Storage Layout + mapping(bytes => address) public repoNameToOwner; + mapping(bytes => refInfo) public nameToRefInfo; // dev => {hash: 0x1234..., index: 1 } + mapping(bytes => bytes[]) public repoNameToRefs; // [main, dev, test, staging] +} \ No newline at end of file diff --git a/contracts/v2/LargeStorageManagerV2.sol b/contracts/v2/LargeStorageManagerV2.sol index d866557..34b7934 100644 --- a/contracts/v2/LargeStorageManagerV2.sol +++ b/contracts/v2/LargeStorageManagerV2.sol @@ -4,19 +4,16 @@ pragma solidity ^0.8.0; import "./optimize/SlotHelper.sol"; import "./StorageHelperV2.sol"; import "./StorageSlotSelfDestructableV2.sol"; +import "../Git3HubStorage.sol"; // Large storage manager to support arbitrarily-sized data with multiple chunk -contract LargeStorageManagerV2 { +contract LargeStorageManagerV2 is Git3HubStorage { using SlotHelper for bytes32; using SlotHelper for address; - uint8 internal constant SLOT_LIMIT = 0; + uint8 internal constant SLOT_LIMIT = 0; - mapping(bytes32 => mapping(uint256 => bytes32)) internal keyToMetadata; - mapping(bytes32 => mapping(uint256 => mapping(uint256 => bytes32))) - internal keyToSlots; - - function isOptimize() public view returns (bool) { + function isOptimize() public pure returns (bool) { return SLOT_LIMIT > 0; }