Skip to content

Commit

Permalink
feat(protocol): Add a setter where all fee calc params can be set wit…
Browse files Browse the repository at this point in the history
…h one go (#13816)

Co-authored-by: adaki2004 <[email protected]>
Co-authored-by: David <[email protected]>
  • Loading branch information
3 people committed May 25, 2023
1 parent f23c895 commit eb29686
Show file tree
Hide file tree
Showing 14 changed files with 247 additions and 48 deletions.
1 change: 0 additions & 1 deletion packages/protocol/contracts/L1/TaikoConfig.sol
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ library TaikoConfig {
ethDepositGas: 21000,
ethDepositMaxFee: 1 ether / 10,
txListCacheExpiry: 0,
adjustmentQuotient: 32000,
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 @@ -27,7 +27,6 @@ library TaikoData {
uint64 maxEthDepositsPerBlock;
uint96 maxEthDepositAmount;
uint96 minEthDepositAmount;
uint16 adjustmentQuotient;
bool relaySignalRoot;
}

Expand Down Expand Up @@ -136,7 +135,8 @@ library TaikoData {
// Slot 7: never or rarely changed
uint64 genesisHeight;
uint64 genesisTimestamp;
uint64 __reserved71;
uint16 adjustmentQuotient;
uint48 __reserved71;
uint64 __reserved72;
// Slot 8
uint64 accProposedAt;
Expand Down
4 changes: 3 additions & 1 deletion packages/protocol/contracts/L1/TaikoEvents.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,7 @@ abstract contract TaikoEvents {

event EthDeposited(TaikoData.EthDeposit deposit);

event ProofTimeTargetChanged(uint64 proofTimeTarget);
event ProofParamsChanged(
uint64 proofTimeTarget, uint64 proofTimeIssued, uint64 blockFee, uint16 adjustmentQuotient
);
}
33 changes: 26 additions & 7 deletions packages/protocol/contracts/L1/TaikoL1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,15 @@ contract TaikoL1 is EssentialContract, ICrossChainSync, TaikoEvents, TaikoErrors
* @param _initProofTimeTarget Initial (reasonable) proof submission time target.
* @param _initProofTimeIssued Initial proof time issued corresponding
* with the initial block fee.
* @param _adjustmentQuotient Block fee calculation adjustment quotient.
*/
function init(
address _addressManager,
bytes32 _genesisBlockHash,
uint64 _initBlockFee,
uint64 _initProofTimeTarget,
uint64 _initProofTimeIssued
uint64 _initProofTimeIssued,
uint16 _adjustmentQuotient
) external initializer {
EssentialContract._init(_addressManager);
LibVerifying.init({
Expand All @@ -56,7 +58,8 @@ contract TaikoL1 is EssentialContract, ICrossChainSync, TaikoEvents, TaikoErrors
genesisBlockHash: _genesisBlockHash,
initBlockFee: _initBlockFee,
initProofTimeTarget: _initProofTimeTarget,
initProofTimeIssued: _initProofTimeIssued
initProofTimeIssued: _initProofTimeIssued,
adjustmentQuotient: _adjustmentQuotient
});
}

Expand Down Expand Up @@ -138,11 +141,15 @@ 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.
* @param newBlockFee New blockfee. If set to type(uint64).max, let it be unchanged.
* @param newAdjustmentQuotient New adjustment quotient. If set to type(uint16).max, let it be unchanged.
*/
function setProofParams(uint64 newProofTimeTarget, uint64 newProofTimeIssued)
external
onlyOwner
{
function setProofParams(
uint64 newProofTimeTarget,
uint64 newProofTimeIssued,
uint64 newBlockFee,
uint16 newAdjustmentQuotient
) external onlyOwner {
if (newProofTimeTarget == 0 || newProofTimeIssued == 0) {
revert L1_INVALID_PARAM();
}
Expand All @@ -153,8 +160,20 @@ contract TaikoL1 is EssentialContract, ICrossChainSync, TaikoEvents, TaikoErrors
if (newProofTimeIssued != type(uint64).max) {
state.proofTimeIssued = newProofTimeIssued;
}
// Special case in a way - that we leave the blockFee unchanged
// because the level we are at is fine.
if (newBlockFee != type(uint64).max) {
state.blockFee = newBlockFee;
}
// Special case in a way - that we leave the adjustmentQuotient unchanged
// because we the 'slowlyness' of the curve is fine.
if (newAdjustmentQuotient != type(uint16).max) {
state.adjustmentQuotient = newAdjustmentQuotient;
}

emit ProofTimeTargetChanged(newProofTimeTarget);
emit ProofParamsChanged(
newProofTimeTarget, newProofTimeIssued, newBlockFee, newAdjustmentQuotient
);
}

function depositTaikoToken(uint256 amount) external nonReentrant {
Expand Down
15 changes: 7 additions & 8 deletions packages/protocol/contracts/L1/libs/LibTokenomics.sol
Original file line number Diff line number Diff line change
Expand Up @@ -78,30 +78,29 @@ library LibTokenomics {
* Calculate the newProofTimeIssued and blockFee
*
* @param state The actual state data
* @param config Config data
* @param proofTime The actual proof time
* @return newProofTimeIssued Accumulated proof time
* @return blockFee New block fee
*/
function getNewBlockFeeAndProofTimeIssued(
TaikoData.State storage state,
TaikoData.Config memory config,
uint64 proofTime
) internal view returns (uint64 newProofTimeIssued, uint64 blockFee) {
function getNewBlockFeeAndProofTimeIssued(TaikoData.State storage state, uint64 proofTime)
internal
view
returns (uint64 newProofTimeIssued, uint64 blockFee)
{
newProofTimeIssued = (state.proofTimeIssued > state.proofTimeTarget)
? state.proofTimeIssued - state.proofTimeTarget
: uint64(0);
newProofTimeIssued += proofTime;

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

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

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

blockFee = uint64(result.min(type(uint64).max));
}
Expand Down
11 changes: 5 additions & 6 deletions packages/protocol/contracts/L1/libs/LibVerifying.sol
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ library LibVerifying {
bytes32 genesisBlockHash,
uint64 initBlockFee,
uint64 initProofTimeTarget,
uint64 initProofTimeIssued
uint64 initProofTimeIssued,
uint16 adjustmentQuotient
) internal {
if (
config.chainId <= 1 || config.maxNumProposedBlocks == 1
Expand All @@ -43,8 +44,7 @@ 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.adjustmentQuotient == 0 || initProofTimeTarget == 0
|| initProofTimeIssued == 0
|| adjustmentQuotient == 0 || initProofTimeTarget == 0 || initProofTimeIssued == 0
) revert L1_INVALID_CONFIG();

uint64 timeNow = uint64(block.timestamp);
Expand All @@ -54,6 +54,7 @@ library LibVerifying {
state.blockFee = initBlockFee;
state.proofTimeIssued = initProofTimeIssued;
state.proofTimeTarget = initProofTimeTarget;
state.adjustmentQuotient = adjustmentQuotient;
state.numBlocks = 1;

TaikoData.Block storage blk = state.blocks[0];
Expand Down Expand Up @@ -113,7 +114,6 @@ library LibVerifying {

_markBlockVerified({
state: state,
config: config,
blk: blk,
fcId: uint24(fcId),
fc: fc,
Expand Down Expand Up @@ -143,7 +143,6 @@ library LibVerifying {

function _markBlockVerified(
TaikoData.State storage state,
TaikoData.Config memory config,
TaikoData.Block storage blk,
TaikoData.ForkChoice storage fc,
uint24 fcId,
Expand All @@ -157,7 +156,7 @@ library LibVerifying {
uint64 reward = LibTokenomics.getProofReward(state, proofTime);

(state.proofTimeIssued, state.blockFee) =
LibTokenomics.getNewBlockFeeAndProofTimeIssued(state, config, proofTime);
LibTokenomics.getNewBlockFeeAndProofTimeIssued(state, proofTime);

unchecked {
state.accBlockFees -= reward;
Expand Down
8 changes: 4 additions & 4 deletions packages/protocol/script/DeployOnL1.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ contract DeployOnL1 is Script {
// 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"));
uint16 public ADJUSTMENT_QUOTIENT = uint16(vm.envUint("ADJUSTMENT_QUOTIENT"));

TaikoL1 taikoL1;
address public addressManagerProxy;
Expand Down Expand Up @@ -120,9 +121,7 @@ contract DeployOnL1 is Script {
}

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

address taikoL1Proxy = deployProxy(
Expand All @@ -135,7 +134,8 @@ contract DeployOnL1 is Script {
genesisHash,
feeBase,
INITIAL_PROOF_TIME_TARGET,
initProofTimeIssued
initProofTimeIssued,
uint16(ADJUSTMENT_QUOTIENT)
)
)
);
Expand Down
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 @@ -15,6 +15,7 @@ TAIKO_TOKEN_PREMINT_AMOUNT=0xffff \
TREASURY=0xa0Ee7A142d267C1f36714E4a8F75612F20a79720 \
L2_GENESIS_HASH=0xee1950562d42f0da28bd4550d88886bc90894c77c9c9eaefef775d4c8223f259 \
INITIAL_PROOF_TIME_TARGET=101 \
ADJUSTMENT_QUOTIENT=32000 \
forge script script/DeployOnL1.s.sol:DeployOnL1 \
--fork-url http://localhost:8545 \
--broadcast \
Expand Down
Loading

0 comments on commit eb29686

Please sign in to comment.