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 ../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

View File

@ -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,
@ -47,7 +50,7 @@ func `%`*(address: Address): JsonNode =
% $address
func `%`*(stint: StInt|StUint): JsonNode=
%("0x" & stint.toHex)
%(stint.toString)
func `%`*(arr: openArray[byte]): JsonNode =
%("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:
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":

View File

@ -1,4 +1,5 @@
import std/os
import std/options
import pkg/ethers
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
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")

View File

@ -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"

View File

@ -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":

2
vendor/nim-ethers vendored

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