Skip to content

Commit

Permalink
feat: adds testing for edge cases of penatly and residual balance sla…
Browse files Browse the repository at this point in the history
…shing
  • Loading branch information
ckartik committed Sep 15, 2024
1 parent 496badd commit dc87e36
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 3 deletions.
8 changes: 8 additions & 0 deletions contracts/contracts/interfaces/IProviderRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ interface IProviderRegistry {
/// @dev Event emitted when the fee payout period in blocks is updated
event FeePayoutPeriodBlocksUpdated(uint256 indexed newFeePayoutPeriodBlocks);

/// @dev Event emitted when there are insufficient funds to slash
event InsufficientFundsToSlash(
address indexed provider,
uint256 providerStake,
uint256 residualAmount,
uint256 penaltyFee
);

function registerAndStake(bytes calldata blsPublicKey) external payable;

function stake() external payable;
Expand Down
50 changes: 47 additions & 3 deletions contracts/test/core/ProviderRegistryTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ contract ProviderRegistryTest is Test {
event FeeTransfer(uint256 amount, address indexed recipient);
event PenaltyFeeRecipientUpdated(address indexed newPenaltyFeeRecipient);
event FeePayoutPeriodBlocksUpdated(uint256 indexed newFeePayoutPeriodBlocks);
event InsufficientFundsToSlash(
address indexed provider,
uint256 providerStake,
uint256 residualAmt,
uint256 penaltyFee
);

function setUp() public {
testNumber = 42;
Expand Down Expand Up @@ -257,21 +263,59 @@ contract ProviderRegistryTest is Test {
vm.expectRevert(bytes(""));
providerRegistry.slash(1 ether, provider, payable(bidder),100);
}

function testFail_ShouldRetrieveFundsGreaterThanStake() public {
function test_ShouldRetrieveFundsWhenSlashIsGreaterThanStake() public {
vm.prank(address(this));
providerRegistry.setPreconfirmationsContract(address(this));

vm.deal(provider, 3 ether);
vm.prank(provider);
providerRegistry.registerAndStake{value: 2 ether}(validBLSPubkey);
address bidder = vm.addr(4);
vm.expectRevert(bytes(""));
vm.prank(address(this));

vm.expectEmit(true, true, true, true);
emit InsufficientFundsToSlash(provider, 2 ether, 3 ether, 0.3 ether);
providerRegistry.slash(3 ether, provider, payable(bidder), 100);

assertEq(providerRegistry.getAccumulatedPenaltyFee(), 0);
assertEq(providerRegistry.providerStakes(provider), 0 ether);
}

function test_ShouldRetrieveFundsWhenSlashIsGreaterThanStakePenaltyNotCovered() public {
vm.prank(address(this));
providerRegistry.setPreconfirmationsContract(address(this));

vm.deal(provider, 3 ether);
vm.prank(provider);
providerRegistry.registerAndStake{value: 3 ether}(validBLSPubkey);
address bidder = vm.addr(4);
vm.prank(address(this));

vm.expectEmit(true, true, true, true);
emit InsufficientFundsToSlash(provider, 3 ether, 3 ether, 0.3 ether);
providerRegistry.slash(3 ether, provider, payable(bidder), 100);

assertEq(providerRegistry.getAccumulatedPenaltyFee(), 0);
assertEq(providerRegistry.providerStakes(provider), 0 ether);
}

function test_ShouldRetrieveFundsWhenSlashIsGreaterThanStakePenaltyNotFullyCovered() public {
vm.prank(address(this));
providerRegistry.setPreconfirmationsContract(address(this));

vm.deal(provider, 3.1 ether);
vm.prank(provider);
providerRegistry.registerAndStake{value: 3.1 ether}(validBLSPubkey);
address bidder = vm.addr(4);
vm.prank(address(this));

vm.expectEmit(true, true, true, true);
emit InsufficientFundsToSlash(provider, 3.1 ether, 3 ether, 0.3 ether);
providerRegistry.slash(3 ether, provider, payable(bidder), 100);

assertEq(providerRegistry.getAccumulatedPenaltyFee(), 0.1 ether);
assertEq(providerRegistry.providerStakes(provider), 0 ether);
}
function test_PenaltyFeeBehavior() public {
providerRegistry.setNewPenaltyFeeRecipient(vm.addr(6));
vm.deal(provider, 3 ether);
Expand Down

0 comments on commit dc87e36

Please sign in to comment.