Skip to content

Commit 42150c6

Browse files
committed
fix tests
1 parent d30269a commit 42150c6

File tree

6 files changed

+57
-27
lines changed

6 files changed

+57
-27
lines changed

src/test/validium/FastWithdrawVault.t.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ contract FastWithdrawVaultTest is ValidiumTestBase {
268268
address(messenger),
269269
address(template),
270270
address(factory),
271-
address(scrollChainValidium)
271+
address(rollup)
272272
)
273273
)
274274
);

src/test/validium/L1ERC20GatewayValidium.t.sol

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {AddressAliasHelper} from "../../libraries/common/AddressAliasHelper.sol"
1414
import {IL1ERC20GatewayValidium} from "../../validium/IL1ERC20GatewayValidium.sol";
1515
import {IL2ERC20GatewayValidium} from "../../validium/IL2ERC20GatewayValidium.sol";
1616
import {L1ERC20GatewayValidium} from "../../validium/L1ERC20GatewayValidium.sol";
17+
import {ScrollChainValidium} from "../../validium/ScrollChainValidium.sol";
1718

1819
import {TransferReentrantToken} from "../mocks/tokens/TransferReentrantToken.sol";
1920
import {FeeOnTransferToken} from "../mocks/tokens/FeeOnTransferToken.sol";
@@ -128,47 +129,63 @@ contract L1ERC20GatewayValidiumTest is ValidiumTestBase {
128129
_deposit(sender, amount, recipient, gasLimit);
129130
}
130131

132+
function testDepositERC20WrongKey(
133+
uint256 amount,
134+
bytes memory recipient,
135+
uint256 gasLimit
136+
) public {
137+
(uint256 keyId, ) = rollup.getLatestEncryptionKey();
138+
hevm.expectRevert(ScrollChainValidium.ErrorUnknownEncryptionKey.selector);
139+
gateway.depositERC20(address(l1Token), recipient, amount, gasLimit, keyId + 1);
140+
}
141+
131142
function testDepositReentrantToken(uint256 amount) public {
143+
(uint256 keyId, ) = rollup.getLatestEncryptionKey();
144+
132145
// should revert, reentrant before transfer
133146
reentrantToken.setReentrantCall(
134147
address(gateway),
135148
0,
136149
abi.encodeWithSignature(
137-
"depositERC20(address,bytes,uint256,uint256)",
150+
"depositERC20(address,bytes,uint256,uint256,uint256)",
138151
address(reentrantToken),
139152
new bytes(0),
140153
amount,
141-
defaultGasLimit
154+
defaultGasLimit,
155+
keyId
142156
),
143157
true
144158
);
145159
amount = bound(amount, 1, reentrantToken.balanceOf(address(this)));
146160
hevm.expectRevert("ReentrancyGuard: reentrant call");
147-
gateway.depositERC20(address(reentrantToken), new bytes(0), amount, defaultGasLimit);
161+
162+
gateway.depositERC20(address(reentrantToken), new bytes(0), amount, defaultGasLimit, keyId);
148163

149164
// should revert, reentrant after transfer
150165
reentrantToken.setReentrantCall(
151166
address(gateway),
152167
0,
153168
abi.encodeWithSignature(
154-
"depositERC20(address,bytes,uint256,uint256)",
169+
"depositERC20(address,bytes,uint256,uint256,uint256)",
155170
address(reentrantToken),
156171
new bytes(0),
157172
amount,
158-
defaultGasLimit
173+
defaultGasLimit,
174+
keyId
159175
),
160176
false
161177
);
162178
amount = bound(amount, 1, reentrantToken.balanceOf(address(this)));
163179
hevm.expectRevert("ReentrancyGuard: reentrant call");
164-
gateway.depositERC20(address(reentrantToken), new bytes(0), amount, defaultGasLimit);
180+
gateway.depositERC20(address(reentrantToken), new bytes(0), amount, defaultGasLimit, keyId);
165181
}
166182

167183
function testFeeOnTransferTokenFailed(uint256 amount) public {
168184
feeToken.setFeeRate(1e9);
169185
amount = bound(amount, 1, feeToken.balanceOf(address(this)));
186+
(uint256 keyId, ) = rollup.getLatestEncryptionKey();
170187
hevm.expectRevert(L1ERC20GatewayValidium.ErrorAmountIsZero.selector);
171-
gateway.depositERC20(address(feeToken), new bytes(0), amount, defaultGasLimit);
188+
gateway.depositERC20(address(feeToken), new bytes(0), amount, defaultGasLimit, keyId);
172189
}
173190

174191
function testFeeOnTransferTokenSucceed(uint256 amount, uint256 feeRate) public {
@@ -179,7 +196,8 @@ contract L1ERC20GatewayValidiumTest is ValidiumTestBase {
179196
// should succeed, for valid amount
180197
uint256 balanceBefore = feeToken.balanceOf(address(gateway));
181198
uint256 fee = (amount * feeRate) / 1e9;
182-
gateway.depositERC20(address(feeToken), new bytes(0), amount, defaultGasLimit);
199+
(uint256 keyId, ) = rollup.getLatestEncryptionKey();
200+
gateway.depositERC20(address(feeToken), new bytes(0), amount, defaultGasLimit, keyId);
183201
uint256 balanceAfter = feeToken.balanceOf(address(gateway));
184202
assertEq(balanceBefore + amount - fee, balanceAfter);
185203
}
@@ -245,7 +263,8 @@ contract L1ERC20GatewayValidiumTest is ValidiumTestBase {
245263
amount = bound(amount, 1, l1Token.balanceOf(address(this)));
246264

247265
// deposit some token to L1ERC20GatewayValidium
248-
gateway.depositERC20(address(l1Token), new bytes(0), amount, defaultGasLimit);
266+
(uint256 keyId, ) = rollup.getLatestEncryptionKey();
267+
gateway.depositERC20(address(l1Token), new bytes(0), amount, defaultGasLimit, keyId);
249268

250269
// do finalize withdraw token
251270
bytes memory message = abi.encodeWithSelector(
@@ -302,7 +321,8 @@ contract L1ERC20GatewayValidiumTest is ValidiumTestBase {
302321
amount = bound(amount, 1, l1Token.balanceOf(address(this)));
303322

304323
// deposit some token to L1ERC20GatewayValidium
305-
gateway.depositERC20(address(l1Token), new bytes(0), amount, defaultGasLimit);
324+
(uint256 keyId, ) = rollup.getLatestEncryptionKey();
325+
gateway.depositERC20(address(l1Token), new bytes(0), amount, defaultGasLimit, keyId);
306326

307327
// do finalize withdraw token
308328
bytes memory message = abi.encodeWithSelector(
@@ -385,11 +405,12 @@ contract L1ERC20GatewayValidiumTest is ValidiumTestBase {
385405
);
386406

387407
if (amount == 0) {
408+
(uint256 keyId, ) = rollup.getLatestEncryptionKey();
388409
hevm.expectRevert(L1ERC20GatewayValidium.ErrorAmountIsZero.selector);
389410
if (from == address(this)) {
390-
gateway.depositERC20(address(l1Token), recipient, amount, gasLimit);
411+
gateway.depositERC20(address(l1Token), recipient, amount, gasLimit, keyId);
391412
} else {
392-
gateway.depositERC20(address(l1Token), from, recipient, amount, gasLimit);
413+
gateway.depositERC20(address(l1Token), from, recipient, amount, gasLimit, keyId);
393414
}
394415
} else {
395416
// emit QueueTransaction from L1MessageQueueV2
@@ -412,10 +433,11 @@ contract L1ERC20GatewayValidiumTest is ValidiumTestBase {
412433
uint256 gatewayBalance = l1Token.balanceOf(address(gateway));
413434
uint256 feeVaultBalance = address(feeVault).balance;
414435
assertEq(l1Messenger.messageSendTimestamp(keccak256(xDomainCalldata)), 0);
436+
(uint256 keyId, ) = rollup.getLatestEncryptionKey();
415437
if (from == address(this)) {
416-
gateway.depositERC20(address(l1Token), recipient, amount, gasLimit);
438+
gateway.depositERC20(address(l1Token), recipient, amount, gasLimit, keyId);
417439
} else {
418-
gateway.depositERC20(address(l1Token), from, recipient, amount, gasLimit);
440+
gateway.depositERC20(address(l1Token), from, recipient, amount, gasLimit, keyId);
419441
}
420442
assertEq(amount + gatewayBalance, l1Token.balanceOf(address(gateway)));
421443
assertEq(feeVaultBalance, address(feeVault).balance);
@@ -433,7 +455,8 @@ contract L1ERC20GatewayValidiumTest is ValidiumTestBase {
433455
address(counterpartGateway),
434456
address(messenger),
435457
address(template),
436-
address(factory)
458+
address(factory),
459+
address(rollup)
437460
)
438461
)
439462
);

src/test/validium/L1WETHGatewayValidium.t.sol

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,15 @@ contract L1WETHGatewayValidiumTest is ValidiumTestBase {
108108
message
109109
);
110110

111+
(uint256 keyId, ) = rollup.getLatestEncryptionKey();
112+
111113
if (amount == 0) {
112114
hevm.expectRevert(L1ERC20GatewayValidium.ErrorAmountIsZero.selector);
113-
wethGateway.deposit(recipient, amount);
115+
wethGateway.deposit(recipient, amount, keyId);
114116
} else {
115117
// revert when ErrorInsufficientValue
116118
hevm.expectRevert(L1WETHGatewayValidium.ErrorInsufficientValue.selector);
117-
wethGateway.deposit{value: amount - 1}(recipient, amount);
119+
wethGateway.deposit{value: amount - 1}(recipient, amount, keyId);
118120

119121
// emit QueueTransaction from L1MessageQueueV2
120122
{
@@ -137,7 +139,7 @@ contract L1WETHGatewayValidiumTest is ValidiumTestBase {
137139
uint256 gatewayBalance = weth.balanceOf(address(gateway));
138140
uint256 feeVaultBalance = address(feeVault).balance;
139141
assertEq(l1Messenger.messageSendTimestamp(keccak256(xDomainCalldata)), 0);
140-
wethGateway.deposit{value: amount}(recipient, amount);
142+
wethGateway.deposit{value: amount}(recipient, amount, keyId);
141143
assertEq(ethBalance - amount, address(this).balance);
142144
assertEq(amount + gatewayBalance, weth.balanceOf(address(gateway)));
143145
assertEq(feeVaultBalance, address(feeVault).balance);
@@ -155,7 +157,8 @@ contract L1WETHGatewayValidiumTest is ValidiumTestBase {
155157
address(counterpartGateway),
156158
address(messenger),
157159
address(template),
158-
address(factory)
160+
address(factory),
161+
address(rollup)
159162
)
160163
)
161164
);

src/test/validium/ValidiumTestBase.t.sol

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ abstract contract ValidiumTestBase is ScrollTestBase {
130130
address(new ScrollChainValidium(_chainId, address(messageQueueV2), address(verifier)))
131131
);
132132
rollup.initialize(address(this));
133+
rollup.grantRole(rollup.KEY_MANAGER_ROLE(), address(this));
134+
rollup.registerNewEncryptionKey(hex"123456789012345678901234567890123456789012345678901234567890123456");
133135

134136
// Make nonzero block.timestamp
135137
hevm.warp(1);

src/validium/L1ERC20GatewayValidium.sol

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,8 @@ contract L1ERC20GatewayValidium is ScrollGatewayBase, IL1ERC20GatewayValidium {
203203
uint256 _gasLimit,
204204
uint256 _keyId
205205
) internal virtual nonReentrant {
206-
// Fetch the encryption key with the given key-id.
207-
bytes memory _encryptionKey = IScrollChainValidium(scrollChainValidium).getEncryptionKey(_keyId);
206+
// Validate the encryption key with the given key-id.
207+
IScrollChainValidium(scrollChainValidium).getEncryptionKey(_keyId);
208208

209209
// 1. Transfer token into this contract.
210210
_amount = _transferERC20In(_msgSender(), _token, _amount);

src/validium/ScrollChainValidium.sol

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ contract ScrollChainValidium is AccessControlUpgradeable, PausableUpgradeable, I
6464
bytes32 public constant PROVER_ROLE = keccak256("PROVER_ROLE");
6565

6666
/// @notice The role that can rotate encryption keys.
67-
bytes32 public constant KEY_REGISTERER_ROLE = keccak256("KEY_REGISTERER_ROLE");
67+
bytes32 public constant KEY_MANAGER_ROLE = keccak256("KEY_MANAGER_ROLE");
6868

6969
/***********************
7070
* Immutable Variables *
@@ -156,14 +156,14 @@ contract ScrollChainValidium is AccessControlUpgradeable, PausableUpgradeable, I
156156
/// @inheritdoc IScrollChainValidium
157157
function getLatestEncryptionKey() external view override returns (uint256, bytes memory) {
158158
uint256 _numKeys = encryptionKeys.length;
159-
if (_numKeys == 0) revert ErrorInvalidEncryptionKeyLength();
159+
if (_numKeys == 0) revert ErrorUnknownEncryptionKey();
160160
return (_numKeys - 1, encryptionKeys[_numKeys - 1].key);
161161
}
162162

163163
/// @inheritdoc IScrollChainValidium
164164
function getEncryptionKey(uint256 _keyId) external view override returns (bytes memory) {
165165
uint256 _numKeys = encryptionKeys.length;
166-
if (_numKeys == 0) revert ErrorInvalidEncryptionKeyLength();
166+
if (_numKeys == 0) revert ErrorUnknownEncryptionKey();
167167
if (_keyId >= _numKeys) revert ErrorUnknownEncryptionKey();
168168
if (_keyId < _numKeys - 1) revert ErrorDeprecatedEncryptionKey();
169169
return encryptionKeys[_numKeys - 1].key;
@@ -274,7 +274,7 @@ contract ScrollChainValidium is AccessControlUpgradeable, PausableUpgradeable, I
274274
* Restricted Functions *
275275
************************/
276276

277-
function registerNewEncryptionKey(bytes memory _key) external onlyRole(KEY_REGISTERER_ROLE) {
277+
function registerNewEncryptionKey(bytes memory _key) external onlyRole(KEY_MANAGER_ROLE) {
278278
if (_key.length != 33) revert ErrorInvalidEncryptionKeyLength();
279279
uint256 _keyId = encryptionKeys.length;
280280

@@ -362,7 +362,9 @@ contract ScrollChainValidium is AccessControlUpgradeable, PausableUpgradeable, I
362362
bytes32 withdrawRoot = withdrawRoots[batchIndex];
363363

364364
// Get the encryption key at the time of on-chain message queue index.
365-
bytes memory encryptionKey = _getEncryptionKey(totalL1MessagesPoppedOverall - 1);
365+
bytes memory encryptionKey = totalL1MessagesPoppedOverall == 0
366+
? _getEncryptionKey(0)
367+
: _getEncryptionKey(totalL1MessagesPoppedOverall - 1);
366368

367369
bytes memory publicInputs = abi.encodePacked(
368370
layer2ChainId,

0 commit comments

Comments
 (0)