2025-12-18 18:23:09 +01:00
|
|
|
## Logos Storage
|
2022-03-14 10:06:36 -06:00
|
|
|
## Copyright (c) 2022 Status Research & Development GmbH
|
|
|
|
|
## Licensed under either of
|
|
|
|
|
## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
|
|
|
|
## * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
|
|
|
|
## at your option.
|
|
|
|
|
## This file may not be copied, modified, or distributed except according to
|
|
|
|
|
## those terms.
|
|
|
|
|
|
2022-08-24 15:15:59 +03:00
|
|
|
# This module defines all operations on Manifest
|
|
|
|
|
|
2025-12-11 10:03:43 +01:00
|
|
|
{.push raises: [], gcsafe.}
|
2022-03-14 10:06:36 -06:00
|
|
|
|
|
|
|
|
import pkg/libp2p/protobuf/minprotobuf
|
2023-12-22 06:04:01 -06:00
|
|
|
import pkg/libp2p/[cid, multihash, multicodec]
|
2022-03-14 10:06:36 -06:00
|
|
|
import pkg/questionable/results
|
|
|
|
|
|
|
|
|
|
import ../errors
|
2022-08-24 15:15:59 +03:00
|
|
|
import ../utils
|
2023-11-09 09:47:09 +01:00
|
|
|
import ../utils/json
|
2023-07-06 16:23:27 -07:00
|
|
|
import ../units
|
2022-03-15 12:47:31 -06:00
|
|
|
import ../blocktype
|
2024-02-07 14:54:57 -06:00
|
|
|
import ../indexingstrategy
|
feat: create logging proxy (#663)
* implement a logging proxy
The logging proxy:
- prevents the need to import chronicles (as well as export except toJson),
- prevents the need to override `writeValue` or use or import nim-json-seralization elsewhere in the codebase, allowing for sole use of utils/json for de/serialization,
- and handles json formatting correctly in chronicles json sinks
* Rename logging -> logutils to avoid ambiguity with common names
* clean up
* add setProperty for JsonRecord, remove nim-json-serialization conflict
* Allow specifying textlines and json format separately
Not specifying a LogFormat will apply the formatting to both textlines and json sinks.
Specifying a LogFormat will apply the formatting to only that sink.
* remove unneeded usages of std/json
We only need to import utils/json instead of std/json
* move serialization from rest/json to utils/json so it can be shared
* fix NoColors ambiguity
Was causing unit tests to fail on Windows.
* Remove nre usage to fix Windows error
Windows was erroring with `could not load: pcre64.dll`. Instead of fixing that error, remove the pcre usage :)
* Add logutils module doc
* Shorten logutils.formatIt for `NBytes`
Both json and textlines formatIt were not needed, and could be combined into one formatIt
* remove debug integration test config
debug output and logformat of json for integration test logs
* Use ## module doc to support docgen
* bump nim-poseidon2 to export fromBytes
Before the changes in this branch, fromBytes was likely being resolved by nim-stew, or other dependency. With the changes in this branch, that dependency was removed and fromBytes could no longer be resolved. By exporting fromBytes from nim-poseidon, the correct resolution is now happening.
* fixes to get compiling after rebasing master
* Add support for Result types being logged using formatIt
2024-01-23 18:35:03 +11:00
|
|
|
import ../logutils
|
2023-07-06 16:23:27 -07:00
|
|
|
|
2024-02-07 14:54:57 -06:00
|
|
|
# TODO: Manifest should be reworked to more concrete types,
|
|
|
|
|
# perhaps using inheritance
|
2024-07-18 18:04:33 -03:00
|
|
|
type Manifest* = ref object of RootObj
|
2023-11-14 11:52:27 -06:00
|
|
|
treeCid {.serialize.}: Cid # Root of the merkle tree
|
2024-02-07 14:54:57 -06:00
|
|
|
datasetSize {.serialize.}: NBytes # Total size of all blocks
|
2023-11-14 11:52:27 -06:00
|
|
|
blockSize {.serialize.}: NBytes
|
|
|
|
|
# Size of each contained block (might not be needed if blocks are len-prefixed)
|
2023-12-21 00:41:43 -06:00
|
|
|
codec: MultiCodec # Dataset codec
|
2023-11-14 11:52:27 -06:00
|
|
|
hcodec: MultiCodec # Multihash codec
|
2023-12-21 00:41:43 -06:00
|
|
|
version: CidVersion # Cid version
|
2024-10-25 14:43:19 +01:00
|
|
|
filename {.serialize.}: ?string # The filename of the content uploaded (optional)
|
|
|
|
|
mimetype {.serialize.}: ?string # The mimetype of the content uploaded (optional)
|
2023-07-19 16:06:59 +02:00
|
|
|
|
|
|
|
|
############################################################
|
|
|
|
|
# Accessors
|
|
|
|
|
############################################################
|
|
|
|
|
|
2024-02-07 14:54:57 -06:00
|
|
|
func blockSize*(self: Manifest): NBytes =
|
2023-07-19 16:06:59 +02:00
|
|
|
self.blockSize
|
|
|
|
|
|
2024-02-07 14:54:57 -06:00
|
|
|
func datasetSize*(self: Manifest): NBytes =
|
2023-11-14 13:02:17 +01:00
|
|
|
self.datasetSize
|
2023-07-19 16:06:59 +02:00
|
|
|
|
2024-02-07 14:54:57 -06:00
|
|
|
func version*(self: Manifest): CidVersion =
|
2023-07-19 16:06:59 +02:00
|
|
|
self.version
|
|
|
|
|
|
2024-02-07 14:54:57 -06:00
|
|
|
func hcodec*(self: Manifest): MultiCodec =
|
2023-07-19 16:06:59 +02:00
|
|
|
self.hcodec
|
|
|
|
|
|
2024-02-07 14:54:57 -06:00
|
|
|
func codec*(self: Manifest): MultiCodec =
|
2023-07-19 16:06:59 +02:00
|
|
|
self.codec
|
|
|
|
|
|
2024-02-07 14:54:57 -06:00
|
|
|
func treeCid*(self: Manifest): Cid =
|
2023-11-14 13:02:17 +01:00
|
|
|
self.treeCid
|
2022-03-14 10:06:36 -06:00
|
|
|
|
2024-02-07 14:54:57 -06:00
|
|
|
func blocksCount*(self: Manifest): int =
|
2023-11-14 13:02:17 +01:00
|
|
|
divUp(self.datasetSize.int, self.blockSize.int)
|
2022-03-14 10:06:36 -06:00
|
|
|
|
2024-10-25 14:43:19 +01:00
|
|
|
func filename*(self: Manifest): ?string =
|
|
|
|
|
self.filename
|
|
|
|
|
|
|
|
|
|
func mimetype*(self: Manifest): ?string =
|
|
|
|
|
self.mimetype
|
|
|
|
|
|
2023-11-14 13:02:17 +01:00
|
|
|
############################################################
|
|
|
|
|
# Operations on block list
|
|
|
|
|
############################################################
|
2022-03-14 10:06:36 -06:00
|
|
|
|
2022-12-02 18:00:55 -06:00
|
|
|
func isManifest*(cid: Cid): ?!bool =
|
2023-12-22 06:04:01 -06:00
|
|
|
success (ManifestCodec == ?cid.contentType().mapFailure(CodexError))
|
2022-12-02 18:00:55 -06:00
|
|
|
|
|
|
|
|
func isManifest*(mc: MultiCodec): ?!bool =
|
2023-12-22 06:04:01 -06:00
|
|
|
success mc == ManifestCodec
|
2022-12-02 18:00:55 -06:00
|
|
|
|
2022-08-24 15:15:59 +03:00
|
|
|
############################################################
|
|
|
|
|
# Various sizes and verification
|
|
|
|
|
############################################################
|
|
|
|
|
|
2024-02-07 14:54:57 -06:00
|
|
|
func `==`*(a, b: Manifest): bool =
|
2023-11-14 13:02:17 +01:00
|
|
|
(a.treeCid == b.treeCid) and (a.datasetSize == b.datasetSize) and
|
|
|
|
|
(a.blockSize == b.blockSize) and (a.version == b.version) and (a.hcodec == b.hcodec) and
|
2025-12-17 15:20:48 +11:00
|
|
|
(a.codec == b.codec) and (a.filename == b.filename) and
|
|
|
|
|
(a.mimetype == b.mimetype)
|
2023-11-14 13:02:17 +01:00
|
|
|
|
2024-02-07 14:54:57 -06:00
|
|
|
func `$`*(self: Manifest): string =
|
2024-10-25 14:43:19 +01:00
|
|
|
result =
|
2023-11-14 13:02:17 +01:00
|
|
|
"treeCid: " & $self.treeCid & ", datasetSize: " & $self.datasetSize & ", blockSize: " &
|
|
|
|
|
$self.blockSize & ", version: " & $self.version & ", hcodec: " & $self.hcodec &
|
2025-12-17 15:20:48 +11:00
|
|
|
", codec: " & $self.codec
|
2024-10-25 14:43:19 +01:00
|
|
|
|
|
|
|
|
if self.filename.isSome:
|
|
|
|
|
result &= ", filename: " & $self.filename
|
|
|
|
|
|
|
|
|
|
if self.mimetype.isSome:
|
|
|
|
|
result &= ", mimetype: " & $self.mimetype
|
|
|
|
|
|
|
|
|
|
return result
|
2022-08-24 15:15:59 +03:00
|
|
|
|
|
|
|
|
############################################################
|
|
|
|
|
# Constructors
|
|
|
|
|
############################################################
|
|
|
|
|
|
2024-02-07 14:54:57 -06:00
|
|
|
func new*(
|
2023-12-21 00:41:43 -06:00
|
|
|
T: type Manifest,
|
|
|
|
|
treeCid: Cid,
|
|
|
|
|
blockSize: NBytes,
|
|
|
|
|
datasetSize: NBytes,
|
|
|
|
|
version: CidVersion = CIDv1,
|
2023-12-22 06:04:01 -06:00
|
|
|
hcodec = Sha256HashCodec,
|
|
|
|
|
codec = BlockCodec,
|
2024-10-25 14:43:19 +01:00
|
|
|
filename: ?string = string.none,
|
|
|
|
|
mimetype: ?string = string.none,
|
|
|
|
|
): Manifest =
|
2022-03-14 10:06:36 -06:00
|
|
|
T(
|
2023-11-14 13:02:17 +01:00
|
|
|
treeCid: treeCid,
|
|
|
|
|
blockSize: blockSize,
|
|
|
|
|
datasetSize: datasetSize,
|
2022-03-14 10:06:36 -06:00
|
|
|
version: version,
|
|
|
|
|
codec: codec,
|
|
|
|
|
hcodec: hcodec,
|
2024-10-25 14:43:19 +01:00
|
|
|
filename: filename,
|
|
|
|
|
mimetype: mimetype,
|
|
|
|
|
)
|
2023-12-12 09:11:54 +01:00
|
|
|
|
2024-02-07 14:54:57 -06:00
|
|
|
func new*(T: type Manifest, data: openArray[byte]): ?!Manifest =
|
|
|
|
|
## Create a manifest instance from given data
|
|
|
|
|
##
|
|
|
|
|
|
|
|
|
|
Manifest.decode(data)
|