[purchasing] Use Clock instead of getTime()
This commit is contained in:
parent
4210cca6e7
commit
1e44ed5fd3
|
@ -34,7 +34,7 @@ proc new*(_: type ContractInteractions,
|
||||||
let proofs = OnChainProofs.new(contract)
|
let proofs = OnChainProofs.new(contract)
|
||||||
let clock = OnChainClock.new(signer.provider)
|
let clock = OnChainClock.new(signer.provider)
|
||||||
some ContractInteractions(
|
some ContractInteractions(
|
||||||
purchasing: Purchasing.new(market),
|
purchasing: Purchasing.new(market, clock),
|
||||||
sales: Sales.new(market),
|
sales: Sales.new(market),
|
||||||
proving: Proving.new(proofs, clock),
|
proving: Proving.new(proofs, clock),
|
||||||
clock: clock
|
clock: clock
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
import std/times
|
|
||||||
import std/tables
|
import std/tables
|
||||||
import pkg/stint
|
import pkg/stint
|
||||||
import pkg/chronos
|
import pkg/chronos
|
||||||
import pkg/questionable
|
import pkg/questionable
|
||||||
import pkg/nimcrypto
|
import pkg/nimcrypto
|
||||||
import ./market
|
import ./market
|
||||||
|
import ./clock
|
||||||
|
|
||||||
export questionable
|
export questionable
|
||||||
export market
|
export market
|
||||||
|
@ -12,6 +12,7 @@ export market
|
||||||
type
|
type
|
||||||
Purchasing* = ref object
|
Purchasing* = ref object
|
||||||
market: Market
|
market: Market
|
||||||
|
clock: Clock
|
||||||
purchases: Table[array[32, byte], Purchase]
|
purchases: Table[array[32, byte], Purchase]
|
||||||
proofProbability*: UInt256
|
proofProbability*: UInt256
|
||||||
requestExpiryInterval*: UInt256
|
requestExpiryInterval*: UInt256
|
||||||
|
@ -19,6 +20,7 @@ type
|
||||||
Purchase* = ref object
|
Purchase* = ref object
|
||||||
future: Future[void]
|
future: Future[void]
|
||||||
market: Market
|
market: Market
|
||||||
|
clock: Clock
|
||||||
offerExpiryMargin: UInt256
|
offerExpiryMargin: UInt256
|
||||||
request*: StorageRequest
|
request*: StorageRequest
|
||||||
offers*: seq[StorageOffer]
|
offers*: seq[StorageOffer]
|
||||||
|
@ -31,9 +33,10 @@ const DefaultOfferExpiryMargin = (8 * 60).u256
|
||||||
proc start(purchase: Purchase) {.gcsafe.}
|
proc start(purchase: Purchase) {.gcsafe.}
|
||||||
func id*(purchase: Purchase): array[32, byte]
|
func id*(purchase: Purchase): array[32, byte]
|
||||||
|
|
||||||
proc new*(_: type Purchasing, market: Market): Purchasing =
|
proc new*(_: type Purchasing, market: Market, clock: Clock): Purchasing =
|
||||||
Purchasing(
|
Purchasing(
|
||||||
market: market,
|
market: market,
|
||||||
|
clock: clock,
|
||||||
proofProbability: DefaultProofProbability,
|
proofProbability: DefaultProofProbability,
|
||||||
requestExpiryInterval: DefaultRequestExpiryInterval,
|
requestExpiryInterval: DefaultRequestExpiryInterval,
|
||||||
offerExpiryMargin: DefaultOfferExpiryMargin
|
offerExpiryMargin: DefaultOfferExpiryMargin
|
||||||
|
@ -44,7 +47,7 @@ proc populate*(purchasing: Purchasing, request: StorageRequest): StorageRequest
|
||||||
if result.ask.proofProbability == 0.u256:
|
if result.ask.proofProbability == 0.u256:
|
||||||
result.ask.proofProbability = purchasing.proofProbability
|
result.ask.proofProbability = purchasing.proofProbability
|
||||||
if result.expiry == 0.u256:
|
if result.expiry == 0.u256:
|
||||||
result.expiry = (getTime().toUnix().u256 + purchasing.requestExpiryInterval)
|
result.expiry = (purchasing.clock.now().u256 + purchasing.requestExpiryInterval)
|
||||||
if result.nonce == array[32, byte].default:
|
if result.nonce == array[32, byte].default:
|
||||||
doAssert randomBytes(result.nonce) == 32
|
doAssert randomBytes(result.nonce) == 32
|
||||||
|
|
||||||
|
@ -53,6 +56,7 @@ proc purchase*(purchasing: Purchasing, request: StorageRequest): Purchase =
|
||||||
let purchase = Purchase(
|
let purchase = Purchase(
|
||||||
request: request,
|
request: request,
|
||||||
market: purchasing.market,
|
market: purchasing.market,
|
||||||
|
clock: purchasing.clock,
|
||||||
offerExpiryMargin: purchasing.offerExpiryMargin
|
offerExpiryMargin: purchasing.offerExpiryMargin
|
||||||
)
|
)
|
||||||
purchase.start()
|
purchase.start()
|
||||||
|
@ -68,7 +72,7 @@ func getPurchase*(purchasing: Purchasing, id: array[32, byte]): ?Purchase =
|
||||||
proc selectOffer(purchase: Purchase) {.async.} =
|
proc selectOffer(purchase: Purchase) {.async.} =
|
||||||
var cheapest: ?StorageOffer
|
var cheapest: ?StorageOffer
|
||||||
for offer in purchase.offers:
|
for offer in purchase.offers:
|
||||||
without getTime().toUnix().u256 < offer.expiry - purchase.offerExpiryMargin:
|
without purchase.clock.now().u256 < offer.expiry - purchase.offerExpiryMargin:
|
||||||
continue
|
continue
|
||||||
without current =? cheapest:
|
without current =? cheapest:
|
||||||
cheapest = some offer
|
cheapest = some offer
|
||||||
|
|
|
@ -4,17 +4,20 @@ import pkg/chronos
|
||||||
import pkg/stint
|
import pkg/stint
|
||||||
import pkg/dagger/purchasing
|
import pkg/dagger/purchasing
|
||||||
import ./helpers/mockmarket
|
import ./helpers/mockmarket
|
||||||
|
import ./helpers/mockclock
|
||||||
import ./examples
|
import ./examples
|
||||||
|
|
||||||
suite "Purchasing":
|
suite "Purchasing":
|
||||||
|
|
||||||
var purchasing: Purchasing
|
var purchasing: Purchasing
|
||||||
var market: MockMarket
|
var market: MockMarket
|
||||||
|
var clock: MockClock
|
||||||
var request: StorageRequest
|
var request: StorageRequest
|
||||||
|
|
||||||
setup:
|
setup:
|
||||||
market = MockMarket.new()
|
market = MockMarket.new()
|
||||||
purchasing = Purchasing.new(market)
|
clock = MockClock.new()
|
||||||
|
purchasing = Purchasing.new(market, clock)
|
||||||
request = StorageRequest(
|
request = StorageRequest(
|
||||||
ask: StorageAsk(
|
ask: StorageAsk(
|
||||||
duration: uint16.example.u256,
|
duration: uint16.example.u256,
|
||||||
|
|
Loading…
Reference in New Issue