feat: bigint uses decimal over hex encoding (#452)

This commit is contained in:
Adam Uhlíř 2023-06-21 07:46:18 +02:00 committed by GitHub
parent 4cd8dd2e05
commit cfd2cf9302
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 50 additions and 40 deletions

View File

@ -19,6 +19,7 @@ import pkg/stint
import ../sales import ../sales
import ../purchasing import ../purchasing
import ../utils/stintutils
proc encodeString*(cid: type Cid): Result[string, cstring] = proc encodeString*(cid: type Cid): Result[string, cstring] =
ok($cid) ok($cid)
@ -72,7 +73,7 @@ proc encodeString*(value: bool): Result[string, cstring] =
proc decodeString*(_: type UInt256, value: string): Result[UInt256, cstring] = proc decodeString*(_: type UInt256, value: string): Result[UInt256, cstring] =
try: try:
ok UInt256.fromHex(value) ok UInt256.fromDecimal(value)
except ValueError as e: except ValueError as e:
err e.msg.cstring err e.msg.cstring

View File

@ -4,6 +4,9 @@ import pkg/stew/byteutils
import pkg/questionable/results import pkg/questionable/results
import ../sales import ../sales
import ../purchasing import ../purchasing
import ../utils/stintutils
export json
type type
StorageRequestParams* = object StorageRequestParams* = object
@ -17,22 +20,22 @@ type
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.fromDecimal(json["size"].getStr)
let duration = ?catch UInt256.fromHex(json["duration"].getStr) let duration = ?catch UInt256.fromDecimal(json["duration"].getStr)
let minPrice = ?catch UInt256.fromHex(json["minPrice"].getStr) let minPrice = ?catch UInt256.fromDecimal(json["minPrice"].getStr)
let maxCollateral = ?catch UInt256.fromHex(json["maxCollateral"].getStr) let maxCollateral = ?catch UInt256.fromDecimal(json["maxCollateral"].getStr)
success Availability.init(size, duration, minPrice, maxCollateral) success Availability.init(size, duration, minPrice, maxCollateral)
proc fromJson*(_: type StorageRequestParams, proc fromJson*(_: type StorageRequestParams,
bytes: seq[byte]): ?! StorageRequestParams = bytes: seq[byte]): ?! StorageRequestParams =
let json = ?catch parseJson(string.fromBytes(bytes)) let json = ?catch parseJson(string.fromBytes(bytes))
let duration = ?catch UInt256.fromHex(json["duration"].getStr) let duration = ?catch UInt256.fromDecimal(json["duration"].getStr)
let proofProbability = ?catch UInt256.fromHex(json["proofProbability"].getStr) let proofProbability = ?catch UInt256.fromDecimal(json["proofProbability"].getStr)
let reward = ?catch UInt256.fromHex(json["reward"].getStr) let reward = ?catch UInt256.fromDecimal(json["reward"].getStr)
let collateral = ?catch UInt256.fromHex(json["collateral"].getStr) let collateral = ?catch UInt256.fromDecimal(json["collateral"].getStr)
let expiry = UInt256.fromHex(json["expiry"].getStr).catch.option let expiry = UInt256.fromDecimal(json["expiry"].getStr).catch.option
let nodes = strutils.fromHex[uint](json["nodes"].getStr).catch.option let nodes = parseUInt(json["nodes"].getStr).catch.option
let tolerance = strutils.fromHex[uint](json["tolerance"].getStr).catch.option let tolerance = parseUInt(json["tolerance"].getStr).catch.option
success StorageRequestParams( success StorageRequestParams(
duration: duration, duration: duration,
proofProbability: proofProbability, proofProbability: proofProbability,
@ -46,8 +49,8 @@ proc fromJson*(_: type StorageRequestParams,
func `%`*(address: Address): JsonNode = func `%`*(address: Address): JsonNode =
% $address % $address
func `%`*(stint: StInt|StUint): JsonNode = func `%`*(stint: StInt|StUint): JsonNode=
%("0x" & stint.toHex) %(stint.toString)
func `%`*(arr: openArray[byte]): JsonNode = func `%`*(arr: openArray[byte]): JsonNode =
%("0x" & arr.toHex) %("0x" & arr.toHex)

View File

@ -0,0 +1,4 @@
import pkg/stint
func fromDecimal*(T: typedesc[StUint|StInt], s: string): T {.inline.} =
parse(s, type result, radix = 10)

View File

@ -40,11 +40,11 @@ components:
Duration: Duration:
type: string 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: ProofProbability:
type: string type: string
description: How often storage proofs are required as hexadecimal string description: How often storage proofs are required as decimal string
Expiry: Expiry:
type: string type: string
@ -106,15 +106,15 @@ components:
description: Hexadecimal identifier of the availability description: Hexadecimal identifier of the availability
size: size:
type: string type: string
description: Size of available storage in bytes as hexadecimal string description: Size of available storage in bytes as decimal string
duration: duration:
$ref: "#/components/schemas/Duration" $ref: "#/components/schemas/Duration"
minPrice: minPrice:
type: string 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: maxCollateral:
type: string 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: Slot:
type: object type: object
@ -164,7 +164,7 @@ components:
description: Number of slots (eq. hosts) that the Request want to have the content spread over description: Number of slots (eq. hosts) that the Request want to have the content spread over
slotSize: slotSize:
type: string 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: duration:
$ref: "#/components/schemas/Duration" $ref: "#/components/schemas/Duration"
proofProbability: proofProbability:
@ -406,7 +406,7 @@ paths:
$ref: "#/components/schemas/StorageRequestCreation" $ref: "#/components/schemas/StorageRequestCreation"
responses: responses:
"200": "200":
description: Returns the Request ID as hexadecimal string description: Returns the Request ID as decimal string
"400": "400":
description: Invalid or missing Request ID description: Invalid or missing Request ID
"404": "404":

View File

@ -1,4 +1,5 @@
import std/os import std/os
import std/options
import pkg/ethers import pkg/ethers
import pkg/codex/contracts/marketplace import pkg/codex/contracts/marketplace

View File

@ -4,10 +4,10 @@ proc currentTime*(provider: Provider): Future[UInt256] {.async.} =
return (!await provider.getBlock(BlockTag.latest)).timestamp return (!await provider.getBlock(BlockTag.latest)).timestamp
proc advanceTime*(provider: JsonRpcProvider, seconds: UInt256) {.async.} = 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") discard await provider.send("evm_mine")
proc advanceTimeTo*(provider: JsonRpcProvider, timestamp: UInt256) {.async.} = proc advanceTimeTo*(provider: JsonRpcProvider, timestamp: UInt256) {.async.} =
if (await provider.currentTime()) != timestamp: 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") discard await provider.send("evm_mine")

View File

@ -35,11 +35,11 @@ proc requestStorage*(client: CodexClient,
collateral: uint64): string = collateral: uint64): string =
let url = client.baseurl & "/storage/request/" & cid let url = client.baseurl & "/storage/request/" & cid
let json = %*{ let json = %*{
"duration": "0x" & duration.toHex, "duration": $duration,
"reward": "0x" & reward.toHex, "reward": $reward,
"proofProbability": "0x" & proofProbability.toHex, "proofProbability": $proofProbability,
"expiry": "0x" & expiry.toHex, "expiry": $expiry,
"collateral": "0x" & collateral.toHex, "collateral": $collateral,
} }
let response = client.http.post(url, $json) let response = client.http.post(url, $json)
assert response.status == "200 OK" assert response.status == "200 OK"
@ -59,10 +59,10 @@ proc postAvailability*(client: CodexClient,
size, duration, minPrice: uint64, maxCollateral: uint64): JsonNode = size, duration, minPrice: uint64, maxCollateral: uint64): JsonNode =
let url = client.baseurl & "/sales/availability" let url = client.baseurl & "/sales/availability"
let json = %*{ let json = %*{
"size": "0x" & size.toHex, "size": $size,
"duration": "0x" & duration.toHex, "duration": $duration,
"minPrice": "0x" & minPrice.toHex, "minPrice": $minPrice,
"maxCollateral": "0x" & maxCollateral.toHex "maxCollateral": $maxCollateral,
} }
let response = client.http.post(url, $json) let response = client.http.post(url, $json)
assert response.status == "200 OK" assert response.status == "200 OK"

View File

@ -2,7 +2,8 @@ import std/json
import pkg/chronos import pkg/chronos
import pkg/stint import pkg/stint
import pkg/ethers/erc20 import pkg/ethers/erc20
import codex/contracts import pkg/codex/contracts
import pkg/codex/utils/stintutils
import ../contracts/time import ../contracts/time
import ../contracts/deployment import ../contracts/deployment
import ../codex/helpers/eventually import ../codex/helpers/eventually
@ -52,9 +53,9 @@ twonodessuite "Integration tests", debug1 = false, debug2 = false:
let cid = client1.upload("some file contents") let cid = client1.upload("some file contents")
let id = client1.requestStorage(cid, duration=1, reward=2, proofProbability=3, expiry=expiry, collateral=200) let id = client1.requestStorage(cid, duration=1, reward=2, proofProbability=3, expiry=expiry, collateral=200)
let purchase = client1.getPurchase(id) let purchase = client1.getPurchase(id)
check purchase{"request"}{"ask"}{"duration"} == %"0x1" check purchase{"request"}{"ask"}{"duration"} == %"1"
check purchase{"request"}{"ask"}{"reward"} == %"0x2" check purchase{"request"}{"ask"}{"reward"} == %"2"
check purchase{"request"}{"ask"}{"proofProbability"} == %"0x3" check purchase{"request"}{"ask"}{"proofProbability"} == %"3"
test "node remembers purchase status after restart": test "node remembers purchase status after restart":
let expiry = (await provider.currentTime()) + 30 let expiry = (await provider.currentTime()) + 30
@ -66,8 +67,8 @@ twonodessuite "Integration tests", debug1 = false, debug2 = false:
client1.restart() client1.restart()
check eventually (not isNil client1.getPurchase(id){"request"}{"ask"}) check eventually (not isNil client1.getPurchase(id){"request"}{"ask"})
check client1.getPurchase(id){"request"}{"ask"}{"duration"} == %"0x1" check client1.getPurchase(id){"request"}{"ask"}{"duration"} == %"1"
check client1.getPurchase(id){"request"}{"ask"}{"reward"} == %"0x2" check client1.getPurchase(id){"request"}{"ask"}{"reward"} == %"2"
test "nodes negotiate contracts on the marketplace": test "nodes negotiate contracts on the marketplace":
let size: uint64 = 0xFFFFF let size: uint64 = 0xFFFFF
@ -83,7 +84,7 @@ twonodessuite "Integration tests", debug1 = false, debug2 = false:
check client1.getPurchase(purchase){"error"} == newJNull() check client1.getPurchase(purchase){"error"} == newJNull()
let availabilities = client2.getAvailabilities() let availabilities = client2.getAvailabilities()
check availabilities.len == 1 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 check newSize > 0 and newSize < size.u256
test "node slots gets paid out": test "node slots gets paid out":

2
vendor/nim-ethers vendored

@ -1 +1 @@
Subproject commit 18e225607cc6add166b93df6ac4229936a641318 Subproject commit 0321e6d7bd9c703c9e9bf31ee8664adac1d6cbe7