Skip to content

Commit 51bae14

Browse files
authored
Reward validator when marking missing proof (#209)
1 parent 6753d20 commit 51bae14

File tree

6 files changed

+44
-4
lines changed

6 files changed

+44
-4
lines changed

configuration/configuration.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const DEFAULT_CONFIGURATION = {
77
repairRewardPercentage: 10,
88
maxNumberOfSlashes: 2,
99
slashPercentage: 20,
10+
validatorRewardPercentage: 20, // percentage of the slashed amount going to the validators
1011
},
1112
proofs: {
1213
// period has to be less than downtime * blocktime

contracts/Configuration.sol

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ struct CollateralConfig {
1414
uint8 repairRewardPercentage;
1515
uint8 maxNumberOfSlashes; // frees slot when the number of slashing reaches this value
1616
uint8 slashPercentage; // percentage of the collateral that is slashed
17+
uint8 validatorRewardPercentage; // percentage of the slashed amount going to the validators
1718
}
1819

1920
struct ProofConfig {

contracts/FuzzMarketplace.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ contract FuzzMarketplace is Marketplace {
99
constructor()
1010
Marketplace(
1111
MarketplaceConfig(
12-
CollateralConfig(10, 5, 10),
12+
CollateralConfig(10, 5, 10, 20),
1313
ProofConfig(10, 5, 64, "", 67),
1414
SlotReservationsConfig(20)
1515
),

contracts/Marketplace.sol

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,10 +334,14 @@ contract Marketplace is SlotReservations, Proofs, StateRetrieval, Endian {
334334
Slot storage slot = _slots[slotId];
335335
Request storage request = _requests[slot.requestId];
336336

337-
// TODO: Reward for validator that calls this function
338-
339337
uint256 slashedAmount = (request.ask.collateralPerSlot() *
340338
_config.collateral.slashPercentage) / 100;
339+
340+
uint256 validatorRewardAmount = (slashedAmount *
341+
_config.collateral.validatorRewardPercentage) / 100;
342+
_marketplaceTotals.sent += validatorRewardAmount;
343+
assert(_token.transfer(msg.sender, validatorRewardAmount));
344+
341345
slot.currentCollateral -= slashedAmount;
342346
if (missingProofs(slotId) >= _config.collateral.maxNumberOfSlashes) {
343347
// When the number of slashings is at or above the allowed amount,

test/Marketplace.test.js

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ describe("Marketplace", function () {
106106
host2,
107107
host3,
108108
hostRewardRecipient,
109-
hostCollateralRecipient
109+
hostCollateralRecipient,
110+
validatorRecipient
110111
let request
111112
let slot
112113

@@ -123,6 +124,7 @@ describe("Marketplace", function () {
123124
host3,
124125
hostRewardRecipient,
125126
hostCollateralRecipient,
127+
validatorRecipient,
126128
] = await ethers.getSigners()
127129
host = host1
128130

@@ -136,6 +138,7 @@ describe("Marketplace", function () {
136138
host3,
137139
hostRewardRecipient,
138140
hostCollateralRecipient,
141+
validatorRecipient,
139142
]) {
140143
await token.mint(account.address, ACCOUNT_STARTING_BALANCE)
141144
}
@@ -1384,6 +1387,36 @@ describe("Marketplace", function () {
13841387
)
13851388
)
13861389
})
1390+
1391+
it("rewards validator when marking proof as missing", async function () {
1392+
const id = slotId(slot)
1393+
const { slashCriterion, slashPercentage, validatorRewardPercentage } =
1394+
config.collateral
1395+
await marketplace.reserveSlot(slot.request, slot.index)
1396+
await marketplace.fillSlot(slot.request, slot.index, proof)
1397+
1398+
switchAccount(validatorRecipient)
1399+
1400+
const startBalance = await token.balanceOf(validatorRecipient.address)
1401+
1402+
await waitUntilProofIsRequired(id)
1403+
let missedPeriod = periodOf(await currentTime())
1404+
await advanceTimeForNextBlock(period + 1)
1405+
await marketplace.markProofAsMissing(id, missedPeriod)
1406+
1407+
const endBalance = await token.balanceOf(validatorRecipient.address)
1408+
1409+
const collateral = collateralPerSlot(request)
1410+
const slashedAmount = (collateral * slashPercentage) / 100
1411+
1412+
const expectedReward = Math.round(
1413+
(slashedAmount * validatorRewardPercentage) / 100
1414+
)
1415+
1416+
expect(endBalance.toNumber()).to.equal(
1417+
startBalance.toNumber() + expectedReward
1418+
)
1419+
})
13871420
})
13881421

13891422
it("frees slot when collateral slashed below minimum threshold", async function () {

test/examples.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const exampleConfiguration = () => ({
77
repairRewardPercentage: 10,
88
maxNumberOfSlashes: 5,
99
slashPercentage: 10,
10+
validatorRewardPercentage: 20,
1011
},
1112
proofs: {
1213
period: 10,

0 commit comments

Comments
 (0)