From e47b38af11014c0651e9dd96592bb9e52249b4b2 Mon Sep 17 00:00:00 2001 From: Jaremy Creechley Date: Thu, 22 Jun 2023 08:11:18 -0700 Subject: [PATCH] Improving proc/func formatting consistency (#454) * Fixes/workarounds for nimsuggest failures in codex.nim. * remove rng prefix - it appears to work now * format new's to be more consistent * making proc formatting a bit more consistent --- codex/blockexchange/engine/discovery.nim | 29 ++++--- codex/blockexchange/engine/engine.nim | 21 +++-- codex/blockexchange/engine/pendingblocks.nim | 41 +++++---- codex/blockexchange/network/network.nim | 85 +++++++++++-------- codex/blockexchange/network/networkpeer.nim | 3 +- codex/blockexchange/peers/peerctxstore.nim | 4 +- codex/blocktype.nim | 24 ++++-- codex/chunker.nim | 37 ++++---- codex/codex.nim | 16 +++- .../interactions/hostinteractions.nim | 12 ++- codex/contracts/market.nim | 15 ++-- codex/discovery.nim | 59 ++++++++----- codex/erasure.nim | 12 ++- codex/erasure/backend.nim | 22 +++-- codex/erasure/backends/leopard.nim | 51 ++++++----- codex/erasure/erasure.nim | 24 +++--- codex/errors.nim | 5 +- codex/manifest/coders.nim | 12 +-- codex/manifest/manifest.nim | 29 ++++--- codex/node.nim | 77 ++++++++++------- codex/purchasing.nim | 6 +- codex/purchasing/purchase.nim | 23 +++-- codex/rest/json.nim | 11 ++- codex/sales/reservations.nim | 5 +- codex/storageproofs/por/por.nim | 20 +++-- codex/storageproofs/storageproofs.nim | 23 ++--- codex/storageproofs/stpnetwork.nim | 15 ++-- codex/storageproofs/stpstore.nim | 27 +++--- codex/stores/blockstore.nim | 7 +- codex/stores/cachestore.nim | 11 ++- codex/stores/maintenance.nim | 11 ++- codex/stores/networkstore.nim | 18 ++-- codex/stores/repostore.nim | 47 ++++++---- codex/streams/asyncstreamwrapper.nim | 19 +++-- codex/streams/storestream.nim | 22 +++-- codex/utils/addrutils.nim | 7 +- codex/utils/asyncheapqueue.nim | 5 +- codex/utils/timer.nim | 7 +- codex/validation.nim | 11 ++- tests/codex/helpers.nim | 7 +- tests/codex/helpers/mockclock.nim | 7 +- tests/codex/helpers/mockdiscovery.nim | 21 +++-- tests/codex/helpers/mockmarket.nim | 10 ++- tests/codex/helpers/mocktimer.nim | 11 ++- tests/codex/helpers/nodeutils.nim | 5 +- tests/codex/helpers/randomchunker.nim | 14 +-- tests/codex/stores/commonstoretests.nim | 9 +- tests/integration/codexclient.nim | 28 ++++-- 48 files changed, 594 insertions(+), 391 deletions(-) diff --git a/codex/blockexchange/engine/discovery.nim b/codex/blockexchange/engine/discovery.nim index f5e9f97e..5a930967 100644 --- a/codex/blockexchange/engine/discovery.nim +++ b/codex/blockexchange/engine/discovery.nim @@ -242,19 +242,22 @@ proc stop*(b: DiscoveryEngine) {.async.} = trace "Discovery engine stopped" proc new*( - T: type DiscoveryEngine, - localStore: BlockStore, - peers: PeerCtxStore, - network: BlockExcNetwork, - discovery: Discovery, - pendingBlocks: PendingBlocksManager, - concurrentAdvReqs = DefaultConcurrentAdvertRequests, - concurrentDiscReqs = DefaultConcurrentDiscRequests, - discoveryLoopSleep = DefaultDiscoveryLoopSleep, - advertiseLoopSleep = DefaultAdvertiseLoopSleep, - minPeersPerBlock = DefaultMinPeersPerBlock, - advertiseType = BlockType.Both): DiscoveryEngine = - T( + T: type DiscoveryEngine, + localStore: BlockStore, + peers: PeerCtxStore, + network: BlockExcNetwork, + discovery: Discovery, + pendingBlocks: PendingBlocksManager, + concurrentAdvReqs = DefaultConcurrentAdvertRequests, + concurrentDiscReqs = DefaultConcurrentDiscRequests, + discoveryLoopSleep = DefaultDiscoveryLoopSleep, + advertiseLoopSleep = DefaultAdvertiseLoopSleep, + minPeersPerBlock = DefaultMinPeersPerBlock, + advertiseType = BlockType.Both +): DiscoveryEngine = + ## Create a discovery engine instance for advertising services + ## + DiscoveryEngine( localStore: localStore, peers: peers, network: network, diff --git a/codex/blockexchange/engine/engine.nim b/codex/blockexchange/engine/engine.nim index bf8de648..b8a05935 100644 --- a/codex/blockexchange/engine/engine.nim +++ b/codex/blockexchange/engine/engine.nim @@ -496,15 +496,18 @@ proc blockexcTaskRunner(b: BlockExcEngine) {.async.} = trace "Exiting blockexc task runner" proc new*( - T: type BlockExcEngine, - localStore: BlockStore, - wallet: WalletRef, - network: BlockExcNetwork, - discovery: DiscoveryEngine, - peerStore: PeerCtxStore, - pendingBlocks: PendingBlocksManager, - concurrentTasks = DefaultConcurrentTasks, - peersPerRequest = DefaultMaxPeersPerRequest): T = + T: type BlockExcEngine, + localStore: BlockStore, + wallet: WalletRef, + network: BlockExcNetwork, + discovery: DiscoveryEngine, + peerStore: PeerCtxStore, + pendingBlocks: PendingBlocksManager, + concurrentTasks = DefaultConcurrentTasks, + peersPerRequest = DefaultMaxPeersPerRequest +): BlockExcEngine = + ## Create new block exchange engine instance + ## let engine = BlockExcEngine( diff --git a/codex/blockexchange/engine/pendingblocks.nim b/codex/blockexchange/engine/pendingblocks.nim index 20038217..171311f4 100644 --- a/codex/blockexchange/engine/pendingblocks.nim +++ b/codex/blockexchange/engine/pendingblocks.nim @@ -34,10 +34,11 @@ type blocks*: Table[Cid, BlockReq] # pending Block requests proc getWantHandle*( - p: PendingBlocksManager, - cid: Cid, - timeout = DefaultBlockTimeout, - inFlight = false): Future[Block] {.async.} = + p: PendingBlocksManager, + cid: Cid, + timeout = DefaultBlockTimeout, + inFlight = false +): Future[Block] {.async.} = ## Add an event for a block ## @@ -60,9 +61,8 @@ proc getWantHandle*( finally: p.blocks.del(cid) -proc resolve*( - p: PendingBlocksManager, - blocks: seq[Block]) = +proc resolve*(p: PendingBlocksManager, + blocks: seq[Block]) = ## Resolve pending blocks ## @@ -73,28 +73,25 @@ proc resolve*( trace "Resolving block", cid = blk.cid pending[].handle.complete(blk) -proc setInFlight*( - p: PendingBlocksManager, - cid: Cid, - inFlight = true) = +proc setInFlight*(p: PendingBlocksManager, + cid: Cid, + inFlight = true) = p.blocks.withValue(cid, pending): pending[].inFlight = inFlight trace "Setting inflight", cid, inFlight = pending[].inFlight -proc isInFlight*( - p: PendingBlocksManager, - cid: Cid): bool = +proc isInFlight*(p: PendingBlocksManager, + cid: Cid + ): bool = p.blocks.withValue(cid, pending): result = pending[].inFlight trace "Getting inflight", cid, inFlight = result -proc pending*( - p: PendingBlocksManager, - cid: Cid): bool = cid in p.blocks +proc pending*(p: PendingBlocksManager, cid: Cid): bool = + cid in p.blocks -proc contains*( - p: PendingBlocksManager, - cid: Cid): bool = p.pending(cid) +proc contains*(p: PendingBlocksManager, cid: Cid): bool = + p.pending(cid) iterator wantList*(p: PendingBlocksManager): Cid = for k in p.blocks.keys: @@ -107,5 +104,5 @@ iterator wantHandles*(p: PendingBlocksManager): Future[Block] = func len*(p: PendingBlocksManager): int = p.blocks.len -func new*(T: type PendingBlocksManager): T = - T() +func new*(T: type PendingBlocksManager): PendingBlocksManager = + PendingBlocksManager() diff --git a/codex/blockexchange/network/network.nim b/codex/blockexchange/network/network.nim index 6cab389f..ef048e45 100644 --- a/codex/blockexchange/network/network.nim +++ b/codex/blockexchange/network/network.nim @@ -102,12 +102,16 @@ proc handleWantList( # TODO: make into a template proc makeWantList*( - cids: seq[Cid], - priority: int = 0, - cancel: bool = false, - wantType: WantType = WantType.WantHave, - full: bool = false, - sendDontHave: bool = false): Wantlist = + cids: seq[Cid], + priority: int = 0, + cancel: bool = false, + wantType: WantType = WantType.WantHave, + full: bool = false, + sendDontHave: bool = false +): Wantlist = + ## make list of wanted entries + ## + Wantlist( entries: cids.mapIt( Entry( @@ -119,14 +123,15 @@ proc makeWantList*( full: full) proc sendWantList*( - b: BlockExcNetwork, - id: PeerId, - cids: seq[Cid], - priority: int32 = 0, - cancel: bool = false, - wantType: WantType = WantType.WantHave, - full: bool = false, - sendDontHave: bool = false): Future[void] = + b: BlockExcNetwork, + id: PeerId, + cids: seq[Cid], + priority: int32 = 0, + cancel: bool = false, + wantType: WantType = WantType.WantHave, + full: bool = false, + sendDontHave: bool = false +): Future[void] = ## Send a want message to peer ## @@ -142,9 +147,10 @@ proc sendWantList*( b.send(id, Message(wantlist: msg)) proc handleBlocks( - b: BlockExcNetwork, - peer: NetworkPeer, - blocks: seq[pb.Block]) {.async.} = + b: BlockExcNetwork, + peer: NetworkPeer, + blocks: seq[pb.Block] +) {.async.} = ## Handle incoming blocks ## @@ -203,9 +209,10 @@ proc sendBlockPresence*( b.send(id, Message(blockPresences: @presence)) proc handleAccount( - network: BlockExcNetwork, - peer: NetworkPeer, - account: Account) {.async.} = + network: BlockExcNetwork, + peer: NetworkPeer, + account: Account +) {.async.} = ## Handle account info ## @@ -213,34 +220,43 @@ proc handleAccount( await network.handlers.onAccount(peer.id, account) proc sendAccount*( - b: BlockExcNetwork, - id: PeerId, - account: Account): Future[void] = + b: BlockExcNetwork, + id: PeerId, + account: Account +): Future[void] = ## Send account info to remote ## b.send(id, Message(account: AccountMessage.init(account))) proc sendPayment*( - b: BlockExcNetwork, - id: PeerId, - payment: SignedState): Future[void] = + b: BlockExcNetwork, + id: PeerId, + payment: SignedState +): Future[void] = ## Send payment to remote ## b.send(id, Message(payment: StateChannelUpdate.init(payment))) proc handlePayment( - network: BlockExcNetwork, - peer: NetworkPeer, - payment: SignedState) {.async.} = + network: BlockExcNetwork, + peer: NetworkPeer, + payment: SignedState +) {.async.} = ## Handle payment ## if not network.handlers.onPayment.isNil: await network.handlers.onPayment(peer.id, payment) -proc rpcHandler(b: BlockExcNetwork, peer: NetworkPeer, msg: Message) {.async.} = +proc rpcHandler( + b: BlockExcNetwork, + peer: NetworkPeer, + msg: Message +) {.async.} = + ## handle rpc messages + ## try: if msg.wantlist.entries.len > 0: asyncSpawn b.handleWantList(peer, msg.wantlist) @@ -325,10 +341,11 @@ method init*(b: BlockExcNetwork) = b.codec = Codec proc new*( - T: type BlockExcNetwork, - switch: Switch, - connProvider: ConnProvider = nil, - maxInflight = MaxInflight): T = + T: type BlockExcNetwork, + switch: Switch, + connProvider: ConnProvider = nil, + maxInflight = MaxInflight +): BlockExcNetwork = ## Create a new BlockExcNetwork instance ## diff --git a/codex/blockexchange/network/networkpeer.nim b/codex/blockexchange/network/networkpeer.nim index e2aadb8e..2c8890b0 100644 --- a/codex/blockexchange/network/networkpeer.nim +++ b/codex/blockexchange/network/networkpeer.nim @@ -85,7 +85,8 @@ func new*( T: type NetworkPeer, peer: PeerId, connProvider: ConnProvider, - rpcHandler: RPCHandler): T = + rpcHandler: RPCHandler +): NetworkPeer = doAssert(not isNil(connProvider), "should supply connection provider") diff --git a/codex/blockexchange/peers/peerctxstore.nim b/codex/blockexchange/peers/peerctxstore.nim index e57cac39..a38196b2 100644 --- a/codex/blockexchange/peers/peerctxstore.nim +++ b/codex/blockexchange/peers/peerctxstore.nim @@ -93,5 +93,5 @@ func selectCheapest*(self: PeerCtxStore, cid: Cid): seq[BlockExcPeerCtx] = return peers proc new*(T: type PeerCtxStore): PeerCtxStore = - T( - peers: initOrderedTable[PeerId, BlockExcPeerCtx]()) + ## create new instance of a peer context store + PeerCtxStore(peers: initOrderedTable[PeerId, BlockExcPeerCtx]()) diff --git a/codex/blocktype.nim b/codex/blocktype.nim index 6212a2d2..fb9cb481 100644 --- a/codex/blocktype.nim +++ b/codex/blocktype.nim @@ -126,11 +126,14 @@ proc `$`*(b: Block): string = result &= "\ndata: " & string.fromBytes(b.data) func new*( - T: type Block, - data: openArray[byte] = [], - version = CIDv1, - mcodec = multiCodec("sha2-256"), - codec = multiCodec("raw")): ?!T = + T: type Block, + data: openArray[byte] = [], + version = CIDv1, + mcodec = multiCodec("sha2-256"), + codec = multiCodec("raw") +): ?!Block = + ## creates a new block for both storage and network IO + ## let hash = ? MultiHash.digest($mcodec, data).mapFailure @@ -143,10 +146,13 @@ func new*( data: @data).success func new*( - T: type Block, - cid: Cid, - data: openArray[byte], - verify: bool = true): ?!T = + T: type Block, + cid: Cid, + data: openArray[byte], + verify: bool = true +): ?!Block = + ## creates a new block for both storage and network IO + ## let mhash = ? cid.mhash.mapFailure diff --git a/codex/chunker.nim b/codex/chunker.nim index 9d4d0fd2..436db0fc 100644 --- a/codex/chunker.nim +++ b/codex/chunker.nim @@ -60,21 +60,25 @@ proc getBytes*(c: Chunker): Future[seq[byte]] {.async.} = return move buff func new*( - T: type Chunker, - reader: Reader, - chunkSize = DefaultChunkSize, - pad = true): T = - - T(reader: reader, + T: type Chunker, + reader: Reader, + chunkSize = DefaultChunkSize, + pad = true +): Chunker = + ## create a new Chunker instance + ## + Chunker( + reader: reader, offset: 0, chunkSize: chunkSize, pad: pad) proc new*( - T: type LPStreamChunker, - stream: LPStream, - chunkSize = DefaultChunkSize, - pad = true): T = + T: type LPStreamChunker, + stream: LPStream, + chunkSize = DefaultChunkSize, + pad = true +): LPStreamChunker = ## create the default File chunker ## @@ -92,16 +96,17 @@ proc new*( return res - T.new( + LPStreamChunker.new( reader = reader, chunkSize = chunkSize, pad = pad) proc new*( - T: type FileChunker, - file: File, - chunkSize = DefaultChunkSize, - pad = true): T = + T: type FileChunker, + file: File, + chunkSize = DefaultChunkSize, + pad = true +): FileChunker = ## create the default File chunker ## @@ -123,7 +128,7 @@ proc new*( return total - T.new( + FileChunker.new( reader = reader, chunkSize = chunkSize, pad = pad) diff --git a/codex/codex.nim b/codex/codex.nim index ba37d6a0..f470b738 100644 --- a/codex/codex.nim +++ b/codex/codex.nim @@ -51,7 +51,13 @@ type CodexPrivateKey* = libp2p.PrivateKey # alias -proc bootstrapInteractions(config: CodexConf, repo: RepoStore): Future[Contracts] {.async.} = +proc bootstrapInteractions( + config: CodexConf, + repo: RepoStore +): Future[Contracts] {.async.} = + ## bootstrap interactions and return contracts + ## using clients, hosts, validators pairings + ## if not config.persistence and not config.validator: if config.ethAccount.isSome: @@ -150,7 +156,8 @@ proc stop*(s: CodexServer) {.async.} = proc new*( T: type CodexServer, config: CodexConf, - privateKey: CodexPrivateKey): CodexServer = + privateKey: CodexPrivateKey +): CodexServer = ## create CodexServer including setting up datastore, repostore, etc let switch = SwitchBuilder @@ -231,9 +238,10 @@ proc new*( switch.mount(network) - T( + CodexServer( config: config, codexNode: codexNode, restServer: restServer, repoStore: repoStore, - maintenance: maintenance) + maintenance: maintenance + ) diff --git a/codex/contracts/interactions/hostinteractions.nim b/codex/contracts/interactions/hostinteractions.nim index 2f9ba92d..5e8beac5 100644 --- a/codex/contracts/interactions/hostinteractions.nim +++ b/codex/contracts/interactions/hostinteractions.nim @@ -14,10 +14,14 @@ type sales*: Sales proving*: Proving -proc new*(_: type HostInteractions, - clock: OnChainClock, - sales: Sales, - proving: Proving): HostInteractions = +proc new*( + _: type HostInteractions, + clock: OnChainClock, + sales: Sales, + proving: Proving +): HostInteractions = + ## Create a new HostInteractions instance + ## HostInteractions(clock: clock, sales: sales, proving: proving) method start*(self: HostInteractions) {.async.} = diff --git a/codex/contracts/market.nim b/codex/contracts/market.nim index 3ef4ec38..b7b7c94f 100644 --- a/codex/contracts/market.nim +++ b/codex/contracts/market.nim @@ -74,7 +74,7 @@ method getRequest(market: OnChainMarket, raise e method requestState*(market: OnChainMarket, - requestId: RequestId): Future[?RequestState] {.async.} = + requestId: RequestId): Future[?RequestState] {.async.} = try: return some await market.contract.requestState(requestId) except ProviderError as e: @@ -100,9 +100,8 @@ method getHost(market: OnChainMarket, else: return none Address -method getActiveSlot*( - market: OnChainMarket, - slotId: SlotId): Future[?Slot] {.async.} = +method getActiveSlot*(market: OnChainMarket, + slotId: SlotId): Future[?Slot] {.async.} = try: return some await market.contract.getActiveSlot(slotId) @@ -154,9 +153,11 @@ method markProofAsMissing*(market: OnChainMarket, period: Period) {.async.} = await market.contract.markProofAsMissing(id, period) -method canProofBeMarkedAsMissing*(market: OnChainMarket, - id: SlotId, - period: Period): Future[bool] {.async.} = +method canProofBeMarkedAsMissing*( + market: OnChainMarket, + id: SlotId, + period: Period +): Future[bool] {.async.} = let provider = market.contract.provider let contractWithoutSigner = market.contract.connect(provider) let overrides = CallOverrides(blockTag: some BlockTag.pending) diff --git a/codex/discovery.nim b/codex/discovery.nim index 836ddb88..73fd712e 100644 --- a/codex/discovery.nim +++ b/codex/discovery.nim @@ -56,25 +56,27 @@ proc toNodeId*(host: ca.Address): NodeId = readUintBE[256](keccak256.digest(host.toArray).data) proc findPeer*( - d: Discovery, - peerId: PeerId): Future[?PeerRecord] {.async.} = + d: Discovery, + peerId: PeerId +): Future[?PeerRecord] {.async.} = trace "protocol.resolve..." + ## Find peer using the given Discovery object + ## let node = await d.protocol.resolve(toNodeId(peerId)) return if node.isSome(): - trace "protocol.resolve some data" node.get().record.data.some else: - trace "protocol.resolve none" PeerRecord.none method find*( - d: Discovery, - cid: Cid): Future[seq[SignedPeerRecord]] {.async, base.} = + d: Discovery, + cid: Cid +): Future[seq[SignedPeerRecord]] {.async, base.} = ## Find block providers - ## + ## trace "Finding providers for block", cid without providers =? @@ -98,8 +100,9 @@ method provide*(d: Discovery, cid: Cid) {.async, base.} = trace "Provided to nodes", nodes = nodes.len method find*( - d: Discovery, - host: ca.Address): Future[seq[SignedPeerRecord]] {.async, base.} = + d: Discovery, + host: ca.Address +): Future[seq[SignedPeerRecord]] {.async, base.} = ## Find host providers ## @@ -129,11 +132,20 @@ method provide*(d: Discovery, host: ca.Address) {.async, base.} = if nodes.len > 0: trace "Provided to nodes", nodes = nodes.len -method removeProvider*(d: Discovery, peerId: PeerId): Future[void] {.base.} = +method removeProvider*( + d: Discovery, + peerId: PeerId +): Future[void] {.base.} = + ## Remove provider from providers table + ## + trace "Removing provider", peerId d.protocol.removeProvidersLocal(peerId) proc updateAnnounceRecord*(d: Discovery, addrs: openArray[MultiAddress]) = + ## Update providers record + ## + d.announceAddrs = @addrs trace "Updating announce record", addrs = d.announceAddrs @@ -146,6 +158,9 @@ proc updateAnnounceRecord*(d: Discovery, addrs: openArray[MultiAddress]) = .expect("Should update SPR") proc updateDhtRecord*(d: Discovery, ip: ValidIpAddress, port: Port) = + ## Update providers record + ## + trace "Updating Dht record", ip, port = $port d.dhtRecord = SignedPeerRecord.init( d.key, PeerRecord.init(d.peerId, @[ @@ -154,10 +169,6 @@ proc updateDhtRecord*(d: Discovery, ip: ValidIpAddress, port: Port) = IpTransportProtocol.udpProtocol, port)])).expect("Should construct signed record").some - if not d.protocol.isNil: - d.protocol.updateRecord(d.dhtRecord) - .expect("Should update SPR") - proc start*(d: Discovery) {.async.} = d.protocol.open() await d.protocol.start() @@ -166,17 +177,19 @@ proc stop*(d: Discovery) {.async.} = await d.protocol.closeWait() proc new*( - T: type Discovery, - key: PrivateKey, - bindIp = ValidIpAddress.init(IPv4_any()), - bindPort = 0.Port, - announceAddrs: openArray[MultiAddress], - bootstrapNodes: openArray[SignedPeerRecord] = [], - store: Datastore = SQLiteDatastore.new(Memory) - .expect("Should not fail!")): T = + T: type Discovery, + key: PrivateKey, + bindIp = ValidIpAddress.init(IPv4_any()), + bindPort = 0.Port, + announceAddrs: openArray[MultiAddress], + bootstrapNodes: openArray[SignedPeerRecord] = [], + store: Datastore = SQLiteDatastore.new(Memory).expect("Should not fail!") +): Discovery = + ## Create a new Discovery node instance for the given key and datastore + ## var - self = T( + self = Discovery( key: key, peerId: PeerId.init(key).expect("Should construct PeerId")) diff --git a/codex/erasure.nim b/codex/erasure.nim index b14d8e52..5dfebcd4 100644 --- a/codex/erasure.nim +++ b/codex/erasure.nim @@ -12,8 +12,14 @@ import ./erasure/backends/leopard export erasure -func leoEncoderProvider*(size, buffers, parity: int): EncoderBackend {.raises: [Defect].} = +func leoEncoderProvider*( + size, buffers, parity: int +): EncoderBackend {.raises: [Defect].} = + ## create new Leo Encoder LeoEncoderBackend.new(size, buffers, parity) -func leoDecoderProvider*(size, buffers, parity: int): DecoderBackend {.raises: [Defect].} = - LeoDecoderBackend.new(size, buffers, parity) +func leoDecoderProvider*( + size, buffers, parity: int +): DecoderBackend {.raises: [Defect].} = + ## create new Leo Decoder + LeoDecoderBackend.new(size, buffers, parity) diff --git a/codex/erasure/backend.nim b/codex/erasure/backend.nim index d3c95211..2fa53d68 100644 --- a/codex/erasure/backend.nim +++ b/codex/erasure/backend.nim @@ -23,17 +23,25 @@ type DecoderBackend* = ref object of Backend method release*(self: Backend) {.base.} = + ## release the backend + ## raiseAssert("not implemented!") method encode*( - self: EncoderBackend, - buffers, - parity: var openArray[seq[byte]]): Result[void, cstring] {.base.} = + self: EncoderBackend, + buffers, + parity: var openArray[seq[byte]] +): Result[void, cstring] {.base.} = + ## encode buffers using a backend + ## raiseAssert("not implemented!") method decode*( - self: DecoderBackend, - buffers, - parity, - recovered: var openArray[seq[byte]]): Result[void, cstring] {.base.} = + self: DecoderBackend, + buffers, + parity, + recovered: var openArray[seq[byte]] +): Result[void, cstring] {.base.} = + ## decode buffers using a backend + ## raiseAssert("not implemented!") diff --git a/codex/erasure/backends/leopard.nim b/codex/erasure/backends/leopard.nim index bf0a8161..2051fc97 100644 --- a/codex/erasure/backends/leopard.nim +++ b/codex/erasure/backends/leopard.nim @@ -22,9 +22,11 @@ type decoder*: Option[LeoDecoder] method encode*( - self: LeoEncoderBackend, - data, - parity: var openArray[seq[byte]]): Result[void, cstring] = + self: LeoEncoderBackend, + data, + parity: var openArray[seq[byte]] +): Result[void, cstring] = + ## Encode data using Leopard backend if parity.len == 0: return ok() @@ -41,12 +43,15 @@ method encode*( encoder.encode(data, parity) method decode*( - self: LeoDecoderBackend, - data, - parity, - recovered: var openArray[seq[byte]]): Result[void, cstring] = + self: LeoDecoderBackend, + data, + parity, + recovered: var openArray[seq[byte]] +): Result[void, cstring] = + ## Decode data using given Leopard backend - var decoder = if self.decoder.isNone: + var decoder = + if self.decoder.isNone: self.decoder = (? LeoDecoder.init( self.blockSize, self.buffers, @@ -65,22 +70,28 @@ method release*(self: LeoDecoderBackend) = if self.decoder.isSome: self.decoder.get().free() -func new*( - T: type LeoEncoderBackend, - blockSize, - buffers, - parity: int): T = - T( +proc new*( + T: type LeoEncoderBackend, + blockSize, + buffers, + parity: int +): LeoEncoderBackend = + ## Create an instance of an Leopard Encoder backend + ## + LeoEncoderBackend( blockSize: blockSize, buffers: buffers, parity: parity) -func new*( - T: type LeoDecoderBackend, - blockSize, - buffers, - parity: int): T = - T( +proc new*( + T: type LeoDecoderBackend, + blockSize, + buffers, + parity: int +): LeoDecoderBackend = + ## Create an instance of an Leopard Decoder backend + ## + LeoDecoderBackend( blockSize: blockSize, buffers: buffers, parity: parity) diff --git a/codex/erasure/erasure.nim b/codex/erasure/erasure.nim index fb0a8f28..5d9c60fa 100644 --- a/codex/erasure/erasure.nim +++ b/codex/erasure/erasure.nim @@ -63,10 +63,11 @@ type store*: BlockStore proc encode*( - self: Erasure, - manifest: Manifest, - blocks: int, - parity: int): Future[?!Manifest] {.async.} = + self: Erasure, + manifest: Manifest, + blocks: int, + parity: int +): Future[?!Manifest] {.async.} = ## Encode a manifest into one that is erasure protected. ## ## `manifest` - the original manifest to be encoded @@ -153,8 +154,9 @@ proc encode*( return encoded.success proc decode*( - self: Erasure, - encoded: Manifest): Future[?!Manifest] {.async.} = + self: Erasure, + encoded: Manifest +): Future[?!Manifest] {.async.} = ## Decode a protected manifest into it's original ## manifest ## @@ -265,10 +267,12 @@ proc stop*(self: Erasure) {.async.} = return proc new*( - T: type Erasure, - store: BlockStore, - encoderProvider: EncoderProvider, - decoderProvider: DecoderProvider): Erasure = + T: type Erasure, + store: BlockStore, + encoderProvider: EncoderProvider, + decoderProvider: DecoderProvider +): Erasure = + ## Create a new Erasure instance for encoding and decoding manifests Erasure( store: store, diff --git a/codex/errors.nim b/codex/errors.nim index b3777f2a..40bd7749 100644 --- a/codex/errors.nim +++ b/codex/errors.nim @@ -14,8 +14,9 @@ type CodexResult*[T] = Result[T, ref CodexError] template mapFailure*( - exp: untyped, - exc: typed = type CodexError): untyped = + exp: untyped, + exc: typed = type CodexError +): untyped = ## Convert `Result[T, E]` to `Result[E, ref CatchableError]` ## diff --git a/codex/manifest/coders.nim b/codex/manifest/coders.nim index c16e95d1..06ccbdc2 100644 --- a/codex/manifest/coders.nim +++ b/codex/manifest/coders.nim @@ -166,8 +166,9 @@ func decode*(_: DagPBCoder, data: openArray[byte]): ?!Manifest = self.success proc encode*( - self: Manifest, - encoder = ManifestContainers[$DagPBCodec]): ?!seq[byte] = + self: Manifest, + encoder = ManifestContainers[$DagPBCodec] +): ?!seq[byte] = ## Encode a manifest using `encoder` ## @@ -177,9 +178,10 @@ proc encode*( encoder.encode(self) func decode*( - _: type Manifest, - data: openArray[byte], - decoder = ManifestContainers[$DagPBCodec]): ?!Manifest = + _: type Manifest, + data: openArray[byte], + decoder = ManifestContainers[$DagPBCodec] +): ?!Manifest = ## Decode a manifest using `decoder` ## diff --git a/codex/manifest/manifest.nim b/codex/manifest/manifest.nim index 3fc3aa2f..d5b7c293 100644 --- a/codex/manifest/manifest.nim +++ b/codex/manifest/manifest.nim @@ -159,14 +159,15 @@ proc cid*(self: Manifest): ?!Cid = ############################################################ proc new*( - T: type Manifest, - blocks: openArray[Cid] = [], - protected = false, - version = CIDv1, - hcodec = multiCodec("sha2-256"), - codec = multiCodec("raw"), - blockSize = BlockSize): ?!T = - ## Create a manifest using array of `Cid`s + T: type Manifest, + blocks: openArray[Cid] = [], + protected = false, + version = CIDv1, + hcodec = multiCodec("sha2-256"), + codec = multiCodec("raw"), + blockSize = BlockSize +): ?!Manifest = + ## Create a manifest using an array of `Cid`s ## if hcodec notin EmptyDigests[version]: @@ -182,9 +183,10 @@ proc new*( protected: protected).success proc new*( - T: type Manifest, - manifest: Manifest, - ecK, ecM: int): ?!Manifest = + T: type Manifest, + manifest: Manifest, + ecK, ecM: int +): ?!Manifest = ## Create an erasure protected dataset from an ## un-protected one ## @@ -223,5 +225,8 @@ proc new*( proc new*( T: type Manifest, data: openArray[byte], - decoder = ManifestContainers[$DagPBCodec]): ?!T = + decoder = ManifestContainers[$DagPBCodec] +): ?!Manifest = + ## Create a manifest instance from given data + ## Manifest.decode(data, decoder) diff --git a/codex/node.nim b/codex/node.nim index 1941e10b..7248a58c 100644 --- a/codex/node.nim +++ b/codex/node.nim @@ -60,18 +60,23 @@ type proc findPeer*( node: CodexNodeRef, - peerId: PeerId): Future[?PeerRecord] {.async.} = + peerId: PeerId +): Future[?PeerRecord] {.async.} = + ## Find peer using the discovery service from the given CodexNode + ## return await node.discovery.findPeer(peerId) proc connect*( - node: CodexNodeRef, - peerId: PeerId, - addrs: seq[MultiAddress]): Future[void] = + node: CodexNodeRef, + peerId: PeerId, + addrs: seq[MultiAddress] +): Future[void] = node.switch.connect(peerId, addrs) proc fetchManifest*( - node: CodexNodeRef, - cid: Cid): Future[?!Manifest] {.async.} = + node: CodexNodeRef, + cid: Cid +): Future[?!Manifest] {.async.} = ## Fetch and decode a manifest block ## @@ -93,10 +98,11 @@ proc fetchManifest*( return manifest.success proc fetchBatched*( - node: CodexNodeRef, - manifest: Manifest, - batchSize = FetchBatch, - onBatch: BatchProc = nil): Future[?!void] {.async, gcsafe.} = + node: CodexNodeRef, + manifest: Manifest, + batchSize = FetchBatch, + onBatch: BatchProc = nil +): Future[?!void] {.async, gcsafe.} = ## Fetch manifest in batches of `batchSize` ## @@ -122,8 +128,9 @@ proc fetchBatched*( return success() proc retrieve*( - node: CodexNodeRef, - cid: Cid): Future[?!LPStream] {.async.} = + node: CodexNodeRef, + cid: Cid +): Future[?!LPStream] {.async.} = ## Retrieve by Cid a single block or an entire dataset described by manifest ## @@ -175,9 +182,10 @@ proc retrieve*( return failure("Unable to retrieve Cid!") proc store*( - self: CodexNodeRef, - stream: LPStream, - blockSize = BlockSize): Future[?!Cid] {.async.} = + self: CodexNodeRef, + stream: LPStream, + blockSize = BlockSize +): Future[?!Cid] {.async.} = ## Save stream contents as dataset with given blockSize ## to nodes's BlockStore, and return Cid of its manifest ## @@ -238,15 +246,17 @@ proc store*( return manifest.cid.success -proc requestStorage*(self: CodexNodeRef, - cid: Cid, - duration: UInt256, - proofProbability: UInt256, - nodes: uint, - tolerance: uint, - reward: UInt256, - collateral: UInt256, - expiry = UInt256.none): Future[?!PurchaseId] {.async.} = +proc requestStorage*( + self: CodexNodeRef, + cid: Cid, + duration: UInt256, + proofProbability: UInt256, + nodes: uint, + tolerance: uint, + reward: UInt256, + collateral: UInt256, + expiry = UInt256.none +): Future[?!PurchaseId] {.async.} = ## Initiate a request for storage sequence, this might ## be a multistep procedure. ## @@ -311,14 +321,17 @@ proc requestStorage*(self: CodexNodeRef, return success purchase.id proc new*( - T: type CodexNodeRef, - switch: Switch, - store: BlockStore, - engine: BlockExcEngine, - erasure: Erasure, - discovery: Discovery, - contracts = Contracts.default): T = - T( + T: type CodexNodeRef, + switch: Switch, + store: BlockStore, + engine: BlockExcEngine, + erasure: Erasure, + discovery: Discovery, + contracts = Contracts.default +): CodexNodeRef = + ## Create new instance of a Codex node, call `start` to run it + ## + CodexNodeRef( switch: switch, blockStore: store, engine: engine, diff --git a/codex/purchasing.nim b/codex/purchasing.nim index 656f1d23..c7a5f7fe 100644 --- a/codex/purchasing.nim +++ b/codex/purchasing.nim @@ -47,7 +47,8 @@ proc stop*(purchasing: Purchasing) {.async.} = discard proc populate*(purchasing: Purchasing, - request: StorageRequest): Future[StorageRequest] {.async.} = + request: StorageRequest + ): Future[StorageRequest] {.async.} = result = request if result.ask.proofProbability == 0.u256: result.ask.proofProbability = purchasing.proofProbability @@ -60,7 +61,8 @@ proc populate*(purchasing: Purchasing, result.client = await purchasing.market.getSigner() proc purchase*(purchasing: Purchasing, - request: StorageRequest): Future[Purchase] {.async.} = + request: StorageRequest + ): Future[Purchase] {.async.} = let request = await purchasing.populate(request) let purchase = Purchase.new(request, purchasing.market, purchasing.clock) purchase.start() diff --git a/codex/purchasing/purchase.nim b/codex/purchasing/purchase.nim index 451ab37c..07498044 100644 --- a/codex/purchasing/purchase.nim +++ b/codex/purchasing/purchase.nim @@ -24,10 +24,14 @@ export Purchase export purchaseid export statemachine -func new*(_: type Purchase, - requestId: RequestId, - market: Market, - clock: Clock): Purchase = +func new*( + _: type Purchase, + requestId: RequestId, + market: Market, + clock: Clock +): Purchase = + ## create a new instance of a Purchase + ## Purchase( future: Future[void].new(), requestId: requestId, @@ -35,10 +39,13 @@ func new*(_: type Purchase, clock: clock ) -func new*(_: type Purchase, - request: StorageRequest, - market: Market, - clock: Clock): Purchase = +func new*( + _: type Purchase, + request: StorageRequest, + market: Market, + clock: Clock +): Purchase = + ## Create a new purchase using the given market and clock let purchase = Purchase.new(request.id, market, clock) purchase.request = some request return purchase diff --git a/codex/rest/json.nim b/codex/rest/json.nim index 200cb651..d5ad697d 100644 --- a/codex/rest/json.nim +++ b/codex/rest/json.nim @@ -18,7 +18,10 @@ type nodes*: ?uint tolerance*: ?uint -proc fromJson*(_: type Availability, bytes: seq[byte]): ?!Availability = +proc fromJson*( + _: type Availability, + bytes: seq[byte] +): ?!Availability = let json = ?catch parseJson(string.fromBytes(bytes)) let size = ?catch UInt256.fromDecimal(json["size"].getStr) let duration = ?catch UInt256.fromDecimal(json["duration"].getStr) @@ -26,8 +29,10 @@ proc fromJson*(_: type Availability, bytes: seq[byte]): ?!Availability = let maxCollateral = ?catch UInt256.fromDecimal(json["maxCollateral"].getStr) success Availability.init(size, duration, minPrice, maxCollateral) -proc fromJson*(_: type StorageRequestParams, - bytes: seq[byte]): ?! StorageRequestParams = +proc fromJson*( + _: type StorageRequestParams, + bytes: seq[byte] +): ?! StorageRequestParams = let json = ?catch parseJson(string.fromBytes(bytes)) let duration = ?catch UInt256.fromDecimal(json["duration"].getStr) let proofProbability = ?catch UInt256.fromDecimal(json["proofProbability"].getStr) diff --git a/codex/sales/reservations.nim b/codex/sales/reservations.nim index 91f21112..86aacd64 100644 --- a/codex/sales/reservations.nim +++ b/codex/sales/reservations.nim @@ -58,9 +58,8 @@ const SalesKey = (CodexMetaKey / "sales").tryGet # TODO: move to sales module ReservationsKey = (SalesKey / "reservations").tryGet -proc new*( - T: type Reservations, - repo: RepoStore): Reservations = +proc new*(T: type Reservations, + repo: RepoStore): Reservations = T(repo: repo) diff --git a/codex/storageproofs/por/por.nim b/codex/storageproofs/por/por.nim index d51a01c4..a4ead3c3 100644 --- a/codex/storageproofs/por/por.nim +++ b/codex/storageproofs/por/por.nim @@ -321,10 +321,11 @@ proc generateQuery*(tau: Tau, l: int): seq[QElement] = result.add(q) proc generateProof*( - stream: SeekableStream, - q: seq[QElement], - authenticators: seq[blst_p1], - s: int64): Future[Proof] {.async.} = + stream: SeekableStream, + q: seq[QElement], + authenticators: seq[blst_p1], + s: int64 +): Future[Proof] {.async.} = ## Generata BLS proofs for a given query ## @@ -432,11 +433,12 @@ proc verifyProof*( return verifyPairings(sum, self.spk.key, sigma, g) proc init*( - T: type PoR, - stream: SeekableStream, - ssk: SecretKey, - spk: PublicKey, - blockSize: int64): Future[PoR] {.async.} = + T: type PoR, + stream: SeekableStream, + ssk: SecretKey, + spk: PublicKey, + blockSize: int64 +): Future[PoR] {.async.} = ## Set up the POR scheme by generating tags and metadata ## diff --git a/codex/storageproofs/storageproofs.nim b/codex/storageproofs/storageproofs.nim index 179e7654..aec6532b 100644 --- a/codex/storageproofs/storageproofs.nim +++ b/codex/storageproofs/storageproofs.nim @@ -32,10 +32,11 @@ type stpStore*: StpStore proc upload*( - self: StorageProofs, - cid: Cid, - indexes: seq[int], - host: ca.Address): Future[?!void] {.async.} = + self: StorageProofs, + cid: Cid, + indexes: seq[int], + host: ca.Address +): Future[?!void] {.async.} = ## Upload authenticators ## @@ -56,8 +57,9 @@ proc upload*( # discard proc setupProofs*( - self: StorageProofs, - manifest: Manifest): Future[?!void] {.async.} = + self: StorageProofs, + manifest: Manifest +): Future[?!void] {.async.} = ## Setup storage authentication ## @@ -75,10 +77,11 @@ proc setupProofs*( return await self.stpStore.store(por.toMessage(), cid) proc init*( - T: type StorageProofs, - network: StpNetwork, - store: BlockStore, - stpStore: StpStore): StorageProofs = + T: type StorageProofs, + network: StpNetwork, + store: BlockStore, + stpStore: StpStore +): StorageProofs = var self = T( diff --git a/codex/storageproofs/stpnetwork.nim b/codex/storageproofs/stpnetwork.nim index e1a117a5..5b0d1be8 100644 --- a/codex/storageproofs/stpnetwork.nim +++ b/codex/storageproofs/stpnetwork.nim @@ -36,11 +36,12 @@ type tagsHandle*: TagsHandler proc uploadTags*( - self: StpNetwork, - cid: Cid, - indexes: seq[int], - tags: seq[seq[byte]], - host: ca.Address): Future[?!void] {.async.} = + self: StpNetwork, + cid: Cid, + indexes: seq[int], + tags: seq[seq[byte]], + host: ca.Address +): Future[?!void] {.async.} = # Upload tags to `host` # @@ -93,7 +94,9 @@ method init*(self: StpNetwork) = proc new*( T: type StpNetwork, switch: Switch, - discovery: Discovery): StpNetwork = + discovery: Discovery +): StpNetwork = + ## create a new StpNetwork instance let self = StpNetwork( switch: switch, diff --git a/codex/storageproofs/stpstore.nim b/codex/storageproofs/stpstore.nim index b4ab14bd..ff9ceb54 100644 --- a/codex/storageproofs/stpstore.nim +++ b/codex/storageproofs/stpstore.nim @@ -33,7 +33,8 @@ template stpPath*(self: StpStore, cid: Cid): string = proc retrieve*( self: StpStore, - cid: Cid): Future[?!PorMessage] {.async.} = + cid: Cid +): Future[?!PorMessage] {.async.} = ## Retrieve authenticators from data store ## @@ -51,7 +52,8 @@ proc retrieve*( proc store*( self: StpStore, por: PorMessage, - cid: Cid): Future[?!void] {.async.} = + cid: Cid +): Future[?!void] {.async.} = ## Persist storage proofs ## @@ -74,9 +76,10 @@ proc store*( return success() proc retrieve*( - self: StpStore, - cid: Cid, - blocks: seq[int]): Future[?!seq[Tag]] {.async.} = + self: StpStore, + cid: Cid, + blocks: seq[int] +): Future[?!seq[Tag]] {.async.} = var tags: seq[Tag] for b in blocks: var tag = Tag(idx: b) @@ -92,9 +95,10 @@ proc retrieve*( return tags.success proc store*( - self: StpStore, - tags: seq[Tag], - cid: Cid): Future[?!void] {.async.} = + self: StpStore, + tags: seq[Tag], + cid: Cid +): Future[?!void] {.async.} = let dir = self.stpPath(cid) @@ -117,7 +121,10 @@ proc store*( proc init*( T: type StpStore, authDir: string, - postfixLen: int = 2): StpStore = - T( + postfixLen: int = 2 +): StpStore = + ## Init StpStore + ## + StpStore( authDir: authDir, postfixLen: postfixLen) diff --git a/codex/stores/blockstore.nim b/codex/stores/blockstore.nim index cd01eedd..32ed91e8 100644 --- a/codex/stores/blockstore.nim +++ b/codex/stores/blockstore.nim @@ -45,9 +45,10 @@ method getBlock*(self: BlockStore, cid: Cid): Future[?!Block] {.base.} = raiseAssert("Not implemented!") method putBlock*( - self: BlockStore, - blk: Block, - ttl = Duration.none): Future[?!void] {.base.} = + self: BlockStore, + blk: Block, + ttl = Duration.none +): Future[?!void] {.base.} = ## Put a block to the blockstore ## diff --git a/codex/stores/cachestore.nim b/codex/stores/cachestore.nim index c7929ff0..4e875a40 100644 --- a/codex/stores/cachestore.nim +++ b/codex/stores/cachestore.nim @@ -79,8 +79,9 @@ func cids(self: CacheStore): (iterator: Cid {.gcsafe.}) = yield cid method listBlocks*( - self: CacheStore, - blockType = BlockType.Manifest): Future[?!BlocksIter] {.async.} = + self: CacheStore, + blockType = BlockType.Manifest +): Future[?!BlocksIter] {.async.} = ## Get the list of blocks in the BlockStore. This is an intensive operation ## @@ -193,7 +194,11 @@ func new*( blocks: openArray[Block] = [], cacheSize: Positive = DefaultCacheSize, # in bytes chunkSize: Positive = DefaultChunkSize # in bytes - ): CacheStore {.raises: [Defect, ValueError].} = +): CacheStore {.raises: [Defect, ValueError].} = + ## Create a new CacheStore instance + ## + ## `cacheSize` and `chunkSize` are both in bytes + ## if cacheSize < chunkSize: raise newException(ValueError, "cacheSize cannot be less than chunkSize") diff --git a/codex/stores/maintenance.nim b/codex/stores/maintenance.nim index e12edb84..ce06dafa 100644 --- a/codex/stores/maintenance.nim +++ b/codex/stores/maintenance.nim @@ -33,14 +33,19 @@ type numberOfBlocksPerInterval: int offset: int -proc new*(T: type BlockMaintainer, +proc new*( + T: type BlockMaintainer, repoStore: RepoStore, interval: Duration, numberOfBlocksPerInterval = 100, timer = Timer.new(), clock: Clock = SystemClock.new() - ): T = - T( +): BlockMaintainer = + ## Create new BlockMaintainer instance + ## + ## Call `start` to begin looking for for expired blocks + ## + BlockMaintainer( repoStore: repoStore, interval: interval, numberOfBlocksPerInterval: numberOfBlocksPerInterval, diff --git a/codex/stores/networkstore.nim b/codex/stores/networkstore.nim index a6e9a948..e31fbbd2 100644 --- a/codex/stores/networkstore.nim +++ b/codex/stores/networkstore.nim @@ -47,9 +47,10 @@ method getBlock*(self: NetworkStore, cid: Cid): Future[?!bt.Block] {.async.} = return success blk method putBlock*( - self: NetworkStore, - blk: bt.Block, - ttl = Duration.none): Future[?!void] {.async.} = + self: NetworkStore, + blk: bt.Block, + ttl = Duration.none +): Future[?!void] {.async.} = ## Store block locally and notify the network ## @@ -88,11 +89,10 @@ method close*(self: NetworkStore): Future[void] {.async.} = proc new*( T: type NetworkStore, engine: BlockExcEngine, - localStore: BlockStore): T = - - let - self = NetworkStore( + localStore: BlockStore +): NetworkStore = + ## Create new instance of a NetworkStore + ## + NetworkStore( localStore: localStore, engine: engine) - - return self diff --git a/codex/stores/repostore.nim b/codex/stores/repostore.nim index bc2b3798..6e2731a4 100644 --- a/codex/stores/repostore.nim +++ b/codex/stores/repostore.nim @@ -92,7 +92,13 @@ proc getBlockExpirationTimestamp(self: RepoStore, ttl: ?Duration): SecondsSince1 let duration = ttl |? self.blockTtl self.clock.now() + duration.seconds -proc getBlockExpirationEntry(self: RepoStore, batch: var seq[BatchEntry], cid: Cid, ttl: ?Duration): ?!BatchEntry = +proc getBlockExpirationEntry( + self: RepoStore, + batch: var seq[BatchEntry], + cid: Cid, + ttl: ?Duration +): ?!BatchEntry = + ## Get an expiration entry for a batch without key =? createBlockExpirationMetadataKey(cid), err: return failure(err) @@ -100,9 +106,10 @@ proc getBlockExpirationEntry(self: RepoStore, batch: var seq[BatchEntry], cid: C return success((key, value)) method putBlock*( - self: RepoStore, - blk: Block, - ttl = Duration.none): Future[?!void] {.async.} = + self: RepoStore, + blk: Block, + ttl = Duration.none +): Future[?!void] {.async.} = ## Put a block to the blockstore ## @@ -201,8 +208,9 @@ method hasBlock*(self: RepoStore, cid: Cid): Future[?!bool] {.async.} = return await self.repoDs.has(key) method listBlocks*( - self: RepoStore, - blockType = BlockType.Manifest): Future[?!BlocksIter] {.async.} = + self: RepoStore, + blockType = BlockType.Manifest +): Future[?!BlocksIter] {.async.} = ## Get the list of blocks in the RepoStore. ## This is an intensive operation ## @@ -237,7 +245,13 @@ proc createBlockExpirationQuery(maxNumber: int, offset: int): ?!Query = let queryKey = ? createBlockExpirationMetadataQueryKey() success Query.init(queryKey, offset = offset, limit = maxNumber) -method getBlockExpirations*(self: RepoStore, maxNumber: int, offset: int): Future[?!BlockExpirationIter] {.async, base.} = +method getBlockExpirations*( + self: RepoStore, + maxNumber: int, + offset: int +): Future[?!BlockExpirationIter] {.async, base.} = + ## Get block experiartions from the given RepoStore + ## without query =? createBlockExpirationQuery(maxNumber, offset), err: trace "Unable to format block expirations query" return failure(err) @@ -388,14 +402,17 @@ proc stop*(self: RepoStore): Future[void] {.async.} = self.started = false func new*( - T: type RepoStore, - repoDs: Datastore, - metaDs: Datastore, - clock: Clock = SystemClock.new(), - postFixLen = 2, - quotaMaxBytes = DefaultQuotaBytes, - blockTtl = DefaultBlockTtl): T = - T( + T: type RepoStore, + repoDs: Datastore, + metaDs: Datastore, + clock: Clock = SystemClock.new(), + postFixLen = 2, + quotaMaxBytes = DefaultQuotaBytes, + blockTtl = DefaultBlockTtl +): RepoStore = + ## Create new instance of a RepoStore + ## + RepoStore( repoDs: repoDs, metaDs: metaDs, clock: clock, diff --git a/codex/streams/asyncstreamwrapper.nim b/codex/streams/asyncstreamwrapper.nim index a0829b7b..f2491e8d 100644 --- a/codex/streams/asyncstreamwrapper.nim +++ b/codex/streams/asyncstreamwrapper.nim @@ -32,9 +32,12 @@ method initStream*(self: AsyncStreamWrapper) = procCall LPStream(self).initStream() proc new*( - C: type AsyncStreamWrapper, - reader: AsyncStreamReader = nil, - writer: AsyncStreamWriter = nil): AsyncStreamWrapper = + C: type AsyncStreamWrapper, + reader: AsyncStreamReader = nil, + writer: AsyncStreamWriter = nil +): AsyncStreamWrapper = + ## Create new instance of an asynchronous stream wrapper + ## let stream = C(reader: reader, writer: writer) @@ -57,9 +60,10 @@ template withExceptions(body: untyped) = raise newException(LPStreamError, exc.msg) method readOnce*( - self: AsyncStreamWrapper, - pbytes: pointer, - nbytes: int): Future[int] {.async.} = + self: AsyncStreamWrapper, + pbytes: pointer, + nbytes: int +): Future[int] {.async.} = trace "Reading bytes from reader", bytes = nbytes if isNil(self.reader): @@ -75,7 +79,8 @@ method readOnce*( proc completeWrite( self: AsyncStreamWrapper, fut: Future[void], - msgLen: int): Future[void] {.async.} = + msgLen: int +): Future[void] {.async.} = withExceptions: await fut diff --git a/codex/streams/storestream.nim b/codex/streams/storestream.nim index 6649d1bc..5b921109 100644 --- a/codex/streams/storestream.nim +++ b/codex/streams/storestream.nim @@ -38,12 +38,14 @@ type pad*: bool # Pad last block to manifest.blockSize? proc new*( - T: type StoreStream, - store: BlockStore, - manifest: Manifest, - pad = true): T = - - result = T( + T: type StoreStream, + store: BlockStore, + manifest: Manifest, + pad = true +): StoreStream = + ## Create a new StoreStream instance for a given store and manifest + ## + result = StoreStream( store: store, manifest: manifest, pad: pad, @@ -62,12 +64,14 @@ method atEof*(self: StoreStream): bool = self.offset >= self.size method readOnce*( - self: StoreStream, - pbytes: pointer, - nbytes: int): Future[int] {.async.} = + self: StoreStream, + pbytes: pointer, + nbytes: int +): Future[int] {.async.} = ## Read `nbytes` from current position in the StoreStream into output buffer pointed by `pbytes`. ## Return how many bytes were actually read before EOF was encountered. ## Raise exception if we are already at EOF. + ## trace "Reading from manifest", cid = self.manifest.cid.get(), blocks = self.manifest.len if self.atEof: diff --git a/codex/utils/addrutils.nim b/codex/utils/addrutils.nim index 6ae00e39..f044581a 100644 --- a/codex/utils/addrutils.nim +++ b/codex/utils/addrutils.nim @@ -17,9 +17,10 @@ import pkg/libp2p import pkg/stew/shims/net func remapAddr*( - address: MultiAddress, - ip: Option[ValidIpAddress] = ValidIpAddress.none, - port: Option[Port] = Port.none): MultiAddress = + address: MultiAddress, + ip: Option[ValidIpAddress] = ValidIpAddress.none, + port: Option[Port] = Port.none +): MultiAddress = ## Remap addresses to new IP and/or Port ## diff --git a/codex/utils/asyncheapqueue.nim b/codex/utils/asyncheapqueue.nim index 272c3158..17ca1f78 100644 --- a/codex/utils/asyncheapqueue.nim +++ b/codex/utils/asyncheapqueue.nim @@ -34,8 +34,9 @@ type Empty, Full proc newAsyncHeapQueue*[T]( - maxsize: int = 0, - queueType: QueueType = QueueType.Min): AsyncHeapQueue[T] = + maxsize: int = 0, + queueType: QueueType = QueueType.Min +): AsyncHeapQueue[T] = ## Creates a new asynchronous queue ``AsyncHeapQueue``. ## diff --git a/codex/utils/timer.nim b/codex/utils/timer.nim index 446570b6..4eefc599 100644 --- a/codex/utils/timer.nim +++ b/codex/utils/timer.nim @@ -22,10 +22,9 @@ type name: string loopFuture: Future[void] -proc new*(T: type Timer, timerName = "Unnamed Timer"): T = - T( - name: timerName - ) +proc new*(T: type Timer, timerName = "Unnamed Timer"): Timer = + ## Create a new Timer intance with the given name + Timer(name: timerName) proc timerLoop(timer: Timer) {.async.} = try: diff --git a/codex/validation.nim b/codex/validation.nim index 0431179b..a3f69296 100644 --- a/codex/validation.nim +++ b/codex/validation.nim @@ -22,10 +22,13 @@ type logScope: topics = "codex validator" -proc new*(_: type Validation, - clock: Clock, - market: Market, - maxSlots: int): Validation = +proc new*( + _: type Validation, + clock: Clock, + market: Market, + maxSlots: int +): Validation = + ## Create a new Validation instance Validation(clock: clock, market: market, maxSlots: maxSlots) proc slots*(validation: Validation): seq[SlotId] = diff --git a/tests/codex/helpers.nim b/tests/codex/helpers.nim index 306ecf8e..3ba48a15 100644 --- a/tests/codex/helpers.nim +++ b/tests/codex/helpers.nim @@ -31,9 +31,10 @@ proc lenPrefix*(msg: openArray[byte]): seq[byte] = return buf proc corruptBlocks*( - store: BlockStore, - manifest: Manifest, - blks, bytes: int): Future[seq[int]] {.async.} = + store: BlockStore, + manifest: Manifest, + blks, bytes: int +): Future[seq[int]] {.async.} = var pos: seq[int] doAssert blks < manifest.len diff --git a/tests/codex/helpers/mockclock.nim b/tests/codex/helpers/mockclock.nim index 38a3156d..ada449f9 100644 --- a/tests/codex/helpers/mockclock.nim +++ b/tests/codex/helpers/mockclock.nim @@ -12,8 +12,11 @@ type until: SecondsSince1970 future: Future[void] -func new*(_: type MockClock, - time: SecondsSince1970 = getTime().toUnix): MockClock = +func new*( + _: type MockClock, + time: SecondsSince1970 = getTime().toUnix +): MockClock = + ## Create a mock clock instance MockClock(time: time) proc set*(clock: MockClock, time: SecondsSince1970) = diff --git a/tests/codex/helpers/mockdiscovery.nim b/tests/codex/helpers/mockdiscovery.nim index 310d5ad2..5d5e8132 100644 --- a/tests/codex/helpers/mockdiscovery.nim +++ b/tests/codex/helpers/mockdiscovery.nim @@ -24,17 +24,21 @@ type publishHostProvideHandler*: proc(d: MockDiscovery, host: ca.Address): Future[void] {.gcsafe.} -proc new*(T: type MockDiscovery): T = - T() +proc new*(T: type MockDiscovery): MockDiscovery = + MockDiscovery() proc findPeer*( - d: Discovery, - peerId: PeerId): Future[?PeerRecord] {.async.} = + d: Discovery, + peerId: PeerId +): Future[?PeerRecord] {.async.} = + ## mock find a peer - always return none + ## return none(PeerRecord) method find*( - d: MockDiscovery, - cid: Cid): Future[seq[SignedPeerRecord]] {.async.} = + d: MockDiscovery, + cid: Cid +): Future[seq[SignedPeerRecord]] {.async.} = if isNil(d.findBlockProvidersHandler): return @@ -47,8 +51,9 @@ method provide*(d: MockDiscovery, cid: Cid): Future[void] {.async.} = await d.publishBlockProvideHandler(d, cid) method find*( - d: MockDiscovery, - host: ca.Address): Future[seq[SignedPeerRecord]] {.async.} = + d: MockDiscovery, + host: ca.Address +): Future[seq[SignedPeerRecord]] {.async.} = if isNil(d.findHostProvidersHandler): return diff --git a/tests/codex/helpers/mockmarket.nim b/tests/codex/helpers/mockmarket.nim index 5f89e9f6..6616ef57 100644 --- a/tests/codex/helpers/mockmarket.nim +++ b/tests/codex/helpers/mockmarket.nim @@ -82,6 +82,8 @@ proc hash*(requestId: RequestId): Hash = hash(requestId.toArray) proc new*(_: type MockMarket): MockMarket = + ## Create a new mocked Market instance + ## let config = MarketplaceConfig( collateral: CollateralConfig( repairRewardPercentage: 10, @@ -136,7 +138,7 @@ method getActiveSlot*( return none Slot method requestState*(market: MockMarket, - requestId: RequestId): Future[?RequestState] {.async.} = + requestId: RequestId): Future[?RequestState] {.async.} = return market.requestState.?[requestId] method slotState*(market: MockMarket, @@ -320,9 +322,9 @@ method subscribeSlotFreed*(market: MockMarket, return subscription method subscribeRequestCancelled*(market: MockMarket, - requestId: RequestId, - callback: OnRequestCancelled): - Future[Subscription] {.async.} = + requestId: RequestId, + callback: OnRequestCancelled): + Future[Subscription] {.async.} = let subscription = RequestCancelledSubscription( market: market, requestId: requestId, diff --git a/tests/codex/helpers/mocktimer.nim b/tests/codex/helpers/mocktimer.nim index 4b11f9cd..17eaca1e 100644 --- a/tests/codex/helpers/mocktimer.nim +++ b/tests/codex/helpers/mocktimer.nim @@ -18,13 +18,18 @@ type mockInterval*: Duration mockCallback: timer.TimerCallback -proc new*(T: type MockTimer): T = - T( +proc new*(T: type MockTimer): MockTimer = + ## Create a mocked Timer instance + MockTimer( startCalled: 0, stopCalled: 0 ) -method start*(mockTimer: MockTimer, callback: timer.TimerCallback, interval: Duration) = +method start*( + mockTimer: MockTimer, + callback: timer.TimerCallback, + interval: Duration +) = mockTimer.mockCallback = callback mockTimer.mockInterval = interval inc mockTimer.startCalled diff --git a/tests/codex/helpers/nodeutils.nim b/tests/codex/helpers/nodeutils.nim index 081de7f9..df4b470e 100644 --- a/tests/codex/helpers/nodeutils.nim +++ b/tests/codex/helpers/nodeutils.nim @@ -24,8 +24,9 @@ type networkStore: NetworkStore] proc generateNodes*( - num: Natural, - blocks: openArray[bt.Block] = []): seq[NodesComponents] = + num: Natural, + blocks: openArray[bt.Block] = [] +): seq[NodesComponents] = for i in 0..