Skip to content

Commit

Permalink
feat(protocol): fix issues in AssignmentHook (#15486)
Browse files Browse the repository at this point in the history
Co-authored-by: David <[email protected]>
Co-authored-by: Daniel Wang <[email protected]>
Co-authored-by: Daniel Wang <[email protected]>
  • Loading branch information
4 people authored Jan 13, 2024
1 parent 28c8122 commit a394abd
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 16 deletions.
4 changes: 4 additions & 0 deletions packages/protocol/contracts/L1/ITaikoL1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,8 @@ interface ITaikoL1 {
/// @notice Verifies up to a certain number of blocks.
/// @param maxBlocksToVerify Max number of blocks to verify.
function verifyBlocks(uint64 maxBlocksToVerify) external;

/// @notice Gets the configuration of the TaikoL1 contract.
/// @return Config struct containing configuration parameters.
function getConfig() external view returns (TaikoData.Config memory);
}
5 changes: 2 additions & 3 deletions packages/protocol/contracts/L1/TaikoL1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,8 @@ contract TaikoL1 is
return ITierProvider(resolve("tier_provider", false)).getMinTier(rand);
}

/// @notice Gets the configuration of the TaikoL1 contract.
/// @return Config struct containing configuration parameters.
function getConfig() public view virtual returns (TaikoData.Config memory) {
/// @inheritdoc ITaikoL1
function getConfig() public view virtual override returns (TaikoData.Config memory) {
// All hard-coded configurations:
// - treasury: the actual TaikoL2 address.
// - blockMaxTxs: 150 (limited by the PSE zkEVM circuits)
Expand Down
35 changes: 24 additions & 11 deletions packages/protocol/contracts/L1/hooks/AssignmentHook.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import "lib/openzeppelin-contracts/contracts/token/ERC20/IERC20.sol";
import "lib/openzeppelin-contracts/contracts/token/ERC20/utils/SafeERC20.sol";
import "../../common/EssentialContract.sol";
import "../../libs/LibAddress.sol";
import "../TaikoData.sol";
import "../ITaikoL1.sol";
import "./IHook.sol";

/// @title AssignmentHook
Expand All @@ -39,7 +39,7 @@ contract AssignmentHook is EssentialContract, IHook {

struct Input {
ProverAssignment assignment;
uint256 tip;
uint256 tip; // A tip to L1 block builder
}

// Max gas paying the prover. This should be large enough to prevent the
Expand Down Expand Up @@ -70,6 +70,11 @@ contract AssignmentHook is EssentialContract, IHook {
nonReentrant
onlyFromNamed("taiko")
{
// Note that
// - 'msg.sender' is the TaikoL1 contract address
// - 'block.coinbase' is the L1 block builder
// - 'meta.coinbase' is the L2 block proposer

Input memory input = abi.decode(data, (Input));
ProverAssignment memory assignment = input.assignment;

Expand All @@ -85,15 +90,16 @@ contract AssignmentHook is EssentialContract, IHook {

// Hash the assignment with the blobHash, this hash will be signed by
// the prover, therefore, we add a string as a prefix.
bytes32 hash = hashAssignment(assignment, msg.sender, meta.blobHash);
address taikoL1Address = msg.sender;
bytes32 hash = hashAssignment(assignment, taikoL1Address, meta.blobHash);

if (!blk.assignedProver.isValidSignature(hash, assignment.signature)) {
revert HOOK_ASSIGNMENT_INVALID_SIG();
}

// Send the liveness bond to the Taiko contract
IERC20 tko = IERC20(resolve("taiko_token", false));
tko.transferFrom(blk.assignedProver, msg.sender, blk.livenessBond);
tko.transferFrom(blk.assignedProver, taikoL1Address, blk.livenessBond);

// Find the prover fee using the minimal tier
uint256 proverFee = _getProverFee(assignment.tierFees, meta.minTier);
Expand All @@ -102,12 +108,13 @@ contract AssignmentHook is EssentialContract, IHook {
// Ether or ERC20 tokens.
uint256 refund;
if (assignment.feeToken == address(0)) {
if (msg.value < proverFee + input.tip) {
uint256 totalFee = proverFee + input.tip;
if (msg.value < totalFee) {
revert HOOK_ASSIGNMENT_INSUFFICIENT_FEE();
}

unchecked {
refund = msg.value - proverFee - input.tip;
refund = msg.value - totalFee;
}

// Paying Ether
Expand All @@ -120,7 +127,9 @@ contract AssignmentHook is EssentialContract, IHook {
refund = msg.value - input.tip;
}
// Paying ERC20 tokens
IERC20(assignment.feeToken).safeTransferFrom(msg.sender, blk.assignedProver, proverFee);
IERC20(assignment.feeToken).safeTransferFrom(
meta.coinbase, blk.assignedProver, proverFee
);
}

// block.coinbase can be address(0) in tests
Expand All @@ -129,25 +138,29 @@ contract AssignmentHook is EssentialContract, IHook {
}

if (refund != 0) {
msg.sender.sendEther(refund);
// Send all remaininger Ether back to TaikoL1 contract
taikoL1Address.sendEther(refund);
}

emit BlockAssigned(blk.assignedProver, meta, assignment);
}

function hashAssignment(
ProverAssignment memory assignment,
address taikoAddress,
address taikoL1Address,
bytes32 blobHash
)
public
pure
view
returns (bytes32)
{
return keccak256(
abi.encode(
"PROVER_ASSIGNMENT",
taikoAddress,
ITaikoL1(taikoL1Address).getConfig().chainId,
taikoL1Address,
address(this),
assignment.metaHash,
blobHash,
assignment.feeToken,
assignment.expiry,
Expand Down
6 changes: 4 additions & 2 deletions packages/protocol/contracts/L1/libs/LibProposing.sol
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,10 @@ library LibProposing {
}

// Check that after hooks, the Taiko Token balance of this contract
// have increased by at least config.livenessBond
if (tko.balanceOf(address(this)) < tkoBalance + config.livenessBond) {
// have increased by the same amount as config.livenessBond (to prevent)
// multiple draining payments by a malicious proposer nesting the same
// hook.
if (tko.balanceOf(address(this)) != tkoBalance + config.livenessBond) {
revert L1_LIVENESS_BOND_NOT_RECEIVED();
}
}
Expand Down

2 comments on commit a394abd

@vercel
Copy link

@vercel vercel bot commented on a394abd Jan 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

bridge-ui-v2-internal – ./packages/bridge-ui-v2

bridge-ui-v2-internal.vercel.app
bridge-ui-v2-internal-taikoxyz.vercel.app
bridge-ui-v2-internal-git-alpha-6-taikoxyz.vercel.app

@vercel
Copy link

@vercel vercel bot commented on a394abd Jan 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

bridge-ui-v2-a6 – ./packages/bridge-ui-v2

bridge-ui-v2-a6-taikoxyz.vercel.app
bridge-ui-v2-a6-git-alpha-6-taikoxyz.vercel.app
bridge-ui-v2-a6.vercel.app
bridge.katla.taiko.xyz

Please sign in to comment.