Compare commits
29 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 5ab164d229 | |||
| 26538c5884 | |||
| c202e2d598 | |||
| a7874add88 | |||
| 3d84be22e2 | |||
| 25766dc0ec | |||
| 676e93a372 | |||
| 5a6770e5e2 | |||
| 529e8d49ee | |||
| a94c035c0a | |||
| 24480418f0 | |||
| a46245ffe3 | |||
| 7c1c13e139 | |||
| 836a93cd96 | |||
| b47a742dd0 | |||
| 6e14882246 | |||
| a7466a0e02 | |||
| 78f45012db | |||
| f6a2f62ea9 | |||
| 3efeededc5 | |||
| c482350ec6 | |||
| f7a7a8072f | |||
| acd068e5ab | |||
| 8d5a41a790 | |||
| caa17d933c | |||
| 039b05cf7e | |||
| 37b10b59aa | |||
| a9ede22bbd | |||
| b656003306 |
@@ -15,7 +15,6 @@ jobs:
|
||||
runs-on: ${{ matrix.os }}
|
||||
env:
|
||||
CARGO_TERM_COLOR: always
|
||||
IPINFO_API_TOKEN: ${{ secrets.IPINFO_API_TOKEN }}
|
||||
continue-on-error: true
|
||||
steps:
|
||||
- name: Check out repository code
|
||||
|
||||
@@ -54,7 +54,3 @@ nym-network-monitor/__pycache__
|
||||
nym-network-monitor/*.key
|
||||
nym-network-monitor/.envrc
|
||||
nym-network-monitor/.envrc
|
||||
|
||||
|
||||
*.sqlite
|
||||
.build
|
||||
@@ -4,6 +4,91 @@ Post 1.0.0 release, the changelog format is based on [Keep a Changelog](https://
|
||||
|
||||
## [Unreleased]
|
||||
|
||||
## [2025.1-reeses] (2025-01-15)
|
||||
|
||||
- Feture/legacy alert ([#5346])
|
||||
- chore: readjusted --mode behaviour to fix the regression ([#5331])
|
||||
- chore: apply 1.84 linter suggestions ([#5330])
|
||||
- bugfix: make sure refresh data key matches bond info ([#5329])
|
||||
- reduce log severity for number of packets being delayed ([#5321])
|
||||
- feat: warn users if node is run in exit mode only ([#5320])
|
||||
- Bugfix/contract version assignment ([#5318])
|
||||
- fixed client session histogram buckets ([#5316])
|
||||
- amend 250gb limit ([#5313])
|
||||
- feature: expand nym-node prometheus metrics ([#5298])
|
||||
- Cherry picked #5286 ([#5287])
|
||||
- Add close to credential storage ([#5283])
|
||||
- feature: wireguard metrics ([#5278])
|
||||
- Add PATCH support to nym-http-api-client ([#5260])
|
||||
- chore: removed legacy socks5 listener ([#5259])
|
||||
- bugfix: make sure to apply gateway score filtering when choosing initial node ([#5256])
|
||||
- Update TS bindings ([#5255])
|
||||
- Add conversion unit tests for auth msg ([#5251])
|
||||
- Add control messages to GatewayTransciver ([#5247])
|
||||
- Remove unneeded async function annotation ([#5246])
|
||||
- bugfix: make sure to update timestamp of last batch verification to prevent double redemption ([#5239])
|
||||
- Add FromStr impl for UserAgent ([#5236])
|
||||
- Extend swagger docs ([#5235])
|
||||
- TicketType derive Hash and Eq ([#5233])
|
||||
- Add fd callback to client core ([#5230])
|
||||
- Extend raw ws fd for gateway client ([#5218])
|
||||
- Shipping raw metrics to PG ([#5216])
|
||||
- Change sqlite journal mode to WAL ([#5213])
|
||||
- Derive serialize for UserAgent ([#5210])
|
||||
- Restore Location fields ([#5208])
|
||||
- better date serialization ([#5207])
|
||||
- Fix overflow ([#5204])
|
||||
- feature: hopefully final steps of the smoosh™️ ([#5201])
|
||||
- Fix overflow ([#5184])
|
||||
- NS API - Gateway stats scraping ([#5180])
|
||||
- introduced initial internal commands for nym-cli: ecash key and request generation ([#5174])
|
||||
- Move NS client to separate package under NS API ([#5171])
|
||||
- build(deps): bump micromatch from 4.0.4 to 4.0.8 in /testnet-faucet ([#4813])
|
||||
|
||||
[#5346]: https://github.com/nymtech/nym/pull/5346
|
||||
[#5331]: https://github.com/nymtech/nym/pull/5331
|
||||
[#5330]: https://github.com/nymtech/nym/pull/5330
|
||||
[#5329]: https://github.com/nymtech/nym/pull/5329
|
||||
[#5321]: https://github.com/nymtech/nym/pull/5321
|
||||
[#5320]: https://github.com/nymtech/nym/pull/5320
|
||||
[#5318]: https://github.com/nymtech/nym/pull/5318
|
||||
[#5316]: https://github.com/nymtech/nym/pull/5316
|
||||
[#5313]: https://github.com/nymtech/nym/pull/5313
|
||||
[#5298]: https://github.com/nymtech/nym/pull/5298
|
||||
[#5287]: https://github.com/nymtech/nym/pull/5287
|
||||
[#5283]: https://github.com/nymtech/nym/pull/5283
|
||||
[#5278]: https://github.com/nymtech/nym/pull/5278
|
||||
[#5260]: https://github.com/nymtech/nym/pull/5260
|
||||
[#5259]: https://github.com/nymtech/nym/pull/5259
|
||||
[#5256]: https://github.com/nymtech/nym/pull/5256
|
||||
[#5255]: https://github.com/nymtech/nym/pull/5255
|
||||
[#5251]: https://github.com/nymtech/nym/pull/5251
|
||||
[#5247]: https://github.com/nymtech/nym/pull/5247
|
||||
[#5246]: https://github.com/nymtech/nym/pull/5246
|
||||
[#5239]: https://github.com/nymtech/nym/pull/5239
|
||||
[#5236]: https://github.com/nymtech/nym/pull/5236
|
||||
[#5235]: https://github.com/nymtech/nym/pull/5235
|
||||
[#5233]: https://github.com/nymtech/nym/pull/5233
|
||||
[#5230]: https://github.com/nymtech/nym/pull/5230
|
||||
[#5218]: https://github.com/nymtech/nym/pull/5218
|
||||
[#5216]: https://github.com/nymtech/nym/pull/5216
|
||||
[#5213]: https://github.com/nymtech/nym/pull/5213
|
||||
[#5210]: https://github.com/nymtech/nym/pull/5210
|
||||
[#5208]: https://github.com/nymtech/nym/pull/5208
|
||||
[#5207]: https://github.com/nymtech/nym/pull/5207
|
||||
[#5204]: https://github.com/nymtech/nym/pull/5204
|
||||
[#5201]: https://github.com/nymtech/nym/pull/5201
|
||||
[#5184]: https://github.com/nymtech/nym/pull/5184
|
||||
[#5180]: https://github.com/nymtech/nym/pull/5180
|
||||
[#5174]: https://github.com/nymtech/nym/pull/5174
|
||||
[#5171]: https://github.com/nymtech/nym/pull/5171
|
||||
[#4813]: https://github.com/nymtech/nym/pull/4813
|
||||
|
||||
## [2024.14-crunch-patched] (2024-12-17)
|
||||
|
||||
- Fixes an issue to allow previously registred clients to connect to latest nym-nodes
|
||||
- Fixes compatibility issues between nym-nodes and older clients
|
||||
|
||||
## [2024.14-crunch] (2024-12-11)
|
||||
|
||||
- Merge/release/2024.14-crunch ([#5242])
|
||||
|
||||
Generated
+58
-60
@@ -2416,7 +2416,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "explorer-api"
|
||||
version = "1.1.43"
|
||||
version = "1.1.44"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"clap 4.5.20",
|
||||
@@ -2483,9 +2483,9 @@ checksum = "4443176a9f2c162692bd3d352d745ef9413eec5782a80d8fd6f8a1ac692a07f7"
|
||||
|
||||
[[package]]
|
||||
name = "fancy_constructor"
|
||||
version = "1.2.2"
|
||||
version = "1.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f71f317e4af73b2f8f608fac190c52eac4b1879d2145df1db2fe48881ca69435"
|
||||
checksum = "07b19d0e43eae2bfbafe4931b5e79c73fb1a849ca15cd41a761a7b8587f9a1a2"
|
||||
dependencies = [
|
||||
"macroific",
|
||||
"proc-macro2",
|
||||
@@ -2846,15 +2846,15 @@ checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b"
|
||||
|
||||
[[package]]
|
||||
name = "gloo-net"
|
||||
version = "0.5.0"
|
||||
version = "0.6.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "43aaa242d1239a8822c15c645f02166398da4f8b5c4bae795c1f5b44e9eee173"
|
||||
checksum = "c06f627b1a58ca3d42b45d6104bf1e1a03799df472df00988b6ba21accc10580"
|
||||
dependencies = [
|
||||
"futures-channel",
|
||||
"futures-core",
|
||||
"futures-sink",
|
||||
"gloo-utils 0.2.0",
|
||||
"http 0.2.12",
|
||||
"http 1.1.0",
|
||||
"js-sys",
|
||||
"pin-project",
|
||||
"serde",
|
||||
@@ -3432,7 +3432,8 @@ checksum = "ce23b50ad8242c51a442f3ff322d56b02f08852c77e4c0b4d3fd684abc89c683"
|
||||
[[package]]
|
||||
name = "indexed_db_futures"
|
||||
version = "0.4.2"
|
||||
source = "git+https://github.com/TiemenSch/rust-indexed-db?branch=update-uuid#9745d015707008b0c410115d787014a6d1af2efb"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0704b71f13f81b5933d791abf2de26b33c40935143985220299a357721166706"
|
||||
dependencies = [
|
||||
"accessory",
|
||||
"cfg-if",
|
||||
@@ -3670,10 +3671,11 @@ checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b"
|
||||
|
||||
[[package]]
|
||||
name = "js-sys"
|
||||
version = "0.3.72"
|
||||
version = "0.3.76"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6a88f1bda2bd75b0452a14784937d796722fdebfe50df998aeb3f0b7603019a9"
|
||||
checksum = "6717b6b5b077764fb5966237269cb3c64edddde4b14ce42647430a78ced9e7b7"
|
||||
dependencies = [
|
||||
"once_cell",
|
||||
"wasm-bindgen",
|
||||
]
|
||||
|
||||
@@ -4393,7 +4395,7 @@ checksum = "830b246a0e5f20af87141b25c173cd1b609bd7779a4617d6ec582abaf90870f3"
|
||||
|
||||
[[package]]
|
||||
name = "nym-api"
|
||||
version = "1.1.47"
|
||||
version = "1.1.48"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"async-trait",
|
||||
@@ -4642,7 +4644,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "nym-cli"
|
||||
version = "1.1.45"
|
||||
version = "1.1.46"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"base64 0.22.1",
|
||||
@@ -4725,7 +4727,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "nym-client"
|
||||
version = "1.1.45"
|
||||
version = "1.1.46"
|
||||
dependencies = [
|
||||
"bs58",
|
||||
"clap 4.5.20",
|
||||
@@ -4984,7 +4986,6 @@ dependencies = [
|
||||
"log",
|
||||
"nym-network-defaults",
|
||||
"serde",
|
||||
"thiserror",
|
||||
"toml 0.8.14",
|
||||
"url",
|
||||
]
|
||||
@@ -5028,7 +5029,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "nym-credential-proxy"
|
||||
version = "0.1.7"
|
||||
version = "0.1.6"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"async-trait",
|
||||
@@ -5222,6 +5223,31 @@ dependencies = [
|
||||
"zeroize",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "nym-data-observatory"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"axum 0.7.7",
|
||||
"chrono",
|
||||
"clap 4.5.20",
|
||||
"nym-bin-common",
|
||||
"nym-network-defaults",
|
||||
"nym-node-requests",
|
||||
"nym-task",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"sqlx",
|
||||
"tokio",
|
||||
"tokio-util",
|
||||
"tower-http",
|
||||
"tracing",
|
||||
"tracing-subscriber",
|
||||
"utoipa",
|
||||
"utoipa-swagger-ui",
|
||||
"utoipauto",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "nym-dkg"
|
||||
version = "0.1.0"
|
||||
@@ -5655,18 +5681,20 @@ version = "0.1.0"
|
||||
dependencies = [
|
||||
"dashmap",
|
||||
"lazy_static",
|
||||
"log",
|
||||
"prometheus",
|
||||
"tracing",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "nym-mixnet-client"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"dashmap",
|
||||
"futures",
|
||||
"nym-sphinx",
|
||||
"nym-task",
|
||||
"tokio",
|
||||
"tokio-stream",
|
||||
"tokio-util",
|
||||
"tracing",
|
||||
]
|
||||
@@ -5786,7 +5814,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "nym-network-requester"
|
||||
version = "1.1.46"
|
||||
version = "1.1.47"
|
||||
dependencies = [
|
||||
"addr",
|
||||
"anyhow",
|
||||
@@ -5837,7 +5865,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "nym-node"
|
||||
version = "1.2.0"
|
||||
version = "1.3.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"async-trait",
|
||||
@@ -6209,7 +6237,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "nym-socks5-client"
|
||||
version = "1.1.45"
|
||||
version = "1.1.46"
|
||||
dependencies = [
|
||||
"bs58",
|
||||
"clap 4.5.20",
|
||||
@@ -6535,7 +6563,10 @@ name = "nym-topology"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"async-trait",
|
||||
"bs58",
|
||||
"log",
|
||||
"nym-api-requests",
|
||||
"nym-bin-common",
|
||||
"nym-config",
|
||||
"nym-crypto",
|
||||
"nym-mixnet-contract-common",
|
||||
@@ -6544,10 +6575,10 @@ dependencies = [
|
||||
"nym-sphinx-types",
|
||||
"rand",
|
||||
"reqwest 0.12.4",
|
||||
"semver 1.0.23",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"thiserror",
|
||||
"tracing",
|
||||
"tsify",
|
||||
"wasm-bindgen",
|
||||
"wasm-utils",
|
||||
@@ -6808,7 +6839,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "nymvisor"
|
||||
version = "0.1.10"
|
||||
version = "0.1.11"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"bytes",
|
||||
@@ -6836,40 +6867,6 @@ dependencies = [
|
||||
"url",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "nyx-chain-watcher"
|
||||
version = "0.1.8"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"async-trait",
|
||||
"axum 0.7.7",
|
||||
"chrono",
|
||||
"clap 4.5.20",
|
||||
"nym-bin-common",
|
||||
"nym-config",
|
||||
"nym-network-defaults",
|
||||
"nym-node-requests",
|
||||
"nym-task",
|
||||
"nym-validator-client",
|
||||
"nyxd-scraper",
|
||||
"reqwest 0.12.4",
|
||||
"rocket",
|
||||
"schemars",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"sqlx",
|
||||
"thiserror",
|
||||
"time",
|
||||
"tokio",
|
||||
"tokio-util",
|
||||
"tower-http",
|
||||
"tracing",
|
||||
"tracing-subscriber",
|
||||
"utoipa",
|
||||
"utoipa-swagger-ui",
|
||||
"utoipauto",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "nyxd-scraper"
|
||||
version = "0.1.0"
|
||||
@@ -10792,12 +10789,13 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-futures"
|
||||
version = "0.4.45"
|
||||
version = "0.4.49"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cc7ec4f8827a71586374db3e87abdb5a2bb3a15afed140221307c3ec06b1f63b"
|
||||
checksum = "38176d9b44ea84e9184eff0bc34cc167ed044f816accfe5922e54d84cf48eca2"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"js-sys",
|
||||
"once_cell",
|
||||
"wasm-bindgen",
|
||||
"web-sys",
|
||||
]
|
||||
@@ -10937,9 +10935,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "wasmtimer"
|
||||
version = "0.2.0"
|
||||
version = "0.4.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5f656cd8858a5164932d8a90f936700860976ec21eb00e0fe2aa8cab13f6b4cf"
|
||||
checksum = "0048ad49a55b9deb3953841fa1fc5858f0efbcb7a18868c899a360269fac1b23"
|
||||
dependencies = [
|
||||
"futures",
|
||||
"js-sys",
|
||||
@@ -10951,9 +10949,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "web-sys"
|
||||
version = "0.3.72"
|
||||
version = "0.3.76"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f6488b90108c040df0fe62fa815cbdee25124641df01814dd7282749234c6112"
|
||||
checksum = "04dd7223427d52553d3702c004d3b2fe07c148165faa56313cb00211e31c12bc"
|
||||
dependencies = [
|
||||
"js-sys",
|
||||
"wasm-bindgen",
|
||||
|
||||
+10
-14
@@ -118,8 +118,8 @@ members = [
|
||||
"nym-credential-proxy/nym-credential-proxy",
|
||||
"nym-credential-proxy/nym-credential-proxy-requests",
|
||||
"nym-credential-proxy/vpn-api-lib-wasm",
|
||||
"nym-data-observatory",
|
||||
"nym-network-monitor",
|
||||
"nyx-chain-watcher",
|
||||
"nym-node",
|
||||
"nym-node/nym-node-requests",
|
||||
"nym-node/nym-node-metrics",
|
||||
@@ -147,9 +147,7 @@ members = [
|
||||
"tools/internal/contract-state-importer/importer-cli",
|
||||
"tools/internal/contract-state-importer/importer-contract",
|
||||
"tools/internal/testnet-manager",
|
||||
"tools/internal/testnet-manager/dkg-bypass-contract",
|
||||
"common/verloc",
|
||||
"tools/internal/mixnet-connectivity-check",
|
||||
"tools/internal/testnet-manager/dkg-bypass-contract", "common/verloc", "tools/internal/mixnet-connectivity-check",
|
||||
]
|
||||
|
||||
default-members = [
|
||||
@@ -158,11 +156,11 @@ default-members = [
|
||||
"explorer-api",
|
||||
"nym-api",
|
||||
"nym-credential-proxy/nym-credential-proxy",
|
||||
"nym-data-observatory",
|
||||
"nym-node",
|
||||
"nym-node-status-api/nym-node-status-agent",
|
||||
"nym-node-status-api/nym-node-status-api",
|
||||
"nym-validator-rewarder",
|
||||
"nyx-chain-watcher",
|
||||
"service-providers/authenticator",
|
||||
"service-providers/ip-packet-router",
|
||||
"service-providers/network-requester",
|
||||
@@ -396,19 +394,17 @@ prost = { version = "0.12", default-features = false }
|
||||
|
||||
# wasm-related dependencies
|
||||
gloo-utils = "0.2.0"
|
||||
gloo-net = "0.5.0"
|
||||
gloo-net = "0.6.0"
|
||||
|
||||
# use a separate branch due to feature unification failures
|
||||
# this is blocked until the upstream removes outdates `wasm_bindgen` feature usage
|
||||
# indexed_db_futures = "0.4.1"
|
||||
indexed_db_futures = { git = "https://github.com/TiemenSch/rust-indexed-db", branch = "update-uuid" }
|
||||
js-sys = "0.3.70"
|
||||
# TODO: migrate to 0.6+
|
||||
indexed_db_futures = "0.4.2"
|
||||
js-sys = "0.3.76"
|
||||
serde-wasm-bindgen = "0.6.5"
|
||||
tsify = "0.4.5"
|
||||
wasm-bindgen = "0.2.99"
|
||||
wasm-bindgen-futures = "0.4.45"
|
||||
wasmtimer = "0.2.0"
|
||||
web-sys = "0.3.72"
|
||||
wasm-bindgen-futures = "0.4.49"
|
||||
wasmtimer = "0.4.1"
|
||||
web-sys = "0.3.76"
|
||||
|
||||
# Profile settings for individual crates
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "nym-client"
|
||||
version = "1.1.45"
|
||||
version = "1.1.46"
|
||||
authors = ["Dave Hrycyszyn <futurechimp@users.noreply.github.com>", "Jędrzej Stuczyński <andrew@nymtech.net>"]
|
||||
description = "Implementation of the Nym Client"
|
||||
edition = "2021"
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "nym-socks5-client"
|
||||
version = "1.1.45"
|
||||
version = "1.1.46"
|
||||
authors = ["Dave Hrycyszyn <futurechimp@users.noreply.github.com>"]
|
||||
description = "A SOCKS5 localhost proxy that converts incoming messages to Sphinx and sends them to a Nym address"
|
||||
edition = "2021"
|
||||
|
||||
@@ -28,7 +28,7 @@ pub type HmacSha256 = Hmac<Sha256>;
|
||||
pub type Nonce = u64;
|
||||
pub type Taken = Option<SystemTime>;
|
||||
|
||||
pub const BANDWIDTH_CAP_PER_DAY: u64 = 1024 * 1024 * 1024; // 1 GB
|
||||
pub const BANDWIDTH_CAP_PER_DAY: u64 = 250 * 1024 * 1024 * 1024; // 250 GB
|
||||
|
||||
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
||||
pub struct IpPair {
|
||||
|
||||
@@ -3,7 +3,7 @@ name = "nym-client-core"
|
||||
version = "1.1.15"
|
||||
authors = ["Dave Hrycyszyn <futurechimp@users.noreply.github.com>"]
|
||||
edition = "2021"
|
||||
rust-version = "1.76"
|
||||
rust-version = "1.70"
|
||||
license.workspace = true
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
@@ -45,7 +45,7 @@ nym-nonexhaustive-delayqueue = { path = "../nonexhaustive-delayqueue" }
|
||||
nym-sphinx = { path = "../nymsphinx" }
|
||||
nym-statistics-common = { path = "../statistics" }
|
||||
nym-pemstore = { path = "../pemstore" }
|
||||
nym-topology = { path = "../topology", features = ["persistence"] }
|
||||
nym-topology = { path = "../topology", features = ["serializable"] }
|
||||
nym-mixnet-client = { path = "../client-libs/mixnet-client", default-features = false }
|
||||
nym-validator-client = { path = "../client-libs/validator-client", default-features = false }
|
||||
nym-task = { path = "../task" }
|
||||
|
||||
@@ -550,15 +550,6 @@ pub struct Topology {
|
||||
/// Specifies a minimum performance of a gateway that is used on route construction.
|
||||
/// This setting is only applicable when `NymApi` topology is used.
|
||||
pub minimum_gateway_performance: u8,
|
||||
|
||||
/// Specifies whether this client should attempt to retrieve all available network nodes
|
||||
/// as opposed to just active mixnodes/gateways.
|
||||
/// Useless without `ignore_epoch_roles = true`
|
||||
pub use_extended_topology: bool,
|
||||
|
||||
/// Specifies whether this client should ignore the current epoch role of the target egress node
|
||||
/// when constructing the final hop packets.
|
||||
pub ignore_egress_epoch_role: bool,
|
||||
}
|
||||
|
||||
#[allow(clippy::large_enum_variant)]
|
||||
@@ -595,8 +586,6 @@ impl Default for Topology {
|
||||
topology_structure: TopologyStructure::default(),
|
||||
minimum_mixnode_performance: DEFAULT_MIN_MIXNODE_PERFORMANCE,
|
||||
minimum_gateway_performance: DEFAULT_MIN_GATEWAY_PERFORMANCE,
|
||||
use_extended_topology: false,
|
||||
ignore_egress_epoch_role: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -112,7 +112,7 @@ where
|
||||
source,
|
||||
}
|
||||
})?;
|
||||
hardcoded_topology.entry_capable_nodes().cloned().collect()
|
||||
hardcoded_topology.get_gateways()
|
||||
} else {
|
||||
let mut rng = rand::thread_rng();
|
||||
crate::init::helpers::current_gateways(
|
||||
@@ -128,7 +128,7 @@ where
|
||||
// make sure the list of available gateways doesn't overlap the list of known gateways
|
||||
let available_gateways = available_gateways
|
||||
.into_iter()
|
||||
.filter(|g| !registered_gateways.contains(&g.identity()))
|
||||
.filter(|g| !registered_gateways.contains(g.identity()))
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
if available_gateways.is_empty() {
|
||||
|
||||
@@ -167,7 +167,7 @@ where
|
||||
source,
|
||||
}
|
||||
})?;
|
||||
hardcoded_topology.entry_capable_nodes().cloned().collect()
|
||||
hardcoded_topology.get_gateways()
|
||||
} else {
|
||||
let mut rng = rand::thread_rng();
|
||||
crate::init::helpers::current_gateways(
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
use super::received_buffer::ReceivedBufferMessage;
|
||||
use super::statistics_control::StatisticsControl;
|
||||
use super::topology_control::geo_aware_provider::GeoAwareTopologyProvider;
|
||||
use crate::client::base_client::storage::helpers::store_client_keys;
|
||||
use crate::client::base_client::storage::MixnetClientStorage;
|
||||
use crate::client::cover_traffic_stream::LoopCoverTrafficStream;
|
||||
@@ -23,7 +24,7 @@ use crate::client::replies::reply_storage::{
|
||||
};
|
||||
use crate::client::topology_control::nym_api_provider::NymApiTopologyProvider;
|
||||
use crate::client::topology_control::{
|
||||
TopologyAccessor, TopologyRefresher, TopologyRefresherConfig,
|
||||
nym_api_provider, TopologyAccessor, TopologyRefresher, TopologyRefresherConfig,
|
||||
};
|
||||
use crate::config::{Config, DebugConfig};
|
||||
use crate::error::ClientCoreError;
|
||||
@@ -463,8 +464,8 @@ where
|
||||
details_store
|
||||
.upgrade_stored_remote_gateway_key(gateway_client.gateway_identity(), &updated_key)
|
||||
.await.map_err(|err| {
|
||||
error!("failed to store upgraded gateway key! this connection might be forever broken now: {err}");
|
||||
ClientCoreError::GatewaysDetailsStoreError { source: Box::new(err) }
|
||||
error!("failed to store upgraded gateway key! this connection might be forever broken now: {err}");
|
||||
ClientCoreError::GatewaysDetailsStoreError { source: Box::new(err) }
|
||||
})?
|
||||
}
|
||||
|
||||
@@ -538,15 +539,15 @@ where
|
||||
// if no custom provider was ... provided ..., create one using nym-api
|
||||
custom_provider.unwrap_or_else(|| match config_topology.topology_structure {
|
||||
config::TopologyStructure::NymApi => Box::new(NymApiTopologyProvider::new(
|
||||
config_topology,
|
||||
nym_api_provider::Config {
|
||||
min_mixnode_performance: config_topology.minimum_mixnode_performance,
|
||||
min_gateway_performance: config_topology.minimum_gateway_performance,
|
||||
},
|
||||
nym_api_urls,
|
||||
user_agent,
|
||||
)),
|
||||
config::TopologyStructure::GeoAware(group_by) => {
|
||||
warn!("using deprecated 'GeoAware' topology provider - this option will be removed very soon");
|
||||
|
||||
#[allow(deprecated)]
|
||||
Box::new(crate::client::topology_control::GeoAwareTopologyProvider::new(nym_api_urls, group_by))
|
||||
Box::new(GeoAwareTopologyProvider::new(nym_api_urls, group_by))
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -557,7 +558,7 @@ where
|
||||
topology_provider: Box<dyn TopologyProvider + Send + Sync>,
|
||||
topology_config: config::Topology,
|
||||
topology_accessor: TopologyAccessor,
|
||||
local_gateway: NodeIdentity,
|
||||
local_gateway: &NodeIdentity,
|
||||
wait_for_gateway: bool,
|
||||
mut shutdown: TaskClient,
|
||||
) -> Result<(), ClientCoreError> {
|
||||
@@ -589,7 +590,7 @@ where
|
||||
};
|
||||
|
||||
if let Err(err) = topology_refresher
|
||||
.ensure_contains_routable_egress(local_gateway)
|
||||
.ensure_contains_gateway(local_gateway)
|
||||
.await
|
||||
{
|
||||
if let Some(waiting_timeout) = gateway_wait_timeout {
|
||||
@@ -739,8 +740,7 @@ where
|
||||
|
||||
// channels responsible for controlling ack messages
|
||||
let (ack_sender, ack_receiver) = mpsc::unbounded();
|
||||
let shared_topology_accessor =
|
||||
TopologyAccessor::new(self.config.debug.topology.ignore_egress_epoch_role);
|
||||
let shared_topology_accessor = TopologyAccessor::new();
|
||||
|
||||
// Shutdown notifier for signalling tasks to stop
|
||||
let shutdown = self
|
||||
|
||||
@@ -163,7 +163,6 @@ impl LoopCoverTrafficStream<OsRng> {
|
||||
// poisson delay, but is it really a problem?
|
||||
let topology_permit = self.topology_access.get_read_permit().await;
|
||||
// the ack is sent back to ourselves (and then ignored)
|
||||
|
||||
let topology_ref = match topology_permit.try_get_valid_topology_ref(
|
||||
&self.our_full_destination,
|
||||
Some(&self.our_full_destination),
|
||||
|
||||
@@ -28,6 +28,7 @@ pub enum InputMessage {
|
||||
recipient: Recipient,
|
||||
data: Vec<u8>,
|
||||
lane: TransmissionLane,
|
||||
mix_hops: Option<u8>,
|
||||
},
|
||||
|
||||
/// Creates a message used for a duplex anonymous communication where the recipient
|
||||
@@ -43,6 +44,7 @@ pub enum InputMessage {
|
||||
data: Vec<u8>,
|
||||
reply_surbs: u32,
|
||||
lane: TransmissionLane,
|
||||
mix_hops: Option<u8>,
|
||||
},
|
||||
|
||||
/// Attempt to use our internally received and stored `ReplySurb` to send the message back
|
||||
@@ -92,6 +94,29 @@ impl InputMessage {
|
||||
recipient,
|
||||
data,
|
||||
lane,
|
||||
mix_hops: None,
|
||||
};
|
||||
if let Some(packet_type) = packet_type {
|
||||
InputMessage::new_wrapper(message, packet_type)
|
||||
} else {
|
||||
message
|
||||
}
|
||||
}
|
||||
|
||||
// IMHO `new_regular` should take `mix_hops: Option<u8>` as an argument instead of creating
|
||||
// this function, but that would potentially break backwards compatibility with the current API
|
||||
pub fn new_regular_with_custom_hops(
|
||||
recipient: Recipient,
|
||||
data: Vec<u8>,
|
||||
lane: TransmissionLane,
|
||||
packet_type: Option<PacketType>,
|
||||
mix_hops: Option<u8>,
|
||||
) -> Self {
|
||||
let message = InputMessage::Regular {
|
||||
recipient,
|
||||
data,
|
||||
lane,
|
||||
mix_hops,
|
||||
};
|
||||
if let Some(packet_type) = packet_type {
|
||||
InputMessage::new_wrapper(message, packet_type)
|
||||
@@ -112,6 +137,7 @@ impl InputMessage {
|
||||
data,
|
||||
reply_surbs,
|
||||
lane,
|
||||
mix_hops: None,
|
||||
};
|
||||
if let Some(packet_type) = packet_type {
|
||||
InputMessage::new_wrapper(message, packet_type)
|
||||
@@ -128,12 +154,14 @@ impl InputMessage {
|
||||
reply_surbs: u32,
|
||||
lane: TransmissionLane,
|
||||
packet_type: Option<PacketType>,
|
||||
mix_hops: Option<u8>,
|
||||
) -> Self {
|
||||
let message = InputMessage::Anonymous {
|
||||
recipient,
|
||||
data,
|
||||
reply_surbs,
|
||||
lane,
|
||||
mix_hops,
|
||||
};
|
||||
if let Some(packet_type) = packet_type {
|
||||
InputMessage::new_wrapper(message, packet_type)
|
||||
|
||||
+35
-8
@@ -73,10 +73,11 @@ where
|
||||
content: Vec<u8>,
|
||||
lane: TransmissionLane,
|
||||
packet_type: PacketType,
|
||||
mix_hops: Option<u8>,
|
||||
) {
|
||||
if let Err(err) = self
|
||||
.message_handler
|
||||
.try_send_plain_message(recipient, content, lane, packet_type)
|
||||
.try_send_plain_message(recipient, content, lane, packet_type, mix_hops)
|
||||
.await
|
||||
{
|
||||
warn!("failed to send a plain message - {err}")
|
||||
@@ -90,10 +91,18 @@ where
|
||||
reply_surbs: u32,
|
||||
lane: TransmissionLane,
|
||||
packet_type: PacketType,
|
||||
mix_hops: Option<u8>,
|
||||
) {
|
||||
if let Err(err) = self
|
||||
.message_handler
|
||||
.try_send_message_with_reply_surbs(recipient, content, reply_surbs, lane, packet_type)
|
||||
.try_send_message_with_reply_surbs(
|
||||
recipient,
|
||||
content,
|
||||
reply_surbs,
|
||||
lane,
|
||||
packet_type,
|
||||
mix_hops,
|
||||
)
|
||||
.await
|
||||
{
|
||||
warn!("failed to send a repliable message - {err}")
|
||||
@@ -106,8 +115,9 @@ where
|
||||
recipient,
|
||||
data,
|
||||
lane,
|
||||
mix_hops,
|
||||
} => {
|
||||
self.handle_plain_message(recipient, data, lane, PacketType::Mix)
|
||||
self.handle_plain_message(recipient, data, lane, PacketType::Mix, mix_hops)
|
||||
.await
|
||||
}
|
||||
InputMessage::Anonymous {
|
||||
@@ -115,9 +125,17 @@ where
|
||||
data,
|
||||
reply_surbs,
|
||||
lane,
|
||||
mix_hops,
|
||||
} => {
|
||||
self.handle_repliable_message(recipient, data, reply_surbs, lane, PacketType::Mix)
|
||||
.await
|
||||
self.handle_repliable_message(
|
||||
recipient,
|
||||
data,
|
||||
reply_surbs,
|
||||
lane,
|
||||
PacketType::Mix,
|
||||
mix_hops,
|
||||
)
|
||||
.await
|
||||
}
|
||||
InputMessage::Reply {
|
||||
recipient_tag,
|
||||
@@ -135,8 +153,9 @@ where
|
||||
recipient,
|
||||
data,
|
||||
lane,
|
||||
mix_hops,
|
||||
} => {
|
||||
self.handle_plain_message(recipient, data, lane, packet_type)
|
||||
self.handle_plain_message(recipient, data, lane, packet_type, mix_hops)
|
||||
.await
|
||||
}
|
||||
InputMessage::Anonymous {
|
||||
@@ -144,9 +163,17 @@ where
|
||||
data,
|
||||
reply_surbs,
|
||||
lane,
|
||||
mix_hops,
|
||||
} => {
|
||||
self.handle_repliable_message(recipient, data, reply_surbs, lane, packet_type)
|
||||
.await
|
||||
self.handle_repliable_message(
|
||||
recipient,
|
||||
data,
|
||||
reply_surbs,
|
||||
lane,
|
||||
packet_type,
|
||||
mix_hops,
|
||||
)
|
||||
.await
|
||||
}
|
||||
InputMessage::Reply {
|
||||
recipient_tag,
|
||||
|
||||
@@ -70,6 +70,7 @@ pub(crate) struct PendingAcknowledgement {
|
||||
message_chunk: Fragment,
|
||||
delay: SphinxDelay,
|
||||
destination: PacketDestination,
|
||||
mix_hops: Option<u8>,
|
||||
retransmissions: u32,
|
||||
}
|
||||
|
||||
@@ -79,11 +80,13 @@ impl PendingAcknowledgement {
|
||||
message_chunk: Fragment,
|
||||
delay: SphinxDelay,
|
||||
recipient: Recipient,
|
||||
mix_hops: Option<u8>,
|
||||
) -> Self {
|
||||
PendingAcknowledgement {
|
||||
message_chunk,
|
||||
delay,
|
||||
destination: PacketDestination::KnownRecipient(recipient.into()),
|
||||
mix_hops,
|
||||
retransmissions: 0,
|
||||
}
|
||||
}
|
||||
@@ -101,6 +104,9 @@ impl PendingAcknowledgement {
|
||||
recipient_tag,
|
||||
extra_surb_request,
|
||||
},
|
||||
// Messages sent using SURBs are using the number of mix hops set by the recipient when
|
||||
// they provided the SURBs, so it doesn't make sense to include it here.
|
||||
mix_hops: None,
|
||||
retransmissions: 0,
|
||||
}
|
||||
}
|
||||
|
||||
+8
-1
@@ -52,12 +52,18 @@ where
|
||||
packet_recipient: Recipient,
|
||||
chunk_data: Fragment,
|
||||
packet_type: PacketType,
|
||||
mix_hops: Option<u8>,
|
||||
) -> Result<PreparedFragment, PreparationError> {
|
||||
debug!("retransmitting normal packet...");
|
||||
|
||||
// TODO: Figure out retransmission packet type signaling
|
||||
self.message_handler
|
||||
.try_prepare_single_chunk_for_sending(packet_recipient, chunk_data, packet_type)
|
||||
.try_prepare_single_chunk_for_sending(
|
||||
packet_recipient,
|
||||
chunk_data,
|
||||
packet_type,
|
||||
mix_hops,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
@@ -104,6 +110,7 @@ where
|
||||
**recipient,
|
||||
timed_out_ack.message_chunk.clone(),
|
||||
packet_type,
|
||||
timed_out_ack.mix_hops,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
@@ -15,11 +15,11 @@ use nym_sphinx::anonymous_replies::requests::{AnonymousSenderTag, RepliableMessa
|
||||
use nym_sphinx::anonymous_replies::{ReplySurb, SurbEncryptionKey};
|
||||
use nym_sphinx::chunking::fragment::{Fragment, FragmentIdentifier};
|
||||
use nym_sphinx::message::NymMessage;
|
||||
use nym_sphinx::params::{PacketSize, PacketType};
|
||||
use nym_sphinx::params::{PacketSize, PacketType, DEFAULT_NUM_MIX_HOPS};
|
||||
use nym_sphinx::preparer::{MessagePreparer, PreparedFragment};
|
||||
use nym_sphinx::Delay;
|
||||
use nym_task::connections::TransmissionLane;
|
||||
use nym_topology::{NymRouteProvider, NymTopologyError};
|
||||
use nym_topology::{NymTopology, NymTopologyError};
|
||||
use rand::{CryptoRng, Rng};
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
@@ -100,6 +100,10 @@ pub(crate) struct Config {
|
||||
/// Average delay an acknowledgement packet is going to get delay at a single mixnode.
|
||||
average_ack_delay: Duration,
|
||||
|
||||
/// Number of mix hops each packet ('real' message, ack, reply) is expected to take.
|
||||
/// Note that it does not include gateway hops.
|
||||
num_mix_hops: u8,
|
||||
|
||||
/// Primary predefined packet size used for the encapsulated messages.
|
||||
primary_packet_size: PacketSize,
|
||||
|
||||
@@ -121,11 +125,19 @@ impl Config {
|
||||
deterministic_route_selection,
|
||||
average_packet_delay,
|
||||
average_ack_delay,
|
||||
num_mix_hops: DEFAULT_NUM_MIX_HOPS,
|
||||
primary_packet_size: PacketSize::default(),
|
||||
secondary_packet_size: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Allows setting non-default number of expected mix hops in the network.
|
||||
#[allow(dead_code)]
|
||||
pub fn with_mix_hops(mut self, hops: u8) -> Self {
|
||||
self.num_mix_hops = hops;
|
||||
self
|
||||
}
|
||||
|
||||
/// Allows setting non-default size of the sphinx packets sent out.
|
||||
pub fn with_custom_primary_packet_size(mut self, packet_size: PacketSize) -> Self {
|
||||
self.primary_packet_size = packet_size;
|
||||
@@ -173,7 +185,9 @@ where
|
||||
config.sender_address,
|
||||
config.average_packet_delay,
|
||||
config.average_ack_delay,
|
||||
);
|
||||
)
|
||||
.with_mix_hops(config.num_mix_hops);
|
||||
|
||||
MessageHandler {
|
||||
config,
|
||||
rng,
|
||||
@@ -202,7 +216,7 @@ where
|
||||
fn get_topology<'a>(
|
||||
&self,
|
||||
permit: &'a TopologyReadPermit<'a>,
|
||||
) -> Result<&'a NymRouteProvider, PreparationError> {
|
||||
) -> Result<&'a NymTopology, PreparationError> {
|
||||
match permit.try_get_valid_topology_ref(&self.config.sender_address, None) {
|
||||
Ok(topology_ref) => Ok(topology_ref),
|
||||
Err(err) => {
|
||||
@@ -219,8 +233,9 @@ where
|
||||
return self.config.primary_packet_size;
|
||||
};
|
||||
|
||||
let primary_count = msg.required_packets(self.config.primary_packet_size);
|
||||
let secondary_count = msg.required_packets(secondary_packet);
|
||||
let primary_count =
|
||||
msg.required_packets(self.config.primary_packet_size, self.config.num_mix_hops);
|
||||
let secondary_count = msg.required_packets(secondary_packet, self.config.num_mix_hops);
|
||||
|
||||
trace!("This message would require: {primary_count} primary packets or {secondary_count} secondary packets...");
|
||||
// if there would be no benefit in using the secondary packet - use the primary (duh)
|
||||
@@ -409,9 +424,10 @@ where
|
||||
message: Vec<u8>,
|
||||
lane: TransmissionLane,
|
||||
packet_type: PacketType,
|
||||
mix_hops: Option<u8>,
|
||||
) -> Result<(), PreparationError> {
|
||||
let message = NymMessage::new_plain(message);
|
||||
self.try_split_and_send_non_reply_message(message, recipient, lane, packet_type)
|
||||
self.try_split_and_send_non_reply_message(message, recipient, lane, packet_type, mix_hops)
|
||||
.await
|
||||
}
|
||||
|
||||
@@ -421,6 +437,7 @@ where
|
||||
recipient: Recipient,
|
||||
lane: TransmissionLane,
|
||||
packet_type: PacketType,
|
||||
mix_hops: Option<u8>,
|
||||
) -> Result<(), PreparationError> {
|
||||
debug!("Sending non-reply message with packet type {packet_type}");
|
||||
// TODO: I really dislike existence of this assertion, it implies code has to be re-organised
|
||||
@@ -453,6 +470,7 @@ where
|
||||
&self.config.ack_key,
|
||||
&recipient,
|
||||
packet_type,
|
||||
mix_hops,
|
||||
)?;
|
||||
|
||||
let real_message = RealMessage::new(
|
||||
@@ -460,7 +478,8 @@ where
|
||||
Some(fragment.fragment_identifier()),
|
||||
);
|
||||
let delay = prepared_fragment.total_delay;
|
||||
let pending_ack = PendingAcknowledgement::new_known(fragment, delay, recipient);
|
||||
let pending_ack =
|
||||
PendingAcknowledgement::new_known(fragment, delay, recipient, mix_hops);
|
||||
|
||||
real_messages.push(real_message);
|
||||
pending_acks.push(pending_ack);
|
||||
@@ -477,6 +496,7 @@ where
|
||||
recipient: Recipient,
|
||||
amount: u32,
|
||||
packet_type: PacketType,
|
||||
mix_hops: Option<u8>,
|
||||
) -> Result<(), PreparationError> {
|
||||
debug!("Sending additional reply SURBs with packet type {packet_type}");
|
||||
let sender_tag = self.get_or_create_sender_tag(&recipient);
|
||||
@@ -493,6 +513,7 @@ where
|
||||
recipient,
|
||||
TransmissionLane::AdditionalReplySurbs,
|
||||
packet_type,
|
||||
mix_hops,
|
||||
)
|
||||
.await?;
|
||||
|
||||
@@ -509,6 +530,7 @@ where
|
||||
num_reply_surbs: u32,
|
||||
lane: TransmissionLane,
|
||||
packet_type: PacketType,
|
||||
mix_hops: Option<u8>,
|
||||
) -> Result<(), SurbWrappedPreparationError> {
|
||||
debug!("Sending message with reply SURBs with packet type {packet_type}");
|
||||
let sender_tag = self.get_or_create_sender_tag(&recipient);
|
||||
@@ -519,7 +541,7 @@ where
|
||||
let message =
|
||||
NymMessage::new_repliable(RepliableMessage::new_data(message, sender_tag, reply_surbs));
|
||||
|
||||
self.try_split_and_send_non_reply_message(message, recipient, lane, packet_type)
|
||||
self.try_split_and_send_non_reply_message(message, recipient, lane, packet_type, mix_hops)
|
||||
.await?;
|
||||
|
||||
log::trace!("storing {} reply keys", reply_keys.len());
|
||||
@@ -533,18 +555,23 @@ where
|
||||
recipient: Recipient,
|
||||
chunk: Fragment,
|
||||
packet_type: PacketType,
|
||||
mix_hops: Option<u8>,
|
||||
) -> Result<PreparedFragment, PreparationError> {
|
||||
debug!("Sending single chunk with packet type {packet_type}");
|
||||
let topology_permit = self.topology_access.get_read_permit().await;
|
||||
let topology = self.get_topology(&topology_permit)?;
|
||||
|
||||
let prepared_fragment = self.message_preparer.prepare_chunk_for_sending(
|
||||
chunk,
|
||||
topology,
|
||||
&self.config.ack_key,
|
||||
&recipient,
|
||||
packet_type,
|
||||
)?;
|
||||
let prepared_fragment = self
|
||||
.message_preparer
|
||||
.prepare_chunk_for_sending(
|
||||
chunk,
|
||||
topology,
|
||||
&self.config.ack_key,
|
||||
&recipient,
|
||||
packet_type,
|
||||
mix_hops,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
Ok(prepared_fragment)
|
||||
}
|
||||
@@ -597,13 +624,16 @@ where
|
||||
Err(err) => return Err(err.return_surbs(vec![reply_surb])),
|
||||
};
|
||||
|
||||
let prepared_fragment = self.message_preparer.prepare_reply_chunk_for_sending(
|
||||
chunk,
|
||||
topology,
|
||||
&self.config.ack_key,
|
||||
reply_surb,
|
||||
PacketType::Mix,
|
||||
)?;
|
||||
let prepared_fragment = self
|
||||
.message_preparer
|
||||
.prepare_reply_chunk_for_sending(
|
||||
chunk,
|
||||
topology,
|
||||
&self.config.ack_key,
|
||||
reply_surb,
|
||||
PacketType::Mix,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
Ok(prepared_fragment)
|
||||
}
|
||||
|
||||
@@ -230,7 +230,6 @@ where
|
||||
// poisson delay, but is it really a problem?
|
||||
let topology_permit = self.topology_access.get_read_permit().await;
|
||||
// the ack is sent back to ourselves (and then ignored)
|
||||
|
||||
let topology_ref = match topology_permit.try_get_valid_topology_ref(
|
||||
&self.config.our_full_destination,
|
||||
Some(&self.config.our_full_destination),
|
||||
|
||||
@@ -516,6 +516,7 @@ where
|
||||
recipient,
|
||||
to_send,
|
||||
nym_sphinx::params::PacketType::Mix,
|
||||
self.config.reply_surbs.surb_mix_hops,
|
||||
)
|
||||
.await
|
||||
{
|
||||
|
||||
@@ -2,7 +2,8 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
use nym_sphinx::addressing::clients::Recipient;
|
||||
use nym_topology::{NymRouteProvider, NymTopology, NymTopologyError};
|
||||
use nym_sphinx::params::DEFAULT_NUM_MIX_HOPS;
|
||||
use nym_topology::{NymTopology, NymTopologyError};
|
||||
use std::ops::Deref;
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::sync::Arc;
|
||||
@@ -16,36 +17,29 @@ pub struct TopologyAccessorInner {
|
||||
// few seconds, while reads are needed every single packet generated.
|
||||
// However, proper benchmarks will be needed to determine if `RwLock` is indeed a better
|
||||
// approach than a `Mutex`
|
||||
topology: RwLock<NymRouteProvider>,
|
||||
topology: RwLock<Option<NymTopology>>,
|
||||
}
|
||||
|
||||
impl TopologyAccessorInner {
|
||||
fn new(initial: NymRouteProvider) -> Self {
|
||||
fn new() -> Self {
|
||||
TopologyAccessorInner {
|
||||
controlled_manually: AtomicBool::new(false),
|
||||
released_manual_control: Notify::new(),
|
||||
topology: RwLock::new(initial),
|
||||
topology: RwLock::new(None),
|
||||
}
|
||||
}
|
||||
|
||||
async fn update(&self, new: Option<NymTopology>) {
|
||||
let mut guard = self.topology.write().await;
|
||||
|
||||
match new {
|
||||
Some(updated) => {
|
||||
guard.update(updated);
|
||||
}
|
||||
None => guard.clear_topology(),
|
||||
}
|
||||
*self.topology.write().await = new;
|
||||
}
|
||||
}
|
||||
|
||||
pub struct TopologyReadPermit<'a> {
|
||||
permit: RwLockReadGuard<'a, NymRouteProvider>,
|
||||
permit: RwLockReadGuard<'a, Option<NymTopology>>,
|
||||
}
|
||||
|
||||
impl Deref for TopologyReadPermit<'_> {
|
||||
type Target = NymRouteProvider;
|
||||
type Target = Option<NymTopology>;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.permit
|
||||
@@ -59,31 +53,43 @@ impl<'a> TopologyReadPermit<'a> {
|
||||
&'a self,
|
||||
ack_recipient: &Recipient,
|
||||
packet_recipient: Option<&Recipient>,
|
||||
) -> Result<&'a NymRouteProvider, NymTopologyError> {
|
||||
let route_provider = self.permit.deref();
|
||||
let topology = &route_provider.topology;
|
||||
|
||||
) -> Result<&'a NymTopology, NymTopologyError> {
|
||||
// 1. Have we managed to get anything from the refresher, i.e. have the nym-api queries gone through?
|
||||
topology.ensure_not_empty()?;
|
||||
let topology = self
|
||||
.permit
|
||||
.as_ref()
|
||||
.ok_or(NymTopologyError::EmptyNetworkTopology)?;
|
||||
|
||||
// 2. does the topology have a node on each mixing layer?
|
||||
topology.ensure_minimally_routable()?;
|
||||
// 2. does it have any mixnode at all?
|
||||
// 3. does it have any gateways at all?
|
||||
// 4. does it have a mixnode on each layer?
|
||||
topology.ensure_can_construct_path_through(DEFAULT_NUM_MIX_HOPS)?;
|
||||
|
||||
// 3. does it contain OUR gateway (so that we could create an ack packet)?
|
||||
let _ = route_provider.egress_by_identity(ack_recipient.gateway())?;
|
||||
|
||||
// 4. for our target recipient, does it contain THEIR gateway (so that we send anything over?)
|
||||
if let Some(recipient) = packet_recipient {
|
||||
let _ = route_provider.egress_by_identity(recipient.gateway())?;
|
||||
// 5. does it contain OUR gateway (so that we could create an ack packet)?
|
||||
if !topology.gateway_exists(ack_recipient.gateway()) {
|
||||
return Err(NymTopologyError::NonExistentGatewayError {
|
||||
identity_key: ack_recipient.gateway().to_base58_string(),
|
||||
});
|
||||
}
|
||||
|
||||
Ok(route_provider)
|
||||
// 6. for our target recipient, does it contain THEIR gateway (so that we could create
|
||||
if let Some(recipient) = packet_recipient {
|
||||
if !topology.gateway_exists(recipient.gateway()) {
|
||||
return Err(NymTopologyError::NonExistentGatewayError {
|
||||
identity_key: recipient.gateway().to_base58_string(),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Ok(topology)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> From<RwLockReadGuard<'a, NymRouteProvider>> for TopologyReadPermit<'a> {
|
||||
fn from(permit: RwLockReadGuard<'a, NymRouteProvider>) -> Self {
|
||||
TopologyReadPermit { permit }
|
||||
impl<'a> From<RwLockReadGuard<'a, Option<NymTopology>>> for TopologyReadPermit<'a> {
|
||||
fn from(read_permit: RwLockReadGuard<'a, Option<NymTopology>>) -> Self {
|
||||
TopologyReadPermit {
|
||||
permit: read_permit,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -93,11 +99,9 @@ pub struct TopologyAccessor {
|
||||
}
|
||||
|
||||
impl TopologyAccessor {
|
||||
pub fn new(ignore_egress_epoch_roles: bool) -> Self {
|
||||
pub fn new() -> Self {
|
||||
TopologyAccessor {
|
||||
inner: Arc::new(TopologyAccessorInner::new(NymRouteProvider::new_empty(
|
||||
ignore_egress_epoch_roles,
|
||||
))),
|
||||
inner: Arc::new(TopologyAccessorInner::new()),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -117,21 +121,8 @@ impl TopologyAccessor {
|
||||
self.inner.released_manual_control.notified().await
|
||||
}
|
||||
|
||||
#[deprecated(note = "use .current_route_provider instead")]
|
||||
pub async fn current_topology(&self) -> Option<NymTopology> {
|
||||
self.current_route_provider()
|
||||
.await
|
||||
.as_ref()
|
||||
.map(|p| p.topology.clone())
|
||||
}
|
||||
|
||||
pub async fn current_route_provider(&self) -> Option<RwLockReadGuard<NymRouteProvider>> {
|
||||
let provider = self.inner.topology.read().await;
|
||||
if provider.topology.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(provider)
|
||||
}
|
||||
self.inner.topology.read().await.clone()
|
||||
}
|
||||
|
||||
pub async fn manually_change_topology(&self, new_topology: NymTopology) {
|
||||
@@ -149,11 +140,15 @@ impl TopologyAccessor {
|
||||
// only used by the client at startup to get a slightly more reasonable error message
|
||||
// (currently displays as unused because health checker is disabled due to required changes)
|
||||
pub async fn ensure_is_routable(&self) -> Result<(), NymTopologyError> {
|
||||
self.inner
|
||||
.topology
|
||||
.read()
|
||||
.await
|
||||
.topology
|
||||
.ensure_minimally_routable()
|
||||
match self.inner.topology.read().await.deref() {
|
||||
None => Err(NymTopologyError::EmptyNetworkTopology),
|
||||
Some(ref topology) => topology.ensure_can_construct_path_through(DEFAULT_NUM_MIX_HOPS),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for TopologyAccessor {
|
||||
fn default() -> Self {
|
||||
TopologyAccessor::new()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ use log::{debug, error};
|
||||
use nym_explorer_client::{ExplorerClient, PrettyDetailedMixNodeBond};
|
||||
use nym_network_defaults::var_names::EXPLORER_API;
|
||||
use nym_topology::{
|
||||
nym_topology_from_basic_info,
|
||||
provider_trait::{async_trait, TopologyProvider},
|
||||
NymTopology,
|
||||
};
|
||||
@@ -14,6 +15,8 @@ use url::Url;
|
||||
|
||||
pub use nym_country_group::CountryGroup;
|
||||
|
||||
const MIN_NODES_PER_LAYER: usize = 1;
|
||||
|
||||
fn create_explorer_client() -> Option<ExplorerClient> {
|
||||
let Ok(explorer_api_url) = std::env::var(EXPLORER_API) else {
|
||||
error!("Missing EXPLORER_API");
|
||||
@@ -60,20 +63,30 @@ fn log_mixnode_distribution(mixnodes: &HashMap<CountryGroup, Vec<NodeId>>) {
|
||||
}
|
||||
|
||||
fn check_layer_integrity(topology: NymTopology) -> Result<(), ()> {
|
||||
if topology.ensure_minimally_routable().is_err() {
|
||||
let mixes = topology.mixes();
|
||||
if mixes.keys().len() < 3 {
|
||||
error!("Layer is missing in topology!");
|
||||
return Err(());
|
||||
}
|
||||
for (layer, mixnodes) in mixes {
|
||||
debug!("Layer {:?} has {} mixnodes", layer, mixnodes.len());
|
||||
if mixnodes.len() < MIN_NODES_PER_LAYER {
|
||||
error!(
|
||||
"There are only {} mixnodes in layer {:?}",
|
||||
mixnodes.len(),
|
||||
layer
|
||||
);
|
||||
return Err(());
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[deprecated(note = "use NymApiTopologyProvider instead as explorer API will soon be removed")]
|
||||
pub struct GeoAwareTopologyProvider {
|
||||
validator_client: nym_validator_client::client::NymApiClient,
|
||||
filter_on: GroupBy,
|
||||
}
|
||||
|
||||
#[allow(deprecated)]
|
||||
impl GeoAwareTopologyProvider {
|
||||
pub fn new(mut nym_api_urls: Vec<Url>, filter_on: GroupBy) -> GeoAwareTopologyProvider {
|
||||
log::info!(
|
||||
@@ -91,15 +104,6 @@ impl GeoAwareTopologyProvider {
|
||||
}
|
||||
|
||||
async fn get_topology(&self) -> Option<NymTopology> {
|
||||
let rewarded_set = self
|
||||
.validator_client
|
||||
.get_current_rewarded_set()
|
||||
.await
|
||||
.inspect_err(|err| error!("failed to get current rewarded set: {err}"))
|
||||
.ok()?;
|
||||
|
||||
let mut topology = NymTopology::new_empty(rewarded_set);
|
||||
|
||||
let mixnodes = match self
|
||||
.validator_client
|
||||
.get_all_basic_active_mixing_assigned_nodes()
|
||||
@@ -183,8 +187,7 @@ impl GeoAwareTopologyProvider {
|
||||
.filter(|m| filtered_mixnode_ids.contains(&m.node_id))
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
topology.add_skimmed_nodes(&mixnodes);
|
||||
topology.add_skimmed_nodes(&gateways);
|
||||
let topology = nym_topology_from_basic_info(&mixnodes, &gateways);
|
||||
|
||||
// TODO: return real error type
|
||||
check_layer_integrity(topology.clone()).ok()?;
|
||||
@@ -193,7 +196,6 @@ impl GeoAwareTopologyProvider {
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(deprecated)]
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
#[async_trait]
|
||||
impl TopologyProvider for GeoAwareTopologyProvider {
|
||||
@@ -203,7 +205,6 @@ impl TopologyProvider for GeoAwareTopologyProvider {
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(deprecated)]
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
#[async_trait(?Send)]
|
||||
impl TopologyProvider for GeoAwareTopologyProvider {
|
||||
|
||||
@@ -19,7 +19,6 @@ mod accessor;
|
||||
pub mod geo_aware_provider;
|
||||
pub mod nym_api_provider;
|
||||
|
||||
#[allow(deprecated)]
|
||||
pub use geo_aware_provider::GeoAwareTopologyProvider;
|
||||
pub use nym_api_provider::{Config as NymApiTopologyProviderConfig, NymApiTopologyProvider};
|
||||
pub use nym_topology::provider_trait::TopologyProvider;
|
||||
@@ -28,7 +27,7 @@ pub use nym_topology::provider_trait::TopologyProvider;
|
||||
const MAX_FAILURE_COUNT: usize = 10;
|
||||
|
||||
pub struct TopologyRefresherConfig {
|
||||
pub refresh_rate: Duration,
|
||||
refresh_rate: Duration,
|
||||
}
|
||||
|
||||
impl TopologyRefresherConfig {
|
||||
@@ -97,24 +96,28 @@ impl TopologyRefresher {
|
||||
self.topology_accessor.ensure_is_routable().await
|
||||
}
|
||||
|
||||
pub async fn ensure_contains_routable_egress(
|
||||
pub async fn ensure_contains_gateway(
|
||||
&self,
|
||||
egress: NodeIdentity,
|
||||
gateway: &NodeIdentity,
|
||||
) -> Result<(), NymTopologyError> {
|
||||
let topology = self
|
||||
.topology_accessor
|
||||
.current_route_provider()
|
||||
.current_topology()
|
||||
.await
|
||||
.ok_or(NymTopologyError::EmptyNetworkTopology)?;
|
||||
|
||||
let _ = topology.egress_by_identity(egress)?;
|
||||
if !topology.gateway_exists(gateway) {
|
||||
return Err(NymTopologyError::NonExistentGatewayError {
|
||||
identity_key: gateway.to_base58_string(),
|
||||
});
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn wait_for_gateway(
|
||||
&mut self,
|
||||
gateway: NodeIdentity,
|
||||
gateway: &NodeIdentity,
|
||||
timeout_duration: Duration,
|
||||
) -> Result<(), NymTopologyError> {
|
||||
info!(
|
||||
@@ -132,7 +135,7 @@ impl TopologyRefresher {
|
||||
})
|
||||
}
|
||||
_ = self.try_refresh() => {
|
||||
if self.ensure_contains_routable_egress(gateway).await.is_ok() {
|
||||
if self.ensure_contains_gateway(gateway).await.is_ok() {
|
||||
return Ok(())
|
||||
}
|
||||
info!("gateway '{gateway}' is still not online...");
|
||||
|
||||
@@ -4,39 +4,32 @@
|
||||
use async_trait::async_trait;
|
||||
use log::{debug, error, warn};
|
||||
use nym_topology::provider_trait::TopologyProvider;
|
||||
use nym_topology::NymTopology;
|
||||
use nym_topology::{NymTopology, NymTopologyError};
|
||||
use nym_validator_client::UserAgent;
|
||||
use rand::prelude::SliceRandom;
|
||||
use rand::thread_rng;
|
||||
use std::cmp::min;
|
||||
use url::Url;
|
||||
|
||||
// the same values as our current (10.06.24) blacklist
|
||||
pub const DEFAULT_MIN_MIXNODE_PERFORMANCE: u8 = 50;
|
||||
pub const DEFAULT_MIN_GATEWAY_PERFORMANCE: u8 = 50;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Config {
|
||||
pub min_mixnode_performance: u8,
|
||||
pub min_gateway_performance: u8,
|
||||
pub use_extended_topology: bool,
|
||||
pub ignore_egress_epoch_role: bool,
|
||||
}
|
||||
|
||||
impl From<nym_client_core_config_types::Topology> for Config {
|
||||
fn from(value: nym_client_core_config_types::Topology) -> Self {
|
||||
impl Default for Config {
|
||||
fn default() -> Self {
|
||||
// old values that decided on blacklist membership
|
||||
Config {
|
||||
min_mixnode_performance: value.minimum_mixnode_performance,
|
||||
min_gateway_performance: value.minimum_gateway_performance,
|
||||
use_extended_topology: value.use_extended_topology,
|
||||
ignore_egress_epoch_role: value.ignore_egress_epoch_role,
|
||||
min_mixnode_performance: DEFAULT_MIN_MIXNODE_PERFORMANCE,
|
||||
min_gateway_performance: DEFAULT_MIN_GATEWAY_PERFORMANCE,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Config {
|
||||
// if we're using 'extended' topology, filter the nodes based on the lowest set performance
|
||||
fn min_node_performance(&self) -> u8 {
|
||||
min(self.min_mixnode_performance, self.min_gateway_performance)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct NymApiTopologyProvider {
|
||||
config: Config,
|
||||
|
||||
@@ -46,11 +39,7 @@ pub struct NymApiTopologyProvider {
|
||||
}
|
||||
|
||||
impl NymApiTopologyProvider {
|
||||
pub fn new(
|
||||
config: impl Into<Config>,
|
||||
mut nym_api_urls: Vec<Url>,
|
||||
user_agent: Option<UserAgent>,
|
||||
) -> Self {
|
||||
pub fn new(config: Config, mut nym_api_urls: Vec<Url>, user_agent: Option<UserAgent>) -> Self {
|
||||
nym_api_urls.shuffle(&mut thread_rng());
|
||||
|
||||
let validator_client = if let Some(user_agent) = user_agent {
|
||||
@@ -63,7 +52,7 @@ impl NymApiTopologyProvider {
|
||||
};
|
||||
|
||||
NymApiTopologyProvider {
|
||||
config: config.into(),
|
||||
config,
|
||||
validator_client,
|
||||
nym_api_urls,
|
||||
currently_used_api: 0,
|
||||
@@ -81,69 +70,70 @@ impl NymApiTopologyProvider {
|
||||
.change_nym_api(self.nym_api_urls[self.currently_used_api].clone())
|
||||
}
|
||||
|
||||
/// Verifies whether nodes a reasonably distributed among all mix layers.
|
||||
///
|
||||
/// In ideal world we would have 33% nodes on layer 1, 33% on layer 2 and 33% on layer 3.
|
||||
/// However, this is a rather unrealistic expectation, instead we check whether there exists
|
||||
/// a layer with more than 66% of nodes or with fewer than 15% and if so, we trigger a failure.
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `topology`: active topology constructed from validator api data
|
||||
fn check_layer_distribution(
|
||||
&self,
|
||||
active_topology: &NymTopology,
|
||||
) -> Result<(), NymTopologyError> {
|
||||
let lower_threshold = 0.15;
|
||||
let upper_threshold = 0.66;
|
||||
active_topology.ensure_even_layer_distribution(lower_threshold, upper_threshold)
|
||||
}
|
||||
|
||||
async fn get_current_compatible_topology(&mut self) -> Option<NymTopology> {
|
||||
let rewarded_set = self
|
||||
let mixnodes = match self
|
||||
.validator_client
|
||||
.get_current_rewarded_set()
|
||||
.get_all_basic_active_mixing_assigned_nodes()
|
||||
.await
|
||||
.inspect_err(|err| error!("failed to get current rewarded set: {err}"))
|
||||
.ok()?;
|
||||
|
||||
let mut topology = NymTopology::new_empty(rewarded_set);
|
||||
|
||||
if self.config.use_extended_topology {
|
||||
let all_nodes = self
|
||||
.validator_client
|
||||
.get_all_basic_nodes()
|
||||
.await
|
||||
.inspect_err(|err| error!("failed to get network nodes: {err}"))
|
||||
.ok()?;
|
||||
|
||||
debug!(
|
||||
"there are {} nodes on the network (before filtering)",
|
||||
all_nodes.len()
|
||||
);
|
||||
topology.add_additional_nodes(all_nodes.iter().filter(|n| {
|
||||
n.performance.round_to_integer() >= self.config.min_node_performance()
|
||||
}));
|
||||
} else {
|
||||
// if we're not using extended topology, we're only getting active set mixnodes and gateways
|
||||
|
||||
let mixnodes = self
|
||||
.validator_client
|
||||
.get_all_basic_active_mixing_assigned_nodes()
|
||||
.await
|
||||
.inspect_err(|err| error!("failed to get network mixnodes: {err}"))
|
||||
.ok()?;
|
||||
|
||||
// TODO: we really should be getting ACTIVE gateways only
|
||||
let gateways = self
|
||||
.validator_client
|
||||
.get_all_basic_entry_assigned_nodes()
|
||||
.await
|
||||
.inspect_err(|err| error!("failed to get network gateways: {err}"))
|
||||
.ok()?;
|
||||
|
||||
debug!(
|
||||
"there are {} mixnodes and {} gateways in total (before performance filtering)",
|
||||
mixnodes.len(),
|
||||
gateways.len()
|
||||
);
|
||||
|
||||
topology.add_additional_nodes(mixnodes.iter().filter(|m| {
|
||||
m.performance.round_to_integer() >= self.config.min_mixnode_performance
|
||||
}));
|
||||
topology.add_additional_nodes(gateways.iter().filter(|m| {
|
||||
m.performance.round_to_integer() >= self.config.min_gateway_performance
|
||||
}));
|
||||
{
|
||||
Err(err) => {
|
||||
error!("failed to get network mixnodes - {err}");
|
||||
return None;
|
||||
}
|
||||
Ok(mixes) => mixes,
|
||||
};
|
||||
|
||||
if !topology.is_minimally_routable() {
|
||||
error!("the current filtered active topology can't be used to construct any packets");
|
||||
return None;
|
||||
}
|
||||
let gateways = match self
|
||||
.validator_client
|
||||
.get_all_basic_entry_assigned_nodes()
|
||||
.await
|
||||
{
|
||||
Err(err) => {
|
||||
error!("failed to get network gateways - {err}");
|
||||
return None;
|
||||
}
|
||||
Ok(gateways) => gateways,
|
||||
};
|
||||
|
||||
Some(topology)
|
||||
debug!(
|
||||
"there are {} mixnodes and {} gateways in total (before performance filtering)",
|
||||
mixnodes.len(),
|
||||
gateways.len()
|
||||
);
|
||||
|
||||
let topology = NymTopology::from_unordered(
|
||||
mixnodes.iter().filter(|m| {
|
||||
m.performance.round_to_integer() >= self.config.min_mixnode_performance
|
||||
}),
|
||||
gateways.iter().filter(|g| {
|
||||
g.performance.round_to_integer() >= self.config.min_gateway_performance
|
||||
}),
|
||||
);
|
||||
if let Err(err) = self.check_layer_distribution(&topology) {
|
||||
warn!("The current filtered active topology has extremely skewed layer distribution. It cannot be used: {err}");
|
||||
self.use_next_nym_api();
|
||||
None
|
||||
} else {
|
||||
Some(topology)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -152,11 +142,7 @@ impl NymApiTopologyProvider {
|
||||
#[async_trait]
|
||||
impl TopologyProvider for NymApiTopologyProvider {
|
||||
async fn get_new_topology(&mut self) -> Option<NymTopology> {
|
||||
let Some(topology) = self.get_current_compatible_topology().await else {
|
||||
self.use_next_nym_api();
|
||||
return None;
|
||||
};
|
||||
Some(topology)
|
||||
self.get_current_compatible_topology().await
|
||||
}
|
||||
}
|
||||
|
||||
@@ -164,10 +150,6 @@ impl TopologyProvider for NymApiTopologyProvider {
|
||||
#[async_trait(?Send)]
|
||||
impl TopologyProvider for NymApiTopologyProvider {
|
||||
async fn get_new_topology(&mut self) -> Option<NymTopology> {
|
||||
let Some(topology) = self.get_current_compatible_topology().await else {
|
||||
self.use_next_nym_api();
|
||||
return None;
|
||||
};
|
||||
Some(topology)
|
||||
self.get_current_compatible_topology().await
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,8 +4,8 @@
|
||||
use crate::client::mix_traffic::transceiver::ErasedGatewayError;
|
||||
use nym_crypto::asymmetric::identity::Ed25519RecoveryError;
|
||||
use nym_gateway_client::error::GatewayClientError;
|
||||
use nym_topology::node::RoutingNodeError;
|
||||
use nym_topology::{NodeId, NymTopologyError};
|
||||
use nym_topology::gateway::GatewayConversionError;
|
||||
use nym_topology::NymTopologyError;
|
||||
use nym_validator_client::ValidatorClientError;
|
||||
use std::error::Error;
|
||||
use std::path::PathBuf;
|
||||
@@ -74,10 +74,10 @@ pub enum ClientCoreError {
|
||||
#[error("the gateway id is invalid - {0}")]
|
||||
UnableToCreatePublicKeyFromGatewayId(Ed25519RecoveryError),
|
||||
|
||||
#[error("the node is malformed: {source}")]
|
||||
#[error("The gateway is malformed: {source}")]
|
||||
MalformedGateway {
|
||||
#[from]
|
||||
source: Box<RoutingNodeError>,
|
||||
source: GatewayConversionError,
|
||||
},
|
||||
|
||||
#[error("failed to establish connection to gateway: {source}")]
|
||||
@@ -159,9 +159,6 @@ pub enum ClientCoreError {
|
||||
#[error("the specified gateway '{gateway}' does not support the wss protocol")]
|
||||
UnsupportedWssProtocol { gateway: String },
|
||||
|
||||
#[error("node {id} ({identity}) does not support mixnet entry mode")]
|
||||
UnsupportedEntry { id: NodeId, identity: String },
|
||||
|
||||
#[error(
|
||||
"failed to load custom topology using path '{}'. detailed message: {source}", file_path.display()
|
||||
)]
|
||||
|
||||
@@ -7,7 +7,7 @@ use futures::{SinkExt, StreamExt};
|
||||
use log::{debug, info, trace, warn};
|
||||
use nym_crypto::asymmetric::identity;
|
||||
use nym_gateway_client::GatewayClient;
|
||||
use nym_topology::node::RoutingNode;
|
||||
use nym_topology::gateway;
|
||||
use nym_validator_client::client::IdentityKeyRef;
|
||||
use nym_validator_client::UserAgent;
|
||||
use rand::{seq::SliceRandom, Rng};
|
||||
@@ -15,7 +15,6 @@ use std::{sync::Arc, time::Duration};
|
||||
use tungstenite::Message;
|
||||
use url::Url;
|
||||
|
||||
use nym_topology::NodeId;
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
use tokio::net::TcpStream;
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
@@ -26,6 +25,7 @@ use tokio::time::Instant;
|
||||
use tokio_tungstenite::connect_async;
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
use tokio_tungstenite::{MaybeTlsStream, WebSocketStream};
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
use wasm_utils::websocket::JSWebsocket;
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
@@ -48,30 +48,22 @@ const PING_TIMEOUT: Duration = Duration::from_millis(1000);
|
||||
|
||||
// The abstraction that some of these helpers use
|
||||
pub trait ConnectableGateway {
|
||||
fn node_id(&self) -> NodeId;
|
||||
fn identity(&self) -> identity::PublicKey;
|
||||
fn clients_address(&self, prefer_ipv6: bool) -> Option<String>;
|
||||
fn identity(&self) -> &identity::PublicKey;
|
||||
fn clients_address(&self) -> String;
|
||||
fn is_wss(&self) -> bool;
|
||||
}
|
||||
|
||||
impl ConnectableGateway for RoutingNode {
|
||||
fn node_id(&self) -> NodeId {
|
||||
self.node_id
|
||||
impl ConnectableGateway for gateway::LegacyNode {
|
||||
fn identity(&self) -> &identity::PublicKey {
|
||||
self.identity()
|
||||
}
|
||||
|
||||
fn identity(&self) -> identity::PublicKey {
|
||||
self.identity_key
|
||||
}
|
||||
|
||||
fn clients_address(&self, prefer_ipv6: bool) -> Option<String> {
|
||||
self.ws_entry_address(prefer_ipv6)
|
||||
fn clients_address(&self) -> String {
|
||||
self.clients_address()
|
||||
}
|
||||
|
||||
fn is_wss(&self) -> bool {
|
||||
self.entry
|
||||
.as_ref()
|
||||
.map(|e| e.clients_wss_port.is_some())
|
||||
.unwrap_or_default()
|
||||
self.clients_wss_port.is_some()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -91,7 +83,7 @@ pub async fn current_gateways<R: Rng>(
|
||||
nym_apis: &[Url],
|
||||
user_agent: Option<UserAgent>,
|
||||
minimum_performance: u8,
|
||||
) -> Result<Vec<RoutingNode>, ClientCoreError> {
|
||||
) -> Result<Vec<gateway::LegacyNode>, ClientCoreError> {
|
||||
let nym_api = nym_apis
|
||||
.choose(rng)
|
||||
.ok_or(ClientCoreError::ListOfNymApisIsEmpty)?;
|
||||
@@ -112,7 +104,7 @@ pub async fn current_gateways<R: Rng>(
|
||||
.iter()
|
||||
.filter(|g| g.performance.round_to_integer() >= minimum_performance)
|
||||
.filter_map(|gateway| gateway.try_into().ok())
|
||||
.collect::<Vec<_>>();
|
||||
.collect::<Vec<gateway::LegacyNode>>();
|
||||
log::debug!("After checking validity: {}", valid_gateways.len());
|
||||
log::trace!("Valid gateways: {:#?}", valid_gateways);
|
||||
|
||||
@@ -142,12 +134,7 @@ async fn measure_latency<G>(gateway: &G) -> Result<GatewayWithLatency<G>, Client
|
||||
where
|
||||
G: ConnectableGateway,
|
||||
{
|
||||
let Some(addr) = gateway.clients_address(false) else {
|
||||
return Err(ClientCoreError::UnsupportedEntry {
|
||||
id: gateway.node_id(),
|
||||
identity: gateway.identity().to_string(),
|
||||
});
|
||||
};
|
||||
let addr = gateway.clients_address();
|
||||
trace!(
|
||||
"establishing connection to {} ({addr})...",
|
||||
gateway.identity(),
|
||||
@@ -218,7 +205,7 @@ pub async fn choose_gateway_by_latency<R: Rng, G: ConnectableGateway + Clone>(
|
||||
let gateways_with_latency = Arc::new(tokio::sync::Mutex::new(Vec::new()));
|
||||
futures::stream::iter(gateways)
|
||||
.for_each_concurrent(CONCURRENT_GATEWAYS_MEASURED, |gateway| async {
|
||||
let id = gateway.identity();
|
||||
let id = *gateway.identity();
|
||||
trace!("measuring latency to {id}...");
|
||||
match measure_latency(gateway).await {
|
||||
Ok(with_latency) => {
|
||||
@@ -265,9 +252,9 @@ fn filter_by_tls<G: ConnectableGateway>(
|
||||
|
||||
pub(super) fn uniformly_random_gateway<R: Rng>(
|
||||
rng: &mut R,
|
||||
gateways: &[RoutingNode],
|
||||
gateways: &[gateway::LegacyNode],
|
||||
must_use_tls: bool,
|
||||
) -> Result<RoutingNode, ClientCoreError> {
|
||||
) -> Result<gateway::LegacyNode, ClientCoreError> {
|
||||
filter_by_tls(gateways, must_use_tls)?
|
||||
.choose(rng)
|
||||
.ok_or(ClientCoreError::NoGatewaysOnNetwork)
|
||||
@@ -276,9 +263,9 @@ pub(super) fn uniformly_random_gateway<R: Rng>(
|
||||
|
||||
pub(super) fn get_specified_gateway(
|
||||
gateway_identity: IdentityKeyRef,
|
||||
gateways: &[RoutingNode],
|
||||
gateways: &[gateway::LegacyNode],
|
||||
must_use_tls: bool,
|
||||
) -> Result<RoutingNode, ClientCoreError> {
|
||||
) -> Result<gateway::LegacyNode, ClientCoreError> {
|
||||
log::debug!("Requesting specified gateway: {}", gateway_identity);
|
||||
let user_gateway = identity::PublicKey::from_base58_string(gateway_identity)
|
||||
.map_err(ClientCoreError::UnableToCreatePublicKeyFromGatewayId)?;
|
||||
@@ -288,14 +275,7 @@ pub(super) fn get_specified_gateway(
|
||||
.find(|gateway| gateway.identity_key == user_gateway)
|
||||
.ok_or_else(|| ClientCoreError::NoGatewayWithId(gateway_identity.to_string()))?;
|
||||
|
||||
let Some(entry_details) = gateway.entry.as_ref() else {
|
||||
return Err(ClientCoreError::UnsupportedEntry {
|
||||
id: gateway.node_id,
|
||||
identity: gateway.identity().to_string(),
|
||||
});
|
||||
};
|
||||
|
||||
if must_use_tls && entry_details.clients_wss_port.is_none() {
|
||||
if must_use_tls && gateway.clients_wss_port.is_none() {
|
||||
return Err(ClientCoreError::UnsupportedWssProtocol {
|
||||
gateway: gateway_identity.to_string(),
|
||||
});
|
||||
|
||||
@@ -19,7 +19,7 @@ use crate::init::types::{
|
||||
use nym_client_core_gateways_storage::GatewaysDetailsStore;
|
||||
use nym_client_core_gateways_storage::{GatewayDetails, GatewayRegistration};
|
||||
use nym_gateway_client::client::InitGatewayClient;
|
||||
use nym_topology::node::RoutingNode;
|
||||
use nym_topology::gateway;
|
||||
use rand::rngs::OsRng;
|
||||
use rand::{CryptoRng, RngCore};
|
||||
use serde::Serialize;
|
||||
@@ -50,7 +50,7 @@ async fn setup_new_gateway<K, D>(
|
||||
key_store: &K,
|
||||
details_store: &D,
|
||||
selection_specification: GatewaySelectionSpecification,
|
||||
available_gateways: Vec<RoutingNode>,
|
||||
available_gateways: Vec<gateway::LegacyNode>,
|
||||
) -> Result<InitialisationResult, ClientCoreError>
|
||||
where
|
||||
K: KeyStore,
|
||||
|
||||
@@ -13,7 +13,7 @@ use nym_crypto::asymmetric::identity;
|
||||
use nym_gateway_client::client::InitGatewayClient;
|
||||
use nym_gateway_requests::shared_key::SharedGatewayKey;
|
||||
use nym_sphinx::addressing::clients::Recipient;
|
||||
use nym_topology::node::RoutingNode;
|
||||
use nym_topology::gateway;
|
||||
use nym_validator_client::client::IdentityKey;
|
||||
use nym_validator_client::nyxd::AccountId;
|
||||
use serde::Serialize;
|
||||
@@ -38,23 +38,16 @@ pub enum SelectedGateway {
|
||||
|
||||
impl SelectedGateway {
|
||||
pub fn from_topology_node(
|
||||
node: RoutingNode,
|
||||
node: gateway::LegacyNode,
|
||||
must_use_tls: bool,
|
||||
) -> Result<Self, ClientCoreError> {
|
||||
// for now, let's use 'old' behaviour, if you want to change it, you can pass it up the enum stack yourself : )
|
||||
let prefer_ipv6 = false;
|
||||
|
||||
let gateway_listener = if must_use_tls {
|
||||
node.ws_entry_address_tls()
|
||||
node.clients_address_tls()
|
||||
.ok_or(ClientCoreError::UnsupportedWssProtocol {
|
||||
gateway: node.identity_key.to_base58_string(),
|
||||
})?
|
||||
} else {
|
||||
node.ws_entry_address(prefer_ipv6)
|
||||
.ok_or(ClientCoreError::UnsupportedEntry {
|
||||
id: node.node_id,
|
||||
identity: node.identity_key.to_base58_string(),
|
||||
})?
|
||||
node.clients_address()
|
||||
};
|
||||
|
||||
let gateway_listener =
|
||||
@@ -207,7 +200,7 @@ pub enum GatewaySetup {
|
||||
specification: GatewaySelectionSpecification,
|
||||
|
||||
// TODO: seems to be a bit inefficient to pass them by value
|
||||
available_gateways: Vec<RoutingNode>,
|
||||
available_gateways: Vec<gateway::LegacyNode>,
|
||||
},
|
||||
|
||||
ReuseConnection {
|
||||
|
||||
@@ -14,7 +14,8 @@ pub mod error;
|
||||
pub mod init;
|
||||
|
||||
pub use nym_topology::{
|
||||
HardcodedTopologyProvider, NymRouteProvider, NymTopology, NymTopologyError, TopologyProvider,
|
||||
HardcodedTopologyProvider, NymTopology, NymTopologyError, SerializableNymTopology,
|
||||
SerializableTopologyError, TopologyProvider,
|
||||
};
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
|
||||
@@ -8,10 +8,12 @@ license.workspace = true
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
dashmap = { workspace = true }
|
||||
futures = { workspace = true }
|
||||
tracing = { workspace = true }
|
||||
tokio = { workspace = true, features = ["time"] }
|
||||
tokio = { workspace = true, features = ["time", "sync"] }
|
||||
tokio-util = { workspace = true, features = ["codec"], optional = true }
|
||||
tokio-stream = { workspace = true }
|
||||
|
||||
# internal
|
||||
nym-sphinx = { path = "../../nymsphinx" }
|
||||
|
||||
@@ -1,21 +1,24 @@
|
||||
// Copyright 2021 - Nym Technologies SA <contact@nymtech.net>
|
||||
// Copyright 2021-2024 - Nym Technologies SA <contact@nymtech.net>
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
use futures::channel::mpsc;
|
||||
use dashmap::DashMap;
|
||||
use futures::StreamExt;
|
||||
use nym_sphinx::addressing::nodes::NymNodeRoutingAddress;
|
||||
use nym_sphinx::framing::codec::NymCodec;
|
||||
use nym_sphinx::framing::packet::FramedNymPacket;
|
||||
use nym_sphinx::params::PacketType;
|
||||
use nym_sphinx::NymPacket;
|
||||
use std::collections::HashMap;
|
||||
use std::io;
|
||||
use std::net::SocketAddr;
|
||||
use std::sync::atomic::{AtomicU32, Ordering};
|
||||
use std::ops::Deref;
|
||||
use std::sync::atomic::{AtomicU32, AtomicUsize, Ordering};
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
use tokio::net::TcpStream;
|
||||
use tokio::sync::mpsc;
|
||||
use tokio::sync::mpsc::error::TrySendError;
|
||||
use tokio::time::sleep;
|
||||
use tokio_stream::wrappers::ReceiverStream;
|
||||
use tokio_util::codec::Framed;
|
||||
use tracing::*;
|
||||
|
||||
@@ -55,11 +58,37 @@ pub trait SendWithoutResponse {
|
||||
}
|
||||
|
||||
pub struct Client {
|
||||
conn_new: HashMap<NymNodeRoutingAddress, ConnectionSender>,
|
||||
active_connections: ActiveConnections,
|
||||
connections_count: Arc<AtomicUsize>,
|
||||
config: Config,
|
||||
}
|
||||
|
||||
struct ConnectionSender {
|
||||
#[derive(Default, Clone)]
|
||||
pub struct ActiveConnections {
|
||||
inner: Arc<DashMap<NymNodeRoutingAddress, ConnectionSender>>,
|
||||
}
|
||||
|
||||
impl ActiveConnections {
|
||||
pub fn pending_packets(&self) -> usize {
|
||||
self.inner
|
||||
.iter()
|
||||
.map(|sender| {
|
||||
let max_capacity = sender.channel.max_capacity();
|
||||
let capacity = sender.channel.capacity();
|
||||
max_capacity - capacity
|
||||
})
|
||||
.sum()
|
||||
}
|
||||
}
|
||||
|
||||
impl Deref for ActiveConnections {
|
||||
type Target = DashMap<NymNodeRoutingAddress, ConnectionSender>;
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.inner
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ConnectionSender {
|
||||
channel: mpsc::Sender<FramedNymPacket>,
|
||||
current_reconnection_attempt: Arc<AtomicU32>,
|
||||
}
|
||||
@@ -73,46 +102,53 @@ impl ConnectionSender {
|
||||
}
|
||||
}
|
||||
|
||||
impl Client {
|
||||
pub fn new(config: Config) -> Client {
|
||||
Client {
|
||||
conn_new: HashMap::new(),
|
||||
config,
|
||||
struct ManagedConnection {
|
||||
address: SocketAddr,
|
||||
message_receiver: ReceiverStream<FramedNymPacket>,
|
||||
connection_timeout: Duration,
|
||||
current_reconnection: Arc<AtomicU32>,
|
||||
}
|
||||
|
||||
impl ManagedConnection {
|
||||
fn new(
|
||||
address: SocketAddr,
|
||||
message_receiver: mpsc::Receiver<FramedNymPacket>,
|
||||
connection_timeout: Duration,
|
||||
current_reconnection: Arc<AtomicU32>,
|
||||
) -> Self {
|
||||
ManagedConnection {
|
||||
address,
|
||||
message_receiver: ReceiverStream::new(message_receiver),
|
||||
connection_timeout,
|
||||
current_reconnection,
|
||||
}
|
||||
}
|
||||
|
||||
async fn manage_connection(
|
||||
address: SocketAddr,
|
||||
receiver: mpsc::Receiver<FramedNymPacket>,
|
||||
connection_timeout: Duration,
|
||||
current_reconnection: &AtomicU32,
|
||||
) {
|
||||
async fn run(self) {
|
||||
let address = self.address;
|
||||
let connection_fut = TcpStream::connect(address);
|
||||
|
||||
let conn = match tokio::time::timeout(connection_timeout, connection_fut).await {
|
||||
let conn = match tokio::time::timeout(self.connection_timeout, connection_fut).await {
|
||||
Ok(stream_res) => match stream_res {
|
||||
Ok(stream) => {
|
||||
debug!("Managed to establish connection to {}", address);
|
||||
debug!("Managed to establish connection to {}", self.address);
|
||||
// if we managed to connect, reset the reconnection count (whatever it might have been)
|
||||
current_reconnection.store(0, Ordering::Release);
|
||||
self.current_reconnection.store(0, Ordering::Release);
|
||||
Framed::new(stream, NymCodec)
|
||||
}
|
||||
Err(err) => {
|
||||
debug!(
|
||||
"failed to establish connection to {} (err: {})",
|
||||
address, err
|
||||
);
|
||||
debug!("failed to establish connection to {address} (err: {err})",);
|
||||
return;
|
||||
}
|
||||
},
|
||||
Err(_) => {
|
||||
debug!(
|
||||
"failed to connect to {} within {:?}",
|
||||
address, connection_timeout
|
||||
"failed to connect to {address} within {:?}",
|
||||
self.connection_timeout
|
||||
);
|
||||
|
||||
// we failed to connect - increase reconnection attempt
|
||||
current_reconnection.fetch_add(1, Ordering::SeqCst);
|
||||
self.current_reconnection.fetch_add(1, Ordering::SeqCst);
|
||||
return;
|
||||
}
|
||||
};
|
||||
@@ -120,15 +156,28 @@ impl Client {
|
||||
// Take whatever the receiver channel produces and put it on the connection.
|
||||
// We could have as well used conn.send_all(receiver.map(Ok)), but considering we don't care
|
||||
// about neither receiver nor the connection, it doesn't matter which one gets consumed
|
||||
if let Err(err) = receiver.map(Ok).forward(conn).await {
|
||||
warn!("Failed to forward packets to {} - {err}", address);
|
||||
if let Err(err) = self.message_receiver.map(Ok).forward(conn).await {
|
||||
warn!("Failed to forward packets to {address}: {err}");
|
||||
}
|
||||
|
||||
debug!(
|
||||
"connection manager to {} is finished. Either the connection failed or mixnet client got dropped",
|
||||
address
|
||||
"connection manager to {address} is finished. Either the connection failed or mixnet client got dropped",
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
impl Client {
|
||||
pub fn new(config: Config, connections_count: Arc<AtomicUsize>) -> Client {
|
||||
Client {
|
||||
active_connections: Default::default(),
|
||||
connections_count,
|
||||
config,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn active_connections(&self) -> ActiveConnections {
|
||||
self.active_connections.clone()
|
||||
}
|
||||
|
||||
/// If we're trying to reconnect, determine how long we should wait.
|
||||
fn determine_backoff(&self, current_attempt: u32) -> Option<Duration> {
|
||||
@@ -148,7 +197,7 @@ impl Client {
|
||||
}
|
||||
|
||||
fn make_connection(&mut self, address: NymNodeRoutingAddress, pending_packet: FramedNymPacket) {
|
||||
let (mut sender, receiver) = mpsc::channel(self.config.maximum_connection_buffer_size);
|
||||
let (sender, receiver) = mpsc::channel(self.config.maximum_connection_buffer_size);
|
||||
|
||||
// this CAN'T fail because we just created the channel which has a non-zero capacity
|
||||
if self.config.maximum_connection_buffer_size > 0 {
|
||||
@@ -156,15 +205,16 @@ impl Client {
|
||||
}
|
||||
|
||||
// if we already tried to connect to `address` before, grab the current attempt count
|
||||
let current_reconnection_attempt = if let Some(existing) = self.conn_new.get_mut(&address) {
|
||||
existing.channel = sender;
|
||||
Arc::clone(&existing.current_reconnection_attempt)
|
||||
} else {
|
||||
let new_entry = ConnectionSender::new(sender);
|
||||
let current_attempt = Arc::clone(&new_entry.current_reconnection_attempt);
|
||||
self.conn_new.insert(address, new_entry);
|
||||
current_attempt
|
||||
};
|
||||
let current_reconnection_attempt =
|
||||
if let Some(mut existing) = self.active_connections.get_mut(&address) {
|
||||
existing.channel = sender;
|
||||
Arc::clone(&existing.current_reconnection_attempt)
|
||||
} else {
|
||||
let new_entry = ConnectionSender::new(sender);
|
||||
let current_attempt = Arc::clone(&new_entry.current_reconnection_attempt);
|
||||
self.active_connections.insert(address, new_entry);
|
||||
current_attempt
|
||||
};
|
||||
|
||||
// load the actual value.
|
||||
let reconnection_attempt = current_reconnection_attempt.load(Ordering::Acquire);
|
||||
@@ -173,6 +223,7 @@ impl Client {
|
||||
// copy the value before moving into another task
|
||||
let initial_connection_timeout = self.config.initial_connection_timeout;
|
||||
|
||||
let connections_count = self.connections_count.clone();
|
||||
tokio::spawn(async move {
|
||||
// before executing the manager, wait for what was specified, if anything
|
||||
if let Some(backoff) = backoff {
|
||||
@@ -180,13 +231,16 @@ impl Client {
|
||||
sleep(backoff).await;
|
||||
}
|
||||
|
||||
Self::manage_connection(
|
||||
connections_count.fetch_add(1, Ordering::SeqCst);
|
||||
ManagedConnection::new(
|
||||
address.into(),
|
||||
receiver,
|
||||
initial_connection_timeout,
|
||||
¤t_reconnection_attempt,
|
||||
current_reconnection_attempt,
|
||||
)
|
||||
.await
|
||||
.run()
|
||||
.await;
|
||||
connections_count.fetch_sub(1, Ordering::SeqCst);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -201,49 +255,47 @@ impl SendWithoutResponse for Client {
|
||||
trace!("Sending packet to {address:?}");
|
||||
let framed_packet = FramedNymPacket::new(packet, packet_type);
|
||||
|
||||
if let Some(sender) = self.conn_new.get_mut(&address) {
|
||||
if let Err(err) = sender.channel.try_send(framed_packet) {
|
||||
if err.is_full() {
|
||||
debug!("Connection to {} seems to not be able to handle all the traffic - dropping the current packet", address);
|
||||
// it's not a 'big' error, but we did not manage to send the packet
|
||||
// if the queue is full, we can't really do anything but to drop the packet
|
||||
Err(io::Error::new(
|
||||
io::ErrorKind::WouldBlock,
|
||||
"connection queue is full",
|
||||
))
|
||||
} else if err.is_disconnected() {
|
||||
debug!(
|
||||
"Connection to {} seems to be dead. attempting to re-establish it...",
|
||||
address
|
||||
);
|
||||
// it's not a 'big' error, but we did not manage to send the packet, but queue
|
||||
// it up to send it as soon as the connection is re-established
|
||||
self.make_connection(address, err.into_inner());
|
||||
Err(io::Error::new(
|
||||
io::ErrorKind::ConnectionAborted,
|
||||
"reconnection attempt is in progress",
|
||||
))
|
||||
} else {
|
||||
// this can't really happen, but let's safe-guard against it in case something changes in futures library
|
||||
Err(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
"unknown connection buffer error",
|
||||
))
|
||||
}
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
} else {
|
||||
let Some(sender) = self.active_connections.get_mut(&address) else {
|
||||
// there was never a connection to begin with
|
||||
debug!("establishing initial connection to {}", address);
|
||||
// it's not a 'big' error, but we did not manage to send the packet, but queue the packet
|
||||
// for sending for as soon as the connection is created
|
||||
self.make_connection(address, framed_packet);
|
||||
Err(io::Error::new(
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::NotConnected,
|
||||
"connection is in progress",
|
||||
))
|
||||
}
|
||||
));
|
||||
};
|
||||
|
||||
let sending_res = sender.channel.try_send(framed_packet);
|
||||
drop(sender);
|
||||
|
||||
sending_res.map_err(|err| {
|
||||
match err {
|
||||
TrySendError::Full(_) => {
|
||||
debug!("Connection to {address} seems to not be able to handle all the traffic - dropping the current packet");
|
||||
// it's not a 'big' error, but we did not manage to send the packet
|
||||
// if the queue is full, we can't really do anything but to drop the packet
|
||||
io::Error::new(
|
||||
io::ErrorKind::WouldBlock,
|
||||
"connection queue is full",
|
||||
)
|
||||
}
|
||||
TrySendError::Closed(dropped) => {
|
||||
debug!(
|
||||
"Connection to {address} seems to be dead. attempting to re-establish it...",
|
||||
);
|
||||
|
||||
// it's not a 'big' error, but we did not manage to send the packet, but queue
|
||||
// it up to send it as soon as the connection is re-established
|
||||
self.make_connection(address, dropped);
|
||||
io::Error::new(
|
||||
io::ErrorKind::ConnectionAborted,
|
||||
"reconnection attempt is in progress",
|
||||
)
|
||||
}
|
||||
}
|
||||
} )
|
||||
}
|
||||
}
|
||||
|
||||
@@ -252,12 +304,15 @@ mod tests {
|
||||
use super::*;
|
||||
|
||||
fn dummy_client() -> Client {
|
||||
Client::new(Config {
|
||||
initial_reconnection_backoff: Duration::from_millis(10_000),
|
||||
maximum_reconnection_backoff: Duration::from_millis(300_000),
|
||||
initial_connection_timeout: Duration::from_millis(1_500),
|
||||
maximum_connection_buffer_size: 128,
|
||||
})
|
||||
Client::new(
|
||||
Config {
|
||||
initial_reconnection_backoff: Duration::from_millis(10_000),
|
||||
maximum_reconnection_backoff: Duration::from_millis(300_000),
|
||||
initial_connection_timeout: Duration::from_millis(1_500),
|
||||
maximum_connection_buffer_size: 128,
|
||||
},
|
||||
Default::default(),
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -32,10 +32,10 @@ use time::Date;
|
||||
use url::Url;
|
||||
|
||||
pub use crate::nym_api::NymApiClientExt;
|
||||
use nym_mixnet_contract_common::EpochRewardedSet;
|
||||
pub use nym_mixnet_contract_common::{
|
||||
mixnode::MixNodeDetails, GatewayBond, IdentityKey, IdentityKeyRef, NodeId, NymNodeDetails,
|
||||
};
|
||||
|
||||
// re-export the type to not break existing imports
|
||||
pub use crate::coconut::EcashApiClient;
|
||||
|
||||
@@ -367,10 +367,6 @@ impl NymApiClient {
|
||||
Ok(self.nym_api.get_basic_gateways().await?.nodes)
|
||||
}
|
||||
|
||||
pub async fn get_current_rewarded_set(&self) -> Result<EpochRewardedSet, ValidatorClientError> {
|
||||
Ok(self.nym_api.get_rewarded_set().await?.into())
|
||||
}
|
||||
|
||||
/// retrieve basic information for nodes are capable of operating as an entry gateway
|
||||
/// this includes legacy gateways and nym-nodes
|
||||
pub async fn get_all_basic_entry_assigned_nodes(
|
||||
|
||||
@@ -13,7 +13,7 @@ use nym_api_requests::ecash::models::{
|
||||
use nym_api_requests::ecash::VerificationKeyResponse;
|
||||
use nym_api_requests::models::{
|
||||
AnnotationResponse, ApiHealthResponse, LegacyDescribedMixNode, NodePerformanceResponse,
|
||||
NodeRefreshBody, NymNodeDescription, RewardedSetResponse,
|
||||
NodeRefreshBody, NymNodeDescription,
|
||||
};
|
||||
use nym_api_requests::nym_nodes::PaginatedCachedNodesResponse;
|
||||
use nym_api_requests::pagination::PaginatedResponse;
|
||||
@@ -235,15 +235,6 @@ pub trait NymApiClientExt: ApiClient {
|
||||
.await
|
||||
}
|
||||
|
||||
#[instrument(level = "debug", skip(self))]
|
||||
async fn get_rewarded_set(&self) -> Result<RewardedSetResponse, NymAPIError> {
|
||||
self.get_json(
|
||||
&[routes::API_VERSION, "nym-nodes", "rewarded-set"],
|
||||
NO_PARAMS,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
/// retrieve basic information for nodes are capable of operating as an entry gateway
|
||||
/// this includes legacy gateways and nym-nodes
|
||||
#[instrument(level = "debug", skip(self))]
|
||||
@@ -921,7 +912,6 @@ pub trait NymApiClientExt: ApiClient {
|
||||
.await
|
||||
}
|
||||
|
||||
#[instrument(level = "debug", skip(self))]
|
||||
async fn force_refresh_describe_cache(
|
||||
&self,
|
||||
request: &NodeRefreshBody,
|
||||
@@ -934,7 +924,6 @@ pub trait NymApiClientExt: ApiClient {
|
||||
.await
|
||||
}
|
||||
|
||||
#[instrument(level = "debug", skip(self))]
|
||||
async fn issued_ticketbooks_for(
|
||||
&self,
|
||||
expiration_date: Date,
|
||||
@@ -951,7 +940,6 @@ pub trait NymApiClientExt: ApiClient {
|
||||
.await
|
||||
}
|
||||
|
||||
#[instrument(level = "debug", skip(self))]
|
||||
async fn issued_ticketbooks_challenge(
|
||||
&self,
|
||||
expiration_date: Date,
|
||||
|
||||
+12
-15
@@ -26,10 +26,10 @@ use nym_mixnet_contract_common::{
|
||||
reward_params::{Performance, RewardingParams},
|
||||
rewarding::{EstimatedCurrentEpochRewardResponse, PendingRewardResponse},
|
||||
ContractBuildInformation, ContractState, ContractStateParams, CurrentIntervalResponse,
|
||||
CurrentNymNodeVersionResponse, Delegation, EpochEventId, EpochRewardedSet, EpochStatus,
|
||||
GatewayBond, GatewayBondResponse, GatewayOwnershipResponse, HistoricalNymNodeVersionEntry,
|
||||
IdentityKey, IdentityKeyRef, IntervalEventId, MixNodeBond, MixNodeDetails,
|
||||
MixOwnershipResponse, MixnodeDetailsByIdentityResponse, MixnodeDetailsResponse, NodeId,
|
||||
CurrentNymNodeVersionResponse, Delegation, EpochEventId, EpochStatus, GatewayBond,
|
||||
GatewayBondResponse, GatewayOwnershipResponse, HistoricalNymNodeVersionEntry, IdentityKey,
|
||||
IdentityKeyRef, IntervalEventId, MixNodeBond, MixNodeDetails, MixOwnershipResponse,
|
||||
MixnodeDetailsByIdentityResponse, MixnodeDetailsResponse, NodeId,
|
||||
NumberOfPendingEventsResponse, NymNodeBond, NymNodeDetails, NymNodeVersionHistoryResponse,
|
||||
PagedAllDelegationsResponse, PagedDelegatorDelegationsResponse, PagedGatewayResponse,
|
||||
PagedMixnodeBondsResponse, PagedNodeDelegationsResponse, PendingEpochEvent,
|
||||
@@ -670,7 +670,7 @@ impl<T> PagedMixnetQueryClient for T where T: MixnetQueryClient {}
|
||||
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
|
||||
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
|
||||
pub trait MixnetQueryClientExt: MixnetQueryClient {
|
||||
async fn get_rewarded_set(&self) -> Result<EpochRewardedSet, NyxdError> {
|
||||
async fn get_rewarded_set(&self) -> Result<RewardedSet, NyxdError> {
|
||||
let error_response = |message| Err(NyxdError::extension_query_failure("mixnet", message));
|
||||
|
||||
let metadata = self.get_rewarded_set_metadata().await?;
|
||||
@@ -711,16 +711,13 @@ pub trait MixnetQueryClientExt: MixnetQueryClient {
|
||||
return error_response("the nodes assigned for 'standby' returned unexpected epoch_id");
|
||||
}
|
||||
|
||||
Ok(EpochRewardedSet {
|
||||
epoch_id: expected_epoch_id,
|
||||
assignment: RewardedSet {
|
||||
entry_gateways: entry.nodes,
|
||||
exit_gateways: exit.nodes,
|
||||
layer1: layer1.nodes,
|
||||
layer2: layer2.nodes,
|
||||
layer3: layer3.nodes,
|
||||
standby: standby.nodes,
|
||||
},
|
||||
Ok(RewardedSet {
|
||||
entry_gateways: entry.nodes,
|
||||
exit_gateways: exit.nodes,
|
||||
layer1: layer1.nodes,
|
||||
layer2: layer2.nodes,
|
||||
layer3: layer3.nodes,
|
||||
standby: standby.nodes,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,8 +12,7 @@ dirs = { workspace = true, optional = true }
|
||||
handlebars = { workspace = true }
|
||||
log = { workspace = true }
|
||||
serde = { workspace = true, features = ["derive"] }
|
||||
thiserror = { workspace = true }
|
||||
toml = { workspace = true, features = ["display"] }
|
||||
toml = { workspace = true }
|
||||
url = { workspace = true }
|
||||
|
||||
nym-network-defaults = { path = "../network-defaults", features = ["utoipa"] }
|
||||
|
||||
@@ -1,10 +0,0 @@
|
||||
use std::io;
|
||||
use thiserror::Error;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum NymConfigTomlError {
|
||||
#[error(transparent)]
|
||||
FileIoFailure(#[from] io::Error),
|
||||
#[error(transparent)]
|
||||
TomlSerializeFailure(#[from] toml::ser::Error),
|
||||
}
|
||||
@@ -13,7 +13,6 @@ pub use helpers::{parse_urls, OptionalSet};
|
||||
pub use toml::de::Error as TomlDeError;
|
||||
|
||||
pub mod defaults;
|
||||
pub mod error;
|
||||
pub mod helpers;
|
||||
pub mod legacy_helpers;
|
||||
pub mod serde_helpers;
|
||||
@@ -96,42 +95,6 @@ where
|
||||
config.format_to_writer(file)
|
||||
}
|
||||
|
||||
pub fn save_unformatted_config_to_file<C, P>(
|
||||
config: &C,
|
||||
path: P,
|
||||
) -> Result<(), error::NymConfigTomlError>
|
||||
where
|
||||
C: Serialize + ?Sized,
|
||||
P: AsRef<Path>,
|
||||
{
|
||||
let path = path.as_ref();
|
||||
log::info!("saving config file to {}", path.display());
|
||||
|
||||
if let Some(parent) = path.parent() {
|
||||
create_dir_all(parent)?;
|
||||
}
|
||||
|
||||
let mut file = File::create(path)?;
|
||||
|
||||
// TODO: check for whether any of our configs store anything sensitive
|
||||
// and change that to 0o644 instead
|
||||
#[cfg(target_family = "unix")]
|
||||
{
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
|
||||
let mut perms = fs::metadata(path)?.permissions();
|
||||
perms.set_mode(0o600);
|
||||
fs::set_permissions(path, perms)?;
|
||||
}
|
||||
|
||||
// let serde format the TOML in whatever ugly way it chooses
|
||||
// TODO: in https://docs.rs/toml/latest/toml/fn.to_string_pretty.html it recommends using
|
||||
// https://docs.rs/toml_edit/latest/toml_edit/struct.DocumentMut.html to preserve formatting
|
||||
let toml_string = toml::to_string_pretty(config)?;
|
||||
|
||||
Ok(file.write_all(toml_string.as_bytes())?)
|
||||
}
|
||||
|
||||
pub fn deserialize_config_from_toml_str<C>(raw: &str) -> Result<C, TomlDeError>
|
||||
where
|
||||
C: DeserializeOwned,
|
||||
|
||||
@@ -7,7 +7,6 @@
|
||||
use crate::constants::{TOKEN_SUPPLY, UNIT_DELEGATION_BASE};
|
||||
use crate::error::MixnetContractError;
|
||||
use crate::helpers::IntoBaseDecimal;
|
||||
use crate::nym_node::Role;
|
||||
use crate::reward_params::{NodeRewardingParameters, RewardingParams};
|
||||
use crate::rewarding::helpers::truncate_reward;
|
||||
use crate::rewarding::RewardDistribution;
|
||||
@@ -612,16 +611,6 @@ pub enum LegacyMixLayer {
|
||||
Three = 3,
|
||||
}
|
||||
|
||||
impl From<LegacyMixLayer> for Role {
|
||||
fn from(layer: LegacyMixLayer) -> Self {
|
||||
match layer {
|
||||
LegacyMixLayer::One => Role::Layer1,
|
||||
LegacyMixLayer::Two => Role::Layer2,
|
||||
LegacyMixLayer::Three => Role::Layer3,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<LegacyMixLayer> for String {
|
||||
fn from(layer: LegacyMixLayer) -> Self {
|
||||
(layer as u8).to_string()
|
||||
|
||||
@@ -863,11 +863,4 @@ pub enum QueryMsg {
|
||||
pub struct MigrateMsg {
|
||||
pub unsafe_skip_state_updates: Option<bool>,
|
||||
pub vesting_contract_address: Option<String>,
|
||||
pub current_nym_node_semver: String,
|
||||
|
||||
#[serde(default)]
|
||||
pub version_score_weights: OutdatedVersionWeights,
|
||||
|
||||
#[serde(default)]
|
||||
pub version_score_params: VersionScoreFormulaParams,
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
|
||||
use crate::config_score::{ConfigScoreParams, OutdatedVersionWeights, VersionScoreFormulaParams};
|
||||
use crate::nym_node::Role;
|
||||
use crate::EpochId;
|
||||
use contracts_common::Percent;
|
||||
use cosmwasm_schema::cw_serde;
|
||||
use cosmwasm_std::Coin;
|
||||
@@ -33,23 +32,6 @@ impl RoleAssignment {
|
||||
}
|
||||
}
|
||||
|
||||
#[cw_serde]
|
||||
#[derive(Default)]
|
||||
pub struct EpochRewardedSet {
|
||||
pub epoch_id: EpochId,
|
||||
|
||||
pub assignment: RewardedSet,
|
||||
}
|
||||
|
||||
impl From<(EpochId, RewardedSet)> for EpochRewardedSet {
|
||||
fn from((epoch_id, assignment): (EpochId, RewardedSet)) -> Self {
|
||||
EpochRewardedSet {
|
||||
epoch_id,
|
||||
assignment,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cw_serde]
|
||||
#[derive(Default)]
|
||||
pub struct RewardedSet {
|
||||
@@ -87,29 +69,6 @@ impl RewardedSet {
|
||||
pub fn rewarded_set_size(&self) -> usize {
|
||||
self.active_set_size() + self.standby.len()
|
||||
}
|
||||
|
||||
pub fn get_role(&self, node_id: NodeId) -> Option<Role> {
|
||||
// given each role has ~100 entries in them, doing linear lookup with vec should be fine
|
||||
if self.entry_gateways.contains(&node_id) {
|
||||
return Some(Role::EntryGateway);
|
||||
}
|
||||
if self.exit_gateways.contains(&node_id) {
|
||||
return Some(Role::ExitGateway);
|
||||
}
|
||||
if self.layer1.contains(&node_id) {
|
||||
return Some(Role::Layer1);
|
||||
}
|
||||
if self.layer2.contains(&node_id) {
|
||||
return Some(Role::Layer2);
|
||||
}
|
||||
if self.layer3.contains(&node_id) {
|
||||
return Some(Role::Layer3);
|
||||
}
|
||||
if self.standby.contains(&node_id) {
|
||||
return Some(Role::Standby);
|
||||
}
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
#[cw_serde]
|
||||
|
||||
+195
-195
@@ -192,28 +192,6 @@ impl Client {
|
||||
&self.base_url
|
||||
}
|
||||
|
||||
pub fn create_request<B, K, V>(
|
||||
&self,
|
||||
method: reqwest::Method,
|
||||
path: PathSegments<'_>,
|
||||
params: Params<'_, K, V>,
|
||||
json_body: Option<&B>,
|
||||
) -> RequestBuilder
|
||||
where
|
||||
B: Serialize + ?Sized,
|
||||
K: AsRef<str>,
|
||||
V: AsRef<str>,
|
||||
{
|
||||
let url = sanitize_url(&self.base_url, path, params);
|
||||
let mut request = self.reqwest_client.request(method.clone(), url);
|
||||
|
||||
if let Some(body) = json_body {
|
||||
request = request.json(body);
|
||||
}
|
||||
|
||||
request
|
||||
}
|
||||
|
||||
pub fn create_get_request<K, V>(
|
||||
&self,
|
||||
path: PathSegments<'_>,
|
||||
@@ -227,6 +205,38 @@ impl Client {
|
||||
self.reqwest_client.get(url)
|
||||
}
|
||||
|
||||
#[instrument(level = "debug", skip_all, fields(path=?path))]
|
||||
async fn send_get_request<K, V, E>(
|
||||
&self,
|
||||
path: PathSegments<'_>,
|
||||
params: Params<'_, K, V>,
|
||||
) -> Result<Response, HttpClientError<E>>
|
||||
where
|
||||
K: AsRef<str>,
|
||||
V: AsRef<str>,
|
||||
E: Display,
|
||||
{
|
||||
tracing::trace!("Sending GET request");
|
||||
let url = sanitize_url(&self.base_url, path, params);
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
{
|
||||
Ok(
|
||||
wasmtimer::tokio::timeout(
|
||||
self.request_timeout,
|
||||
self.reqwest_client.get(url).send(),
|
||||
)
|
||||
.await
|
||||
.map_err(|_timeout| HttpClientError::RequestTimeout)??,
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
{
|
||||
Ok(self.reqwest_client.get(url).send().await?)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn create_post_request<B, K, V>(
|
||||
&self,
|
||||
path: PathSegments<'_>,
|
||||
@@ -242,6 +252,36 @@ impl Client {
|
||||
self.reqwest_client.post(url).json(json_body)
|
||||
}
|
||||
|
||||
async fn send_post_request<B, K, V, E>(
|
||||
&self,
|
||||
path: PathSegments<'_>,
|
||||
params: Params<'_, K, V>,
|
||||
json_body: &B,
|
||||
) -> Result<Response, HttpClientError<E>>
|
||||
where
|
||||
B: Serialize + ?Sized,
|
||||
K: AsRef<str>,
|
||||
V: AsRef<str>,
|
||||
E: Display,
|
||||
{
|
||||
let url = sanitize_url(&self.base_url, path, params);
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
{
|
||||
Ok(wasmtimer::tokio::timeout(
|
||||
self.request_timeout,
|
||||
self.reqwest_client.post(url).json(json_body).send(),
|
||||
)
|
||||
.await
|
||||
.map_err(|_timeout| HttpClientError::RequestTimeout)??)
|
||||
}
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
{
|
||||
Ok(self.reqwest_client.post(url).json(json_body).send().await?)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn create_delete_request<K, V>(
|
||||
&self,
|
||||
path: PathSegments<'_>,
|
||||
@@ -255,6 +295,35 @@ impl Client {
|
||||
self.reqwest_client.delete(url)
|
||||
}
|
||||
|
||||
pub async fn send_delete_request<K, V, E>(
|
||||
&self,
|
||||
path: PathSegments<'_>,
|
||||
params: Params<'_, K, V>,
|
||||
) -> Result<Response, HttpClientError<E>>
|
||||
where
|
||||
K: AsRef<str>,
|
||||
V: AsRef<str>,
|
||||
E: Display,
|
||||
{
|
||||
tracing::trace!("Sending DELETE request");
|
||||
let url = sanitize_url(&self.base_url, path, params);
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
{
|
||||
Ok(wasmtimer::tokio::timeout(
|
||||
self.request_timeout,
|
||||
self.reqwest_client.delete(url).send(),
|
||||
)
|
||||
.await
|
||||
.map_err(|_timeout| HttpClientError::RequestTimeout)??)
|
||||
}
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
{
|
||||
Ok(self.reqwest_client.delete(url).send().await?)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn create_patch_request<B, K, V>(
|
||||
&self,
|
||||
path: PathSegments<'_>,
|
||||
@@ -270,87 +339,6 @@ impl Client {
|
||||
self.reqwest_client.patch(url).json(json_body)
|
||||
}
|
||||
|
||||
async fn send_request<B, K, V, E>(
|
||||
&self,
|
||||
method: reqwest::Method,
|
||||
path: PathSegments<'_>,
|
||||
params: Params<'_, K, V>,
|
||||
json_body: Option<&B>,
|
||||
) -> Result<Response, HttpClientError<E>>
|
||||
where
|
||||
B: Serialize + ?Sized,
|
||||
K: AsRef<str>,
|
||||
V: AsRef<str>,
|
||||
E: Display,
|
||||
{
|
||||
let url = sanitize_url(&self.base_url, path, params);
|
||||
|
||||
let mut request = self.reqwest_client.request(method.clone(), url);
|
||||
|
||||
if let Some(body) = json_body {
|
||||
request = request.json(body);
|
||||
}
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
{
|
||||
Ok(
|
||||
wasmtimer::tokio::timeout(self.request_timeout, request.send())
|
||||
.await
|
||||
.map_err(|_timeout| HttpClientError::RequestTimeout)??,
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
{
|
||||
Ok(request.send().await?)
|
||||
}
|
||||
}
|
||||
|
||||
#[instrument(level = "debug", skip_all, fields(path=?path))]
|
||||
async fn send_get_request<K, V, E>(
|
||||
&self,
|
||||
path: PathSegments<'_>,
|
||||
params: Params<'_, K, V>,
|
||||
) -> Result<Response, HttpClientError<E>>
|
||||
where
|
||||
K: AsRef<str>,
|
||||
V: AsRef<str>,
|
||||
E: Display,
|
||||
{
|
||||
self.send_request(reqwest::Method::GET, path, params, None::<&()>)
|
||||
.await
|
||||
}
|
||||
|
||||
async fn send_post_request<B, K, V, E>(
|
||||
&self,
|
||||
path: PathSegments<'_>,
|
||||
params: Params<'_, K, V>,
|
||||
json_body: &B,
|
||||
) -> Result<Response, HttpClientError<E>>
|
||||
where
|
||||
B: Serialize + ?Sized,
|
||||
K: AsRef<str>,
|
||||
V: AsRef<str>,
|
||||
E: Display,
|
||||
{
|
||||
self.send_request(reqwest::Method::POST, path, params, Some(json_body))
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn send_delete_request<K, V, E>(
|
||||
&self,
|
||||
path: PathSegments<'_>,
|
||||
params: Params<'_, K, V>,
|
||||
) -> Result<Response, HttpClientError<E>>
|
||||
where
|
||||
K: AsRef<str>,
|
||||
V: AsRef<str>,
|
||||
E: Display,
|
||||
{
|
||||
self.send_request(reqwest::Method::DELETE, path, params, None::<&()>)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn send_patch_request<B, K, V, E>(
|
||||
&self,
|
||||
path: PathSegments<'_>,
|
||||
@@ -363,8 +351,27 @@ impl Client {
|
||||
V: AsRef<str>,
|
||||
E: Display,
|
||||
{
|
||||
self.send_request(reqwest::Method::PATCH, path, params, Some(json_body))
|
||||
let url = sanitize_url(&self.base_url, path, params);
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
{
|
||||
Ok(wasmtimer::tokio::timeout(
|
||||
self.request_timeout,
|
||||
self.reqwest_client.patch(url).json(json_body).send(),
|
||||
)
|
||||
.await
|
||||
.map_err(|_timeout| HttpClientError::RequestTimeout)??)
|
||||
}
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
{
|
||||
Ok(self
|
||||
.reqwest_client
|
||||
.patch(url)
|
||||
.json(json_body)
|
||||
.send()
|
||||
.await?)
|
||||
}
|
||||
}
|
||||
|
||||
#[instrument(level = "debug", skip_all)]
|
||||
@@ -432,39 +439,6 @@ impl Client {
|
||||
parse_response(res, true).await
|
||||
}
|
||||
|
||||
async fn call_json_endpoint<B, T, S, E>(
|
||||
&self,
|
||||
method: reqwest::Method,
|
||||
endpoint: S,
|
||||
json_body: Option<&B>,
|
||||
) -> Result<T, HttpClientError<E>>
|
||||
where
|
||||
B: Serialize + ?Sized,
|
||||
for<'a> T: Deserialize<'a>,
|
||||
E: Display + DeserializeOwned,
|
||||
S: AsRef<str>,
|
||||
{
|
||||
let mut request = self
|
||||
.reqwest_client
|
||||
.request(method.clone(), self.base_url.join(endpoint.as_ref())?);
|
||||
|
||||
if let Some(body) = json_body {
|
||||
request = request.json(body);
|
||||
}
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
let res = {
|
||||
wasmtimer::tokio::timeout(self.request_timeout, request.send())
|
||||
.await
|
||||
.map_err(|_timeout| HttpClientError::RequestTimeout)??
|
||||
};
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
let res = { request.send().await? };
|
||||
|
||||
parse_response(res, false).await
|
||||
}
|
||||
|
||||
#[instrument(level = "debug", skip_all)]
|
||||
pub async fn get_json_endpoint<T, S, E>(&self, endpoint: S) -> Result<T, HttpClientError<E>>
|
||||
where
|
||||
@@ -472,8 +446,27 @@ impl Client {
|
||||
E: Display + DeserializeOwned,
|
||||
S: AsRef<str>,
|
||||
{
|
||||
self.call_json_endpoint(reqwest::Method::GET, endpoint, None::<&()>)
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
let res = {
|
||||
wasmtimer::tokio::timeout(
|
||||
self.request_timeout,
|
||||
self.reqwest_client
|
||||
.get(self.base_url.join(endpoint.as_ref())?)
|
||||
.send(),
|
||||
)
|
||||
.await
|
||||
.map_err(|_timeout| HttpClientError::RequestTimeout)??
|
||||
};
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
let res = {
|
||||
self.reqwest_client
|
||||
.get(self.base_url.join(endpoint.as_ref())?)
|
||||
.send()
|
||||
.await?
|
||||
};
|
||||
|
||||
parse_response(res, false).await
|
||||
}
|
||||
|
||||
pub async fn post_json_endpoint<B, T, S, E>(
|
||||
@@ -487,8 +480,29 @@ impl Client {
|
||||
E: Display + DeserializeOwned,
|
||||
S: AsRef<str>,
|
||||
{
|
||||
self.call_json_endpoint(reqwest::Method::POST, endpoint, Some(json_body))
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
let res = {
|
||||
wasmtimer::tokio::timeout(
|
||||
self.request_timeout,
|
||||
self.reqwest_client
|
||||
.post(self.base_url.join(endpoint.as_ref())?)
|
||||
.json(json_body)
|
||||
.send(),
|
||||
)
|
||||
.await
|
||||
.map_err(|_timeout| HttpClientError::RequestTimeout)??
|
||||
};
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
let res = {
|
||||
self.reqwest_client
|
||||
.post(self.base_url.join(endpoint.as_ref())?)
|
||||
.json(json_body)
|
||||
.send()
|
||||
.await?
|
||||
};
|
||||
|
||||
parse_response(res, true).await
|
||||
}
|
||||
|
||||
pub async fn delete_json_endpoint<T, S, E>(&self, endpoint: S) -> Result<T, HttpClientError<E>>
|
||||
@@ -497,8 +511,27 @@ impl Client {
|
||||
E: Display + DeserializeOwned,
|
||||
S: AsRef<str>,
|
||||
{
|
||||
self.call_json_endpoint(reqwest::Method::DELETE, endpoint, None::<&()>)
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
let res = {
|
||||
wasmtimer::tokio::timeout(
|
||||
self.request_timeout,
|
||||
self.reqwest_client
|
||||
.delete(self.base_url.join(endpoint.as_ref())?)
|
||||
.send(),
|
||||
)
|
||||
.await
|
||||
.map_err(|_timeout| HttpClientError::RequestTimeout)??
|
||||
};
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
let res = {
|
||||
self.reqwest_client
|
||||
.delete(self.base_url.join(endpoint.as_ref())?)
|
||||
.send()
|
||||
.await?
|
||||
};
|
||||
|
||||
parse_response(res, false).await
|
||||
}
|
||||
|
||||
pub async fn patch_json_endpoint<B, T, S, E>(
|
||||
@@ -512,8 +545,29 @@ impl Client {
|
||||
E: Display + DeserializeOwned,
|
||||
S: AsRef<str>,
|
||||
{
|
||||
self.call_json_endpoint(reqwest::Method::PATCH, endpoint, Some(json_body))
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
let res = {
|
||||
wasmtimer::tokio::timeout(
|
||||
self.request_timeout,
|
||||
self.reqwest_client
|
||||
.patch(self.base_url.join(endpoint.as_ref())?)
|
||||
.json(json_body)
|
||||
.send(),
|
||||
)
|
||||
.await
|
||||
.map_err(|_timeout| HttpClientError::RequestTimeout)??
|
||||
};
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
let res = {
|
||||
self.reqwest_client
|
||||
.patch(self.base_url.join(endpoint.as_ref())?)
|
||||
.json(json_body)
|
||||
.send()
|
||||
.await?
|
||||
};
|
||||
|
||||
parse_response(res, true).await
|
||||
}
|
||||
}
|
||||
|
||||
@@ -558,19 +612,6 @@ pub trait ApiClient {
|
||||
V: AsRef<str> + Sync,
|
||||
E: Display + DeserializeOwned;
|
||||
|
||||
async fn patch_json<B, T, K, V, E>(
|
||||
&self,
|
||||
path: PathSegments<'_>,
|
||||
params: Params<'_, K, V>,
|
||||
json_body: &B,
|
||||
) -> Result<T, HttpClientError<E>>
|
||||
where
|
||||
B: Serialize + ?Sized + Sync,
|
||||
for<'a> T: Deserialize<'a>,
|
||||
K: AsRef<str> + Sync,
|
||||
V: AsRef<str> + Sync,
|
||||
E: Display + DeserializeOwned;
|
||||
|
||||
/// `get` json data from the provided absolute endpoint, i.e. for example `"/api/v1/mixnodes?since=12345"`
|
||||
async fn get_json_from<T, S, E>(&self, endpoint: S) -> Result<T, HttpClientError<E>>
|
||||
where
|
||||
@@ -594,17 +635,6 @@ pub trait ApiClient {
|
||||
for<'a> T: Deserialize<'a>,
|
||||
E: Display + DeserializeOwned,
|
||||
S: AsRef<str> + Sync + Send;
|
||||
|
||||
async fn patch_json_data_at<B, T, S, E>(
|
||||
&self,
|
||||
endpoint: S,
|
||||
json_body: &B,
|
||||
) -> Result<T, HttpClientError<E>>
|
||||
where
|
||||
B: Serialize + ?Sized + Sync,
|
||||
for<'a> T: Deserialize<'a>,
|
||||
E: Display + DeserializeOwned,
|
||||
S: AsRef<str> + Sync + Send;
|
||||
}
|
||||
|
||||
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
|
||||
@@ -654,22 +684,6 @@ impl ApiClient for Client {
|
||||
self.delete_json(path, params).await
|
||||
}
|
||||
|
||||
async fn patch_json<B, T, K, V, E>(
|
||||
&self,
|
||||
path: PathSegments<'_>,
|
||||
params: Params<'_, K, V>,
|
||||
json_body: &B,
|
||||
) -> Result<T, HttpClientError<E>>
|
||||
where
|
||||
B: Serialize + ?Sized + Sync,
|
||||
for<'a> T: Deserialize<'a>,
|
||||
K: AsRef<str> + Sync,
|
||||
V: AsRef<str> + Sync,
|
||||
E: Display + DeserializeOwned,
|
||||
{
|
||||
self.patch_json(path, params, json_body).await
|
||||
}
|
||||
|
||||
async fn get_json_from<T, S, E>(&self, endpoint: S) -> Result<T, HttpClientError<E>>
|
||||
where
|
||||
for<'a> T: Deserialize<'a>,
|
||||
@@ -701,20 +715,6 @@ impl ApiClient for Client {
|
||||
{
|
||||
self.delete_json_endpoint(endpoint).await
|
||||
}
|
||||
|
||||
async fn patch_json_data_at<B, T, S, E>(
|
||||
&self,
|
||||
endpoint: S,
|
||||
json_body: &B,
|
||||
) -> Result<T, HttpClientError<E>>
|
||||
where
|
||||
B: Serialize + ?Sized + Sync,
|
||||
for<'a> T: Deserialize<'a>,
|
||||
E: Display + DeserializeOwned,
|
||||
S: AsRef<str> + Sync + Send,
|
||||
{
|
||||
self.patch_json_endpoint(endpoint, json_body).await
|
||||
}
|
||||
}
|
||||
|
||||
// utility function that should solve the double slash problem in API urls forever.
|
||||
|
||||
@@ -63,7 +63,6 @@ impl From<v6::request::StaticConnectRequest> for v7::request::StaticConnectReque
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(deprecated)]
|
||||
impl From<v6::request::DynamicConnectRequest> for v7::request::DynamicConnectRequest {
|
||||
fn from(dynamic_connect_request: v6::request::DynamicConnectRequest) -> Self {
|
||||
Self {
|
||||
|
||||
@@ -51,7 +51,6 @@ impl IpPacketRequest {
|
||||
)
|
||||
}
|
||||
|
||||
#[allow(deprecated)]
|
||||
pub fn new_dynamic_connect_request(
|
||||
reply_to: Recipient,
|
||||
reply_to_hops: Option<u8>,
|
||||
@@ -286,9 +285,6 @@ pub struct DynamicConnectRequest {
|
||||
|
||||
// The number of mix node hops that responses should take, in addition to the entry and exit
|
||||
// node. Zero means only client -> entry -> exit -> client.
|
||||
#[deprecated(
|
||||
note = "clients can no longer control number of hops to use. this field is scheduled for removal in V8"
|
||||
)]
|
||||
pub reply_to_hops: Option<u8>,
|
||||
|
||||
// The average delay at each mix node, in milliseconds. Currently this is not supported by the
|
||||
|
||||
@@ -2,9 +2,10 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
use crate::error::NetworkTestingError;
|
||||
use crate::node::{NodeType, TestableNode};
|
||||
use crate::node::TestableNode;
|
||||
use crate::NodeId;
|
||||
use nym_sphinx::message::NymMessage;
|
||||
use nym_topology::node::RoutingNode;
|
||||
use nym_topology::{gateway, mix};
|
||||
use serde::de::DeserializeOwned;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
@@ -25,76 +26,73 @@ pub struct TestMessage<T = Empty> {
|
||||
}
|
||||
|
||||
impl<T> TestMessage<T> {
|
||||
pub fn new(tested_node: TestableNode, msg_id: u32, total_msgs: u32, ext: T) -> Self {
|
||||
pub fn new<N: Into<TestableNode>>(node: N, msg_id: u32, total_msgs: u32, ext: T) -> Self {
|
||||
TestMessage {
|
||||
tested_node,
|
||||
tested_node: node.into(),
|
||||
msg_id,
|
||||
total_msgs,
|
||||
ext,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new_mix(node: &RoutingNode, msg_id: u32, total_msgs: u32, ext: T) -> Self {
|
||||
Self::new(
|
||||
TestableNode::new_routing(node, NodeType::Mixnode),
|
||||
msg_id,
|
||||
total_msgs,
|
||||
ext,
|
||||
)
|
||||
pub fn new_mix(node: &mix::LegacyNode, msg_id: u32, total_msgs: u32, ext: T) -> Self {
|
||||
Self::new(node, msg_id, total_msgs, ext)
|
||||
}
|
||||
|
||||
pub fn new_gateway(node: &RoutingNode, msg_id: u32, total_msgs: u32, ext: T) -> Self {
|
||||
Self::new(
|
||||
TestableNode::new_routing(node, NodeType::Gateway),
|
||||
msg_id,
|
||||
total_msgs,
|
||||
ext,
|
||||
)
|
||||
// pub fn new_gateway(node: &gateway::Node, msg_id: u32, total_msgs: u32, ext: T) -> Self {
|
||||
// Self::new(node, msg_id, total_msgs, ext)
|
||||
// }
|
||||
|
||||
pub fn new_serialized<N>(
|
||||
node: N,
|
||||
msg_id: u32,
|
||||
total_msgs: u32,
|
||||
ext: T,
|
||||
) -> Result<Vec<u8>, NetworkTestingError>
|
||||
where
|
||||
N: Into<TestableNode>,
|
||||
T: Serialize,
|
||||
{
|
||||
Self::new(node, msg_id, total_msgs, ext).as_bytes()
|
||||
}
|
||||
|
||||
pub fn new_plaintexts(
|
||||
node: TestableNode,
|
||||
pub fn new_plaintexts<N>(
|
||||
node: &N,
|
||||
total_msgs: u32,
|
||||
ext: T,
|
||||
) -> Result<Vec<Vec<u8>>, NetworkTestingError>
|
||||
where
|
||||
for<'a> &'a N: Into<TestableNode>,
|
||||
T: Serialize + Clone,
|
||||
{
|
||||
let mut msgs = Vec::with_capacity(total_msgs as usize);
|
||||
for msg_id in 1..=total_msgs {
|
||||
msgs.push(Self::new(node.clone(), msg_id, total_msgs, ext.clone()).as_bytes()?)
|
||||
msgs.push(Self::new(node, msg_id, total_msgs, ext.clone()).as_bytes()?)
|
||||
}
|
||||
Ok(msgs)
|
||||
}
|
||||
|
||||
pub fn mix_plaintexts(
|
||||
node: &RoutingNode,
|
||||
node: &mix::LegacyNode,
|
||||
total_msgs: u32,
|
||||
ext: T,
|
||||
) -> Result<Vec<Vec<u8>>, NetworkTestingError>
|
||||
where
|
||||
T: Serialize + Clone,
|
||||
{
|
||||
Self::new_plaintexts(
|
||||
TestableNode::new_routing(node, NodeType::Mixnode),
|
||||
total_msgs,
|
||||
ext,
|
||||
)
|
||||
Self::new_plaintexts(node, total_msgs, ext)
|
||||
}
|
||||
|
||||
pub fn legacy_gateway_plaintexts(
|
||||
node: &RoutingNode,
|
||||
node: &gateway::LegacyNode,
|
||||
node_id: NodeId,
|
||||
total_msgs: u32,
|
||||
ext: T,
|
||||
) -> Result<Vec<Vec<u8>>, NetworkTestingError>
|
||||
where
|
||||
T: Serialize + Clone,
|
||||
{
|
||||
Self::new_plaintexts(
|
||||
TestableNode::new_routing(node, NodeType::Gateway),
|
||||
total_msgs,
|
||||
ext,
|
||||
)
|
||||
Self::new_plaintexts(&(node, node_id), total_msgs, ext)
|
||||
}
|
||||
|
||||
pub fn as_json_string(&self) -> Result<String, NetworkTestingError>
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
use crate::NodeId;
|
||||
use nym_topology::node::RoutingNode;
|
||||
use nym_topology::{gateway, mix};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fmt::{Display, Formatter};
|
||||
|
||||
@@ -24,14 +24,6 @@ impl TestableNode {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new_routing(routing_node: &RoutingNode, typ: NodeType) -> Self {
|
||||
TestableNode::new(
|
||||
routing_node.identity_key.to_base58_string(),
|
||||
typ,
|
||||
routing_node.node_id,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn new_mixnode(encoded_identity: String, node_id: NodeId) -> Self {
|
||||
TestableNode::new(encoded_identity, NodeType::Mixnode, node_id)
|
||||
}
|
||||
@@ -45,6 +37,38 @@ impl TestableNode {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> From<&'a mix::LegacyNode> for TestableNode {
|
||||
fn from(value: &'a mix::LegacyNode) -> Self {
|
||||
TestableNode {
|
||||
encoded_identity: value.identity_key.to_base58_string(),
|
||||
typ: NodeType::Mixnode,
|
||||
node_id: value.mix_id,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> From<(&'a gateway::LegacyNode, NodeId)> for TestableNode {
|
||||
fn from((gateway, node_id): (&'a gateway::LegacyNode, NodeId)) -> Self {
|
||||
(&(gateway, node_id)).into()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> From<&'a (gateway::LegacyNode, NodeId)> for TestableNode {
|
||||
fn from((gateway, node_id): &'a (gateway::LegacyNode, NodeId)) -> Self {
|
||||
(gateway, *node_id).into()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'b> From<&'a (&'b gateway::LegacyNode, NodeId)> for TestableNode {
|
||||
fn from((gateway, node_id): &'a (&'b gateway::LegacyNode, NodeId)) -> Self {
|
||||
TestableNode {
|
||||
encoded_identity: gateway.identity_key.to_base58_string(),
|
||||
typ: NodeType::Gateway,
|
||||
node_id: *node_id,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for TestableNode {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
write!(
|
||||
|
||||
@@ -2,22 +2,21 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
use crate::error::NetworkTestingError;
|
||||
use crate::Empty;
|
||||
use crate::NodeId;
|
||||
use crate::TestMessage;
|
||||
use nym_sphinx::acknowledgements::AckKey;
|
||||
use nym_sphinx::addressing::clients::Recipient;
|
||||
use nym_sphinx::message::NymMessage;
|
||||
use nym_sphinx::params::PacketSize;
|
||||
use nym_sphinx::params::{PacketSize, DEFAULT_NUM_MIX_HOPS};
|
||||
use nym_sphinx::preparer::{FragmentPreparer, PreparedFragment};
|
||||
use nym_sphinx_params::PacketType;
|
||||
use nym_topology::node::RoutingNode;
|
||||
use nym_topology::{NymRouteProvider, NymTopology, Role};
|
||||
use nym_topology::{gateway, mix, NymTopology};
|
||||
use rand::{CryptoRng, Rng};
|
||||
use serde::Serialize;
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
|
||||
pub use nym_topology::node::LegacyMixLayer;
|
||||
|
||||
pub struct NodeTester<R> {
|
||||
rng: R,
|
||||
|
||||
@@ -39,6 +38,10 @@ pub struct NodeTester<R> {
|
||||
/// Average delay an acknowledgement packet is going to get delay at a single mixnode.
|
||||
average_ack_delay: Duration,
|
||||
|
||||
/// Number of mix hops each packet ('real' message, ack, reply) is expected to take.
|
||||
/// Note that it does not include gateway hops.
|
||||
num_mix_hops: u8,
|
||||
|
||||
// while acks are going to be ignored they still need to be constructed
|
||||
// so that the gateway would be able to correctly process and forward the message
|
||||
ack_key: Arc<AckKey>,
|
||||
@@ -67,27 +70,41 @@ where
|
||||
deterministic_route_selection,
|
||||
average_packet_delay,
|
||||
average_ack_delay,
|
||||
num_mix_hops: DEFAULT_NUM_MIX_HOPS,
|
||||
ack_key,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn testable_mix_topology(&self, layer: LegacyMixLayer, node: &RoutingNode) -> NymTopology {
|
||||
/// Allows setting non-default number of expected mix hops in the network.
|
||||
#[allow(dead_code)]
|
||||
pub fn with_mix_hops(mut self, hops: u8) -> Self {
|
||||
self.num_mix_hops = hops;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn testable_mix_topology(&self, node: &mix::LegacyNode) -> NymTopology {
|
||||
let mut topology = self.base_topology.clone();
|
||||
topology.set_testable_node(layer.into(), node.clone());
|
||||
topology.set_mixes_in_layer(node.layer as u8, vec![node.clone()]);
|
||||
topology
|
||||
}
|
||||
|
||||
pub fn testable_gateway_topology(&self, node: &RoutingNode) -> NymTopology {
|
||||
pub fn testable_gateway_topology(&self, gateway: &gateway::LegacyNode) -> NymTopology {
|
||||
let mut topology = self.base_topology.clone();
|
||||
topology.set_testable_node(Role::EntryGateway, node.clone());
|
||||
topology.set_testable_node(Role::ExitGateway, node.clone());
|
||||
topology.set_gateways(vec![gateway.clone()]);
|
||||
topology
|
||||
}
|
||||
|
||||
pub fn simple_mixnode_test_packets(
|
||||
&mut self,
|
||||
mix: &mix::LegacyNode,
|
||||
test_packets: u32,
|
||||
) -> Result<Vec<PreparedFragment>, NetworkTestingError> {
|
||||
self.mixnode_test_packets(mix, Empty, test_packets, None)
|
||||
}
|
||||
|
||||
pub fn mixnode_test_packets<T>(
|
||||
&mut self,
|
||||
mix: &RoutingNode,
|
||||
legacy_mix_layer: LegacyMixLayer,
|
||||
mix: &mix::LegacyNode,
|
||||
msg_ext: T,
|
||||
test_packets: u32,
|
||||
custom_recipient: Option<Recipient>,
|
||||
@@ -95,9 +112,7 @@ where
|
||||
where
|
||||
T: Serialize + Clone,
|
||||
{
|
||||
let ephemeral_topology =
|
||||
NymRouteProvider::from(self.testable_mix_topology(legacy_mix_layer, mix))
|
||||
.with_ignore_egress_epoch_roles(true);
|
||||
let ephemeral_topology = self.testable_mix_topology(mix);
|
||||
|
||||
let mut packets = Vec::with_capacity(test_packets as usize);
|
||||
for plaintext in TestMessage::mix_plaintexts(mix, test_packets, msg_ext)? {
|
||||
@@ -113,7 +128,7 @@ where
|
||||
|
||||
pub fn mixnodes_test_packets<T>(
|
||||
&mut self,
|
||||
nodes: &[(LegacyMixLayer, RoutingNode)],
|
||||
nodes: &[mix::LegacyNode],
|
||||
msg_ext: T,
|
||||
test_packets: u32,
|
||||
custom_recipient: Option<Recipient>,
|
||||
@@ -122,10 +137,9 @@ where
|
||||
T: Serialize + Clone,
|
||||
{
|
||||
let mut packets = Vec::new();
|
||||
for (layer, node) in nodes {
|
||||
for node in nodes {
|
||||
packets.append(&mut self.mixnode_test_packets(
|
||||
node,
|
||||
*layer,
|
||||
msg_ext.clone(),
|
||||
test_packets,
|
||||
custom_recipient,
|
||||
@@ -135,10 +149,9 @@ where
|
||||
Ok(packets)
|
||||
}
|
||||
|
||||
pub fn existing_identity_mixnode_test_packets<T>(
|
||||
pub fn existing_mixnode_test_packets<T>(
|
||||
&mut self,
|
||||
encoded_mix_identity: String,
|
||||
layer: LegacyMixLayer,
|
||||
mix_id: NodeId,
|
||||
msg_ext: T,
|
||||
test_packets: u32,
|
||||
custom_recipient: Option<Recipient>,
|
||||
@@ -146,30 +159,39 @@ where
|
||||
where
|
||||
T: Serialize + Clone,
|
||||
{
|
||||
let Ok(identity) = encoded_mix_identity.parse() else {
|
||||
let Some(node) = self.base_topology.find_mix(mix_id) else {
|
||||
return Err(NetworkTestingError::NonExistentMixnode { mix_id });
|
||||
};
|
||||
|
||||
self.mixnode_test_packets(&node.clone(), msg_ext, test_packets, custom_recipient)
|
||||
}
|
||||
|
||||
pub fn existing_identity_mixnode_test_packets<T>(
|
||||
&mut self,
|
||||
encoded_mix_identity: String,
|
||||
msg_ext: T,
|
||||
test_packets: u32,
|
||||
custom_recipient: Option<Recipient>,
|
||||
) -> Result<Vec<PreparedFragment>, NetworkTestingError>
|
||||
where
|
||||
T: Serialize + Clone,
|
||||
{
|
||||
let Some(node) = self
|
||||
.base_topology
|
||||
.find_mix_by_identity(&encoded_mix_identity)
|
||||
else {
|
||||
return Err(NetworkTestingError::NonExistentMixnodeIdentity {
|
||||
mix_identity: encoded_mix_identity,
|
||||
});
|
||||
};
|
||||
|
||||
let Some(node) = self.base_topology.find_node_by_identity(identity) else {
|
||||
return Err(NetworkTestingError::NonExistentMixnodeIdentity {
|
||||
mix_identity: encoded_mix_identity,
|
||||
});
|
||||
};
|
||||
|
||||
self.mixnode_test_packets(
|
||||
&node.clone(),
|
||||
layer,
|
||||
msg_ext,
|
||||
test_packets,
|
||||
custom_recipient,
|
||||
)
|
||||
self.mixnode_test_packets(&node.clone(), msg_ext, test_packets, custom_recipient)
|
||||
}
|
||||
|
||||
pub fn legacy_gateway_test_packets<T>(
|
||||
&mut self,
|
||||
gateway: &RoutingNode,
|
||||
gateway: &gateway::LegacyNode,
|
||||
node_id: NodeId,
|
||||
msg_ext: T,
|
||||
test_packets: u32,
|
||||
custom_recipient: Option<Recipient>,
|
||||
@@ -177,11 +199,12 @@ where
|
||||
where
|
||||
T: Serialize + Clone,
|
||||
{
|
||||
let ephemeral_topology = NymRouteProvider::from(self.testable_gateway_topology(gateway))
|
||||
.with_ignore_egress_epoch_roles(true);
|
||||
let ephemeral_topology = self.testable_gateway_topology(gateway);
|
||||
|
||||
let mut packets = Vec::with_capacity(test_packets as usize);
|
||||
for plaintext in TestMessage::legacy_gateway_plaintexts(gateway, test_packets, msg_ext)? {
|
||||
for plaintext in
|
||||
TestMessage::legacy_gateway_plaintexts(gateway, node_id, test_packets, msg_ext)?
|
||||
{
|
||||
packets.push(self.wrap_plaintext_data(
|
||||
plaintext,
|
||||
&ephemeral_topology,
|
||||
@@ -192,10 +215,36 @@ where
|
||||
Ok(packets)
|
||||
}
|
||||
|
||||
pub fn existing_gateway_test_packets<T>(
|
||||
&mut self,
|
||||
node_id: NodeId,
|
||||
encoded_gateway_identity: String,
|
||||
msg_ext: T,
|
||||
test_packets: u32,
|
||||
custom_recipient: Option<Recipient>,
|
||||
) -> Result<Vec<PreparedFragment>, NetworkTestingError>
|
||||
where
|
||||
T: Serialize + Clone,
|
||||
{
|
||||
let Some(node) = self.base_topology.find_gateway(&encoded_gateway_identity) else {
|
||||
return Err(NetworkTestingError::NonExistentGateway {
|
||||
gateway_identity: encoded_gateway_identity,
|
||||
});
|
||||
};
|
||||
|
||||
self.legacy_gateway_test_packets(
|
||||
&node.clone(),
|
||||
node_id,
|
||||
msg_ext,
|
||||
test_packets,
|
||||
custom_recipient,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn wrap_plaintext_data(
|
||||
&mut self,
|
||||
plaintext: Vec<u8>,
|
||||
topology: &NymRouteProvider,
|
||||
topology: &NymTopology,
|
||||
custom_recipient: Option<Recipient>,
|
||||
) -> Result<PreparedFragment, NetworkTestingError> {
|
||||
let message = NymMessage::new_plain(plaintext);
|
||||
@@ -225,13 +274,14 @@ where
|
||||
&address,
|
||||
&address,
|
||||
PacketType::Mix,
|
||||
None,
|
||||
)?)
|
||||
}
|
||||
|
||||
pub fn create_test_packet<T>(
|
||||
&mut self,
|
||||
message: &TestMessage<T>,
|
||||
topology: &NymRouteProvider,
|
||||
topology: &NymTopology,
|
||||
custom_recipient: Option<Recipient>,
|
||||
) -> Result<PreparedFragment, NetworkTestingError>
|
||||
where
|
||||
@@ -257,6 +307,10 @@ impl<R: CryptoRng + Rng> FragmentPreparer for NodeTester<R> {
|
||||
1
|
||||
}
|
||||
|
||||
fn num_mix_hops(&self) -> u8 {
|
||||
self.num_mix_hops
|
||||
}
|
||||
|
||||
fn average_packet_delay(&self) -> Duration {
|
||||
self.average_packet_delay
|
||||
}
|
||||
|
||||
@@ -65,6 +65,14 @@ impl<T> NonExhaustiveDelayQueue<T> {
|
||||
pub fn remove(&mut self, key: &QueueKey) -> Expired<T> {
|
||||
self.inner.remove(key)
|
||||
}
|
||||
|
||||
pub fn len(&self) -> usize {
|
||||
self.inner.len()
|
||||
}
|
||||
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.inner.is_empty()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Default for NonExhaustiveDelayQueue<T> {
|
||||
|
||||
@@ -12,6 +12,6 @@ license.workspace = true
|
||||
|
||||
[dependencies]
|
||||
prometheus = { workspace = true }
|
||||
log = { workspace = true }
|
||||
tracing = { workspace = true }
|
||||
dashmap = { workspace = true }
|
||||
lazy_static = { workspace = true }
|
||||
|
||||
+390
-81
@@ -1,14 +1,18 @@
|
||||
use dashmap::DashMap;
|
||||
pub use log::error;
|
||||
use log::{debug, warn};
|
||||
use std::fmt;
|
||||
pub use std::time::Instant;
|
||||
use tracing::{debug, error, warn};
|
||||
|
||||
use prometheus::{core::Collector, Encoder as _, IntCounter, IntGauge, Registry, TextEncoder};
|
||||
use prometheus::{
|
||||
core::Collector, Encoder as _, Gauge, Histogram, HistogramOpts, IntCounter, IntGauge, Registry,
|
||||
TextEncoder,
|
||||
};
|
||||
|
||||
pub use prometheus::HistogramTimer;
|
||||
pub use std::time::Instant;
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! prepend_package_name {
|
||||
($name: literal) => {
|
||||
($name: tt) => {
|
||||
&format!(
|
||||
"{}_{}",
|
||||
std::module_path!()
|
||||
@@ -23,15 +27,29 @@ macro_rules! prepend_package_name {
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! inc_by {
|
||||
($name:literal, $x:expr, $help: expr) => {
|
||||
$crate::REGISTRY.maybe_register_and_inc_by(
|
||||
$crate::prepend_package_name!($name),
|
||||
$x as i64,
|
||||
$help,
|
||||
);
|
||||
};
|
||||
($name:literal, $x:expr) => {
|
||||
$crate::REGISTRY.inc_by($crate::prepend_package_name!($name), $x as i64);
|
||||
$crate::REGISTRY.maybe_register_and_inc_by(
|
||||
$crate::prepend_package_name!($name),
|
||||
$x as i64,
|
||||
None,
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! inc {
|
||||
($name:literal, $help: expr) => {
|
||||
$crate::REGISTRY.maybe_register_and_inc($crate::prepend_package_name!($name), $help);
|
||||
};
|
||||
($name:literal) => {
|
||||
$crate::REGISTRY.inc($crate::prepend_package_name!($name));
|
||||
$crate::REGISTRY.maybe_register_and_inc($crate::prepend_package_name!($name), None);
|
||||
};
|
||||
}
|
||||
|
||||
@@ -42,6 +60,71 @@ macro_rules! metrics {
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! set_metric {
|
||||
($name:literal, $x:expr, $help: expr) => {
|
||||
$crate::REGISTRY.maybe_register_and_set(
|
||||
$crate::prepend_package_name!($name),
|
||||
$x as i64,
|
||||
$help,
|
||||
);
|
||||
};
|
||||
($name:literal, $x:expr) => {
|
||||
$crate::REGISTRY.maybe_register_and_set(
|
||||
$crate::prepend_package_name!($name),
|
||||
$x as i64,
|
||||
None,
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! set_metric_float {
|
||||
($name:literal, $x:expr, $help: expr) => {
|
||||
$crate::REGISTRY.maybe_register_and_set_float(
|
||||
$crate::prepend_package_name!($name),
|
||||
$x as f64,
|
||||
$help,
|
||||
);
|
||||
};
|
||||
($name:literal, $x:expr) => {
|
||||
$crate::REGISTRY.maybe_register_and_set_float(
|
||||
$crate::prepend_package_name!($name),
|
||||
$x as f64,
|
||||
None,
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! add_histogram_obs {
|
||||
($name:expr, $x:expr, $b:expr, $help:expr) => {
|
||||
$crate::REGISTRY.maybe_register_and_add_to_histogram(
|
||||
$crate::prepend_package_name!($name),
|
||||
$x as f64,
|
||||
Some($b),
|
||||
$help,
|
||||
);
|
||||
};
|
||||
|
||||
($name:expr, $x:expr, $b:expr) => {
|
||||
$crate::REGISTRY.maybe_register_and_add_to_histogram(
|
||||
$crate::prepend_package_name!($name),
|
||||
$x as f64,
|
||||
Some($b),
|
||||
None,
|
||||
);
|
||||
};
|
||||
($name:expr, $x:expr) => {
|
||||
$crate::REGISTRY.maybe_register_and_add_to_histogram(
|
||||
$crate::prepend_package_name!($name),
|
||||
$x as f64,
|
||||
None,
|
||||
None,
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! nanos {
|
||||
( $name:literal, $x:expr ) => {{
|
||||
@@ -50,7 +133,7 @@ macro_rules! nanos {
|
||||
let r = $x;
|
||||
let duration = start.elapsed().as_nanos() as i64;
|
||||
let name = $crate::prepend_package_name!($name);
|
||||
$crate::REGISTRY.inc_by(&format!("{}_nanos", $name), duration);
|
||||
$crate::REGISTRY.maybe_register_and_inc_by(&format!("{}_nanos", $name), duration, None);
|
||||
r
|
||||
}};
|
||||
}
|
||||
@@ -59,15 +142,100 @@ lazy_static::lazy_static! {
|
||||
pub static ref REGISTRY: MetricsController = MetricsController::default();
|
||||
}
|
||||
|
||||
pub fn metrics_registry() -> &'static MetricsController {
|
||||
®ISTRY
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct MetricsController {
|
||||
registry: Registry,
|
||||
registry_index: DashMap<String, Metric>,
|
||||
}
|
||||
|
||||
enum Metric {
|
||||
C(Box<IntCounter>),
|
||||
G(Box<IntGauge>),
|
||||
pub enum Metric {
|
||||
IntCounter(Box<IntCounter>),
|
||||
IntGauge(Box<IntGauge>),
|
||||
FloatGauge(Box<Gauge>),
|
||||
Histogram(Box<Histogram>),
|
||||
}
|
||||
|
||||
impl Metric {
|
||||
pub fn new_int_counter(name: &str, help: &str) -> Option<Self> {
|
||||
match IntCounter::new(sanitize_metric_name(name), help) {
|
||||
Ok(c) => Some(c.into()),
|
||||
Err(err) => {
|
||||
error!("Failed to create counter {name:?}: {err}");
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new_int_gauge(name: &str, help: &str) -> Option<Self> {
|
||||
match IntGauge::new(sanitize_metric_name(name), help) {
|
||||
Ok(g) => Some(g.into()),
|
||||
Err(err) => {
|
||||
error!("Failed to create gauge {name:?}: {err}");
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new_float_gauge(name: &str, help: &str) -> Option<Self> {
|
||||
match Gauge::new(sanitize_metric_name(name), help) {
|
||||
Ok(g) => Some(g.into()),
|
||||
Err(err) => {
|
||||
error!("Failed to create gauge {name:?}: {err}");
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new_histogram(name: &str, help: &str, buckets: Option<&[f64]>) -> Option<Self> {
|
||||
let mut opts = HistogramOpts::new(sanitize_metric_name(name), help);
|
||||
if let Some(buckets) = buckets {
|
||||
opts = opts.buckets(buckets.to_vec())
|
||||
}
|
||||
match Histogram::with_opts(opts) {
|
||||
Ok(h) => Some(Metric::Histogram(Box::new(h))),
|
||||
Err(err) => {
|
||||
error!("failed to create histogram {name:?}: {err}");
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn as_collector(&self) -> Box<dyn Collector> {
|
||||
match self {
|
||||
Metric::IntCounter(c) => c.clone(),
|
||||
Metric::IntGauge(g) => g.clone(),
|
||||
Metric::FloatGauge(g) => g.clone(),
|
||||
Metric::Histogram(h) => h.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<IntCounter> for Metric {
|
||||
fn from(v: IntCounter) -> Self {
|
||||
Metric::IntCounter(Box::new(v))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<IntGauge> for Metric {
|
||||
fn from(v: IntGauge) -> Self {
|
||||
Metric::IntGauge(Box::new(v))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Gauge> for Metric {
|
||||
fn from(v: Gauge) -> Self {
|
||||
Metric::FloatGauge(Box::new(v))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Histogram> for Metric {
|
||||
fn from(v: Histogram) -> Self {
|
||||
Metric::Histogram(Box::new(v))
|
||||
}
|
||||
}
|
||||
|
||||
fn fq_name(c: &dyn Collector) -> String {
|
||||
@@ -81,34 +249,92 @@ impl Metric {
|
||||
#[inline(always)]
|
||||
fn fq_name(&self) -> String {
|
||||
match self {
|
||||
Metric::C(c) => fq_name(c.as_ref()),
|
||||
Metric::G(g) => fq_name(g.as_ref()),
|
||||
Metric::IntCounter(c) => fq_name(c.as_ref()),
|
||||
Metric::IntGauge(g) => fq_name(g.as_ref()),
|
||||
Metric::FloatGauge(g) => fq_name(g.as_ref()),
|
||||
Metric::Histogram(h) => fq_name(h.as_ref()),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn inc(&self) {
|
||||
match self {
|
||||
Metric::C(c) => c.inc(),
|
||||
Metric::G(g) => g.inc(),
|
||||
Metric::IntCounter(c) => c.inc(),
|
||||
Metric::IntGauge(g) => g.inc(),
|
||||
Metric::FloatGauge(g) => g.inc(),
|
||||
Metric::Histogram(_) => {
|
||||
warn!("invalid operation: attempted to call increment on a histogram")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn inc_by(&self, value: i64) {
|
||||
match self {
|
||||
Metric::C(c) => c.inc_by(value as u64),
|
||||
Metric::G(g) => g.add(value),
|
||||
Metric::IntCounter(c) => c.inc_by(value as u64),
|
||||
Metric::IntGauge(g) => g.add(value),
|
||||
Metric::FloatGauge(g) => {
|
||||
warn!("attempted to increment a float gauge ('{}') by an integer - this is most likely a bug", self.fq_name());
|
||||
g.add(value as f64)
|
||||
}
|
||||
Metric::Histogram(_) => {
|
||||
warn!("invalid operation: attempted to call increment on a histogram")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn set(&self, value: i64) {
|
||||
match self {
|
||||
Metric::C(_c) => {
|
||||
Metric::IntCounter(_c) => {
|
||||
warn!("Cannot set value for counter {:?}", self.fq_name());
|
||||
}
|
||||
Metric::G(g) => g.set(value),
|
||||
Metric::IntGauge(g) => g.set(value),
|
||||
Metric::FloatGauge(g) => {
|
||||
warn!("attempted to set a float gauge ('{}') to an integer value - this is most likely a bug", self.fq_name());
|
||||
g.set(value as f64)
|
||||
}
|
||||
Metric::Histogram(_) => {
|
||||
warn!("invalid operation: attempted to call set on a histogram")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn set_float(&self, value: f64) {
|
||||
match self {
|
||||
Metric::IntCounter(_c) => {
|
||||
warn!("Cannot set value for counter {:?}", self.fq_name());
|
||||
}
|
||||
Metric::IntGauge(g) => {
|
||||
warn!("attempted to set a integer gauge ('{}') to a float value - this is most likely a bug", self.fq_name());
|
||||
g.set(value as i64)
|
||||
}
|
||||
Metric::FloatGauge(g) => g.set(value),
|
||||
Metric::Histogram(_) => {
|
||||
warn!("invalid operation: attempted to call increment on a histogram")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn add_histogram_observation(&self, value: f64) {
|
||||
match self {
|
||||
Metric::Histogram(h) => {
|
||||
h.observe(value);
|
||||
}
|
||||
_ => warn!("attempted to add histogram observation on a non-histogram metric"),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn start_timer(&self) -> Option<HistogramTimer> {
|
||||
match self {
|
||||
Metric::Histogram(h) => Some(h.start_timer()),
|
||||
_ => {
|
||||
warn!("attempted to start histogram observation on a non-histogram metric");
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -145,93 +371,165 @@ impl MetricsController {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set(&self, name: &str, value: i64) {
|
||||
pub fn register_int_gauge<'a>(&self, name: &str, help: impl Into<Option<&'a str>>) {
|
||||
let Some(metric) = Metric::new_int_gauge(name, help.into().unwrap_or(name)) else {
|
||||
return;
|
||||
};
|
||||
self.register_metric(metric);
|
||||
}
|
||||
|
||||
pub fn register_float_gauge<'a>(&self, name: &str, help: impl Into<Option<&'a str>>) {
|
||||
let Some(metric) = Metric::new_float_gauge(name, help.into().unwrap_or(name)) else {
|
||||
return;
|
||||
};
|
||||
self.register_metric(metric);
|
||||
}
|
||||
|
||||
pub fn register_int_counter<'a>(&self, name: &str, help: impl Into<Option<&'a str>>) {
|
||||
let Some(metric) = Metric::new_int_counter(name, help.into().unwrap_or(name)) else {
|
||||
return;
|
||||
};
|
||||
self.register_metric(metric);
|
||||
}
|
||||
|
||||
pub fn register_histogram<'a>(
|
||||
&self,
|
||||
name: &str,
|
||||
help: impl Into<Option<&'a str>>,
|
||||
buckets: Option<&[f64]>,
|
||||
) {
|
||||
let Some(metric) = Metric::new_histogram(name, help.into().unwrap_or(name), buckets) else {
|
||||
return;
|
||||
};
|
||||
self.register_metric(metric);
|
||||
}
|
||||
|
||||
pub fn set(&self, name: &str, value: i64) -> bool {
|
||||
if let Some(metric) = self.registry_index.get(name) {
|
||||
metric.set(value);
|
||||
true
|
||||
} else {
|
||||
let gauge = match IntGauge::new(sanitize_metric_name(name), name) {
|
||||
Ok(g) => g,
|
||||
Err(e) => {
|
||||
debug!("Failed to create gauge {:?}:\n{}", name, e);
|
||||
return;
|
||||
}
|
||||
};
|
||||
self.register_gauge(Box::new(gauge));
|
||||
self.set(name, value)
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
pub fn inc(&self, name: &str) {
|
||||
pub fn set_float(&self, name: &str, value: f64) -> bool {
|
||||
if let Some(metric) = self.registry_index.get(name) {
|
||||
metric.set_float(value);
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_to_histogram(&self, name: &str, value: f64) -> bool {
|
||||
if let Some(metric) = self.registry_index.get(name) {
|
||||
metric.add_histogram_observation(value);
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
pub fn start_timer(&self, name: &str) -> Option<HistogramTimer> {
|
||||
self.registry_index
|
||||
.get(name)
|
||||
.and_then(|metric| metric.start_timer())
|
||||
}
|
||||
|
||||
pub fn inc(&self, name: &str) -> bool {
|
||||
if let Some(metric) = self.registry_index.get(name) {
|
||||
metric.inc();
|
||||
true
|
||||
} else {
|
||||
let counter = match IntCounter::new(sanitize_metric_name(name), name) {
|
||||
Ok(c) => c,
|
||||
Err(e) => {
|
||||
debug!("Failed to create counter {:?}:\n{}", name, e);
|
||||
return;
|
||||
}
|
||||
};
|
||||
self.register_counter(Box::new(counter));
|
||||
self.inc(name)
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
pub fn inc_by(&self, name: &str, value: i64) {
|
||||
pub fn inc_by(&self, name: &str, value: i64) -> bool {
|
||||
if let Some(metric) = self.registry_index.get(name) {
|
||||
metric.inc_by(value);
|
||||
true
|
||||
} else {
|
||||
let counter = match IntCounter::new(sanitize_metric_name(name), name) {
|
||||
Ok(c) => c,
|
||||
Err(e) => {
|
||||
debug!("Failed to create counter {:?}:\n{}", name, e);
|
||||
return;
|
||||
}
|
||||
};
|
||||
self.register_counter(Box::new(counter));
|
||||
self.inc_by(name, value)
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
fn register_gauge(&self, metric: Box<IntGauge>) {
|
||||
let fq_name = metric
|
||||
.desc()
|
||||
.first()
|
||||
.map(|d| d.fq_name.clone())
|
||||
.unwrap_or_default();
|
||||
pub fn maybe_register_and_set<'a>(
|
||||
&self,
|
||||
name: &str,
|
||||
value: i64,
|
||||
help: impl Into<Option<&'a str>>,
|
||||
) {
|
||||
if !self.set(name, value) {
|
||||
let help = help.into();
|
||||
self.register_int_gauge(name, help);
|
||||
self.set(name, value);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn maybe_register_and_set_float<'a>(
|
||||
&self,
|
||||
name: &str,
|
||||
value: f64,
|
||||
help: impl Into<Option<&'a str>>,
|
||||
) {
|
||||
if !self.set_float(name, value) {
|
||||
let help = help.into();
|
||||
self.register_float_gauge(name, help);
|
||||
self.set_float(name, value);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn maybe_register_and_add_to_histogram<'a>(
|
||||
&self,
|
||||
name: &str,
|
||||
value: f64,
|
||||
buckets: Option<&[f64]>,
|
||||
help: impl Into<Option<&'a str>>,
|
||||
) {
|
||||
if !self.add_to_histogram(name, value) {
|
||||
let help = help.into();
|
||||
self.register_histogram(name, help, buckets);
|
||||
self.add_to_histogram(name, value);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn maybe_register_and_inc<'a>(&self, name: &str, help: impl Into<Option<&'a str>>) {
|
||||
if !self.inc(name) {
|
||||
let help = help.into();
|
||||
self.register_int_counter(name, help);
|
||||
self.inc(name);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn maybe_register_and_inc_by<'a>(
|
||||
&self,
|
||||
name: &str,
|
||||
value: i64,
|
||||
help: impl Into<Option<&'a str>>,
|
||||
) {
|
||||
if !self.inc_by(name, value) {
|
||||
let help = help.into();
|
||||
self.register_int_counter(name, help);
|
||||
self.inc_by(name, value);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn register_metric(&self, metric: impl Into<Metric>) {
|
||||
let m = metric.into();
|
||||
let fq_name = m.fq_name();
|
||||
|
||||
if self.registry_index.contains_key(&fq_name) {
|
||||
return;
|
||||
}
|
||||
|
||||
match self.registry.register(metric.clone()) {
|
||||
match self.registry.register(m.as_collector()) {
|
||||
Ok(_) => {
|
||||
self.registry_index
|
||||
.insert(fq_name, Metric::G(metric.clone()));
|
||||
self.registry_index.insert(fq_name, m);
|
||||
}
|
||||
Err(e) => {
|
||||
debug!("Failed to register {:?}:\n{}", fq_name, e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn register_counter(&self, metric: Box<IntCounter>) {
|
||||
let fq_name = metric
|
||||
.desc()
|
||||
.first()
|
||||
.map(|d| d.fq_name.clone())
|
||||
.unwrap_or_default();
|
||||
|
||||
if self.registry_index.contains_key(&fq_name) {
|
||||
return;
|
||||
}
|
||||
match self.registry.register(metric.clone()) {
|
||||
Ok(_) => {
|
||||
self.registry_index
|
||||
.insert(fq_name, Metric::C(metric.clone()));
|
||||
}
|
||||
Err(e) => {
|
||||
debug!("Failed to register {:?}:\n{}", fq_name, e)
|
||||
Err(err) => {
|
||||
debug!("Failed to register '{fq_name}': {err}")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -275,4 +573,15 @@ mod tests {
|
||||
"packets_sent_34_242_65_133:1789"
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn prepend_package_name() {
|
||||
let literal = prepend_package_name!("foo");
|
||||
assert_eq!(literal, "nym_metrics_foo");
|
||||
|
||||
let bar = "bar";
|
||||
let format = format!("foomp_{}", bar);
|
||||
let formatted = prepend_package_name!(format);
|
||||
assert_eq!(formatted, "nym_metrics_foomp_bar");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,10 +8,10 @@ use nym_sphinx_addressing::nodes::{
|
||||
NymNodeRoutingAddress, NymNodeRoutingAddressError, MAX_NODE_ADDRESS_UNPADDED_LEN,
|
||||
};
|
||||
use nym_sphinx_params::packet_sizes::PacketSize;
|
||||
use nym_sphinx_params::PacketType;
|
||||
use nym_sphinx_params::{PacketType, DEFAULT_NUM_MIX_HOPS};
|
||||
use nym_sphinx_types::delays::Delay;
|
||||
use nym_sphinx_types::{NymPacket, NymPacketError, MIN_PACKET_SIZE};
|
||||
use nym_topology::{NymRouteProvider, NymTopologyError};
|
||||
use nym_topology::{NymTopology, NymTopologyError};
|
||||
use rand::{CryptoRng, RngCore};
|
||||
|
||||
use std::time;
|
||||
@@ -43,13 +43,14 @@ impl SurbAck {
|
||||
ack_key: &AckKey,
|
||||
marshaled_fragment_id: [u8; 5],
|
||||
average_delay: time::Duration,
|
||||
topology: &NymRouteProvider,
|
||||
topology: &NymTopology,
|
||||
packet_type: PacketType,
|
||||
) -> Result<Self, NymTopologyError>
|
||||
where
|
||||
R: RngCore + CryptoRng,
|
||||
{
|
||||
let route = topology.random_route_to_egress(rng, recipient.gateway())?;
|
||||
let route =
|
||||
topology.random_route_to_gateway(rng, DEFAULT_NUM_MIX_HOPS, recipient.gateway())?;
|
||||
let delays = nym_sphinx_routing::generate_hop_delays(average_delay, route.len());
|
||||
let destination = recipient.as_sphinx_destination();
|
||||
|
||||
|
||||
@@ -131,8 +131,8 @@ impl Recipient {
|
||||
&self.client_encryption_key
|
||||
}
|
||||
|
||||
pub fn gateway(&self) -> NodeIdentity {
|
||||
self.gateway
|
||||
pub fn gateway(&self) -> &NodeIdentity {
|
||||
&self.gateway
|
||||
}
|
||||
|
||||
pub fn to_bytes(self) -> RecipientBytes {
|
||||
|
||||
@@ -6,9 +6,9 @@ use nym_crypto::{generic_array::typenum::Unsigned, Digest};
|
||||
use nym_sphinx_addressing::clients::Recipient;
|
||||
use nym_sphinx_addressing::nodes::{NymNodeRoutingAddress, MAX_NODE_ADDRESS_UNPADDED_LEN};
|
||||
use nym_sphinx_params::packet_sizes::PacketSize;
|
||||
use nym_sphinx_params::{PacketType, ReplySurbKeyDigestAlgorithm};
|
||||
use nym_sphinx_params::{PacketType, ReplySurbKeyDigestAlgorithm, DEFAULT_NUM_MIX_HOPS};
|
||||
use nym_sphinx_types::{NymPacket, SURBMaterial, SphinxError, SURB};
|
||||
use nym_topology::{NymRouteProvider, NymTopologyError};
|
||||
use nym_topology::{NymTopology, NymTopologyError};
|
||||
use rand::{CryptoRng, RngCore};
|
||||
use serde::de::{Error as SerdeError, Visitor};
|
||||
use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
||||
@@ -89,12 +89,13 @@ impl ReplySurb {
|
||||
rng: &mut R,
|
||||
recipient: &Recipient,
|
||||
average_delay: time::Duration,
|
||||
topology: &NymRouteProvider,
|
||||
topology: &NymTopology,
|
||||
) -> Result<Self, NymTopologyError>
|
||||
where
|
||||
R: RngCore + CryptoRng,
|
||||
{
|
||||
let route = topology.random_route_to_egress(rng, recipient.gateway())?;
|
||||
let route =
|
||||
topology.random_route_to_gateway(rng, DEFAULT_NUM_MIX_HOPS, recipient.gateway())?;
|
||||
let delays = nym_sphinx_routing::generate_hop_delays(average_delay, route.len());
|
||||
let destination = recipient.as_sphinx_destination();
|
||||
|
||||
@@ -109,12 +110,15 @@ impl ReplySurb {
|
||||
|
||||
/// Returns the expected number of bytes the [`ReplySURB`] will take after serialization.
|
||||
/// Useful for deserialization from a bytes stream.
|
||||
pub fn serialized_len() -> usize {
|
||||
pub fn serialized_len(mix_hops: u8) -> usize {
|
||||
use nym_sphinx_types::{HEADER_SIZE, NODE_ADDRESS_LENGTH, PAYLOAD_KEY_SIZE};
|
||||
|
||||
// the SURB itself consists of SURB_header, first hop address and set of payload keys
|
||||
// for each hop (3x mix + egress)
|
||||
SurbEncryptionKeySize::USIZE + HEADER_SIZE + NODE_ADDRESS_LENGTH + 4 * PAYLOAD_KEY_SIZE
|
||||
// (note extra 1 for the gateway)
|
||||
SurbEncryptionKeySize::USIZE
|
||||
+ HEADER_SIZE
|
||||
+ NODE_ADDRESS_LENGTH
|
||||
+ (1 + mix_hops as usize) * PAYLOAD_KEY_SIZE
|
||||
}
|
||||
|
||||
pub fn encryption_key(&self) -> &SurbEncryptionKey {
|
||||
|
||||
@@ -169,7 +169,10 @@ impl RepliableMessage {
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn try_from_bytes(bytes: &[u8]) -> Result<Self, InvalidReplyRequestError> {
|
||||
pub fn try_from_bytes(
|
||||
bytes: &[u8],
|
||||
num_mix_hops: u8,
|
||||
) -> Result<Self, InvalidReplyRequestError> {
|
||||
if bytes.len() < SENDER_TAG_SIZE + 1 {
|
||||
return Err(InvalidReplyRequestError::RequestTooShortToDeserialize);
|
||||
}
|
||||
@@ -177,8 +180,11 @@ impl RepliableMessage {
|
||||
AnonymousSenderTag::from_bytes(bytes[..SENDER_TAG_SIZE].try_into().unwrap());
|
||||
let content_tag = RepliableMessageContentTag::try_from(bytes[SENDER_TAG_SIZE])?;
|
||||
|
||||
let content =
|
||||
RepliableMessageContent::try_from_bytes(&bytes[SENDER_TAG_SIZE + 1..], content_tag)?;
|
||||
let content = RepliableMessageContent::try_from_bytes(
|
||||
&bytes[SENDER_TAG_SIZE + 1..],
|
||||
num_mix_hops,
|
||||
content_tag,
|
||||
)?;
|
||||
|
||||
Ok(RepliableMessage {
|
||||
sender_tag,
|
||||
@@ -186,20 +192,23 @@ impl RepliableMessage {
|
||||
})
|
||||
}
|
||||
|
||||
pub fn serialized_size(&self) -> usize {
|
||||
pub fn serialized_size(&self, num_mix_hops: u8) -> usize {
|
||||
let content_type_size = 1;
|
||||
SENDER_TAG_SIZE + content_type_size + self.content.serialized_size()
|
||||
SENDER_TAG_SIZE + content_type_size + self.content.serialized_size(num_mix_hops)
|
||||
}
|
||||
}
|
||||
|
||||
// this recovery code is shared between all variants containing reply surbs
|
||||
fn recover_reply_surbs(bytes: &[u8]) -> Result<(Vec<ReplySurb>, usize), InvalidReplyRequestError> {
|
||||
fn recover_reply_surbs(
|
||||
bytes: &[u8],
|
||||
num_mix_hops: u8,
|
||||
) -> Result<(Vec<ReplySurb>, usize), InvalidReplyRequestError> {
|
||||
let mut consumed = mem::size_of::<u32>();
|
||||
if bytes.len() < consumed {
|
||||
return Err(InvalidReplyRequestError::RequestTooShortToDeserialize);
|
||||
}
|
||||
let num_surbs = u32::from_be_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]);
|
||||
let surb_size = ReplySurb::serialized_len();
|
||||
let surb_size = ReplySurb::serialized_len(num_mix_hops);
|
||||
if bytes[consumed..].len() < num_surbs as usize * surb_size {
|
||||
return Err(InvalidReplyRequestError::RequestTooShortToDeserialize);
|
||||
}
|
||||
@@ -298,13 +307,14 @@ impl RepliableMessageContent {
|
||||
|
||||
fn try_from_bytes(
|
||||
bytes: &[u8],
|
||||
num_mix_hops: u8,
|
||||
tag: RepliableMessageContentTag,
|
||||
) -> Result<Self, InvalidReplyRequestError> {
|
||||
if bytes.is_empty() {
|
||||
return Err(InvalidReplyRequestError::RequestTooShortToDeserialize);
|
||||
}
|
||||
|
||||
let (reply_surbs, n) = recover_reply_surbs(bytes)?;
|
||||
let (reply_surbs, n) = recover_reply_surbs(bytes, num_mix_hops)?;
|
||||
|
||||
match tag {
|
||||
RepliableMessageContentTag::Data => Ok(RepliableMessageContent::Data {
|
||||
@@ -330,7 +340,7 @@ impl RepliableMessageContent {
|
||||
}
|
||||
}
|
||||
|
||||
fn serialized_size(&self) -> usize {
|
||||
fn serialized_size(&self, num_mix_hops: u8) -> usize {
|
||||
match self {
|
||||
RepliableMessageContent::Data {
|
||||
message,
|
||||
@@ -338,18 +348,19 @@ impl RepliableMessageContent {
|
||||
} => {
|
||||
let num_reply_surbs_tag = mem::size_of::<u32>();
|
||||
num_reply_surbs_tag
|
||||
+ reply_surbs.len() * ReplySurb::serialized_len()
|
||||
+ reply_surbs.len() * ReplySurb::serialized_len(num_mix_hops)
|
||||
+ message.len()
|
||||
}
|
||||
RepliableMessageContent::AdditionalSurbs { reply_surbs } => {
|
||||
let num_reply_surbs_tag = mem::size_of::<u32>();
|
||||
num_reply_surbs_tag + reply_surbs.len() * ReplySurb::serialized_len()
|
||||
num_reply_surbs_tag + reply_surbs.len() * ReplySurb::serialized_len(num_mix_hops)
|
||||
}
|
||||
RepliableMessageContent::Heartbeat {
|
||||
additional_reply_surbs,
|
||||
} => {
|
||||
let num_reply_surbs_tag = mem::size_of::<u32>();
|
||||
num_reply_surbs_tag + additional_reply_surbs.len() * ReplySurb::serialized_len()
|
||||
num_reply_surbs_tag
|
||||
+ additional_reply_surbs.len() * ReplySurb::serialized_len(num_mix_hops)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -567,11 +578,11 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn reply_surb(rng: &mut ChaCha20Rng) -> ReplySurb {
|
||||
pub(super) fn reply_surb(rng: &mut ChaCha20Rng, num_mix_hops: u8) -> ReplySurb {
|
||||
// due to gateway
|
||||
const HOPS: u8 = 4;
|
||||
let route = (0..HOPS).map(|_| node(rng)).collect();
|
||||
let delays = (0..HOPS)
|
||||
let num_hops = num_mix_hops + 1;
|
||||
let route = (0..num_hops).map(|_| node(rng)).collect();
|
||||
let delays = (0..num_hops)
|
||||
.map(|_| Delay::new_from_nanos(rng.next_u64()))
|
||||
.collect();
|
||||
let mut destination_bytes = [0u8; 32];
|
||||
@@ -594,40 +605,47 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn reply_surbs(rng: &mut ChaCha20Rng, n: usize) -> Vec<ReplySurb> {
|
||||
pub(super) fn reply_surbs(
|
||||
rng: &mut ChaCha20Rng,
|
||||
num_mix_hops: u8,
|
||||
n: usize,
|
||||
) -> Vec<ReplySurb> {
|
||||
let mut surbs = Vec::with_capacity(n);
|
||||
for _ in 0..n {
|
||||
surbs.push(reply_surb(rng))
|
||||
surbs.push(reply_surb(rng, num_mix_hops))
|
||||
}
|
||||
surbs
|
||||
}
|
||||
|
||||
pub(super) fn repliable_content_data(
|
||||
rng: &mut ChaCha20Rng,
|
||||
num_mix_hops: u8,
|
||||
msg_len: usize,
|
||||
surbs: usize,
|
||||
) -> RepliableMessageContent {
|
||||
RepliableMessageContent::Data {
|
||||
message: random_vec_u8(rng, msg_len),
|
||||
reply_surbs: reply_surbs(rng, surbs),
|
||||
reply_surbs: reply_surbs(rng, num_mix_hops, surbs),
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn repliable_content_surbs(
|
||||
rng: &mut ChaCha20Rng,
|
||||
num_mix_hops: u8,
|
||||
surbs: usize,
|
||||
) -> RepliableMessageContent {
|
||||
RepliableMessageContent::AdditionalSurbs {
|
||||
reply_surbs: reply_surbs(rng, surbs),
|
||||
reply_surbs: reply_surbs(rng, num_mix_hops, surbs),
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn repliable_content_heartbeat(
|
||||
rng: &mut ChaCha20Rng,
|
||||
num_mix_hops: u8,
|
||||
surbs: usize,
|
||||
) -> RepliableMessageContent {
|
||||
RepliableMessageContent::Heartbeat {
|
||||
additional_reply_surbs: reply_surbs(rng, surbs),
|
||||
additional_reply_surbs: reply_surbs(rng, num_mix_hops, surbs),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -658,54 +676,70 @@ mod tests {
|
||||
#[test]
|
||||
fn serialized_size_matches_actual_serialization() {
|
||||
let mut rng = fixtures::test_rng();
|
||||
let num_mix_hops = 3;
|
||||
|
||||
let data1 = RepliableMessage {
|
||||
sender_tag: fixtures::sender_tag(&mut rng),
|
||||
content: fixtures::repliable_content_data(&mut rng, 10000, 0),
|
||||
content: fixtures::repliable_content_data(&mut rng, num_mix_hops, 10000, 0),
|
||||
};
|
||||
assert_eq!(data1.serialized_size(), data1.into_bytes().len());
|
||||
assert_eq!(
|
||||
data1.serialized_size(num_mix_hops),
|
||||
data1.into_bytes().len()
|
||||
);
|
||||
|
||||
let data2 = RepliableMessage {
|
||||
sender_tag: fixtures::sender_tag(&mut rng),
|
||||
content: fixtures::repliable_content_data(&mut rng, 10, 100),
|
||||
content: fixtures::repliable_content_data(&mut rng, num_mix_hops, 10, 100),
|
||||
};
|
||||
assert_eq!(data2.serialized_size(), data2.into_bytes().len());
|
||||
assert_eq!(
|
||||
data2.serialized_size(num_mix_hops),
|
||||
data2.into_bytes().len()
|
||||
);
|
||||
|
||||
let data3 = RepliableMessage {
|
||||
sender_tag: fixtures::sender_tag(&mut rng),
|
||||
content: fixtures::repliable_content_data(&mut rng, 100000, 1000),
|
||||
content: fixtures::repliable_content_data(&mut rng, num_mix_hops, 100000, 1000),
|
||||
};
|
||||
assert_eq!(data3.serialized_size(), data3.into_bytes().len());
|
||||
assert_eq!(
|
||||
data3.serialized_size(num_mix_hops),
|
||||
data3.into_bytes().len()
|
||||
);
|
||||
|
||||
let additional_surbs1 = RepliableMessage {
|
||||
sender_tag: fixtures::sender_tag(&mut rng),
|
||||
content: fixtures::repliable_content_surbs(&mut rng, 1),
|
||||
content: fixtures::repliable_content_surbs(&mut rng, num_mix_hops, 1),
|
||||
};
|
||||
assert_eq!(
|
||||
additional_surbs1.serialized_size(),
|
||||
additional_surbs1.serialized_size(num_mix_hops),
|
||||
additional_surbs1.into_bytes().len()
|
||||
);
|
||||
|
||||
let additional_surbs2 = RepliableMessage {
|
||||
sender_tag: fixtures::sender_tag(&mut rng),
|
||||
content: fixtures::repliable_content_surbs(&mut rng, 1000),
|
||||
content: fixtures::repliable_content_surbs(&mut rng, num_mix_hops, 1000),
|
||||
};
|
||||
assert_eq!(
|
||||
additional_surbs2.serialized_size(),
|
||||
additional_surbs2.serialized_size(num_mix_hops),
|
||||
additional_surbs2.into_bytes().len()
|
||||
);
|
||||
|
||||
let heartbeat1 = RepliableMessage {
|
||||
sender_tag: fixtures::sender_tag(&mut rng),
|
||||
content: fixtures::repliable_content_heartbeat(&mut rng, 1),
|
||||
content: fixtures::repliable_content_heartbeat(&mut rng, num_mix_hops, 1),
|
||||
};
|
||||
assert_eq!(heartbeat1.serialized_size(), heartbeat1.into_bytes().len());
|
||||
assert_eq!(
|
||||
heartbeat1.serialized_size(num_mix_hops),
|
||||
heartbeat1.into_bytes().len()
|
||||
);
|
||||
|
||||
let heartbeat2 = RepliableMessage {
|
||||
sender_tag: fixtures::sender_tag(&mut rng),
|
||||
content: fixtures::repliable_content_heartbeat(&mut rng, 1000),
|
||||
content: fixtures::repliable_content_heartbeat(&mut rng, num_mix_hops, 1000),
|
||||
};
|
||||
assert_eq!(heartbeat2.serialized_size(), heartbeat2.into_bytes().len());
|
||||
assert_eq!(
|
||||
heartbeat2.serialized_size(num_mix_hops),
|
||||
heartbeat2.into_bytes().len()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -716,33 +750,49 @@ mod tests {
|
||||
#[test]
|
||||
fn serialized_size_matches_actual_serialization() {
|
||||
let mut rng = fixtures::test_rng();
|
||||
let num_mix_hops = 3;
|
||||
|
||||
let data1 = fixtures::repliable_content_data(&mut rng, 10000, 0);
|
||||
assert_eq!(data1.serialized_size(), data1.into_bytes().len());
|
||||
|
||||
let data2 = fixtures::repliable_content_data(&mut rng, 10, 100);
|
||||
assert_eq!(data2.serialized_size(), data2.into_bytes().len());
|
||||
|
||||
let data3 = fixtures::repliable_content_data(&mut rng, 100000, 1000);
|
||||
assert_eq!(data3.serialized_size(), data3.into_bytes().len());
|
||||
|
||||
let additional_surbs1 = fixtures::repliable_content_surbs(&mut rng, 1);
|
||||
let data1 = fixtures::repliable_content_data(&mut rng, num_mix_hops, 10000, 0);
|
||||
assert_eq!(
|
||||
additional_surbs1.serialized_size(),
|
||||
data1.serialized_size(num_mix_hops),
|
||||
data1.into_bytes().len()
|
||||
);
|
||||
|
||||
let data2 = fixtures::repliable_content_data(&mut rng, num_mix_hops, 10, 100);
|
||||
assert_eq!(
|
||||
data2.serialized_size(num_mix_hops),
|
||||
data2.into_bytes().len()
|
||||
);
|
||||
|
||||
let data3 = fixtures::repliable_content_data(&mut rng, num_mix_hops, 100000, 1000);
|
||||
assert_eq!(
|
||||
data3.serialized_size(num_mix_hops),
|
||||
data3.into_bytes().len()
|
||||
);
|
||||
|
||||
let additional_surbs1 = fixtures::repliable_content_surbs(&mut rng, num_mix_hops, 1);
|
||||
assert_eq!(
|
||||
additional_surbs1.serialized_size(num_mix_hops),
|
||||
additional_surbs1.into_bytes().len()
|
||||
);
|
||||
|
||||
let additional_surbs2 = fixtures::repliable_content_surbs(&mut rng, 1000);
|
||||
let additional_surbs2 = fixtures::repliable_content_surbs(&mut rng, num_mix_hops, 1000);
|
||||
assert_eq!(
|
||||
additional_surbs2.serialized_size(),
|
||||
additional_surbs2.serialized_size(num_mix_hops),
|
||||
additional_surbs2.into_bytes().len()
|
||||
);
|
||||
|
||||
let heartbeat1 = fixtures::repliable_content_heartbeat(&mut rng, 1);
|
||||
assert_eq!(heartbeat1.serialized_size(), heartbeat1.into_bytes().len());
|
||||
let heartbeat1 = fixtures::repliable_content_heartbeat(&mut rng, num_mix_hops, 1);
|
||||
assert_eq!(
|
||||
heartbeat1.serialized_size(num_mix_hops),
|
||||
heartbeat1.into_bytes().len()
|
||||
);
|
||||
|
||||
let heartbeat2 = fixtures::repliable_content_heartbeat(&mut rng, 1000);
|
||||
assert_eq!(heartbeat2.serialized_size(), heartbeat2.into_bytes().len());
|
||||
let heartbeat2 = fixtures::repliable_content_heartbeat(&mut rng, num_mix_hops, 1000);
|
||||
assert_eq!(
|
||||
heartbeat2.serialized_size(num_mix_hops),
|
||||
heartbeat2.into_bytes().len()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -69,11 +69,11 @@ pub mod monitoring {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn fragment_sent(fragment: &Fragment, client_nonce: i32, destination: PublicKey) {
|
||||
pub fn fragment_sent(fragment: &Fragment, client_nonce: i32, destination: PublicKey, hops: u8) {
|
||||
if enabled() {
|
||||
let id = fragment.fragment_identifier().set_id();
|
||||
let mut entry = FRAGMENTS_SENT.entry(id).or_default();
|
||||
let s = SentFragment::new(fragment.header(), now!(), client_nonce, destination);
|
||||
let s = SentFragment::new(fragment.header(), now!(), client_nonce, destination, hops);
|
||||
entry.push(s);
|
||||
}
|
||||
}
|
||||
@@ -82,11 +82,16 @@ pub mod monitoring {
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct FragmentMixParams {
|
||||
destination: PublicKey,
|
||||
hops: u8,
|
||||
}
|
||||
|
||||
impl FragmentMixParams {
|
||||
pub fn destination(&self) -> PublicKey {
|
||||
self.destination
|
||||
pub fn destination(&self) -> &PublicKey {
|
||||
&self.destination
|
||||
}
|
||||
|
||||
pub fn hops(&self) -> u8 {
|
||||
self.hops
|
||||
}
|
||||
}
|
||||
|
||||
@@ -100,8 +105,14 @@ pub struct SentFragment {
|
||||
}
|
||||
|
||||
impl SentFragment {
|
||||
fn new(header: FragmentHeader, at: u64, client_nonce: i32, destination: PublicKey) -> Self {
|
||||
let mixnet_params = FragmentMixParams { destination };
|
||||
fn new(
|
||||
header: FragmentHeader,
|
||||
at: u64,
|
||||
client_nonce: i32,
|
||||
destination: PublicKey,
|
||||
hops: u8,
|
||||
) -> Self {
|
||||
let mixnet_params = FragmentMixParams { destination, hops };
|
||||
SentFragment {
|
||||
header,
|
||||
at,
|
||||
|
||||
@@ -10,9 +10,11 @@ use nym_sphinx_addressing::nodes::NymNodeRoutingAddress;
|
||||
use nym_sphinx_chunking::fragment::COVER_FRAG_ID;
|
||||
use nym_sphinx_forwarding::packet::MixPacket;
|
||||
use nym_sphinx_params::packet_sizes::PacketSize;
|
||||
use nym_sphinx_params::{PacketEncryptionAlgorithm, PacketHkdfAlgorithm, PacketType};
|
||||
use nym_sphinx_params::{
|
||||
PacketEncryptionAlgorithm, PacketHkdfAlgorithm, PacketType, DEFAULT_NUM_MIX_HOPS,
|
||||
};
|
||||
use nym_sphinx_types::NymPacket;
|
||||
use nym_topology::{NymRouteProvider, NymTopologyError};
|
||||
use nym_topology::{NymTopology, NymTopologyError};
|
||||
use rand::{CryptoRng, RngCore};
|
||||
|
||||
use std::time;
|
||||
@@ -34,7 +36,7 @@ pub enum CoverMessageError {
|
||||
|
||||
pub fn generate_loop_cover_surb_ack<R>(
|
||||
rng: &mut R,
|
||||
topology: &NymRouteProvider,
|
||||
topology: &NymTopology,
|
||||
ack_key: &AckKey,
|
||||
full_address: &Recipient,
|
||||
average_ack_delay: time::Duration,
|
||||
@@ -57,7 +59,7 @@ where
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub fn generate_loop_cover_packet<R>(
|
||||
rng: &mut R,
|
||||
topology: &NymRouteProvider,
|
||||
topology: &NymTopology,
|
||||
ack_key: &AckKey,
|
||||
full_address: &Recipient,
|
||||
average_ack_delay: time::Duration,
|
||||
@@ -116,7 +118,8 @@ where
|
||||
.chain(cover_content)
|
||||
.collect();
|
||||
|
||||
let route = topology.random_route_to_egress(rng, full_address.gateway())?;
|
||||
let route =
|
||||
topology.random_route_to_gateway(rng, DEFAULT_NUM_MIX_HOPS, full_address.gateway())?;
|
||||
let delays = nym_sphinx_routing::generate_hop_delays(average_packet_delay, route.len());
|
||||
let destination = full_address.as_sphinx_destination();
|
||||
|
||||
|
||||
@@ -16,6 +16,10 @@ pub mod packet_sizes;
|
||||
pub mod packet_types;
|
||||
pub mod packet_version;
|
||||
|
||||
// If somebody can provide an argument why it might be reasonable to have more than 255 mix hops,
|
||||
// I will change this to [`usize`]
|
||||
pub const DEFAULT_NUM_MIX_HOPS: u8 = 3;
|
||||
|
||||
// TODO: not entirely sure how to feel about those being defined here, ideally it'd be where [`Fragment`]
|
||||
// is defined, but that'd introduce circular dependencies as the acknowledgements crate also needs
|
||||
// access to that
|
||||
|
||||
@@ -1,10 +1,19 @@
|
||||
// Copyright 2023 - Nym Technologies SA <contact@nymtech.net>
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
use nym_sphinx_types::{delays, Delay};
|
||||
use std::time::Duration;
|
||||
|
||||
use nym_sphinx_addressing::clients::Recipient;
|
||||
use nym_sphinx_types::{delays, Delay, Node};
|
||||
use thiserror::Error;
|
||||
|
||||
pub trait SphinxRouteMaker {
|
||||
type Error;
|
||||
|
||||
fn sphinx_route(&mut self, hops: u8, destination: &Recipient)
|
||||
-> Result<Vec<Node>, Self::Error>;
|
||||
}
|
||||
|
||||
#[derive(Debug, Error, Clone, Copy)]
|
||||
#[error("the route vector contains {available} nodes while {requested} hops are required")]
|
||||
pub struct InvalidNumberOfHops {
|
||||
@@ -12,6 +21,29 @@ pub struct InvalidNumberOfHops {
|
||||
requested: u8,
|
||||
}
|
||||
|
||||
// if one wants to provide a hardcoded route, they can
|
||||
impl SphinxRouteMaker for Vec<Node> {
|
||||
type Error = InvalidNumberOfHops;
|
||||
|
||||
fn sphinx_route(
|
||||
&mut self,
|
||||
hops: u8,
|
||||
_destination: &Recipient,
|
||||
) -> Result<Vec<Node>, InvalidNumberOfHops> {
|
||||
// it's the responsibility of the caller to ensure the hardcoded route has correct number of hops
|
||||
// and that it's final hop include the recipient's gateway.
|
||||
|
||||
if self.len() != hops as usize {
|
||||
Err(InvalidNumberOfHops {
|
||||
available: self.len(),
|
||||
requested: hops,
|
||||
})
|
||||
} else {
|
||||
Ok(self.clone())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn generate_hop_delays(average_packet_delay: Duration, num_hops: usize) -> Vec<Delay> {
|
||||
if average_packet_delay.is_zero() {
|
||||
vec![nym_sphinx_types::Delay::new_from_millis(0); num_hops]
|
||||
|
||||
@@ -149,7 +149,7 @@ impl NymMessage {
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn try_from_bytes(bytes: &[u8]) -> Result<Self, NymMessageError> {
|
||||
fn try_from_bytes(bytes: &[u8], num_mix_hops: u8) -> Result<Self, NymMessageError> {
|
||||
if bytes.is_empty() {
|
||||
return Err(NymMessageError::EmptyMessage);
|
||||
}
|
||||
@@ -158,7 +158,7 @@ impl NymMessage {
|
||||
match typ_tag {
|
||||
NymMessageType::Plain => Ok(NymMessage::Plain(bytes[1..].to_vec())),
|
||||
NymMessageType::Repliable => Ok(NymMessage::Repliable(
|
||||
RepliableMessage::try_from_bytes(&bytes[1..])?,
|
||||
RepliableMessage::try_from_bytes(&bytes[1..], num_mix_hops)?,
|
||||
)),
|
||||
NymMessageType::Reply => Ok(NymMessage::Reply(ReplyMessage::try_from_bytes(
|
||||
&bytes[1..],
|
||||
@@ -166,10 +166,10 @@ impl NymMessage {
|
||||
}
|
||||
}
|
||||
|
||||
fn serialized_size(&self) -> usize {
|
||||
fn serialized_size(&self, num_mix_hops: u8) -> usize {
|
||||
let inner_size = match self {
|
||||
NymMessage::Plain(msg) => msg.len(),
|
||||
NymMessage::Repliable(msg) => msg.serialized_size(),
|
||||
NymMessage::Repliable(msg) => msg.serialized_size(num_mix_hops),
|
||||
NymMessage::Reply(msg) => msg.serialized_size(),
|
||||
};
|
||||
let message_type_size = 1;
|
||||
@@ -207,9 +207,9 @@ impl NymMessage {
|
||||
}
|
||||
|
||||
/// Determines the number of required packets of the provided size for the split message.
|
||||
pub fn required_packets(&self, packet_size: PacketSize) -> usize {
|
||||
pub fn required_packets(&self, packet_size: PacketSize, num_mix_hops: u8) -> usize {
|
||||
let plaintext_per_packet = self.true_available_plaintext_per_packet(packet_size);
|
||||
let serialized_len = self.serialized_size();
|
||||
let serialized_len = self.serialized_size(num_mix_hops);
|
||||
|
||||
let (num_fragments, _) =
|
||||
chunking::number_of_required_fragments(serialized_len, plaintext_per_packet);
|
||||
@@ -279,11 +279,11 @@ impl PaddedMessage {
|
||||
}
|
||||
|
||||
// reverse of NymMessage::pad_to_full_packet_lengths
|
||||
pub fn remove_padding(self) -> Result<NymMessage, NymMessageError> {
|
||||
pub fn remove_padding(self, num_mix_hops: u8) -> Result<NymMessage, NymMessageError> {
|
||||
// we are looking for first occurrence of 1 in the tail and we get its index
|
||||
if let Some(padding_end) = self.0.iter().rposition(|b| *b == 1) {
|
||||
// and now we only take bytes until that point (but not including it)
|
||||
NymMessage::try_from_bytes(&self.0[..padding_end])
|
||||
NymMessage::try_from_bytes(&self.0[..padding_end], num_mix_hops)
|
||||
} else {
|
||||
Err(NymMessageError::InvalidMessagePadding)
|
||||
}
|
||||
@@ -304,7 +304,7 @@ mod tests {
|
||||
fn serialized_size_matches_actual_serialization() {
|
||||
// plain
|
||||
let plain = NymMessage::new_plain(vec![1, 2, 3, 4, 5]);
|
||||
assert_eq!(plain.serialized_size(), plain.into_bytes().len());
|
||||
assert_eq!(plain.serialized_size(3), plain.into_bytes().len());
|
||||
|
||||
// a single variant for each repliable and reply is enough as they are more thoroughly tested
|
||||
// internally
|
||||
@@ -313,9 +313,9 @@ mod tests {
|
||||
[42u8; 16].into(),
|
||||
vec![],
|
||||
));
|
||||
assert_eq!(repliable.serialized_size(), repliable.into_bytes().len());
|
||||
assert_eq!(repliable.serialized_size(3), repliable.into_bytes().len());
|
||||
|
||||
let reply = NymMessage::new_reply(ReplyMessage::new_data_message(vec![1, 2, 3, 4, 5]));
|
||||
assert_eq!(reply.serialized_size(), reply.into_bytes().len());
|
||||
assert_eq!(reply.serialized_size(3), reply.into_bytes().len());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,9 +14,9 @@ use nym_sphinx_anonymous_replies::reply_surb::ReplySurb;
|
||||
use nym_sphinx_chunking::fragment::{Fragment, FragmentIdentifier};
|
||||
use nym_sphinx_forwarding::packet::MixPacket;
|
||||
use nym_sphinx_params::packet_sizes::PacketSize;
|
||||
use nym_sphinx_params::{PacketType, ReplySurbKeyDigestAlgorithm};
|
||||
use nym_sphinx_params::{PacketType, ReplySurbKeyDigestAlgorithm, DEFAULT_NUM_MIX_HOPS};
|
||||
use nym_sphinx_types::{Delay, NymPacket};
|
||||
use nym_topology::{NymRouteProvider, NymTopologyError};
|
||||
use nym_topology::{NymTopology, NymTopologyError};
|
||||
use rand::{CryptoRng, Rng, SeedableRng};
|
||||
use rand_chacha::ChaCha8Rng;
|
||||
|
||||
@@ -54,13 +54,14 @@ pub trait FragmentPreparer {
|
||||
fn deterministic_route_selection(&self) -> bool;
|
||||
fn rng(&mut self) -> &mut Self::Rng;
|
||||
fn nonce(&self) -> i32;
|
||||
fn num_mix_hops(&self) -> u8;
|
||||
fn average_packet_delay(&self) -> Duration;
|
||||
fn average_ack_delay(&self) -> Duration;
|
||||
|
||||
fn generate_reply_surbs(
|
||||
&mut self,
|
||||
amount: usize,
|
||||
topology: &NymRouteProvider,
|
||||
topology: &NymTopology,
|
||||
reply_recipient: &Recipient,
|
||||
) -> Result<Vec<ReplySurb>, NymTopologyError> {
|
||||
let mut reply_surbs = Vec::with_capacity(amount);
|
||||
@@ -78,7 +79,7 @@ pub trait FragmentPreparer {
|
||||
&mut self,
|
||||
recipient: &Recipient,
|
||||
fragment_id: FragmentIdentifier,
|
||||
topology: &NymRouteProvider,
|
||||
topology: &NymTopology,
|
||||
ack_key: &AckKey,
|
||||
packet_type: PacketType,
|
||||
) -> Result<SurbAck, NymTopologyError> {
|
||||
@@ -108,7 +109,7 @@ pub trait FragmentPreparer {
|
||||
fn prepare_reply_chunk_for_sending(
|
||||
&mut self,
|
||||
fragment: Fragment,
|
||||
topology: &NymRouteProvider,
|
||||
topology: &NymTopology,
|
||||
ack_key: &AckKey,
|
||||
reply_surb: ReplySurb,
|
||||
packet_sender: &Recipient,
|
||||
@@ -129,8 +130,9 @@ pub trait FragmentPreparer {
|
||||
.expect("the message has been incorrectly fragmented");
|
||||
|
||||
// this is not going to be accurate by any means. but that's the best estimation we can do
|
||||
let expected_forward_delay =
|
||||
Delay::new_from_millis((self.average_packet_delay().as_millis() * 3) as u64);
|
||||
let expected_forward_delay = Delay::new_from_millis(
|
||||
(self.average_packet_delay().as_millis() * self.num_mix_hops() as u128) as u64,
|
||||
);
|
||||
|
||||
let fragment_identifier = fragment.fragment_identifier();
|
||||
|
||||
@@ -188,11 +190,12 @@ pub trait FragmentPreparer {
|
||||
fn prepare_chunk_for_sending(
|
||||
&mut self,
|
||||
fragment: Fragment,
|
||||
topology: &NymRouteProvider,
|
||||
topology: &NymTopology,
|
||||
ack_key: &AckKey,
|
||||
packet_sender: &Recipient,
|
||||
packet_recipient: &Recipient,
|
||||
packet_type: PacketType,
|
||||
mix_hops: Option<u8>,
|
||||
) -> Result<PreparedFragment, NymTopologyError> {
|
||||
debug!("Preparing chunk for sending");
|
||||
// each plain or repliable packet (i.e. not a reply) attaches an ephemeral public key so that the recipient
|
||||
@@ -201,7 +204,8 @@ pub trait FragmentPreparer {
|
||||
|
||||
let fragment_header = fragment.header();
|
||||
let destination = packet_recipient.gateway();
|
||||
monitoring::fragment_sent(&fragment, self.nonce(), destination);
|
||||
let hops = mix_hops.unwrap_or(self.num_mix_hops());
|
||||
monitoring::fragment_sent(&fragment, self.nonce(), *destination, hops);
|
||||
|
||||
let non_reply_overhead = encryption::PUBLIC_KEY_SIZE;
|
||||
let expected_plaintext = match packet_type {
|
||||
@@ -236,16 +240,16 @@ pub trait FragmentPreparer {
|
||||
};
|
||||
|
||||
// generate pseudorandom route for the packet
|
||||
log::trace!("Preparing chunk for sending");
|
||||
log::trace!("Preparing chunk for sending with {hops} mix hops");
|
||||
let route = if self.deterministic_route_selection() {
|
||||
log::trace!("using deterministic route selection");
|
||||
let seed = fragment_header.seed().wrapping_mul(self.nonce());
|
||||
let mut rng = ChaCha8Rng::seed_from_u64(seed as u64);
|
||||
topology.random_route_to_egress(&mut rng, destination)?
|
||||
topology.random_route_to_gateway(&mut rng, hops, destination)?
|
||||
} else {
|
||||
log::trace!("using pseudorandom route selection");
|
||||
let mut rng = self.rng();
|
||||
topology.random_route_to_egress(&mut rng, destination)?
|
||||
topology.random_route_to_gateway(&mut rng, hops, destination)?
|
||||
};
|
||||
|
||||
let destination = packet_recipient.as_sphinx_destination();
|
||||
@@ -331,6 +335,10 @@ pub struct MessagePreparer<R> {
|
||||
/// Average delay an acknowledgement packet is going to get delay at a single mixnode.
|
||||
average_ack_delay: Duration,
|
||||
|
||||
/// Number of mix hops each packet ('real' message, ack, reply) is expected to take.
|
||||
/// Note that it does not include gateway hops.
|
||||
num_mix_hops: u8,
|
||||
|
||||
nonce: i32,
|
||||
}
|
||||
|
||||
@@ -353,10 +361,17 @@ where
|
||||
sender_address,
|
||||
average_packet_delay,
|
||||
average_ack_delay,
|
||||
num_mix_hops: DEFAULT_NUM_MIX_HOPS,
|
||||
nonce,
|
||||
}
|
||||
}
|
||||
|
||||
/// Allows setting non-default number of expected mix hops in the network.
|
||||
pub fn with_mix_hops(mut self, hops: u8) -> Self {
|
||||
self.num_mix_hops = hops;
|
||||
self
|
||||
}
|
||||
|
||||
/// Overwrites existing sender address with the provided value.
|
||||
pub fn set_sender_address(&mut self, sender_address: Recipient) {
|
||||
self.sender_address = sender_address;
|
||||
@@ -365,7 +380,7 @@ where
|
||||
pub fn generate_reply_surbs(
|
||||
&mut self,
|
||||
amount: usize,
|
||||
topology: &NymRouteProvider,
|
||||
topology: &NymTopology,
|
||||
) -> Result<Vec<ReplySurb>, NymTopologyError> {
|
||||
let mut reply_surbs = Vec::with_capacity(amount);
|
||||
for _ in 0..amount {
|
||||
@@ -384,7 +399,7 @@ where
|
||||
pub fn prepare_reply_chunk_for_sending(
|
||||
&mut self,
|
||||
fragment: Fragment,
|
||||
topology: &NymRouteProvider,
|
||||
topology: &NymTopology,
|
||||
ack_key: &AckKey,
|
||||
reply_surb: ReplySurb,
|
||||
packet_type: PacketType,
|
||||
@@ -405,10 +420,11 @@ where
|
||||
pub fn prepare_chunk_for_sending(
|
||||
&mut self,
|
||||
fragment: Fragment,
|
||||
topology: &NymRouteProvider,
|
||||
topology: &NymTopology,
|
||||
ack_key: &AckKey,
|
||||
packet_recipient: &Recipient,
|
||||
packet_type: PacketType,
|
||||
mix_hops: Option<u8>,
|
||||
) -> Result<PreparedFragment, NymTopologyError> {
|
||||
let sender = self.sender_address;
|
||||
|
||||
@@ -420,6 +436,7 @@ where
|
||||
&sender,
|
||||
packet_recipient,
|
||||
packet_type,
|
||||
mix_hops,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -427,7 +444,7 @@ where
|
||||
pub fn generate_surb_ack(
|
||||
&mut self,
|
||||
fragment_id: FragmentIdentifier,
|
||||
topology: &NymRouteProvider,
|
||||
topology: &NymTopology,
|
||||
ack_key: &AckKey,
|
||||
packet_type: PacketType,
|
||||
) -> Result<SurbAck, NymTopologyError> {
|
||||
@@ -466,6 +483,10 @@ impl<R: CryptoRng + Rng> FragmentPreparer for MessagePreparer<R> {
|
||||
self.nonce
|
||||
}
|
||||
|
||||
fn num_mix_hops(&self) -> u8 {
|
||||
self.num_mix_hops
|
||||
}
|
||||
|
||||
fn average_packet_delay(&self) -> Duration {
|
||||
self.average_packet_delay
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ use nym_sphinx_chunking::reconstruction::MessageReconstructor;
|
||||
use nym_sphinx_chunking::ChunkingError;
|
||||
use nym_sphinx_params::{
|
||||
PacketEncryptionAlgorithm, PacketHkdfAlgorithm, ReplySurbEncryptionAlgorithm,
|
||||
DEFAULT_NUM_MIX_HOPS,
|
||||
};
|
||||
use thiserror::Error;
|
||||
|
||||
@@ -78,6 +79,7 @@ pub enum MessageRecoveryError {
|
||||
pub trait MessageReceiver {
|
||||
fn new() -> Self;
|
||||
fn reconstructor(&mut self) -> &mut MessageReconstructor;
|
||||
fn num_mix_hops(&self) -> u8;
|
||||
|
||||
fn decrypt_raw_message<C>(
|
||||
&self,
|
||||
@@ -141,7 +143,7 @@ pub trait MessageReceiver {
|
||||
fragment: Fragment,
|
||||
) -> Result<Option<(NymMessage, Vec<i32>)>, MessageRecoveryError> {
|
||||
if let Some((message, used_sets)) = self.reconstructor().insert_new_fragment(fragment) {
|
||||
match PaddedMessage::new_reconstructed(message).remove_padding() {
|
||||
match PaddedMessage::new_reconstructed(message).remove_padding(self.num_mix_hops()) {
|
||||
Ok(message) => Ok(Some((message, used_sets))),
|
||||
Err(err) => Err(MessageRecoveryError::MalformedReconstructedMessage {
|
||||
source: err,
|
||||
@@ -154,11 +156,28 @@ pub trait MessageReceiver {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
#[derive(Clone)]
|
||||
pub struct SphinxMessageReceiver {
|
||||
/// High level public structure used to buffer all received data [`Fragment`]s and eventually
|
||||
/// returning original messages that they encapsulate.
|
||||
reconstructor: MessageReconstructor,
|
||||
|
||||
/// Number of mix hops each packet ('real' message, ack, reply) is expected to take.
|
||||
/// Note that it does not include gateway hops.
|
||||
num_mix_hops: u8,
|
||||
}
|
||||
|
||||
impl SphinxMessageReceiver {
|
||||
/// Allows setting non-default number of expected mix hops in the network.
|
||||
// IMPORTANT NOTE: this is among others used to deserialize SURBs. Meaning that this is a
|
||||
// global setting and currently always set to the default value. The implication is that it is
|
||||
// not currently possible to have different number of hops for different SURB messages. So,
|
||||
// don't try to use <3 mix hops for SURBs until this is refactored.
|
||||
#[must_use]
|
||||
pub fn with_mix_hops(mut self, hops: u8) -> Self {
|
||||
self.num_mix_hops = hops;
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl MessageReceiver for SphinxMessageReceiver {
|
||||
@@ -182,4 +201,112 @@ impl MessageReceiver for SphinxMessageReceiver {
|
||||
fn reconstructor(&mut self) -> &mut MessageReconstructor {
|
||||
&mut self.reconstructor
|
||||
}
|
||||
|
||||
fn num_mix_hops(&self) -> u8 {
|
||||
self.num_mix_hops
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for SphinxMessageReceiver {
|
||||
fn default() -> Self {
|
||||
SphinxMessageReceiver {
|
||||
reconstructor: Default::default(),
|
||||
num_mix_hops: DEFAULT_NUM_MIX_HOPS,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod message_receiver {
|
||||
use super::*;
|
||||
use nym_crypto::asymmetric::identity;
|
||||
use nym_mixnet_contract_common::LegacyMixLayer;
|
||||
use nym_topology::{gateway, mix, NymTopology};
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
// TODO: is it somehow maybe possible to move it to `topology` and have if conditionally
|
||||
// available to other modules?
|
||||
/// Returns a hardcoded, valid instance of [`NymTopology`] that is to be used in
|
||||
/// tests requiring instance of topology.
|
||||
#[allow(dead_code)]
|
||||
fn topology_fixture() -> NymTopology {
|
||||
let mut mixes = BTreeMap::new();
|
||||
mixes.insert(
|
||||
1,
|
||||
vec![mix::LegacyNode {
|
||||
mix_id: 123,
|
||||
host: "10.20.30.40".parse().unwrap(),
|
||||
mix_host: "10.20.30.40:1789".parse().unwrap(),
|
||||
identity_key: identity::PublicKey::from_base58_string(
|
||||
"3ebjp1Fb9hdcS1AR6AZihgeJiMHkB5jjJUsvqNnfQwU7",
|
||||
)
|
||||
.unwrap(),
|
||||
sphinx_key: encryption::PublicKey::from_base58_string(
|
||||
"B3GzG62aXAZNg14RoMCp3BhELNBrySLr2JqrwyfYFzRc",
|
||||
)
|
||||
.unwrap(),
|
||||
layer: LegacyMixLayer::One,
|
||||
version: "0.8.0-dev".into(),
|
||||
}],
|
||||
);
|
||||
|
||||
mixes.insert(
|
||||
2,
|
||||
vec![mix::LegacyNode {
|
||||
mix_id: 234,
|
||||
host: "11.21.31.41".parse().unwrap(),
|
||||
mix_host: "11.21.31.41:1789".parse().unwrap(),
|
||||
identity_key: identity::PublicKey::from_base58_string(
|
||||
"D6YaMzLSY7mANtSQRKXsmMZpqgqiVkeiagKM4V4oFPFr",
|
||||
)
|
||||
.unwrap(),
|
||||
sphinx_key: encryption::PublicKey::from_base58_string(
|
||||
"5Z1VqYwM2xeKxd8H7fJpGWasNiDFijYBAee7MErkZ5QT",
|
||||
)
|
||||
.unwrap(),
|
||||
layer: LegacyMixLayer::Two,
|
||||
version: "0.8.0-dev".into(),
|
||||
}],
|
||||
);
|
||||
|
||||
mixes.insert(
|
||||
3,
|
||||
vec![mix::LegacyNode {
|
||||
mix_id: 456,
|
||||
host: "12.22.32.42".parse().unwrap(),
|
||||
mix_host: "12.22.32.42:1789".parse().unwrap(),
|
||||
identity_key: identity::PublicKey::from_base58_string(
|
||||
"GkWDysw4AjESv1KiAiVn7JzzCMJeksxNSXVfr1PpX8wD",
|
||||
)
|
||||
.unwrap(),
|
||||
sphinx_key: encryption::PublicKey::from_base58_string(
|
||||
"9EyjhCggr2QEA2nakR88YHmXgpy92DWxoe2draDRkYof",
|
||||
)
|
||||
.unwrap(),
|
||||
layer: LegacyMixLayer::Three,
|
||||
version: "0.8.0-dev".into(),
|
||||
}],
|
||||
);
|
||||
|
||||
NymTopology::new(
|
||||
// currently coco_nodes don't really exist so this is still to be determined
|
||||
mixes,
|
||||
vec![gateway::LegacyNode {
|
||||
node_id: 789,
|
||||
host: "1.2.3.4".parse().unwrap(),
|
||||
mix_host: "1.2.3.4:1789".parse().unwrap(),
|
||||
clients_ws_port: 9000,
|
||||
clients_wss_port: None,
|
||||
identity_key: identity::PublicKey::from_base58_string(
|
||||
"FioFa8nMmPpQnYi7JyojoTuwGLeyNS8BF4ChPr29zUML",
|
||||
)
|
||||
.unwrap(),
|
||||
sphinx_key: encryption::PublicKey::from_base58_string(
|
||||
"EB42xvMFMD5rUCstE2CDazgQQJ22zLv8SPm1Luxni44c",
|
||||
)
|
||||
.unwrap(),
|
||||
version: "0.8.0-dev".into(),
|
||||
}],
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,43 +42,8 @@ impl PendingSync {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct BlockProcessorConfig {
|
||||
pub pruning_options: PruningOptions,
|
||||
pub store_precommits: bool,
|
||||
pub explicit_starting_block_height: Option<u32>,
|
||||
pub use_best_effort_start_height: bool,
|
||||
}
|
||||
|
||||
impl Default for BlockProcessorConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
pruning_options: PruningOptions::nothing(),
|
||||
store_precommits: true,
|
||||
explicit_starting_block_height: None,
|
||||
use_best_effort_start_height: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl BlockProcessorConfig {
|
||||
pub fn new(
|
||||
pruning_options: PruningOptions,
|
||||
store_precommits: bool,
|
||||
explicit_starting_block_height: Option<u32>,
|
||||
use_best_effort_start_height: bool,
|
||||
) -> Self {
|
||||
Self {
|
||||
pruning_options,
|
||||
store_precommits,
|
||||
explicit_starting_block_height,
|
||||
use_best_effort_start_height,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct BlockProcessor {
|
||||
config: BlockProcessorConfig,
|
||||
pruning_options: PruningOptions,
|
||||
cancel: CancellationToken,
|
||||
synced: Arc<Notify>,
|
||||
last_processed_height: u32,
|
||||
@@ -100,10 +65,9 @@ pub struct BlockProcessor {
|
||||
msg_modules: Vec<Box<dyn MsgModule + Send>>,
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
impl BlockProcessor {
|
||||
pub async fn new(
|
||||
config: BlockProcessorConfig,
|
||||
pruning_options: PruningOptions,
|
||||
cancel: CancellationToken,
|
||||
synced: Arc<Notify>,
|
||||
incoming: UnboundedReceiver<BlockToProcess>,
|
||||
@@ -117,10 +81,8 @@ impl BlockProcessor {
|
||||
let last_pruned = storage.get_pruned_height().await?;
|
||||
let last_pruned_height = last_pruned.try_into().unwrap_or_default();
|
||||
|
||||
debug!(last_processed_height = %last_processed_height, pruned_height = %last_pruned_height, "setting up block processor...");
|
||||
|
||||
Ok(BlockProcessor {
|
||||
config,
|
||||
pruning_options,
|
||||
cancel,
|
||||
synced,
|
||||
last_processed_height,
|
||||
@@ -139,7 +101,7 @@ impl BlockProcessor {
|
||||
}
|
||||
|
||||
pub fn with_pruning(mut self, pruning_options: PruningOptions) -> Self {
|
||||
self.config.pruning_options = pruning_options;
|
||||
self.pruning_options = pruning_options;
|
||||
self
|
||||
}
|
||||
|
||||
@@ -166,7 +128,7 @@ impl BlockProcessor {
|
||||
// we won't end up with a corrupted storage.
|
||||
let mut tx = self.storage.begin_processing_tx().await?;
|
||||
|
||||
persist_block(&full_info, &mut tx, self.config.store_precommits).await?;
|
||||
persist_block(&full_info, &mut tx).await?;
|
||||
|
||||
// let the modules do whatever they want
|
||||
// the ones wanting the full block:
|
||||
@@ -279,7 +241,7 @@ impl BlockProcessor {
|
||||
|
||||
#[instrument(skip(self))]
|
||||
async fn prune_storage(&mut self) -> Result<(), ScraperError> {
|
||||
let keep_recent = self.config.pruning_options.strategy_keep_recent();
|
||||
let keep_recent = self.pruning_options.strategy_keep_recent();
|
||||
let last_to_keep = self.last_processed_height - keep_recent;
|
||||
|
||||
info!(
|
||||
@@ -320,12 +282,12 @@ impl BlockProcessor {
|
||||
async fn maybe_prune_storage(&mut self) -> Result<(), ScraperError> {
|
||||
debug!("checking for storage pruning");
|
||||
|
||||
if self.config.pruning_options.strategy.is_nothing() {
|
||||
if self.pruning_options.strategy.is_nothing() {
|
||||
trace!("the current pruning strategy is 'nothing'");
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let interval = self.config.pruning_options.strategy_interval();
|
||||
let interval = self.pruning_options.strategy_interval();
|
||||
if self.last_pruned_height + interval <= self.last_processed_height {
|
||||
self.prune_storage().await?;
|
||||
}
|
||||
@@ -401,69 +363,20 @@ impl BlockProcessor {
|
||||
// but we need it to help the compiler figure out the future is `Send`
|
||||
async fn startup_resync(&mut self) -> Result<(), ScraperError> {
|
||||
assert!(self.pending_sync.is_empty());
|
||||
info!("attempting to run startup resync...");
|
||||
|
||||
self.maybe_prune_storage().await?;
|
||||
|
||||
let latest_block = self.rpc_client.current_block_height().await? as u32;
|
||||
info!("obtained latest block height: {latest_block}");
|
||||
|
||||
if latest_block > self.last_processed_height && self.last_processed_height != 0 {
|
||||
info!("we have already processed some blocks in the past - attempting to resume...");
|
||||
// in case we were offline for a while,
|
||||
// make sure we don't request blocks we'd have to prune anyway
|
||||
let keep_recent = self.config.pruning_options.strategy_keep_recent();
|
||||
let keep_recent = self.pruning_options.strategy_keep_recent();
|
||||
let last_to_keep = latest_block - keep_recent;
|
||||
|
||||
if !self.config.pruning_options.strategy.is_nothing() {
|
||||
self.last_processed_height = max(self.last_processed_height, last_to_keep);
|
||||
}
|
||||
self.last_processed_height = max(self.last_processed_height, last_to_keep);
|
||||
|
||||
let request_range = self.last_processed_height + 1..latest_block + 1;
|
||||
info!(
|
||||
keep_recent = %keep_recent,
|
||||
last_to_keep = %last_to_keep,
|
||||
last_processed_height = %self.last_processed_height,
|
||||
"we need to request {request_range:?} to resync"
|
||||
);
|
||||
self.request_missing_blocks(request_range).await?;
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
// this is the first time starting up
|
||||
if self.last_processed_height == 0 {
|
||||
info!("this is the first time starting up");
|
||||
let Some(starting_height) = self.config.explicit_starting_block_height else {
|
||||
info!("no starting block height set - will use the default behaviour");
|
||||
// nothing to do
|
||||
return Ok(());
|
||||
};
|
||||
|
||||
info!("attempting to start the scraper from block {starting_height}");
|
||||
let earliest_available =
|
||||
self.rpc_client.earliest_available_block_height().await? as u32;
|
||||
info!("earliest available block height: {earliest_available}");
|
||||
|
||||
if earliest_available > starting_height && self.config.use_best_effort_start_height {
|
||||
error!("the earliest available block is higher than the desired starting height");
|
||||
return Err(ScraperError::BlocksUnavailable {
|
||||
height: starting_height,
|
||||
});
|
||||
}
|
||||
|
||||
let starting_height = if earliest_available > starting_height {
|
||||
// add few additional blocks to account for all the startup waiting
|
||||
// because the node might have pruned few blocks since
|
||||
earliest_available + 10
|
||||
} else {
|
||||
starting_height
|
||||
};
|
||||
|
||||
let request_range = starting_height..latest_block + 1;
|
||||
|
||||
info!("going to start the scraper from block {starting_height}");
|
||||
info!("we need to request {request_range:?} before properly starting up");
|
||||
|
||||
info!("we need to request {request_range:?} to resync");
|
||||
self.request_missing_blocks(request_range).await?;
|
||||
}
|
||||
|
||||
@@ -471,7 +384,7 @@ impl BlockProcessor {
|
||||
}
|
||||
|
||||
pub(crate) async fn run(&mut self) {
|
||||
info!("starting block processor processing loop");
|
||||
info!("starting processing loop");
|
||||
|
||||
// sure, we could be more efficient and reset it on every processed block,
|
||||
// but the overhead is so minimal that it doesn't matter
|
||||
|
||||
@@ -84,7 +84,13 @@ impl TryFrom<Event> for BlockToProcess {
|
||||
|
||||
// TODO: we're losing `result_begin_block` and `result_end_block` here but maybe that's fine?
|
||||
let maybe_block = match event.data {
|
||||
EventData::NewBlock { block, .. } => block,
|
||||
// we don't care about `NewBlock` until CometBFT 0.38, i.e. until we upgrade to wasmd 0.50
|
||||
EventData::NewBlock { .. } => {
|
||||
return Err(ScraperError::InvalidSubscriptionEvent {
|
||||
query,
|
||||
kind: "NewBlock".to_string(),
|
||||
})
|
||||
}
|
||||
EventData::LegacyNewBlock { block, .. } => block,
|
||||
EventData::Tx { .. } => {
|
||||
return Err(ScraperError::InvalidSubscriptionEvent {
|
||||
|
||||
@@ -19,9 +19,6 @@ pub enum ScraperError {
|
||||
#[error("the block scraper is already running")]
|
||||
ScraperAlreadyRunning,
|
||||
|
||||
#[error("block information for height {height} is not available on the provided rpc endpoint")]
|
||||
BlocksUnavailable { height: u32 },
|
||||
|
||||
#[error("failed to establish websocket connection to {url}: {source}")]
|
||||
WebSocketConnectionFailure {
|
||||
url: String,
|
||||
|
||||
@@ -15,7 +15,6 @@ pub(crate) mod scraper;
|
||||
pub mod storage;
|
||||
|
||||
pub use block_processor::pruning::{PruningOptions, PruningStrategy};
|
||||
pub use block_processor::types::ParsedTransactionResponse;
|
||||
pub use modules::{BlockModule, MsgModule, TxModule};
|
||||
pub use scraper::{Config, NyxdScraper, StartingBlockOpts};
|
||||
pub use scraper::{Config, NyxdScraper};
|
||||
pub use storage::models;
|
||||
|
||||
@@ -117,17 +117,6 @@ impl RpcClient {
|
||||
Ok(info.last_block_height.value())
|
||||
}
|
||||
|
||||
pub(crate) async fn earliest_available_block_height(&self) -> Result<u64, ScraperError> {
|
||||
debug!("getting earliest available block height");
|
||||
|
||||
let status = self
|
||||
.inner
|
||||
.status()
|
||||
.await
|
||||
.map_err(|source| ScraperError::AbciInfoQueryFailure { source })?;
|
||||
Ok(status.sync_info.earliest_block_height.value())
|
||||
}
|
||||
|
||||
async fn get_transaction_results(
|
||||
&self,
|
||||
raw: &[Vec<u8>],
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
use crate::block_processor::types::BlockToProcess;
|
||||
use crate::block_processor::{BlockProcessor, BlockProcessorConfig};
|
||||
use crate::block_processor::BlockProcessor;
|
||||
use crate::block_requester::{BlockRequest, BlockRequester};
|
||||
use crate::error::ScraperError;
|
||||
use crate::modules::{BlockModule, MsgModule, TxModule};
|
||||
@@ -24,15 +24,6 @@ use url::Url;
|
||||
|
||||
mod subscriber;
|
||||
|
||||
#[derive(Default, Clone, Copy)]
|
||||
pub struct StartingBlockOpts {
|
||||
pub start_block_height: Option<u32>,
|
||||
|
||||
/// If the scraper fails to start from the desired height, rather than failing,
|
||||
/// attempt to use the next available height
|
||||
pub use_best_effort_start_height: bool,
|
||||
}
|
||||
|
||||
pub struct Config {
|
||||
/// Url to the websocket endpoint of a validator, for example `wss://rpc.nymtech.net/websocket`
|
||||
pub websocket_url: Url,
|
||||
@@ -43,10 +34,6 @@ pub struct Config {
|
||||
pub database_path: PathBuf,
|
||||
|
||||
pub pruning_options: PruningOptions,
|
||||
|
||||
pub store_precommits: bool,
|
||||
|
||||
pub start_block: StartingBlockOpts,
|
||||
}
|
||||
|
||||
pub struct NyxdScraperBuilder {
|
||||
@@ -73,16 +60,8 @@ impl NyxdScraperBuilder {
|
||||
req_rx,
|
||||
processing_tx.clone(),
|
||||
);
|
||||
|
||||
let block_processor_config = BlockProcessorConfig::new(
|
||||
scraper.config.pruning_options,
|
||||
scraper.config.store_precommits,
|
||||
scraper.config.start_block.start_block_height,
|
||||
scraper.config.start_block.use_best_effort_start_height,
|
||||
);
|
||||
|
||||
let mut block_processor = BlockProcessor::new(
|
||||
block_processor_config,
|
||||
scraper.config.pruning_options,
|
||||
scraper.cancel_token.clone(),
|
||||
scraper.startup_sync.clone(),
|
||||
processing_rx,
|
||||
@@ -139,7 +118,7 @@ pub struct NyxdScraper {
|
||||
task_tracker: TaskTracker,
|
||||
cancel_token: CancellationToken,
|
||||
startup_sync: Arc<Notify>,
|
||||
storage: ScraperStorage,
|
||||
pub storage: ScraperStorage,
|
||||
rpc_client: RpcClient,
|
||||
}
|
||||
|
||||
@@ -163,10 +142,6 @@ impl NyxdScraper {
|
||||
})
|
||||
}
|
||||
|
||||
pub fn storage(&self) -> ScraperStorage {
|
||||
self.storage.clone()
|
||||
}
|
||||
|
||||
fn start_tasks(
|
||||
&self,
|
||||
mut block_requester: BlockRequester,
|
||||
@@ -183,10 +158,7 @@ impl NyxdScraper {
|
||||
self.task_tracker.close();
|
||||
}
|
||||
|
||||
// DO NOT USE UNLESS YOU KNOW EXACTLY WHAT YOU'RE DOING
|
||||
// AS THIS WILL NOT USE ANY OF YOUR REGISTERED MODULES
|
||||
// YOU WILL BE FIRED IF YOU USE IT : )
|
||||
pub async fn unsafe_process_single_block(&self, height: u32) -> Result<(), ScraperError> {
|
||||
pub async fn process_single_block(&self, height: u32) -> Result<(), ScraperError> {
|
||||
info!(height = height, "attempting to process a single block");
|
||||
if !self.task_tracker.is_empty() {
|
||||
return Err(ScraperError::ScraperAlreadyRunning);
|
||||
@@ -205,10 +177,7 @@ impl NyxdScraper {
|
||||
block_processor.process_block(block.into()).await
|
||||
}
|
||||
|
||||
// DO NOT USE UNLESS YOU KNOW EXACTLY WHAT YOU'RE DOING
|
||||
// AS THIS WILL NOT USE ANY OF YOUR REGISTERED MODULES
|
||||
// YOU WILL BE FIRED IF YOU USE IT : )
|
||||
pub async fn unsafe_process_block_range(
|
||||
pub async fn process_block_range(
|
||||
&self,
|
||||
starting_height: Option<u32>,
|
||||
end_height: Option<u32>,
|
||||
@@ -225,10 +194,10 @@ impl NyxdScraper {
|
||||
.await?
|
||||
.with_pruning(PruningOptions::nothing());
|
||||
|
||||
let mut current_height = self.rpc_client.current_block_height().await? as u32;
|
||||
let current_height = self.rpc_client.current_block_height().await? as u32;
|
||||
let last_processed = block_processor.last_process_height();
|
||||
|
||||
let mut starting_height = match starting_height {
|
||||
let starting_height = match starting_height {
|
||||
// always attempt to use whatever the user has provided
|
||||
Some(explicit) => explicit,
|
||||
None => {
|
||||
@@ -242,8 +211,7 @@ impl NyxdScraper {
|
||||
}
|
||||
};
|
||||
|
||||
let must_catch_up = end_height.is_none();
|
||||
let mut end_height = match end_height {
|
||||
let end_height = match end_height {
|
||||
// always attempt to use whatever the user has provided
|
||||
Some(explicit) => explicit,
|
||||
None => {
|
||||
@@ -258,62 +226,32 @@ impl NyxdScraper {
|
||||
}
|
||||
};
|
||||
|
||||
let mut last_processed = starting_height;
|
||||
info!(
|
||||
starting_height = starting_height,
|
||||
end_height = end_height,
|
||||
"attempting to process block range"
|
||||
);
|
||||
|
||||
while last_processed < current_height {
|
||||
info!(
|
||||
starting_height = starting_height,
|
||||
end_height = end_height,
|
||||
"attempting to process block range"
|
||||
);
|
||||
let range = (starting_height..=end_height).collect::<Vec<_>>();
|
||||
|
||||
let range = (starting_height..=end_height).collect::<Vec<_>>();
|
||||
|
||||
// the most likely bottleneck here are going to be the chain queries,
|
||||
// so batch multiple requests
|
||||
for batch in range.chunks(4) {
|
||||
let batch_result = join_all(
|
||||
batch
|
||||
.iter()
|
||||
.map(|height| self.rpc_client.get_basic_block_details(*height)),
|
||||
)
|
||||
.await;
|
||||
for result in batch_result {
|
||||
match result {
|
||||
Ok(block) => block_processor.process_block(block.into()).await?,
|
||||
Err(err) => {
|
||||
error!("failed to retrieve the block: {err}. stopping...");
|
||||
return Err(err);
|
||||
}
|
||||
// the most likely bottleneck here are going to be the chain queries,
|
||||
// so batch multiple requests
|
||||
for batch in range.chunks(4) {
|
||||
let batch_result = join_all(
|
||||
batch
|
||||
.iter()
|
||||
.map(|height| self.rpc_client.get_basic_block_details(*height)),
|
||||
)
|
||||
.await;
|
||||
for result in batch_result {
|
||||
match result {
|
||||
Ok(block) => block_processor.process_block(block.into()).await?,
|
||||
Err(err) => {
|
||||
error!("failed to retrieve the block: {err}. stopping...");
|
||||
return Err(err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// if we don't need to catch up, return early
|
||||
if !must_catch_up {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
// check if we have caught up to the current block height
|
||||
last_processed = end_height;
|
||||
current_height = self.rpc_client.current_block_height().await? as u32;
|
||||
|
||||
info!(
|
||||
last_processed = last_processed,
|
||||
current_height = current_height,
|
||||
"🏃 still need to catch up..."
|
||||
);
|
||||
|
||||
starting_height = last_processed + 1;
|
||||
end_height = current_height;
|
||||
}
|
||||
|
||||
if must_catch_up {
|
||||
info!(
|
||||
last_processed = last_processed,
|
||||
current_height = current_height,
|
||||
"✅ block processing has caught up!"
|
||||
);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
@@ -337,15 +275,8 @@ impl NyxdScraper {
|
||||
req_tx: Sender<BlockRequest>,
|
||||
processing_rx: UnboundedReceiver<BlockToProcess>,
|
||||
) -> Result<BlockProcessor, ScraperError> {
|
||||
let block_processor_config = BlockProcessorConfig::new(
|
||||
self.config.pruning_options,
|
||||
self.config.store_precommits,
|
||||
self.config.start_block.start_block_height,
|
||||
self.config.start_block.use_best_effort_start_height,
|
||||
);
|
||||
|
||||
BlockProcessor::new(
|
||||
block_processor_config,
|
||||
self.config.pruning_options,
|
||||
self.cancel_token.clone(),
|
||||
self.startup_sync.clone(),
|
||||
processing_rx,
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
|
||||
use crate::block_processor::types::BlockToProcess;
|
||||
use crate::error::ScraperError;
|
||||
use tendermint_rpc::client::CompatMode;
|
||||
use tendermint_rpc::event::Event;
|
||||
use tendermint_rpc::query::EventType;
|
||||
use tendermint_rpc::{SubscriptionClient, WebSocketClient, WebSocketClientDriver};
|
||||
@@ -39,16 +38,7 @@ impl ChainSubscriber {
|
||||
) -> Result<Self, ScraperError> {
|
||||
// sure, we could have just used websocket client entirely, but let's keep the logic for
|
||||
// getting current blocks and historical blocks completely separate with the dual connection
|
||||
let websocket_url = websocket_endpoint.as_str().try_into().map_err(|source| {
|
||||
ScraperError::WebSocketConnectionFailure {
|
||||
url: websocket_endpoint.to_string(),
|
||||
source,
|
||||
}
|
||||
})?;
|
||||
|
||||
let (client, driver) = WebSocketClient::builder(websocket_url)
|
||||
.compat_mode(CompatMode::V0_37)
|
||||
.build()
|
||||
let (client, driver) = WebSocketClient::new(websocket_endpoint.as_str())
|
||||
.await
|
||||
.map_err(|source| ScraperError::WebSocketConnectionFailure {
|
||||
url: websocket_endpoint.to_string(),
|
||||
|
||||
@@ -54,12 +54,6 @@ pub(crate) fn log_db_operation_time(op_name: &str, start_time: Instant) {
|
||||
impl ScraperStorage {
|
||||
#[instrument]
|
||||
pub async fn init<P: AsRef<Path> + Debug>(database_path: P) -> Result<Self, ScraperError> {
|
||||
let database_path = database_path.as_ref();
|
||||
debug!(
|
||||
"initialising scraper database path to '{}'",
|
||||
database_path.display()
|
||||
);
|
||||
|
||||
let opts = sqlx::sqlite::SqliteConnectOptions::new()
|
||||
.journal_mode(sqlx::sqlite::SqliteJournalMode::Wal)
|
||||
.synchronous(SqliteSynchronous::Normal)
|
||||
@@ -225,7 +219,6 @@ impl ScraperStorage {
|
||||
pub async fn persist_block(
|
||||
block: &FullBlockInformation,
|
||||
tx: &mut StorageTransaction,
|
||||
store_precommits: bool,
|
||||
) -> Result<(), ScraperError> {
|
||||
let total_gas = crate::helpers::tx_gas_sum(&block.transactions);
|
||||
|
||||
@@ -238,12 +231,11 @@ pub async fn persist_block(
|
||||
// persist block data
|
||||
persist_block_data(&block.block, total_gas, tx).await?;
|
||||
|
||||
if store_precommits {
|
||||
if let Some(commit) = &block.block.last_commit {
|
||||
persist_commits(commit, &block.validators, tx).await?;
|
||||
} else {
|
||||
warn!("no commits for block {}", block.block.header.height)
|
||||
}
|
||||
// persist commits
|
||||
if let Some(commit) = &block.block.last_commit {
|
||||
persist_commits(commit, &block.validators, tx).await?;
|
||||
} else {
|
||||
warn!("no commits for block {}", block.block.header.height)
|
||||
}
|
||||
|
||||
// persist txs
|
||||
|
||||
@@ -12,13 +12,15 @@ documentation = { workspace = true }
|
||||
|
||||
[dependencies]
|
||||
async-trait = { workspace = true, optional = true }
|
||||
tracing = { workspace = true }
|
||||
bs58 = { workspace = true }
|
||||
log = { workspace = true }
|
||||
rand = { workspace = true }
|
||||
reqwest = { workspace = true, features = ["json"] }
|
||||
serde = { workspace = true, features = ["derive"] }
|
||||
semver = { workspace = true }
|
||||
thiserror = { workspace = true }
|
||||
|
||||
# 'serde' feature
|
||||
# 'serializable' feature
|
||||
serde = { workspace = true, features = ["derive"], optional = true }
|
||||
serde_json = { workspace = true, optional = true }
|
||||
|
||||
# 'wasm-serde-types' feature
|
||||
@@ -26,6 +28,7 @@ tsify = { workspace = true, features = ["js"], optional = true }
|
||||
wasm-bindgen = { workspace = true, optional = true }
|
||||
|
||||
## internal
|
||||
nym-bin-common = { path = "../bin-common" }
|
||||
nym-config = { path = "../config" }
|
||||
nym-crypto = { path = "../crypto", features = ["sphinx", "outfox"] }
|
||||
nym-mixnet-contract-common = { path = "../cosmwasm-smart-contracts/mixnet-contract" }
|
||||
@@ -48,5 +51,5 @@ wasm-utils = { path = "../wasm/utils", default-features = false, optional = true
|
||||
default = ["provider-trait"]
|
||||
provider-trait = ["async-trait"]
|
||||
wasm-serde-types = ["tsify", "wasm-bindgen", "wasm-utils"]
|
||||
persistence = ["serde_json"]
|
||||
serializable = ["serde", "serde_json"]
|
||||
outfox = []
|
||||
|
||||
@@ -4,28 +4,18 @@
|
||||
use std::array::TryFromSliceError;
|
||||
|
||||
use crate::MixLayer;
|
||||
use nym_sphinx_addressing::NodeIdentity;
|
||||
use nym_sphinx_types::NymPacketError;
|
||||
use thiserror::Error;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum NymTopologyError {
|
||||
#[error("the provided network topology is empty - there are no valid nodes on it - the network request(s) probably failed")]
|
||||
#[error("The provided network topology is empty - there are no mixnodes and no gateways on it - the network request(s) probably failed")]
|
||||
EmptyNetworkTopology,
|
||||
|
||||
#[error("no node with identity {node_identity} is known")]
|
||||
NonExistentNode { node_identity: Box<NodeIdentity> },
|
||||
|
||||
#[error("could not use node with identity {node_identity} as egress since it didn't get assigned valid role in the current epoch")]
|
||||
InvalidEgressRole { node_identity: Box<NodeIdentity> },
|
||||
|
||||
#[error("one (or more) of mixing layers does not have any valid nodes available")]
|
||||
InsufficientMixingNodes,
|
||||
|
||||
#[error("The provided network topology has no gateways available")]
|
||||
NoGatewaysAvailable,
|
||||
|
||||
#[error("The provided network topology has no valid mixnodes available")]
|
||||
#[error("The provided network topology has no mixnodes available")]
|
||||
NoMixnodesAvailable,
|
||||
|
||||
#[error("Gateway with identity key {identity_key} doesn't exist")]
|
||||
@@ -65,6 +55,12 @@ pub enum NymTopologyError {
|
||||
#[error("{0}")]
|
||||
ReqwestError(#[from] reqwest::Error),
|
||||
|
||||
#[error("{0}")]
|
||||
MixnodeConversionError(#[from] crate::mix::MixnodeConversionError),
|
||||
|
||||
#[error("{0}")]
|
||||
GatewayConversionError(#[from] crate::gateway::GatewayConversionError),
|
||||
|
||||
#[error("{0}")]
|
||||
VarError(#[from] std::env::VarError),
|
||||
}
|
||||
|
||||
@@ -0,0 +1,174 @@
|
||||
// Copyright 2021 - Nym Technologies SA <contact@nymtech.net>
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
use crate::{NetworkAddress, NodeVersion};
|
||||
use nym_api_requests::nym_nodes::SkimmedNode;
|
||||
use nym_crypto::asymmetric::{encryption, identity};
|
||||
use nym_mixnet_contract_common::NodeId;
|
||||
use nym_sphinx_addressing::nodes::{NodeIdentity, NymNodeRoutingAddress};
|
||||
use nym_sphinx_types::Node as SphinxNode;
|
||||
use rand::seq::SliceRandom;
|
||||
use rand::thread_rng;
|
||||
use std::fmt;
|
||||
use std::fmt::Formatter;
|
||||
use std::io;
|
||||
use std::net::AddrParseError;
|
||||
use std::net::SocketAddr;
|
||||
use thiserror::Error;
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum GatewayConversionError {
|
||||
#[error("gateway identity key was malformed - {0}")]
|
||||
InvalidIdentityKey(#[from] identity::Ed25519RecoveryError),
|
||||
|
||||
#[error("gateway sphinx key was malformed - {0}")]
|
||||
InvalidSphinxKey(#[from] encryption::KeyRecoveryError),
|
||||
|
||||
#[error("'{value}' is not a valid gateway address - {source}")]
|
||||
InvalidAddress {
|
||||
value: String,
|
||||
#[source]
|
||||
source: io::Error,
|
||||
},
|
||||
|
||||
#[error("'{gateway}' has not provided any valid ip addresses")]
|
||||
NoIpAddressesProvided { gateway: String },
|
||||
|
||||
#[error("'{gateway}' has provided a malformed ip address: {err}")]
|
||||
MalformedIpAddress {
|
||||
gateway: String,
|
||||
|
||||
#[source]
|
||||
err: AddrParseError,
|
||||
},
|
||||
|
||||
#[error("provided node is not an entry gateway in this epoch!")]
|
||||
NotGateway,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct LegacyNode {
|
||||
pub node_id: NodeId,
|
||||
|
||||
pub host: NetworkAddress,
|
||||
// we're keeping this as separate resolved field since we do not want to be resolving the potential
|
||||
// hostname every time we want to construct a path via this node
|
||||
pub mix_host: SocketAddr,
|
||||
|
||||
// #[serde(alias = "clients_port")]
|
||||
pub clients_ws_port: u16,
|
||||
|
||||
// #[serde(default)]
|
||||
pub clients_wss_port: Option<u16>,
|
||||
|
||||
pub identity_key: identity::PublicKey,
|
||||
pub sphinx_key: encryption::PublicKey, // TODO: or nymsphinx::PublicKey? both are x25519
|
||||
|
||||
// to be removed:
|
||||
pub version: NodeVersion,
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for LegacyNode {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
||||
f.debug_struct("gateway::Node")
|
||||
.field("host", &self.host)
|
||||
.field("mix_host", &self.mix_host)
|
||||
.field("clients_ws_port", &self.clients_ws_port)
|
||||
.field("clients_wss_port", &self.clients_wss_port)
|
||||
.field("identity_key", &self.identity_key.to_base58_string())
|
||||
.field("sphinx_key", &self.sphinx_key.to_base58_string())
|
||||
.field("version", &self.version)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl LegacyNode {
|
||||
pub fn parse_host(raw: &str) -> Result<NetworkAddress, GatewayConversionError> {
|
||||
// safety: this conversion is infallible
|
||||
// (but we retain result return type for legacy reasons)
|
||||
Ok(raw.parse().unwrap())
|
||||
}
|
||||
|
||||
pub fn extract_mix_host(
|
||||
host: &NetworkAddress,
|
||||
mix_port: u16,
|
||||
) -> Result<SocketAddr, GatewayConversionError> {
|
||||
Ok(host.to_socket_addrs(mix_port).map_err(|err| {
|
||||
GatewayConversionError::InvalidAddress {
|
||||
value: host.to_string(),
|
||||
source: err,
|
||||
}
|
||||
})?[0])
|
||||
}
|
||||
|
||||
pub fn identity(&self) -> &NodeIdentity {
|
||||
&self.identity_key
|
||||
}
|
||||
|
||||
pub fn clients_address(&self) -> String {
|
||||
self.clients_address_tls()
|
||||
.unwrap_or_else(|| self.clients_address_no_tls())
|
||||
}
|
||||
|
||||
pub fn clients_address_no_tls(&self) -> String {
|
||||
format!("ws://{}:{}", self.host, self.clients_ws_port)
|
||||
}
|
||||
|
||||
pub fn clients_address_tls(&self) -> Option<String> {
|
||||
self.clients_wss_port
|
||||
.map(|p| format!("wss://{}:{p}", self.host))
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for LegacyNode {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "legacy gateway {} @ {}", self.node_id, self.host)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> From<&'a LegacyNode> for SphinxNode {
|
||||
fn from(node: &'a LegacyNode) -> Self {
|
||||
let node_address_bytes = NymNodeRoutingAddress::from(node.mix_host)
|
||||
.try_into()
|
||||
.unwrap();
|
||||
|
||||
SphinxNode::new(node_address_bytes, (&node.sphinx_key).into())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> TryFrom<&'a SkimmedNode> for LegacyNode {
|
||||
type Error = GatewayConversionError;
|
||||
|
||||
fn try_from(value: &'a SkimmedNode) -> Result<Self, Self::Error> {
|
||||
let Some(entry_details) = &value.entry else {
|
||||
return Err(GatewayConversionError::NotGateway);
|
||||
};
|
||||
|
||||
if value.ip_addresses.is_empty() {
|
||||
return Err(GatewayConversionError::NoIpAddressesProvided {
|
||||
gateway: value.ed25519_identity_pubkey.to_base58_string(),
|
||||
});
|
||||
}
|
||||
|
||||
// safety: we just checked the slice is not empty
|
||||
#[allow(clippy::unwrap_used)]
|
||||
let ip = value.ip_addresses.choose(&mut thread_rng()).unwrap();
|
||||
|
||||
let host = if let Some(hostname) = &entry_details.hostname {
|
||||
NetworkAddress::Hostname(hostname.to_string())
|
||||
} else {
|
||||
NetworkAddress::IpAddr(*ip)
|
||||
};
|
||||
|
||||
Ok(LegacyNode {
|
||||
node_id: value.node_id,
|
||||
host,
|
||||
mix_host: SocketAddr::new(*ip, value.mix_port),
|
||||
clients_ws_port: entry_details.ws_port,
|
||||
clients_wss_port: entry_details.wss_port,
|
||||
identity_key: value.ed25519_identity_pubkey,
|
||||
sphinx_key: value.x25519_sphinx_pubkey,
|
||||
version: NodeVersion::Unknown,
|
||||
})
|
||||
}
|
||||
}
|
||||
+486
-448
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,135 @@
|
||||
// Copyright 2021 - Nym Technologies SA <contact@nymtech.net>
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
use crate::{NetworkAddress, NodeVersion};
|
||||
use nym_api_requests::nym_nodes::{NodeRole, SkimmedNode};
|
||||
use nym_crypto::asymmetric::{encryption, identity};
|
||||
pub use nym_mixnet_contract_common::LegacyMixLayer;
|
||||
use nym_mixnet_contract_common::NodeId;
|
||||
use nym_sphinx_addressing::nodes::NymNodeRoutingAddress;
|
||||
use nym_sphinx_types::Node as SphinxNode;
|
||||
use rand::seq::SliceRandom;
|
||||
use rand::thread_rng;
|
||||
use std::fmt::Formatter;
|
||||
use std::io;
|
||||
use std::net::SocketAddr;
|
||||
use thiserror::Error;
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum MixnodeConversionError {
|
||||
#[error("mixnode identity key was malformed - {0}")]
|
||||
InvalidIdentityKey(#[from] identity::Ed25519RecoveryError),
|
||||
|
||||
#[error("mixnode sphinx key was malformed - {0}")]
|
||||
InvalidSphinxKey(#[from] encryption::KeyRecoveryError),
|
||||
|
||||
#[error("'{value}' is not a valid mixnode address - {source}")]
|
||||
InvalidAddress {
|
||||
value: String,
|
||||
#[source]
|
||||
source: io::Error,
|
||||
},
|
||||
|
||||
#[error("invalid mix layer")]
|
||||
InvalidLayer,
|
||||
|
||||
#[error("'{mixnode}' has not provided any valid ip addresses")]
|
||||
NoIpAddressesProvided { mixnode: String },
|
||||
|
||||
#[error("provided node is not a mixnode in this epoch!")]
|
||||
NotMixnode,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct LegacyNode {
|
||||
pub mix_id: NodeId,
|
||||
pub host: NetworkAddress,
|
||||
// we're keeping this as separate resolved field since we do not want to be resolving the potential
|
||||
// hostname every time we want to construct a path via this node
|
||||
pub mix_host: SocketAddr,
|
||||
pub identity_key: identity::PublicKey,
|
||||
pub sphinx_key: encryption::PublicKey, // TODO: or nymsphinx::PublicKey? both are x25519
|
||||
pub layer: LegacyMixLayer,
|
||||
|
||||
// to be removed:
|
||||
pub version: NodeVersion,
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for LegacyNode {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_struct("mix::Node")
|
||||
.field("mix_id", &self.mix_id)
|
||||
.field("host", &self.host)
|
||||
.field("mix_host", &self.mix_host)
|
||||
.field("identity_key", &self.identity_key.to_base58_string())
|
||||
.field("sphinx_key", &self.sphinx_key.to_base58_string())
|
||||
.field("layer", &self.layer)
|
||||
.field("version", &self.version)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl LegacyNode {
|
||||
pub fn parse_host(raw: &str) -> Result<NetworkAddress, MixnodeConversionError> {
|
||||
// safety: this conversion is infallible
|
||||
// (but we retain result return type for legacy reasons)
|
||||
Ok(raw.parse().unwrap())
|
||||
}
|
||||
|
||||
pub fn extract_mix_host(
|
||||
host: &NetworkAddress,
|
||||
mix_port: u16,
|
||||
) -> Result<SocketAddr, MixnodeConversionError> {
|
||||
Ok(host.to_socket_addrs(mix_port).map_err(|err| {
|
||||
MixnodeConversionError::InvalidAddress {
|
||||
value: host.to_string(),
|
||||
source: err,
|
||||
}
|
||||
})?[0])
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> From<&'a LegacyNode> for SphinxNode {
|
||||
fn from(node: &'a LegacyNode) -> Self {
|
||||
let node_address_bytes = NymNodeRoutingAddress::from(node.mix_host)
|
||||
.try_into()
|
||||
.unwrap();
|
||||
|
||||
SphinxNode::new(node_address_bytes, (&node.sphinx_key).into())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> TryFrom<&'a SkimmedNode> for LegacyNode {
|
||||
type Error = MixnodeConversionError;
|
||||
|
||||
fn try_from(value: &'a SkimmedNode) -> Result<Self, Self::Error> {
|
||||
if value.ip_addresses.is_empty() {
|
||||
return Err(MixnodeConversionError::NoIpAddressesProvided {
|
||||
mixnode: value.ed25519_identity_pubkey.to_base58_string(),
|
||||
});
|
||||
}
|
||||
|
||||
let layer = match value.role {
|
||||
NodeRole::Mixnode { layer } => layer
|
||||
.try_into()
|
||||
.map_err(|_| MixnodeConversionError::InvalidLayer)?,
|
||||
_ => return Err(MixnodeConversionError::NotMixnode),
|
||||
};
|
||||
|
||||
// safety: we just checked the slice is not empty
|
||||
#[allow(clippy::unwrap_used)]
|
||||
let ip = value.ip_addresses.choose(&mut thread_rng()).unwrap();
|
||||
|
||||
let host = NetworkAddress::IpAddr(*ip);
|
||||
|
||||
Ok(LegacyNode {
|
||||
mix_id: value.node_id,
|
||||
host,
|
||||
mix_host: SocketAddr::new(*ip, value.mix_port),
|
||||
identity_key: value.ed25519_identity_pubkey,
|
||||
sphinx_key: value.x25519_sphinx_pubkey,
|
||||
layer,
|
||||
version: NodeVersion::Unknown,
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,143 +0,0 @@
|
||||
// Copyright 2024 - Nym Technologies SA <contact@nymtech.net>
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
use nym_api_requests::models::DeclaredRoles;
|
||||
use nym_api_requests::nym_nodes::SkimmedNode;
|
||||
use nym_crypto::asymmetric::{ed25519, x25519};
|
||||
use nym_mixnet_contract_common::NodeId;
|
||||
use nym_sphinx_addressing::nodes::NymNodeRoutingAddress;
|
||||
use nym_sphinx_types::Node as SphinxNode;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::net::{IpAddr, SocketAddr};
|
||||
use thiserror::Error;
|
||||
|
||||
pub use nym_mixnet_contract_common::LegacyMixLayer;
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum RoutingNodeError {
|
||||
#[error("node {node_id} ('{identity}') has not provided any valid ip addresses")]
|
||||
NoIpAddressesProvided { node_id: NodeId, identity: String },
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
pub struct EntryDetails {
|
||||
// to allow client to choose ipv6 preference, if available
|
||||
pub ip_addresses: Vec<IpAddr>,
|
||||
pub clients_ws_port: u16,
|
||||
pub hostname: Option<String>,
|
||||
pub clients_wss_port: Option<u16>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
|
||||
pub struct SupportedRoles {
|
||||
pub mixnode: bool,
|
||||
pub mixnet_entry: bool,
|
||||
pub mixnet_exit: bool,
|
||||
}
|
||||
|
||||
impl From<DeclaredRoles> for SupportedRoles {
|
||||
fn from(value: DeclaredRoles) -> Self {
|
||||
SupportedRoles {
|
||||
mixnode: value.mixnode,
|
||||
mixnet_entry: value.entry,
|
||||
mixnet_exit: value.exit_nr && value.exit_ipr,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
pub struct RoutingNode {
|
||||
pub node_id: NodeId,
|
||||
|
||||
pub mix_host: SocketAddr,
|
||||
|
||||
pub entry: Option<EntryDetails>,
|
||||
pub identity_key: ed25519::PublicKey,
|
||||
pub sphinx_key: x25519::PublicKey,
|
||||
|
||||
pub supported_roles: SupportedRoles,
|
||||
}
|
||||
|
||||
impl RoutingNode {
|
||||
pub fn ws_entry_address_tls(&self) -> Option<String> {
|
||||
let entry = self.entry.as_ref()?;
|
||||
let hostname = entry.hostname.as_ref()?;
|
||||
let wss_port = entry.clients_wss_port?;
|
||||
|
||||
Some(format!("wss://{hostname}:{wss_port}"))
|
||||
}
|
||||
|
||||
pub fn ws_entry_address_no_tls(&self, prefer_ipv6: bool) -> Option<String> {
|
||||
let entry = self.entry.as_ref()?;
|
||||
|
||||
if let Some(hostname) = entry.hostname.as_ref() {
|
||||
return Some(format!("ws://{hostname}:{}", entry.clients_ws_port));
|
||||
}
|
||||
|
||||
if prefer_ipv6 {
|
||||
if let Some(ipv6) = entry.ip_addresses.iter().find(|ip| ip.is_ipv6()) {
|
||||
return Some(format!("ws://{ipv6}:{}", entry.clients_ws_port));
|
||||
}
|
||||
}
|
||||
|
||||
let any_ip = entry.ip_addresses.first()?;
|
||||
Some(format!("ws://{any_ip}:{}", entry.clients_ws_port))
|
||||
}
|
||||
|
||||
pub fn ws_entry_address(&self, prefer_ipv6: bool) -> Option<String> {
|
||||
if let Some(tls) = self.ws_entry_address_tls() {
|
||||
return Some(tls);
|
||||
}
|
||||
self.ws_entry_address_no_tls(prefer_ipv6)
|
||||
}
|
||||
|
||||
pub fn identity(&self) -> ed25519::PublicKey {
|
||||
self.identity_key
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> From<&'a RoutingNode> for SphinxNode {
|
||||
fn from(node: &'a RoutingNode) -> Self {
|
||||
// SAFETY: this conversion is infallible as all versions of socket addresses have
|
||||
// sufficiently small bytes representation to fit inside `NodeAddressBytes`
|
||||
#[allow(clippy::unwrap_used)]
|
||||
let node_address_bytes = NymNodeRoutingAddress::from(node.mix_host)
|
||||
.try_into()
|
||||
.unwrap();
|
||||
|
||||
SphinxNode::new(node_address_bytes, (&node.sphinx_key).into())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> TryFrom<&'a SkimmedNode> for RoutingNode {
|
||||
type Error = RoutingNodeError;
|
||||
|
||||
fn try_from(value: &'a SkimmedNode) -> Result<Self, Self::Error> {
|
||||
// IF YOU EVER ADD "performance" TO RoutingNode,
|
||||
// MAKE SURE TO UPDATE THE LAZY IMPLEMENTATION OF
|
||||
// `impl NodeDescriptionTopologyExt for NymNodeDescription`!!!
|
||||
|
||||
let Some(first_ip) = value.ip_addresses.first() else {
|
||||
return Err(RoutingNodeError::NoIpAddressesProvided {
|
||||
node_id: value.node_id,
|
||||
identity: value.ed25519_identity_pubkey.to_string(),
|
||||
});
|
||||
};
|
||||
|
||||
let entry = value.entry.as_ref().map(|entry| EntryDetails {
|
||||
ip_addresses: value.ip_addresses.clone(),
|
||||
clients_ws_port: entry.ws_port,
|
||||
hostname: entry.hostname.clone(),
|
||||
clients_wss_port: entry.wss_port,
|
||||
});
|
||||
|
||||
Ok(RoutingNode {
|
||||
node_id: value.node_id,
|
||||
mix_host: SocketAddr::new(*first_ip, value.mix_port),
|
||||
entry,
|
||||
identity_key: value.ed25519_identity_pubkey,
|
||||
sphinx_key: value.x25519_sphinx_pubkey,
|
||||
supported_roles: value.supported_roles.into(),
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -22,7 +22,7 @@ pub struct HardcodedTopologyProvider {
|
||||
}
|
||||
|
||||
impl HardcodedTopologyProvider {
|
||||
#[cfg(feature = "persistence")]
|
||||
#[cfg(feature = "serializable")]
|
||||
pub fn new_from_file<P: AsRef<std::path::Path>>(path: P) -> std::io::Result<Self> {
|
||||
NymTopology::new_from_file(path).map(Self::new)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
// Copyright 2023 - Nym Technologies SA <contact@nymtech.net>
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
use crate::{NymTopology, NymTopologyError};
|
||||
use nym_sphinx_addressing::clients::Recipient;
|
||||
use nym_sphinx_routing::SphinxRouteMaker;
|
||||
use nym_sphinx_types::Node;
|
||||
use rand::{CryptoRng, Rng};
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub struct NymTopologyRouteProvider<R> {
|
||||
rng: R,
|
||||
inner: NymTopology,
|
||||
}
|
||||
|
||||
impl<R> SphinxRouteMaker for NymTopologyRouteProvider<R>
|
||||
where
|
||||
R: Rng + CryptoRng,
|
||||
{
|
||||
type Error = NymTopologyError;
|
||||
|
||||
fn sphinx_route(
|
||||
&mut self,
|
||||
hops: u8,
|
||||
destination: &Recipient,
|
||||
) -> Result<Vec<Node>, NymTopologyError> {
|
||||
self.inner
|
||||
.random_route_to_gateway(&mut self.rng, hops, destination.gateway())
|
||||
}
|
||||
}
|
||||
@@ -1,122 +0,0 @@
|
||||
// Copyright 2024 - Nym Technologies SA <contact@nymtech.net>
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
use nym_mixnet_contract_common::nym_node::Role;
|
||||
use nym_mixnet_contract_common::{EpochId, EpochRewardedSet, NodeId, RewardedSet};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashSet;
|
||||
|
||||
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
|
||||
pub struct CachedEpochRewardedSet {
|
||||
pub epoch_id: EpochId,
|
||||
|
||||
pub entry_gateways: HashSet<NodeId>,
|
||||
|
||||
pub exit_gateways: HashSet<NodeId>,
|
||||
|
||||
pub layer1: HashSet<NodeId>,
|
||||
|
||||
pub layer2: HashSet<NodeId>,
|
||||
|
||||
pub layer3: HashSet<NodeId>,
|
||||
|
||||
pub standby: HashSet<NodeId>,
|
||||
}
|
||||
|
||||
impl From<EpochRewardedSet> for CachedEpochRewardedSet {
|
||||
fn from(value: EpochRewardedSet) -> Self {
|
||||
CachedEpochRewardedSet {
|
||||
epoch_id: value.epoch_id,
|
||||
entry_gateways: value.assignment.entry_gateways.into_iter().collect(),
|
||||
exit_gateways: value.assignment.exit_gateways.into_iter().collect(),
|
||||
layer1: value.assignment.layer1.into_iter().collect(),
|
||||
layer2: value.assignment.layer2.into_iter().collect(),
|
||||
layer3: value.assignment.layer3.into_iter().collect(),
|
||||
standby: value.assignment.standby.into_iter().collect(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<CachedEpochRewardedSet> for EpochRewardedSet {
|
||||
fn from(value: CachedEpochRewardedSet) -> Self {
|
||||
EpochRewardedSet {
|
||||
epoch_id: value.epoch_id,
|
||||
assignment: RewardedSet {
|
||||
entry_gateways: value.entry_gateways.into_iter().collect(),
|
||||
exit_gateways: value.exit_gateways.into_iter().collect(),
|
||||
layer1: value.layer1.into_iter().collect(),
|
||||
layer2: value.layer2.into_iter().collect(),
|
||||
layer3: value.layer3.into_iter().collect(),
|
||||
standby: value.standby.into_iter().collect(),
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl CachedEpochRewardedSet {
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.entry_gateways.is_empty()
|
||||
&& self.exit_gateways.is_empty()
|
||||
&& self.layer1.is_empty()
|
||||
&& self.layer2.is_empty()
|
||||
&& self.layer3.is_empty()
|
||||
&& self.standby.is_empty()
|
||||
}
|
||||
|
||||
pub fn role(&self, node_id: NodeId) -> Option<Role> {
|
||||
if self.entry_gateways.contains(&node_id) {
|
||||
Some(Role::EntryGateway)
|
||||
} else if self.exit_gateways.contains(&node_id) {
|
||||
Some(Role::ExitGateway)
|
||||
} else if self.layer1.contains(&node_id) {
|
||||
Some(Role::Layer1)
|
||||
} else if self.layer2.contains(&node_id) {
|
||||
Some(Role::Layer2)
|
||||
} else if self.layer3.contains(&node_id) {
|
||||
Some(Role::Layer3)
|
||||
} else if self.standby.contains(&node_id) {
|
||||
Some(Role::Standby)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub fn legacy_mix_layer(&self, node_id: &NodeId) -> Option<u8> {
|
||||
if self.layer1.contains(node_id) {
|
||||
Some(1)
|
||||
} else if self.layer2.contains(node_id) {
|
||||
Some(2)
|
||||
} else if self.layer3.contains(node_id) {
|
||||
Some(3)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_standby(&self, node_id: &NodeId) -> bool {
|
||||
self.standby.contains(node_id)
|
||||
}
|
||||
|
||||
pub fn is_active_mixnode(&self, node_id: &NodeId) -> bool {
|
||||
self.layer1.contains(node_id)
|
||||
|| self.layer2.contains(node_id)
|
||||
|| self.layer3.contains(node_id)
|
||||
}
|
||||
|
||||
pub fn gateways(&self) -> HashSet<NodeId> {
|
||||
let mut gateways =
|
||||
HashSet::with_capacity(self.entry_gateways.len() + self.exit_gateways.len());
|
||||
gateways.extend(&self.entry_gateways);
|
||||
gateways.extend(&self.exit_gateways);
|
||||
gateways
|
||||
}
|
||||
|
||||
pub fn active_mixnodes(&self) -> HashSet<NodeId> {
|
||||
let mut mixnodes =
|
||||
HashSet::with_capacity(self.layer1.len() + self.layer2.len() + self.layer3.len());
|
||||
mixnodes.extend(&self.layer1);
|
||||
mixnodes.extend(&self.layer2);
|
||||
mixnodes.extend(&self.layer3);
|
||||
mixnodes
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,262 @@
|
||||
// Copyright 2023 - Nym Technologies SA <contact@nymtech.net>
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
#![allow(unknown_lints)]
|
||||
// clippy::empty_docs is not on stable as of 1.77
|
||||
|
||||
// due to the code generated by Tsify
|
||||
#![allow(clippy::empty_docs)]
|
||||
|
||||
use crate::gateway::GatewayConversionError;
|
||||
use crate::mix::MixnodeConversionError;
|
||||
use crate::{gateway, mix, MixLayer, NymTopology};
|
||||
use nym_config::defaults::{DEFAULT_CLIENT_LISTENING_PORT, DEFAULT_MIX_LISTENING_PORT};
|
||||
use nym_crypto::asymmetric::{encryption, identity};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::BTreeMap;
|
||||
use std::net::{IpAddr, SocketAddr};
|
||||
use thiserror::Error;
|
||||
|
||||
#[cfg(feature = "wasm-serde-types")]
|
||||
use tsify::Tsify;
|
||||
|
||||
use nym_mixnet_contract_common::NodeId;
|
||||
#[cfg(feature = "wasm-serde-types")]
|
||||
use wasm_bindgen::{prelude::wasm_bindgen, JsValue};
|
||||
|
||||
#[cfg(feature = "wasm-serde-types")]
|
||||
use wasm_utils::error::simple_js_error;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum SerializableTopologyError {
|
||||
#[error("got invalid mix layer {value}. Expected 1, 2 or 3.")]
|
||||
InvalidMixLayer { value: u8 },
|
||||
|
||||
#[error(transparent)]
|
||||
GatewayConversion(#[from] GatewayConversionError),
|
||||
|
||||
#[error(transparent)]
|
||||
MixnodeConversion(#[from] MixnodeConversionError),
|
||||
|
||||
#[error("The provided mixnode map was malformed: {msg}")]
|
||||
MalformedMixnodeMap { msg: String },
|
||||
|
||||
#[error("The provided gateway list was malformed: {msg}")]
|
||||
MalformedGatewayList { msg: String },
|
||||
}
|
||||
|
||||
#[cfg(feature = "wasm-serde-types")]
|
||||
impl From<SerializableTopologyError> for JsValue {
|
||||
fn from(value: SerializableTopologyError) -> Self {
|
||||
simple_js_error(value.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[cfg_attr(feature = "wasm-serde-types", derive(Tsify))]
|
||||
#[cfg_attr(feature = "wasm-serde-types", tsify(into_wasm_abi, from_wasm_abi))]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct SerializableNymTopology {
|
||||
pub mixnodes: BTreeMap<MixLayer, Vec<SerializableMixNode>>,
|
||||
pub gateways: Vec<SerializableGateway>,
|
||||
}
|
||||
|
||||
impl TryFrom<SerializableNymTopology> for NymTopology {
|
||||
type Error = SerializableTopologyError;
|
||||
|
||||
fn try_from(value: SerializableNymTopology) -> Result<Self, Self::Error> {
|
||||
let mut converted_mixes = BTreeMap::new();
|
||||
|
||||
for (layer, nodes) in value.mixnodes {
|
||||
let layer_nodes = nodes
|
||||
.into_iter()
|
||||
.map(TryInto::try_into)
|
||||
.collect::<Result<_, _>>()?;
|
||||
|
||||
converted_mixes.insert(layer, layer_nodes);
|
||||
}
|
||||
|
||||
let gateways = value
|
||||
.gateways
|
||||
.into_iter()
|
||||
.map(TryInto::try_into)
|
||||
.collect::<Result<_, _>>()?;
|
||||
|
||||
Ok(NymTopology::new(converted_mixes, gateways))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<NymTopology> for SerializableNymTopology {
|
||||
fn from(value: NymTopology) -> Self {
|
||||
SerializableNymTopology {
|
||||
mixnodes: value
|
||||
.mixes()
|
||||
.iter()
|
||||
.map(|(&l, nodes)| (l, nodes.iter().map(Into::into).collect()))
|
||||
.collect(),
|
||||
gateways: value.gateways().iter().map(Into::into).collect(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[cfg_attr(feature = "wasm-serde-types", derive(Tsify))]
|
||||
#[cfg_attr(feature = "wasm-serde-types", tsify(into_wasm_abi, from_wasm_abi))]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct SerializableMixNode {
|
||||
// this is a `MixId` but due to typescript issue, we're using u32 directly.
|
||||
#[serde(alias = "mix_id")]
|
||||
pub mix_id: u32,
|
||||
|
||||
pub host: String,
|
||||
|
||||
#[cfg_attr(feature = "wasm-serde-types", tsify(optional))]
|
||||
#[serde(alias = "mix_port")]
|
||||
pub mix_port: Option<u16>,
|
||||
|
||||
#[serde(alias = "identity_key")]
|
||||
pub identity_key: String,
|
||||
|
||||
#[serde(alias = "sphinx_key")]
|
||||
pub sphinx_key: String,
|
||||
|
||||
// this is a `MixLayer` but due to typescript issue, we're using u8 directly.
|
||||
pub layer: u8,
|
||||
|
||||
#[cfg_attr(feature = "wasm-serde-types", tsify(optional))]
|
||||
pub version: Option<String>,
|
||||
}
|
||||
|
||||
impl TryFrom<SerializableMixNode> for mix::LegacyNode {
|
||||
type Error = SerializableTopologyError;
|
||||
|
||||
fn try_from(value: SerializableMixNode) -> Result<Self, Self::Error> {
|
||||
let host = mix::LegacyNode::parse_host(&value.host)?;
|
||||
|
||||
let mix_port = value.mix_port.unwrap_or(DEFAULT_MIX_LISTENING_PORT);
|
||||
let version = value.version.map(|v| v.as_str().into()).unwrap_or_default();
|
||||
|
||||
// try to completely resolve the host in the mix situation to avoid doing it every
|
||||
// single time we want to construct a path
|
||||
let mix_host = mix::LegacyNode::extract_mix_host(&host, mix_port)?;
|
||||
|
||||
Ok(mix::LegacyNode {
|
||||
mix_id: value.mix_id,
|
||||
host,
|
||||
mix_host,
|
||||
identity_key: identity::PublicKey::from_base58_string(&value.identity_key)
|
||||
.map_err(MixnodeConversionError::from)?,
|
||||
sphinx_key: encryption::PublicKey::from_base58_string(&value.sphinx_key)
|
||||
.map_err(MixnodeConversionError::from)?,
|
||||
layer: mix::LegacyMixLayer::try_from(value.layer)
|
||||
.map_err(|_| SerializableTopologyError::InvalidMixLayer { value: value.layer })?,
|
||||
version,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> From<&'a mix::LegacyNode> for SerializableMixNode {
|
||||
fn from(value: &'a mix::LegacyNode) -> Self {
|
||||
SerializableMixNode {
|
||||
mix_id: value.mix_id,
|
||||
host: value.host.to_string(),
|
||||
mix_port: Some(value.mix_host.port()),
|
||||
identity_key: value.identity_key.to_base58_string(),
|
||||
sphinx_key: value.sphinx_key.to_base58_string(),
|
||||
layer: value.layer.into(),
|
||||
version: Some(value.version.to_string()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[cfg_attr(feature = "wasm-serde-types", derive(Tsify))]
|
||||
#[cfg_attr(feature = "wasm-serde-types", tsify(into_wasm_abi, from_wasm_abi))]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct SerializableGateway {
|
||||
pub host: String,
|
||||
|
||||
pub node_id: NodeId,
|
||||
|
||||
// optional ip address in the case of host being a hostname that can't be resolved
|
||||
// (thank you wasm)
|
||||
#[cfg_attr(feature = "wasm-serde-types", tsify(optional))]
|
||||
#[serde(alias = "explicit_ip")]
|
||||
pub explicit_ip: Option<IpAddr>,
|
||||
|
||||
#[cfg_attr(feature = "wasm-serde-types", tsify(optional))]
|
||||
#[serde(alias = "mix_port")]
|
||||
pub mix_port: Option<u16>,
|
||||
|
||||
#[cfg_attr(feature = "wasm-serde-types", tsify(optional))]
|
||||
#[serde(alias = "clients_port")]
|
||||
#[serde(alias = "clients_ws_port")]
|
||||
pub clients_ws_port: Option<u16>,
|
||||
|
||||
#[cfg_attr(feature = "wasm-serde-types", tsify(optional))]
|
||||
#[serde(alias = "clients_wss_port")]
|
||||
pub clients_wss_port: Option<u16>,
|
||||
|
||||
#[serde(alias = "identity_key")]
|
||||
pub identity_key: String,
|
||||
|
||||
#[serde(alias = "sphinx_key")]
|
||||
pub sphinx_key: String,
|
||||
|
||||
#[cfg_attr(feature = "wasm-serde-types", tsify(optional))]
|
||||
pub version: Option<String>,
|
||||
}
|
||||
|
||||
impl TryFrom<SerializableGateway> for gateway::LegacyNode {
|
||||
type Error = SerializableTopologyError;
|
||||
|
||||
fn try_from(value: SerializableGateway) -> Result<Self, Self::Error> {
|
||||
let host = gateway::LegacyNode::parse_host(&value.host)?;
|
||||
|
||||
let mix_port = value.mix_port.unwrap_or(DEFAULT_MIX_LISTENING_PORT);
|
||||
let clients_ws_port = value
|
||||
.clients_ws_port
|
||||
.unwrap_or(DEFAULT_CLIENT_LISTENING_PORT);
|
||||
let version = value.version.map(|v| v.as_str().into()).unwrap_or_default();
|
||||
|
||||
// try to completely resolve the host in the mix situation to avoid doing it every
|
||||
// single time we want to construct a path
|
||||
let mix_host = if let Some(explicit_ip) = value.explicit_ip {
|
||||
SocketAddr::new(explicit_ip, mix_port)
|
||||
} else {
|
||||
gateway::LegacyNode::extract_mix_host(&host, mix_port)?
|
||||
};
|
||||
|
||||
Ok(gateway::LegacyNode {
|
||||
node_id: value.node_id,
|
||||
host,
|
||||
mix_host,
|
||||
clients_ws_port,
|
||||
clients_wss_port: value.clients_wss_port,
|
||||
identity_key: identity::PublicKey::from_base58_string(&value.identity_key)
|
||||
.map_err(GatewayConversionError::from)?,
|
||||
sphinx_key: encryption::PublicKey::from_base58_string(&value.sphinx_key)
|
||||
.map_err(GatewayConversionError::from)?,
|
||||
version,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> From<&'a gateway::LegacyNode> for SerializableGateway {
|
||||
fn from(value: &'a gateway::LegacyNode) -> Self {
|
||||
SerializableGateway {
|
||||
host: value.host.to_string(),
|
||||
node_id: value.node_id,
|
||||
explicit_ip: Some(value.mix_host.ip()),
|
||||
mix_port: Some(value.mix_host.port()),
|
||||
clients_ws_port: Some(value.clients_ws_port),
|
||||
clients_wss_port: value.clients_wss_port,
|
||||
identity_key: value.identity_key.to_base58_string(),
|
||||
sphinx_key: value.sphinx_key.to_base58_string(),
|
||||
version: Some(value.version.to_string()),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,123 +0,0 @@
|
||||
// Copyright 2023 - Nym Technologies SA <contact@nymtech.net>
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// due to the code generated by Tsify
|
||||
#![allow(clippy::empty_docs)]
|
||||
|
||||
use crate::node::{EntryDetails, RoutingNode, RoutingNodeError, SupportedRoles};
|
||||
use crate::{CachedEpochRewardedSet, NymTopology};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashMap;
|
||||
use std::net::SocketAddr;
|
||||
use thiserror::Error;
|
||||
use tsify::Tsify;
|
||||
use wasm_bindgen::{prelude::wasm_bindgen, JsValue};
|
||||
use wasm_utils::error::simple_js_error;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum SerializableTopologyError {
|
||||
#[error(transparent)]
|
||||
NodeConversion(#[from] RoutingNodeError),
|
||||
|
||||
#[error("{provided} is not a valid ed25519 public key")]
|
||||
MalformedIdentity { provided: String },
|
||||
|
||||
#[error("{provided} is not a valid x25519 public key")]
|
||||
MalformedSphinxKey { provided: String },
|
||||
}
|
||||
|
||||
#[cfg(feature = "wasm-serde-types")]
|
||||
impl From<SerializableTopologyError> for JsValue {
|
||||
fn from(value: SerializableTopologyError) -> Self {
|
||||
simple_js_error(value.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, Tsify)]
|
||||
#[tsify(into_wasm_abi, from_wasm_abi)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct WasmFriendlyNymTopology {
|
||||
pub rewarded_set: CachedEpochRewardedSet,
|
||||
|
||||
pub node_details: HashMap<u32, WasmFriendlyRoutingNode>,
|
||||
}
|
||||
|
||||
impl TryFrom<WasmFriendlyNymTopology> for NymTopology {
|
||||
type Error = SerializableTopologyError;
|
||||
|
||||
fn try_from(value: WasmFriendlyNymTopology) -> Result<Self, Self::Error> {
|
||||
let node_details = value
|
||||
.node_details
|
||||
.into_values()
|
||||
.map(|details| details.try_into())
|
||||
.collect::<Result<_, _>>()?;
|
||||
|
||||
Ok(NymTopology::new(value.rewarded_set, node_details))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<NymTopology> for WasmFriendlyNymTopology {
|
||||
fn from(value: NymTopology) -> Self {
|
||||
WasmFriendlyNymTopology {
|
||||
rewarded_set: value.rewarded_set,
|
||||
node_details: value
|
||||
.node_details
|
||||
.into_iter()
|
||||
.map(|(k, v)| (k, v.into()))
|
||||
.collect(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, Tsify)]
|
||||
#[tsify(into_wasm_abi, from_wasm_abi)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct WasmFriendlyRoutingNode {
|
||||
pub node_id: u32,
|
||||
|
||||
pub mix_host: SocketAddr,
|
||||
|
||||
pub entry: Option<EntryDetails>,
|
||||
pub identity_key: String,
|
||||
pub sphinx_key: String,
|
||||
|
||||
pub supported_roles: SupportedRoles,
|
||||
}
|
||||
|
||||
impl TryFrom<WasmFriendlyRoutingNode> for RoutingNode {
|
||||
type Error = SerializableTopologyError;
|
||||
|
||||
fn try_from(value: WasmFriendlyRoutingNode) -> Result<Self, Self::Error> {
|
||||
Ok(RoutingNode {
|
||||
node_id: value.node_id,
|
||||
mix_host: value.mix_host,
|
||||
entry: value.entry,
|
||||
identity_key: value.identity_key.as_str().parse().map_err(|_| {
|
||||
SerializableTopologyError::MalformedIdentity {
|
||||
provided: value.identity_key,
|
||||
}
|
||||
})?,
|
||||
sphinx_key: value.sphinx_key.as_str().parse().map_err(|_| {
|
||||
SerializableTopologyError::MalformedIdentity {
|
||||
provided: value.sphinx_key,
|
||||
}
|
||||
})?,
|
||||
supported_roles: value.supported_roles,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl From<RoutingNode> for WasmFriendlyRoutingNode {
|
||||
fn from(node: RoutingNode) -> Self {
|
||||
WasmFriendlyRoutingNode {
|
||||
node_id: node.node_id,
|
||||
mix_host: node.mix_host,
|
||||
entry: node.entry,
|
||||
identity_key: node.identity_key.to_string(),
|
||||
sphinx_key: node.sphinx_key.to_string(),
|
||||
supported_roles: node.supported_roles,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -29,10 +29,10 @@ nym-credential-storage = { path = "../../credential-storage" }
|
||||
nym-crypto = { path = "../../crypto", features = ["asymmetric", "serde"] }
|
||||
nym-gateway-client = { path = "../../client-libs/gateway-client", default-features = false, features = ["wasm"] }
|
||||
nym-sphinx = { path = "../../nymsphinx" }
|
||||
nym-sphinx-acknowledgements = { path = "../../nymsphinx/acknowledgements", features = ["serde"] }
|
||||
nym-sphinx-acknowledgements = { path = "../../nymsphinx/acknowledgements", features = ["serde"]}
|
||||
nym-statistics-common = { path = "../../statistics" }
|
||||
nym-task = { path = "../../task" }
|
||||
nym-topology = { path = "../../topology", features = ["wasm-serde-types"] }
|
||||
nym-topology = { path = "../../topology", features = ["serializable", "wasm-serde-types"] }
|
||||
nym-validator-client = { path = "../../client-libs/validator-client", default-features = false }
|
||||
wasm-utils = { path = "../utils" }
|
||||
wasm-storage = { path = "../storage" }
|
||||
|
||||
@@ -387,15 +387,6 @@ pub struct TopologyWasm {
|
||||
/// Specifies a minimum performance of a gateway that is used on route construction.
|
||||
/// This setting is only applicable when `NymApi` topology is used.
|
||||
pub minimum_gateway_performance: u8,
|
||||
|
||||
/// Specifies whether this client should attempt to retrieve all available network nodes
|
||||
/// as opposed to just active mixnodes/gateways.
|
||||
/// Useless without `ignore_epoch_roles = true`
|
||||
pub use_extended_topology: bool,
|
||||
|
||||
/// Specifies whether this client should ignore the current epoch role of the target egress node
|
||||
/// when constructing the final hop packets.
|
||||
pub ignore_egress_epoch_role: bool,
|
||||
}
|
||||
|
||||
impl Default for TopologyWasm {
|
||||
@@ -418,8 +409,6 @@ impl From<TopologyWasm> for ConfigTopology {
|
||||
topology_structure: Default::default(),
|
||||
minimum_mixnode_performance: topology.minimum_mixnode_performance,
|
||||
minimum_gateway_performance: topology.minimum_gateway_performance,
|
||||
use_extended_topology: topology.use_extended_topology,
|
||||
ignore_egress_epoch_role: topology.ignore_egress_epoch_role,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -435,8 +424,6 @@ impl From<ConfigTopology> for TopologyWasm {
|
||||
disable_refreshing: topology.disable_refreshing,
|
||||
minimum_mixnode_performance: topology.minimum_mixnode_performance,
|
||||
minimum_gateway_performance: topology.minimum_gateway_performance,
|
||||
use_extended_topology: topology.use_extended_topology,
|
||||
ignore_egress_epoch_role: topology.ignore_egress_epoch_role,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -271,17 +271,6 @@ pub struct TopologyWasmOverride {
|
||||
/// This setting is only applicable when `NymApi` topology is used.
|
||||
#[tsify(optional)]
|
||||
pub minimum_gateway_performance: Option<u8>,
|
||||
|
||||
/// Specifies whether this client should attempt to retrieve all available network nodes
|
||||
/// as opposed to just active mixnodes/gateways.
|
||||
/// Useless without `ignore_epoch_roles = true`
|
||||
#[tsify(optional)]
|
||||
pub use_extended_topology: Option<bool>,
|
||||
|
||||
/// Specifies whether this client should ignore the current epoch role of the target egress node
|
||||
/// when constructing the final hop packets.
|
||||
#[tsify(optional)]
|
||||
pub ignore_egress_epoch_role: Option<bool>,
|
||||
}
|
||||
|
||||
impl From<TopologyWasmOverride> for TopologyWasm {
|
||||
@@ -305,12 +294,6 @@ impl From<TopologyWasmOverride> for TopologyWasm {
|
||||
minimum_gateway_performance: value
|
||||
.minimum_gateway_performance
|
||||
.unwrap_or(def.minimum_gateway_performance),
|
||||
use_extended_topology: value
|
||||
.use_extended_topology
|
||||
.unwrap_or(def.use_extended_topology),
|
||||
ignore_egress_epoch_role: value
|
||||
.ignore_egress_epoch_role
|
||||
.unwrap_or(def.ignore_egress_epoch_role),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,8 +15,7 @@ use nym_client_core::init::{
|
||||
};
|
||||
use nym_sphinx::addressing::clients::Recipient;
|
||||
use nym_sphinx::anonymous_replies::requests::AnonymousSenderTag;
|
||||
use nym_topology::wasm_helpers::WasmFriendlyNymTopology;
|
||||
use nym_topology::{NymTopology, RoutingNode};
|
||||
use nym_topology::{gateway, NymTopology, SerializableNymTopology};
|
||||
use nym_validator_client::client::IdentityKey;
|
||||
use nym_validator_client::NymApiClient;
|
||||
use rand::thread_rng;
|
||||
@@ -56,7 +55,7 @@ pub fn parse_sender_tag(tag: &str) -> Result<AnonymousSenderTag, WasmCoreError>
|
||||
|
||||
pub async fn current_network_topology_async(
|
||||
nym_api_url: String,
|
||||
) -> Result<WasmFriendlyNymTopology, WasmCoreError> {
|
||||
) -> Result<SerializableNymTopology, WasmCoreError> {
|
||||
let url: Url = match nym_api_url.parse() {
|
||||
Ok(url) => url,
|
||||
Err(source) => {
|
||||
@@ -68,17 +67,12 @@ pub async fn current_network_topology_async(
|
||||
};
|
||||
|
||||
let api_client = NymApiClient::new(url);
|
||||
let rewarded_set = api_client.get_current_rewarded_set().await?;
|
||||
let mixnodes = api_client
|
||||
.get_all_basic_active_mixing_assigned_nodes()
|
||||
.await?;
|
||||
let gateways = api_client.get_all_basic_entry_assigned_nodes().await?;
|
||||
|
||||
let mut topology = NymTopology::new_empty(rewarded_set);
|
||||
topology.add_skimmed_nodes(&mixnodes);
|
||||
topology.add_skimmed_nodes(&gateways);
|
||||
|
||||
Ok(topology.into())
|
||||
Ok(NymTopology::from_basic(&mixnodes, &gateways).into())
|
||||
}
|
||||
|
||||
#[wasm_bindgen(js_name = "currentNetworkTopology")]
|
||||
@@ -96,7 +90,7 @@ pub async fn setup_gateway_wasm(
|
||||
client_store: &ClientStorage,
|
||||
force_tls: bool,
|
||||
chosen_gateway: Option<IdentityKey>,
|
||||
gateways: Vec<RoutingNode>,
|
||||
gateways: &[gateway::LegacyNode],
|
||||
) -> Result<InitialisationResult, WasmCoreError> {
|
||||
// TODO: so much optimization and extra features could be added here, but that's for the future
|
||||
|
||||
@@ -113,7 +107,7 @@ pub async fn setup_gateway_wasm(
|
||||
|
||||
GatewaySetup::New {
|
||||
specification: selection_spec,
|
||||
available_gateways: gateways,
|
||||
available_gateways: gateways.to_vec(),
|
||||
}
|
||||
};
|
||||
|
||||
@@ -131,7 +125,7 @@ pub async fn setup_gateway_from_api(
|
||||
) -> Result<InitialisationResult, WasmCoreError> {
|
||||
let mut rng = thread_rng();
|
||||
let gateways = current_gateways(&mut rng, nym_apis, None, minimum_performance).await?;
|
||||
setup_gateway_wasm(client_store, force_tls, chosen_gateway, gateways).await
|
||||
setup_gateway_wasm(client_store, force_tls, chosen_gateway, &gateways).await
|
||||
}
|
||||
|
||||
pub async fn setup_from_topology(
|
||||
@@ -140,6 +134,6 @@ pub async fn setup_from_topology(
|
||||
topology: &NymTopology,
|
||||
client_store: &ClientStorage,
|
||||
) -> Result<InitialisationResult, WasmCoreError> {
|
||||
let gateways = topology.entry_capable_nodes().cloned().collect::<Vec<_>>();
|
||||
let gateways = topology.gateways();
|
||||
setup_gateway_wasm(client_store, force_tls, explicit_gateway, gateways).await
|
||||
}
|
||||
|
||||
@@ -1,26 +1,51 @@
|
||||
// Copyright 2023 - Nym Technologies SA <contact@nymtech.net>
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
use nym_topology::wasm_helpers::SerializableTopologyError;
|
||||
use nym_topology::SerializableTopologyError;
|
||||
use nym_validator_client::client::IdentityKeyRef;
|
||||
use wasm_utils::console_log;
|
||||
|
||||
pub use nym_topology::wasm_helpers::{WasmFriendlyNymTopology, WasmFriendlyRoutingNode};
|
||||
pub use nym_topology::{Role, RoutingNode};
|
||||
pub use nym_topology::{
|
||||
gateway, mix, SerializableGateway, SerializableMixNode, SerializableNymTopology,
|
||||
};
|
||||
|
||||
// redeclare this as a type alias for easy of use
|
||||
pub type WasmTopologyError = SerializableTopologyError;
|
||||
|
||||
// helper trait to define extra functionality on the external type that we used to have here before
|
||||
pub trait SerializableTopologyExt {
|
||||
// fn print(&self);
|
||||
fn print(&self);
|
||||
|
||||
fn ensure_contains_gateway_id(&self, gateway_id: IdentityKeyRef) -> bool;
|
||||
}
|
||||
|
||||
impl SerializableTopologyExt for WasmFriendlyNymTopology {
|
||||
impl SerializableTopologyExt for SerializableNymTopology {
|
||||
fn print(&self) {
|
||||
if !self.mixnodes.is_empty() {
|
||||
console_log!("mixnodes:");
|
||||
for (layer, nodes) in &self.mixnodes {
|
||||
console_log!("\tlayer {layer}:");
|
||||
for node in nodes {
|
||||
// console_log!("\t\t{} - {}", node.mix_id, node.identity_key)
|
||||
console_log!("\t\t{} - {:?}", node.mix_id, node)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
console_log!("NO MIXNODES")
|
||||
}
|
||||
|
||||
if !self.gateways.is_empty() {
|
||||
console_log!("gateways:");
|
||||
for gateway in &self.gateways {
|
||||
// console_log!("\t{}", gateway.identity_key)
|
||||
console_log!("\t{:?}", gateway)
|
||||
}
|
||||
} else {
|
||||
console_log!("NO GATEWAYS")
|
||||
}
|
||||
}
|
||||
|
||||
fn ensure_contains_gateway_id(&self, gateway_id: IdentityKeyRef) -> bool {
|
||||
self.node_details
|
||||
.values()
|
||||
.any(|node| node.identity_key == gateway_id)
|
||||
self.gateways.iter().any(|g| g.identity_key == gateway_id)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -267,18 +267,27 @@ impl PeerController {
|
||||
}))
|
||||
}
|
||||
|
||||
fn update_metrics(&self, new_host: &Host) {
|
||||
async fn update_metrics(&self, new_host: &Host) {
|
||||
let now = SystemTime::now();
|
||||
const ACTIVITY_THRESHOLD: Duration = Duration::from_secs(60);
|
||||
|
||||
let old_host = self.host_information.read().await;
|
||||
|
||||
let total_peers = new_host.peers.len();
|
||||
let mut active_peers = 0;
|
||||
let mut total_rx = 0;
|
||||
let mut total_tx = 0;
|
||||
let mut new_rx = 0;
|
||||
let mut new_tx = 0;
|
||||
|
||||
for peer in new_host.peers.values() {
|
||||
total_rx += peer.rx_bytes;
|
||||
total_tx += peer.tx_bytes;
|
||||
for (peer_key, peer) in new_host.peers.iter() {
|
||||
// only consider pre-existing peers,
|
||||
// so that the value would always be increasing
|
||||
if let Some(prior) = old_host.peers.get(peer_key) {
|
||||
let delta_rx = peer.rx_bytes.saturating_sub(prior.rx_bytes);
|
||||
let delta_tx = prior.tx_bytes.saturating_sub(prior.tx_bytes);
|
||||
|
||||
new_rx += delta_rx;
|
||||
new_tx += delta_tx;
|
||||
}
|
||||
|
||||
// if a peer hasn't performed a handshake in last minute,
|
||||
// I think it's reasonable to assume it's no longer active
|
||||
@@ -296,10 +305,10 @@ impl PeerController {
|
||||
self.metrics.wireguard.update(
|
||||
// if the conversion fails it means we're running not running on a 64bit system
|
||||
// and that's a reason enough for this failure.
|
||||
total_rx.try_into().expect(
|
||||
new_rx.try_into().expect(
|
||||
"failed to convert bytes from u64 to usize - are you running on non 64bit system?",
|
||||
),
|
||||
total_tx.try_into().expect(
|
||||
new_tx.try_into().expect(
|
||||
"failed to convert bytes from u64 to usize - are you running on non 64bit system?",
|
||||
),
|
||||
total_peers,
|
||||
@@ -316,7 +325,7 @@ impl PeerController {
|
||||
log::error!("Can't read wireguard kernel data");
|
||||
continue;
|
||||
};
|
||||
self.update_metrics(&host);
|
||||
self.update_metrics(&host).await;
|
||||
|
||||
*self.host_information.write().await = host;
|
||||
}
|
||||
|
||||
@@ -464,10 +464,7 @@ pub fn instantiate_contracts(
|
||||
mixnet_contract_address.clone(),
|
||||
&nym_mixnet_contract_common::MigrateMsg {
|
||||
vesting_contract_address: Some(vesting_contract_address.to_string()),
|
||||
current_nym_node_semver: "1.1.10".to_string(),
|
||||
version_score_weights: Default::default(),
|
||||
unsafe_skip_state_updates: Some(true),
|
||||
version_score_params: Default::default(),
|
||||
},
|
||||
mixnet_code_id,
|
||||
)
|
||||
|
||||
@@ -3470,43 +3470,13 @@
|
||||
"$schema": "http://json-schema.org/draft-07/schema#",
|
||||
"title": "MigrateMsg",
|
||||
"type": "object",
|
||||
"required": [
|
||||
"current_nym_node_semver"
|
||||
],
|
||||
"properties": {
|
||||
"current_nym_node_semver": {
|
||||
"type": "string"
|
||||
},
|
||||
"unsafe_skip_state_updates": {
|
||||
"type": [
|
||||
"boolean",
|
||||
"null"
|
||||
]
|
||||
},
|
||||
"version_score_params": {
|
||||
"default": {
|
||||
"penalty": "0.995",
|
||||
"penalty_scaling": "1.65"
|
||||
},
|
||||
"allOf": [
|
||||
{
|
||||
"$ref": "#/definitions/VersionScoreFormulaParams"
|
||||
}
|
||||
]
|
||||
},
|
||||
"version_score_weights": {
|
||||
"default": {
|
||||
"major": 100,
|
||||
"minor": 10,
|
||||
"patch": 1,
|
||||
"prerelease": 1
|
||||
},
|
||||
"allOf": [
|
||||
{
|
||||
"$ref": "#/definitions/OutdatedVersionWeights"
|
||||
}
|
||||
]
|
||||
},
|
||||
"vesting_contract_address": {
|
||||
"type": [
|
||||
"string",
|
||||
@@ -3514,63 +3484,7 @@
|
||||
]
|
||||
}
|
||||
},
|
||||
"additionalProperties": false,
|
||||
"definitions": {
|
||||
"Decimal": {
|
||||
"description": "A fixed-point decimal value with 18 fractional digits, i.e. Decimal(1_000_000_000_000_000_000) == 1.0\n\nThe greatest possible value that can be represented is 340282366920938463463.374607431768211455 (which is (2^128 - 1) / 10^18)",
|
||||
"type": "string"
|
||||
},
|
||||
"OutdatedVersionWeights": {
|
||||
"description": "Defines weights for calculating numbers of versions behind the current release.",
|
||||
"type": "object",
|
||||
"required": [
|
||||
"major",
|
||||
"minor",
|
||||
"patch",
|
||||
"prerelease"
|
||||
],
|
||||
"properties": {
|
||||
"major": {
|
||||
"type": "integer",
|
||||
"format": "uint32",
|
||||
"minimum": 0.0
|
||||
},
|
||||
"minor": {
|
||||
"type": "integer",
|
||||
"format": "uint32",
|
||||
"minimum": 0.0
|
||||
},
|
||||
"patch": {
|
||||
"type": "integer",
|
||||
"format": "uint32",
|
||||
"minimum": 0.0
|
||||
},
|
||||
"prerelease": {
|
||||
"type": "integer",
|
||||
"format": "uint32",
|
||||
"minimum": 0.0
|
||||
}
|
||||
},
|
||||
"additionalProperties": false
|
||||
},
|
||||
"VersionScoreFormulaParams": {
|
||||
"description": "Given the formula of version_score = penalty ^ (versions_behind_factor ^ penalty_scaling) define the relevant parameters",
|
||||
"type": "object",
|
||||
"required": [
|
||||
"penalty",
|
||||
"penalty_scaling"
|
||||
],
|
||||
"properties": {
|
||||
"penalty": {
|
||||
"$ref": "#/definitions/Decimal"
|
||||
},
|
||||
"penalty_scaling": {
|
||||
"$ref": "#/definitions/Decimal"
|
||||
}
|
||||
},
|
||||
"additionalProperties": false
|
||||
}
|
||||
}
|
||||
"additionalProperties": false
|
||||
},
|
||||
"sudo": null,
|
||||
"responses": {
|
||||
|
||||
@@ -2,43 +2,13 @@
|
||||
"$schema": "http://json-schema.org/draft-07/schema#",
|
||||
"title": "MigrateMsg",
|
||||
"type": "object",
|
||||
"required": [
|
||||
"current_nym_node_semver"
|
||||
],
|
||||
"properties": {
|
||||
"current_nym_node_semver": {
|
||||
"type": "string"
|
||||
},
|
||||
"unsafe_skip_state_updates": {
|
||||
"type": [
|
||||
"boolean",
|
||||
"null"
|
||||
]
|
||||
},
|
||||
"version_score_params": {
|
||||
"default": {
|
||||
"penalty": "0.995",
|
||||
"penalty_scaling": "1.65"
|
||||
},
|
||||
"allOf": [
|
||||
{
|
||||
"$ref": "#/definitions/VersionScoreFormulaParams"
|
||||
}
|
||||
]
|
||||
},
|
||||
"version_score_weights": {
|
||||
"default": {
|
||||
"major": 100,
|
||||
"minor": 10,
|
||||
"patch": 1,
|
||||
"prerelease": 1
|
||||
},
|
||||
"allOf": [
|
||||
{
|
||||
"$ref": "#/definitions/OutdatedVersionWeights"
|
||||
}
|
||||
]
|
||||
},
|
||||
"vesting_contract_address": {
|
||||
"type": [
|
||||
"string",
|
||||
@@ -46,61 +16,5 @@
|
||||
]
|
||||
}
|
||||
},
|
||||
"additionalProperties": false,
|
||||
"definitions": {
|
||||
"Decimal": {
|
||||
"description": "A fixed-point decimal value with 18 fractional digits, i.e. Decimal(1_000_000_000_000_000_000) == 1.0\n\nThe greatest possible value that can be represented is 340282366920938463463.374607431768211455 (which is (2^128 - 1) / 10^18)",
|
||||
"type": "string"
|
||||
},
|
||||
"OutdatedVersionWeights": {
|
||||
"description": "Defines weights for calculating numbers of versions behind the current release.",
|
||||
"type": "object",
|
||||
"required": [
|
||||
"major",
|
||||
"minor",
|
||||
"patch",
|
||||
"prerelease"
|
||||
],
|
||||
"properties": {
|
||||
"major": {
|
||||
"type": "integer",
|
||||
"format": "uint32",
|
||||
"minimum": 0.0
|
||||
},
|
||||
"minor": {
|
||||
"type": "integer",
|
||||
"format": "uint32",
|
||||
"minimum": 0.0
|
||||
},
|
||||
"patch": {
|
||||
"type": "integer",
|
||||
"format": "uint32",
|
||||
"minimum": 0.0
|
||||
},
|
||||
"prerelease": {
|
||||
"type": "integer",
|
||||
"format": "uint32",
|
||||
"minimum": 0.0
|
||||
}
|
||||
},
|
||||
"additionalProperties": false
|
||||
},
|
||||
"VersionScoreFormulaParams": {
|
||||
"description": "Given the formula of version_score = penalty ^ (versions_behind_factor ^ penalty_scaling) define the relevant parameters",
|
||||
"type": "object",
|
||||
"required": [
|
||||
"penalty",
|
||||
"penalty_scaling"
|
||||
],
|
||||
"properties": {
|
||||
"penalty": {
|
||||
"$ref": "#/definitions/Decimal"
|
||||
},
|
||||
"penalty_scaling": {
|
||||
"$ref": "#/definitions/Decimal"
|
||||
}
|
||||
},
|
||||
"additionalProperties": false
|
||||
}
|
||||
}
|
||||
"additionalProperties": false
|
||||
}
|
||||
|
||||
@@ -603,7 +603,7 @@ pub fn query(
|
||||
#[entry_point]
|
||||
pub fn migrate(
|
||||
mut deps: DepsMut<'_>,
|
||||
env: Env,
|
||||
_env: Env,
|
||||
msg: MigrateMsg,
|
||||
) -> Result<Response, MixnetContractError> {
|
||||
set_build_information!(deps.storage)?;
|
||||
@@ -612,7 +612,7 @@ pub fn migrate(
|
||||
let skip_state_updates = msg.unsafe_skip_state_updates.unwrap_or(false);
|
||||
|
||||
if !skip_state_updates {
|
||||
crate::queued_migrations::add_config_score_params(deps.branch(), env, &msg)?;
|
||||
crate::queued_migrations::restore_node_version_history(deps.branch())?;
|
||||
}
|
||||
|
||||
// due to circular dependency on contract addresses (i.e. mixnet contract requiring vesting contract address
|
||||
|
||||
@@ -34,9 +34,13 @@ impl NymNodeVersionHistory<'_> {
|
||||
}
|
||||
|
||||
fn next_id(&self, storage: &mut dyn Storage) -> Result<u32, MixnetContractError> {
|
||||
let next = self.id_counter.may_load(storage)?.unwrap_or_default();
|
||||
self.id_counter.save(storage, &next)?;
|
||||
Ok(next)
|
||||
let id = self
|
||||
.id_counter
|
||||
.may_load(storage)?
|
||||
.map(|current| current + 1)
|
||||
.unwrap_or_default();
|
||||
self.id_counter.save(storage, &id)?;
|
||||
Ok(id)
|
||||
}
|
||||
|
||||
pub fn current_version(
|
||||
@@ -56,10 +60,10 @@ impl NymNodeVersionHistory<'_> {
|
||||
pub fn insert_new(
|
||||
&self,
|
||||
storage: &mut dyn Storage,
|
||||
entry: HistoricalNymNodeVersion,
|
||||
entry: &HistoricalNymNodeVersion,
|
||||
) -> Result<u32, MixnetContractError> {
|
||||
let next_id = self.next_id(storage)?;
|
||||
self.version_history.save(storage, next_id, &entry)?;
|
||||
self.version_history.save(storage, next_id, entry)?;
|
||||
Ok(next_id)
|
||||
}
|
||||
|
||||
@@ -79,7 +83,7 @@ impl NymNodeVersionHistory<'_> {
|
||||
// treat this as genesis
|
||||
let genesis =
|
||||
HistoricalNymNodeVersion::genesis(raw_semver.to_string(), env.block.height);
|
||||
return self.insert_new(storage, genesis);
|
||||
return self.insert_new(storage, &genesis);
|
||||
};
|
||||
|
||||
let current_semver = current.version_information.semver_unchecked();
|
||||
@@ -99,7 +103,7 @@ impl NymNodeVersionHistory<'_> {
|
||||
introduced_at_height: env.block.height,
|
||||
difference_since_genesis: diff,
|
||||
};
|
||||
self.insert_new(storage, entry)
|
||||
self.insert_new(storage, &entry)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -170,3 +174,218 @@ pub(crate) fn initialise_storage(
|
||||
ADMIN.set(deps, Some(initial_admin))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[cfg(test)]
|
||||
mod nym_node_version_history {
|
||||
use super::*;
|
||||
use crate::support::tests::test_helpers::TestSetup;
|
||||
use cosmwasm_std::testing::{mock_dependencies, mock_env};
|
||||
|
||||
#[test]
|
||||
fn getting_current() -> anyhow::Result<()> {
|
||||
// empty storage
|
||||
let deps = mock_dependencies();
|
||||
let storage = NymNodeVersionHistory::new();
|
||||
assert!(storage.current_version(&deps.storage)?.is_none());
|
||||
|
||||
let mut test = TestSetup::new();
|
||||
|
||||
let zeroth = storage.current_version(test.storage())?.unwrap();
|
||||
let manual_zeroth = storage.version_history.load(test.storage(), 0)?;
|
||||
assert_eq!(zeroth.version_information, manual_zeroth);
|
||||
|
||||
// manually update the counter to make sure data is still read correctly
|
||||
let dummy = HistoricalNymNodeVersion {
|
||||
semver: "1.2.3".to_string(),
|
||||
introduced_at_height: 1234,
|
||||
difference_since_genesis: Default::default(),
|
||||
};
|
||||
storage.id_counter.save(test.storage_mut(), &123)?;
|
||||
storage
|
||||
.version_history
|
||||
.save(test.storage_mut(), 123, &dummy)?;
|
||||
|
||||
let updated = storage.current_version(test.storage())?.unwrap();
|
||||
assert_eq!(updated.version_information, dummy);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn inserting_new_entry() -> anyhow::Result<()> {
|
||||
let mut test = TestSetup::new();
|
||||
let storage = NymNodeVersionHistory::new();
|
||||
|
||||
let first = HistoricalNymNodeVersion {
|
||||
semver: "1.1.1".to_string(),
|
||||
introduced_at_height: 12,
|
||||
difference_since_genesis: Default::default(),
|
||||
};
|
||||
let second = HistoricalNymNodeVersion {
|
||||
semver: "1.1.2".to_string(),
|
||||
introduced_at_height: 123,
|
||||
difference_since_genesis: Default::default(),
|
||||
};
|
||||
let third = HistoricalNymNodeVersion {
|
||||
semver: "1.1.3".to_string(),
|
||||
introduced_at_height: 1234,
|
||||
difference_since_genesis: Default::default(),
|
||||
};
|
||||
|
||||
assert_eq!(storage.id_counter.load(test.storage())?, 0);
|
||||
|
||||
// id is correctly incremented for each case and no entry is overwritten
|
||||
storage.insert_new(test.storage_mut(), &first)?;
|
||||
assert_eq!(storage.id_counter.load(test.storage())?, 1);
|
||||
|
||||
storage.insert_new(test.storage_mut(), &second)?;
|
||||
assert_eq!(storage.id_counter.load(test.storage())?, 2);
|
||||
|
||||
storage.insert_new(test.storage_mut(), &third)?;
|
||||
assert_eq!(storage.id_counter.load(test.storage())?, 3);
|
||||
|
||||
assert_eq!(storage.version_history.load(test.storage(), 1)?, first);
|
||||
assert_eq!(storage.version_history.load(test.storage(), 2)?, second);
|
||||
assert_eq!(storage.version_history.load(test.storage(), 3)?, third);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn inserting_initial_semver() -> anyhow::Result<()> {
|
||||
// empty storage
|
||||
let mut deps = mock_dependencies();
|
||||
let env = mock_env();
|
||||
let storage = NymNodeVersionHistory::new();
|
||||
|
||||
assert!(storage
|
||||
.id_counter
|
||||
.may_load(deps.as_mut().storage)?
|
||||
.is_none());
|
||||
|
||||
storage.try_insert_new(deps.as_mut().storage, &env, "1.1.1")?;
|
||||
assert_eq!(storage.id_counter.load(deps.as_mut().storage)?, 0);
|
||||
|
||||
assert_eq!(
|
||||
storage
|
||||
.version_history
|
||||
.load(deps.as_ref().storage, 0)?
|
||||
.semver,
|
||||
"1.1.1"
|
||||
);
|
||||
assert_eq!(
|
||||
storage
|
||||
.current_version(deps.as_ref().storage)?
|
||||
.unwrap()
|
||||
.version_information
|
||||
.semver,
|
||||
"1.1.1"
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn inserting_second_semver() -> anyhow::Result<()> {
|
||||
let mut test = TestSetup::new();
|
||||
let env = test.env();
|
||||
let storage = NymNodeVersionHistory::new();
|
||||
|
||||
// lower version
|
||||
assert!(storage
|
||||
.try_insert_new(test.storage_mut(), &env, "1.1.9")
|
||||
.is_err());
|
||||
assert!(storage
|
||||
.try_insert_new(test.storage_mut(), &env, "1.0.1")
|
||||
.is_err());
|
||||
|
||||
// malformed
|
||||
assert!(storage
|
||||
.try_insert_new(test.storage_mut(), &env, "1.0")
|
||||
.is_err());
|
||||
assert!(storage
|
||||
.try_insert_new(test.storage_mut(), &env, "1.0bad")
|
||||
.is_err());
|
||||
assert!(storage
|
||||
.try_insert_new(test.storage_mut(), &env, "foomp")
|
||||
.is_err());
|
||||
|
||||
// patch
|
||||
let mut test = TestSetup::new();
|
||||
storage.try_insert_new(test.storage_mut(), &env, "1.1.11")?;
|
||||
let current = storage
|
||||
.current_version(test.storage_mut())?
|
||||
.unwrap()
|
||||
.version_information;
|
||||
assert_eq!(current.semver, "1.1.11");
|
||||
assert_eq!(current.difference_since_genesis.major, 0);
|
||||
assert_eq!(current.difference_since_genesis.minor, 0);
|
||||
assert_eq!(current.difference_since_genesis.patch, 1);
|
||||
assert_eq!(current.difference_since_genesis.prerelease, 0);
|
||||
|
||||
// minor
|
||||
let mut test = TestSetup::new();
|
||||
storage.try_insert_new(test.storage_mut(), &env, "1.2.0")?;
|
||||
let current = storage
|
||||
.current_version(test.storage_mut())?
|
||||
.unwrap()
|
||||
.version_information;
|
||||
assert_eq!(current.semver, "1.2.0");
|
||||
assert_eq!(current.difference_since_genesis.major, 0);
|
||||
assert_eq!(current.difference_since_genesis.minor, 1);
|
||||
assert_eq!(current.difference_since_genesis.patch, 0);
|
||||
assert_eq!(current.difference_since_genesis.prerelease, 0);
|
||||
|
||||
// minor alt.
|
||||
let mut test = TestSetup::new();
|
||||
storage.try_insert_new(test.storage_mut(), &env, "1.2.3")?;
|
||||
let current = storage
|
||||
.current_version(test.storage_mut())?
|
||||
.unwrap()
|
||||
.version_information;
|
||||
assert_eq!(current.semver, "1.2.3");
|
||||
assert_eq!(current.difference_since_genesis.major, 0);
|
||||
assert_eq!(current.difference_since_genesis.minor, 1);
|
||||
assert_eq!(current.difference_since_genesis.patch, 0);
|
||||
assert_eq!(current.difference_since_genesis.prerelease, 0);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn inserting_subsequent_semver() -> anyhow::Result<()> {
|
||||
let mut test = TestSetup::new();
|
||||
let env = test.env();
|
||||
let storage = NymNodeVersionHistory::new();
|
||||
|
||||
storage.try_insert_new(test.storage_mut(), &env, "1.2.0")?;
|
||||
storage.try_insert_new(test.storage_mut(), &env, "1.2.1")?;
|
||||
storage.try_insert_new(test.storage_mut(), &env, "1.2.3")?;
|
||||
let current = storage
|
||||
.current_version(test.storage_mut())?
|
||||
.unwrap()
|
||||
.version_information;
|
||||
assert_eq!(current.semver, "1.2.3");
|
||||
assert_eq!(current.difference_since_genesis.major, 0);
|
||||
assert_eq!(current.difference_since_genesis.minor, 1);
|
||||
assert_eq!(current.difference_since_genesis.patch, 3);
|
||||
assert_eq!(current.difference_since_genesis.prerelease, 0);
|
||||
|
||||
storage.try_insert_new(test.storage_mut(), &env, "1.3.0")?;
|
||||
let current = storage
|
||||
.current_version(test.storage_mut())?
|
||||
.unwrap()
|
||||
.version_information;
|
||||
assert_eq!(current.semver, "1.3.0");
|
||||
assert_eq!(current.difference_since_genesis.major, 0);
|
||||
assert_eq!(current.difference_since_genesis.minor, 2);
|
||||
assert_eq!(current.difference_since_genesis.patch, 3);
|
||||
assert_eq!(current.difference_since_genesis.prerelease, 0);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -238,4 +238,446 @@ pub mod tests {
|
||||
// let res = try_update_contract_settings(deps.as_mut(), info, new_params);
|
||||
// assert_eq!(Err(MixnetContractError::ZeroActiveSet), res);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod updating_current_nym_node_semver {
|
||||
use super::*;
|
||||
use crate::mixnet_contract_settings::queries::query_current_nym_node_version;
|
||||
use crate::support::tests::test_helpers::TestSetup;
|
||||
|
||||
#[test]
|
||||
fn is_restricted_to_the_admin() -> anyhow::Result<()> {
|
||||
let mut test = TestSetup::new();
|
||||
|
||||
let not_admin = mock_info("not-admin", &[]);
|
||||
let admin = mock_info(test.admin().as_ref(), &[]);
|
||||
|
||||
let env = test.env();
|
||||
let res = try_update_current_nym_node_semver(
|
||||
test.deps_mut(),
|
||||
env,
|
||||
not_admin,
|
||||
"1.2.1".to_string(),
|
||||
);
|
||||
assert!(res.is_err());
|
||||
|
||||
let env = test.env();
|
||||
let res = try_update_current_nym_node_semver(
|
||||
test.deps_mut(),
|
||||
env,
|
||||
admin,
|
||||
"1.2.1".to_string(),
|
||||
);
|
||||
assert!(res.is_ok());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn updates_current_semver_value() -> anyhow::Result<()> {
|
||||
let mut test = TestSetup::new();
|
||||
|
||||
let res = query_current_nym_node_version(test.deps())?;
|
||||
|
||||
let initial = res.version.unwrap().version_information.semver;
|
||||
// sanity check to make sure our contract init hasn't changed
|
||||
assert_eq!(initial, "1.1.10");
|
||||
|
||||
let update = "1.2.0".to_string();
|
||||
|
||||
let env = test.env();
|
||||
let sender = test.admin_sender();
|
||||
try_update_current_nym_node_semver(test.deps_mut(), env, sender, update.clone())?;
|
||||
|
||||
let updated = query_current_nym_node_version(test.deps())?;
|
||||
let version = updated.version.unwrap().version_information.semver;
|
||||
assert_eq!(version, update);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod semver_chain_updates {
|
||||
use super::*;
|
||||
use crate::mixnet_contract_settings::queries::query_nym_node_version_history_paged;
|
||||
use mixnet_contract_common::{
|
||||
HistoricalNymNodeVersion, HistoricalNymNodeVersionEntry, TotalVersionDifference,
|
||||
};
|
||||
|
||||
fn test_setup_with_initial_checks() -> anyhow::Result<TestSetup> {
|
||||
let test = TestSetup::new();
|
||||
|
||||
let res = query_current_nym_node_version(test.deps())?;
|
||||
let initial = res.version.unwrap().version_information.semver;
|
||||
|
||||
// sanity check to make sure our contract init hasn't changed
|
||||
assert_eq!(initial, "1.1.10");
|
||||
|
||||
let history = query_nym_node_version_history_paged(test.deps(), None, None)?;
|
||||
assert_eq!(history.history.len(), 1);
|
||||
|
||||
Ok(test)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn single_patch() -> anyhow::Result<()> {
|
||||
let mut test = test_setup_with_initial_checks()?;
|
||||
let initial = query_current_nym_node_version(test.deps())?
|
||||
.version
|
||||
.unwrap();
|
||||
|
||||
let env = test.env();
|
||||
let sender = test.admin_sender();
|
||||
try_update_current_nym_node_semver(
|
||||
test.deps_mut(),
|
||||
env.clone(),
|
||||
sender,
|
||||
"1.1.11".to_string(),
|
||||
)?;
|
||||
|
||||
let history =
|
||||
query_nym_node_version_history_paged(test.deps(), None, None)?.history;
|
||||
assert_eq!(history.len(), 2);
|
||||
assert_eq!(history[0], initial);
|
||||
assert_eq!(
|
||||
history[1],
|
||||
HistoricalNymNodeVersionEntry {
|
||||
id: 1,
|
||||
version_information: HistoricalNymNodeVersion {
|
||||
semver: "1.1.11".to_string(),
|
||||
introduced_at_height: env.block.height,
|
||||
difference_since_genesis: TotalVersionDifference {
|
||||
major: 0,
|
||||
minor: 0,
|
||||
patch: 1,
|
||||
prerelease: 0,
|
||||
},
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn single_minor() -> anyhow::Result<()> {
|
||||
let mut test = test_setup_with_initial_checks()?;
|
||||
let initial = query_current_nym_node_version(test.deps())?
|
||||
.version
|
||||
.unwrap();
|
||||
|
||||
let env = test.env();
|
||||
let sender = test.admin_sender();
|
||||
try_update_current_nym_node_semver(
|
||||
test.deps_mut(),
|
||||
env.clone(),
|
||||
sender,
|
||||
"1.2.0".to_string(),
|
||||
)?;
|
||||
|
||||
let history =
|
||||
query_nym_node_version_history_paged(test.deps(), None, None)?.history;
|
||||
assert_eq!(history.len(), 2);
|
||||
assert_eq!(history[0], initial);
|
||||
assert_eq!(
|
||||
history[1],
|
||||
HistoricalNymNodeVersionEntry {
|
||||
id: 1,
|
||||
version_information: HistoricalNymNodeVersion {
|
||||
semver: "1.2.0".to_string(),
|
||||
introduced_at_height: env.block.height,
|
||||
difference_since_genesis: TotalVersionDifference {
|
||||
major: 0,
|
||||
minor: 1,
|
||||
patch: 0,
|
||||
prerelease: 0,
|
||||
},
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn multiple_patches() -> anyhow::Result<()> {
|
||||
let mut test = test_setup_with_initial_checks()?;
|
||||
let initial = query_current_nym_node_version(test.deps())?
|
||||
.version
|
||||
.unwrap();
|
||||
|
||||
let mut env = test.env();
|
||||
let sender = test.admin_sender();
|
||||
try_update_current_nym_node_semver(
|
||||
test.deps_mut(),
|
||||
env.clone(),
|
||||
sender.clone(),
|
||||
"1.1.11".to_string(),
|
||||
)?;
|
||||
env.block.height += 1;
|
||||
try_update_current_nym_node_semver(
|
||||
test.deps_mut(),
|
||||
env.clone(),
|
||||
sender.clone(),
|
||||
"1.1.12".to_string(),
|
||||
)?;
|
||||
env.block.height += 1;
|
||||
try_update_current_nym_node_semver(
|
||||
test.deps_mut(),
|
||||
env.clone(),
|
||||
sender,
|
||||
"1.1.13".to_string(),
|
||||
)?;
|
||||
|
||||
let history =
|
||||
query_nym_node_version_history_paged(test.deps(), None, None)?.history;
|
||||
assert_eq!(history.len(), 4);
|
||||
assert_eq!(history[0], initial);
|
||||
assert_eq!(
|
||||
history[1],
|
||||
HistoricalNymNodeVersionEntry {
|
||||
id: 1,
|
||||
version_information: HistoricalNymNodeVersion {
|
||||
semver: "1.1.11".to_string(),
|
||||
introduced_at_height: env.block.height - 2,
|
||||
difference_since_genesis: TotalVersionDifference {
|
||||
major: 0,
|
||||
minor: 0,
|
||||
patch: 1,
|
||||
prerelease: 0,
|
||||
},
|
||||
},
|
||||
}
|
||||
);
|
||||
assert_eq!(
|
||||
history[2],
|
||||
HistoricalNymNodeVersionEntry {
|
||||
id: 2,
|
||||
version_information: HistoricalNymNodeVersion {
|
||||
semver: "1.1.12".to_string(),
|
||||
introduced_at_height: env.block.height - 1,
|
||||
difference_since_genesis: TotalVersionDifference {
|
||||
major: 0,
|
||||
minor: 0,
|
||||
patch: 2,
|
||||
prerelease: 0,
|
||||
},
|
||||
},
|
||||
}
|
||||
);
|
||||
assert_eq!(
|
||||
history[3],
|
||||
HistoricalNymNodeVersionEntry {
|
||||
id: 3,
|
||||
version_information: HistoricalNymNodeVersion {
|
||||
semver: "1.1.13".to_string(),
|
||||
introduced_at_height: env.block.height,
|
||||
difference_since_genesis: TotalVersionDifference {
|
||||
major: 0,
|
||||
minor: 0,
|
||||
patch: 3,
|
||||
prerelease: 0,
|
||||
},
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn multiple_minors() -> anyhow::Result<()> {
|
||||
let mut test = test_setup_with_initial_checks()?;
|
||||
let initial = query_current_nym_node_version(test.deps())?
|
||||
.version
|
||||
.unwrap();
|
||||
|
||||
let mut env = test.env();
|
||||
let sender = test.admin_sender();
|
||||
try_update_current_nym_node_semver(
|
||||
test.deps_mut(),
|
||||
env.clone(),
|
||||
sender.clone(),
|
||||
"1.2.0".to_string(),
|
||||
)?;
|
||||
env.block.height += 1;
|
||||
try_update_current_nym_node_semver(
|
||||
test.deps_mut(),
|
||||
env.clone(),
|
||||
sender.clone(),
|
||||
"1.3.0".to_string(),
|
||||
)?;
|
||||
env.block.height += 1;
|
||||
try_update_current_nym_node_semver(
|
||||
test.deps_mut(),
|
||||
env.clone(),
|
||||
sender,
|
||||
"1.4.0".to_string(),
|
||||
)?;
|
||||
|
||||
let history =
|
||||
query_nym_node_version_history_paged(test.deps(), None, None)?.history;
|
||||
assert_eq!(history.len(), 4);
|
||||
assert_eq!(history[0], initial);
|
||||
assert_eq!(
|
||||
history[1],
|
||||
HistoricalNymNodeVersionEntry {
|
||||
id: 1,
|
||||
version_information: HistoricalNymNodeVersion {
|
||||
semver: "1.2.0".to_string(),
|
||||
introduced_at_height: env.block.height - 2,
|
||||
difference_since_genesis: TotalVersionDifference {
|
||||
major: 0,
|
||||
minor: 1,
|
||||
patch: 0,
|
||||
prerelease: 0,
|
||||
},
|
||||
},
|
||||
}
|
||||
);
|
||||
assert_eq!(
|
||||
history[2],
|
||||
HistoricalNymNodeVersionEntry {
|
||||
id: 2,
|
||||
version_information: HistoricalNymNodeVersion {
|
||||
semver: "1.3.0".to_string(),
|
||||
introduced_at_height: env.block.height - 1,
|
||||
difference_since_genesis: TotalVersionDifference {
|
||||
major: 0,
|
||||
minor: 2,
|
||||
patch: 0,
|
||||
prerelease: 0,
|
||||
},
|
||||
},
|
||||
}
|
||||
);
|
||||
assert_eq!(
|
||||
history[3],
|
||||
HistoricalNymNodeVersionEntry {
|
||||
id: 3,
|
||||
version_information: HistoricalNymNodeVersion {
|
||||
semver: "1.4.0".to_string(),
|
||||
introduced_at_height: env.block.height,
|
||||
difference_since_genesis: TotalVersionDifference {
|
||||
major: 0,
|
||||
minor: 3,
|
||||
patch: 0,
|
||||
prerelease: 0,
|
||||
},
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mixed_multiple_updates() -> anyhow::Result<()> {
|
||||
let mut test = test_setup_with_initial_checks()?;
|
||||
let initial = query_current_nym_node_version(test.deps())?
|
||||
.version
|
||||
.unwrap();
|
||||
|
||||
let mut env = test.env();
|
||||
let sender = test.admin_sender();
|
||||
try_update_current_nym_node_semver(
|
||||
test.deps_mut(),
|
||||
env.clone(),
|
||||
sender.clone(),
|
||||
"1.2.0".to_string(),
|
||||
)?;
|
||||
env.block.height += 1;
|
||||
try_update_current_nym_node_semver(
|
||||
test.deps_mut(),
|
||||
env.clone(),
|
||||
sender.clone(),
|
||||
"1.2.1".to_string(),
|
||||
)?;
|
||||
env.block.height += 1;
|
||||
try_update_current_nym_node_semver(
|
||||
test.deps_mut(),
|
||||
env.clone(),
|
||||
sender.clone(),
|
||||
"1.2.3".to_string(),
|
||||
)?;
|
||||
env.block.height += 1;
|
||||
try_update_current_nym_node_semver(
|
||||
test.deps_mut(),
|
||||
env.clone(),
|
||||
sender,
|
||||
"1.3.0".to_string(),
|
||||
)?;
|
||||
|
||||
let history =
|
||||
query_nym_node_version_history_paged(test.deps(), None, None)?.history;
|
||||
assert_eq!(history.len(), 5);
|
||||
assert_eq!(history[0], initial);
|
||||
assert_eq!(
|
||||
history[1],
|
||||
HistoricalNymNodeVersionEntry {
|
||||
id: 1,
|
||||
version_information: HistoricalNymNodeVersion {
|
||||
semver: "1.2.0".to_string(),
|
||||
introduced_at_height: env.block.height - 3,
|
||||
difference_since_genesis: TotalVersionDifference {
|
||||
major: 0,
|
||||
minor: 1,
|
||||
patch: 0,
|
||||
prerelease: 0,
|
||||
},
|
||||
},
|
||||
}
|
||||
);
|
||||
assert_eq!(
|
||||
history[2],
|
||||
HistoricalNymNodeVersionEntry {
|
||||
id: 2,
|
||||
version_information: HistoricalNymNodeVersion {
|
||||
semver: "1.2.1".to_string(),
|
||||
introduced_at_height: env.block.height - 2,
|
||||
difference_since_genesis: TotalVersionDifference {
|
||||
major: 0,
|
||||
minor: 1,
|
||||
patch: 1,
|
||||
prerelease: 0,
|
||||
},
|
||||
},
|
||||
}
|
||||
);
|
||||
assert_eq!(
|
||||
history[3],
|
||||
HistoricalNymNodeVersionEntry {
|
||||
id: 3,
|
||||
version_information: HistoricalNymNodeVersion {
|
||||
semver: "1.2.3".to_string(),
|
||||
introduced_at_height: env.block.height - 1,
|
||||
difference_since_genesis: TotalVersionDifference {
|
||||
major: 0,
|
||||
minor: 1,
|
||||
patch: 3,
|
||||
prerelease: 0,
|
||||
},
|
||||
},
|
||||
}
|
||||
);
|
||||
assert_eq!(
|
||||
history[4],
|
||||
HistoricalNymNodeVersionEntry {
|
||||
id: 4,
|
||||
version_information: HistoricalNymNodeVersion {
|
||||
semver: "1.3.0".to_string(),
|
||||
introduced_at_height: env.block.height,
|
||||
difference_since_genesis: TotalVersionDifference {
|
||||
major: 0,
|
||||
minor: 2,
|
||||
patch: 3,
|
||||
prerelease: 0,
|
||||
},
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,86 +1,153 @@
|
||||
// Copyright 2022-2024 - Nym Technologies SA <contact@nymtech.net>
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
mod config_score_params {
|
||||
use crate::constants::CONTRACT_STATE_KEY;
|
||||
use crate::mixnet_contract_settings::storage as mixnet_params_storage;
|
||||
mod node_version_history {
|
||||
use crate::mixnet_contract_settings::storage::NymNodeVersionHistory;
|
||||
use cosmwasm_std::{Addr, Coin, DepsMut, Env};
|
||||
use cw_storage_plus::Item;
|
||||
use cosmwasm_std::DepsMut;
|
||||
use mixnet_contract_common::error::MixnetContractError;
|
||||
use mixnet_contract_common::{
|
||||
ConfigScoreParams, ContractState, ContractStateParams, DelegationsParams, MigrateMsg,
|
||||
OperatingCostRange, OperatorsParams, ProfitMarginRange,
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::str::FromStr;
|
||||
use mixnet_contract_common::{HistoricalNymNodeVersion, TotalVersionDifference};
|
||||
|
||||
pub(crate) fn add_config_score_params(
|
||||
pub(crate) fn restore_node_version_history(
|
||||
deps: DepsMut<'_>,
|
||||
env: Env,
|
||||
msg: &MigrateMsg,
|
||||
) -> Result<(), MixnetContractError> {
|
||||
if semver::Version::from_str(&msg.current_nym_node_semver).is_err() {
|
||||
return Err(MixnetContractError::InvalidNymNodeSemver {
|
||||
provided: msg.current_nym_node_semver.to_string(),
|
||||
// sanity check:
|
||||
let storage = NymNodeVersionHistory::new();
|
||||
let Some(current) = storage.current_version(deps.storage)? else {
|
||||
return Err(MixnetContractError::FailedMigration {
|
||||
comment: "no node version history set".to_string(),
|
||||
});
|
||||
};
|
||||
if current.version_information.semver != "1.2.1"
|
||||
|| current.version_information.introduced_at_height != 15902170
|
||||
{
|
||||
return Err(MixnetContractError::FailedMigration {
|
||||
comment: format!("unexpected current node version history. got: {current:?}"),
|
||||
});
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct OldContractState {
|
||||
pub owner: Option<Addr>,
|
||||
pub rewarding_validator_address: Addr,
|
||||
pub vesting_contract_address: Addr,
|
||||
pub rewarding_denom: String,
|
||||
pub params: OldContractStateParams,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct OldContractStateParams {
|
||||
pub minimum_delegation: Option<Coin>,
|
||||
pub minimum_pledge: Coin,
|
||||
#[serde(default)]
|
||||
pub profit_margin: ProfitMarginRange,
|
||||
#[serde(default)]
|
||||
pub interval_operating_cost: OperatingCostRange,
|
||||
}
|
||||
|
||||
const OLD_CONTRACT_STATE: Item<'_, OldContractState> = Item::new(CONTRACT_STATE_KEY);
|
||||
let old_state = OLD_CONTRACT_STATE.load(deps.storage)?;
|
||||
|
||||
#[allow(deprecated)]
|
||||
let new_state = ContractState {
|
||||
owner: old_state.owner,
|
||||
rewarding_validator_address: old_state.rewarding_validator_address,
|
||||
vesting_contract_address: old_state.vesting_contract_address,
|
||||
rewarding_denom: old_state.rewarding_denom,
|
||||
params: ContractStateParams {
|
||||
delegations_params: DelegationsParams {
|
||||
minimum_delegation: old_state.params.minimum_delegation,
|
||||
},
|
||||
operators_params: OperatorsParams {
|
||||
minimum_pledge: old_state.params.minimum_pledge,
|
||||
profit_margin: old_state.params.profit_margin,
|
||||
interval_operating_cost: old_state.params.interval_operating_cost,
|
||||
},
|
||||
config_score_params: ConfigScoreParams {
|
||||
version_weights: msg.version_score_weights,
|
||||
version_score_formula_params: msg.version_score_params,
|
||||
},
|
||||
},
|
||||
let lost = HistoricalNymNodeVersion {
|
||||
semver: "1.1.12".to_string(),
|
||||
introduced_at_height: 15779133,
|
||||
difference_since_genesis: TotalVersionDifference::default(),
|
||||
};
|
||||
|
||||
mixnet_params_storage::CONTRACT_STATE.save(deps.storage, &new_state)?;
|
||||
#[allow(clippy::unwrap_used)]
|
||||
// SAFETY: this information was already stored in the contract, so it must be a valid semver
|
||||
let difference_since_genesis = lost.cumulative_difference_since_genesis(
|
||||
¤t.version_information.semver.parse().unwrap(),
|
||||
);
|
||||
let updated_current = HistoricalNymNodeVersion {
|
||||
semver: current.version_information.semver,
|
||||
introduced_at_height: current.version_information.introduced_at_height,
|
||||
difference_since_genesis,
|
||||
};
|
||||
|
||||
// initialise the version chain
|
||||
NymNodeVersionHistory::new().try_insert_new(
|
||||
deps.storage,
|
||||
&env,
|
||||
&msg.current_nym_node_semver,
|
||||
)?;
|
||||
// restore overwritten entry for 1.1.12
|
||||
storage.version_history.save(deps.storage, 0, &lost)?;
|
||||
|
||||
// re-insert 1.2.1 as the current
|
||||
storage
|
||||
.version_history
|
||||
.save(deps.storage, 1, &updated_current)?;
|
||||
storage.id_counter.save(deps.storage, &1)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) use config_score_params::add_config_score_params;
|
||||
pub(crate) use node_version_history::restore_node_version_history;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::mixnet_contract_settings::queries::query_nym_node_version_history_paged;
|
||||
use crate::mixnet_contract_settings::storage::NymNodeVersionHistory;
|
||||
use cosmwasm_std::testing::{mock_dependencies, mock_env};
|
||||
use mixnet_contract_common::{
|
||||
HistoricalNymNodeVersion, HistoricalNymNodeVersionEntry, TotalVersionDifference,
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn fixing_history_storage() -> anyhow::Result<()> {
|
||||
// current state on mainnet:
|
||||
let mut deps = mock_dependencies();
|
||||
let storage = NymNodeVersionHistory::new();
|
||||
|
||||
storage.id_counter.save(deps.as_mut().storage, &0)?;
|
||||
storage.version_history.save(
|
||||
deps.as_mut().storage,
|
||||
0,
|
||||
&HistoricalNymNodeVersion {
|
||||
semver: "1.2.1".to_string(),
|
||||
introduced_at_height: 15902170,
|
||||
difference_since_genesis: Default::default(),
|
||||
},
|
||||
)?;
|
||||
|
||||
// run migration
|
||||
restore_node_version_history(deps.as_mut())?;
|
||||
|
||||
let current = storage.current_version(deps.as_ref().storage)?.unwrap();
|
||||
assert_eq!(current.version_information.semver, "1.2.1");
|
||||
assert_eq!(current.version_information.introduced_at_height, 15902170);
|
||||
assert_eq!(
|
||||
current.version_information.difference_since_genesis,
|
||||
TotalVersionDifference {
|
||||
major: 0,
|
||||
minor: 1,
|
||||
patch: 0,
|
||||
prerelease: 0,
|
||||
}
|
||||
);
|
||||
|
||||
let history = query_nym_node_version_history_paged(deps.as_ref(), None, None)?.history;
|
||||
assert_eq!(history.len(), 2);
|
||||
assert_eq!(
|
||||
history,
|
||||
vec![
|
||||
HistoricalNymNodeVersionEntry {
|
||||
id: 0,
|
||||
version_information: HistoricalNymNodeVersion {
|
||||
semver: "1.1.12".to_string(),
|
||||
introduced_at_height: 15779133,
|
||||
difference_since_genesis: Default::default(),
|
||||
},
|
||||
},
|
||||
HistoricalNymNodeVersionEntry {
|
||||
id: 1,
|
||||
version_information: HistoricalNymNodeVersion {
|
||||
semver: "1.2.1".to_string(),
|
||||
introduced_at_height: 15902170,
|
||||
difference_since_genesis: TotalVersionDifference {
|
||||
major: 0,
|
||||
minor: 1,
|
||||
patch: 0,
|
||||
prerelease: 0,
|
||||
},
|
||||
},
|
||||
}
|
||||
]
|
||||
);
|
||||
|
||||
let counter = storage.id_counter.load(deps.as_ref().storage)?;
|
||||
assert_eq!(counter, 1);
|
||||
|
||||
// make sure adding another version doesn't mess anything up
|
||||
storage.try_insert_new(deps.as_mut().storage, &mock_env(), "1.3.0")?;
|
||||
|
||||
let current = storage.current_version(deps.as_ref().storage)?.unwrap();
|
||||
assert_eq!(current.version_information.semver, "1.3.0");
|
||||
assert_eq!(
|
||||
current.version_information.difference_since_genesis,
|
||||
TotalVersionDifference {
|
||||
major: 0,
|
||||
minor: 2,
|
||||
patch: 0,
|
||||
prerelease: 0,
|
||||
}
|
||||
);
|
||||
let counter = storage.id_counter.load(deps.as_ref().storage)?;
|
||||
assert_eq!(counter, 2);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@ pub mod test_helpers {
|
||||
};
|
||||
use crate::interval::{pending_events, storage as interval_storage};
|
||||
use crate::mixnet_contract_settings::storage::{
|
||||
self as mixnet_params_storage, minimum_node_pledge,
|
||||
self as mixnet_params_storage, minimum_node_pledge, ADMIN,
|
||||
};
|
||||
use crate::mixnet_contract_settings::storage::{rewarding_denom, rewarding_validator_address};
|
||||
use crate::mixnodes::helpers::get_mixnode_details_by_id;
|
||||
@@ -318,6 +318,10 @@ pub mod test_helpers {
|
||||
compare_decimals(mix_info.delegates, subtotal, Some(epsilon))
|
||||
}
|
||||
|
||||
pub fn admin(&self) -> Addr {
|
||||
ADMIN.get(self.deps()).unwrap().unwrap()
|
||||
}
|
||||
|
||||
pub fn random_address(&mut self) -> String {
|
||||
format!("n1foomp{}", self.rng.next_u64())
|
||||
}
|
||||
@@ -330,6 +334,14 @@ pub mod test_helpers {
|
||||
self.deps.as_mut()
|
||||
}
|
||||
|
||||
pub fn storage(&self) -> &dyn Storage {
|
||||
self.deps().storage
|
||||
}
|
||||
|
||||
pub fn storage_mut(&mut self) -> &mut dyn Storage {
|
||||
self.deps_mut().storage
|
||||
}
|
||||
|
||||
pub fn env(&self) -> Env {
|
||||
self.env.clone()
|
||||
}
|
||||
@@ -470,6 +482,10 @@ pub mod test_helpers {
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
pub fn admin_sender(&self) -> MessageInfo {
|
||||
mock_info(self.admin().as_ref(), &[])
|
||||
}
|
||||
|
||||
pub fn owner(&self) -> MessageInfo {
|
||||
self.owner.clone()
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
```sh
|
||||
|
||||
Binary Name: nym-api
|
||||
Build Timestamp: 2024-12-18T10:26:40.905460408Z
|
||||
Build Timestamp: 2024-12-10T11:37:07.839071360Z
|
||||
Build Version: 1.1.46
|
||||
Commit SHA: 62045d76b32265f6a5e6358ab7aebbc827d47dcd
|
||||
Commit Date: 2024-11-26T11:53:05.000000000+01:00
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
```sh
|
||||
|
||||
Binary Name: nym-client
|
||||
Build Timestamp: 2024-12-18T10:26:40.905460408Z
|
||||
Build Timestamp: 2024-12-10T11:37:07.839071360Z
|
||||
Build Version: 1.1.44
|
||||
Commit SHA: 62045d76b32265f6a5e6358ab7aebbc827d47dcd
|
||||
Commit Date: 2024-11-26T11:53:05.000000000+01:00
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
```sh
|
||||
|
||||
Binary Name: nym-node
|
||||
Build Timestamp: 2024-12-18T10:26:40.905460408Z
|
||||
Build Timestamp: 2024-12-10T11:37:07.839071360Z
|
||||
Build Version: 1.1.11
|
||||
Commit SHA: 62045d76b32265f6a5e6358ab7aebbc827d47dcd
|
||||
Commit Date: 2024-11-26T11:53:05.000000000+01:00
|
||||
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
```sh
|
||||
|
||||
Binary Name: nym-socks5-client
|
||||
Build Timestamp: 2024-12-18T10:26:40.905460408Z
|
||||
Build Timestamp: 2024-12-10T11:37:07.839071360Z
|
||||
Build Version: 1.1.44
|
||||
Commit SHA: 62045d76b32265f6a5e6358ab7aebbc827d47dcd
|
||||
Commit Date: 2024-11-26T11:53:05.000000000+01:00
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
```sh
|
||||
|
||||
Binary Name: nymvisor
|
||||
Build Timestamp: 2024-12-18T10:26:40.905460408Z
|
||||
Build Timestamp: 2024-12-10T11:37:07.839071360Z
|
||||
Build Version: 0.1.9
|
||||
Commit SHA: 62045d76b32265f6a5e6358ab7aebbc827d47dcd
|
||||
Commit Date: 2024-11-26T11:53:05.000000000+01:00
|
||||
|
||||
@@ -152,7 +152,7 @@ Example output:
|
||||
```sh
|
||||
|
||||
Binary Name: nym-socks5-client
|
||||
Build Timestamp: 2024-12-18T10:26:40.905460408Z
|
||||
Build Timestamp: 2024-12-10T11:37:07.839071360Z
|
||||
Build Version: 1.1.44
|
||||
Commit SHA: 62045d76b32265f6a5e6358ab7aebbc827d47dcd
|
||||
Commit Date: 2024-11-26T11:53:05.000000000+01:00
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user