Skip to content

Commit

Permalink
refactor(protocol): additional cleaning up (#13646)
Browse files Browse the repository at this point in the history
  • Loading branch information
dantaik authored Apr 25, 2023
1 parent 8ebd2ce commit a1fc8b1
Show file tree
Hide file tree
Showing 11 changed files with 28 additions and 56 deletions.
4 changes: 2 additions & 2 deletions packages/protocol/contracts/L1/TaikoData.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ library TaikoData {
}

struct StateVariables {
uint64 basefee;
uint64 blockFee;
uint64 accBlockFees;
uint64 genesisHeight;
uint64 genesisTimestamp;
Expand Down Expand Up @@ -142,7 +142,7 @@ library TaikoData {
uint64 numBlocks;
uint64 nextEthDepositToProcess;
// Slot 9
uint64 basefee;
uint64 blockFee;
uint64 proofTimeIssued;
uint64 lastVerifiedBlockId;
uint64 __reserved91;
Expand Down
8 changes: 4 additions & 4 deletions packages/protocol/contracts/L1/TaikoL1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,21 @@ contract TaikoL1 is EssentialContract, IXchainSync, TaikoEvents, TaikoErrors {
*
* @param _addressManager The AddressManager address.
* @param _genesisBlockHash The block hash of the genesis block.
* @param _initBasefee Initial (reasonable) basefee value.
* @param _initBlockFee Initial (reasonable) block fee value.
* @param _initProofTimeIssued Initial proof time which keeps the inflow/outflow in balance
*/
function init(
address _addressManager,
bytes32 _genesisBlockHash,
uint64 _initBasefee,
uint64 _initBlockFee,
uint64 _initProofTimeIssued
) external initializer {
EssentialContract._init(_addressManager);
LibVerifying.init({
state: state,
config: getConfig(),
genesisBlockHash: _genesisBlockHash,
initBasefee: _initBasefee,
initBlockFee: _initBlockFee,
initProofTimeIssued: _initProofTimeIssued
});
}
Expand Down Expand Up @@ -152,7 +152,7 @@ contract TaikoL1 is EssentialContract, IXchainSync, TaikoEvents, TaikoErrors {
}

function getBlockFee() public view returns (uint64) {
return state.basefee;
return state.blockFee;
}

function getProofReward(
Expand Down
6 changes: 3 additions & 3 deletions packages/protocol/contracts/L1/libs/LibProposing.sol
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,12 @@ library LibProposing {
blk.metaHash = LibUtils.hashMetadata(meta);
blk.proposer = msg.sender;

if (state.taikoTokenBalances[msg.sender] < state.basefee)
if (state.taikoTokenBalances[msg.sender] < state.blockFee)
revert L1_INSUFFICIENT_TOKEN();

unchecked {
state.taikoTokenBalances[msg.sender] -= state.basefee;
state.accBlockFees += state.basefee;
state.taikoTokenBalances[msg.sender] -= state.blockFee;
state.accBlockFees += state.blockFee;
state.accProposedAt += meta.timestamp;
}

Expand Down
39 changes: 6 additions & 33 deletions packages/protocol/contracts/L1/libs/LibTokenomics.sol
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ library LibTokenomics {
}

/**
* Update the baseFee for proofs
* Update the block reward for proofs
*
* @param state The actual state data
* @param proofTime The actual proof time
Expand Down Expand Up @@ -92,19 +92,19 @@ library LibTokenomics {
}

/**
* Calculate the newProofTimeIssued and newBasefee
* 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 newBasefee New basefee
* @return BlockFee New block fee
*/
function getNewBaseFeeandProofTimeIssued(
function getNewBlockFeeAndProofTimeIssued(
TaikoData.State storage state,
TaikoData.Config memory config,
uint64 proofTime
) internal view returns (uint64 newProofTimeIssued, uint64 newBasefee) {
) internal view returns (uint64 newProofTimeIssued, uint64 BlockFee) {
newProofTimeIssued = (state.proofTimeIssued > config.proofTimeTarget)
? state.proofTimeIssued - config.proofTimeTarget
: uint64(0);
Expand All @@ -121,33 +121,6 @@ library LibTokenomics {
Math.SCALING_FACTOR_1E18) /
(config.proofTimeTarget * config.adjustmentQuotient);

newBasefee = uint64(result.min(type(uint64).max));
}

/**
* Calculating the exponential smoothened with (target/quotient)
*
* @param value Result of cumulativeProofTime
* @param target Target proof time
* @param quotient Quotient
* @return uint64 Calculated new basefee
*/
function _calcBasefee(
uint256 value,
uint256 target,
uint256 quotient
) private pure returns (uint64) {
uint256 x = (value * Math.SCALING_FACTOR_1E18) / (target * quotient);

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

uint256 result = (uint256(Math.exp(int256(x))) /
Math.SCALING_FACTOR_1E18) / (target * quotient);

if (result > type(uint64).max) return type(uint64).max;

return uint64(result);
BlockFee = uint64(result.min(type(uint64).max));
}
}
2 changes: 1 addition & 1 deletion packages/protocol/contracts/L1/libs/LibUtils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ library LibUtils {
) internal view returns (TaikoData.StateVariables memory) {
return
TaikoData.StateVariables({
basefee: state.basefee,
blockFee: state.blockFee,
accBlockFees: state.accBlockFees,
genesisHeight: state.genesisHeight,
genesisTimestamp: state.genesisTimestamp,
Expand Down
8 changes: 4 additions & 4 deletions packages/protocol/contracts/L1/libs/LibVerifying.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ library LibVerifying {
TaikoData.State storage state,
TaikoData.Config memory config,
bytes32 genesisBlockHash,
uint64 initBasefee,
uint64 initBlockFee,
uint64 initProofTimeIssued
) internal {
if (
Expand Down Expand Up @@ -61,7 +61,7 @@ library LibVerifying {
state.genesisHeight = uint64(block.number);
state.genesisTimestamp = timeNow;

state.basefee = initBasefee;
state.blockFee = initBlockFee;
state.proofTimeIssued = initProofTimeIssued;
state.numBlocks = 1;

Expand Down Expand Up @@ -160,8 +160,8 @@ library LibVerifying {

uint64 reward = LibTokenomics.getProofReward(state, proofTime);

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

unchecked {
state.accBlockFees -= reward;
Expand Down
3 changes: 2 additions & 1 deletion packages/protocol/contracts/L2/TaikoL2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,8 @@ contract TaikoL2 is EssentialContract, TaikoL2Signer, IXchainSync {
}

// On L2, basefee is not burnt, but sent to a treasure instead.
// TODO(daniel): how to verify the tresasure address in protocol?
// The circuits will need to verify the basefee recipient is the deginated
// address.
if (block.basefee != basefee)
revert L2_BASEFEE_MISMATCH(uint64(basefee), uint64(block.basefee));

Expand Down
4 changes: 2 additions & 2 deletions packages/protocol/test/TaikoL1.sim.sol
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ contract TaikoL1Simulation is TaikoL1TestBase, FoundryRandom {
"time,",
"lastVerifiedBlockId,",
"numBlocks,",
"baseFee,",
"blockFee,",
"accProposedAt"
);
console2.log(str);
Expand All @@ -149,7 +149,7 @@ contract TaikoL1Simulation is TaikoL1TestBase, FoundryRandom {
",",
Strings.toString(vars.numBlocks),
",",
Strings.toString(vars.basefee),
Strings.toString(vars.blockFee),
",",
Strings.toString(vars.accProposedAt)
);
Expand Down
1 change: 0 additions & 1 deletion packages/protocol/test/TaikoL1.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,6 @@ contract TaikoL1Test is TaikoL1TestBase {
uint256 iterationCnt = 10;
// Declare here so that block prop/prove/verif. can be used in 1 place
TaikoData.BlockMetadata memory meta;
TaikoData.ForkChoice memory fk;
bytes32 blockHash;
bytes32 signalRoot;
bytes32[] memory parentHashes = new bytes32[](iterationCnt);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ struct Config {
uint256 chainId;
uint256 maxNumProposedBlocks;
uint256 ringBufferSize;
uint256 maxNumVerifiedBlocks;
uint256 maxVerificationsPerTx;
uint256 blockMaxGasLimit;
uint256 maxTransactionsPerBlock;
Expand All @@ -35,7 +34,7 @@ struct Config {

```solidity
struct StateVariables {
uint64 basefee;
uint64 blockFee;
uint64 accBlockFees;
uint64 genesisHeight;
uint64 genesisTimestamp;
Expand Down Expand Up @@ -163,7 +162,7 @@ struct State {
uint64 accBlockFees;
uint64 numBlocks;
uint64 nextEthDepositToProcess;
uint64 basefee;
uint64 blockFee;
uint64 proofTimeIssued;
uint64 lastVerifiedBlockId;
uint64 __reserved91;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ receive() external payable
### init

```solidity
function init(address _addressManager, bytes32 _genesisBlockHash, uint64 _initBasefee, uint64 _initProofTimeIssued) external
function init(address _addressManager, bytes32 _genesisBlockHash, uint64 _initBlockFee, uint64 _initProofTimeIssued) external
```

Initialize the rollup.
Expand All @@ -30,7 +30,7 @@ Initialize the rollup.
| --------------------- | ------- | ------------------------------------------------------------ |
| \_addressManager | address | The AddressManager address. |
| \_genesisBlockHash | bytes32 | The block hash of the genesis block. |
| \_initBasefee | uint64 | Initial (reasonable) basefee value. |
| \_initBlockFee | uint64 | Initial (reasonable) block fee value. |
| \_initProofTimeIssued | uint64 | Initial proof time which keeps the inflow/outflow in balance |

### proposeBlock
Expand Down

0 comments on commit a1fc8b1

Please sign in to comment.