[sales] Use UInt256 for size and duration
Use the same data types for availability and requests for simplicity.
This commit is contained in:
parent
ef80871984
commit
d564cc2375
|
@ -4,12 +4,9 @@ import pkg/stew/byteutils
|
||||||
import pkg/questionable/results
|
import pkg/questionable/results
|
||||||
import ../sales
|
import ../sales
|
||||||
|
|
||||||
func fromHex(T: type SomeInteger, s: string): T =
|
|
||||||
strutils.fromHex[T](s)
|
|
||||||
|
|
||||||
proc fromJson*(_: type Availability, bytes: seq[byte]): ?!Availability =
|
proc fromJson*(_: type Availability, bytes: seq[byte]): ?!Availability =
|
||||||
let json = ?catch parseJson(string.fromBytes(bytes))
|
let json = ?catch parseJson(string.fromBytes(bytes))
|
||||||
let size = ?catch UInt256.fromHex(json["size"].getStr)
|
let size = ?catch UInt256.fromHex(json["size"].getStr)
|
||||||
let duration = ?catch uint64.fromHex(json["duration"].getStr)
|
let duration = ?catch UInt256.fromHex(json["duration"].getStr)
|
||||||
let minPrice = ?catch uint64.fromHex(json["minPrice"].getStr)
|
let minPrice = ?catch UInt256.fromHex(json["minPrice"].getStr)
|
||||||
success Availability.init(size, duration, minPrice)
|
success Availability.init(size, duration, minPrice)
|
||||||
|
|
|
@ -20,8 +20,8 @@ type
|
||||||
onSale: ?OnSale
|
onSale: ?OnSale
|
||||||
Availability* = object
|
Availability* = object
|
||||||
id*: array[32, byte]
|
id*: array[32, byte]
|
||||||
size*: uint64
|
size*: UInt256
|
||||||
duration*: uint64
|
duration*: UInt256
|
||||||
minPrice*: UInt256
|
minPrice*: UInt256
|
||||||
Negotiation = ref object
|
Negotiation = ref object
|
||||||
sales: Sales
|
sales: Sales
|
||||||
|
@ -38,8 +38,8 @@ func new*(_: type Sales, market: Market): Sales =
|
||||||
Sales(market: market, offerExpiryInterval: DefaultOfferExpiryInterval)
|
Sales(market: market, offerExpiryInterval: DefaultOfferExpiryInterval)
|
||||||
|
|
||||||
proc init*(_: type Availability,
|
proc init*(_: type Availability,
|
||||||
size: uint64,
|
size: UInt256,
|
||||||
duration: uint64,
|
duration: UInt256,
|
||||||
minPrice: UInt256): Availability =
|
minPrice: UInt256): Availability =
|
||||||
var id: array[32, byte]
|
var id: array[32, byte]
|
||||||
doAssert randomBytes(id) == 32
|
doAssert randomBytes(id) == 32
|
||||||
|
@ -56,8 +56,8 @@ func remove*(sales: Sales, availability: Availability) =
|
||||||
|
|
||||||
func findAvailability(sales: Sales, ask: StorageAsk): ?Availability =
|
func findAvailability(sales: Sales, ask: StorageAsk): ?Availability =
|
||||||
for availability in sales.available:
|
for availability in sales.available:
|
||||||
if ask.size <= availability.size.u256 and
|
if ask.size <= availability.size and
|
||||||
ask.duration <= availability.duration.u256 and
|
ask.duration <= availability.duration and
|
||||||
ask.maxPrice >= availability.minPrice:
|
ask.maxPrice >= availability.minPrice:
|
||||||
return some availability
|
return some availability
|
||||||
|
|
||||||
|
|
|
@ -54,4 +54,8 @@ proc example*(_: type Cid): Cid =
|
||||||
bt.Block.example.cid
|
bt.Block.example.cid
|
||||||
|
|
||||||
proc example*(_: type Availability): Availability =
|
proc example*(_: type Availability): Availability =
|
||||||
Availability.init(uint16.example, uint16.example, uint64.example.u256)
|
Availability.init(
|
||||||
|
size = uint16.example.u256,
|
||||||
|
duration = uint16.example.u256,
|
||||||
|
minPrice = uint64.example.u256
|
||||||
|
)
|
||||||
|
|
|
@ -7,10 +7,16 @@ import ./examples
|
||||||
|
|
||||||
suite "Sales":
|
suite "Sales":
|
||||||
|
|
||||||
let availability = Availability.init(size=100, duration=60, minPrice=42.u256)
|
let availability = Availability.init(
|
||||||
let request = StorageRequest(
|
size=100.u256,
|
||||||
ask: StorageAsk(duration: 60.u256, size: 100.u256, maxPrice:42.u256)
|
duration=60.u256,
|
||||||
|
minPrice=42.u256
|
||||||
)
|
)
|
||||||
|
let request = StorageRequest(ask: StorageAsk(
|
||||||
|
duration: 60.u256,
|
||||||
|
size: 100.u256,
|
||||||
|
maxPrice:42.u256
|
||||||
|
))
|
||||||
|
|
||||||
var sales: Sales
|
var sales: Sales
|
||||||
var market: MockMarket
|
var market: MockMarket
|
||||||
|
@ -41,8 +47,8 @@ suite "Sales":
|
||||||
check sales.available.len == 0
|
check sales.available.len == 0
|
||||||
|
|
||||||
test "generates unique ids for storage availability":
|
test "generates unique ids for storage availability":
|
||||||
let availability1 = Availability.init(size=1, duration=2, minPrice=3.u256)
|
let availability1 = Availability.init(1.u256, 2.u256, 3.u256)
|
||||||
let availability2 = Availability.init(size=1, duration=2, minPrice=3.u256)
|
let availability2 = Availability.init(1.u256, 2.u256, 3.u256)
|
||||||
check availability1.id != availability2.id
|
check availability1.id != availability2.id
|
||||||
|
|
||||||
test "offers available storage when matching request comes in":
|
test "offers available storage when matching request comes in":
|
||||||
|
|
Loading…
Reference in New Issue