Skip to content

Commit

Permalink
feat(protocol): Move proofTimeTarget to state var and adjust scripts/…
Browse files Browse the repository at this point in the history
…tests (#13769)

Co-authored-by: Daniel Wang <99078276+dantaik@users.noreply.github.com>
  • Loading branch information
adaki2004 and dantaik authored May 16, 2023
1 parent a9305d5 commit 40086b1
Show file tree
Hide file tree
Showing 16 changed files with 165 additions and 24 deletions.
2 changes: 0 additions & 2 deletions packages/protocol/contracts/L1/TaikoConfig.sol
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ library TaikoConfig {
ethDepositGas: 21000,
ethDepositMaxFee: 1 ether / 10,
txListCacheExpiry: 0,
// 85s based on A2 testnet status, or set to 1800 for 30mins (mainnet mock)
proofTimeTarget: 1800,
adjustmentQuotient: 16,
relaySignalRoot: false
});
Expand Down
4 changes: 2 additions & 2 deletions packages/protocol/contracts/L1/TaikoData.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ library TaikoData {
uint64 maxEthDepositsPerBlock;
uint96 maxEthDepositAmount;
uint96 minEthDepositAmount;
uint64 proofTimeTarget;
uint8 adjustmentQuotient;
bool relaySignalRoot;
}
Expand All @@ -40,6 +39,7 @@ library TaikoData {
uint64 genesisTimestamp;
uint64 numBlocks;
uint64 proofTimeIssued;
uint64 proofTimeTarget;
uint64 lastVerifiedBlockId;
uint64 accProposedAt;
uint64 nextEthDepositToProcess;
Expand Down Expand Up @@ -150,7 +150,7 @@ library TaikoData {
uint64 blockFee;
uint64 proofTimeIssued;
uint64 lastVerifiedBlockId;
uint64 __reserved91;
uint64 proofTimeTarget;
// Reserved
uint256[42] __gap;
}
Expand Down
2 changes: 2 additions & 0 deletions packages/protocol/contracts/L1/TaikoEvents.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ abstract contract TaikoEvents {
event BlockVerified(uint256 indexed id, bytes32 blockHash);

event EthDeposited(TaikoData.EthDeposit deposit);

event ProofTimeTargetChanged(uint64 proofTimeTarget);
}
22 changes: 22 additions & 0 deletions packages/protocol/contracts/L1/TaikoL1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@ contract TaikoL1 is EssentialContract, ICrossChainSync, TaikoEvents, TaikoErrors
* @param _addressManager The AddressManager address.
* @param _genesisBlockHash The block hash of the genesis block.
* @param _initBlockFee Initial (reasonable) block fee value.
* @param _initProofTimeTarget Initial (reasonable) proof submission time target.
* @param _initProofTimeIssued Initial proof time issued corresponding
* with the initial block fee.
*/
function init(
address _addressManager,
bytes32 _genesisBlockHash,
uint64 _initBlockFee,
uint64 _initProofTimeTarget,
uint64 _initProofTimeIssued
) external initializer {
EssentialContract._init(_addressManager);
Expand All @@ -53,6 +55,7 @@ contract TaikoL1 is EssentialContract, ICrossChainSync, TaikoEvents, TaikoErrors
config: getConfig(),
genesisBlockHash: _genesisBlockHash,
initBlockFee: _initBlockFee,
initProofTimeTarget: _initProofTimeTarget,
initProofTimeIssued: _initProofTimeIssued
});
}
Expand Down Expand Up @@ -131,6 +134,25 @@ contract TaikoL1 is EssentialContract, ICrossChainSync, TaikoEvents, TaikoErrors
});
}

/**
* Change proof parameters (time target and time issued) - to avoid complex/risky upgrades in case need to change relatively frequently.
* @param newProofTimeTarget New proof time target.
* @param newProofTimeIssued New proof time issued. If set to type(uint64).max, let it be unchanged.
*/
function setProofParams(uint64 newProofTimeTarget, uint64 newProofTimeIssued) external onlyOwner {
if (newProofTimeTarget == 0 || newProofTimeIssued == 0)
revert L1_INVALID_PARAM();

state.proofTimeTarget = newProofTimeTarget;
// Special case in a way - that we leave the proofTimeIssued unchanged
// because we think provers will adjust behavior.
if (newProofTimeIssued != type(uint64).max) {
state.proofTimeIssued = newProofTimeIssued;
}

emit ProofTimeTargetChanged(newProofTimeTarget);
}

function depositTaikoToken(uint256 amount) external nonReentrant {
LibTokenomics.depositTaikoToken(state, AddressResolver(this), amount);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/protocol/contracts/L1/libs/LibProving.sol
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ library LibProving {
fc.prover = evidence.prover;

if (evidence.prover == address(1)) {
fc.provenAt = uint64(block.timestamp.max(blk.proposedAt + config.proofTimeTarget));
fc.provenAt = uint64(block.timestamp.max(blk.proposedAt + state.proofTimeTarget));
} else {
fc.provenAt = uint64(block.timestamp);
}
Expand Down
8 changes: 4 additions & 4 deletions packages/protocol/contracts/L1/libs/LibTokenomics.sol
Original file line number Diff line number Diff line change
Expand Up @@ -88,20 +88,20 @@ library LibTokenomics {
TaikoData.Config memory config,
uint64 proofTime
) internal view returns (uint64 newProofTimeIssued, uint64 blockFee) {
newProofTimeIssued = (state.proofTimeIssued > config.proofTimeTarget)
? state.proofTimeIssued - config.proofTimeTarget
newProofTimeIssued = (state.proofTimeIssued > state.proofTimeTarget)
? state.proofTimeIssued - state.proofTimeTarget
: uint64(0);
newProofTimeIssued += proofTime;

uint256 x = (newProofTimeIssued * Math.SCALING_FACTOR_1E18)
/ (config.proofTimeTarget * config.adjustmentQuotient);
/ (state.proofTimeTarget * config.adjustmentQuotient);

if (Math.MAX_EXP_INPUT <= x) {
x = Math.MAX_EXP_INPUT;
}

uint256 result = (uint256(Math.exp(int256(x))) / Math.SCALING_FACTOR_1E18)
/ (config.proofTimeTarget * config.adjustmentQuotient);
/ (state.proofTimeTarget * config.adjustmentQuotient);

blockFee = uint64(result.min(type(uint64).max));
}
Expand Down
1 change: 1 addition & 0 deletions packages/protocol/contracts/L1/libs/LibUtils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ library LibUtils {
genesisTimestamp: state.genesisTimestamp,
numBlocks: state.numBlocks,
proofTimeIssued: state.proofTimeIssued,
proofTimeTarget: state.proofTimeTarget,
lastVerifiedBlockId: state.lastVerifiedBlockId,
accProposedAt: state.accProposedAt,
nextEthDepositToProcess: state.nextEthDepositToProcess,
Expand Down
5 changes: 4 additions & 1 deletion packages/protocol/contracts/L1/libs/LibVerifying.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ library LibVerifying {
TaikoData.Config memory config,
bytes32 genesisBlockHash,
uint64 initBlockFee,
uint64 initProofTimeTarget,
uint64 initProofTimeIssued
) internal {
if (
Expand All @@ -43,14 +44,16 @@ library LibVerifying {
// EIP-4844 blob deleted after 30 days
|| config.txListCacheExpiry > 30 * 24 hours || config.ethDepositGas == 0
|| config.ethDepositMaxFee == 0 || config.ethDepositMaxFee >= type(uint96).max
|| config.proofTimeTarget == 0 || config.adjustmentQuotient == 0
|| config.adjustmentQuotient == 0 || initProofTimeTarget == 0 || initProofTimeIssued == 0
) revert L1_INVALID_CONFIG();

uint64 timeNow = uint64(block.timestamp);
state.genesisHeight = uint64(block.number);
state.genesisTimestamp = timeNow;

state.blockFee = initBlockFee;
state.proofTimeIssued = initProofTimeIssued;
state.proofTimeTarget = initProofTimeTarget;
state.numBlocks = 1;

TaikoData.Block storage blk = state.blocks[0];
Expand Down
16 changes: 14 additions & 2 deletions packages/protocol/script/DeployOnL1.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,18 @@ contract DeployOnL1 is Script {

uint256 public taikoTokenPremintAmount = vm.envUint("TAIKO_TOKEN_PREMINT_AMOUNT");

// Change it based on 'consensus' / experience / expected result
// Based in seconds. Please set carefully.
// For testnet it could be somewhere 85-100s
// For mainnet it could be around 1800 s (30mins)
// Can be adjusted later with setters
uint64 public INITIAL_PROOF_TIME_TARGET = uint64(vm.envUint("INITIAL_PROOF_TIME_TARGET"));

TaikoL1 taikoL1;
address public addressManagerProxy;

error FAILED_TO_DEPLOY_PLONK_VERIFIER(string contractPath);
error PROOF_TIME_TARGET_NOT_SET();

function run() external {
require(owner != address(0), "owner is zero");
Expand Down Expand Up @@ -107,9 +115,13 @@ contract DeployOnL1 is Script {
// Calculating it for our needs based on testnet/mainnet. We need it in
// order to make the fees on the same level - in ideal circumstences.
// See Brecht's comment https://github.com/taikoxyz/taiko-mono/pull/13564
if(INITIAL_PROOF_TIME_TARGET == 0) {
revert PROOF_TIME_TARGET_NOT_SET();
}

uint64 initProofTimeIssued = LibLn.calcInitProofTimeIssued(
feeBase,
uint16(taikoL1.getConfig().proofTimeTarget),
uint16(INITIAL_PROOF_TIME_TARGET),
uint8(taikoL1.getConfig().adjustmentQuotient)
);

Expand All @@ -118,7 +130,7 @@ contract DeployOnL1 is Script {
address(taikoL1),
bytes.concat(
taikoL1.init.selector,
abi.encode(addressManagerProxy, genesisHash, feeBase, initProofTimeIssued)
abi.encode(addressManagerProxy, genesisHash, feeBase, initProofTimeIssued, initProofTimeIssued)
)
);
setAddress("taiko", taikoL1Proxy);
Expand Down
22 changes: 22 additions & 0 deletions packages/protocol/script/DetermineNewProofTimeIssued.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;

import "forge-std/Script.sol";
import "forge-std/console2.sol";
import {LibLn} from "../test/LibLn.sol";

uint16 constant DESIRED_PROOF_TIME_TARGET = 500;
uint8 constant ADJUSTMENT_QUOTIENT = 16;

contract DetermineProofTimeIssued is Script {

function run() public {
uint16 proofTimeTarget = DESIRED_PROOF_TIME_TARGET; // Approx. value which close to what is in the simulation
uint64 feeBase = 1e8; // 1 TKO
uint64 initProofTimeIssued =
LibLn.calcInitProofTimeIssued(feeBase, proofTimeTarget, ADJUSTMENT_QUOTIENT);

console2.log("The proof time target is:", proofTimeTarget);
console2.log("The associated proof time issued is:", initProofTimeIssued);
}
}
1 change: 1 addition & 0 deletions packages/protocol/script/test_deploy_on_l1.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ TAIKO_TOKEN_PREMINT_RECIPIENT=0xa0Ee7A142d267C1f36714E4a8F75612F20a79720 \
TAIKO_TOKEN_PREMINT_AMOUNT=0xffff \
TREASURE=0xa0Ee7A142d267C1f36714E4a8F75612F20a79720 \
L2_GENESIS_HASH=0xee1950562d42f0da28bd4550d88886bc90894c77c9c9eaefef775d4c8223f259 \
INITIAL_PROOF_TIME_TARGET=101 \
forge script script/DeployOnL1.s.sol:DeployOnL1 \
--fork-url http://localhost:8545 \
--broadcast \
Expand Down
11 changes: 5 additions & 6 deletions packages/protocol/test/TaikoL1.sim.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {LibLn} from "./LibLn.sol";

/// @dev Tweak this if you iwhs to set - the config and the calculation of the proofTimeIssued
/// @dev also originates from this
uint16 constant PROOF_TIME_TARGET = 375; //sec. Approx mainnet scenario
uint16 constant INITIAL_PROOF_TIME_TARGET = 375; //sec. Approx mainnet scenario

/// @dev Warning: this test will take 7-10 minutes and require 1GB memory.
/// `pnpm sim`
Expand All @@ -25,7 +25,6 @@ contract TaikoL1_b is TaikoL1 {
config.ringBufferSize = 1200;
config.maxVerificationsPerTx = 10;
config.proofCooldownPeriod = 1 minutes;
config.proofTimeTarget = PROOF_TIME_TARGET;
config.realProofSkipSize = 0;
}
}
Expand Down Expand Up @@ -86,7 +85,7 @@ contract TaikoL1Simulation is TaikoL1TestBase {
}

function setUp() public override {
uint16 proofTimeTarget = PROOF_TIME_TARGET; // Approx. value which close to what is in the simulation
proofTimeTarget = INITIAL_PROOF_TIME_TARGET; // Approx. value which close to what is in the simulation

initProofTimeIssued =
LibLn.calcInitProofTimeIssued(feeBase, proofTimeTarget, ADJUSTMENT_QUOTIENT);
Expand Down Expand Up @@ -162,7 +161,7 @@ contract TaikoL1Simulation is TaikoL1TestBase {
uint256 proposedIndex;

console2.log("Last second:", maxTime);
console2.log("Proof time target:", PROOF_TIME_TARGET);
console2.log("Proof time target:", INITIAL_PROOF_TIME_TARGET);
console2.log("Average proposal time: ", totalDiffsProp / blocksToSimulate);
console2.log("Average proof time: ", totalDiffsProve / blocksToSimulate);
printVariableHeaders();
Expand Down Expand Up @@ -340,7 +339,7 @@ contract TaikoL1Simulation is TaikoL1TestBase {
uint256 proposedIndex;

console2.log("Last second:", maxTime);
console2.log("Proof time target:", PROOF_TIME_TARGET);
console2.log("Proof time target:", INITIAL_PROOF_TIME_TARGET);
console2.log("Average proposal time: ", totalDiffsProp / blocksToSimulate);
console2.log("Average proof time: ", totalDiffsProve / blocksToSimulate);
printVariableHeaders();
Expand Down Expand Up @@ -523,7 +522,7 @@ contract TaikoL1Simulation is TaikoL1TestBase {
uint256 proposedIndex;

console2.log("Last second:", maxTime);
console2.log("Proof time target:", PROOF_TIME_TARGET);
console2.log("Proof time target:", INITIAL_PROOF_TIME_TARGET);
console2.log("Average proposal time: ", totalDiffsProp / blocksToSimulate);
console2.log("Average proof time: ", totalDiffsProve / blocksToSimulate);
printVariableHeaders();
Expand Down
7 changes: 5 additions & 2 deletions packages/protocol/test/TaikoL1LibTokenomicsMainnet.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ import {Strings} from "@openzeppelin/contracts/utils/Strings.sol";
import {TaikoL1TestBase} from "./TaikoL1TestBase.t.sol";
import {LibLn} from "./LibLn.sol";

/// @dev Tweak this if you iwhs to set - the config and the calculation of the proofTimeIssued
/// @dev also originates from this
uint16 constant INITIAL_PROOF_TIME_TARGET = 2160; //sec. Approx mainnet scenario

contract TaikoL1MainnetMockConfig is TaikoL1 {
function getConfig() public pure override returns (TaikoData.Config memory config) {
config = TaikoConfig.getConfig();
Expand All @@ -23,7 +27,6 @@ contract TaikoL1MainnetMockConfig is TaikoL1 {
config.maxNumProposedBlocks = 200;
config.ringBufferSize = 240;
config.proofCooldownPeriod = 0;
config.proofTimeTarget = 2160;
}
}

Expand All @@ -41,7 +44,7 @@ contract TaikoL1LibTokenomicsMainnet is TaikoL1TestBase {
}

function setUp() public override {
uint16 proofTimeTarget = 2160; // Approx. mainnet value
proofTimeTarget = INITIAL_PROOF_TIME_TARGET; // Approx. mainnet value
// Calculating it for our needs based on testnet/mainnet proof vars.
// See Brecht's comment https://github.com/taikoxyz/taiko-mono/pull/13564
initProofTimeIssued =
Expand Down
Loading

0 comments on commit 40086b1

Please sign in to comment.