Use Cid object instead of buffer

This commit is contained in:
Arnaud 2025-01-31 08:22:08 +01:00
parent 4c414204b7
commit 9a4fc315db
No known key found for this signature in database
GPG Key ID: 69D6CE281FCAE663
8 changed files with 31 additions and 13 deletions

View File

@ -6,8 +6,10 @@ import pkg/nimcrypto
import pkg/ethers/fields
import pkg/questionable/results
import pkg/stew/byteutils
import pkg/libp2p/[cid, multicodec]
import ../logutils
import ../utils/json
from ../errors import mapFailure
export contractabi
@ -29,7 +31,7 @@ type
maxSlotLoss* {.serialize.}: uint64
StorageContent* = object
cid* {.serialize.}: seq[byte]
cid* {.serialize.}: Cid
merkleRoot*: array[32, byte]
Slot* = object
@ -120,6 +122,9 @@ func fromTuple(_: type StorageAsk, tupl: tuple): StorageAsk =
func fromTuple(_: type StorageContent, tupl: tuple): StorageContent =
StorageContent(cid: tupl[0], merkleRoot: tupl[1])
func solidityType*(_: type Cid): string =
solidityType(seq[byte])
func solidityType*(_: type StorageContent): string =
solidityType(StorageContent.fieldTypes)
@ -129,6 +134,10 @@ func solidityType*(_: type StorageAsk): string =
func solidityType*(_: type StorageRequest): string =
solidityType(StorageRequest.fieldTypes)
# Note: it seems to be ok to ignore the vbuffer offset for now
func encode*(encoder: var AbiEncoder, cid: Cid) =
encoder.write(cid.data.buffer)
func encode*(encoder: var AbiEncoder, content: StorageContent) =
encoder.write(content.fieldValues)
@ -141,8 +150,12 @@ func encode*(encoder: var AbiEncoder, id: RequestId | SlotId | Nonce) =
func encode*(encoder: var AbiEncoder, request: StorageRequest) =
encoder.write(request.fieldValues)
func encode*(encoder: var AbiEncoder, request: Slot) =
encoder.write(request.fieldValues)
func encode*(encoder: var AbiEncoder, slot: Slot) =
encoder.write(slot.fieldValues)
func decode*(decoder: var AbiDecoder, T: type Cid): ?!T =
let data = ?decoder.read(seq[byte])
Cid.init(data).mapFailure
func decode*(decoder: var AbiDecoder, T: type StorageContent): ?!T =
let tupl = ?decoder.read(StorageContent.fieldTypes)

View File

@ -439,7 +439,7 @@ proc setupRequest(
collateral: collateral,
maxSlotLoss: tolerance,
),
content: StorageContent(cid: manifestBlk.cid.data.buffer, merkleRoot: verifyRoot),
content: StorageContent(cid: manifestBlk.cid, merkleRoot: verifyRoot),
expiry: expiry,
)
@ -501,7 +501,7 @@ proc onStore(
trace "Received a request to store a slot"
without cid =? Cid.init(request.content.cid).mapFailure, err:
without cid =? Cid.init(request.content.cid.data.buffer).mapFailure, err:
trace "Unable to parse Cid", cid
return failure(err)
@ -574,7 +574,7 @@ proc onProve(
##
let
cidStr = slot.request.content.cid
cidStr = $slot.request.content.cid.data.buffer
slotIdx = slot.slotIndex.truncate(Natural)
logScope:

View File

@ -53,7 +53,8 @@ method run*(state: SaleFilled, machine: Machine): Future[?State] {.async.} =
raiseAssert "onExpiryUpdate callback not set"
let requestEnd = await market.getRequestEnd(data.requestId)
if err =? (await onExpiryUpdate(request.content.cid, requestEnd)).errorOption:
if err =?
(await onExpiryUpdate(request.content.cid.data.buffer, requestEnd)).errorOption:
return some State(SaleErrored(error: err))
when codex_enable_proof_failures:

View File

@ -115,7 +115,7 @@ asyncchecksuite "Test Node - Host contracts":
test "onStore callback":
let onStore = !sales.onStore
var request = StorageRequest.example
request.content.cid = verifiableBlock.cid.data.buffer
request.content.cid = verifiableBlock.cid
request.expiry = (getTime() + DefaultBlockTtl.toTimesDuration + 1.hours).toUnix.u256
var fetchedBytes: uint = 0

View File

@ -164,5 +164,5 @@ asyncchecksuite "Test Node - Basic":
check:
(await verifiableBlock.cid in localStore) == true
request.content.cid == verifiableBlock.cid.data.buffer
request.content.cid == verifiableBlock.cid
request.content.merkleRoot == builder.verifyRoot.get.toBytes

View File

@ -46,7 +46,9 @@ asyncchecksuite "Sales - start":
reward: 10.u256,
collateral: 200.u256,
),
content: StorageContent(cid: "some cid".toBytes),
content: StorageContent(
cid: Cid.init("zb2rhheVmk3bLks5MgzTqyznLu1zqGH5jrfTA1eAZXrjx7Vob").tryGet
),
expiry: (getTime() + initDuration(hours = 1)).toUnix.u256,
)
@ -150,7 +152,9 @@ asyncchecksuite "Sales":
reward: 10.u256,
collateral: 200.u256,
),
content: StorageContent(cid: "some cid".toBytes),
content: StorageContent(
cid: Cid.init("zb2rhheVmk3bLks5MgzTqyznLu1zqGH5jrfTA1eAZXrjx7Vob").tryGet
),
expiry: (getTime() + initDuration(hours = 1)).toUnix.u256,
)

View File

@ -3,6 +3,7 @@ import std/importutils
import pkg/chronos
import pkg/ethers/erc20
import codex/contracts
import pkg/libp2p/cid
import ../ethertest
import ./examples
import ./time

View File

@ -57,8 +57,7 @@ proc example*(_: type StorageRequest): StorageRequest =
maxSlotLoss: 2, # 2 slots can be freed without data considered to be lost
),
content: StorageContent(
cid:
Cid.init("zb2rhheVmk3bLks5MgzTqyznLu1zqGH5jrfTA1eAZXrjx7Vob").tryGet.data.buffer,
cid: Cid.init("zb2rhheVmk3bLks5MgzTqyznLu1zqGH5jrfTA1eAZXrjx7Vob").tryGet,
merkleRoot: array[32, byte].example,
),
expiry: (60 * 60).u256, # 1 hour ,