Skip to content

Commit

Permalink
test(protocol): use custom errors for TaikoL1.sol (#13143)
Browse files Browse the repository at this point in the history
Co-authored-by: David <david@taiko.xyz>
  • Loading branch information
dantaik and davidtaikocha authored Feb 14, 2023
1 parent 21e62fd commit 4f7ab64
Show file tree
Hide file tree
Showing 20 changed files with 425 additions and 304 deletions.
4 changes: 3 additions & 1 deletion packages/protocol/contracts/L1/TaikoL1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ contract TaikoL1 is EssentialContract, IHeaderSync, TaikoEvents {
TaikoData.State public state;
uint256[100] private __gap;

error L1_INVALID_PARAM();

function init(
address _addressManager,
bytes32 _genesisBlockHash,
Expand Down Expand Up @@ -171,7 +173,7 @@ contract TaikoL1 is EssentialContract, IHeaderSync, TaikoEvents {
* @param maxBlocks Max number of blocks to verify.
*/
function verifyBlocks(uint256 maxBlocks) external nonReentrant {
require(maxBlocks > 0, "L1:maxBlocks");
if (maxBlocks == 0) revert L1_INVALID_PARAM();
LibVerifying.verifyBlocks({
state: state,
config: getConfig(),
Expand Down
12 changes: 7 additions & 5 deletions packages/protocol/contracts/L1/TkoToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ contract TkoToken is EssentialContract, ERC20Upgradeable, IMintableERC20 {
uint256[50] private __gap;

/*********************
* Events *
* Events and Errors *
*********************/
event Mint(address account, uint256 amount);
event Burn(address account, uint256 amount);

error TKO_INVALID_ADDR();

/*********************
* External Functions*
*********************/
Expand All @@ -55,7 +57,7 @@ contract TkoToken is EssentialContract, ERC20Upgradeable, IMintableERC20 {
address to,
uint256 amount
) public override(ERC20Upgradeable, IERC20Upgradeable) returns (bool) {
require(to != address(this), "TKO: invalid to");
if (to == address(this)) revert TKO_INVALID_ADDR();
return ERC20Upgradeable.transfer(to, amount);
}

Expand All @@ -64,7 +66,7 @@ contract TkoToken is EssentialContract, ERC20Upgradeable, IMintableERC20 {
address to,
uint256 amount
) public override(ERC20Upgradeable, IERC20Upgradeable) returns (bool) {
require(to != address(this), "TKO: invalid to");
if (to == address(this)) revert TKO_INVALID_ADDR();
return ERC20Upgradeable.transferFrom(from, to, amount);
}

Expand All @@ -78,7 +80,7 @@ contract TkoToken is EssentialContract, ERC20Upgradeable, IMintableERC20 {
address account,
uint256 amount
) public onlyFromNamed("proto_broker") {
require(account != address(0), "TKO: invalid address");
if (account == address(0)) revert TKO_INVALID_ADDR();
_mint(account, amount);
emit Mint(account, amount);
}
Expand All @@ -93,7 +95,7 @@ contract TkoToken is EssentialContract, ERC20Upgradeable, IMintableERC20 {
address account,
uint256 amount
) public onlyFromNamed("proto_broker") {
require(account != address(0), "TKO: invalid address");
if (account == address(0)) revert TKO_INVALID_ADDR();
_burn(account, amount);
emit Burn(account, amount);
}
Expand Down
84 changes: 47 additions & 37 deletions packages/protocol/contracts/L1/libs/LibProposing.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ library LibProposing {
);
event BlockProposed(uint256 indexed id, TaikoData.BlockMetadata meta);

error L1_METADATA_FIELD();
error L1_EXTRA_DATA();
error L1_ID();
error L1_TOO_MANY();
error L1_GAS_LIMIT();
error L1_COMMITTED();
error L1_NOT_COMMITTED();
error L1_SOLO_PROPOSER();
error L1_INPUT_SIZE();
error L1_TX_LIST();

function commitBlock(
TaikoData.State storage state,
TaikoData.Config memory config,
Expand All @@ -38,7 +49,8 @@ library LibProposing {

bytes32 hash = _aggregateCommitHash(block.number, commitHash);

require(state.commits[msg.sender][commitSlot] != hash, "L1:committed");
if (state.commits[msg.sender][commitSlot] == hash)
revert L1_COMMITTED();
state.commits[msg.sender][commitSlot] = hash;

emit BlockCommitted({
Expand All @@ -60,14 +72,12 @@ library LibProposing {

// TODO(daniel): remove this special address.
address soloProposer = resolver.resolve("solo_proposer", true);
require(
soloProposer == address(0) || soloProposer == msg.sender,
"L1:soloProposer"
);
if (soloProposer != address(0) && soloProposer != msg.sender)
revert L1_SOLO_PROPOSER();

assert(!LibUtils.isHalted(state));

require(inputs.length == 2, "L1:inputs:size");
if (inputs.length != 2) revert L1_INPUT_SIZE();
TaikoData.BlockMetadata memory meta = abi.decode(
inputs[0],
(TaikoData.BlockMetadata)
Expand All @@ -82,18 +92,16 @@ library LibProposing {
{
bytes calldata txList = inputs[1];
// perform validation and populate some fields
require(
txList.length >= 0 &&
txList.length <= config.maxBytesPerTxList &&
meta.txListHash == txList.hashTxList(),
"L1:txList"
);

require(
state.nextBlockId <
state.latestVerifiedId + config.maxNumBlocks,
"L1:tooMany"
);
if (
txList.length < 0 ||
txList.length > config.maxBytesPerTxList ||
meta.txListHash != txList.hashTxList()
) revert L1_TX_LIST();

if (
state.nextBlockId >=
state.latestVerifiedId + config.maxNumBlocks
) revert L1_TOO_MANY();

meta.id = state.nextBlockId;
meta.l1Height = block.number - 1;
Expand Down Expand Up @@ -190,7 +198,9 @@ library LibProposing {
uint256 maxNumBlocks,
uint256 id
) internal view returns (TaikoData.ProposedBlock storage) {
require(id > state.latestVerifiedId && id < state.nextBlockId, "L1:id");
if (id <= state.latestVerifiedId || id >= state.nextBlockId) {
revert L1_ID();
}
return state.getProposedBlock(maxNumBlocks, id);
}

Expand All @@ -216,16 +226,15 @@ library LibProposing {
meta.txListHash
);

require(
isCommitValid({
if (
!isCommitValid({
state: state,
commitConfirmations: commitConfirmations,
commitSlot: meta.commitSlot,
commitHeight: meta.commitHeight,
commitHash: commitHash
}),
"L1:notCommitted"
);
})
) revert L1_NOT_COMMITTED();

if (meta.commitSlot == 0) {
// Special handling of slot 0 for refund; non-zero slots
Expand All @@ -238,19 +247,20 @@ library LibProposing {
TaikoData.Config memory config,
TaikoData.BlockMetadata memory meta
) private pure {
require(
meta.id == 0 &&
meta.l1Height == 0 &&
meta.l1Hash == 0 &&
meta.mixHash == 0 &&
meta.timestamp == 0 &&
meta.beneficiary != address(0) &&
meta.txListHash != 0,
"L1:placeholder"
);

require(meta.gasLimit <= config.blockMaxGasLimit, "L1:gasLimit");
require(meta.extraData.length <= 32, "L1:extraData");
if (
meta.id != 0 ||
meta.l1Height != 0 ||
meta.l1Hash != 0 ||
meta.mixHash != 0 ||
meta.timestamp != 0 ||
meta.beneficiary == address(0) ||
meta.txListHash == 0
) revert L1_METADATA_FIELD();

if (meta.gasLimit > config.blockMaxGasLimit) revert L1_GAS_LIMIT();
if (meta.extraData.length > 32) {
revert L1_EXTRA_DATA();
}
}

function _calculateCommitHash(
Expand Down
Loading

0 comments on commit 4f7ab64

Please sign in to comment.