Skip to content

Commit

Permalink
Light cleanup (#616)
Browse files Browse the repository at this point in the history
mostly whitespaces cleanup
  • Loading branch information
dryajov authored Nov 14, 2023
1 parent 06bb21b commit 8d61391
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 67 deletions.
20 changes: 10 additions & 10 deletions codex/blockexchange/engine/engine.nim
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@ proc stop*(b: BlockExcEngine) {.async.} =


proc sendWantHave(
b: BlockExcEngine,
address: BlockAddress,
selectedPeer: BlockExcPeerCtx,
b: BlockExcEngine,
address: BlockAddress,
selectedPeer: BlockExcPeerCtx,
peers: seq[BlockExcPeerCtx]): Future[void] {.async.} =
trace "Sending wantHave request to peers", address
for p in peers:
Expand All @@ -137,8 +137,8 @@ proc sendWantHave(
wantType = WantType.WantHave) # we only want to know if the peer has the block

proc sendWantBlock(
b: BlockExcEngine,
address: BlockAddress,
b: BlockExcEngine,
address: BlockAddress,
blockPeer: BlockExcPeerCtx): Future[void] {.async.} =
trace "Sending wantBlock request to", peer = blockPeer.id, address
await b.network.request.sendWantList(
Expand Down Expand Up @@ -189,22 +189,22 @@ proc requestBlock*(
if peers.len == 0:
b.discovery.queueFindBlocksReq(@[address.cidOrTreeCid])

let maybePeer =
let maybePeer =
if peers.len > 0:
peers[hash(address) mod peers.len].some
elif b.peers.len > 0:
toSeq(b.peers)[hash(address) mod b.peers.len].some
else:
BlockExcPeerCtx.none

if peer =? maybePeer:
asyncSpawn b.monitorBlockHandle(blockFuture, address, peer.id)
b.pendingBlocks.setInFlight(address)
await b.sendWantBlock(address, peer)
codex_block_exchange_want_block_lists_sent.inc()
await b.sendWantHave(address, peer, toSeq(b.peers))
codex_block_exchange_want_have_lists_sent.inc()

return await blockFuture

proc requestBlock*(
Expand Down Expand Up @@ -320,7 +320,7 @@ proc validateBlockDelivery(
if bd.address.leaf:
without proof =? bd.proof:
return failure("Missing proof")

if proof.index != bd.address.index:
return failure("Proof index " & $proof.index & " doesn't match leaf index " & $bd.address.index)

Expand Down Expand Up @@ -539,7 +539,7 @@ proc taskHandler*(b: BlockExcEngine, task: BlockExcPeerCtx) {.gcsafe, async.} =
trace "Handling lookup for entry", address = e.address
if e.address.leaf:
(await b.localStore.getBlockAndProof(e.address.treeCid, e.address.index)).map(
(blkAndProof: (Block, MerkleProof)) =>
(blkAndProof: (Block, MerkleProof)) =>
BlockDelivery(address: e.address, blk: blkAndProof[0], proof: blkAndProof[1].some)
)
else:
Expand Down
2 changes: 1 addition & 1 deletion codex/blockexchange/engine/pendingblocks.nim
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ proc resolve*(
retrievalDurationUs = (stopTime - startTime) div 1000

blockReq.handle.complete(bd.blk)

codex_block_exchange_retrieval_time_us.set(retrievalDurationUs)
trace "Block retrieval time", retrievalDurationUs, address = bd.address
else:
Expand Down
8 changes: 4 additions & 4 deletions codex/blocktype.nim
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func new*(
codec = multiCodec("raw")
): ?!Block =
## creates a new block for both storage and network IO
##
##

let
hash = ? MultiHash.digest($mcodec, data).mapFailure
Expand Down Expand Up @@ -132,7 +132,7 @@ func new*(

proc emptyCid*(version: CidVersion, hcodec: MultiCodec, dcodec: MultiCodec): ?!Cid =
## Returns cid representing empty content, given cid version, hash codec and data codec
##
##

const
Sha256 = multiCodec("sha2-256")
Expand Down Expand Up @@ -161,11 +161,11 @@ proc emptyBlock*(version: CidVersion, hcodec: MultiCodec): ?!Block =
.flatMap((cid: Cid) => Block.new(cid = cid, data = @[]))

proc emptyBlock*(cid: Cid): ?!Block =
cid.mhash.mapFailure.flatMap((mhash: MultiHash) =>
cid.mhash.mapFailure.flatMap((mhash: MultiHash) =>
emptyBlock(cid.cidver, mhash.mcodec))

proc isEmpty*(cid: Cid): bool =
success(cid) == cid.mhash.mapFailure.flatMap((mhash: MultiHash) =>
success(cid) == cid.mhash.mapFailure.flatMap((mhash: MultiHash) =>
emptyCid(cid.cidver, mhash.mcodec, cid.mcodec))

proc isEmpty*(blk: Block): bool =
Expand Down
8 changes: 4 additions & 4 deletions codex/erasure/erasure.nim
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ proc getPendingBlocks(

proc isFinished(): bool = pendingBlocks.len == 0

proc genNext(): Future[(?!bt.Block, int)] {.async.} =
proc genNext(): Future[(?!bt.Block, int)] {.async.} =
let completedFut = await one(pendingBlocks)
if (let i = pendingBlocks.find(completedFut); i >= 0):
pendingBlocks.del(i)
Expand All @@ -112,7 +112,7 @@ proc getPendingBlocks(
raise newException(CatchableError, "Future for block id not found, tree cid: " & $manifest.treeCid & ", index: " & $index)

Iter.new(genNext, isFinished)

proc prepareEncodingData(
self: Erasure,
manifest: Manifest,
Expand All @@ -134,7 +134,7 @@ proc prepareEncodingData(
without blk =? blkOrErr, err:
warn "Failed retreiving a block", treeCid = manifest.treeCid, idx, msg = err.msg
continue

let pos = indexToPos(params.steps, idx, step)
shallowCopy(data[pos], if blk.isEmpty: emptyBlock else: blk.data)
cids[idx] = blk.cid
Expand Down Expand Up @@ -168,7 +168,7 @@ proc prepareDecodingData(
## `emptyBlock` - the empty block to be used for padding
##

let
let
indicies = toSeq(countup(step, encoded.blocksCount - 1, encoded.steps))
pendingBlocksIter = self.getPendingBlocks(encoded, indicies)

Expand Down
20 changes: 10 additions & 10 deletions codex/manifest/manifest.nim
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@ export types

type
Manifest* = ref object of RootObj
treeCid {.serialize.}: Cid # Root of the merkle tree
datasetSize {.serialize.}: NBytes # Total size of all blocks
blockSize {.serialize.}: NBytes # Size of each contained block (might not be needed if blocks are len-prefixed)
version: CidVersion # Cid version
hcodec: MultiCodec # Multihash codec
codec: MultiCodec # Data set codec
case protected {.serialize.}: bool # Protected datasets have erasure coded info
treeCid {.serialize.}: Cid # Root of the merkle tree
datasetSize {.serialize.}: NBytes # Total size of all blocks
blockSize {.serialize.}: NBytes # Size of each contained block (might not be needed if blocks are len-prefixed)
version: CidVersion # Cid version
hcodec: MultiCodec # Multihash codec
codec: MultiCodec # Data set codec
case protected {.serialize.}: bool # Protected datasets have erasure coded info
of true:
ecK: int # Number of blocks to encode
ecM: int # Number of resulting parity blocks
originalTreeCid: Cid # The original root of the dataset being erasure coded
ecK: int # Number of blocks to encode
ecM: int # Number of resulting parity blocks
originalTreeCid: Cid # The original root of the dataset being erasure coded
originalDatasetSize: NBytes
else:
discard
Expand Down
48 changes: 24 additions & 24 deletions codex/merkletree/merkletree.nim
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type
###########################################################

func computeTreeHeight(leavesCount: int): int =
if isPowerOfTwo(leavesCount):
if isPowerOfTwo(leavesCount):
fastLog2(leavesCount) + 1
else:
fastLog2(leavesCount) + 2
Expand Down Expand Up @@ -84,24 +84,24 @@ proc init*(

proc addDataBlock*(self: var MerkleTreeBuilder, dataBlock: openArray[byte]): ?!void =
## Hashes the data block and adds the result of hashing to a buffer
##
##
let oldLen = self.buffer.len
self.buffer.setLen(oldLen + self.digestSize)
digestFn(self.mcodec, self.buffer, oldLen, dataBlock)

proc addLeaf*(self: var MerkleTreeBuilder, leaf: MultiHash): ?!void =
if leaf.mcodec != self.mcodec or leaf.size != self.digestSize:
return failure("Expected mcodec to be " & $self.mcodec & " and digest size to be " &
return failure("Expected mcodec to be " & $self.mcodec & " and digest size to be " &
$self.digestSize & " but was " & $leaf.mcodec & " and " & $leaf.size)

let oldLen = self.buffer.len
self.buffer.setLen(oldLen + self.digestSize)
self.buffer[oldLen..<oldLen + self.digestSize] = leaf.data.buffer[leaf.dpos..<leaf.dpos + self.digestSize]
success()

proc build*(self: MerkleTreeBuilder): ?!MerkleTree =
## Builds a tree from previously added data blocks
##
##
## Tree built from data blocks A, B and C is
## H5=H(H3 & H4)
## / \
Expand All @@ -114,7 +114,7 @@ proc build*(self: MerkleTreeBuilder): ?!MerkleTree =
## Memory layout is [H0, H1, H2, H3, H4, H5]
##
let
mcodec = self.mcodec
mcodec = self.mcodec
digestSize = self.digestSize
leavesCount = self.buffer.len div self.digestSize

Expand All @@ -123,7 +123,7 @@ proc build*(self: MerkleTreeBuilder): ?!MerkleTree =

let levels = computeLevels(leavesCount)
let totalNodes = levels[^1].offset + 1

var tree = MerkleTree(mcodec: mcodec, digestSize: digestSize, leavesCount: leavesCount, nodesBuffer: newSeq[byte](totalNodes * digestSize))

# copy leaves
Expand All @@ -134,7 +134,7 @@ proc build*(self: MerkleTreeBuilder): ?!MerkleTree =
var one = newSeq[byte](digestSize)
one[^1] = 0x01

var
var
concatBuf = newSeq[byte](2 * digestSize)
prevLevel = levels[0]
for level in levels[1..^1]:
Expand Down Expand Up @@ -180,7 +180,7 @@ proc nodes*(self: (MerkleTree | MerkleProof)): seq[MultiHash] {.noSideEffect.} =
proc mcodec*(self: (MerkleTree | MerkleProof)): MultiCodec =
self.mcodec

proc digestSize*(self: (MerkleTree | MerkleProof)): Natural =
proc digestSize*(self: (MerkleTree | MerkleProof)): Natural =
self.digestSize

proc root*(self: MerkleTree): MultiHash =
Expand All @@ -204,7 +204,7 @@ proc leavesCount*(self: MerkleTree): Natural =
proc getLeaf*(self: MerkleTree, index: Natural): ?!MultiHash =
if index >= self.leavesCount:
return failure("Index " & $index & " out of range [0.." & $(self.leavesCount - 1) & "]" )

success(self.nodeBufferToMultiHash(index))

proc getLeafCid*(self: MerkleTree, index: Natural, version = CIDv1, dataCodec = multiCodec("raw")): ?!Cid =
Expand All @@ -216,7 +216,7 @@ proc height*(self: MerkleTree): Natural =

proc getProof*(self: MerkleTree, index: Natural): ?!MerkleProof =
## Extracts proof from a tree for a given index
##
##
## Given a tree built from data blocks A, B and C
## H5
## / \
Expand All @@ -230,7 +230,7 @@ proc getProof*(self: MerkleTree, index: Natural): ?!MerkleProof =
## - 0,[H1, H4] for data block A
## - 1,[H0, H4] for data block B
## - 2,[0x00, H3] for data block C
##
##
if index >= self.leavesCount:
return failure("Index " & $index & " out of range [0.." & $(self.leavesCount - 1) & "]" )

Expand All @@ -250,7 +250,7 @@ proc getProof*(self: MerkleTree, index: Natural): ?!MerkleProof =
var dummyValue = if level.index == 0: zero else: one

if siblingIndex < level.offset + level.width:
proofNodesBuffer[level.index * self.digestSize..<(level.index + 1) * self.digestSize] =
proofNodesBuffer[level.index * self.digestSize..<(level.index + 1) * self.digestSize] =
self.nodesBuffer[siblingIndex * self.digestSize..<(siblingIndex + 1) * self.digestSize]
else:
proofNodesBuffer[level.index * self.digestSize..<(level.index + 1) * self.digestSize] = dummyValue
Expand Down Expand Up @@ -281,9 +281,9 @@ proc init*(
if totalNodes * digestSize == nodesBuffer.len:
success(
MerkleTree(
mcodec: mcodec,
digestSize: digestSize,
leavesCount: leavesCount,
mcodec: mcodec,
digestSize: digestSize,
leavesCount: leavesCount,
nodesBuffer: nodesBuffer
)
)
Expand All @@ -296,22 +296,22 @@ proc init*(
): ?!MerkleTree =
without leaf =? leaves.?[0]:
return failure("At least one leaf is required")

var builder = ? MerkleTreeBuilder.init(mcodec = leaf.mcodec)

for l in leaves:
let res = builder.addLeaf(l)
if res.isErr:
return failure(res.error)

builder.build()

proc init*(
T: type MerkleTree,
cids: openArray[Cid]
): ?!MerkleTree =
var leaves = newSeq[MultiHash]()

for cid in cids:
let res = cid.mhash.mapFailure
if res.isErr:
Expand Down Expand Up @@ -345,7 +345,7 @@ proc verifyLeaf*(self: MerkleProof, leaf: MultiHash, treeRoot: MultiHash): ?!boo
else:
concatBuf[0..^1] = self.nodesBuffer[offset..<(offset + self.digestSize)] & digestBuf
? digestFn(self.mcodec, digestBuf, 0, concatBuf)

let computedRoot = ? MultiHash.init(self.mcodec, digestBuf).mapFailure

success(computedRoot == treeRoot)
Expand All @@ -369,8 +369,8 @@ proc `$`*(self: MerkleProof): string =
", nodes: " & $self.nodes

func `==`*(a, b: MerkleProof): bool =
(a.index == b.index) and
(a.mcodec == b.mcodec) and
(a.index == b.index) and
(a.mcodec == b.mcodec) and
(a.digestSize == b.digestSize) and
(a.nodesBuffer == b.nodesBuffer)

Expand All @@ -385,11 +385,11 @@ proc init*(
let
mcodec = nodes[0].mcodec
digestSize = nodes[0].size

var nodesBuffer = newSeq[byte](nodes.len * digestSize)
for nodeIndex, node in nodes:
nodesBuffer[nodeIndex * digestSize..<(nodeIndex + 1) * digestSize] = node.data.buffer[node.dpos..<node.dpos + digestSize]

success(MerkleProof(mcodec: mcodec, digestSize: digestSize, index: index, nodesBuffer: nodesBuffer))

func init*(
Expand Down
6 changes: 3 additions & 3 deletions codex/node.nim
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ proc fetchBatched*(
onBatch: BatchProc = nil): Future[?!void] {.async, gcsafe.} =
## Fetch manifest in batches of `batchSize`
##

let batchCount = divUp(manifest.blocksCount, batchSize)

trace "Fetching blocks in batches of", size = batchSize
Expand Down Expand Up @@ -209,7 +209,7 @@ proc store*(

without blk =? bt.Block.new(cid, chunk, verify = false):
return failure("Unable to init block from chunk!")

cids.add(cid)

if err =? (await self.blockStore.putBlock(blk)).errorOption:
Expand All @@ -228,7 +228,7 @@ proc store*(

without treeCid =? tree.rootCid(CIDv1, dataCodec), err:
return failure(err)

for index, cid in cids:
without proof =? tree.getProof(index), err:
return failure(err)
Expand Down
4 changes: 2 additions & 2 deletions codex/stores/blockstore.nim
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ method getBlock*(self: BlockStore, address: BlockAddress): Future[?!Block] {.bas

method getBlockAndProof*(self: BlockStore, treeCid: Cid, index: Natural): Future[?!(Block, MerkleProof)] {.base.} =
## Get a block and associated inclusion proof by Cid of a merkle tree and an index of a leaf in a tree
##
##

raiseAssert("getBlockAndProof not implemented!")

method putBlock*(
Expand Down
Loading

0 comments on commit 8d61391

Please sign in to comment.