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

Reward validator when marking missing proof #209

Merged
merged 3 commits into from
Jan 27, 2025
Merged
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
1 change: 1 addition & 0 deletions configuration/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions contracts/Configuration.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion contracts/FuzzMarketplace.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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)
),
Expand Down
8 changes: 6 additions & 2 deletions contracts/Marketplace.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
35 changes: 34 additions & 1 deletion test/Marketplace.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ describe("Marketplace", function () {
host2,
host3,
hostRewardRecipient,
hostCollateralRecipient
hostCollateralRecipient,
validatorRecipient
let request
let slot

Expand All @@ -123,6 +124,7 @@ describe("Marketplace", function () {
host3,
hostRewardRecipient,
hostCollateralRecipient,
validatorRecipient,
] = await ethers.getSigners()
host = host1

Expand All @@ -136,6 +138,7 @@ describe("Marketplace", function () {
host3,
hostRewardRecipient,
hostCollateralRecipient,
validatorRecipient,
]) {
await token.mint(account.address, ACCOUNT_STARTING_BALANCE)
}
Expand Down Expand Up @@ -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 () {
Expand Down
1 change: 1 addition & 0 deletions test/examples.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const exampleConfiguration = () => ({
repairRewardPercentage: 10,
maxNumberOfSlashes: 5,
slashPercentage: 10,
validatorRewardPercentage: 20,
},
proofs: {
period: 10,
Expand Down
Loading