[market] Replace fulfillRequest() by fillSlot()

This commit is contained in:
Mark Spanbroek 2022-07-20 15:59:07 +02:00 committed by markspanbroek
parent 67e4a28ed0
commit 156cd5ba73
4 changed files with 39 additions and 21 deletions

View File

@ -55,6 +55,12 @@ method fulfillRequest(market: OnChainMarket,
proof: seq[byte]) {.async.} = proof: seq[byte]) {.async.} =
await market.contract.fulfillRequest(requestId, proof) await market.contract.fulfillRequest(requestId, proof)
method fillSlot(market: OnChainMarket,
requestId: array[32, byte],
slotIndex: UInt256,
proof: seq[byte]) {.async.} =
await market.contract.fillSlot(requestId, slotIndex, proof)
method subscribeRequests(market: OnChainMarket, method subscribeRequests(market: OnChainMarket,
callback: OnRequest): callback: OnRequest):
Future[MarketSubscription] {.async.} = Future[MarketSubscription] {.async.} =

View File

@ -35,6 +35,12 @@ method fulfillRequest*(market: Market,
proof: seq[byte]) {.base, async.} = proof: seq[byte]) {.base, async.} =
raiseAssert("not implemented") raiseAssert("not implemented")
method fillSlot*(market: Market,
requestId: array[32, byte],
slotIndex: UInt256,
proof: seq[byte]) {.base, async.} =
raiseAssert("not implemented")
method subscribeRequests*(market: Market, method subscribeRequests*(market: Market,
callback: OnRequest): callback: OnRequest):
Future[Subscription] {.base, async.} = Future[Subscription] {.base, async.} =

View File

@ -17,7 +17,7 @@ proc example*(_: type StorageRequest): StorageRequest =
duration: (10 * 60 * 60).u256, # 10 hours duration: (10 * 60 * 60).u256, # 10 hours
proofProbability: 4.u256, # require a proof roughly once every 4 periods proofProbability: 4.u256, # require a proof roughly once every 4 periods
reward: 84.u256, reward: 84.u256,
slots: 42 slots: 4
), ),
content: StorageContent( content: StorageContent(
cid: "zb2rhheVmk3bLks5MgzTqyznLu1zqGH5jrfTA1eAZXrjx7Vob", cid: "zb2rhheVmk3bLks5MgzTqyznLu1zqGH5jrfTA1eAZXrjx7Vob",

View File

@ -12,12 +12,13 @@ ethersuite "On-Chain Market":
var storage: Storage var storage: Storage
var token: TestToken var token: TestToken
var request: StorageRequest var request: StorageRequest
var slotIndex: UInt256
setup: setup:
let deployment = deployment() let deployment = deployment()
storage = Storage.new(!deployment.address(Storage), provider.getSigner()) storage = Storage.new(!deployment.address(Storage), provider.getSigner())
token = TestToken.new(!deployment.address(TestToken), provider.getSigner()) token = TestToken.new(!deployment.address(TestToken), provider.getSigner())
await token.mint(accounts[0], 1000.u256) await token.mint(accounts[0], 1_000_000_000.u256)
let collateral = await storage.collateralAmount() let collateral = await storage.collateralAmount()
await token.approve(storage.address, collateral) await token.approve(storage.address, collateral)
@ -28,6 +29,8 @@ ethersuite "On-Chain Market":
request = StorageRequest.example request = StorageRequest.example
request.client = accounts[0] request.client = accounts[0]
slotIndex = (request.ask.slots div 2).u256
test "fails to instantiate when contract does not have a signer": test "fails to instantiate when contract does not have a signer":
let storageWithoutSigner = storage.connect(provider) let storageWithoutSigner = storage.connect(provider)
expect AssertionError: expect AssertionError:
@ -37,19 +40,19 @@ ethersuite "On-Chain Market":
check (await market.getSigner()) == (await provider.getSigner().getAddress()) check (await market.getSigner()) == (await provider.getSigner().getAddress())
test "supports storage requests": test "supports storage requests":
await token.approve(storage.address, request.ask.reward) await token.approve(storage.address, request.price)
check (await market.requestStorage(request)) == request check (await market.requestStorage(request)) == request
test "sets client address when submitting storage request": test "sets client address when submitting storage request":
var requestWithoutClient = request var requestWithoutClient = request
requestWithoutClient.client = Address.default requestWithoutClient.client = Address.default
await token.approve(storage.address, request.ask.reward) await token.approve(storage.address, request.price)
let submitted = await market.requestStorage(requestWithoutClient) let submitted = await market.requestStorage(requestWithoutClient)
check submitted.client == accounts[0] check submitted.client == accounts[0]
test "can retrieve previously submitted requests": test "can retrieve previously submitted requests":
check (await market.getRequest(request.id)) == none StorageRequest check (await market.getRequest(request.id)) == none StorageRequest
await token.approve(storage.address, request.ask.reward) await token.approve(storage.address, request.price)
discard await market.requestStorage(request) discard await market.requestStorage(request)
check (await market.getRequest(request.id)) == some request check (await market.getRequest(request.id)) == some request
@ -60,42 +63,43 @@ ethersuite "On-Chain Market":
receivedIds.add(id) receivedIds.add(id)
receivedAsks.add(ask) receivedAsks.add(ask)
let subscription = await market.subscribeRequests(onRequest) let subscription = await market.subscribeRequests(onRequest)
await token.approve(storage.address, request.ask.reward) await token.approve(storage.address, request.price)
discard await market.requestStorage(request) discard await market.requestStorage(request)
check receivedIds == @[request.id] check receivedIds == @[request.id]
check receivedAsks == @[request.ask] check receivedAsks == @[request.ask]
await subscription.unsubscribe() await subscription.unsubscribe()
test "supports fulfilling of requests": test "supports filling of slots":
await token.approve(storage.address, request.ask.reward) await token.approve(storage.address, request.price)
discard await market.requestStorage(request) discard await market.requestStorage(request)
await market.fulfillRequest(request.id, proof) await market.fillSlot(request.id, slotIndex, proof)
test "can retrieve host that fulfilled request": test "can retrieve host that filled slot":
await token.approve(storage.address, request.ask.reward) await token.approve(storage.address, request.price)
discard await market.requestStorage(request) discard await market.requestStorage(request)
check (await market.getHost(request.id)) == none Address check (await market.getHost(request.slotId(slotIndex))) == none Address
await market.fulfillRequest(request.id, proof) await market.fillSlot(request.id, slotIndex, proof)
check (await market.getHost(request.id)) == some accounts[0] check (await market.getHost(request.slotId(slotIndex))) == some accounts[0]
test "support fulfillment subscriptions": test "support fulfillment subscriptions":
await token.approve(storage.address, request.ask.reward) await token.approve(storage.address, request.price)
discard await market.requestStorage(request) discard await market.requestStorage(request)
var receivedIds: seq[array[32, byte]] var receivedIds: seq[array[32, byte]]
proc onFulfillment(id: array[32, byte]) = proc onFulfillment(id: array[32, byte]) =
receivedIds.add(id) receivedIds.add(id)
let subscription = await market.subscribeFulfillment(request.id, onFulfillment) let subscription = await market.subscribeFulfillment(request.id, onFulfillment)
await market.fulfillRequest(request.id, proof) for slotIndex in 0..<request.ask.slots:
await market.fillSlot(request.id, slotIndex.u256, proof)
check receivedIds == @[request.id] check receivedIds == @[request.id]
await subscription.unsubscribe() await subscription.unsubscribe()
test "subscribes only to fulfillmentof a certain request": test "subscribes only to fulfillment of a certain request":
var otherRequest = StorageRequest.example var otherRequest = StorageRequest.example
otherRequest.client = accounts[0] otherRequest.client = accounts[0]
await token.approve(storage.address, request.ask.reward) await token.approve(storage.address, request.price)
discard await market.requestStorage(request) discard await market.requestStorage(request)
await token.approve(storage.address, otherrequest.ask.reward) await token.approve(storage.address, otherrequest.price)
discard await market.requestStorage(otherRequest) discard await market.requestStorage(otherRequest)
var receivedIds: seq[array[32, byte]] var receivedIds: seq[array[32, byte]]
@ -104,8 +108,10 @@ ethersuite "On-Chain Market":
let subscription = await market.subscribeFulfillment(request.id, onFulfillment) let subscription = await market.subscribeFulfillment(request.id, onFulfillment)
await market.fulfillRequest(request.id, proof) for slotIndex in 0..<request.ask.slots:
await market.fulfillRequest(otherRequest.id, proof) await market.fillSlot(request.id, slotIndex.u256, proof)
for slotIndex in 0..<otherRequest.ask.slots:
await market.fillSlot(otherRequest.id, slotIndex.u256, proof)
check receivedIds == @[request.id] check receivedIds == @[request.id]