Skip to content

Commit

Permalink
feat(protocol): also add rewardPerGas into BlockProposedEvent (#14124)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel Wang <[email protected]>
  • Loading branch information
dantaik and dong77 authored Jul 8, 2023
1 parent fd07f3f commit c4c671e
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 41 deletions.
13 changes: 9 additions & 4 deletions packages/protocol/contracts/L1/TaikoEvents.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ abstract contract TaikoEvents {
// The following events must match the definitions in corresponding L1
// libraries.
event BlockProposed(
uint256 indexed id,
uint256 indexed blockId,
address indexed assignedProver,
uint64 blockFee,
uint32 rewardPerGas,
uint64 feePerGas,
TaikoData.BlockMetadata meta
);

event BlockProven(
uint256 indexed id,
uint256 indexed blockId,
bytes32 parentHash,
bytes32 blockHash,
bytes32 signalRoot,
Expand All @@ -28,7 +29,11 @@ abstract contract TaikoEvents {
);

event BlockVerified(
uint256 indexed id, bytes32 blockHash, address prover, uint64 reward
uint256 indexed blockId,
bytes32 blockHash,
address prover,
uint64 blockFee,
uint64 proofReward
);

event EthDeposited(TaikoData.EthDeposit deposit);
Expand Down
4 changes: 2 additions & 2 deletions packages/protocol/contracts/L1/TaikoL1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,10 @@ contract TaikoL1 is
}

function getBlockFee(uint32 gasLimit) public view returns (uint64) {
return LibProposing.getBlockFee({
return LibUtils.getBlockFee({
state: state,
config: getConfig(),
gasLimit: gasLimit
gasAmount: gasLimit
});
}

Expand Down
34 changes: 11 additions & 23 deletions packages/protocol/contracts/L1/libs/LibProposing.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { IMintableERC20 } from "../../common/IMintableERC20.sol";
import { IProverPool } from "../ProverPool.sol";
import { LibAddress } from "../../libs/LibAddress.sol";
import { LibEthDepositing } from "./LibEthDepositing.sol";
import { LibL2Consts } from "../../L2/LibL2Consts.sol";
import { LibUtils } from "./LibUtils.sol";
import { SafeCastUpgradeable } from
"@openzeppelin/contracts-upgradeable/utils/math/SafeCastUpgradeable.sol";
Expand All @@ -26,9 +25,10 @@ library LibProposing {
using SafeCastUpgradeable for uint256;

event BlockProposed(
uint256 indexed id,
uint256 indexed blockId,
address indexed assignedProver,
uint64 blockFee,
uint32 rewardPerGas,
uint64 feePerGas,
TaikoData.BlockMetadata meta
);

Expand Down Expand Up @@ -133,13 +133,19 @@ library LibProposing {
);
}

uint64 blockFee = getBlockFee(state, config, meta.gasLimit);
uint64 blockFee = LibUtils.getBlockFee(state, config, meta.gasLimit);

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

emit BlockProposed(state.numBlocks, blk.assignedProver, blockFee, meta);
emit BlockProposed({
blockId: state.numBlocks,
assignedProver: blk.assignedProver,
rewardPerGas: blk.rewardPerGas,
feePerGas: state.feePerGas,
meta: meta
});

unchecked {
++state.numBlocks;
Expand All @@ -160,24 +166,6 @@ library LibProposing {
if (blk.blockId != blockId) revert L1_BLOCK_ID();
}

// If auction is tied to gas, we should charge users based on gas as well. At
// this point gasUsed (in proposeBlock()) is always gasLimit, so use it and
// in case of differences refund after verification
function getBlockFee(
TaikoData.State storage state,
TaikoData.Config memory config,
uint32 gasLimit
)
internal
view
returns (uint64)
{
// The diff between gasLimit and gasUsed will be redistributed back to
// the balance of proposer
return state.feePerGas
* (gasLimit + LibL2Consts.ANCHOR_GAS_COST + config.blockFeeBaseGas);
}

function _validateBlock(
TaikoData.State storage state,
TaikoData.Config memory config,
Expand Down
4 changes: 2 additions & 2 deletions packages/protocol/contracts/L1/libs/LibProving.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ library LibProving {
using LibUtils for TaikoData.State;

event BlockProven(
uint256 indexed id,
uint256 indexed blockId,
bytes32 parentHash,
bytes32 blockHash,
bytes32 signalRoot,
Expand Down Expand Up @@ -222,7 +222,7 @@ library LibProving {
}

emit BlockProven({
id: blk.blockId,
blockId: blk.blockId,
parentHash: evidence.parentHash,
blockHash: evidence.blockHash,
signalRoot: evidence.signalRoot,
Expand Down
14 changes: 14 additions & 0 deletions packages/protocol/contracts/L1/libs/LibUtils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

pragma solidity ^0.8.20;

import { LibL2Consts } from "../../L2/LibL2Consts.sol";
import { LibMath } from "../../libs/LibMath.sol";
import { LibEthDepositing } from "./LibEthDepositing.sol";
import { SafeCastUpgradeable } from
Expand Down Expand Up @@ -71,6 +72,19 @@ library LibUtils {
});
}

function getBlockFee(
TaikoData.State storage state,
TaikoData.Config memory config,
uint32 gasAmount
)
internal
view
returns (uint64)
{
return state.feePerGas
* (gasAmount + LibL2Consts.ANCHOR_GAS_COST + config.blockFeeBaseGas);
}

function movingAverage(
uint256 maValue,
uint256 newValue,
Expand Down
30 changes: 23 additions & 7 deletions packages/protocol/contracts/L1/libs/LibVerifying.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ library LibVerifying {
using LibMath for uint256;

event BlockVerified(
uint256 indexed id, bytes32 blockHash, address prover, uint64 reward
uint256 indexed blockId,
bytes32 blockHash,
address prover,
uint64 blockFee,
uint64 proofReward
);

event CrossChainSynced(
uint256 indexed srcHeight, bytes32 blockHash, bytes32 signalRoot
);
Expand Down Expand Up @@ -90,7 +93,13 @@ library LibVerifying {
fc.provenAt = timeNow;
}

emit BlockVerified(0, genesisBlockHash, address(0), 0);
emit BlockVerified({
blockId: 0,
blockHash: genesisBlockHash,
prover: address(0),
blockFee: 0,
proofReward: 0
});
}

function verifyBlocks(
Expand Down Expand Up @@ -236,12 +245,19 @@ library LibVerifying {

blk.verifiedForkChoiceId = fcId;

// Reward the prover
state.taikoTokenBalances[fc.prover] += proofReward;

// refund the proposer
state.taikoTokenBalances[blk.proposer] +=
(_gasLimit - fc.gasUsed) * blk.feePerGas;

emit BlockVerified(blk.blockId, fc.blockHash, fc.prover, proofReward);
// Reward the prover
state.taikoTokenBalances[fc.prover] += proofReward;

emit BlockVerified({
blockId: blk.blockId,
blockHash: fc.blockHash,
prover: fc.prover,
blockFee: LibUtils.getBlockFee(state, config, fc.gasUsed),
proofReward: proofReward
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ title: TaikoEvents
### BlockProposed

```solidity
event BlockProposed(uint256 id, address assignedProver, uint64 blockFee, struct TaikoData.BlockMetadata meta)
event BlockProposed(uint256 blockId, address assignedProver, uint32 rewardPerGas, uint64 feePerGas, struct TaikoData.BlockMetadata meta)
```

### BlockProven

```solidity
event BlockProven(uint256 id, bytes32 parentHash, bytes32 blockHash, bytes32 signalRoot, address prover, uint32 parentGasUsed)
event BlockProven(uint256 blockId, bytes32 parentHash, bytes32 blockHash, bytes32 signalRoot, address prover, uint32 parentGasUsed)
```

### BlockVerified

```solidity
event BlockVerified(uint256 id, bytes32 blockHash, address prover, uint64 reward)
event BlockVerified(uint256 blockId, bytes32 blockHash, address prover, uint64 blockFee, uint64 proofReward)
```

### EthDeposited
Expand Down

0 comments on commit c4c671e

Please sign in to comment.