From cfd2cf9302435eb53f4141f48aae81d6407a2d53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Uhl=C3=AD=C5=99?= Date: Wed, 21 Jun 2023 07:46:18 +0200 Subject: [PATCH] feat: bigint uses decimal over hex encoding (#452) --- codex/rest/coders.nim | 3 ++- codex/rest/json.nim | 29 +++++++++++++++------------ codex/utils/stintutils.nim | 4 ++++ openapi.yaml | 14 ++++++------- tests/contracts/deployment.nim | 1 + tests/contracts/time.nim | 4 ++-- tests/integration/codexclient.nim | 18 ++++++++--------- tests/integration/testIntegration.nim | 15 +++++++------- vendor/nim-ethers | 2 +- 9 files changed, 50 insertions(+), 40 deletions(-) create mode 100644 codex/utils/stintutils.nim diff --git a/codex/rest/coders.nim b/codex/rest/coders.nim index 8a7c144d..66b41ee3 100644 --- a/codex/rest/coders.nim +++ b/codex/rest/coders.nim @@ -19,6 +19,7 @@ import pkg/stint import ../sales import ../purchasing +import ../utils/stintutils proc encodeString*(cid: type Cid): Result[string, cstring] = ok($cid) @@ -72,7 +73,7 @@ proc encodeString*(value: bool): Result[string, cstring] = proc decodeString*(_: type UInt256, value: string): Result[UInt256, cstring] = try: - ok UInt256.fromHex(value) + ok UInt256.fromDecimal(value) except ValueError as e: err e.msg.cstring diff --git a/codex/rest/json.nim b/codex/rest/json.nim index 28457af2..33e89a7c 100644 --- a/codex/rest/json.nim +++ b/codex/rest/json.nim @@ -4,6 +4,9 @@ import pkg/stew/byteutils import pkg/questionable/results import ../sales import ../purchasing +import ../utils/stintutils + +export json type StorageRequestParams* = object @@ -17,22 +20,22 @@ type 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 UInt256.fromHex(json["duration"].getStr) - let minPrice = ?catch UInt256.fromHex(json["minPrice"].getStr) - let maxCollateral = ?catch UInt256.fromHex(json["maxCollateral"].getStr) + 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.fromHex(json["duration"].getStr) - let proofProbability = ?catch UInt256.fromHex(json["proofProbability"].getStr) - let reward = ?catch UInt256.fromHex(json["reward"].getStr) - let collateral = ?catch UInt256.fromHex(json["collateral"].getStr) - let expiry = UInt256.fromHex(json["expiry"].getStr).catch.option - let nodes = strutils.fromHex[uint](json["nodes"].getStr).catch.option - let tolerance = strutils.fromHex[uint](json["tolerance"].getStr).catch.option + 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, @@ -46,8 +49,8 @@ proc fromJson*(_: type StorageRequestParams, func `%`*(address: Address): JsonNode = % $address -func `%`*(stint: StInt|StUint): JsonNode = - %("0x" & stint.toHex) +func `%`*(stint: StInt|StUint): JsonNode= + %(stint.toString) func `%`*(arr: openArray[byte]): JsonNode = %("0x" & arr.toHex) diff --git a/codex/utils/stintutils.nim b/codex/utils/stintutils.nim new file mode 100644 index 00000000..125ff8b6 --- /dev/null +++ b/codex/utils/stintutils.nim @@ -0,0 +1,4 @@ +import pkg/stint + +func fromDecimal*(T: typedesc[StUint|StInt], s: string): T {.inline.} = + parse(s, type result, radix = 10) diff --git a/openapi.yaml b/openapi.yaml index 085e532d..bbffee1b 100644 --- a/openapi.yaml +++ b/openapi.yaml @@ -40,11 +40,11 @@ components: Duration: type: string - description: The duration of the request in seconds as hexadecimal string + description: The duration of the request in seconds as decimal string ProofProbability: type: string - description: How often storage proofs are required as hexadecimal string + description: How often storage proofs are required as decimal string Expiry: type: string @@ -106,15 +106,15 @@ components: description: Hexadecimal identifier of the availability size: type: string - description: Size of available storage in bytes as hexadecimal string + description: Size of available storage in bytes as decimal string duration: $ref: "#/components/schemas/Duration" minPrice: type: string - description: Minimum price to be paid (in amount of tokens) as hexadecimal string + description: Minimum price to be paid (in amount of tokens) as decimal string maxCollateral: type: string - description: Maximum collateral user is willing to pay per filled Slot (in amount of tokens) + description: Maximum collateral user is willing to pay per filled Slot (in amount of tokens) as decimal string Slot: type: object @@ -164,7 +164,7 @@ components: description: Number of slots (eq. hosts) that the Request want to have the content spread over slotSize: type: string - description: Amount of storage per slot (in bytes) as hexadecimal string + description: Amount of storage per slot (in bytes) as decimal string duration: $ref: "#/components/schemas/Duration" proofProbability: @@ -406,7 +406,7 @@ paths: $ref: "#/components/schemas/StorageRequestCreation" responses: "200": - description: Returns the Request ID as hexadecimal string + description: Returns the Request ID as decimal string "400": description: Invalid or missing Request ID "404": diff --git a/tests/contracts/deployment.nim b/tests/contracts/deployment.nim index 7689a302..73e0d424 100644 --- a/tests/contracts/deployment.nim +++ b/tests/contracts/deployment.nim @@ -1,4 +1,5 @@ import std/os +import std/options import pkg/ethers import pkg/codex/contracts/marketplace diff --git a/tests/contracts/time.nim b/tests/contracts/time.nim index 418e3fd9..05eba0b3 100644 --- a/tests/contracts/time.nim +++ b/tests/contracts/time.nim @@ -4,10 +4,10 @@ proc currentTime*(provider: Provider): Future[UInt256] {.async.} = return (!await provider.getBlock(BlockTag.latest)).timestamp proc advanceTime*(provider: JsonRpcProvider, seconds: UInt256) {.async.} = - discard await provider.send("evm_increaseTime", @[%seconds]) + discard await provider.send("evm_increaseTime", @[%("0x" & seconds.toHex)]) discard await provider.send("evm_mine") proc advanceTimeTo*(provider: JsonRpcProvider, timestamp: UInt256) {.async.} = if (await provider.currentTime()) != timestamp: - discard await provider.send("evm_setNextBlockTimestamp", @[%timestamp]) + discard await provider.send("evm_setNextBlockTimestamp", @[%("0x" & timestamp.toHex)]) discard await provider.send("evm_mine") diff --git a/tests/integration/codexclient.nim b/tests/integration/codexclient.nim index 92117111..dc120de4 100644 --- a/tests/integration/codexclient.nim +++ b/tests/integration/codexclient.nim @@ -35,11 +35,11 @@ proc requestStorage*(client: CodexClient, collateral: uint64): string = let url = client.baseurl & "/storage/request/" & cid let json = %*{ - "duration": "0x" & duration.toHex, - "reward": "0x" & reward.toHex, - "proofProbability": "0x" & proofProbability.toHex, - "expiry": "0x" & expiry.toHex, - "collateral": "0x" & collateral.toHex, + "duration": $duration, + "reward": $reward, + "proofProbability": $proofProbability, + "expiry": $expiry, + "collateral": $collateral, } let response = client.http.post(url, $json) assert response.status == "200 OK" @@ -59,10 +59,10 @@ proc postAvailability*(client: CodexClient, size, duration, minPrice: uint64, maxCollateral: uint64): JsonNode = let url = client.baseurl & "/sales/availability" let json = %*{ - "size": "0x" & size.toHex, - "duration": "0x" & duration.toHex, - "minPrice": "0x" & minPrice.toHex, - "maxCollateral": "0x" & maxCollateral.toHex + "size": $size, + "duration": $duration, + "minPrice": $minPrice, + "maxCollateral": $maxCollateral, } let response = client.http.post(url, $json) assert response.status == "200 OK" diff --git a/tests/integration/testIntegration.nim b/tests/integration/testIntegration.nim index 796c95bf..24fc4c2d 100644 --- a/tests/integration/testIntegration.nim +++ b/tests/integration/testIntegration.nim @@ -2,7 +2,8 @@ import std/json import pkg/chronos import pkg/stint import pkg/ethers/erc20 -import codex/contracts +import pkg/codex/contracts +import pkg/codex/utils/stintutils import ../contracts/time import ../contracts/deployment import ../codex/helpers/eventually @@ -52,9 +53,9 @@ twonodessuite "Integration tests", debug1 = false, debug2 = false: let cid = client1.upload("some file contents") let id = client1.requestStorage(cid, duration=1, reward=2, proofProbability=3, expiry=expiry, collateral=200) let purchase = client1.getPurchase(id) - check purchase{"request"}{"ask"}{"duration"} == %"0x1" - check purchase{"request"}{"ask"}{"reward"} == %"0x2" - check purchase{"request"}{"ask"}{"proofProbability"} == %"0x3" + check purchase{"request"}{"ask"}{"duration"} == %"1" + check purchase{"request"}{"ask"}{"reward"} == %"2" + check purchase{"request"}{"ask"}{"proofProbability"} == %"3" test "node remembers purchase status after restart": let expiry = (await provider.currentTime()) + 30 @@ -66,8 +67,8 @@ twonodessuite "Integration tests", debug1 = false, debug2 = false: client1.restart() check eventually (not isNil client1.getPurchase(id){"request"}{"ask"}) - check client1.getPurchase(id){"request"}{"ask"}{"duration"} == %"0x1" - check client1.getPurchase(id){"request"}{"ask"}{"reward"} == %"0x2" + check client1.getPurchase(id){"request"}{"ask"}{"duration"} == %"1" + check client1.getPurchase(id){"request"}{"ask"}{"reward"} == %"2" test "nodes negotiate contracts on the marketplace": let size: uint64 = 0xFFFFF @@ -83,7 +84,7 @@ twonodessuite "Integration tests", debug1 = false, debug2 = false: check client1.getPurchase(purchase){"error"} == newJNull() let availabilities = client2.getAvailabilities() check availabilities.len == 1 - let newSize = UInt256.fromHex(availabilities[0]{"size"}.getStr) + let newSize = UInt256.fromDecimal(availabilities[0]{"size"}.getStr) check newSize > 0 and newSize < size.u256 test "node slots gets paid out": diff --git a/vendor/nim-ethers b/vendor/nim-ethers index 18e22560..0321e6d7 160000 --- a/vendor/nim-ethers +++ b/vendor/nim-ethers @@ -1 +1 @@ -Subproject commit 18e225607cc6add166b93df6ac4229936a641318 +Subproject commit 0321e6d7bd9c703c9e9bf31ee8664adac1d6cbe7