Skip to content

Commit

Permalink
chore(protocol): add two more tests (#17428)
Browse files Browse the repository at this point in the history
Co-authored-by: Keszey Dániel <[email protected]>
  • Loading branch information
adaki2004 and Keszey Dániel authored May 30, 2024
1 parent db40dbd commit ae64be4
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 0 deletions.
63 changes: 63 additions & 0 deletions packages/protocol/test/L1/TaikoL1.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,69 @@ contract TaikoL1Test is TaikoL1TestBase {
printVariables("after verify");
}

/// @dev Test if a given transition deadline is based on proposal time
function test_L1_in_proving_window_logic() external {
giveEthAndTko(Alice, 1000 ether, 1000 ether);
console2.log("Alice balance:", tko.balanceOf(Alice));
giveEthAndTko(Bob, 1e8 ether, 100 ether);
console2.log("Bob balance:", tko.balanceOf(Bob));
giveEthAndTko(Carol, 1e8 ether, 100 ether);

bytes32 parentHash = GENESIS_BLOCK_HASH;

for (uint256 blockId = 1; blockId <= conf.blockMaxProposals; blockId++) {
(TaikoData.BlockMetadata memory meta,) = proposeBlock(Alice, Bob, 1_000_000, 1024);
bytes32 blockHash;
bytes32 stateRoot;
if (blockId % 2 == 0) {
// Stay within proving window
vm.warp(block.timestamp + 60);

blockHash = bytes32(1e10 + blockId);
stateRoot = bytes32(1e9 + blockId);

bytes32 secondTransitionHash = randBytes32();

// Within window and first transition -> Should revert if not assigned prover or
// guardian
proveBlock(
Carol,
meta,
parentHash,
secondTransitionHash,
stateRoot,
meta.minTier,
TaikoErrors.L1_NOT_ASSIGNED_PROVER.selector
);

// Only guardian or assigned prover is allowed
if (blockId % 4 == 0) {
proveBlock(Bob, meta, parentHash, blockHash, stateRoot, meta.minTier, "");
} else {
proveBlock(
Carol, meta, parentHash, blockHash, stateRoot, LibTiers.TIER_GUARDIAN, ""
);
}
} else {
// Go into the future, outside of block proposal time + window
vm.warp(block.timestamp + 2 days);

blockHash = bytes32(1e10 + blockId);
stateRoot = bytes32(1e9 + blockId);

bytes32 secondTransitionHash = randBytes32();

// Carol can prove since it is outside of the window
proveBlock(
Carol, meta, parentHash, secondTransitionHash, stateRoot, meta.minTier, ""
);

parentHash = blockHash;
}
parentHash = blockHash;
}
}

function test_pauseProving() external {
L1.pauseProving(true);

Expand Down
52 changes: 52 additions & 0 deletions packages/protocol/test/bridge/Bridge2_processMessage.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ contract OutOfQuotaManager is IQuotaManager {
}
}

contract AlwaysAvailableQuotaManager is IQuotaManager {
function consumeQuota(address, uint256) external pure { }
}

contract BridgeTest2_processMessage is BridgeTest2 {
function test_bridge2_processMessage_basic() public dealEther(Alice) assertSameTotalBalance {
vm.startPrank(Alice);
Expand Down Expand Up @@ -424,4 +428,52 @@ contract BridgeTest2_processMessage is BridgeTest2 {

assertEq(davidBalance, David.balance);
}

function test_bridge2_processMessage_and_retryMessage_malicious_way()
public
dealEther(Bob)
dealEther(Alice)
assertSameTotalBalance
{
vm.startPrank(owner);
addressManager.setAddress(
uint64(block.chainid), "quota_manager", address(new OutOfQuotaManager())
);
vm.stopPrank();

IBridge.Message memory message;

message.destChainId = uint64(block.chainid);
message.srcChainId = remoteChainId;

bytes32 hashOfMaliciousMessage =
0x3c6e0b8a9c15224b7f0a1e5f4c8f7683d5a0a4e32a34c6c7c7e1f4d9a9d9f6b4;
message.gasLimit = 1_000_000;
message.fee = 5_000_000;
message.value = 2 ether;
message.destOwner = Alice;
message.to = address(bridge);
message.data = abi.encodeWithSignature("sendSignal(bytes32)", hashOfMaliciousMessage);
uint256 davidBalance = David.balance;

vm.prank(Alice);
bridge.processMessage(message, fakeProof);
bytes32 hash = bridge.hashMessage(message);
assertTrue(bridge.messageStatus(hash) == IBridge.Status.RETRIABLE);

// Allow now quota
vm.startPrank(owner);
addressManager.setAddress(
uint64(block.chainid), "quota_manager", address(new AlwaysAvailableQuotaManager())
);
vm.stopPrank();

uint256 aliceBalanceBeforeRefund = Alice.balance;
vm.prank(message.destOwner);
bridge.retryMessage(message, false);

assertTrue(bridge.messageStatus(hash) == IBridge.Status.DONE);
// She could get back the ether but cannot execute the malicious call.
assertEq(Alice.balance, aliceBalanceBeforeRefund + message.value + message.fee);
}
}

0 comments on commit ae64be4

Please sign in to comment.