Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
d7d51f8
adding nim-groth16 dep
dryajov Apr 1, 2025
1b7d373
adding threading dep
dryajov Apr 1, 2025
2d3114e
wip nim groth16
dryajov Apr 1, 2025
4c5d9e3
adding deps
dryajov Apr 1, 2025
2e92e3a
adding goldilocks deps
dryajov Apr 2, 2025
86e76c9
adding circom-witnessgen
dryajov Apr 9, 2025
0988568
Remove vendor/threading submodule
dryajov May 23, 2025
9070d87
Refactor Circom compatibility backend and add NimGroth16 backend impl…
dryajov May 29, 2025
83b25cf
Refactor backend structure: remove unused files, add NimGroth16 suppo…
dryajov May 29, 2025
d408ebd
Refactor SlotsBuilder and DataSampler types to use generic parameters…
dryajov May 29, 2025
d5e35e7
Refactor codebase: enhance error handling, improve type definitions, …
dryajov May 29, 2025
7e4f984
Add ProverBackendCmd enum and update configuration for nimGroth16 and…
dryajov May 29, 2025
40368b7
Fix import statement in testslots.nim: replace testbackendfactory wit…
dryajov May 29, 2025
76e92cb
Fix typo in testproverfactory.nim: correct getZKeyFile to getZkeyFile
dryajov May 29, 2025
9b02432
Avoid using pointer to task, use the actual task object. Add some che…
dryajov May 29, 2025
1e12b4f
Update subproject commits for circom-witnessgen and nim-groth16
dryajov May 29, 2025
3f33fb4
restore tests
dryajov May 29, 2025
addac17
refactor: rename proverBackendCmd to proverBackend and standardize en…
dryajov May 30, 2025
11e2f24
feat: improve prover logging and add NimGroth16 prover tests
dryajov May 30, 2025
872fb2c
refactor: replace circom-wasm with circom-graph and update G1/G2 poin…
dryajov May 30, 2025
eb6a4b2
feat: add single-block slot proof tests and update NimGroth16 backend…
dryajov May 30, 2025
1f8ad7b
chore: add project documentation and update test configurations
dryajov May 30, 2025
0b68386
chore: add project documentation and update prover configuration in t…
dryajov May 30, 2025
a38e41d
chore: add project docs and update test circuit paths
dryajov May 30, 2025
f8f7456
refactor: simplify error handling in suggestDownloadTool and enhance …
dryajov May 31, 2025
329a652
remove echo (todo: fix trace log)
dryajov May 31, 2025
495020d
fix log scopes
dryajov Jun 3, 2025
109b69f
fix: adjust duration and expiry periods in marketplace test
dryajov Jun 6, 2025
d7b8349
fix(integration): replace polling with started event subscription
emizzle Jun 6, 2025
d1b329b
fix(integration): Use RequestFulfilled events instead of querying the…
emizzle Jun 6, 2025
1fa960e
fix(integration): add timeouts to waiting for started futures
emizzle Jun 6, 2025
db3a1c4
correctly dispose of http request object
dryajov Jun 6, 2025
127972b
wip
dryajov Jun 6, 2025
a8dcbdf
chore: formatting
markspanbroek Jun 26, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -231,3 +231,16 @@
url = https://github.com/vacp2p/nim-ngtcp2.git
ignore = untracked
branch = master
[submodule "vendor/nim-groth16"]
path = vendor/nim-groth16
url = https://github.com/codex-storage/nim-groth16.git
ignore = untracked
branch = master
[submodule "vendor/nim-goldilocks-hash"]
path = vendor/nim-goldilocks-hash
url = https://github.com/codex-storage/nim-goldilocks-hash.git
ignore = untracked
branch = master
[submodule "vendor/circom-witnessgen"]
path = vendor/circom-witnessgen
url = https://github.com/codex-storage/circom-witnessgen.git
1 change: 0 additions & 1 deletion codex.nim
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import pkg/chronos
import pkg/questionable
import pkg/confutils
import pkg/confutils/defs
import pkg/confutils/std/net
import pkg/confutils/toml/defs as confTomlDefs
import pkg/confutils/toml/std/net as confTomlNet
Expand Down
34 changes: 15 additions & 19 deletions codex/codex.nim
Original file line number Diff line number Diff line change
Expand Up @@ -214,22 +214,19 @@ proc new*(
.withTcpTransport({ServerFlags.ReuseAddr})
.build()

var
cache: CacheStore = nil
taskpool: Taskpool

try:
if config.numThreads == ThreadCount(0):
taskpool = Taskpool.new(numThreads = min(countProcessors(), 16))
let numThreads =
if int(config.numThreads) == 0:
countProcessors()
else:
taskpool = Taskpool.new(numThreads = int(config.numThreads))
info "Threadpool started", numThreads = taskpool.numThreads
except CatchableError as exc:
raiseAssert("Failure in taskpool initialization:" & exc.msg)
int(config.numThreads)

var tp =
try:
Taskpool.new(numThreads)
except CatchableError as exc:
raiseAssert("Failure in tp initialization:" & exc.msg)

if config.cacheSize > 0'nb:
cache = CacheStore.new(cacheSize = config.cacheSize)
## Is unused?
info "Threadpool started", numThreads = tp.numThreads

let discoveryDir = config.dataDir / CodexDhtNamespace

Expand Down Expand Up @@ -305,9 +302,8 @@ proc new*(
store = NetworkStore.new(engine, repoStore)
prover =
if config.prover:
let backend =
config.initializeBackend().expect("Unable to create prover backend.")
some Prover.new(store, backend, config.numProofSamples)
let prover = config.initializeProver(tp).expect("Unable to create prover.")
some prover
else:
none Prover

Expand All @@ -317,7 +313,7 @@ proc new*(
engine = engine,
discovery = discovery,
prover = prover,
taskPool = taskpool,
taskPool = tp,
)

restServer = RestServerRef
Expand All @@ -337,5 +333,5 @@ proc new*(
restServer: restServer,
repoStore: repoStore,
maintenance: maintenance,
taskpool: taskpool,
taskpool: tp,
)
67 changes: 47 additions & 20 deletions codex/conf.nim
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import std/terminal # Is not used in tests
import std/options
import std/strutils
import std/typetraits
import std/cpuinfo

import pkg/chronos
import pkg/chronicles/helpers
Expand Down Expand Up @@ -54,9 +55,7 @@ export
DefaultQuotaBytes, DefaultBlockTtl, DefaultBlockInterval, DefaultNumBlocksPerInterval,
DefaultRequestCacheSize, DefaultMaxPriorityFeePerGas

type ThreadCount* = distinct Natural

proc `==`*(a, b: ThreadCount): bool {.borrow.}
type ThreadCount* = range[0 .. 256]

proc defaultDataDir*(): string =
let dataDir =
Expand All @@ -76,7 +75,6 @@ const

DefaultDataDir* = defaultDataDir()
DefaultCircuitDir* = defaultDataDir() / "circuits"
DefaultThreadCount* = ThreadCount(0)

type
StartUpCmd* {.pure.} = enum
Expand All @@ -87,6 +85,13 @@ type
noCmd
prover

ProverBackendCmd* {.pure.} = enum
nimgroth16
circomcompat

Curves* {.pure.} = enum
bn128 = "bn128"

LogKind* {.pure.} = enum
Auto = "auto"
Colors = "colors"
Expand Down Expand Up @@ -193,7 +198,8 @@ type
numThreads* {.
desc:
"Number of worker threads (\"0\" = use as many threads as there are CPU cores available)",
defaultValue: DefaultThreadCount,
defaultValueDesc: "0",
defaultValue: ThreadCount(0),
name: "num-threads"
.}: ThreadCount

Expand Down Expand Up @@ -389,15 +395,40 @@ type
name: "circuit-dir"
.}: OutDir

proverBackend* {.
desc:
"The backend to use for the prover. " &
"Must be one of: nimgroth16, circomcompat",
defaultValue: ProverBackendCmd.nimgroth16,
defaultValueDesc: "nimgroth16",
name: "prover-backend"
.}: ProverBackendCmd

curve* {.
desc: "The curve to use for the storage circuit",
defaultValue: Curves.bn128,
defaultValueDesc: $Curves.bn128,
name: "curve"
.}: Curves

circomR1cs* {.
desc: "The r1cs file for the storage circuit",
defaultValue: $DefaultCircuitDir / "proof_main.r1cs",
defaultValueDesc: $DefaultCircuitDir & "/proof_main.r1cs",
name: "circom-r1cs"
.}: InputFile

circomGraph* {.
desc:
"The graph file for the storage circuit (only used with nimgroth16 backend)",
defaultValue: $DefaultCircuitDir / "proof_main.bin",
defaultValueDesc: $DefaultDataDir & "/circuits/proof_main.bin",
name: "circom-graph"
.}: InputFile

circomWasm* {.
desc: "The wasm file for the storage circuit",
desc:
"The wasm file for the storage circuit (only used with circomcompat backend)",
defaultValue: $DefaultCircuitDir / "proof_main.wasm",
defaultValueDesc: $DefaultDataDir & "/circuits/proof_main.wasm",
name: "circom-wasm"
Expand All @@ -410,11 +441,11 @@ type
name: "circom-zkey"
.}: InputFile

# TODO: should probably be hidden and behind a feature flag
circomNoZkey* {.
desc: "Ignore the zkey file - use only for testing!",
defaultValue: false,
name: "circom-no-zkey"
name: "circom-no-zkey",
hidden
.}: bool

numProofSamples* {.
Expand Down Expand Up @@ -505,7 +536,7 @@ const

proc parseCmdArg*(
T: typedesc[MultiAddress], input: string
): MultiAddress {.upraises: [ValueError].} =
): MultiAddress {.raises: [ValueError].} =
var ma: MultiAddress
try:
let res = MultiAddress.init(input)
Expand All @@ -519,12 +550,8 @@ proc parseCmdArg*(
quit QuitFailure
ma

proc parseCmdArg*(T: type ThreadCount, input: string): T {.upraises: [ValueError].} =
let count = parseInt(input)
if count != 0 and count < 2:
warn "Invalid number of threads", input = input
quit QuitFailure
ThreadCount(count)
proc parseCmdArg*(T: type ThreadCount, val: string): T {.raises: [ValueError].} =
ThreadCount(val.parseUInt())
Copy link

Copilot AI May 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous version enforced a minimum thread count when the value was non-zero. Consider reintroducing a validation check (e.g. ensuring the parsed thread count is either 0 or at least 2) if lower positive values are not desired.

Suggested change
ThreadCount(val.parseUInt())
let threadCount = val.parseUInt()
if threadCount != 0 and threadCount < 2:
raise newException(ValueError, "Thread count must be either 0 or at least 2")
ThreadCount(threadCount)

Copilot uses AI. Check for mistakes.


proc parseCmdArg*(T: type SignedPeerRecord, uri: string): T =
var res: SignedPeerRecord
Expand Down Expand Up @@ -586,7 +613,7 @@ proc parseCmdArg*(T: type Duration, val: string): T =

proc readValue*(
r: var TomlReader, val: var EthAddress
) {.upraises: [SerializationError, IOError].} =
) {.raises: [SerializationError, IOError].} =
val = EthAddress.init(r.readValue(string)).get()

proc readValue*(r: var TomlReader, val: var SignedPeerRecord) =
Expand Down Expand Up @@ -614,7 +641,7 @@ proc readValue*(r: var TomlReader, val: var MultiAddress) =

proc readValue*(
r: var TomlReader, val: var NBytes
) {.upraises: [SerializationError, IOError].} =
) {.raises: [SerializationError, IOError].} =
var value = 0'i64
var str = r.readValue(string)
let count = parseSize(str, value, alwaysBin = true)
Expand All @@ -625,7 +652,7 @@ proc readValue*(

proc readValue*(
r: var TomlReader, val: var ThreadCount
) {.upraises: [SerializationError, IOError].} =
) {.raises: [SerializationError, IOError].} =
var str = r.readValue(string)
try:
val = parseCmdArg(ThreadCount, str)
Expand All @@ -634,7 +661,7 @@ proc readValue*(

proc readValue*(
r: var TomlReader, val: var Duration
) {.upraises: [SerializationError, IOError].} =
) {.raises: [SerializationError, IOError].} =
var str = r.readValue(string)
var dur: Duration
let count = parseDuration(str, dur)
Expand Down Expand Up @@ -701,7 +728,7 @@ proc stripAnsi*(v: string): string =

res

proc updateLogLevel*(logLevel: string) {.upraises: [ValueError].} =
proc updateLogLevel*(logLevel: string) {.raises: [ValueError].} =
# Updates log levels (without clearing old ones)
let directives = logLevel.split(";")
try:
Expand Down
10 changes: 6 additions & 4 deletions codex/erasure/erasure.nim
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ proc asyncEncode*(

proc encodeData(
self: Erasure, manifest: Manifest, params: EncodingParams
): Future[?!Manifest] {.async.} =
): Future[?!Manifest] {.async: (raises: [CancelledError]).} =
## Encode blocks pointed to by the protected manifest
##
## `manifest` - the manifest to encode
Expand Down Expand Up @@ -461,7 +461,7 @@ proc encode*(
blocks: Natural,
parity: Natural,
strategy = SteppedStrategy,
): Future[?!Manifest] {.async.} =
): Future[?!Manifest] {.async: (raises: [CancelledError]).} =
## Encode a manifest into one that is erasure protected.
##
## `manifest` - the original manifest to be encoded
Expand Down Expand Up @@ -554,7 +554,7 @@ proc asyncDecode*(

proc decodeInternal(
self: Erasure, encoded: Manifest
): Future[?!(ref seq[Cid], seq[Natural])] {.async.} =
): Future[?!(ref seq[Cid], seq[Natural])] {.async: (raises: [CancelledError]).} =
logScope:
steps = encoded.steps
rounded_blocks = encoded.rounded
Expand Down Expand Up @@ -638,7 +638,9 @@ proc decodeInternal(

return (cids, recoveredIndices).success

proc decode*(self: Erasure, encoded: Manifest): Future[?!Manifest] {.async.} =
proc decode*(
self: Erasure, encoded: Manifest
): Future[?!Manifest] {.async: (raises: [CancelledError]).} =
## Decode a protected manifest into it's original
## manifest
##
Expand Down
4 changes: 3 additions & 1 deletion codex/nat.nim
Original file line number Diff line number Diff line change
Expand Up @@ -423,10 +423,12 @@
it.remapAddr(ip = newIP, port = tcp)
else:
# NAT mapping failed - use original address
echo "Failed to get external IP, using original address", it
# TODO: `trace` breaks in the mapIt template
# trace "Failed to get external IP, using original address", it
discoveryAddrs.add(getMultiAddrWithIPAndUDPPort(ipPart.get, udpPort))
it
else:
# Invalid multiaddress format - return as is
it

Check warning on line 433 in codex/nat.nim

View check run for this annotation

Codecov / codecov/patch

codex/nat.nim#L433

Added line #L433 was not covered by tests
(newAddrs, discoveryAddrs)
Loading
Loading