Skip to content

Commit e97f8e3

Browse files
authored
Merge pull request #263 from input-output-hk/jpraynaud/251-stake-store-signer
Use stake store in Mithril Signer
2 parents 6c58795 + 83d3071 commit e97f8e3

File tree

10 files changed

+132
-21
lines changed

10 files changed

+132
-21
lines changed

mithril-aggregator/.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ target/
22
mithril-aggregator
33
.DS_Store
44
*.tar.gz
5-
snapshots.json
5+
snapshots.json
6+
stores

mithril-infra/docker-compose.yaml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ services:
3535
- URL_SNAPSHOT_MANIFEST=https://storage.googleapis.com/cardano-${NETWORK:-testnet}/snapshots.json
3636
- SNAPSHOT_STORE_TYPE=local
3737
- SNAPSHOT_UPLOADER_TYPE=gcp
38-
- PENDING_CERTIFICATE_STORE_DIRECTORY=/tmp/mithril/pending_cert_db
39-
- CERTIFICATE_STORE_DIRECTORY=/tmp/mithril/cert_db
40-
- VERIFICATION_KEY_STORE_DIRECTORY=/tmp/mithril/verification_key_db
41-
- STAKE_STORE_DIRECTORY=/tmp/mithril/stake_db
38+
- PENDING_CERTIFICATE_STORE_DIRECTORY=/tmp/mithril-aggregator/pending_cert_db
39+
- CERTIFICATE_STORE_DIRECTORY=/tmp/mithril-aggregator/cert_db
40+
- VERIFICATION_KEY_STORE_DIRECTORY=/tmp/mithril-aggregator/verification_key_db
41+
- STAKE_STORE_DIRECTORY=/tmp/mithril-aggregator/stake_db
4242
logging:
4343
driver: "json-file"
4444
options:
@@ -64,6 +64,7 @@ services:
6464
- PARTY_ID=0
6565
- RUN_INTERVAL=60000
6666
- DB_DIRECTORY=/db
67+
- STAKE_STORE_DIRECTORY=/tmp/mithril-signer-0/stake_db
6768
volumes:
6869
- ./testnet/node.db:/db
6970

@@ -81,6 +82,7 @@ services:
8182
- PARTY_ID=1
8283
- RUN_INTERVAL=60000
8384
- DB_DIRECTORY=/db
85+
- STAKE_STORE_DIRECTORY=/tmp/mithril-signer-1/stake_db
8486
volumes:
8587
- ./testnet/node.db:/db
8688

@@ -98,6 +100,7 @@ services:
98100
- PARTY_ID=2
99101
- RUN_INTERVAL=60000
100102
- DB_DIRECTORY=/db
103+
- STAKE_STORE_DIRECTORY=/tmp/mithril-signer-2/stake_db
101104
volumes:
102105
- ./testnet/node.db:/db
103106

@@ -115,6 +118,7 @@ services:
115118
- PARTY_ID=3
116119
- RUN_INTERVAL=60000
117120
- DB_DIRECTORY=/db
121+
- STAKE_STORE_DIRECTORY=/tmp/mithril-signer-3/stake_db
118122
volumes:
119123
- ./testnet/node.db:/db
120124

mithril-signer/.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
data/
22
target/
33
mithril-signer
4-
.DS_Store
4+
.DS_Store
5+
stores

mithril-signer/config/dev.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
"aggregator_endpoint": "http://localhost:8080/aggregator",
44
"party_id": 0,
55
"run_interval": 20000,
6-
"db_directory": "/db"
6+
"db_directory": "/db",
7+
"stake_store_directory": "/tmp/mithril/stake_db"
78
}

mithril-signer/config/testnet.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
"aggregator_endpoint": "http://aggregator.api.mithril.network/aggregator",
44
"party_id": 0,
55
"run_interval": 20000,
6-
"db_directory": "/db"
6+
"db_directory": "/db",
7+
"stake_store_directory": "./mithril/mithril/stake_db"
78
}

mithril-signer/src/certificate_handler.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,8 @@ mod tests {
151151
aggregator_endpoint: server.url(""),
152152
party_id: 0,
153153
run_interval: 100,
154-
db_directory: Path::new("./").to_path_buf(),
154+
db_directory: Path::new("./db").to_path_buf(),
155+
stake_store_directory: Path::new("./stakes").to_path_buf(),
155156
};
156157
(server, config)
157158
}

mithril-signer/src/entities.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ pub struct Config {
2727
display_fn = "display_path"
2828
)]
2929
pub db_directory: PathBuf,
30+
31+
/// Directory to store stakes
32+
#[table(
33+
title = "Path to the stake store directory",
34+
display_fn = "display_path"
35+
)]
36+
pub stake_store_directory: PathBuf,
3037
}
3138

3239
fn display_path(path: &Path) -> Display<'_> {

mithril-signer/src/main.rs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
use clap::Parser;
2-
use mithril_common::digesters::ImmutableDigester;
32
use slog::{o, Drain, Level, Logger};
43
use slog_scope::debug;
54
use std::env;
5+
use std::error::Error;
66
use std::sync::Arc;
7+
use tokio::sync::RwLock;
78

9+
use mithril_common::crypto_helper::ProtocolStakeDistribution;
10+
use mithril_common::digesters::ImmutableDigester;
11+
use mithril_common::fake_data;
12+
use mithril_common::store::adapter::JsonFileStoreAdapter;
13+
use mithril_common::store::stake_store::{StakeStore, StakeStorer};
814
use mithril_signer::{CertificateHandlerHTTPClient, Config, MithrilSingleSigner, Runtime};
915

1016
/// CLI args
@@ -44,7 +50,7 @@ fn build_logger(min_level: Level) -> Logger {
4450
}
4551

4652
#[tokio::main]
47-
async fn main() -> Result<(), String> {
53+
async fn main() -> Result<(), Box<dyn Error>> {
4854
// Load args
4955
let args = Args::parse();
5056
let _guard = slog_scope::set_global_logger(build_logger(args.log_level()));
@@ -65,14 +71,38 @@ async fn main() -> Result<(), String> {
6571
let single_signer = MithrilSingleSigner::new(config.party_id, protocol_initializer_encoded);
6672
let certificate_handler = CertificateHandlerHTTPClient::new(config.aggregator_endpoint.clone());
6773
let digester = ImmutableDigester::new(config.db_directory, slog_scope::logger());
74+
let stake_store = Arc::new(RwLock::new(StakeStore::new(Box::new(
75+
JsonFileStoreAdapter::new(config.stake_store_directory.clone())?,
76+
))));
77+
setup_dependencies_fake_data(stake_store.clone()).await;
6878

6979
// Should the runtime loop returns an error ? If yes should we abort the loop at the first error or is their some tolerance ?
7080
let mut runtime = Runtime::new(
7181
Box::new(certificate_handler),
7282
Box::new(single_signer),
7383
Box::new(digester),
84+
stake_store.clone(),
7485
);
7586
runtime.infinite_loop(config.run_interval).await;
7687

7788
Ok(())
7889
}
90+
91+
/// Setup dependencies with fake data
92+
// TODO: remove this function when new runtime is implemented
93+
async fn setup_dependencies_fake_data(stake_store: Arc<RwLock<dyn StakeStorer>>) {
94+
// Update stake distribution
95+
let mut stake_store = stake_store.write().await;
96+
let total_signers = 5;
97+
let epoch = 0; // TODO: to remove once the runtime feeds the stake distribution
98+
let stakes: ProtocolStakeDistribution = fake_data::signers_with_stakes(total_signers)
99+
.into_iter()
100+
.map(|signer| signer.into())
101+
.collect::<_>();
102+
for stake in stakes {
103+
stake_store
104+
.save_stake(epoch, stake.into())
105+
.await
106+
.expect("fake stake distribution update failed");
107+
}
108+
}

0 commit comments

Comments
 (0)