Skip to content

Commit 78981d2

Browse files
authored
Merge pull request #1332 from input-output-hk/ensemble/1311/redesign_mithril_client_api
Redesign mithril client api
2 parents b0177e5 + 76347c5 commit 78981d2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+3559
-231
lines changed

.github/workflows/ci.yml

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,17 @@ jobs:
3636
# We separate the build in 2 steps as we want to avoid side effects with Rust feature unification.
3737
- name: Cargo build - Tooling
3838
shell: bash
39-
run: cargo build --features portable --release --workspace --exclude mithril-aggregator --exclude mithril-client-cli --exclude mithril-signer --exclude mithril-stm
39+
run: |
40+
cargo build --features portable --release --workspace \
41+
--exclude mithril-aggregator --exclude mithril-client-cli \
42+
--exclude mithril-client --exclude mithril-signer \
43+
--exclude mithril-stm
4044
4145
- name: Build Mithril workspace & publish artifacts
4246
uses: ./.github/workflows/actions/build-upload-mithril-artifact
4347
with:
44-
binaries-build-args: --bin mithril-aggregator --bin mithril-signer --bin mithril-client --features build-binary
45-
libraries-build-args: --package mithril-stm --package mithril-client-cli
46-
common-build-args: --features bundle_openssl
48+
binaries-build-args: --bin mithril-aggregator --bin mithril-signer --bin mithril-client --features bundle_openssl
49+
libraries-build-args: --package mithril-stm --package mithril-client
4750

4851
- name: Build Debian packages
4952
shell: bash
@@ -82,15 +85,13 @@ jobs:
8285
include:
8386
# Only build client on windows & mac
8487
- os: macos-12
85-
binaries-build-args: --bin mithril-client --features build-binary
86-
libraries-build-args: --package mithril-stm --package mithril-client-cli
87-
common-build-args: --features bundle_openssl
88+
binaries-build-args: --bin mithril-client --features bundle_openssl
89+
libraries-build-args: --package mithril-stm --package mithril-client
8890
- os: windows-latest
8991
# Use `--bins --package <package>` instead of `--bin <package>`, otherwise the 'windows' compatibility
9092
# hack in mithril common cargo.toml doesn't apply (we have no idea why).
91-
binaries-build-args: --bins --package mithril-client-cli --features build-binary
92-
libraries-build-args: --package mithril-stm --package mithril-client-cli --no-default-features --features num-integer-backend
93-
common-build-args: --features bundle_openssl
93+
binaries-build-args: --bins --package mithril-client-cli --features bundle_openssl
94+
libraries-build-args: --package mithril-stm --package mithril-client --no-default-features --features num-integer-backend
9495
runs-on: ${{ matrix.os }}
9596

9697
steps:
@@ -120,9 +121,9 @@ jobs:
120121
test-args: --features portable --workspace
121122
# Only test client on windows & mac (since its the only binaries supported for those os for now)
122123
- os: macos-12
123-
test-args: -p mithril-client-cli
124+
test-args: --package mithril-client --package mithril-client-cli
124125
- os: windows-latest
125-
test-args: -p mithril-client-cli
126+
test-args: --package mithril-client --package mithril-client-cli
126127

127128
runs-on: ${{ matrix.os }}
128129

@@ -144,7 +145,10 @@ jobs:
144145

145146
- name: Run doc tests
146147
run: cargo test --doc ${{ matrix.test-args }}
147-
148+
149+
- name: Ensure examples build
150+
run: cargo build --examples ${{ matrix.test-args }}
151+
148152
- name: Rename junit file to include runner info
149153
shell: bash
150154
if: success() || failure()

.github/workflows/docs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ jobs:
2727
- name: Generate cargo doc
2828
run: |
2929
cargo doc --no-deps -p mithril-stm -p mithril-common -p mithril-aggregator \
30-
-p mithril-signer -p mithril-client-cli --message-format=json \
30+
-p mithril-signer -p mithril-client -p mithril-client-cli --message-format=json \
3131
| clippy-sarif | tee rust-cargo-doc-results.sarif | sarif-fmt
3232
3333
# Update tool sarif metadata from "clippy" to "cargo-doc" (since it's set this way by clippy-sarif)

Cargo.lock

Lines changed: 34 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ resolver = "2"
55
members = [
66
"demo/protocol-demo",
77
"mithril-aggregator",
8+
"mithril-client",
89
"mithril-client-cli",
910
"mithril-common",
1011
"mithril-signer",

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
COMPONENTS = mithril-common mithril-stm mithril-aggregator mithril-client-cli mithril-signer demo/protocol-demo mithril-test-lab/mithril-end-to-end
1+
COMPONENTS = mithril-common mithril-stm mithril-aggregator mithril-client mithril-client-cli mithril-signer demo/protocol-demo mithril-test-lab/mithril-end-to-end
22
GOALS := $(or $(MAKECMDGOALS),all)
33

44
.PHONY: $(GOALS) $(COMPONENTS)

docs/website/docusaurus.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ const config = {
207207
prism: {
208208
theme: lightCodeTheme,
209209
darkTheme: darkCodeTheme,
210+
additionalLanguages: ['rust', 'toml'],
210211
},
211212
mermaid: {
212213
theme: { light: 'base', dark: 'base' },

docs/website/root/manual/developer-docs/nodes/mithril-aggregator.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Mithril aggregator is responsible for collecting individual signatures from the
3131

3232
| Node | Source repository | Rust documentation | Docker packages | REST API
3333
|:-:|:-----------------:|:------------------:|:---------------:|
34-
**Mithril aggregator** | [:arrow_upper_right:](https://github.com/input-output-hk/mithril/tree/main/mithril-aggregator) | [:arrow_upper_right:](https://mithril.network/mithril-aggregator/doc/mithril_aggregator/index.html) | [:arrow_upper_right:](https://github.com/input-output-hk/mithril/pkgs/container/mithril-aggregator) | [:arrow_upper_right:](/aggregator-api)
34+
**Mithril aggregator** | [:arrow_upper_right:](https://github.com/input-output-hk/mithril/tree/main/mithril-aggregator) | [:arrow_upper_right:](https://mithril.network/rust-doc/mithril_aggregator/index.html) | [:arrow_upper_right:](https://github.com/input-output-hk/mithril/pkgs/container/mithril-aggregator) | [:arrow_upper_right:](/aggregator-api)
3535

3636
## Pre-requisites
3737

docs/website/root/manual/developer-docs/nodes/mithril-client-library.md

Lines changed: 95 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ import CompiledBinaries from '../../../compiled-binaries.md'
99

1010
:::info
1111

12-
Mithril Client Library can be used by Rust developers to use the Mithril Network in their applications.
12+
Mithril client library can be used by Rust developers to use the Mithril network in their applications.
13+
14+
It is responsible for handling the different types of data certified by Mithril, and available through a Mithril aggregator:
15+
- [**Snapshot**](../../../glossary.md#snapshot): list, get and download tarball.
16+
- [**Mithril stake distribution**](../../../glossary#stake-distribution): list and get.
17+
- [**Certificate**](../../../glossary#certificate): list, get, and chain validation.
1318

1419
:::
1520

@@ -31,15 +36,9 @@ Mithril Client Library can be used by Rust developers to use the Mithril Network
3136

3237
## Resources
3338

34-
| Node | Source repository | Rust documentation | Docker packages |
35-
|:-:|:-----------------:|:------------------:|:---------------:|
36-
**Mithril client** | [:arrow_upper_right:](https://github.com/input-output-hk/mithril/tree/main/mithril-client-cli) | [:arrow_upper_right:](https://mithril.network/mithril-client/doc/mithril_client/index.html) | [:arrow_upper_right:](https://github.com/input-output-hk/mithril/pkgs/container/mithril-client)
37-
38-
## Resources
39-
40-
| Node | Source repository | Rust documentation | Docker packages |
41-
|:-:|:-----------------:|:------------------:|:---------------:|
42-
**Mithril client** | [:arrow_upper_right:](https://github.com/input-output-hk/mithril/tree/main/mithril-client-cli) | [:arrow_upper_right:](https://mithril.network/mithril-client/doc/mithril_client/index.html) | [:arrow_upper_right:](https://github.com/input-output-hk/mithril/pkgs/container/mithril-client)
39+
| Node | Source repository | Rust documentation |
40+
|:-:|:-----------------:|:------------------:|
41+
**Mithril client** | [:arrow_upper_right:](https://github.com/input-output-hk/mithril/tree/main/mithril-client) | [:arrow_upper_right:](https://mithril.network/mithril-client/doc/mithril_client/index.html) |
4342

4443
## Pre-requisites
4544

@@ -49,56 +48,112 @@ Mithril Client Library can be used by Rust developers to use the Mithril Network
4948

5049
## Installation
5150

52-
In your project crate, use `cargo` to add [mithril-client](https://crates.io/crates/mithril-client) as a dependency:
51+
:::caution
52+
53+
Mithril client library has not yet been published on [crates.io](https://crates.io/), so you won't be able to follow the procedure below. The crate will be published soon.
54+
55+
For now, you can experiment it by adding the dependency manually in the Cargo.toml of your project:
56+
57+
```toml title="/Cargo.toml"
58+
mithril-client = { git = "https://github.com/input-output-hk/mithril.git" }
59+
```
60+
61+
:::
62+
63+
In your project, use `cargo` to add [mithril-client](https://crates.io/crates/mithril-client) crate as a dependency:
5364

5465
```bash
5566
cargo add mithril-client
5667
```
5768

58-
It will add the latest version of the Mithril client library.
69+
:::info
5970

60-
Mithril client is an asynchronous library. It has been tested with the crate [tokio](https://crates.io/crates/tokio), In the Cargo.toml of your project, add the following dependency:
71+
Mithril client is an asynchronous library, you will need a runtime to execute your futures. We recommend to use the crate [tokio](https://crates.io/crates/tokio), as the library has been tested with it.
6172

62-
```toml
63-
tokio = { version = "1.32.0", features = ["full"] }
64-
``````
73+
:::
6574

66-
## Using Mithril Client Library
75+
## Using Mithril client library
6776

68-
If the goal is just to use the existing certificates, it is easier to use the `Client` structure:
77+
Below is a basic example of how to use most of the functions exposed by the Mithril client library:
6978

70-
```rust
71-
use mithril_client::client::Client;
72-
use mithril_client::common::*;
79+
```rust title="/src/main.rs"
80+
use mithril_client::{ClientBuilder, MessageBuilder};
81+
use std::path::Path;
7382

7483
#[tokio::main]
75-
async fn main() -> StdResult<()> {
76-
let client = Client::new("YOUR_AGGREGATOR_ENDPOINT", "YOUR_GENESIS_VERIFICATION_KEY").await?;
77-
let response = client.list_mithril_stake_distributions().await?;
78-
79-
for mithril_stake_distribution in response {
80-
println!("Stake distribution hash = '{}'.", mithril_stake_distribution.hash);
81-
}
82-
84+
async fn main() -> mithril_client::MithrilResult<()> {
85+
let client = ClientBuilder::aggregator("YOUR_AGGREGATOR_ENDPOINT", "YOUR_GENESIS_VERIFICATION_KEY").build()?;
86+
87+
let snapshots = client.snapshot().list().await?;
88+
89+
let last_digest = snapshots.first().unwrap().digest.as_ref();
90+
let snapshot = client.snapshot().get(last_digest).await?.unwrap();
91+
92+
let certificate = client
93+
.certificate()
94+
.verify_chain(&snapshot.certificate_hash)
95+
.await?;
96+
97+
// Note: the directory must already exist, and the user running this code must have read/write access to it.
98+
let target_directory = Path::new("YOUR_TARGET_DIRECTORY");
99+
client
100+
.snapshot()
101+
.download_unpack(&snapshot, target_directory)
102+
.await?;
103+
104+
let message = MessageBuilder::new()
105+
.compute_snapshot_message(&certificate, target_directory)
106+
.await?;
107+
assert!(certificate.match_message(&message));
108+
83109
Ok(())
84110
}
85111
```
86112

87-
Here is an example of the code for the release-preprod network:
113+
:::info
88114

89-
```rust
90-
use mithril_client::client::Client;
91-
use mithril_client::common::*;
115+
Snapshot download and certificate chain validation can take quite some time even with a fast computer and network. We have implemented a feedback mechanism for them, more details on it are available in the [feedback sub-module](https://mithril.network/rust-doc/mithril_client/feedback/index.html).
92116

93-
#[tokio::main]
94-
async fn main() -> StdResult<()> {
95-
let client = Client::new("https://aggregator.release-mainnet.api.mithril.network/aggregator", "5b3132372c37332c3132342c3136312c362c3133372c3133312c3231332c3230372c3131372c3139382c38352c3137362c3139392c3136322c3234312c36382c3132332c3131392c3134352c31332c3233322c3234332c34392c3232392c322c3234392c3230352c3230352c33392c3233352c34345d").await?;
96-
let response = client.list_mithril_stake_distributions().await?;
117+
An example of implementation with the crate [indicatif](https://crates.io/crates/indicatif) is available in the [Mithril repository](https://github.com/input-output-hk/mithril/tree/main/mithril-client/examples/snapshot_list_get_show_download_verify.rs). To run it, execute the following command:
118+
119+
```bash
120+
cargo run --example snapshot_list_get_show_download_verify
121+
```
122+
123+
:::
124+
125+
Here is a working example of the code using the configuration parameters of the `release-preprod` network:
97126

98-
for mithril_stake_distribution in response {
99-
println!("Stake distribution hash = '{}'.", mithril_stake_distribution.hash);
100-
}
127+
```rust title="/src/main.rs"
128+
use mithril_client::{ClientBuilder, MessageBuilder};
129+
use std::path::Path;
101130

131+
#[tokio::main]
132+
async fn main() -> mithril_client::MithrilResult<()> {
133+
let client = ClientBuilder::aggregator("https://aggregator.release-preprod.api.mithril.network/aggregator", "5b3132372c37332c3132342c3136312c362c3133372c3133312c3231332c3230372c3131372c3139382c38352c3137362c3139392c3136322c3234312c36382c3132332c3131392c3134352c31332c3233322c3234332c34392c3232392c322c3234392c3230352c3230352c33392c3233352c34345d").build()?;
134+
135+
let snapshots = client.snapshot().list().await?;
136+
137+
let last_digest = snapshots.first().unwrap().digest.as_ref();
138+
let snapshot = client.snapshot().get(last_digest).await?.unwrap();
139+
140+
let certificate = client
141+
.certificate()
142+
.verify_chain(&snapshot.certificate_hash)
143+
.await?;
144+
145+
// Note: the directory must already exist, and the user running this code must have read/write access to it.
146+
let target_directory = Path::new(".");
147+
client
148+
.snapshot()
149+
.download_unpack(&snapshot, target_directory)
150+
.await?;
151+
152+
let message = MessageBuilder::new()
153+
.compute_snapshot_message(&certificate, target_directory)
154+
.await?;
155+
assert!(certificate.match_message(&message));
156+
102157
Ok(())
103158
}
104159
```

docs/website/root/manual/developer-docs/nodes/mithril-client.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Mithril client is responsible for restoring the **Cardano** blockchain on an emp
3434

3535
| Node | Source repository | Rust documentation | Docker packages |
3636
|:-:|:-----------------:|:------------------:|:---------------:|
37-
**Mithril client** | [:arrow_upper_right:](https://github.com/input-output-hk/mithril/tree/main/mithril-client-cli) | [:arrow_upper_right:](https://mithril.network/mithril-client/doc/mithril_client/index.html) | [:arrow_upper_right:](https://github.com/input-output-hk/mithril/pkgs/container/mithril-client)
37+
**Mithril client CLI** | [:arrow_upper_right:](https://github.com/input-output-hk/mithril/tree/main/mithril-client-cli) | [:arrow_upper_right:](https://mithril.network/rust-doc/mithril_client_cli/index.html) | [:arrow_upper_right:](https://github.com/input-output-hk/mithril/pkgs/container/mithril-client)
3838

3939
## Pre-requisites
4040

@@ -113,15 +113,15 @@ make build
113113
Display the help menu:
114114

115115
```bash
116-
./mithril-client --help
116+
./mithril-client-cli --help
117117
```
118118

119119
You should see:
120120

121121
```bash
122122
This program shows, downloads, and verifies certified blockchain artifacts.
123123

124-
Usage: mithril-client [OPTIONS] <COMMAND>
124+
Usage: mithril-client-cli [OPTIONS] <COMMAND>
125125

126126
Commands:
127127
snapshot Snapshot commands
@@ -148,13 +148,13 @@ Options:
148148
Run in release mode with the default configuration:
149149

150150
```bash
151-
./mithril-client
151+
./mithril-client-cli
152152
```
153153

154154
Run in release mode with a specific mode:
155155

156156
```bash
157-
./mithril-client --run-mode preview
157+
./mithril-client-cli --run-mode preview
158158
```
159159

160160
Run in release mode with a custom configuration using environment variables:
@@ -168,7 +168,7 @@ GENESIS_VERIFICATION_KEY=$(wget -q -O - **YOUR_GENESIS_VERIFICATION_KEY**) NETWO
168168
To display results in JSON format for the `list` and `show` commands, simply use the `--json` (or `-j`) option:
169169

170170
```bash
171-
./mithril-client snapshot list --json
171+
./mithril-client-cli snapshot list --json
172172
```
173173

174174
:::

0 commit comments

Comments
 (0)