Skip to content

Commit

Permalink
using l1 block time
Browse files Browse the repository at this point in the history
  • Loading branch information
qzhodl committed Jun 11, 2024
1 parent 4133af6 commit 8f36177
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 14 deletions.
40 changes: 37 additions & 3 deletions contracts/EthStorageContractL2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import "./EthStorageContract2.sol";

interface IL1Block {
function blockHash(uint256 _historyNumber) external view returns (bytes32);

function number() external view returns (uint64);

function timestamp() external view returns (uint64);
}

contract EthStorageContractL2 is EthStorageContract2 {
Expand All @@ -18,14 +22,44 @@ contract EthStorageContractL2 is EthStorageContract2 {
uint256 _dcfFactor
) EthStorageContract2(_config, _startTime, _storageCost, _dcfFactor) {}

function _getRandao(uint256 l1BlockNumber, bytes calldata headerRlpBytes) internal view override returns (bytes32) {
function getRandao(uint256 l1BlockNumber, bytes calldata headerRlpBytes) internal view returns (bytes32) {
bytes32 bh = l1Block.blockHash(l1BlockNumber);
require(bh != bytes32(0), "failed to obtain blockhash");

return RandaoLib.verifyHeaderAndGetRandao(bh, headerRlpBytes);
}

function _getMaxMiningDrift() internal pure override returns (uint256) {
return maxL2MiningDrift;
function _mine(
uint256 blockNumber,
uint256 shardId,
address miner,
uint256 nonce,
bytes32[] memory encodedSamples,
uint256[] memory masks,
bytes calldata randaoProof,
bytes[] calldata inclusiveProofs,
bytes[] calldata decodeProof
) internal override {
// Obtain the blockhash of the block number of recent blocks
require(l1Block.number() - blockNumber <= maxL1MiningDrift, "block number too old");
// To avoid stack too deep, we resue the hash0 instead of using randao

bytes32 hash0 = getRandao(blockNumber, randaoProof);
// Estimate block timestamp
uint256 mineTs = l1Block.timestamp() - (l1Block.number() - blockNumber) * 12;

// Given a blockhash and a miner, we only allow sampling up to nonce limit times.
require(nonce < nonceLimit, "nonce too big");

// Check if the data matches the hash in metadata and obtain the solution hash.
hash0 = keccak256(abi.encode(miner, hash0, nonce));
hash0 = verifySamples(shardId, hash0, miner, encodedSamples, masks, inclusiveProofs, decodeProof);

// Check difficulty
uint256 diff = _calculateDiffAndInitHashSingleShard(shardId, mineTs);
uint256 required = uint256(2 ** 256 - 1) / diff;
require(uint256(hash0) <= required, "diff not match");

_rewardMiner(shardId, miner, mineTs, diff);
}
}
14 changes: 3 additions & 11 deletions contracts/StorageContract.sol
Original file line number Diff line number Diff line change
Expand Up @@ -248,11 +248,11 @@ abstract contract StorageContract is DecentralizedKV {
bytes calldata randaoProof,
bytes[] calldata inclusiveProofs,
bytes[] calldata decodeProof
) internal {
) internal virtual {
// Obtain the blockhash of the block number of recent blocks
require(block.number - blockNumber <= _getMaxMiningDrift(), "block number too old");
require(block.number - blockNumber <= maxL1MiningDrift, "block number too old");
// To avoid stack too deep, we resue the hash0 instead of using randao
bytes32 hash0 = _getRandao(blockNumber, randaoProof);
bytes32 hash0 = RandaoLib.verifyHistoricalRandao(blockNumber, randaoProof);
// Estimate block timestamp
uint256 mineTs = block.timestamp - (block.number - blockNumber) * 12;

Expand All @@ -270,12 +270,4 @@ abstract contract StorageContract is DecentralizedKV {

_rewardMiner(shardId, miner, mineTs, diff);
}

function _getRandao(uint256 l1BlockNumber, bytes calldata headerRlpBytes) internal view virtual returns (bytes32) {
return RandaoLib.verifyHistoricalRandao(l1BlockNumber, headerRlpBytes);
}

function _getMaxMiningDrift() internal pure virtual returns (uint256) {
return maxL1MiningDrift;
}
}

0 comments on commit 8f36177

Please sign in to comment.