Use Cid object instead of buffer

This commit is contained in:
Arnaud 2025-01-31 08:22:08 +01:00 committed by Eric
parent 3a7fb23179
commit 742e4ecb60
No known key found for this signature in database
8 changed files with 32 additions and 16 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

@ -501,10 +501,7 @@ proc setupRequest(
collateralPerByte: collateralPerByte,
maxSlotLoss: tolerance,
),
content: StorageContent(
cid: manifestBlk.cid.data.buffer,
merkleRoot: verifyRoot,
),
content: StorageContent(cid: manifestBlk.cid, merkleRoot: verifyRoot),
expiry: expiry,
)
@ -567,7 +564,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)
@ -640,7 +637,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

@ -167,7 +167,7 @@ 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
test "Should delete a single block":

View File

@ -46,7 +46,9 @@ asyncchecksuite "Sales - start":
pricePerBytePerSecond: 1.u256,
collateralPerByte: 1.u256,
),
content: StorageContent(cid: "some cid".toBytes),
content: StorageContent(
cid: Cid.init("zb2rhheVmk3bLks5MgzTqyznLu1zqGH5jrfTA1eAZXrjx7Vob").tryGet
),
expiry: (getTime() + initDuration(hours = 1)).toUnix.u256,
)
@ -158,7 +160,9 @@ asyncchecksuite "Sales":
pricePerBytePerSecond: minPricePerBytePerSecond,
collateralPerByte: 1.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 pkg/lrucache
import ../ethertest
import ./examples

View File

@ -57,8 +57,8 @@ 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,
merkleRoot: array[32, byte].example
cid: Cid.init("zb2rhheVmk3bLks5MgzTqyznLu1zqGH5jrfTA1eAZXrjx7Vob").tryGet,
merkleRoot: array[32, byte].example,
),
expiry: (60 * 60).u256, # 1 hour ,
nonce: Nonce.example,