partner_chains_data_sources_cli/
main.rs1use authority_selection_inherents::authority_selection_inputs::AuthoritySelectionDataSource;
2use clap::Parser;
3use partner_chains_db_sync_data_sources::{BlockDataSourceImpl, CandidatesDataSourceImpl, PgPool};
4use sidechain_domain::*;
5use sp_timestamp::Timestamp;
6use std::error::Error;
7
8type Result<T> = std::result::Result<T, Box<dyn Error + Send + Sync>>;
9
10#[tokio::main]
11async fn main() {
12 env_logger::builder().filter_level(log::LevelFilter::Info).init();
13 match Command::parse().run().await {
14 Ok(resp) => println!("{resp}"),
15 Err(err) => log::error!("{}", err.to_string()),
16 }
17}
18
19pub struct DataSources {
20 pub block: BlockDataSourceImpl,
21 pub candidate: CandidatesDataSourceImpl,
22}
23
24macro_rules! follower_commands {
25 (
26 $(
27 $ds:ident {
28 $(
29 async fn $method:ident($($i:literal $arg:ident : $arg_ty:ty),*);
30 )+
31 }
32 )*
33 ) => {
34 #[derive(Debug, clap::Parser)]
35 #[allow(non_camel_case_types)]
36 pub enum Command {
37 $(
38 $(
39 $method {
40 $(
41 #[clap(index = $i)]
42 $arg: $arg_ty
43 ),*
44 }
45 ),*
46 ),*
47 }
48 impl Command {
49 pub async fn run(self) -> Result<String> {
50 match self {
51 $(
52 $(
53 Command::$method { $($arg),* } => {
54 let data_source = crate::data_source::$ds().await.expect("Failed to create data source");
55 let result = data_source.$method($($arg),*).await?;
56 let result = serde_json::to_string_pretty(&result)?;
57 Ok(result)
58 }
59 ),*
60 ),*
61 }
62 }
63 }
64 };
65}
66
67follower_commands! {
68 block {
69 async fn get_latest_block_info();
70 async fn get_latest_stable_block_for(1 reference_timestamp: u64);
71 async fn get_stable_block_for(1 hash: McBlockHash, 2 reference_timestamp: u64);
72 }
73 candidate {
74 async fn get_ariadne_parameters(1 epoch_number: McEpochNumber, 2 d_parameter_policy: PolicyId, 3 permissioned_candidates_policy: PolicyId);
75 async fn get_candidates(1 epoch_number: McEpochNumber, 2 committee_candidate_validator: MainchainAddress);
76 async fn get_epoch_nonce(1 epoch_number: McEpochNumber);
77 }
78}
79
80mod data_source {
81
82 use super::*;
83
84 async fn pool() -> Result<PgPool> {
85 partner_chains_db_sync_data_sources::get_connection_from_env().await
86 }
87
88 pub struct BlockDataSourceWrapper {
89 inner: BlockDataSourceImpl,
90 }
91
92 impl BlockDataSourceWrapper {
93 pub async fn get_latest_block_info(&self) -> Result<MainchainBlock> {
94 self.inner.get_latest_block_info().await
95 }
96
97 pub async fn get_latest_stable_block_for(
98 &self,
99 reference_timestamp: u64,
100 ) -> Result<Option<MainchainBlock>> {
101 self.inner
102 .get_latest_stable_block_for(Timestamp::new(reference_timestamp))
103 .await
104 }
105
106 pub async fn get_stable_block_for(
107 &self,
108 hash: McBlockHash,
109 reference_timestamp: u64,
110 ) -> Result<Option<MainchainBlock>> {
111 self.inner.get_stable_block_for(hash, Timestamp::new(reference_timestamp)).await
112 }
113 }
114
115 pub async fn block() -> Result<BlockDataSourceWrapper> {
116 Ok(BlockDataSourceWrapper {
117 inner: BlockDataSourceImpl::new_from_env(pool().await?).await?,
118 })
119 }
120
121 pub async fn candidate() -> Result<CandidatesDataSourceImpl> {
122 CandidatesDataSourceImpl::new(pool().await?, None).await
123 }
124}