From 2c7de912782b93ca69ecc7e769194eee74227c33 Mon Sep 17 00:00:00 2001 From: Bulat-Ziganshin Date: Thu, 13 Oct 2022 05:09:19 +0300 Subject: [PATCH] Get rid of copyAllScalarFields() --- codex/manifest/manifest.nim | 84 ++++++++++++++++--------------------- codex/manifest/types.nim | 1 + 2 files changed, 37 insertions(+), 48 deletions(-) diff --git a/codex/manifest/manifest.nim b/codex/manifest/manifest.nim index 10e3ea81..b1efe833 100644 --- a/codex/manifest/manifest.nim +++ b/codex/manifest/manifest.nim @@ -153,37 +153,6 @@ proc cid*(self: Manifest): ?!Cid = # Constructors ############################################################ -func copyAllScalarFields( - original: Manifest, - protected: bool - ): Manifest = - ## Sometimes we need to copy all but a few fields - ## from a manifest to another one. - ## It can be mplemented by copying all fields and then - ## making a few edits, but it is inefficient to copy - ## an entire `blocks` array only to drop it. - ## So we made a helper that copies all scalar fields, - ## i.e. all fields except for `blocks`. - ## - - var copy = Manifest( - rootHash: original.rootHash, - originalBytes: original.originalBytes, - blockSize: original.blockSize, - blocks: @[], - version: original.version, - hcodec: original.hcodec, - codec: original.codec, - protected: protected) - - if copy.protected and original.protected: - copy.K = original.K - copy.M = original.M - copy.originalCid = original.originalCid - copy.originalLen = original.originalLen - - return copy - proc new*( T: type Manifest, blocks: openArray[Cid] = [], @@ -207,32 +176,42 @@ proc new*( protected: protected).success proc protect*( - manifest: Manifest, + original: Manifest, K, M: int): ?!Manifest = ## Create an erasure protected dataset manifest from an unprotected one ## - ? manifest.verify() - if manifest.protected: + ? original.verify() + if original.protected: return failure newException(CodexError, "Trying to protect already protected manifest") - var self = copyAllScalarFields(manifest, protected = true) - self.K = K - self.M = M - self.originalCid = ? manifest.cid - self.originalLen = manifest.len + var self = Manifest( + # copy of original fields + rootHash: original.rootHash, + originalBytes: original.originalBytes, + blockSize: original.blockSize, + version: original.version, + hcodec: original.hcodec, + codec: original.codec, + # modified fields + protected: true, + blocks: @[], + K: K, + M: M, + originalCid: ? original.cid, + originalLen: original.len) let encodedLen = self.rounded + (self.steps * M) self.blocks = newSeq[Cid](encodedLen) # copy original manifest blocks for i in 0..