Skip to content

Commit

Permalink
Merge pull request #83 from ethstorage/overried
Browse files Browse the repository at this point in the history
Variables that are assigned only once are changed to constants
  • Loading branch information
iteyelmp authored May 15, 2024
2 parents f1c9c17 + bc8237c commit 7538b23
Show file tree
Hide file tree
Showing 14 changed files with 10,593 additions and 314 deletions.
15 changes: 3 additions & 12 deletions contracts/DecentralizedKV.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,16 @@ pragma solidity ^0.8.0;
import "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
import "./MerkleLib.sol";
import "./BinaryRelated.sol";
import "./EthStorageConstants.sol";

contract DecentralizedKV is OwnableUpgradeable {
contract DecentralizedKV is OwnableUpgradeable, EthStorageConstants {
event Remove(uint256 indexed kvIdx, uint256 indexed lastKvIdx);

enum DecodeType {
RawData,
PaddingPer31Bytes
}

uint256 public storageCost; // Upfront storage cost (pre-dcf)
// Discounted cash flow factor in seconds
// E.g., 0.85 yearly discount in second = 0.9999999948465585 = 340282365167313208607671216367074279424 in Q128.128
uint256 public dcfFactor;
uint256 public startTime;
uint256 public maxKvSize;
uint40 public lastKvIdx; // number of entries in the store

struct PhyAddr {
Expand All @@ -35,14 +30,10 @@ contract DecentralizedKV is OwnableUpgradeable {
/* index - skey, reverse lookup */
mapping(uint256 => bytes32) internal idxMap;

function __init_KV(uint256 _maxKvSize, uint256 _startTime, uint256 _storageCost, uint256 _dcfFactor, address _owner) public onlyInitializing {
function __init_KV(address _owner) public onlyInitializing {
__Context_init();
__Ownable_init(_owner);
lastKvIdx = 0;
startTime = _startTime;
maxKvSize = _maxKvSize;
storageCost = _storageCost;
dcfFactor = _dcfFactor;
}

function pow(uint256 fp, uint256 n) internal pure returns (uint256) {
Expand Down
29 changes: 29 additions & 0 deletions contracts/EthStorageConstants.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;


abstract contract EthStorageConstants {
uint256 public constant maxKvSizeBits = 17; // maxKvSizeBits, 131072
uint256 public constant shardSizeBits = 39; // shardSizeBits ~ 512G
uint256 public constant shardEntryBits = shardSizeBits - maxKvSizeBits;
uint256 public constant sampleLenBits = maxKvSizeBits - sampleSizeBits;
uint256 public constant randomChecks = 2; // randomChecks
uint256 public constant minimumDiff = 4718592000; // minimumDiff 5 * 3 * 3600 * 1024 * 1024 / 12 = 4718592000 for 5 replicas that can have 1M IOs in one epoch
uint256 public constant cutoff = 7200; // cutoff = 2/3 * target internal (3 hours), 3 * 3600 * 2/3
uint256 public constant diffAdjDivisor = 32; // diffAdjDivisor
uint256 public constant treasuryShare = 100; // treasuryShare 10000 = 1.0, 100 means 1%
uint256 public constant prepaidAmount = 3145728000000000000000; // prepaidAmount - 50% * 2^39 / 131072 * 1500000Gwei, it also means 3145 ETH for half of the shard

// Upfront storage cost (pre-dcf)
uint256 public constant storageCost = 1500000000000000; // storageCost - 1,500,000Gwei forever per blob - https://ethresear.ch/t/ethstorage-scaling-ethereum-storage-via-l2-and-da/14223/6#incentivization-for-storing-m-physical-replicas-1
// Discounted cash flow factor in seconds
// E.g., 0.85 yearly discount in second = 0.9999999948465585 = 340282365167313208607671216367074279424 in Q128.128
uint256 public constant dcfFactor = 340282366367469178095360967382638002176; // it mean 0.95 for yearly discount
uint256 public constant startTime = 1713782077;
uint256 public constant maxKvSize = 1 << maxKvSizeBits; // 1 << maxKvSizeBits,131072

uint256 public constant sampleSizeBits = 5; // 32 bytes per sample

// maximum nonce per block
uint256 public constant nonceLimit = 1048576; // nonceLimit 1024 * 1024 = 1M samples and finish sampling in 1.3s with IO rate 6144 MB/s: 4k * 2(random checks) / 6144 = 1.3s
}
8 changes: 1 addition & 7 deletions contracts/EthStorageContract.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,10 @@ contract EthStorageContract is StorageContract, Decoder {
event PutBlob(uint256 indexed kvIdx, uint256 indexed kvSize, bytes32 indexed dataHash);

function initialize(
Config memory _config,
uint256 _startTime,
uint256 _storageCost,
uint256 _dcfFactor,
uint256 _nonceLimit,
address _treasury,
uint256 _prepaidAmount,
address _owner
) public payable initializer {
__init_storage(_config, _startTime, _storageCost, _dcfFactor, _nonceLimit, _treasury, _prepaidAmount, _owner);
__init_storage(_treasury, _owner);
}

function modExp(uint256 _b, uint256 _e, uint256 _m) internal view returns (uint256 result) {
Expand Down
3 changes: 2 additions & 1 deletion contracts/RLPReader.sol
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// SPDX-License-Identifier: MIT
/*
* @author Hamdi Allam [email protected]
* Please reach out with any questions or concerns
Expand Down Expand Up @@ -355,4 +356,4 @@ library RLPReader {
}
}
}
}
}
62 changes: 4 additions & 58 deletions contracts/StorageContract.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,66 +9,20 @@ import "./RandaoLib.sol";
* EthStorage L1 Contract with Decentralized KV Interface and Proof of Storage Verification.
*/
abstract contract StorageContract is DecentralizedKV {
struct Config {
uint256 maxKvSizeBits;
uint256 shardSizeBits;
uint256 randomChecks;
uint256 minimumDiff;
uint256 cutoff;
uint256 diffAdjDivisor;
uint256 treasuryShare; // 10000 = 1.0
}

uint256 public constant sampleSizeBits = 5; // 32 bytes per sample

uint256 public maxKvSizeBits;
uint256 public shardSizeBits;
uint256 public shardEntryBits;
uint256 public sampleLenBits;
uint256 public randomChecks;
uint256 public minimumDiff;
uint256 public cutoff;
uint256 public diffAdjDivisor;
uint256 public treasuryShare; // 10000 = 1.0
uint256 public prepaidAmount;

mapping(uint256 => MiningLib.MiningInfo) public infos;
uint256 public nonceLimit; // maximum nonce per block
address public treasury;
uint256 public prepaidLastMineTime;

function __init_storage(
Config memory _config,
uint256 _startTime,
uint256 _storageCost,
uint256 _dcfFactor,
uint256 _nonceLimit,
address _treasury,
uint256 _prepaidAmount,
address _owner
) public onlyInitializing {
/* Assumptions */
require(_config.shardSizeBits >= _config.maxKvSizeBits, "shardSize too small");
require(_config.maxKvSizeBits >= sampleSizeBits, "maxKvSize too small");
require(_config.randomChecks > 0, "At least one checkpoint needed");

__init_KV(1 << _config.maxKvSizeBits, _startTime, _storageCost, _dcfFactor, _owner);

shardSizeBits = _config.shardSizeBits;
maxKvSizeBits = _config.maxKvSizeBits;
shardEntryBits = _config.shardSizeBits - _config.maxKvSizeBits;
sampleLenBits = _config.maxKvSizeBits - sampleSizeBits;
randomChecks = _config.randomChecks;
minimumDiff = _config.minimumDiff;
cutoff = _config.cutoff;
diffAdjDivisor = _config.diffAdjDivisor;
treasuryShare = _config.treasuryShare;
nonceLimit = _nonceLimit;
__init_KV(_owner);

treasury = _treasury;
prepaidAmount = _prepaidAmount;
prepaidLastMineTime = _startTime;
prepaidLastMineTime = startTime;
// make sure shard0 is ready to mine and pay correctly
infos[0].lastMineTime = _startTime;
infos[0].lastMineTime = startTime;
}

event MinedBlock(
Expand Down Expand Up @@ -240,12 +194,4 @@ abstract contract StorageContract is DecentralizedKV {
) public virtual {
return _mine(blockNumber, shardId, miner, nonce, encodedSamples, masks, randaoProof, inclusiveProofs, decodeProof);
}

function setPrepaidAmount(uint256 _prepaidAmount) public onlyOwner {
prepaidAmount = _prepaidAmount;
}

function setMinimumDiff(uint256 _minimumDiff) public onlyOwner {
minimumDiff = _minimumDiff;
}
}
6 changes: 1 addition & 5 deletions contracts/TestDecentralizedKV.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,9 @@ contract TestDecentralizedKV is DecentralizedKV {
mapping(uint256 => bytes) internal dataMap;

function initialize(
uint256 _maxKvSize,
uint256 _startTime,
uint256 _storageCost,
uint256 _dcfFactor,
address owner
) public initializer {
__init_KV(_maxKvSize, _startTime, _storageCost, _dcfFactor, owner);
__init_KV(owner);
}

function setTimestamp(uint256 ts) public {
Expand Down
20 changes: 20 additions & 0 deletions hardhat.config.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
require("dotenv").config();
const fs = require('fs');

require("@nomiclabs/hardhat-etherscan");
require("@nomiclabs/hardhat-waffle");
require("@nomiclabs/hardhat-web3");
require("hardhat-gas-reporter");
require("solidity-coverage");

const { execSync } = require("child_process");

// This is a sample Hardhat task. To learn how to create your own go to
// https://hardhat.org/guides/create-task.html
task("accounts", "Prints the list of accounts", async (taskArgs, hre) => {
Expand All @@ -16,6 +19,23 @@ task("accounts", "Prints the list of accounts", async (taskArgs, hre) => {
}
});

const deployment_file = "scripts/deployment.json";
task("verify-contract", "Verify contract", async (taskArgs, hre) => {
if (!fs.existsSync(deployment_file)) {
return;
}
const cmd = "npx hardhat verify --network sepolia ";
const data = fs.readFileSync(deployment_file);
const config = JSON.parse(data);

if (config.impl) {
execSync(`${cmd}${config.impl}`);
}
if (config.proxy) {
execSync(`${cmd}${config.proxy}`);
}
console.log("Verify contract success!");
});
// You need to export an object to set up your config
// Go to https://hardhat.org/config/ to learn more

Expand Down
Loading

0 comments on commit 7538b23

Please sign in to comment.