-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
Copy pathProverSet.sol
177 lines (150 loc) · 5.4 KB
/
ProverSet.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import "@openzeppelin/contracts-upgradeable/token/ERC20/extensions/ERC20VotesUpgradeable.sol";
import "@openzeppelin/contracts/interfaces/IERC1271.sol";
import "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "src/shared/common/EssentialContract.sol";
import "src/shared/common/LibStrings.sol";
import "src/shared/common/LibAddress.sol";
import "../based/ITaikoL1.sol";
interface IHasRecipient {
function recipient() external view returns (address);
}
/// @title ProverSet
/// @notice A contract that holds TKO token and acts as a Taiko prover. This contract will simply
/// relay `proveBlock` calls to TaikoL1 so msg.sender doesn't need to hold any TKO.
/// @custom:security-contact security@taiko.xyz
contract ProverSet is EssentialContract, IERC1271 {
bytes4 private constant _EIP1271_MAGICVALUE = 0x1626ba7e;
mapping(address prover => bool isProver) public isProver; // slot 1
address public admin; // slot 2
uint256[48] private __gap;
event ProverEnabled(address indexed prover, bool indexed enabled);
error INVALID_STATUS();
error PERMISSION_DENIED();
modifier onlyAuthorized() {
if (msg.sender != admin && msg.sender != IHasRecipient(admin).recipient()) {
revert PERMISSION_DENIED();
}
_;
}
modifier onlyProver() {
if (!isProver[msg.sender]) revert PERMISSION_DENIED();
_;
}
/// @notice Initializes the contract.
function init(
address _owner,
address _admin,
address _rollupAddressManager
)
external
nonZeroAddr(_admin)
initializer
{
__Essential_init(_owner, _rollupAddressManager);
admin = _admin;
IERC20(tkoToken()).approve(taikoL1(), type(uint256).max);
}
/// @notice Receives ETH as fees.
receive() external payable { }
function approveAllowance(address _address, uint256 _allowance) external onlyOwner {
IERC20(tkoToken()).approve(_address, _allowance);
}
/// @notice Enables or disables a prover.
function enableProver(address _prover, bool _isProver) external onlyAuthorized {
if (isProver[_prover] == _isProver) revert INVALID_STATUS();
isProver[_prover] = _isProver;
emit ProverEnabled(_prover, _isProver);
}
/// @notice Withdraws Taiko tokens back to the admin address.
function withdrawToAdmin(uint256 _amount) external onlyAuthorized {
IERC20(tkoToken()).transfer(admin, _amount);
}
/// @notice Withdraws ETH back to the owner address.
function withdrawEtherToAdmin(uint256 _amount) external onlyAuthorized {
LibAddress.sendEtherAndVerify(admin, _amount);
}
/// @notice Propose a Taiko block.
function proposeBlock(
bytes calldata _params,
bytes calldata _txList
)
external
payable
onlyProver
{
ITaikoL1(taikoL1()).proposeBlock(_params, _txList);
}
/// @notice Propose a Taiko block.
function proposeBlockV2(
bytes calldata _params,
bytes calldata _txList
)
external
payable
onlyProver
{
ITaikoL1(taikoL1()).proposeBlockV2(_params, _txList);
}
/// @notice Propose multiple Taiko blocks.
function proposeBlocksV2(
bytes[] calldata _paramsArr,
bytes[] calldata _txListArr
)
external
payable
onlyProver
{
ITaikoL1(taikoL1()).proposeBlocksV2(_paramsArr, _txListArr);
}
/// @notice Proves or contests a Taiko block.
function proveBlock(uint64 _blockId, bytes calldata _input) external onlyProver {
ITaikoL1(taikoL1()).proveBlock(_blockId, _input);
}
/// @notice Batch proves or contests Taiko blocks.
function proveBlocks(
uint64[] calldata _blockId,
bytes[] calldata _input,
bytes calldata _batchProof
)
external
onlyProver
{
ITaikoL1(taikoL1()).proveBlocks(_blockId, _input, _batchProof);
}
/// @notice Deposits Taiko token to TaikoL1 contract.
function depositBond(uint256 _amount) external onlyAuthorized {
ITaikoL1(taikoL1()).depositBond(_amount);
}
/// @notice Withdraws Taiko token from TaikoL1 contract.
function withdrawBond(uint256 _amount) external onlyAuthorized {
ITaikoL1(taikoL1()).withdrawBond(_amount);
}
/// @notice Delegates token voting right to a delegatee.
/// @param _delegatee The delegatee to receive the voting right.
function delegate(address _delegatee) external onlyAuthorized nonReentrant {
ERC20VotesUpgradeable(tkoToken()).delegate(_delegatee);
}
// This function is necessary for this contract to become an assigned prover.
function isValidSignature(
bytes32 _hash,
bytes calldata _signature
)
external
view
returns (bytes4 magicValue_)
{
(address recovered, ECDSA.RecoverError error) = ECDSA.tryRecover(_hash, _signature);
if (error == ECDSA.RecoverError.NoError && isProver[recovered]) {
magicValue_ = _EIP1271_MAGICVALUE;
}
}
function taikoL1() internal view virtual returns (address) {
return resolve(LibStrings.B_TAIKO, false);
}
function tkoToken() internal view virtual returns (address) {
return resolve(LibStrings.B_TAIKO_TOKEN, false);
}
}