Skip to content

Commit

Permalink
feat(protocol): Calculate fee/reward based on proof time w/ 1559 math (
Browse files Browse the repository at this point in the history
…#13489)

Co-authored-by: Daniel Wang <dan@taiko.xyz>
Co-authored-by: Daniel Wang <99078276+dantaik@users.noreply.github.com>
  • Loading branch information
3 people authored Apr 22, 2023
1 parent 4f15527 commit 60eb6d9
Show file tree
Hide file tree
Showing 51 changed files with 1,815 additions and 1,776 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ taiko-mono/
│ ├── <a href="./packages/relayer">relayer</a>: Bridge backend relayer in Go
│ ├── <a href="./packages/starter-dapp">starter-dapp</a>: Template for Taiko dapps
│ ├── <a href="./packages/status-page">status-page</a>: Taiko protocol status page
│ ├── <a href="./packages/tokenomics">tokenomics</a>: Taiko tokenomics simulations
│ ├── <a href="./packages/website">website</a>: Main documentation website at taiko.xyz (https://taiko.xyz/)
│ └── <a href="./packages/whitepaper">whitepaper</a>: Whitepaper source files with automated publishing
...
Expand Down
3 changes: 2 additions & 1 deletion packages/protocol/.solhintignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ lib/
contracts/test/TestLibRLPReader.sol
contracts/test/TestLibRLPWriter.sol
**/contracts/thirdparty/**/*.sol
test2/GasComparison.t.sol
test2/GasComparison.t.sol
test2/TestLn.sol
17 changes: 3 additions & 14 deletions packages/protocol/contracts/L1/TaikoConfig.sol
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,15 @@ library TaikoConfig {
// transactions list calldata, 8K for the remaining tx fields.
maxBytesPerTxList: 120000,
minTxGasLimit: 21000,
slotSmoothingFactor: 946649,
// 100 basis points or 1%
rewardBurnBips: 100,
proposerDepositPctg: 25, // - 25%
// Moving average factors
feeBaseMAF: 1024,
txListCacheExpiry: 0,
proofTimeTarget: 1800, // 85s based on A2 testnet status, or set to 1800 for 30mins (mainnet mock)
adjustmentQuotient: 16,
relaySignalRoot: false,
enableSoloProposer: false,
enableOracleProver: true,
enableTokenomics: true,
skipZKPVerification: false,
proposingConfig: TaikoData.FeeConfig({
avgTimeMAF: 1024,
dampingFactorBips: 2500 // [125% -> 75%]
}),
provingConfig: TaikoData.FeeConfig({
avgTimeMAF: 1024,
dampingFactorBips: 2500 // [75% -> 125%]
})
skipZKPVerification: false
});
}
}
50 changes: 22 additions & 28 deletions packages/protocol/contracts/L1/TaikoData.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@
pragma solidity ^0.8.18;

library TaikoData {
struct FeeConfig {
uint16 avgTimeMAF;
uint16 dampingFactorBips;
}

struct Config {
uint256 chainId;
uint256 maxNumProposedBlocks;
Expand All @@ -24,29 +19,26 @@ library TaikoData {
uint256 maxTransactionsPerBlock;
uint256 maxBytesPerTxList;
uint256 minTxGasLimit;
uint256 slotSmoothingFactor;
uint256 rewardBurnBips;
uint256 proposerDepositPctg;
// Moving average factors
uint256 feeBaseMAF;
uint256 txListCacheExpiry;
uint64 proofTimeTarget;
uint8 adjustmentQuotient;
bool relaySignalRoot;
bool enableSoloProposer;
bool enableOracleProver;
bool enableTokenomics;
bool skipZKPVerification;
FeeConfig proposingConfig;
FeeConfig provingConfig;
}

struct StateVariables {
uint64 feeBase;
uint64 basefee;
uint64 accBlockFees;
uint64 genesisHeight;
uint64 genesisTimestamp;
uint64 numBlocks;
uint64 proofTimeIssued;
uint64 lastVerifiedBlockId;
uint64 avgBlockTime;
uint64 avgProofTime;
uint64 accProposedAt;
uint64 lastProposedAt;
}

Expand Down Expand Up @@ -142,24 +134,26 @@ library TaikoData {
mapping(uint256 blockId => mapping(bytes32 parentHash => mapping(uint32 parentGasUsed => uint256 forkChoiceId))) forkChoiceIds;
mapping(address account => uint256 balance) balances;
mapping(bytes32 txListHash => TxListInfo) txListInfo;
// Never or rarely changed
// Slot 5: never or rarely changed
uint64 genesisHeight;
uint64 genesisTimestamp;
uint64 __reserved1;
uint64 __reserved2;
// Changed when a block is proposed or proven/verified
// Changed when a block is proposed
uint64 __reserved51;
uint64 __reserved52;
// Slot 6: changed by proposeBlock
uint64 lastProposedAt;
uint64 numBlocks;
uint64 lastProposedAt; // Timestamp when the last block is proposed.
uint64 avgBlockTime; // miliseconds
uint64 __reserved3;
// Changed when a block is proven/verified
uint64 accProposedAt; // also by verifyBlocks
uint64 accBlockFees; // also by verifyBlocks
// Slot 7: changed by proveBlock
// uint64 __reserved71;
// uint64 __reserved72;
// uint64 __reserved73;
// uint64 __reserved74;
// Slot 8: changed by verifyBlocks
uint64 basefee;
uint64 proofTimeIssued;
uint64 lastVerifiedBlockId;
uint64 __reserved4;
// the proof time moving average, note that for each block, only the
// first proof's time is considered.
uint64 avgProofTime; // miliseconds
uint64 feeBase;
uint64 __reserved81;
// Reserved
uint256[43] __gap;
}
Expand Down
41 changes: 20 additions & 21 deletions packages/protocol/contracts/L1/TaikoL1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,23 @@ contract TaikoL1 is EssentialContract, IXchainSync, TaikoEvents, TaikoErrors {
* Initialize the rollup.
*
* @param _addressManager The AddressManager address.
* @param _feeBase The initial value of the proposer-fee/prover-reward feeBase.
* @param _genesisBlockHash The block hash of the genesis block.
* @param _initBasefee Initial (reasonable) basefee value.
* @param _initProofTimeIssued Initial proof time which keeps the inflow/outflow in balance
*/
function init(
address _addressManager,
uint64 _feeBase,
bytes32 _genesisBlockHash
bytes32 _genesisBlockHash,
uint64 _initBasefee,
uint64 _initProofTimeIssued
) external initializer {
EssentialContract._init(_addressManager);
LibVerifying.init({
state: state,
config: getConfig(),
feeBase: _feeBase,
genesisBlockHash: _genesisBlockHash
genesisBlockHash: _genesisBlockHash,
initBasefee: _initBasefee,
initProofTimeIssued: _initProofTimeIssued
});
}

Expand Down Expand Up @@ -155,27 +158,19 @@ contract TaikoL1 is EssentialContract, IXchainSync, TaikoEvents, TaikoErrors {
return state.balances[addr];
}

function getBlockFee()
public
view
returns (uint256 feeAmount, uint256 depositAmount)
{
(, feeAmount, depositAmount) = LibTokenomics.getBlockFee(
state,
getConfig()
);
function getBlockFee() public view returns (uint64) {
return state.basefee;
}

function getProofReward(
uint64 provenAt,
uint64 proposedAt
) public view returns (uint256 reward) {
(, reward, ) = LibTokenomics.getProofReward({
state: state,
config: getConfig(),
provenAt: provenAt,
proposedAt: proposedAt
});
) public view returns (uint64) {
return
LibTokenomics.getProofReward({
state: state,
proofTime: provenAt - proposedAt
});
}

function getBlock(
Expand Down Expand Up @@ -256,4 +251,8 @@ contract TaikoL1 is EssentialContract, IXchainSync, TaikoEvents, TaikoErrors {
function getConfig() public pure virtual returns (TaikoData.Config memory) {
return TaikoConfig.getConfig();
}

function getVerifierName(uint16 id) public pure returns (string memory) {
return LibUtils.getVerifierName(id);
}
}
37 changes: 6 additions & 31 deletions packages/protocol/contracts/L1/libs/LibProposing.sol
Original file line number Diff line number Diff line change
Expand Up @@ -90,45 +90,20 @@ library LibProposing {
blk.metaHash = LibUtils.hashMetadata(meta);
blk.proposer = msg.sender;

if (config.enableTokenomics) {
(uint256 newFeeBase, uint256 fee, uint64 deposit) = LibTokenomics
.getBlockFee(state, config);

uint256 burnAmount = fee + deposit;
if (state.balances[msg.sender] < burnAmount)
revert L1_INSUFFICIENT_TOKEN();

unchecked {
state.balances[msg.sender] -= burnAmount;
}

// Update feeBase and avgBlockTime
state.feeBase = LibUtils
.movingAverage({
maValue: state.feeBase,
newValue: newFeeBase,
maf: config.feeBaseMAF
})
.toUint64();

blk.deposit = uint64(deposit);
}
if (state.balances[msg.sender] < state.basefee)
revert L1_INSUFFICIENT_TOKEN();

unchecked {
state.avgBlockTime = LibUtils
.movingAverage({
maValue: state.avgBlockTime,
newValue: (meta.timestamp - state.lastProposedAt) * 1000,
maf: config.proposingConfig.avgTimeMAF
})
.toUint64();
state.lastProposedAt = meta.timestamp;
state.balances[msg.sender] -= state.basefee;
state.accBlockFees += state.basefee;
state.accProposedAt += meta.timestamp;
}

emit BlockProposed(state.numBlocks, meta);
unchecked {
++state.numBlocks;
}
state.lastProposedAt = meta.timestamp;
}

function getBlock(
Expand Down
10 changes: 4 additions & 6 deletions packages/protocol/contracts/L1/libs/LibProving.sol
Original file line number Diff line number Diff line change
Expand Up @@ -201,13 +201,11 @@ library LibProving {
}
}

bytes memory verifierId = abi.encodePacked(
"verifier_",
evidence.zkproof.verifierId
);

(bool verified, bytes memory ret) = resolver
.resolve(string(verifierId), false)
.resolve(
LibUtils.getVerifierName(evidence.zkproof.verifierId),
false
)
.staticcall(bytes.concat(instance, evidence.zkproof.data));

if (
Expand Down
Loading

0 comments on commit 60eb6d9

Please sign in to comment.