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

fix(protocol): update avg proof time and avg block time #391

Merged
merged 9 commits into from
Dec 9, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
12 changes: 12 additions & 0 deletions packages/protocol/contracts/L1/v1/V1Utils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,18 @@ library V1Utils {
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(
LibData.State storage state,
uint64 mask,
Expand Down
13 changes: 13 additions & 0 deletions packages/protocol/contracts/L1/v1/V1Verifying.sol
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ 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)) {
Expand All @@ -70,6 +74,15 @@ library V1Verifying {
latestL2Height += 1;
latestL2Hash = fc.blockHash;
}

state.avgProofTime = V1Utils
davidtaikocha marked this conversation as resolved.
Show resolved Hide resolved
.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
3 changes: 3 additions & 0 deletions packages/protocol/contracts/libs/LibConstants.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ 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;

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

Expand Down