-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1889 from input-output-hk/dlachaume/1844/document…
…-cardano-stake-distribution Document Cardano Stake Distribution
- Loading branch information
Showing
13 changed files
with
222 additions
and
4 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
target/ | ||
client-cardano-stake-distribution | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
[package] | ||
name = "client-cardano-stake-distribution" | ||
description = "Mithril client cardano stake distribution example" | ||
version = "0.1.0" | ||
authors = ["dev@iohk.io", "mithril-dev@iohk.io"] | ||
documentation = "https://mithril.network/doc" | ||
edition = "2021" | ||
homepage = "https://mithril.network" | ||
license = "Apache-2.0" | ||
repository = "https://github.com/input-output-hk/mithril/" | ||
|
||
[dependencies] | ||
anyhow = "1.0.79" | ||
clap = { version = "4.4.18", features = ["derive", "env"] } | ||
mithril-client = { path = "../../mithril-client", features = ["unstable"] } | ||
slog = "2.7.0" | ||
slog-async = "2.8.0" | ||
slog-term = "2.9.0" | ||
tokio = { version = "1.37.0", features = ["full"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Mithril client library example: Cardano stake distribution | ||
|
||
## Description | ||
|
||
This example shows how to implement a Mithril client and use the features related to the `Cardano stake distribution` type. | ||
|
||
In this example, the client interacts by default with a real aggregator on the network `testing-preview` to: | ||
|
||
- list the available Cardano stake distributions | ||
- get a single Cardano stake distribution | ||
- verify a certificate chain | ||
- compute a message for a Cardano stake distribution | ||
- verify that the certificate signs the computed message | ||
|
||
The crates [`slog`](https://docs.rs/slog/latest/slog/) and [`slog_term`](https://docs.rs/slog-term/latest/slog_term/) are used to report the progress to the console. | ||
|
||
## Build and run the example | ||
|
||
```bash | ||
# Build from the crate directory | ||
cargo build | ||
|
||
# Run from the crate directory | ||
cargo run | ||
|
||
# Run with your custom network configuration | ||
AGGREGATOR_ENDPOINT=YOUR_AGGREGATOR_ENDPOINT GENESIS_VERIFICATION_KEY=YOUR_GENESIS_VERIFICATION_KEY cargo run | ||
|
||
# Example with 'pre-release-preview' network | ||
AGGREGATOR_ENDPOINT=https://aggregator.pre-release-preview.api.mithril.network/aggregator GENESIS_VERIFICATION_KEY=$(curl -s https://raw.githubusercontent.com/input-output-hk/mithril/main/mithril-infra/configuration/pre-release-preview/genesis.vkey) cargo run | ||
``` | ||
|
||
## Links | ||
|
||
- **Developer documentation**: https://docs.rs/mithril-client/latest/mithril_client/ | ||
- **Crates.io**: https://crates.io/crates/mithril-client |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
//! This example shows how to implement a Mithril client and use its features. | ||
//! | ||
//! In this example, the client interacts by default with a real aggregator (`testing-preview`) to get the data. | ||
use anyhow::anyhow; | ||
use clap::Parser; | ||
use slog::info; | ||
use std::sync::Arc; | ||
|
||
use mithril_client::{ClientBuilder, MessageBuilder, MithrilResult}; | ||
|
||
#[derive(Parser, Debug)] | ||
#[command(version)] | ||
pub struct Args { | ||
/// Genesis verification key. | ||
#[clap( | ||
long, | ||
env = "GENESIS_VERIFICATION_KEY", | ||
default_value = "5b3132372c37332c3132342c3136312c362c3133372c3133312c3231332c3230372c3131372c3139382c38352c3137362c3139392c3136322c3234312c36382c3132332c3131392c3134352c31332c3233322c3234332c34392c3232392c322c3234392c3230352c3230352c33392c3233352c34345d" | ||
)] | ||
genesis_verification_key: String, | ||
|
||
/// Aggregator endpoint URL. | ||
#[clap( | ||
long, | ||
env = "AGGREGATOR_ENDPOINT", | ||
default_value = "https://aggregator.testing-preview.api.mithril.network/aggregator" | ||
)] | ||
aggregator_endpoint: String, | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() -> MithrilResult<()> { | ||
let args = Args::parse(); | ||
let logger = build_logger(); | ||
let client = | ||
ClientBuilder::aggregator(&args.aggregator_endpoint, &args.genesis_verification_key) | ||
.with_logger(logger.clone()) | ||
.build()?; | ||
|
||
let cardano_stake_distributions = client.cardano_stake_distribution().list().await?; | ||
info!( | ||
logger, | ||
"Fetched Cardano stake distributions:\n{cardano_stake_distributions:#?}", | ||
); | ||
|
||
let last_epoch = cardano_stake_distributions | ||
.first() | ||
.ok_or(anyhow!( | ||
"No Cardano stake distributions could be listed from aggregator: '{}'", | ||
args.aggregator_endpoint | ||
))? | ||
.epoch; | ||
|
||
let cardano_stake_distribution = client | ||
.cardano_stake_distribution() | ||
.get_by_epoch(last_epoch) | ||
.await? | ||
.ok_or(anyhow!( | ||
"A Cardano stake distribution should exist for hash '{last_epoch}'" | ||
))?; | ||
info!( | ||
logger, | ||
"Fetched details of last Cardano stake distribution:\n{cardano_stake_distribution:#?}", | ||
); | ||
|
||
info!( | ||
logger, | ||
"Checking certificate chain of the last Cardano stake distribution ...", | ||
); | ||
let certificate = client | ||
.certificate() | ||
.verify_chain(&cardano_stake_distribution.certificate_hash) | ||
.await?; | ||
info!( | ||
logger, | ||
"Certificate chain is valid, checking that the last certificate, hash '{}', \ | ||
effectively matches Cardano stake distribution '{}'", | ||
certificate.hash, | ||
cardano_stake_distribution.hash | ||
); | ||
|
||
let message = MessageBuilder::new() | ||
.compute_cardano_stake_distribution_message(&certificate, &cardano_stake_distribution)?; | ||
|
||
if certificate.match_message(&message) { | ||
info!( | ||
logger, | ||
"Certificate '{}' matches Cardano stake distribution '{}'", | ||
certificate.hash, | ||
cardano_stake_distribution.hash | ||
); | ||
Ok(()) | ||
} else { | ||
Err(anyhow::anyhow!( | ||
"Certificate and message did not match:\ncertificate_message: '{}'\n computed_message: '{}'", | ||
certificate.signed_message, | ||
message.compute_hash() | ||
)) | ||
} | ||
} | ||
|
||
fn build_logger() -> slog::Logger { | ||
use slog::Drain; | ||
|
||
let decorator = slog_term::TermDecorator::new().build(); | ||
let drain = slog_term::FullFormat::new(decorator).build().fuse(); | ||
let drain = slog_async::Async::new(drain).build().fuse(); | ||
|
||
slog::Logger::root(Arc::new(drain), slog::o!()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters