Skip to content

Commit

Permalink
feat(protocol): implemented partial and more fair block proposal rewa…
Browse files Browse the repository at this point in the history
…rds (#14888)

Co-authored-by: Daniel Wang <[email protected]>
Co-authored-by: Daniel Wang <[email protected]>
  • Loading branch information
3 people authored Oct 6, 2023
1 parent a74f44f commit 4c78ddb
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 19 deletions.
13 changes: 10 additions & 3 deletions packages/protocol/contracts/L1/TaikoData.sol
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ library TaikoData {
uint24 blockMaxTxListBytes;
// Amount of token to reward to the first block propsoed in each L1
// block.
uint256 proposerRewardPerSecond;
uint256 proposerRewardMax;
uint128 proposerRewardPerL1Block;
uint128 proposerRewardMax;
uint8 proposerRewardPoolPctg;
// ---------------------------------------------------------------------
// Group 3: Proof related configs
// ---------------------------------------------------------------------
Expand Down Expand Up @@ -172,6 +173,11 @@ library TaikoData {
uint64 lastVerifiedBlockId;
}

struct SlotC {
uint128 accumulatedReward;
uint64 lastProposedHeight;
}

/// @dev Struct holding the state variables for the {TaikoL1} contract.
struct State {
// Ring buffer for proposed blocks and a some recent verified blocks.
Expand All @@ -192,6 +198,7 @@ library TaikoData {
mapping(address account => uint256 balance) tokenBalances;
SlotA slotA; // slot 6
SlotB slotB; // slot 7
uint256[143] __gap;
SlotC slotC; // slot 8
uint256[142] __gap;
}
}
4 changes: 3 additions & 1 deletion packages/protocol/contracts/L1/TaikoL1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -317,8 +317,10 @@ contract TaikoL1 is
blockMaxGasLimit: 8_000_000,
blockFeeBaseGas: 20_000,
blockMaxTxListBytes: 120_000,
proposerRewardPerSecond: 25e16, // 0.25 Taiko token
proposerRewardPerL1Block: 3e18, // 0.25 Taiko token * 12s = 3 TKO
proposerRewardMax: 32e18, // 32 Taiko token
proposerRewardPoolPctg: 30, // means that 30% of the accumulated
// reward is given to the next proposer
livenessBond: 10_240e18,
ethDepositRingBufferSize: 1024,
ethDepositMinCountPerBlock: 8,
Expand Down
43 changes: 31 additions & 12 deletions packages/protocol/contracts/L1/libs/LibProposing.sol
Original file line number Diff line number Diff line change
Expand Up @@ -99,27 +99,46 @@ library LibProposing {

// The block reward doesn't undergo automatic halving; instead, we
// depend on Taiko DAO to make necessary adjustments to the rewards.
uint256 reward;
uint128 reward;

if (config.proposerRewardPerSecond > 0 && config.proposerRewardMax > 0)
{
// Unchecked is safe as block.timestamp is always greater than
// block.proposedAt (proposed in the past)
// Reward block proposers with Taiko tokens to encourage chain adoption
// and ensure liveness.
// Rewards are issued only if `proposerRewardPerL1Block` and
// `proposerRewardMax` are set to nonzero values in the configuration.
if (config.proposerRewardPerL1Block != 0) {
unchecked {
uint256 blockTime = block.timestamp
- state.blocks[(b.numBlocks - 1) % config.blockRingBufferSize]
.proposedAt;

if (blockTime > 0) {
reward = (config.proposerRewardPerSecond * blockTime).min(
config.proposerRewardMax
// Mint additional tokens into the reward pool as L1 block
// numbers increase, to incentivize future proposers.
if (
state.slotC.lastProposedHeight != 0
&& state.slotC.lastProposedHeight < block.number
) {
uint256 extraRewardMinted = (
block.number - state.slotC.lastProposedHeight
) * config.proposerRewardPerL1Block;

// Reward pool is capped to `proposerRewardMax`
state.slotC.accumulatedReward = uint128(
(extraRewardMinted + state.slotC.accumulatedReward).min(
config.proposerRewardMax
)
);
}

if (state.slotC.accumulatedReward != 0) {
// The current proposer receives a fixed percentage of the
// reward pool.
reward = state.slotC.accumulatedReward / 100
* config.proposerRewardPoolPctg;

state.slotC.accumulatedReward -= reward;

// Reward must be minted
LibTaikoToken.creditToken(
state, resolver, msg.sender, reward, true
);
}
state.slotC.lastProposedHeight = uint64(block.number);
}
}

Expand Down
9 changes: 8 additions & 1 deletion packages/protocol/contracts/L1/libs/LibVerifying.sol
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ library LibVerifying {
|| config.blockMaxGasLimit == 0 || config.blockMaxTxListBytes == 0
|| config.blockMaxTxListBytes > 128 * 1024 //blob up to 128K
|| config.livenessBond == 0
|| config.livenessBond < 10 * config.proposerRewardPerSecond
|| config.livenessBond < 10 * config.proposerRewardPerL1Block
|| config.ethDepositRingBufferSize <= 1
|| config.ethDepositMinCountPerBlock == 0
|| config.ethDepositMaxCountPerBlock
Expand All @@ -64,6 +64,13 @@ library LibVerifying {
>= type(uint96).max / config.ethDepositMaxCountPerBlock
) revert L1_INVALID_CONFIG();

if (config.proposerRewardPerL1Block != 0) {
if (
config.proposerRewardMax == 0
|| config.proposerRewardPoolPctg == 0
) revert L1_INVALID_CONFIG();
}

// Init state
state.slotA.genesisHeight = uint64(block.number);
state.slotA.genesisTimestamp = uint64(block.timestamp);
Expand Down
2 changes: 1 addition & 1 deletion packages/protocol/test/L1/TaikoL1.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ contract TaikoL1_NoCooldown is TaikoL1 {
config.blockMaxProposals = 10;
config.blockRingBufferSize = 12;
config.livenessBond = 1e18; // 1 Taiko token
config.proposerRewardPerSecond = 1e15; // 0.001 Taiko token
config.proposerRewardPerL1Block = 1e15; // 0.001 Taiko token
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/protocol/test/L1/TaikoL1LibProvingWithTiers.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ contract TaikoL1Tiers is TaikoL1 {
config.blockMaxProposals = 10;
config.blockRingBufferSize = 12;
config.livenessBond = 1e18; // 1 Taiko token
config.proposerRewardPerSecond = 1e15; // 0.001 Taiko token
config.proposerRewardPerL1Block = 1e15; // 0.001 Taiko token
}
}

Expand Down

0 comments on commit 4c78ddb

Please sign in to comment.