2022-05-19 19:56:03 +00:00
|
|
|
## Nim-Codex
|
2021-02-26 00:23:22 +00:00
|
|
|
## Copyright (c) 2021 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-04-05 00:46:13 +00:00
|
|
|
import std/tables
|
2023-11-14 12:02:17 +00:00
|
|
|
import std/sugar
|
2023-12-22 12:04:01 +00:00
|
|
|
|
2022-04-05 00:46:13 +00:00
|
|
|
export tables
|
|
|
|
|
2022-03-18 22:17:51 +00:00
|
|
|
import pkg/upraises
|
|
|
|
|
|
|
|
push: {.upraises: [].}
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2023-11-14 12:02:17 +00:00
|
|
|
import pkg/libp2p/[cid, multicodec, multihash]
|
2021-08-30 19:25:20 +00:00
|
|
|
import pkg/stew/byteutils
|
2022-01-11 02:25:13 +00:00
|
|
|
import pkg/questionable
|
|
|
|
import pkg/questionable/results
|
|
|
|
|
2023-07-06 23:23:27 +00:00
|
|
|
import ./units
|
|
|
|
import ./utils
|
2022-01-11 02:25:13 +00:00
|
|
|
import ./errors
|
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 07:35:03 +00:00
|
|
|
import ./logutils
|
|
|
|
import ./utils/json
|
2023-12-22 12:04:01 +00:00
|
|
|
import ./codextypes
|
2021-02-26 00:23:22 +00:00
|
|
|
|
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 07:35:03 +00:00
|
|
|
export errors, logutils, units, codextypes
|
2023-12-21 06:41:43 +00:00
|
|
|
|
2022-07-28 00:39:17 +00:00
|
|
|
type
|
2024-07-18 21:04:33 +00:00
|
|
|
Block* = ref object of RootObj
|
2022-07-28 00:39:17 +00:00
|
|
|
cid*: Cid
|
|
|
|
data*: seq[byte]
|
|
|
|
|
2023-11-14 12:02:17 +00:00
|
|
|
BlockAddress* = object
|
|
|
|
case leaf*: bool
|
|
|
|
of true:
|
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 07:35:03 +00:00
|
|
|
treeCid* {.serialize.}: Cid
|
|
|
|
index* {.serialize.}: Natural
|
2023-11-14 12:02:17 +00:00
|
|
|
else:
|
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 07:35:03 +00:00
|
|
|
cid* {.serialize.}: Cid
|
|
|
|
|
|
|
|
logutils.formatIt(LogFormat.textLines, BlockAddress):
|
|
|
|
if it.leaf:
|
|
|
|
"treeCid: " & shortLog($it.treeCid) & ", index: " & $it.index
|
|
|
|
else:
|
|
|
|
"cid: " & shortLog($it.cid)
|
|
|
|
|
|
|
|
logutils.formatIt(LogFormat.json, BlockAddress): %it
|
2023-11-14 12:02:17 +00:00
|
|
|
|
|
|
|
proc `==`*(a, b: BlockAddress): bool =
|
|
|
|
a.leaf == b.leaf and
|
|
|
|
(
|
|
|
|
if a.leaf:
|
|
|
|
a.treeCid == b.treeCid and a.index == b.index
|
|
|
|
else:
|
|
|
|
a.cid == b.cid
|
|
|
|
)
|
|
|
|
|
|
|
|
proc `$`*(a: BlockAddress): string =
|
|
|
|
if a.leaf:
|
|
|
|
"treeCid: " & $a.treeCid & ", index: " & $a.index
|
|
|
|
else:
|
|
|
|
"cid: " & $a.cid
|
|
|
|
|
|
|
|
proc address*(b: Block): BlockAddress =
|
|
|
|
BlockAddress(leaf: false, cid: b.cid)
|
|
|
|
|
|
|
|
proc init*(_: type BlockAddress, cid: Cid): BlockAddress =
|
|
|
|
BlockAddress(leaf: false, cid: cid)
|
|
|
|
|
|
|
|
proc init*(_: type BlockAddress, treeCid: Cid, index: Natural): BlockAddress =
|
|
|
|
BlockAddress(leaf: true, treeCid: treeCid, index: index)
|
2022-04-05 00:46:13 +00:00
|
|
|
|
2021-02-26 00:23:22 +00:00
|
|
|
proc `$`*(b: Block): string =
|
|
|
|
result &= "cid: " & $b.cid
|
|
|
|
result &= "\ndata: " & string.fromBytes(b.data)
|
|
|
|
|
2022-03-18 19:50:53 +00:00
|
|
|
func new*(
|
2024-02-19 02:10:16 +00:00
|
|
|
T: type Block,
|
|
|
|
data: openArray[byte] = [],
|
|
|
|
version = CIDv1,
|
|
|
|
mcodec = Sha256HashCodec,
|
|
|
|
codec = BlockCodec): ?!Block =
|
2023-06-22 15:11:18 +00:00
|
|
|
## creates a new block for both storage and network IO
|
2023-11-14 17:52:27 +00:00
|
|
|
##
|
2022-01-11 02:25:13 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
hash = ? MultiHash.digest($mcodec, data).mapFailure
|
|
|
|
cid = ? Cid.init(version, codec, hash).mapFailure
|
|
|
|
|
2022-03-15 18:47:31 +00:00
|
|
|
# TODO: If the hash is `>=` to the data,
|
|
|
|
# use the Cid as a container!
|
|
|
|
Block(
|
2022-01-11 02:25:13 +00:00
|
|
|
cid: cid,
|
2022-03-15 18:47:31 +00:00
|
|
|
data: @data).success
|
2021-08-30 19:25:20 +00:00
|
|
|
|
2023-12-22 12:04:01 +00:00
|
|
|
proc new*(
|
2023-06-22 15:11:18 +00:00
|
|
|
T: type Block,
|
|
|
|
cid: Cid,
|
|
|
|
data: openArray[byte],
|
|
|
|
verify: bool = true
|
|
|
|
): ?!Block =
|
|
|
|
## creates a new block for both storage and network IO
|
2023-11-14 12:02:17 +00:00
|
|
|
##
|
|
|
|
|
|
|
|
if verify:
|
|
|
|
let
|
|
|
|
mhash = ? cid.mhash.mapFailure
|
|
|
|
computedMhash = ? MultiHash.digest($mhash.mcodec, data).mapFailure
|
|
|
|
computedCid = ? Cid.init(cid.cidver, cid.mcodec, computedMhash).mapFailure
|
|
|
|
if computedCid != cid:
|
|
|
|
return "Cid doesn't match the data".failure
|
|
|
|
|
|
|
|
return Block(
|
|
|
|
cid: cid,
|
|
|
|
data: @data
|
|
|
|
).success
|
|
|
|
|
|
|
|
proc emptyBlock*(version: CidVersion, hcodec: MultiCodec): ?!Block =
|
2023-12-22 12:04:01 +00:00
|
|
|
emptyCid(version, hcodec, BlockCodec)
|
2023-11-14 12:02:17 +00:00
|
|
|
.flatMap((cid: Cid) => Block.new(cid = cid, data = @[]))
|
|
|
|
|
|
|
|
proc emptyBlock*(cid: Cid): ?!Block =
|
2023-11-14 17:52:27 +00:00
|
|
|
cid.mhash.mapFailure.flatMap((mhash: MultiHash) =>
|
2023-11-14 12:02:17 +00:00
|
|
|
emptyBlock(cid.cidver, mhash.mcodec))
|
|
|
|
|
|
|
|
proc isEmpty*(cid: Cid): bool =
|
2023-11-14 17:52:27 +00:00
|
|
|
success(cid) == cid.mhash.mapFailure.flatMap((mhash: MultiHash) =>
|
2023-11-14 12:02:17 +00:00
|
|
|
emptyCid(cid.cidver, mhash.mcodec, cid.mcodec))
|
|
|
|
|
|
|
|
proc isEmpty*(blk: Block): bool =
|
|
|
|
blk.cid.isEmpty
|