[purchasing] Remove PurchaseRequest; use StorageRequest
This commit is contained in:
parent
9ade3fdd37
commit
7c9c244836
|
@ -6,19 +6,13 @@ import pkg/nimcrypto
|
||||||
import ./market
|
import ./market
|
||||||
|
|
||||||
export questionable
|
export questionable
|
||||||
|
export market
|
||||||
|
|
||||||
type
|
type
|
||||||
Purchasing* = ref object
|
Purchasing* = ref object
|
||||||
market: Market
|
market: Market
|
||||||
proofProbability*: UInt256
|
proofProbability*: UInt256
|
||||||
requestExpiryInterval*: UInt256
|
requestExpiryInterval*: UInt256
|
||||||
PurchaseRequest* = object
|
|
||||||
duration*: UInt256
|
|
||||||
size*: UInt256
|
|
||||||
contentHash*: array[32, byte]
|
|
||||||
maxPrice*: UInt256
|
|
||||||
proofProbability*: ?UInt256
|
|
||||||
expiry*: ?UInt256
|
|
||||||
Purchase* = ref object
|
Purchase* = ref object
|
||||||
|
|
||||||
const DefaultProofProbability = 100.u256
|
const DefaultProofProbability = 100.u256
|
||||||
|
@ -31,26 +25,14 @@ proc new*(_: type Purchasing, market: Market): Purchasing =
|
||||||
requestExpiryInterval: DefaultRequestExpiryInterval
|
requestExpiryInterval: DefaultRequestExpiryInterval
|
||||||
)
|
)
|
||||||
|
|
||||||
proc getProofProbability(purchasing: Purchasing, request: PurchaseRequest): UInt256 =
|
proc purchase*(purchasing: Purchasing, request: StorageRequest): Purchase =
|
||||||
request.proofProbability |? purchasing.proofProbability
|
var request = request
|
||||||
|
if request.proofProbability == 0.u256:
|
||||||
proc getExpiry(purchasing: Purchasing, request: PurchaseRequest): UInt256 =
|
request.proofProbability = purchasing.proofProbability
|
||||||
request.expiry |? (getTime().toUnix().u256 + purchasing.requestExpiryInterval)
|
if request.expiry == 0.u256:
|
||||||
|
request.expiry = (getTime().toUnix().u256 + purchasing.requestExpiryInterval)
|
||||||
proc getNonce(): array[32, byte] =
|
if request.nonce == array[32, byte].default:
|
||||||
doAssert randomBytes(result) == 32
|
doAssert randomBytes(request.nonce) == 32
|
||||||
|
|
||||||
proc purchase*(purchasing: Purchasing, request: PurchaseRequest): Purchase =
|
|
||||||
let request = StorageRequest(
|
|
||||||
client: Address.default, # TODO
|
|
||||||
duration: request.duration,
|
|
||||||
size: request.size,
|
|
||||||
contentHash: request.contentHash,
|
|
||||||
proofProbability: purchasing.getProofProbability(request),
|
|
||||||
maxPrice: request.maxPrice,
|
|
||||||
expiry: purchasing.getExpiry(request),
|
|
||||||
nonce: getNonce()
|
|
||||||
)
|
|
||||||
asyncSpawn purchasing.market.requestStorage(request)
|
asyncSpawn purchasing.market.requestStorage(request)
|
||||||
|
|
||||||
proc wait*(purchase: Purchase) {.async.} =
|
proc wait*(purchase: Purchase) {.async.} =
|
||||||
|
|
|
@ -6,7 +6,6 @@ import pkg/stint
|
||||||
import pkg/dagger/rng
|
import pkg/dagger/rng
|
||||||
import pkg/dagger/stores
|
import pkg/dagger/stores
|
||||||
import pkg/dagger/blocktype
|
import pkg/dagger/blocktype
|
||||||
import pkg/dagger/purchasing
|
|
||||||
|
|
||||||
proc example*(_: type EthAddress): EthAddress =
|
proc example*(_: type EthAddress): EthAddress =
|
||||||
EthPrivateKey.random().toPublicKey.toAddress
|
EthPrivateKey.random().toPublicKey.toAddress
|
||||||
|
@ -62,10 +61,3 @@ proc example*(_: type BlockExcPeerCtx): BlockExcPeerCtx =
|
||||||
|
|
||||||
proc example*(_: type Cid): Cid =
|
proc example*(_: type Cid): Cid =
|
||||||
Block.example.cid
|
Block.example.cid
|
||||||
|
|
||||||
proc example*(_: type PurchaseRequest): PurchaseRequest =
|
|
||||||
PurchaseRequest(
|
|
||||||
duration: uint16.example.u256,
|
|
||||||
size: uint32.example.u256,
|
|
||||||
contentHash: array[32, byte].example
|
|
||||||
)
|
|
||||||
|
|
|
@ -10,32 +10,36 @@ suite "Purchasing":
|
||||||
|
|
||||||
var purchasing: Purchasing
|
var purchasing: Purchasing
|
||||||
var market: MockMarket
|
var market: MockMarket
|
||||||
var purchaseRequest: PurchaseRequest
|
var request: StorageRequest
|
||||||
|
|
||||||
setup:
|
setup:
|
||||||
market = MockMarket.new()
|
market = MockMarket.new()
|
||||||
purchasing = Purchasing.new(market)
|
purchasing = Purchasing.new(market)
|
||||||
purchaseRequest = PurchaseRequest.example
|
request = StorageRequest(
|
||||||
|
duration: uint16.example.u256,
|
||||||
|
size: uint32.example.u256,
|
||||||
|
contentHash: array[32, byte].example
|
||||||
|
)
|
||||||
|
|
||||||
test "submits a storage request when asked":
|
test "submits a storage request when asked":
|
||||||
await purchasing.purchase(purchaseRequest).wait()
|
await purchasing.purchase(request).wait()
|
||||||
let storageRequest = market.requests[0]
|
let submitted = market.requests[0]
|
||||||
check storageRequest.duration == purchaseRequest.duration
|
check submitted.duration == request.duration
|
||||||
check storageRequest.size == purchaseRequest.size
|
check submitted.size == request.size
|
||||||
check storageRequest.contentHash == purchaseRequest.contentHash
|
check submitted.contentHash == request.contentHash
|
||||||
check storageRequest.maxPrice == purchaseRequest.maxPrice
|
check submitted.maxPrice == request.maxPrice
|
||||||
|
|
||||||
test "has a default value for proof probability":
|
test "has a default value for proof probability":
|
||||||
check purchasing.proofProbability != 0.u256
|
check purchasing.proofProbability != 0.u256
|
||||||
|
|
||||||
test "can change default value for proof probability":
|
test "can change default value for proof probability":
|
||||||
purchasing.proofProbability = 42.u256
|
purchasing.proofProbability = 42.u256
|
||||||
await purchasing.purchase(purchaseRequest).wait()
|
await purchasing.purchase(request).wait()
|
||||||
check market.requests[0].proofProbability == 42.u256
|
check market.requests[0].proofProbability == 42.u256
|
||||||
|
|
||||||
test "can override proof probability per request":
|
test "can override proof probability per request":
|
||||||
purchaseRequest.proofProbability = some 42.u256
|
request.proofProbability = 42.u256
|
||||||
await purchasing.purchase(purchaseRequest).wait()
|
await purchasing.purchase(request).wait()
|
||||||
check market.requests[0].proofProbability == 42.u256
|
check market.requests[0].proofProbability == 42.u256
|
||||||
|
|
||||||
test "has a default value for request expiration interval":
|
test "has a default value for request expiration interval":
|
||||||
|
@ -44,16 +48,16 @@ suite "Purchasing":
|
||||||
test "can change default value for request expiration interval":
|
test "can change default value for request expiration interval":
|
||||||
purchasing.requestExpiryInterval = 42.u256
|
purchasing.requestExpiryInterval = 42.u256
|
||||||
let start = getTime().toUnix()
|
let start = getTime().toUnix()
|
||||||
await purchasing.purchase(purchaseRequest).wait()
|
await purchasing.purchase(request).wait()
|
||||||
check market.requests[0].expiry == (start + 42).u256
|
check market.requests[0].expiry == (start + 42).u256
|
||||||
|
|
||||||
test "can override expiry time per request":
|
test "can override expiry time per request":
|
||||||
let expiry = (getTime().toUnix() + 42).u256
|
let expiry = (getTime().toUnix() + 42).u256
|
||||||
purchaseRequest.expiry = some expiry
|
request.expiry = expiry
|
||||||
await purchasing.purchase(purchaseRequest).wait()
|
await purchasing.purchase(request).wait()
|
||||||
check market.requests[0].expiry == expiry
|
check market.requests[0].expiry == expiry
|
||||||
|
|
||||||
test "includes a random nonce in every storage request":
|
test "includes a random nonce in every storage request":
|
||||||
await purchasing.purchase(purchaseRequest).wait()
|
await purchasing.purchase(request).wait()
|
||||||
await purchasing.purchase(purchaseRequest).wait()
|
await purchasing.purchase(request).wait()
|
||||||
check market.requests[0].nonce != market.requests[1].nonce
|
check market.requests[0].nonce != market.requests[1].nonce
|
||||||
|
|
Loading…
Reference in New Issue