Skip to content

Commit

Permalink
refactor(protocol): improve verifyTxListInvalid (#13096)
Browse files Browse the repository at this point in the history
Co-authored-by: David <[email protected]>
  • Loading branch information
dantaik and davidtaikocha authored Feb 6, 2023
1 parent 3a7523f commit 91a8fe5
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 30 deletions.
5 changes: 2 additions & 3 deletions packages/protocol/contracts/L2/TaikoL2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ contract TaikoL2 is AddressResolver, ReentrancyGuard, IHeaderSync {
*/
function invalidateBlock(
bytes calldata txList,
LibInvalidTxList.Reason hint,
LibInvalidTxList.Hint hint,
uint256 txIdx
) external {
require(
Expand All @@ -104,13 +104,12 @@ contract TaikoL2 is AddressResolver, ReentrancyGuard, IHeaderSync {
require(tx.gasprice == 0, "L2:gasPrice");

TaikoData.Config memory config = getConfig();
LibInvalidTxList.Reason reason = LibInvalidTxList.isTxListInvalid({
LibInvalidTxList.verifyTxListInvalid({
config: config,
encoded: txList,
hint: hint,
txIdx: txIdx
});
require(reason != LibInvalidTxList.Reason.OK, "L2:reason");

if (config.enablePublicInputsCheck) {
_checkPublicInputs();
Expand Down
63 changes: 39 additions & 24 deletions packages/protocol/contracts/libs/LibInvalidTxList.sol
Original file line number Diff line number Diff line change
Expand Up @@ -37,56 +37,71 @@ library LibInvalidTxList {
// NOTE: If the order of this enum changes, then some test cases that using
// this enum in generate_genesis.test.ts may also needs to be
// modified accordingly.
enum Reason {
OK,
BINARY_TOO_LARGE,
BINARY_NOT_DECODABLE,
BLOCK_TOO_MANY_TXS,
BLOCK_GAS_LIMIT_TOO_LARGE,
error ERR_PARAMS_NOT_DEFAULTS();
error ERR_INVALID_TX_IDX();
error ERR_INVALID_HINT();
error ERR_VERIFICAITON_FAILURE();

enum Hint {
NONE,
TX_INVALID_SIG,
TX_GAS_LIMIT_TOO_SMALL
}

function isTxListInvalid(
function verifyTxListInvalid(
TaikoData.Config memory config,
bytes calldata encoded,
Reason hint,
Hint hint,
uint256 txIdx
) internal pure returns (Reason) {
) internal pure {
if (encoded.length > config.maxBytesPerTxList) {
return Reason.BINARY_TOO_LARGE;
_checkParams(hint, txIdx);
return;
}

try LibTxDecoder.decodeTxList(config.chainId, encoded) returns (
LibTxDecoder.TxList memory txList
) {
if (txList.items.length > config.maxTransactionsPerBlock) {
return Reason.BLOCK_TOO_MANY_TXS;
_checkParams(hint, txIdx);
return;
}

if (LibTxDecoder.sumGasLimit(txList) > config.blockMaxGasLimit) {
return Reason.BLOCK_GAS_LIMIT_TOO_LARGE;
_checkParams(hint, txIdx);
return;
}

if (txIdx >= txList.items.length) {
revert ERR_INVALID_TX_IDX();
}

require(txIdx < txList.items.length, "invalid txIdx");
LibTxDecoder.Tx memory _tx = txList.items[txIdx];

if (hint == Reason.TX_INVALID_SIG) {
require(
LibTxUtils.recoverSender(config.chainId, _tx) == address(0),
"bad hint TX_INVALID_SIG"
);
return Reason.TX_INVALID_SIG;
if (hint == Hint.TX_INVALID_SIG) {
if (
LibTxUtils.recoverSender(config.chainId, _tx) != address(0)
) {
revert ERR_INVALID_HINT();
}
return;
}

if (hint == Reason.TX_GAS_LIMIT_TOO_SMALL) {
require(_tx.gasLimit >= config.minTxGasLimit, "bad hint");
return Reason.TX_GAS_LIMIT_TOO_SMALL;
if (hint == Hint.TX_GAS_LIMIT_TOO_SMALL) {
if (_tx.gasLimit >= config.minTxGasLimit) {
revert ERR_INVALID_HINT();
}
return;
}

revert("failed to prove txlist invalid");
revert ERR_VERIFICAITON_FAILURE();
} catch (bytes memory) {
return Reason.BINARY_NOT_DECODABLE;
_checkParams(hint, txIdx);
}
}

// Checks hint and txIdx both have 0 values.
function _checkParams(Hint hint, uint256 txIdx) private pure {
if (hint != Hint.NONE || txIdx != 0) revert ERR_PARAMS_NOT_DEFAULTS();
}
}
6 changes: 3 additions & 3 deletions packages/protocol/test/genesis/generate_genesis.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ action("Generate Genesis", function () {
await expect(
TaikoL2.invalidateBlock(
bytes,
5, // hint: TX_INVALID_SIG
1, // hint: TX_INVALID_SIG
0
)
).to.be.revertedWith("L2:sender");
Expand All @@ -215,7 +215,7 @@ action("Generate Genesis", function () {

const tx = await taikoL2WithGoldenTouchSigner.invalidateBlock(
bytes,
5, // hint: TX_INVALID_SIG
1, // hint: TX_INVALID_SIG
0,
{ gasPrice: 0 }
);
Expand All @@ -228,7 +228,7 @@ action("Generate Genesis", function () {
message: "TaikoL2.invalidateBlock gas cost after 256 L2 blocks",
TxListBytes: ethers.utils.arrayify(bytes).length,
txNums,
reason: "TX_INVALID_SIG",
hint: "TX_INVALID_SIG",
gasUsed: receipt.gasUsed,
});
});
Expand Down

0 comments on commit 91a8fe5

Please sign in to comment.