Skip to content

Commit cfd30bf

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 13f3bc9 commit cfd30bf

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
@@ -66,9 +66,10 @@ contract Marketplace is SlotReservations, Proofs, StateRetrieval, Endian {
6666
}
6767

6868
struct Slot {
69-
SlotState state;
7069
RequestId requestId;
7170
uint64 slotIndex;
71+
uint128 currentCollateral;
72+
SlotState state;
7273
address host;
7374
}
7475

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

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

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

223+
slot.currentCollateral = collateral;
224+
218225
_addToMySlots(slot.host, slotId);
219226

220227
slot.state = SlotState.Filled;
@@ -317,6 +324,7 @@ contract Marketplace is SlotReservations, Proofs, StateRetrieval, Endian {
317324
_vault.designate(fund, validatorAccount, validatorReward);
318325
_vault.burnDesignated(fund, hostAccount, slashedAmount - validatorReward);
319326

327+
slot.currentCollateral -= slashedAmount;
320328
if (missingProofs(slotId) >= _config.collateral.maxNumberOfSlashes) {
321329
// When the number of slashings is at or above the allowed amount,
322330
// free the slot.
@@ -346,6 +354,7 @@ contract Marketplace is SlotReservations, Proofs, StateRetrieval, Endian {
346354
_removeFromMySlots(slot.host, slotId);
347355
delete _reservations[slotId]; // We purge all the reservations for the slot
348356
slot.state = SlotState.Repair;
357+
slot.currentCollateral = 0;
349358
slot.host = address(0);
350359
context.slotsFilled -= 1;
351360
emit SlotFreed(requestId, slot.slotIndex);
@@ -371,6 +380,7 @@ contract Marketplace is SlotReservations, Proofs, StateRetrieval, Endian {
371380
Request storage request = _requests[requestId];
372381
context.state = RequestState.Finished;
373382
Slot storage slot = _slots[slotId];
383+
slot.currentCollateral = 0;
374384

375385
_removeFromMyRequests(request.client, requestId);
376386
_removeFromMySlots(slot.host, slotId);
@@ -392,6 +402,7 @@ contract Marketplace is SlotReservations, Proofs, StateRetrieval, Endian {
392402
SlotId slotId
393403
) private requestIsKnown(requestId) {
394404
Slot storage slot = _slots[slotId];
405+
slot.currentCollateral = 0;
395406
_removeFromMySlots(slot.host, slotId);
396407
FundId fund = requestId.asFundId();
397408
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
@@ -326,6 +326,13 @@ describe("Marketplace", function () {
326326
})
327327
})
328328

329+
it("updates the slot's current collateral", async function() {
330+
await marketplace.reserveSlot(slot.request, slot.index)
331+
await marketplace.fillSlot(slot.request, slot.index, proof)
332+
const collateral = await marketplace.currentCollateral(slotId(slot))
333+
expect(collateral).to.equal(collateralPerSlot(request))
334+
})
335+
329336
it("fails to retrieve a request of an empty slot", async function () {
330337
expect(marketplace.getActiveSlot(slotId(slot))).to.be.revertedWith(
331338
"Marketplace_SlotIsFree"
@@ -549,6 +556,12 @@ describe("Marketplace", function () {
549556
.to.emit(marketplace, "SlotFreed")
550557
.withArgs(slot.request, slot.index)
551558
})
559+
560+
it("updates the slot's current collateral", async function() {
561+
await waitUntilStarted(marketplace, request, proof, token)
562+
await marketplace.freeSlot(id)
563+
expect(await marketplace.currentCollateral(id)).to.equal(0)
564+
})
552565
})
553566

554567
describe("paying out a slot", function () {
@@ -603,6 +616,21 @@ describe("Marketplace", function () {
603616
expect(endBalance - startBalance).to.be.equal(expectedPartialPayout)
604617
})
605618

619+
it("updates the collateral when freeing a finished slot", async function() {
620+
await waitUntilStarted(marketplace, request, proof, token)
621+
await waitUntilFinished(marketplace, requestId(request))
622+
await marketplace.freeSlot(slotId(slot))
623+
expect(await marketplace.currentCollateral(slotId(slot))).to.equal(0)
624+
})
625+
626+
it("updates the collateral when freeing a cancelled slot", async function() {
627+
await marketplace.reserveSlot(slot.request, slot.index)
628+
await marketplace.fillSlot(slot.request, slot.index, proof)
629+
await waitUntilCancelled(marketplace, request)
630+
await marketplace.freeSlot(slotId(slot))
631+
expect(await marketplace.currentCollateral(slotId(slot))).to.equal(0)
632+
})
633+
606634
it("does not pay when the contract hasn't ended", async function () {
607635
await marketplace.reserveSlot(slot.request, slot.index)
608636
await marketplace.fillSlot(slot.request, slot.index, proof)
@@ -1157,7 +1185,13 @@ describe("Marketplace", function () {
11571185
await marketplace.markProofAsMissing(id, missedPeriod)
11581186
const endBalance = await marketplace.getSlotBalance(id)
11591187
expect(endBalance).to.equal(startBalance - slashAmount)
1188+
})
11601189

1190+
it("updates the slot's current collateral", async function() {
1191+
await setNextBlockTimestamp(await currentTime())
1192+
await marketplace.markProofAsMissing(id, missedPeriod)
1193+
const currentCollateral = await marketplace.currentCollateral(id)
1194+
expect(currentCollateral).to.equal(collateral - slashAmount)
11611195
})
11621196

11631197
it("rewards validator when marking proof as missing", async function () {
@@ -1200,6 +1234,11 @@ describe("Marketplace", function () {
12001234
expect(await marketplace.getSlotBalance(slotId(slot))).to.equal(0)
12011235
})
12021236

1237+
it("updates the slot's current collateral", async function() {
1238+
const collateral = await marketplace.currentCollateral(slotId(slot))
1239+
expect(collateral).to.equal(0)
1240+
})
1241+
12031242
it("resets missed proof counter", async function () {
12041243
expect(await marketplace.missingProofs(slotId(slot))).to.equal(0)
12051244
})

0 commit comments

Comments
 (0)