Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: adds testing and ensures event emit matches state #573

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions contracts/contracts/core/BidderRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ contract BidderRegistry is
) external nonReentrant whenNotPaused {
uint256 currentWindow = blockTrackerContract.getCurrentWindow();
uint256 totalAmount;
uint256[] memory withdrawnWindows = new uint256[](windows.length);
uint256[] memory withdrawnAmounts = new uint256[](windows.length);

uint256 len = windows.length;
for (uint256 i = 0; i < len; ++i) {
Expand All @@ -145,13 +147,18 @@ contract BidderRegistry is
usedFunds[msg.sender][uint64(blockNumber)] = 0;
}

emit BidderWithdrawal(msg.sender, window, amount);

withdrawnWindows[i] = window;
withdrawnAmounts[i] = amount;
totalAmount += amount;
}

(bool success, ) = msg.sender.call{value: totalAmount}("");
require(success, BidderWithdrawalTransferFailed(msg.sender, totalAmount));

// Emit events only after successful transfer
for (uint256 i = 0; i < len; ++i) {
emit BidderWithdrawal(msg.sender, withdrawnWindows[i], withdrawnAmounts[i]);
}
}

/**
Expand Down
50 changes: 46 additions & 4 deletions contracts/test/core/BidderRegistryTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,28 @@ contract BidderRegistryTest is Test {
uint256 public feePayoutPeriodBlocks;
BlockTracker public blockTracker;
ProviderRegistry public providerRegistry;
address public oracleAccount;

/// @dev Event emitted when a bidder is registered with their staked amount
event BidderRegistered(address indexed bidder, uint256 indexed stakedAmount, uint256 indexed windowNumber);

event BidderRegistered(
address indexed bidder,
uint256 indexed depositedAmount,
uint256 indexed windowNumber
);
event FeeTransfer(uint256 amount, address indexed recipient);
event ProtocolFeeRecipientUpdated(address indexed newProtocolFeeRecipient);
event FeePayoutPeriodBlocksUpdated(uint256 indexed newFeePayoutPeriodBlocks);
event BidderWithdrawal(address indexed bidder, uint256 indexed window, uint256 indexed amount);

function setUp() public {
testNumber = 42;
feePercent = 10 * 1e16;
minStake = 1e18 wei;
feeRecipient = vm.addr(9);
feePayoutPeriodBlocks = 100;
oracleAccount = address(this);
address blockTrackerProxy = Upgrades.deployUUPSProxy(
"BlockTracker.sol",
abi.encodeCall(BlockTracker.initialize, (address(this), address(this)))
abi.encodeCall(BlockTracker.initialize, (oracleAccount, address(this)))
);
blockTracker = BlockTracker(payable(blockTrackerProxy));

Expand Down Expand Up @@ -557,4 +562,41 @@ contract BidderRegistryTest is Test {
vm.expectRevert(IBidderRegistry.DepositAmountIsZero.selector);
bidderRegistry.depositForWindow{value: 0 ether}(nextWindow);
}

function test_WithdrawFromWindowsEventEmission() public {
uint256 currentWindow = blockTracker.getCurrentWindow();
uint256 nextWindow = currentWindow + 1;
uint256 depositAmount = 1000000000000000000;

// Setup: Deposit for two windows
vm.startPrank(bidder);
bidderRegistry.depositForWindow{value: depositAmount}(nextWindow);
bidderRegistry.depositForWindow{value: depositAmount}(nextWindow + 1);

// Move blocks forward to make windows withdrawable
uint256 newBlockNumber = block.number + WindowFromBlockNumber.BLOCKS_PER_WINDOW * 3;
vm.stopPrank();
vm.prank(oracleAccount);
blockTracker.recordL1Block(uint64(newBlockNumber), "test");

vm.startPrank(bidder);
// Prepare windows array for withdrawal
uint256[] memory windows = new uint256[](2);
windows[0] = nextWindow;
windows[1] = nextWindow + 1;

// Expect events in order
vm.expectEmit(true, true, true, true);
emit BidderWithdrawal(bidder, nextWindow, depositAmount);
vm.expectEmit(true, true, true, true);
emit BidderWithdrawal(bidder, nextWindow + 1, depositAmount);

// Perform withdrawal
bidderRegistry.withdrawFromWindows(windows);
vm.stopPrank();

// Verify state after withdrawal
assertEq(bidderRegistry.getDeposit(bidder, nextWindow), 0);
assertEq(bidderRegistry.getDeposit(bidder, nextWindow + 1), 0);
}
}
Loading