Skip to content

Commit 43f3650

Browse files
committed
marketplace: re-instate currentCollateral()
So that a storage provider can know how much collateral is returned when it calls freeSlot()
1 parent 24552b4 commit 43f3650

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

contracts/Marketplace.sol

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,10 @@ contract Marketplace is SlotReservations, Proofs, StateRetrieval, Endian {
6767
}
6868

6969
struct Slot {
70-
SlotState state;
7170
RequestId requestId;
7271
uint64 slotIndex;
72+
uint128 currentCollateral;
73+
SlotState state;
7374
address host;
7475
}
7576

@@ -100,6 +101,10 @@ contract Marketplace is SlotReservations, Proofs, StateRetrieval, Endian {
100101
return _vault;
101102
}
102103

104+
function currentCollateral(SlotId slotId) public view returns (uint128) {
105+
return _slots[slotId].currentCollateral;
106+
}
107+
103108
function requestStorage(Request calldata request) public {
104109
RequestId id = request.id();
105110

@@ -216,6 +221,8 @@ contract Marketplace is SlotReservations, Proofs, StateRetrieval, Endian {
216221
_vault.designate(fund, hostAccount, designated);
217222
_vault.flow(fund, clientAccount, hostAccount, rate);
218223

224+
slot.currentCollateral = collateral;
225+
219226
_addToMySlots(slot.host, slotId);
220227

221228
slot.state = SlotState.Filled;
@@ -323,6 +330,7 @@ contract Marketplace is SlotReservations, Proofs, StateRetrieval, Endian {
323330
_vault.designate(fund, validatorAccount, validatorReward);
324331
_vault.burnDesignated(fund, hostAccount, slashedAmount - validatorReward);
325332

333+
slot.currentCollateral -= slashedAmount;
326334
if (missingProofs(slotId) >= _config.collateral.maxNumberOfSlashes) {
327335
// When the number of slashings is at or above the allowed amount,
328336
// free the slot.
@@ -352,6 +360,7 @@ contract Marketplace is SlotReservations, Proofs, StateRetrieval, Endian {
352360
_removeFromMySlots(slot.host, slotId);
353361
_reservations[slotId].clear(); // We purge all the reservations for the slot
354362
slot.state = SlotState.Repair;
363+
slot.currentCollateral = 0;
355364
slot.host = address(0);
356365
context.slotsFilled -= 1;
357366
emit SlotFreed(requestId, slot.slotIndex);
@@ -377,6 +386,7 @@ contract Marketplace is SlotReservations, Proofs, StateRetrieval, Endian {
377386
Request storage request = _requests[requestId];
378387
context.state = RequestState.Finished;
379388
Slot storage slot = _slots[slotId];
389+
slot.currentCollateral = 0;
380390

381391
_removeFromMyRequests(request.client, requestId);
382392
_removeFromMySlots(slot.host, slotId);
@@ -398,6 +408,7 @@ contract Marketplace is SlotReservations, Proofs, StateRetrieval, Endian {
398408
SlotId slotId
399409
) private requestIsKnown(requestId) {
400410
Slot storage slot = _slots[slotId];
411+
slot.currentCollateral = 0;
401412
_removeFromMySlots(slot.host, slotId);
402413
FundId fund = requestId.asFundId();
403414
AccountId account = _vault.hostAccount(slot.host, slot.slotIndex);

test/Marketplace.test.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,13 @@ describe("Marketplace", function () {
327327
})
328328
})
329329

330+
it("updates the slot's current collateral", async function () {
331+
await marketplace.reserveSlot(slot.request, slot.index)
332+
await marketplace.fillSlot(slot.request, slot.index, proof)
333+
const collateral = await marketplace.currentCollateral(slotId(slot))
334+
expect(collateral).to.equal(collateralPerSlot(request))
335+
})
336+
330337
it("fails to retrieve a request of an empty slot", async function () {
331338
expect(marketplace.getActiveSlot(slotId(slot))).to.be.revertedWith(
332339
"Marketplace_SlotIsFree"
@@ -583,6 +590,12 @@ describe("Marketplace", function () {
583590
await token.approve(marketplace.address, collateral)
584591
await marketplace.fillSlot(slot.request, slot.index, proof)
585592
})
593+
594+
it("updates the slot's current collateral", async function () {
595+
await waitUntilStarted(marketplace, request, proof, token)
596+
await marketplace.freeSlot(id)
597+
expect(await marketplace.currentCollateral(id)).to.equal(0)
598+
})
586599
})
587600

588601
describe("paying out a slot", function () {
@@ -637,6 +650,21 @@ describe("Marketplace", function () {
637650
expect(endBalance - startBalance).to.be.equal(expectedPartialPayout)
638651
})
639652

653+
it("updates the collateral when freeing a finished slot", async function () {
654+
await waitUntilStarted(marketplace, request, proof, token)
655+
await waitUntilFinished(marketplace, requestId(request))
656+
await marketplace.freeSlot(slotId(slot))
657+
expect(await marketplace.currentCollateral(slotId(slot))).to.equal(0)
658+
})
659+
660+
it("updates the collateral when freeing a cancelled slot", async function () {
661+
await marketplace.reserveSlot(slot.request, slot.index)
662+
await marketplace.fillSlot(slot.request, slot.index, proof)
663+
await waitUntilCancelled(marketplace, request)
664+
await marketplace.freeSlot(slotId(slot))
665+
expect(await marketplace.currentCollateral(slotId(slot))).to.equal(0)
666+
})
667+
640668
it("does not pay when the contract hasn't ended", async function () {
641669
await marketplace.reserveSlot(slot.request, slot.index)
642670
await marketplace.fillSlot(slot.request, slot.index, proof)
@@ -1191,7 +1219,13 @@ describe("Marketplace", function () {
11911219
await marketplace.markProofAsMissing(id, missedPeriod)
11921220
const endBalance = await marketplace.getSlotBalance(id)
11931221
expect(endBalance).to.equal(startBalance - slashAmount)
1222+
})
11941223

1224+
it("updates the slot's current collateral", async function () {
1225+
await setNextBlockTimestamp(await currentTime())
1226+
await marketplace.markProofAsMissing(id, missedPeriod)
1227+
const currentCollateral = await marketplace.currentCollateral(id)
1228+
expect(currentCollateral).to.equal(collateral - slashAmount)
11951229
})
11961230

11971231
it("rewards validator when marking proof as missing", async function () {
@@ -1234,6 +1268,11 @@ describe("Marketplace", function () {
12341268
expect(await marketplace.getSlotBalance(slotId(slot))).to.equal(0)
12351269
})
12361270

1271+
it("updates the slot's current collateral", async function () {
1272+
const collateral = await marketplace.currentCollateral(slotId(slot))
1273+
expect(collateral).to.equal(0)
1274+
})
1275+
12371276
it("resets missed proof counter", async function () {
12381277
expect(await marketplace.missingProofs(slotId(slot))).to.equal(0)
12391278
})

0 commit comments

Comments
 (0)