Skip to content

Commit

Permalink
test(protocol): More tests and remove redundant revert statement (#13642
Browse files Browse the repository at this point in the history
)
  • Loading branch information
adaki2004 authored Apr 24, 2023
1 parent 5ee7788 commit 8eb9f1a
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 4 deletions.
2 changes: 0 additions & 2 deletions packages/protocol/contracts/L1/TaikoToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ contract TaikoToken is EssentialContract, ERC20Upgradeable, IMintableERC20 {
address account,
uint256 amount
) public onlyFromNamed("proto_broker") {
if (account == address(0)) revert TKO_INVALID_ADDR();
_mint(account, amount);
emit Mint(account, amount);
}
Expand All @@ -113,7 +112,6 @@ contract TaikoToken is EssentialContract, ERC20Upgradeable, IMintableERC20 {
address account,
uint256 amount
) public onlyFromNamed("proto_broker") {
if (account == address(0)) revert TKO_INVALID_ADDR();
_burn(account, amount);
emit Burn(account, amount);
}
Expand Down
67 changes: 67 additions & 0 deletions packages/protocol/test/TaikoL1.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -233,4 +233,71 @@ contract TaikoL1Test is TaikoL1TestBase {
console2.log("gas per eth deposit:", gasPerEthDeposit);
console2.log("maxEthDepositsPerBlock:", count);
}

/// @dev getXchainBlockHash tests
function test_getXchainBlockHash0() external {
bytes32 genHash = L1.getXchainBlockHash(0);
assertEq(GENESIS_BLOCK_HASH, genHash);

// Not yet avail.
genHash = L1.getXchainBlockHash(1);
assertEq(bytes32(0), genHash);
}

/// @dev getXchainSignalRoot tests
function test_getXchainSignalRoot() external {
uint256 iterationCnt = 10;
// Declare here so that block prop/prove/verif. can be used in 1 place
TaikoData.BlockMetadata memory meta;
TaikoData.ForkChoice memory fk;
bytes32 blockHash;
bytes32 signalRoot;
bytes32[] memory parentHashes = new bytes32[](iterationCnt);
parentHashes[0] = GENESIS_BLOCK_HASH;

depositTaikoToken(Alice, 1E6 * 1E8, 100000 ether);

// Propose blocks
for (uint256 blockId = 1; blockId < iterationCnt; blockId++) {
//printVariables("before propose");
meta = proposeBlock(Alice, 1000000, 1024);
mine(5);

blockHash = bytes32(1E10 + blockId);
signalRoot = bytes32(1E9 + blockId);

proveBlock(
Bob,
meta,
parentHashes[blockId - 1],
blockId == 1 ? 0 : 1000000,
1000000,
blockHash,
signalRoot,
false
);
verifyBlock(Carol, 1);

// Querying written blockhash
bytes32 genHash = L1.getXchainBlockHash(blockId);
assertEq(blockHash, genHash);

mine(5);
parentHashes[blockId] = blockHash;
}

// 1st
uint256 queriedBlockId = 1;
bytes32 expectedSR = bytes32(1E9 + queriedBlockId);

assertEq(expectedSR, L1.getXchainSignalRoot(queriedBlockId));

// 2nd
queriedBlockId = 2;
expectedSR = bytes32(1E9 + queriedBlockId);
assertEq(expectedSR, L1.getXchainSignalRoot(queriedBlockId));

// Not found
assertEq(bytes32(0), L1.getXchainSignalRoot((iterationCnt + 1)));
}
}
87 changes: 85 additions & 2 deletions packages/protocol/test/TaikoToken.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ contract TaikoTokenTest is Test {

function test_mint_invalid_address() public {
vm.prank(protoBroker);
vm.expectRevert(TaikoToken.TKO_INVALID_ADDR.selector);
vm.expectRevert("ERC20: mint to the zero address");
tko.mint(address(0), 1 ether);
}

Expand All @@ -125,7 +125,7 @@ contract TaikoTokenTest is Test {

function test_burn_invalid_address() public {
vm.prank(protoBroker);
vm.expectRevert(TaikoToken.TKO_INVALID_ADDR.selector);
vm.expectRevert("ERC20: burn from the zero address");
tko.burn(address(0), 1 ether);
}

Expand Down Expand Up @@ -167,6 +167,17 @@ contract TaikoTokenTest is Test {
tko.mint(Eve, amountToMint);
assertEq(tko.balanceOf(Eve), amountToMint);

vm.prank(Eve);
vm.expectRevert("ERC20: transfer to the zero address");
tko.transfer(address(0), amountToMint);
}

function test_transfer_to_contract_address() public {
uint256 amountToMint = 1 ether;
vm.prank(protoBroker);
tko.mint(Eve, amountToMint);
assertEq(tko.balanceOf(Eve), amountToMint);

vm.prank(Eve);
vm.expectRevert(TaikoToken.TKO_INVALID_ADDR.selector);
tko.transfer(address(tko), amountToMint);
Expand All @@ -185,6 +196,78 @@ contract TaikoTokenTest is Test {
assertEq(tko.balanceOf(Eve), amountToMint);
}

function test_transferFrom() public {
uint256 amountToMint = 1 ether;
vm.prank(protoBroker);
tko.mint(Eve, amountToMint);
assertEq(tko.balanceOf(Eve), amountToMint);

vm.prank(Eve);
tko.approve(Dave, 1 ether);

vm.prank(Dave);
tko.transferFrom(Eve, Dave, amountToMint);

assertEq(tko.balanceOf(Eve), 0);
assertEq(tko.balanceOf(Dave), amountToMint);
}

function test_transferFrom_to_is_invalid() public {
uint256 amountToMint = 1 ether;
vm.prank(protoBroker);
tko.mint(Eve, amountToMint);
assertEq(tko.balanceOf(Eve), amountToMint);

vm.prank(Eve);
tko.approve(Dave, 1 ether);

vm.prank(Dave);
vm.expectRevert("ERC20: transfer to the zero address");
tko.transferFrom(Eve, address(0), amountToMint);
}

function test_transferFrom_to_is_the_contract() public {
uint256 amountToMint = 1 ether;
vm.prank(protoBroker);
tko.mint(Eve, amountToMint);
assertEq(tko.balanceOf(Eve), amountToMint);

vm.prank(Eve);
tko.approve(Dave, 1 ether);

vm.prank(Dave);
vm.expectRevert(TaikoToken.TKO_INVALID_ADDR.selector);
tko.transferFrom(Eve, address(tko), amountToMint);
}

function test_transferFrom_from_is_invalid() public {
uint256 amountToMint = 1 ether;
vm.prank(protoBroker);
tko.mint(Eve, amountToMint);
assertEq(tko.balanceOf(Eve), amountToMint);

vm.prank(Eve);
tko.approve(Dave, 1 ether);

vm.prank(Dave);
// transferFrom(address(0)) will always throw has no allowance
vm.expectRevert("ERC20: insufficient allowance");
tko.transferFrom(address(0), Dave, amountToMint);
}

function test_transferFrom_amount_exceeded() public {
uint256 amountToMint = 1 ether;
uint256 amountToTransfer = 2 ether;
vm.prank(protoBroker);
tko.mint(Eve, amountToMint);
assertEq(tko.balanceOf(Eve), amountToMint);

vm.prank(Eve);
vm.expectRevert();
tko.transfer(address(tko), amountToTransfer);
assertEq(tko.balanceOf(Eve), amountToMint);
}

function registerAddress(string memory name, address addr) internal {
addressManager.setAddress(block.chainid, name, addr);
}
Expand Down

0 comments on commit 8eb9f1a

Please sign in to comment.