diff --git a/configuration/configuration.js b/configuration/configuration.js index 4381aa6..938cf63 100644 --- a/configuration/configuration.js +++ b/configuration/configuration.js @@ -7,6 +7,7 @@ const DEFAULT_CONFIGURATION = { repairRewardPercentage: 10, maxNumberOfSlashes: 2, slashPercentage: 20, + validatorRewardPercentage: 20, // percentage of the slashed amount going to the validators }, proofs: { // period has to be less than downtime * blocktime diff --git a/contracts/Configuration.sol b/contracts/Configuration.sol index 2b1fd88..ae75212 100644 --- a/contracts/Configuration.sol +++ b/contracts/Configuration.sol @@ -14,6 +14,7 @@ struct CollateralConfig { uint8 repairRewardPercentage; uint8 maxNumberOfSlashes; // frees slot when the number of slashing reaches this value uint8 slashPercentage; // percentage of the collateral that is slashed + uint8 validatorRewardPercentage; // percentage of the slashed amount going to the validators } struct ProofConfig { diff --git a/contracts/FuzzMarketplace.sol b/contracts/FuzzMarketplace.sol index 6a810a7..d86c391 100644 --- a/contracts/FuzzMarketplace.sol +++ b/contracts/FuzzMarketplace.sol @@ -9,7 +9,7 @@ contract FuzzMarketplace is Marketplace { constructor() Marketplace( MarketplaceConfig( - CollateralConfig(10, 5, 10), + CollateralConfig(10, 5, 10, 20), ProofConfig(10, 5, 64, "", 67), SlotReservationsConfig(20) ), diff --git a/contracts/Marketplace.sol b/contracts/Marketplace.sol index 9b10ba8..8a0c540 100644 --- a/contracts/Marketplace.sol +++ b/contracts/Marketplace.sol @@ -334,10 +334,14 @@ contract Marketplace is SlotReservations, Proofs, StateRetrieval, Endian { Slot storage slot = _slots[slotId]; Request storage request = _requests[slot.requestId]; - // TODO: Reward for validator that calls this function - uint256 slashedAmount = (request.ask.collateralPerSlot() * _config.collateral.slashPercentage) / 100; + + uint256 validatorRewardAmount = (slashedAmount * + _config.collateral.validatorRewardPercentage) / 100; + _marketplaceTotals.sent += validatorRewardAmount; + assert(_token.transfer(msg.sender, validatorRewardAmount)); + slot.currentCollateral -= slashedAmount; if (missingProofs(slotId) >= _config.collateral.maxNumberOfSlashes) { // When the number of slashings is at or above the allowed amount, diff --git a/test/Marketplace.test.js b/test/Marketplace.test.js index da87a4c..d6170b0 100644 --- a/test/Marketplace.test.js +++ b/test/Marketplace.test.js @@ -106,7 +106,8 @@ describe("Marketplace", function () { host2, host3, hostRewardRecipient, - hostCollateralRecipient + hostCollateralRecipient, + validatorRecipient let request let slot @@ -123,6 +124,7 @@ describe("Marketplace", function () { host3, hostRewardRecipient, hostCollateralRecipient, + validatorRecipient, ] = await ethers.getSigners() host = host1 @@ -136,6 +138,7 @@ describe("Marketplace", function () { host3, hostRewardRecipient, hostCollateralRecipient, + validatorRecipient, ]) { await token.mint(account.address, ACCOUNT_STARTING_BALANCE) } @@ -1384,6 +1387,36 @@ describe("Marketplace", function () { ) ) }) + + it("rewards validator when marking proof as missing", async function () { + const id = slotId(slot) + const { slashCriterion, slashPercentage, validatorRewardPercentage } = + config.collateral + await marketplace.reserveSlot(slot.request, slot.index) + await marketplace.fillSlot(slot.request, slot.index, proof) + + switchAccount(validatorRecipient) + + const startBalance = await token.balanceOf(validatorRecipient.address) + + await waitUntilProofIsRequired(id) + let missedPeriod = periodOf(await currentTime()) + await advanceTimeForNextBlock(period + 1) + await marketplace.markProofAsMissing(id, missedPeriod) + + const endBalance = await token.balanceOf(validatorRecipient.address) + + const collateral = collateralPerSlot(request) + const slashedAmount = (collateral * slashPercentage) / 100 + + const expectedReward = Math.round( + (slashedAmount * validatorRewardPercentage) / 100 + ) + + expect(endBalance.toNumber()).to.equal( + startBalance.toNumber() + expectedReward + ) + }) }) it("frees slot when collateral slashed below minimum threshold", async function () { diff --git a/test/examples.js b/test/examples.js index df379ca..4f27b3b 100644 --- a/test/examples.js +++ b/test/examples.js @@ -7,6 +7,7 @@ const exampleConfiguration = () => ({ repairRewardPercentage: 10, maxNumberOfSlashes: 5, slashPercentage: 10, + validatorRewardPercentage: 20, }, proofs: { period: 10,