Skip to content

Commit

Permalink
fix(protocol): update avg proof time and avg block time (#391)
Browse files Browse the repository at this point in the history
  • Loading branch information
dantaik authored Dec 9, 2022
1 parent 742ddcb commit 3681483
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 7 deletions.
4 changes: 3 additions & 1 deletion packages/protocol/contracts/L1/LibData.sol
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ library LibData {
// Changed when a block is proven/finalized
uint64 latestVerifiedHeight;
uint64 latestVerifiedId;
uint64 avgProofTime; // the proof time moving average
// the proof time moving average, note that for each block, only the
// first proof's time is considered.
uint64 avgProofTime;
uint64 __reservedC1;
// Reserved
uint256[42] __gap;
Expand Down
8 changes: 8 additions & 0 deletions packages/protocol/contracts/L1/v1/V1Proposing.sol
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,14 @@ library V1Proposing {
meta.mixHash = bytes32(block.difficulty);
}

state.avgBlockTime = V1Utils
.movingAverage({
maValue: state.avgBlockTime,
newValue: meta.timestamp - state.lastProposedAt,
maf: LibConstants.K_BLOCK_TIME_MAF
})
.toUint64();

state.saveProposedBlock(
state.nextBlockId,
LibData.ProposedBlock({
Expand Down
3 changes: 2 additions & 1 deletion packages/protocol/contracts/L1/v1/V1Proving.sol
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,8 @@ library V1Proving {
);

require(
block.timestamp < V1Utils.uncleProofDeadline(state, fc),
block.timestamp <
V1Utils.uncleProofDeadline(state, fc, target.id),
"L1:tooLate"
);

Expand Down
21 changes: 19 additions & 2 deletions packages/protocol/contracts/L1/v1/V1Utils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,26 @@ library V1Utils {
// Returns a deterministic deadline for uncle proof submission.
function uncleProofDeadline(
LibData.State storage state,
LibData.ForkChoice storage fc
LibData.ForkChoice storage fc,
uint256 blockId
) internal view returns (uint64) {
return fc.provenAt + state.avgProofTime;
if (blockId <= 2 * LibConstants.K_MAX_NUM_BLOCKS) {
return fc.provenAt + LibConstants.K_INITIAL_UNCLE_DELAY;
} else {
return fc.provenAt + state.avgProofTime;
}
}

function movingAverage(
uint256 maValue,
uint256 newValue,
uint256 maf
) internal pure returns (uint256) {
if (maValue == 0) {
return newValue;
}
uint256 _ma = (maValue * (maf - 1) + newValue) / maf;
return _ma > 0 ? _ma : maValue;
}

function setBit(
Expand Down
20 changes: 17 additions & 3 deletions packages/protocol/contracts/L1/v1/V1Verifying.sol
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,28 @@ library V1Verifying {
i++
) {
LibData.ForkChoice storage fc = state.forkChoices[i][latestL2Hash];
LibData.ProposedBlock storage target = LibData.getProposedBlock(
state,
i
);

// Uncle proof can not take more than 2x time the first proof did.
if (!_isVerifiable(state, fc)) {
if (!_isVerifiable(state, fc, i)) {
break;
} else {
if (fc.blockHash != LibConstants.K_BLOCK_DEADEND_HASH) {
latestL2Height += 1;
latestL2Hash = fc.blockHash;
}

state.avgProofTime = V1Utils
.movingAverage({
maValue: state.avgProofTime,
newValue: fc.provenAt - target.proposedAt,
maf: LibConstants.K_PROOF_TIME_MAF
})
.toUint64();

processed += 1;
emit BlockVerified(i, fc.blockHash);
_cleanUp(fc);
Expand Down Expand Up @@ -98,10 +111,11 @@ library V1Verifying {

function _isVerifiable(
LibData.State storage state,
LibData.ForkChoice storage fc
LibData.ForkChoice storage fc,
uint256 blockId
) private view returns (bool) {
return
fc.blockHash != 0 &&
block.timestamp > V1Utils.uncleProofDeadline(state, fc);
block.timestamp > V1Utils.uncleProofDeadline(state, fc, blockId);
}
}
5 changes: 5 additions & 0 deletions packages/protocol/contracts/libs/LibConstants.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ library LibConstants {
uint256 public constant K_TX_MIN_GAS_LIMIT = 21000; // TODO
uint256 public constant K_ANCHOR_TX_GAS_LIMIT = 250000;

uint256 public constant K_BLOCK_TIME_MAF = 1024;
uint256 public constant K_PROOF_TIME_MAF = 1024;

uint64 public constant K_INITIAL_UNCLE_DELAY = 60 minutes;

bytes4 public constant K_ANCHOR_TX_SELECTOR =
bytes4(keccak256("anchor(uint256,bytes32)"));

Expand Down

0 comments on commit 3681483

Please sign in to comment.