Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Variables that are assigned only once are changed to constants #83

Merged
merged 13 commits into from
May 15, 2024
13 changes: 6 additions & 7 deletions contracts/DecentralizedKV.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ contract DecentralizedKV is OwnableUpgradeable {
PaddingPer31Bytes
}

uint256 public storageCost; // Upfront storage cost (pre-dcf)
// 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
iteyelmp marked this conversation as resolved.
Show resolved Hide resolved
// 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 constant dcfFactor = 340282366367469178095360967382638002176; // it mean 0.95 for yearly discount
uint256 public constant maxKvSize = 1 << 17; // 1 << maxKvSizeBits,131072

uint256 public startTime;
uint256 public maxKvSize;
uint40 public lastKvIdx; // number of entries in the store

struct PhyAddr {
Expand All @@ -35,14 +37,11 @@ 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(uint256 _startTime, 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
8 changes: 3 additions & 5 deletions contracts/EthStorageContract.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,14 @@ contract EthStorageContract is StorageContract, Decoder {
event PutBlob(uint256 indexed kvIdx, uint256 indexed kvSize, bytes32 indexed dataHash);

function initialize(
Config memory _config,
uint256 _minimumDiff,
uint256 _startTime,
uint256 _storageCost,
uint256 _dcfFactor,
uint256 _nonceLimit,
address _treasury,
uint256 _prepaidAmount,
address _treasury,
address _owner
) public payable initializer {
__init_storage(_config, _startTime, _storageCost, _dcfFactor, _nonceLimit, _treasury, _prepaidAmount, _owner);
__init_storage(_minimumDiff, _startTime, _nonceLimit, _prepaidAmount, _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 {
}
}
}
}
}
56 changes: 17 additions & 39 deletions contracts/StorageContract.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,19 @@ 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 constant maxKvSizeBits = 17; // 131072
uint256 public constant shardSizeBits = 39; // ~ 512G
uint256 public constant shardEntryBits = shardSizeBits - maxKvSizeBits;
uint256 public constant sampleLenBits = maxKvSizeBits - sampleSizeBits;
uint256 public constant randomChecks = 2;
uint256 public constant cutoff = 7200; // cutoff = 2/3 * target internal (3 hours), 3 * 3600 * 2/3
uint256 public constant diffAdjDivisor = 32;
uint256 public constant treasuryShare = 100; // 10000 = 1.0, 100 means 1%

uint256 public minimumDiff;
uint256 public cutoff;
uint256 public diffAdjDivisor;
uint256 public treasuryShare; // 10000 = 1.0
uint256 public prepaidAmount;

mapping(uint256 => MiningLib.MiningInfo) public infos;
Expand All @@ -38,35 +30,21 @@ abstract contract StorageContract is DecentralizedKV {
uint256 public prepaidLastMineTime;

function __init_storage(
Config memory _config,
uint256 _minimumDiff,
uint256 _startTime,
uint256 _storageCost,
uint256 _dcfFactor,
uint256 _nonceLimit,
address _treasury,
uint256 _prepaidAmount,
address _treasury,
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;
__init_KV(_startTime, _owner);

minimumDiff = _minimumDiff;
prepaidLastMineTime = _startTime;
nonceLimit = _nonceLimit;
treasury = _treasury;
prepaidAmount = _prepaidAmount;
prepaidLastMineTime = _startTime;
treasury = _treasury;

// make sure shard0 is ready to mine and pay correctly
infos[0].lastMineTime = _startTime;
}
Expand Down
5 changes: 1 addition & 4 deletions contracts/TestDecentralizedKV.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,10 @@ 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(_startTime, owner);
}

function setTimestamp(uint256 ts) public {
Expand Down
Loading