Skip to content

Commit

Permalink
Improve integration testing client (CodexClient) and json serializati…
Browse files Browse the repository at this point in the history
…on (#514)

* Improve integration testing client (CodexClient) and json serialization

The current client used for integration testing against the REST endpoints for Codex accepts and passes primitive types. This caused a hard to diagnose bug where a `uint` was not being deserialized correctly.

In addition, the json de/serializing done between the CodexClient and REST client was not easy to read and was not tested.

These changes bring non-primitive types to most of the CodexClient functions, allowing us to lean on the compiler to ensure we're providing correct typings. More importantly, a json de/serialization util was created as a drop-in replacement for the std/json lib, with the main two differences being that field serialization is opt-in (instead of opt-out as in the case of json_serialization) and serialization errors are captured and logged, making debugging serialization issues much easier.

* Update integration test to use nodes=2 and tolerance=1

* clean up
  • Loading branch information
emizzle authored Sep 1, 2023
1 parent fc3eac9 commit 37b3d99
Show file tree
Hide file tree
Showing 11 changed files with 886 additions and 185 deletions.
34 changes: 20 additions & 14 deletions codex/contracts/requests.nim
Original file line number Diff line number Diff line change
@@ -1,31 +1,33 @@
import std/hashes
import std/typetraits
import pkg/contractabi
import pkg/nimcrypto
import pkg/ethers/fields
import pkg/questionable/results
import pkg/stew/byteutils
import pkg/json_serialization
import pkg/upraises
import ../utils/json

export contractabi

type
StorageRequest* = object
client*: Address
ask*: StorageAsk
content*: StorageContent
expiry*: UInt256
client* {.serialize.}: Address
ask* {.serialize.}: StorageAsk
content* {.serialize.}: StorageContent
expiry* {.serialize.}: UInt256
nonce*: Nonce
StorageAsk* = object
slots*: uint64
slotSize*: UInt256
duration*: UInt256
proofProbability*: UInt256
reward*: UInt256
collateral*: UInt256
maxSlotLoss*: uint64
slots* {.serialize.}: uint64
slotSize* {.serialize.}: UInt256
duration* {.serialize.}: UInt256
proofProbability* {.serialize.}: UInt256
reward* {.serialize.}: UInt256
collateral* {.serialize.}: UInt256
maxSlotLoss* {.serialize.}: uint64
StorageContent* = object
cid*: string
cid* {.serialize.}: string
erasure*: StorageErasure
por*: StoragePoR
StorageErasure* = object
Expand All @@ -35,8 +37,8 @@ type
publicKey*: seq[byte]
name*: seq[byte]
Slot* = object
request*: StorageRequest
slotIndex*: UInt256
request* {.serialize.}: StorageRequest
slotIndex* {.serialize.}: UInt256
SlotId* = distinct array[32, byte]
RequestId* = distinct array[32, byte]
Nonce* = distinct array[32, byte]
Expand Down Expand Up @@ -75,6 +77,10 @@ proc fromHex*(T: type SlotId, hex: string): T =
proc fromHex*(T: type Nonce, hex: string): T =
T array[32, byte].fromHex(hex)

proc fromHex*[T: distinct](_: type T, hex: string): T =
type baseType = T.distinctBase
T baseType.fromHex(hex)

func fromTuple(_: type StorageRequest, tupl: tuple): StorageRequest =
StorageRequest(
client: tupl[0],
Expand Down
2 changes: 1 addition & 1 deletion codex/node.nim
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ proc requestStorage*(
## - Run the PoR setup on the erasure dataset
## - Call into the marketplace and purchasing contracts
##
trace "Received a request for storage!", cid, duration, nodes, tolerance, reward
trace "Received a request for storage!", cid, duration, nodes, tolerance, reward, proofProbability, collateral, expiry

without contracts =? self.contracts.client:
trace "Purchasing not available"
Expand Down
16 changes: 13 additions & 3 deletions codex/rest/api.nim
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import pkg/codexdht/discv5/node as dn
import ../node
import ../blocktype
import ../conf
import ../contracts
import ../contracts except `%*`, `%` # imported from contracts/marketplace (exporting ethers)
import ../streams

import ./coders
Expand Down Expand Up @@ -361,10 +361,15 @@ proc initRestApi*(node: CodexNodeRef, conf: CodexConf): RestRouter =

let body = await request.getBody()

without availability =? Availability.fromJson(body), error:
without restAv =? RestAvailability.fromJson(body), error:
return RestApiResponse.error(Http400, error.msg)

let reservations = contracts.sales.context.reservations
# assign id to availability via init
let availability = Availability.init(restAv.size,
restAv.duration,
restAv.minPrice,
restAv.maxCollateral)

if not reservations.hasAvailable(availability.size.truncate(uint)):
return RestApiResponse.error(Http422, "Not enough storage quota")
Expand All @@ -389,7 +394,12 @@ proc initRestApi*(node: CodexNodeRef, conf: CodexConf): RestRouter =
without purchase =? contracts.purchasing.getPurchase(id):
return RestApiResponse.error(Http404)

let json = %purchase
let json = % RestPurchase(
state: purchase.state |? "none",
error: purchase.error.?msg,
request: purchase.request,
requestId: purchase.requestId
)

return RestApiResponse.response($json, contentType="application/json")

Expand Down
86 changes: 22 additions & 64 deletions codex/rest/json.nim
Original file line number Diff line number Diff line change
@@ -1,79 +1,37 @@
import std/json
import std/strutils
import pkg/stew/byteutils
import pkg/questionable
import pkg/questionable/results
import pkg/stew/byteutils
import ../sales
import ../purchasing
import ../utils/stintutils
import ../utils/json

export json

type
StorageRequestParams* = object
duration*: UInt256
proofProbability*: UInt256
reward*: UInt256
collateral*: UInt256
expiry*: ?UInt256
nodes*: ?uint
tolerance*: ?uint

proc fromJson*(
_: type Availability,
bytes: seq[byte]
): ?!Availability =
let json = ?catch parseJson(string.fromBytes(bytes))
let size = ?catch UInt256.fromDecimal(json["size"].getStr)
let duration = ?catch UInt256.fromDecimal(json["duration"].getStr)
let minPrice = ?catch UInt256.fromDecimal(json["minPrice"].getStr)
let maxCollateral = ?catch UInt256.fromDecimal(json["maxCollateral"].getStr)
success Availability.init(size, duration, minPrice, maxCollateral)

proc fromJson*(
_: type StorageRequestParams,
bytes: seq[byte]
): ?! StorageRequestParams =
let json = ?catch parseJson(string.fromBytes(bytes))
let duration = ?catch UInt256.fromDecimal(json["duration"].getStr)
let proofProbability = ?catch UInt256.fromDecimal(json["proofProbability"].getStr)
let reward = ?catch UInt256.fromDecimal(json["reward"].getStr)
let collateral = ?catch UInt256.fromDecimal(json["collateral"].getStr)
let expiry = UInt256.fromDecimal(json["expiry"].getStr).catch.option
let nodes = parseUInt(json["nodes"].getStr).catch.option
let tolerance = parseUInt(json["tolerance"].getStr).catch.option
success StorageRequestParams(
duration: duration,
proofProbability: proofProbability,
reward: reward,
collateral: collateral,
expiry: expiry,
nodes: nodes,
tolerance: tolerance
)

func `%`*(address: Address): JsonNode =
% $address

func `%`*(stint: StInt|StUint): JsonNode=
%(stint.toString)

func `%`*(arr: openArray[byte]): JsonNode =
%("0x" & arr.toHex)

func `%`*(id: RequestId | SlotId | Nonce | AvailabilityId): JsonNode =
% id.toArray
duration* {.serialize.}: UInt256
proofProbability* {.serialize.}: UInt256
reward* {.serialize.}: UInt256
collateral* {.serialize.}: UInt256
expiry* {.serialize.}: ?UInt256
nodes* {.serialize.}: ?uint
tolerance* {.serialize.}: ?uint

RestPurchase* = object
requestId* {.serialize.}: RequestId
request* {.serialize.}: ?StorageRequest
state* {.serialize.}: string
error* {.serialize.}: ?string

RestAvailability* = object
size* {.serialize.}: UInt256
duration* {.serialize.}: UInt256
minPrice* {.serialize.}: UInt256
maxCollateral* {.serialize.}: UInt256

func `%`*(obj: StorageRequest | Slot): JsonNode =
let jsonObj = newJObject()
for k, v in obj.fieldPairs: jsonObj[k] = %v
jsonObj["id"] = %(obj.id)

return jsonObj

func `%`*(purchase: Purchase): JsonNode =
%*{
"state": purchase.state |? "none",
"error": purchase.error.?msg,
"request": purchase.request,
"requestId": purchase.requestId
}
11 changes: 6 additions & 5 deletions codex/sales/reservations.nim
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import pkg/stew/byteutils
import pkg/nimcrypto
import pkg/questionable
import pkg/questionable/results
import ../utils/json

push: {.upraises: [].}

Expand All @@ -34,11 +35,11 @@ logScope:
type
AvailabilityId* = distinct array[32, byte]
Availability* = object
id*: AvailabilityId
size*: UInt256
duration*: UInt256
minPrice*: UInt256
maxCollateral*: UInt256
id* {.serialize.}: AvailabilityId
size* {.serialize.}: UInt256
duration* {.serialize.}: UInt256
minPrice* {.serialize.}: UInt256
maxCollateral* {.serialize.}: UInt256
used*: bool
Reservations* = ref object
repo: RepoStore
Expand Down
Loading

0 comments on commit 37b3d99

Please sign in to comment.