Skip to content

Commit

Permalink
Merge branch 'master' into chore/replace-roothash-by-treecid-for-mani…
Browse files Browse the repository at this point in the history
…fest
  • Loading branch information
gmega authored Oct 30, 2024
2 parents c033273 + 2b5a405 commit ead011a
Show file tree
Hide file tree
Showing 26 changed files with 415 additions and 109 deletions.
13 changes: 7 additions & 6 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,13 @@ jobs:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: '0'
fetch-depth: 0

- uses: actions/setup-node@v4
with:
node-version: 18

- name: Lint OpenAPI
shell: bash
run: npx @redocly/cli lint openapi.yaml

deploy:
Expand All @@ -46,20 +45,22 @@ jobs:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: '0'
fetch-depth: 0

- uses: actions/setup-node@v4
with:
node-version: 18

- name: Build OpenAPI
shell: bash
run: npx @redocly/cli build-docs openapi.yaml --output "openapi/index.html" --title "Codex API"
run: npx @redocly/cli build-docs openapi.yaml --output openapi/index.html --title "Codex API"

- name: Build Postman Collection
run: npx -y openapi-to-postmanv2 -s openapi.yaml -o openapi/postman.json -p -O folderStrategy=Tags,includeAuthInfoInExample=false

- name: Upload artifact
uses: actions/upload-pages-artifact@v3
with:
path: './openapi'
path: openapi

- name: Deploy to GitHub Pages
uses: actions/deploy-pages@v4
10 changes: 5 additions & 5 deletions codex/contracts/market.nim
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ proc approveFunds(market: OnChainMarket, amount: UInt256) {.async.} =
discard await token.increaseAllowance(market.contract.address(), amount).confirm(0)

method getZkeyHash*(market: OnChainMarket): Future[?string] {.async.} =
let config = await market.contract.config()
let config = await market.contract.configuration()
return some config.proofs.zkeyHash

method getSigner*(market: OnChainMarket): Future[Address] {.async.} =
Expand All @@ -65,18 +65,18 @@ method getSigner*(market: OnChainMarket): Future[Address] {.async.} =

method periodicity*(market: OnChainMarket): Future[Periodicity] {.async.} =
convertEthersError:
let config = await market.contract.config()
let config = await market.contract.configuration()
let period = config.proofs.period
return Periodicity(seconds: period)

method proofTimeout*(market: OnChainMarket): Future[UInt256] {.async.} =
convertEthersError:
let config = await market.contract.config()
let config = await market.contract.configuration()
return config.proofs.timeout

method proofDowntime*(market: OnChainMarket): Future[uint8] {.async.} =
convertEthersError:
let config = await market.contract.config()
let config = await market.contract.configuration()
return config.proofs.downtime

method getPointer*(market: OnChainMarket, slotId: SlotId): Future[uint8] {.async.} =
Expand Down Expand Up @@ -176,7 +176,7 @@ method fillSlot(market: OnChainMarket,

method freeSlot*(market: OnChainMarket, slotId: SlotId) {.async.} =
convertEthersError:
var freeSlot: Future[?TransactionResponse]
var freeSlot: Future[Confirmable]
if rewardRecipient =? market.rewardRecipient:
# If --reward-recipient specified, use it as the reward recipient, and use
# the SP's address as the collateral recipient
Expand Down
20 changes: 10 additions & 10 deletions codex/contracts/marketplace.nim
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@ export requests
type
Marketplace* = ref object of Contract

proc config*(marketplace: Marketplace): MarketplaceConfig {.contract, view.}
proc configuration*(marketplace: Marketplace): MarketplaceConfig {.contract, view.}
proc token*(marketplace: Marketplace): Address {.contract, view.}
proc slashMisses*(marketplace: Marketplace): UInt256 {.contract, view.}
proc slashPercentage*(marketplace: Marketplace): UInt256 {.contract, view.}
proc minCollateralThreshold*(marketplace: Marketplace): UInt256 {.contract, view.}

proc requestStorage*(marketplace: Marketplace, request: StorageRequest): ?TransactionResponse {.contract.}
proc fillSlot*(marketplace: Marketplace, requestId: RequestId, slotIndex: UInt256, proof: Groth16Proof): ?TransactionResponse {.contract.}
proc withdrawFunds*(marketplace: Marketplace, requestId: RequestId): ?TransactionResponse {.contract.}
proc withdrawFunds*(marketplace: Marketplace, requestId: RequestId, withdrawAddress: Address): ?TransactionResponse {.contract.}
proc freeSlot*(marketplace: Marketplace, id: SlotId): ?TransactionResponse {.contract.}
proc freeSlot*(marketplace: Marketplace, id: SlotId, rewardRecipient: Address, collateralRecipient: Address): ?TransactionResponse {.contract.}
proc requestStorage*(marketplace: Marketplace, request: StorageRequest): Confirmable {.contract.}
proc fillSlot*(marketplace: Marketplace, requestId: RequestId, slotIndex: UInt256, proof: Groth16Proof): Confirmable {.contract.}
proc withdrawFunds*(marketplace: Marketplace, requestId: RequestId): Confirmable {.contract.}
proc withdrawFunds*(marketplace: Marketplace, requestId: RequestId, withdrawAddress: Address): Confirmable {.contract.}
proc freeSlot*(marketplace: Marketplace, id: SlotId): Confirmable {.contract.}
proc freeSlot*(marketplace: Marketplace, id: SlotId, rewardRecipient: Address, collateralRecipient: Address): Confirmable {.contract.}
proc getRequest*(marketplace: Marketplace, id: RequestId): StorageRequest {.contract, view.}
proc getHost*(marketplace: Marketplace, id: SlotId): Address {.contract, view.}
proc getActiveSlot*(marketplace: Marketplace, id: SlotId): Slot {.contract, view.}
Expand All @@ -49,8 +49,8 @@ proc willProofBeRequired*(marketplace: Marketplace, id: SlotId): bool {.contract
proc getChallenge*(marketplace: Marketplace, id: SlotId): array[32, byte] {.contract, view.}
proc getPointer*(marketplace: Marketplace, id: SlotId): uint8 {.contract, view.}

proc submitProof*(marketplace: Marketplace, id: SlotId, proof: Groth16Proof): ?TransactionResponse {.contract.}
proc markProofAsMissing*(marketplace: Marketplace, id: SlotId, period: UInt256): ?TransactionResponse {.contract.}
proc submitProof*(marketplace: Marketplace, id: SlotId, proof: Groth16Proof): Confirmable {.contract.}
proc markProofAsMissing*(marketplace: Marketplace, id: SlotId, period: UInt256): Confirmable {.contract.}

proc reserveSlot*(marketplace: Marketplace, requestId: RequestId, slotIndex: UInt256): ?TransactionResponse {.contract.}
proc reserveSlot*(marketplace: Marketplace, requestId: RequestId, slotIndex: UInt256): Confirmable {.contract.}
proc canReserveSlot*(marketplace: Marketplace, requestId: RequestId, slotIndex: UInt256): bool {.contract, view.}
40 changes: 38 additions & 2 deletions codex/manifest/coders.nim
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
# This module implements serialization and deserialization of Manifest

import pkg/upraises
import times

push: {.upraises: [].}

Expand Down Expand Up @@ -59,6 +60,9 @@ proc encode*(manifest: Manifest): ?!seq[byte] =
# optional hcodec: MultiCodec = 5 # Multihash codec
# optional version: CidVersion = 6; # Cid version
# optional ErasureInfo erasure = 7; # erasure coding info
# optional filename: ?string = 8; # original filename
# optional mimetype: ?string = 9; # original mimetype
# optional uploadedAt: ?int64 = 10; # original uploadedAt
# }
# ```
#
Expand All @@ -70,6 +74,7 @@ proc encode*(manifest: Manifest): ?!seq[byte] =
header.write(4, manifest.codec.uint32)
header.write(5, manifest.hcodec.uint32)
header.write(6, manifest.version.uint32)

if manifest.protected:
var erasureInfo = initProtoBuffer()
erasureInfo.write(1, manifest.ecK.uint32)
Expand All @@ -90,6 +95,15 @@ proc encode*(manifest: Manifest): ?!seq[byte] =
erasureInfo.finish()
header.write(7, erasureInfo)

if manifest.filename.isSome:
header.write(8, manifest.filename.get())

if manifest.mimetype.isSome:
header.write(9, manifest.mimetype.get())

if manifest.uploadedAt.isSome:
header.write(10, manifest.uploadedAt.get().uint64)

pbNode.write(1, header) # set the treeCid as the data field
pbNode.finish()

Expand Down Expand Up @@ -118,6 +132,9 @@ proc decode*(_: type Manifest, data: openArray[byte]): ?!Manifest =
slotRoots: seq[seq[byte]]
cellSize: uint32
verifiableStrategy: uint32
filename: string
mimetype: string
uploadedAt: uint64

# Decode `Header` message
if pbNode.getField(1, pbHeader).isErr:
Expand Down Expand Up @@ -145,6 +162,15 @@ proc decode*(_: type Manifest, data: openArray[byte]): ?!Manifest =
if pbHeader.getField(7, pbErasureInfo).isErr:
return failure("Unable to decode `erasureInfo` from manifest!")

if pbHeader.getField(8, filename).isErr:
return failure("Unable to decode `filename` from manifest!")

if pbHeader.getField(9, mimetype).isErr:
return failure("Unable to decode `mimetype` from manifest!")

if pbHeader.getField(10, uploadedAt).isErr:
return failure("Unable to decode `uploadedAt` from manifest!")

let protected = pbErasureInfo.buffer.len > 0
var verifiable = false
if protected:
Expand Down Expand Up @@ -183,6 +209,10 @@ proc decode*(_: type Manifest, data: openArray[byte]): ?!Manifest =
let
treeCid = ? Cid.init(treeCidBuf).mapFailure

var filenameOption = if filename.len == 0: string.none else: filename.some
var mimetypeOption = if mimetype.len == 0: string.none else: mimetype.some
var uploadedAtOption = if uploadedAt == 0: int64.none else: uploadedAt.int64.some

let
self = if protected:
Manifest.new(
Expand All @@ -196,15 +226,21 @@ proc decode*(_: type Manifest, data: openArray[byte]): ?!Manifest =
ecM = ecM.int,
originalTreeCid = ? Cid.init(originalTreeCid).mapFailure,
originalDatasetSize = originalDatasetSize.NBytes,
strategy = StrategyType(protectedStrategy))
strategy = StrategyType(protectedStrategy),
filename = filenameOption,
mimetype = mimetypeOption,
uploadedAt = uploadedAtOption)
else:
Manifest.new(
treeCid = treeCid,
datasetSize = datasetSize.NBytes,
blockSize = blockSize.NBytes,
version = CidVersion(version),
hcodec = hcodec.MultiCodec,
codec = codec.MultiCodec)
codec = codec.MultiCodec,
filename = filenameOption,
mimetype = mimetypeOption,
uploadedAt = uploadedAtOption)

? self.verify()

Expand Down
89 changes: 69 additions & 20 deletions codex/manifest/manifest.nim
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ type
codec: MultiCodec # Dataset codec
hcodec: MultiCodec # Multihash codec
version: CidVersion # Cid version
filename {.serialize.}: ?string # The filename of the content uploaded (optional)
mimetype {.serialize.}: ?string # The mimetype of the content uploaded (optional)
uploadedAt {.serialize.}: ?int64 # The UTC creation timestamp in seconds
case protected {.serialize.}: bool # Protected datasets have erasure coded info
of true:
ecK: int # Number of blocks to encode
Expand Down Expand Up @@ -121,6 +124,14 @@ func verifiableStrategy*(self: Manifest): StrategyType =
func numSlotBlocks*(self: Manifest): int =
divUp(self.blocksCount, self.numSlots)

func filename*(self: Manifest): ?string =
self.filename

func mimetype*(self: Manifest): ?string =
self.mimetype

func uploadedAt*(self: Manifest): ?int64 =
self.uploadedAt
############################################################
# Operations on block list
############################################################
Expand Down Expand Up @@ -163,6 +174,9 @@ func `==`*(a, b: Manifest): bool =
(a.hcodec == b.hcodec) and
(a.codec == b.codec) and
(a.protected == b.protected) and
(a.filename == b.filename) and
(a.mimetype == b.mimetype) and
(a.uploadedAt == b.uploadedAt) and
(if a.protected:
(a.ecK == b.ecK) and
(a.ecM == b.ecM) and
Expand All @@ -181,26 +195,38 @@ func `==`*(a, b: Manifest): bool =
true)

func `$`*(self: Manifest): string =
"treeCid: " & $self.treeCid &
result = "treeCid: " & $self.treeCid &
", datasetSize: " & $self.datasetSize &
", blockSize: " & $self.blockSize &
", version: " & $self.version &
", hcodec: " & $self.hcodec &
", codec: " & $self.codec &
", protected: " & $self.protected &
(if self.protected:
", ecK: " & $self.ecK &
", ecM: " & $self.ecM &
", originalTreeCid: " & $self.originalTreeCid &
", originalDatasetSize: " & $self.originalDatasetSize &
", verifiable: " & $self.verifiable &
(if self.verifiable:
", verifyRoot: " & $self.verifyRoot &
", slotRoots: " & $self.slotRoots
else:
"")
", protected: " & $self.protected

if self.filename.isSome:
result &= ", filename: " & $self.filename

if self.mimetype.isSome:
result &= ", mimetype: " & $self.mimetype

if self.uploadedAt.isSome:
result &= ", uploadedAt: " & $self.uploadedAt

result &= (if self.protected:
", ecK: " & $self.ecK &
", ecM: " & $self.ecM &
", originalTreeCid: " & $self.originalTreeCid &
", originalDatasetSize: " & $self.originalDatasetSize &
", verifiable: " & $self.verifiable &
(if self.verifiable:
", verifyRoot: " & $self.verifyRoot &
", slotRoots: " & $self.slotRoots
else:
"")
else:
"")

return result

############################################################
# Constructors
Expand All @@ -214,7 +240,10 @@ func new*(
version: CidVersion = CIDv1,
hcodec = Sha256HashCodec,
codec = BlockCodec,
protected = false): Manifest =
protected = false,
filename: ?string = string.none,
mimetype: ?string = string.none,
uploadedAt: ?int64 = int64.none): Manifest =

T(
treeCid: treeCid,
Expand All @@ -223,7 +252,10 @@ func new*(
version: version,
codec: codec,
hcodec: hcodec,
protected: protected)
protected: protected,
filename: filename,
mimetype: mimetype,
uploadedAt: uploadedAt)

func new*(
T: type Manifest,
Expand All @@ -247,7 +279,11 @@ func new*(
ecK: ecK, ecM: ecM,
originalTreeCid: manifest.treeCid,
originalDatasetSize: manifest.datasetSize,
protectedStrategy: strategy)
protectedStrategy: strategy,
filename: manifest.filename,
mimetype: manifest.mimetype,
uploadedAt: manifest.uploadedAt
)

func new*(
T: type Manifest,
Expand All @@ -263,7 +299,10 @@ func new*(
codec: manifest.codec,
hcodec: manifest.hcodec,
blockSize: manifest.blockSize,
protected: false)
protected: false,
filename: manifest.filename,
mimetype: manifest.mimetype,
uploadedAt: manifest.uploadedAt)

func new*(
T: type Manifest,
Expand All @@ -277,7 +316,10 @@ func new*(
ecM: int,
originalTreeCid: Cid,
originalDatasetSize: NBytes,
strategy = SteppedStrategy): Manifest =
strategy = SteppedStrategy,
filename: ?string = string.none,
mimetype: ?string = string.none,
uploadedAt: ?int64 = int64.none): Manifest =

Manifest(
treeCid: treeCid,
Expand All @@ -291,7 +333,10 @@ func new*(
ecM: ecM,
originalTreeCid: originalTreeCid,
originalDatasetSize: originalDatasetSize,
protectedStrategy: strategy)
protectedStrategy: strategy,
filename: filename,
mimetype: mimetype,
uploadedAt: uploadedAt)

func new*(
T: type Manifest,
Expand Down Expand Up @@ -329,7 +374,11 @@ func new*(
verifyRoot: verifyRoot,
slotRoots: @slotRoots,
cellSize: cellSize,
verifiableStrategy: strategy)
verifiableStrategy: strategy,
filename: manifest.filename,
mimetype: manifest.mimetype,
uploadedAt: manifest.uploadedAt
)

func new*(
T: type Manifest,
Expand Down
Loading

0 comments on commit ead011a

Please sign in to comment.