mirror of
https://github.com/codex-storage/codex-contracts-eth.git
synced 2025-02-13 20:06:35 +00:00
Reward validator when marking missing proof (#209)
This commit is contained in:
parent
6753d20b17
commit
51bae145fc
@ -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
|
||||
|
@ -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 {
|
||||
|
@ -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)
|
||||
),
|
||||
|
@ -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,
|
||||
|
@ -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 () {
|
||||
|
@ -7,6 +7,7 @@ const exampleConfiguration = () => ({
|
||||
repairRewardPercentage: 10,
|
||||
maxNumberOfSlashes: 5,
|
||||
slashPercentage: 10,
|
||||
validatorRewardPercentage: 20,
|
||||
},
|
||||
proofs: {
|
||||
period: 10,
|
||||
|
Loading…
x
Reference in New Issue
Block a user