Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
7 changes: 5 additions & 2 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
url = https://github.com/durability-labs/nim-poseidon2
[submodule "vendor/nimble/libp2p"]
path = vendor/nimble/libp2p
url = https://github.com/durability-labs/nim-libp2p
url = https://github.com/vacp2p/nim-libp2p
[submodule "vendor/nimble/stint"]
path = vendor/nimble/stint
url = https://github.com/durability-labs/nim-stint
Expand Down Expand Up @@ -131,7 +131,7 @@
url = https://github.com/status-im/nim-eth
[submodule "vendor/nimble/json_rpc"]
path = vendor/nimble/json_rpc
url = https://github.com/status-im/nim-json-rpc
url = https://github.com/durability-labs/nim-json-rpc
[submodule "vendor/nimble/serde"]
path = vendor/nimble/serde
url = https://github.com/durability-labs/nim-serde
Expand Down Expand Up @@ -159,3 +159,6 @@
[submodule "vendor/nimble/zippy"]
path = vendor/nimble/zippy
url = https://github.com/guzba/zippy
[submodule "vendor/nimble/lsquic"]
path = vendor/nimble/lsquic
url = https://github.com/vacp2p/nim-lsquic.git
14 changes: 12 additions & 2 deletions archivist/blockexchange/network/network.nim
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,10 @@ proc send*(
except CatchableError as err:
error "Error sending message", peer = id, msg = err.msg
finally:
b.inflightSema.release()
try:
b.inflightSema.release()
except AsyncSemaphoreError as error:
raise newException(AssertionDefect, error.msg, error)

proc handleWantList(
b: BlockExcNetwork, peer: NetworkPeer, list: WantList
Expand Down Expand Up @@ -356,10 +359,17 @@ proc new*(
## Create a new BlockExcNetwork instance
##

var inflightSema: AsyncSemaphore
if maxInflight == 0:
inflightSema = newAsyncSemaphore(1)
discard inflightSema.tryAcquire()
else:
inflightSema = newAsyncSemaphore(maxInFlight)

let self = BlockExcNetwork(
switch: switch,
getConn: connProvider,
inflightSema: newAsyncSemaphore(maxInflight),
inflightSema: inflightSema,
maxInflight: maxInflight,
)

Expand Down
8 changes: 8 additions & 0 deletions archivist/libp2p/contentids_exts.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const ContentIdsExts = [
multiCodec("codex-root"),
multiCodec("codex-manifest"),
multiCodec("codex-block"),
multiCodec("codex-slot-root"),
multiCodec("codex-proving-root"),
multiCodec("codex-slot-cell"),
]
11 changes: 11 additions & 0 deletions archivist/libp2p/multicodec_exts.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const CodecExts = [
("poseidon2-alt_bn_128-sponge-r2", 0xCD10), # bn128 rate 2 sponge
("poseidon2-alt_bn_128-merkle-2kb", 0xCD11), # bn128 2kb compress & merkleize
("poseidon2-alt_bn_128-keyed-compress", 0xCD12), # bn128 keyed compress]
("codex-manifest", 0xCD01),
("codex-block", 0xCD02),
("codex-root", 0xCD03),
("codex-slot-root", 0xCD04),
("codex-proving-root", 0xCD05),
("codex-slot-cell", 0xCD06),
]
40 changes: 40 additions & 0 deletions archivist/libp2p/multihash_exts.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import blscurve/bls_public_exports
import pkg/constantine/hashes
import poseidon2

proc sha2_256hash_constantine(data: openArray[byte], output: var openArray[byte]) =
# Using Constantine's SHA256 instead of mhash for optimal performance on 32-byte merkle node hashing
# See: https://github.com/codex-storage/nim-codex/issues/1162
if len(output) > 0:
let digest = hashes.sha256.hash(data)
copyMem(addr output[0], addr digest[0], 32)

proc poseidon2_sponge_rate2(data: openArray[byte], output: var openArray[byte]) =
if len(output) > 0:
var digest = poseidon2.Sponge.digest(data).toBytes()
copyMem(addr output[0], addr digest[0], uint(len(output)))

proc poseidon2_merkle_2kb_sponge(data: openArray[byte], output: var openArray[byte]) =
if len(output) > 0:
var digest = poseidon2.SpongeMerkle.digest(data, 2048).toBytes()
copyMem(addr output[0], addr digest[0], uint(len(output)))

const Sha2256MultiHash* = MHash(
mcodec: multiCodec("sha2-256"),
size: sha256.sizeDigest,
coder: sha2_256hash_constantine,
)
const HashExts = [
# override sha2-256 hash function
Sha2256MultiHash,
MHash(
mcodec: multiCodec("poseidon2-alt_bn_128-sponge-r2"),
size: 32,
coder: poseidon2_sponge_rate2,
),
MHash(
mcodec: multiCodec("poseidon2-alt_bn_128-merkle-2kb"),
size: 32,
coder: poseidon2_merkle_2kb_sponge,
),
]
1 change: 1 addition & 0 deletions archivist/marketplace/contracts/marketplacecontract.nim
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export requests

type
MarketplaceContract* = ref object of Contract
RequestId = requests.RequestId

Marketplace_RepairRewardPercentageTooHigh* = object of SolidityError
Marketplace_SlashPercentageTooHigh* = object of SolidityError
Expand Down
55 changes: 14 additions & 41 deletions archivist/merkletree/archivist/archivist.nim
Original file line number Diff line number Diff line change
Expand Up @@ -47,28 +47,6 @@ type
ArchivistProof* = ref object of ByteProof
mcodec*: MultiCodec

# CodeHashes is not exported from libp2p
# So we need to recreate it instead of
proc initMultiHashCodeTable(): Table[MultiCodec, MHash] {.compileTime.} =
for item in HashesList:
result[item.mcodec] = item

const CodeHashes = initMultiHashCodeTable()

func mhash*(mcodec: MultiCodec): ?!MHash =
let mhash = CodeHashes.getOrDefault(mcodec)

if isNil(mhash.coder):
return failure "Invalid multihash codec"

success mhash

func digestSize*(self: (ArchivistTree or ArchivistProof)): int =
## Number of leaves
##

self.mhash.size

func getProof*(self: ArchivistTree, index: int): ?!ArchivistProof =
var proof = ArchivistProof(mcodec: self.mcodec)

Expand Down Expand Up @@ -131,17 +109,12 @@ proc `$`*(self: ArchivistProof): string =
", path: " & $self.path.mapIt(byteutils.toHex(it)) & ", mcodec: " & $self.mcodec &
" )"

func compress*(x, y: openArray[byte], key: ByteTreeKey, mhash: MHash): ?!ByteHash =
func compress*(x, y: openArray[byte], key: ByteTreeKey, codec: MultiCodec): ?!ByteHash =
## Compress two hashes
##

# Using Constantine's SHA256 instead of mhash for optimal performance on 32-byte merkle node hashing
# See: https://github.com/logos-storage/nim-codex/issues/1162

let input = @x & @y & @[key.byte]
var digest = hashes.sha256.hash(input)

success @digest
let digest = ?MultiHash.digest(codec, input).mapFailure
success digest.digestBytes

func init*(
_: type ArchivistTree,
Expand All @@ -152,12 +125,12 @@ func init*(
return failure "Empty leaves"

let
mhash = ?mcodec.mhash()
compressor = proc(x, y: seq[byte], key: ByteTreeKey): ?!ByteHash {.noSideEffect.} =
compress(x, y, key, mhash)
Zero: ByteHash = newSeq[byte](mhash.size)
compress(x, y, key, mcodec)
digestSize = ?mcodec.digestSize.mapFailure
Zero: ByteHash = newSeq[byte](digestSize)

if mhash.size != leaves[0].len:
if digestSize != leaves[0].len:
return failure "Invalid hash length"

var self = ArchivistTree(mcodec: mcodec, compress: compressor, zero: Zero)
Expand Down Expand Up @@ -195,12 +168,12 @@ proc fromNodes*(
return failure "Empty nodes"

let
mhash = ?mcodec.mhash()
Zero = newSeq[byte](mhash.size)
digestSize = ?mcodec.digestSize.mapFailure
Zero = newSeq[byte](digestSize)
compressor = proc(x, y: seq[byte], key: ByteTreeKey): ?!ByteHash {.noSideEffect.} =
compress(x, y, key, mhash)
compress(x, y, key, mcodec)

if mhash.size != nodes[0].len:
if digestSize != nodes[0].len:
return failure "Invalid hash length"

var
Expand Down Expand Up @@ -233,10 +206,10 @@ func init*(
return failure "Empty nodes"

let
mhash = ?mcodec.mhash()
Zero = newSeq[byte](mhash.size)
digestSize = ?mcodec.digestSize.mapFailure
Zero = newSeq[byte](digestSize)
compressor = proc(x, y: seq[byte], key: ByteTreeKey): ?!seq[byte] {.noSideEffect.} =
compress(x, y, key, mhash)
compress(x, y, key, mcodec)

success ArchivistProof(
compress: compressor,
Expand Down
5 changes: 5 additions & 0 deletions nim.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
# archivist-dht only supports secp256k1 keys for libp2p nodes
--define:"libp2p_pki_schemes=secp256k1"

# libp2p extensions for poseidon2 hashes
--define:"libp2p_multicodec_exts:../../../../archivist/libp2p/multicodec_exts.nim"
--define:"libp2p_multihash_exts:../../../../archivist/libp2p/multihash_exts.nim"
--define:"libp2p_contentids_exts:../../../../archivist/libp2p/contentids_exts.nim"

# confutils needs old-style case objects
--define:"nimOldCaseObjects"

Expand Down
6 changes: 3 additions & 3 deletions tests/archivist/merkletree/testarchivisttree.nim
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ suite "Test ArchivistTree":
tree == fromNodes

let
mhash = sha256.mhash().tryGet
zero: seq[byte] = newSeq[byte](mhash.size)
digestSize = sha256.digestSize.get
zero: seq[byte] = newSeq[byte](digestSize)
compress = proc(x, y: seq[byte], key: ByteTreeKey): seq[byte] =
compress(x, y, key, mhash).tryGet
compress(x, y, key, sha256).tryGet

makeTree = proc(data: seq[seq[byte]]): ArchivistTree =
ArchivistTree.init(sha256, leaves = data).tryGet
Expand Down
2 changes: 1 addition & 1 deletion vendor/nimble/confutils
Submodule confutils updated 61 files
+4 −0 .gitignore
+279 −257 README.md
+383 −155 confutils.nim
+3 −2 confutils.nimble
+3 −0 confutils/defs.nim
+7 −9 confutils/shell_completion.nim
+10 −0 tests/config_files/nested_cmd.toml
+6 −0 tests/help/.gitignore
+9 −0 tests/help/README.md
+34 −0 tests/help/snapshots/test_argument.txt
+15 −0 tests/help/snapshots/test_argument_abbr.txt
+17 −0 tests/help/snapshots/test_builtins.txt
+10 −0 tests/help/snapshots/test_builtins_lvl1Cmd1.txt
+26 −0 tests/help/snapshots/test_case_opt.txt
+13 −0 tests/help/snapshots/test_case_opt_cmdBlockProcessing.txt
+10 −0 tests/help/snapshots/test_cli_example.txt
+10 −0 tests/help/snapshots/test_cli_example_nims.txt
+16 −0 tests/help/snapshots/test_debug.txt
+18 −0 tests/help/snapshots/test_debug_debug.txt
+11 −0 tests/help/snapshots/test_debug_lvl1Cmd1_debug.txt
+20 −0 tests/help/snapshots/test_default_cmd_desc.txt
+10 −0 tests/help/snapshots/test_default_cmd_desc_exportCommand.txt
+10 −0 tests/help/snapshots/test_default_cmd_desc_printCommand.txt
+8 −0 tests/help/snapshots/test_default_value_desc.txt
+11 −0 tests/help/snapshots/test_dispatch.txt
+23 −0 tests/help/snapshots/test_longdesc.txt
+16 −0 tests/help/snapshots/test_longdesc_lvl1Cmd1.txt
+21 −0 tests/help/snapshots/test_multi_case_values.txt
+31 −0 tests/help/snapshots/test_nested_cmd.txt
+23 −0 tests/help/snapshots/test_nested_cmd_lvl1Cmd1.txt
+10 −0 tests/help/snapshots/test_nested_cmd_lvl1Cmd1_lvl2Cmd2.txt
+13 −0 tests/help/snapshots/test_separator.txt
+49 −0 tests/help/test_argument.nim
+28 −0 tests/help/test_argument_abbr.nim
+30 −0 tests/help/test_builtins.nim
+52 −0 tests/help/test_case_opt.nim
+7 −0 tests/help/test_cli_example.nim
+1 −0 tests/help/test_cli_example.nims
+49 −0 tests/help/test_debug.nim
+34 −0 tests/help/test_default_cmd_desc.nim
+22 −0 tests/help/test_default_value_desc.nim
+16 −0 tests/help/test_dispatch.nim
+42 −0 tests/help/test_longdesc.nim
+25 −0 tests/help/test_multi_case_values.nim
+53 −0 tests/help/test_nested_cmd.nim
+32 −0 tests/help/test_separator.nim
+11 −2 tests/test_all.nim
+99 −0 tests/test_argument.nim
+26 −9 tests/test_config_file.nim
+134 −0 tests/test_dispatch.nim
+43 −3 tests/test_duplicates.nim
+40 −0 tests/test_envvar.nim
+132 −0 tests/test_help.nim
+56 −0 tests/test_multi_case_values.nim
+69 −1 tests/test_nested_cmd.nim
+107 −0 tests/test_obsolete.nim
+26 −0 tests/test_obsolete_overload.nim
+8 −0 tests/test_obsolete_overload_def.nim
+49 −0 tests/test_onloaded.nim
+33 −8 tests/test_parsecmdarg.nim
+79 −0 tests/test_results_opt.nim
2 changes: 1 addition & 1 deletion vendor/nimble/contractabi
2 changes: 1 addition & 1 deletion vendor/nimble/eth
Submodule eth updated 104 files
2 changes: 1 addition & 1 deletion vendor/nimble/httputils
Submodule httputils updated 1 files
+1 −1 httputils.nimble
2 changes: 1 addition & 1 deletion vendor/nimble/json_rpc
2 changes: 1 addition & 1 deletion vendor/nimble/libp2p
Submodule libp2p updated 451 files
1 change: 1 addition & 0 deletions vendor/nimble/lsquic
Submodule lsquic added at 4fb03e
2 changes: 1 addition & 1 deletion vendor/nimble/minilru
2 changes: 1 addition & 1 deletion vendor/nimble/secp256k1
2 changes: 1 addition & 1 deletion vendor/nimble/snappy
2 changes: 1 addition & 1 deletion vendor/nimble/stint
Submodule stint updated 1 files
+1 −1 stint.nimble
2 changes: 1 addition & 1 deletion vendor/nimble/zlib
Submodule zlib updated 3 files
+201 −0 LICENSE-APACHEv2
+21 −0 LICENSE-MIT
+2 −2 zlib.nimble
Loading