-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve integration testing client (CodexClient) and json serializati…
…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
Showing
11 changed files
with
886 additions
and
185 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.