[sales] Use UInt256 for size and duration

Use the same data types for availability and requests
for simplicity.
This commit is contained in:
Mark Spanbroek 2022-05-09 16:51:08 +02:00 committed by markspanbroek
parent ef80871984
commit d564cc2375
4 changed files with 24 additions and 17 deletions

View File

@ -4,12 +4,9 @@ import pkg/stew/byteutils
import pkg/questionable/results
import ../sales
func fromHex(T: type SomeInteger, s: string): T =
strutils.fromHex[T](s)
proc fromJson*(_: type Availability, bytes: seq[byte]): ?!Availability =
let json = ?catch parseJson(string.fromBytes(bytes))
let size = ?catch UInt256.fromHex(json["size"].getStr)
let duration = ?catch uint64.fromHex(json["duration"].getStr)
let minPrice = ?catch uint64.fromHex(json["minPrice"].getStr)
let duration = ?catch UInt256.fromHex(json["duration"].getStr)
let minPrice = ?catch UInt256.fromHex(json["minPrice"].getStr)
success Availability.init(size, duration, minPrice)

View File

@ -20,8 +20,8 @@ type
onSale: ?OnSale
Availability* = object
id*: array[32, byte]
size*: uint64
duration*: uint64
size*: UInt256
duration*: UInt256
minPrice*: UInt256
Negotiation = ref object
sales: Sales
@ -38,8 +38,8 @@ func new*(_: type Sales, market: Market): Sales =
Sales(market: market, offerExpiryInterval: DefaultOfferExpiryInterval)
proc init*(_: type Availability,
size: uint64,
duration: uint64,
size: UInt256,
duration: UInt256,
minPrice: UInt256): Availability =
var id: array[32, byte]
doAssert randomBytes(id) == 32
@ -56,8 +56,8 @@ func remove*(sales: Sales, availability: Availability) =
func findAvailability(sales: Sales, ask: StorageAsk): ?Availability =
for availability in sales.available:
if ask.size <= availability.size.u256 and
ask.duration <= availability.duration.u256 and
if ask.size <= availability.size and
ask.duration <= availability.duration and
ask.maxPrice >= availability.minPrice:
return some availability

View File

@ -54,4 +54,8 @@ proc example*(_: type Cid): Cid =
bt.Block.example.cid
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
)

View File

@ -7,10 +7,16 @@ import ./examples
suite "Sales":
let availability = Availability.init(size=100, duration=60, minPrice=42.u256)
let request = StorageRequest(
ask: StorageAsk(duration: 60.u256, size: 100.u256, maxPrice:42.u256)
let availability = Availability.init(
size=100.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 market: MockMarket
@ -41,8 +47,8 @@ suite "Sales":
check sales.available.len == 0
test "generates unique ids for storage availability":
let availability1 = Availability.init(size=1, duration=2, minPrice=3.u256)
let availability2 = Availability.init(size=1, duration=2, minPrice=3.u256)
let availability1 = Availability.init(1.u256, 2.u256, 3.u256)
let availability2 = Availability.init(1.u256, 2.u256, 3.u256)
check availability1.id != availability2.id
test "offers available storage when matching request comes in":