Skip to content

Commit

Permalink
chore(protocol): change lib functions to internal to lower gas cost (t…
Browse files Browse the repository at this point in the history
  • Loading branch information
dantaik authored Feb 29, 2024
1 parent 33395c3 commit 1d15f2b
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 91 deletions.
54 changes: 10 additions & 44 deletions packages/protocol/contracts/L1/TaikoL1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import "./TaikoEvents.sol";
/// by the Bridge contract.
/// @dev Labeled in AddressResolver as "taiko"
/// @custom:security-contact [email protected]
contract TaikoL1 is EssentialContract, ITaikoL1, ITierProvider, TaikoEvents, TaikoErrors {
contract TaikoL1 is EssentialContract, ITaikoL1, TaikoEvents, TaikoErrors {
TaikoData.State public state;
uint256[50] private __gap;

Expand Down Expand Up @@ -68,7 +68,7 @@ contract TaikoL1 is EssentialContract, ITaikoL1, ITierProvider, TaikoEvents, Tai
(meta, depositsProcessed) = LibProposing.proposeBlock(state, config, this, params, txList);

if (!state.slotB.provingPaused) {
_verifyBlocks(config, config.maxBlocksToVerifyPerProposal);
LibVerifying.verifyBlocks(state, config, this, config.maxBlocksToVerifyPerProposal);
}
}

Expand All @@ -94,12 +94,17 @@ contract TaikoL1 is EssentialContract, ITaikoL1, ITierProvider, TaikoEvents, Tai

uint8 maxBlocksToVerify = LibProving.proveBlock(state, config, this, meta, tran, proof);

_verifyBlocks(config, maxBlocksToVerify);
LibVerifying.verifyBlocks(state, config, this, maxBlocksToVerify);
}

/// @inheritdoc ITaikoL1
function verifyBlocks(uint64 maxBlocksToVerify) external nonReentrant whenNotPaused {
_verifyBlocks(getConfig(), maxBlocksToVerify);
function verifyBlocks(uint64 maxBlocksToVerify)
external
nonReentrant
whenNotPaused
whenProvingNotPaused
{
LibVerifying.verifyBlocks(state, getConfig(), this, maxBlocksToVerify);
}

/// @notice Pause block proving.
Expand Down Expand Up @@ -176,31 +181,6 @@ contract TaikoL1 is EssentialContract, ITaikoL1, ITierProvider, TaikoEvents, Tai
b = state.slotB;
}

/// @notice Retrieves the configuration for a specified tier.
/// @param tierId ID of the tier.
/// @return Tier struct containing the tier's parameters. This
/// function will revert if the tier is not supported.
function getTier(uint16 tierId)
public
view
virtual
override
returns (ITierProvider.Tier memory)
{
return ITierProvider(resolve("tier_provider", false)).getTier(tierId);
}

/// @inheritdoc ITierProvider
function getTierIds() public view override returns (uint16[] memory ids) {
ids = ITierProvider(resolve("tier_provider", false)).getTierIds();
if (ids.length >= type(uint8).max) revert L1_TOO_MANY_TIERS();
}

/// @inheritdoc ITierProvider
function getMinTier(uint256 rand) public view override returns (uint16) {
return ITierProvider(resolve("tier_provider", false)).getMinTier(rand);
}

/// @inheritdoc ITaikoL1
function getConfig() public view virtual override returns (TaikoData.Config memory) {
// All hard-coded configurations:
Expand Down Expand Up @@ -236,20 +216,6 @@ contract TaikoL1 is EssentialContract, ITaikoL1, ITierProvider, TaikoEvents, Tai
});
}

function isConfigValid() public view returns (bool) {
return LibVerifying.isConfigValid(getConfig());
}

function _verifyBlocks(
TaikoData.Config memory config,
uint64 maxBlocksToVerify
)
internal
whenProvingNotPaused
{
LibVerifying.verifyBlocks(state, config, this, maxBlocksToVerify);
}

function _authorizePause(address)
internal
view
Expand Down
2 changes: 1 addition & 1 deletion packages/protocol/contracts/L1/libs/LibDepositing.sol
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ library LibDepositing {
IAddressResolver resolver,
address recipient
)
external
internal
{
if (!canDepositEthToL2(state, config, msg.value)) {
revert L1_INVALID_ETH_DEPOSIT();
Expand Down
2 changes: 1 addition & 1 deletion packages/protocol/contracts/L1/libs/LibProposing.sol
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ library LibProposing {
bytes calldata data,
bytes calldata txList
)
external
internal
returns (
TaikoData.BlockMetadata memory meta,
TaikoData.EthDeposit[] memory depositsProcessed
Expand Down
2 changes: 1 addition & 1 deletion packages/protocol/contracts/L1/libs/LibProving.sol
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ library LibProving {
TaikoData.Transition memory tran,
TaikoData.TierProof memory proof
)
external
internal
returns (uint8 maxBlocksToVerify)
{
// Make sure parentHash is not zero
Expand Down
48 changes: 24 additions & 24 deletions packages/protocol/contracts/L1/libs/LibVerifying.sol
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ library LibVerifying {
)
external
{
if (!isConfigValid(config)) revert L1_INVALID_CONFIG();
if (!_isConfigValid(config)) revert L1_INVALID_CONFIG();

// Init state
state.slotA.genesisHeight = uint64(block.number);
Expand Down Expand Up @@ -69,29 +69,6 @@ library LibVerifying {
});
}

function isConfigValid(TaikoData.Config memory config) public view returns (bool isValid) {
if (
config.chainId <= 1 || config.chainId == block.chainid //
|| config.blockMaxProposals == 1
|| config.blockRingBufferSize <= config.blockMaxProposals + 1
|| config.blockMaxGasLimit == 0 || config.blockMaxTxListBytes == 0
|| config.blockMaxTxListBytes > 128 * 1024 // calldata up to 128K
|| config.livenessBond == 0 || config.ethDepositRingBufferSize <= 1
|| config.ethDepositMinCountPerBlock == 0
// Audit recommendation, and gas tested. Processing 32 deposits (as initially set in
// TaikoL1.sol) costs 72_502 gas.
|| config.ethDepositMaxCountPerBlock > 32
|| config.ethDepositMaxCountPerBlock < config.ethDepositMinCountPerBlock
|| config.ethDepositMinAmount == 0
|| config.ethDepositMaxAmount <= config.ethDepositMinAmount
|| config.ethDepositMaxAmount > type(uint96).max || config.ethDepositGas == 0
|| config.ethDepositMaxFee == 0
|| config.ethDepositMaxFee > type(uint96).max / config.ethDepositMaxCountPerBlock
) return false;

return true;
}

/// @dev Verifies up to N blocks.
function verifyBlocks(
TaikoData.State storage state,
Expand Down Expand Up @@ -252,4 +229,27 @@ library LibVerifying {
);
}
}

function _isConfigValid(TaikoData.Config memory config) private view returns (bool isValid) {
if (
config.chainId <= 1 || config.chainId == block.chainid //
|| config.blockMaxProposals == 1
|| config.blockRingBufferSize <= config.blockMaxProposals + 1
|| config.blockMaxGasLimit == 0 || config.blockMaxTxListBytes == 0
|| config.blockMaxTxListBytes > 128 * 1024 // calldata up to 128K
|| config.livenessBond == 0 || config.ethDepositRingBufferSize <= 1
|| config.ethDepositMinCountPerBlock == 0
// Audit recommendation, and gas tested. Processing 32 deposits (as initially set in
// TaikoL1.sol) costs 72_502 gas.
|| config.ethDepositMaxCountPerBlock > 32
|| config.ethDepositMaxCountPerBlock < config.ethDepositMinCountPerBlock
|| config.ethDepositMinAmount == 0
|| config.ethDepositMaxAmount <= config.ethDepositMinAmount
|| config.ethDepositMaxAmount > type(uint96).max || config.ethDepositGas == 0
|| config.ethDepositMaxFee == 0
|| config.ethDepositMaxFee > type(uint96).max / config.ethDepositMaxCountPerBlock
) return false;

return true;
}
}
4 changes: 4 additions & 0 deletions packages/protocol/contracts/common/EssentialContract.sol
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,16 @@ abstract contract EssentialContract is UUPSUpgradeable, Ownable2StepUpgradeable,
function pause() public virtual whenNotPaused {
_paused = _TRUE;
emit Paused(msg.sender);
// We call the authorize function here to avoid:
// Warning (5740): Unreachable code.
_authorizePause(msg.sender);
}

function unpause() public virtual whenPaused {
_paused = _FALSE;
emit Unpaused(msg.sender);
// We call the authorize function here to avoid:
// Warning (5740): Unreachable code.
_authorizePause(msg.sender);
}

Expand Down
4 changes: 2 additions & 2 deletions packages/protocol/test/L1/TaikoL1.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ contract TaikoL1Test is TaikoL1TestBase {
vm.roll(block.number + 15 * 12);

uint16 minTier = meta.minTier;
vm.warp(block.timestamp + L1.getTier(minTier).cooldownWindow * 60 + 1);
vm.warp(block.timestamp + tierProvider().getTier(minTier).cooldownWindow * 60 + 1);

verifyBlock(Carol, 1);
parentHash = blockHash;
Expand Down Expand Up @@ -89,7 +89,7 @@ contract TaikoL1Test is TaikoL1TestBase {
proveBlock(Bob, Bob, meta, parentHash, blockHash, stateRoot, meta.minTier, "");
vm.roll(block.number + 15 * 12);
uint16 minTier = meta.minTier;
vm.warp(block.timestamp + L1.getTier(minTier).cooldownWindow * 60 + 1);
vm.warp(block.timestamp + tierProvider().getTier(minTier).cooldownWindow * 60 + 1);

verifyBlock(Alice, 2);
parentHash = blockHash;
Expand Down
52 changes: 34 additions & 18 deletions packages/protocol/test/L1/TaikoL1LibProvingWithTiers.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ contract TaikoL1LibProvingWithTiers is TaikoL1TestBase {
vm.roll(block.number + 15 * 12);

uint16 minTier = meta.minTier;
vm.warp(block.timestamp + L1.getTier(minTier).cooldownWindow * 60 + 1);
vm.warp(block.timestamp + tierProvider().getTier(minTier).cooldownWindow * 60 + 1);

verifyBlock(Carol, 1);

Expand Down Expand Up @@ -128,14 +128,17 @@ contract TaikoL1LibProvingWithTiers is TaikoL1TestBase {

vm.roll(block.number + 15 * 12);

vm.warp(block.timestamp + L1.getTier(minTier).cooldownWindow * 60 + 1);
vm.warp(block.timestamp + tierProvider().getTier(minTier).cooldownWindow * 60 + 1);

// Cannot verify block because it is contested..
verifyBlock(Carol, 1);

proveHigherTierProof(meta, parentHash, stateRoot, blockHash, minTier);

vm.warp(block.timestamp + L1.getTier(LibTiers.TIER_GUARDIAN).cooldownWindow * 60 + 1);
vm.warp(
block.timestamp + tierProvider().getTier(LibTiers.TIER_GUARDIAN).cooldownWindow * 60
+ 1
);
// Now can verify
console2.log("Probalom verify-olni");
verifyBlock(Carol, 1);
Expand Down Expand Up @@ -177,15 +180,18 @@ contract TaikoL1LibProvingWithTiers is TaikoL1TestBase {

vm.roll(block.number + 15 * 12);

vm.warp(block.timestamp + L1.getTier(minTier).cooldownWindow * 60 + 1);
vm.warp(block.timestamp + tierProvider().getTier(minTier).cooldownWindow * 60 + 1);

// Cannot verify block because it is contested..
verifyBlock(Carol, 1);

proveHigherTierProof(meta, parentHash, stateRoot, blockHash, minTier);

// Otherwise just not contest
vm.warp(block.timestamp + L1.getTier(LibTiers.TIER_GUARDIAN).cooldownWindow * 60 + 1);
vm.warp(
block.timestamp + tierProvider().getTier(LibTiers.TIER_GUARDIAN).cooldownWindow * 60
+ 1
);
// Now can verify
verifyBlock(Carol, 1);

Expand Down Expand Up @@ -228,7 +234,7 @@ contract TaikoL1LibProvingWithTiers is TaikoL1TestBase {

vm.roll(block.number + 15 * 12);

vm.warp(block.timestamp + L1.getTier(minTier).cooldownWindow * 60 + 1);
vm.warp(block.timestamp + tierProvider().getTier(minTier).cooldownWindow * 60 + 1);

// Cannot verify block because it is contested..
verifyBlock(Carol, 1);
Expand All @@ -239,7 +245,10 @@ contract TaikoL1LibProvingWithTiers is TaikoL1TestBase {
}

// Otherwise just not contest
vm.warp(block.timestamp + L1.getTier(LibTiers.TIER_GUARDIAN).cooldownWindow * 60 + 1);
vm.warp(
block.timestamp + tierProvider().getTier(LibTiers.TIER_GUARDIAN).cooldownWindow * 60
+ 1
);
// Now can verify
verifyBlock(Carol, 1);

Expand Down Expand Up @@ -282,7 +291,8 @@ contract TaikoL1LibProvingWithTiers is TaikoL1TestBase {
vm.roll(block.number + 15 * 12);

vm.warp(
block.timestamp + L1.getTier(LibTiers.TIER_GUARDIAN).cooldownWindow * 60 + 1
block.timestamp
+ tierProvider().getTier(LibTiers.TIER_GUARDIAN).cooldownWindow * 60 + 1
);

// Cannot verify block because it is contested..
Expand All @@ -301,7 +311,10 @@ contract TaikoL1LibProvingWithTiers is TaikoL1TestBase {
}

// Otherwise just not contest
vm.warp(block.timestamp + L1.getTier(LibTiers.TIER_GUARDIAN).cooldownWindow * 60 + 1);
vm.warp(
block.timestamp + tierProvider().getTier(LibTiers.TIER_GUARDIAN).cooldownWindow * 60
+ 1
);
// Now can verify
verifyBlock(Carol, 1);

Expand Down Expand Up @@ -345,7 +358,7 @@ contract TaikoL1LibProvingWithTiers is TaikoL1TestBase {
vm.roll(block.number + 15 * 12);

uint16 minTier = meta.minTier;
vm.warp(block.timestamp + L1.getTier(minTier).cooldownWindow * 60 + 1);
vm.warp(block.timestamp + tierProvider().getTier(minTier).cooldownWindow * 60 + 1);

verifyBlock(Carol, 1);
parentHash = blockHash;
Expand Down Expand Up @@ -379,7 +392,7 @@ contract TaikoL1LibProvingWithTiers is TaikoL1TestBase {
vm.roll(block.number + 15 * 12);

uint16 minTier = meta.minTier;
vm.warp(block.timestamp + L1.getTier(minTier).cooldownWindow * 60 + 1);
vm.warp(block.timestamp + tierProvider().getTier(minTier).cooldownWindow * 60 + 1);

proveBlock(
Bob,
Expand Down Expand Up @@ -441,7 +454,7 @@ contract TaikoL1LibProvingWithTiers is TaikoL1TestBase {
vm.roll(block.number + 15 * 12);

uint16 minTier = meta.minTier;
vm.warp(block.timestamp + L1.getTier(minTier).cooldownWindow * 60 + 1);
vm.warp(block.timestamp + tierProvider().getTier(minTier).cooldownWindow * 60 + 1);

verifyBlock(Carol, 1);

Expand All @@ -458,7 +471,10 @@ contract TaikoL1LibProvingWithTiers is TaikoL1TestBase {
);

vm.roll(block.number + 15 * 12);
vm.warp(block.timestamp + L1.getTier(LibTiers.TIER_GUARDIAN).cooldownWindow * 60 + 1);
vm.warp(
block.timestamp + tierProvider().getTier(LibTiers.TIER_GUARDIAN).cooldownWindow * 60
+ 1
);

verifyBlock(Carol, 1);
parentHash = blockHash;
Expand Down Expand Up @@ -507,7 +523,7 @@ contract TaikoL1LibProvingWithTiers is TaikoL1TestBase {
vm.roll(block.number + 15 * 12);

uint16 minTier = meta.minTier;
vm.warp(block.timestamp + L1.getTier(minTier).cooldownWindow * 60 + 1);
vm.warp(block.timestamp + tierProvider().getTier(minTier).cooldownWindow * 60 + 1);

verifyBlock(Carol, 1);

Expand Down Expand Up @@ -550,7 +566,7 @@ contract TaikoL1LibProvingWithTiers is TaikoL1TestBase {
vm.roll(block.number + 15 * 12);

uint16 minTier = meta.minTier;
vm.warp(block.timestamp + L1.getTier(minTier).cooldownWindow * 60 + 1);
vm.warp(block.timestamp + tierProvider().getTier(minTier).cooldownWindow * 60 + 1);

verifyBlock(Carol, 1);

Expand Down Expand Up @@ -602,7 +618,7 @@ contract TaikoL1LibProvingWithTiers is TaikoL1TestBase {
vm.roll(block.number + 15 * 12);

uint16 minTier = meta.minTier;
vm.warp(block.timestamp + L1.getTier(minTier).cooldownWindow * 60 + 1);
vm.warp(block.timestamp + tierProvider().getTier(minTier).cooldownWindow * 60 + 1);

verifyBlock(Carol, 1);

Expand Down Expand Up @@ -742,7 +758,7 @@ contract TaikoL1LibProvingWithTiers is TaikoL1TestBase {
vm.roll(block.number + 15 * 12);

uint16 minTier = meta.minTier;
vm.warp(block.timestamp + L1.getTier(minTier).cooldownWindow * 60 + 1);
vm.warp(block.timestamp + tierProvider().getTier(minTier).cooldownWindow * 60 + 1);

verifyBlock(Carol, 1);

Expand Down Expand Up @@ -812,7 +828,7 @@ contract TaikoL1LibProvingWithTiers is TaikoL1TestBase {
vm.roll(block.number + 15 * 12);

uint16 minTier = meta.minTier;
vm.warp(block.timestamp + L1.getTier(minTier).cooldownWindow * 60 + 1);
vm.warp(block.timestamp + tierProvider().getTier(minTier).cooldownWindow * 60 + 1);

verifyBlock(Carol, 1);

Expand Down
Loading

0 comments on commit 1d15f2b

Please sign in to comment.