-
Notifications
You must be signed in to change notification settings - Fork 359
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add transaction filtering (#272)
- Loading branch information
1 parent
1e50836
commit cfc96e7
Showing
13 changed files
with
301 additions
and
2 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
l1-contracts/contracts/dev-contracts/test/DummyTransactionFiltererFalse.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity 0.8.20; | ||
|
||
import {ITransactionFilterer} from "../../state-transition/chain-interfaces/ITransactionFilterer.sol"; | ||
|
||
contract TransactionFiltererFalse is ITransactionFilterer { | ||
// add this to be excluded from coverage report | ||
function test() internal virtual {} | ||
|
||
function isTransactionAllowed( | ||
address, | ||
address, | ||
uint256, | ||
uint256, | ||
bytes memory, | ||
address | ||
) external view returns (bool) { | ||
return false; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
l1-contracts/contracts/dev-contracts/test/DummyTransactionFiltererTrue.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity 0.8.20; | ||
|
||
import {ITransactionFilterer} from "../../state-transition/chain-interfaces/ITransactionFilterer.sol"; | ||
|
||
contract TransactionFiltererTrue is ITransactionFilterer { | ||
// add this to be excluded from coverage report | ||
function test() internal virtual {} | ||
|
||
function isTransactionAllowed( | ||
address, | ||
address, | ||
uint256, | ||
uint256, | ||
bytes memory, | ||
address | ||
) external view returns (bool) { | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
l1-contracts/contracts/state-transition/chain-interfaces/ITransactionFilterer.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity 0.8.20; | ||
|
||
/// @title The interface of the L1 -> L2 transaction filterer. | ||
/// @author Matter Labs | ||
/// @custom:security-contact [email protected] | ||
interface ITransactionFilterer { | ||
/// @notice Check if the transaction is allowed | ||
/// @param sender The sender of the transaction | ||
/// @param contractL2 The L2 receiver address | ||
/// @param mintValue The value of the L1 transaction | ||
/// @param l2Value The msg.value of the L2 transaction | ||
/// @param l2Calldata The calldata of the L2 transaction | ||
/// @param refundRecipient The address to refund the excess value | ||
/// @return Whether the transaction is allowed | ||
function isTransactionAllowed( | ||
address sender, | ||
address contractL2, | ||
uint256 mintValue, | ||
uint256 l2Value, | ||
bytes memory l2Calldata, | ||
address refundRecipient | ||
) external view returns (bool); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
...undry/unit/concrete/state-transition/chain-deps/facets/Admin/SetTransactionFilterer.t.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity 0.8.20; | ||
|
||
import {AdminTest} from "./_Admin_Shared.t.sol"; | ||
|
||
contract SetTransactionFiltererTest is AdminTest { | ||
event NewTransactionFilterer(address oldTransactionFilterer, address newTransactionFilterer); | ||
|
||
function test_initialFilterer() public { | ||
address admin = utilsFacet.util_getAdmin(); | ||
address transactionFilterer = makeAddr("transactionFilterer"); | ||
|
||
vm.expectEmit(true, true, true, true, address(adminFacet)); | ||
emit NewTransactionFilterer(address(0), transactionFilterer); | ||
|
||
vm.startPrank(admin); | ||
adminFacet.setTransactionFilterer(transactionFilterer); | ||
} | ||
|
||
function test_replaceFilterer() public { | ||
address admin = utilsFacet.util_getAdmin(); | ||
address f1 = makeAddr("f1"); | ||
address f2 = makeAddr("f2"); | ||
utilsFacet.util_setTransactionFilterer(f1); | ||
|
||
vm.expectEmit(true, true, true, true, address(adminFacet)); | ||
emit NewTransactionFilterer(f1, f2); | ||
|
||
vm.startPrank(admin); | ||
adminFacet.setTransactionFilterer(f2); | ||
} | ||
|
||
function test_revertWhen_notAdmin() public { | ||
address transactionFilterer = makeAddr("transactionFilterer"); | ||
|
||
vm.expectRevert("StateTransition Chain: not admin"); | ||
vm.startPrank(makeAddr("nonAdmin")); | ||
adminFacet.setTransactionFilterer(transactionFilterer); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
...t/concrete/state-transition/chain-deps/facets/Mailbox/BridgehubRequestL2Transaction.t.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity 0.8.20; | ||
|
||
import {MailboxTest} from "./_Mailbox_Shared.t.sol"; | ||
import {BridgehubL2TransactionRequest} from "solpp/common/Messaging.sol"; | ||
import {REQUIRED_L2_GAS_PRICE_PER_PUBDATA} from "solpp/common/Config.sol"; | ||
import {TransactionFiltererTrue} from "solpp/dev-contracts/test/DummyTransactionFiltererTrue.sol"; | ||
import {TransactionFiltererFalse} from "solpp/dev-contracts/test/DummyTransactionFiltererFalse.sol"; | ||
|
||
contract BridgehubRequestL2TransactionTest is MailboxTest { | ||
function test_successWithoutFilterer() public { | ||
address bridgehub = makeAddr("bridgehub"); | ||
|
||
utilsFacet.util_setBridgehub(bridgehub); | ||
utilsFacet.util_setBaseTokenGasPriceMultiplierDenominator(1); | ||
utilsFacet.util_setPriorityTxMaxGasLimit(100000000); | ||
|
||
BridgehubL2TransactionRequest memory req = getBridgehubRequestL2TransactionRequest(); | ||
|
||
vm.deal(bridgehub, 100 ether); | ||
vm.prank(address(bridgehub)); | ||
bytes32 canonicalTxHash = mailboxFacet.bridgehubRequestL2Transaction{value: 10 ether}(req); | ||
assertTrue(canonicalTxHash != bytes32(0), "canonicalTxHash should not be 0"); | ||
} | ||
|
||
function test_successWithFilterer() public { | ||
address bridgehub = makeAddr("bridgehub"); | ||
TransactionFiltererTrue tf = new TransactionFiltererTrue(); | ||
|
||
utilsFacet.util_setBridgehub(bridgehub); | ||
utilsFacet.util_setTransactionFilterer(address(tf)); | ||
utilsFacet.util_setBaseTokenGasPriceMultiplierDenominator(1); | ||
utilsFacet.util_setPriorityTxMaxGasLimit(100000000); | ||
|
||
BridgehubL2TransactionRequest memory req = getBridgehubRequestL2TransactionRequest(); | ||
|
||
vm.deal(bridgehub, 100 ether); | ||
vm.prank(address(bridgehub)); | ||
bytes32 canonicalTxHash = mailboxFacet.bridgehubRequestL2Transaction{value: 10 ether}(req); | ||
assertTrue(canonicalTxHash != bytes32(0), "canonicalTxHash should not be 0"); | ||
} | ||
|
||
function test_revertWhen_FalseFilterer() public { | ||
address bridgehub = makeAddr("bridgehub"); | ||
TransactionFiltererFalse tf = new TransactionFiltererFalse(); | ||
|
||
utilsFacet.util_setBridgehub(bridgehub); | ||
utilsFacet.util_setTransactionFilterer(address(tf)); | ||
utilsFacet.util_setBaseTokenGasPriceMultiplierDenominator(1); | ||
utilsFacet.util_setPriorityTxMaxGasLimit(100000000); | ||
|
||
BridgehubL2TransactionRequest memory req = getBridgehubRequestL2TransactionRequest(); | ||
|
||
vm.deal(bridgehub, 100 ether); | ||
vm.prank(address(bridgehub)); | ||
vm.expectRevert(bytes("tf")); | ||
mailboxFacet.bridgehubRequestL2Transaction{value: 10 ether}(req); | ||
} | ||
|
||
function getBridgehubRequestL2TransactionRequest() private returns (BridgehubL2TransactionRequest memory req) { | ||
bytes[] memory factoryDeps = new bytes[](1); | ||
factoryDeps[0] = "11111111111111111111111111111111"; | ||
|
||
req = BridgehubL2TransactionRequest({ | ||
sender: sender, | ||
contractL2: makeAddr("contractL2"), | ||
mintValue: 2 ether, | ||
l2Value: 10000, | ||
l2Calldata: "", | ||
l2GasLimit: 10000000, | ||
l2GasPerPubdataByteLimit: REQUIRED_L2_GAS_PRICE_PER_PUBDATA, | ||
factoryDeps: factoryDeps, | ||
refundRecipient: sender | ||
}); | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
...st/foundry/unit/concrete/state-transition/chain-deps/facets/Mailbox/_Mailbox_Shared.t.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity 0.8.20; | ||
|
||
import {Test} from "forge-std/Test.sol"; | ||
|
||
import {Utils} from "foundry-test/unit/concrete/Utils/Utils.sol"; | ||
import {UtilsFacet} from "foundry-test/unit/concrete/Utils/UtilsFacet.sol"; | ||
|
||
import {MailboxFacet} from "solpp/state-transition/chain-deps/facets/Mailbox.sol"; | ||
import {Diamond} from "solpp/state-transition/libraries/Diamond.sol"; | ||
import {IMailbox} from "solpp/state-transition/chain-interfaces/IMailbox.sol"; | ||
|
||
contract MailboxTest is Test { | ||
IMailbox internal mailboxFacet; | ||
UtilsFacet internal utilsFacet; | ||
address sender; | ||
|
||
function getMailboxSelectors() public pure returns (bytes4[] memory) { | ||
bytes4[] memory selectors = new bytes4[](1); | ||
selectors[0] = IMailbox.bridgehubRequestL2Transaction.selector; | ||
return selectors; | ||
} | ||
|
||
function setUp() public virtual { | ||
sender = makeAddr("sender"); | ||
vm.deal(sender, 100 ether); | ||
|
||
Diamond.FacetCut[] memory facetCuts = new Diamond.FacetCut[](2); | ||
facetCuts[0] = Diamond.FacetCut({ | ||
facet: address(new MailboxFacet()), | ||
action: Diamond.Action.Add, | ||
isFreezable: true, | ||
selectors: getMailboxSelectors() | ||
}); | ||
facetCuts[1] = Diamond.FacetCut({ | ||
facet: address(new UtilsFacet()), | ||
action: Diamond.Action.Add, | ||
isFreezable: true, | ||
selectors: Utils.getUtilsFacetSelectors() | ||
}); | ||
|
||
address diamondProxy = Utils.makeDiamondProxy(facetCuts); | ||
mailboxFacet = IMailbox(diamondProxy); | ||
utilsFacet = UtilsFacet(diamondProxy); | ||
} | ||
|
||
// add this to be excluded from coverage report | ||
function test() internal virtual {} | ||
} |