From dc87e36f2acdab5a25f366a550e5d9816e2ab6fc Mon Sep 17 00:00:00 2001 From: Kartik Chopra Date: Sun, 15 Sep 2024 15:40:21 -0400 Subject: [PATCH] feat: adds testing for edge cases of penatly and residual balance slashing --- .../interfaces/IProviderRegistry.sol | 8 +++ contracts/test/core/ProviderRegistryTest.sol | 50 +++++++++++++++++-- 2 files changed, 55 insertions(+), 3 deletions(-) diff --git a/contracts/contracts/interfaces/IProviderRegistry.sol b/contracts/contracts/interfaces/IProviderRegistry.sol index ddb5761b5..bb51ef92a 100644 --- a/contracts/contracts/interfaces/IProviderRegistry.sol +++ b/contracts/contracts/interfaces/IProviderRegistry.sol @@ -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; diff --git a/contracts/test/core/ProviderRegistryTest.sol b/contracts/test/core/ProviderRegistryTest.sol index aea0c916f..a6bb9618a 100644 --- a/contracts/test/core/ProviderRegistryTest.sol +++ b/contracts/test/core/ProviderRegistryTest.sol @@ -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; @@ -257,8 +263,7 @@ 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)); @@ -266,12 +271,51 @@ contract ProviderRegistryTest is Test { 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);