partner_chains_smart_contracts_commands/
governance.rs

1use crate::{GenesisUtxo, PaymentFilePath, option_to_json};
2use partner_chains_cardano_offchain::{
3	governance::{MultiSigParameters, get_governance_policy_summary},
4	init_governance::run_init_governance,
5	update_governance::run_update_governance,
6};
7use sidechain_domain::{MainchainKeyHash, UtxoId};
8
9#[derive(Clone, Debug, clap::Subcommand)]
10#[allow(clippy::large_enum_variant)]
11/// Commands for managing the governance of the partner chain
12pub enum GovernanceCmd {
13	/// Initialize Partner Chain governance
14	Init(InitGovernanceCmd),
15	/// Update Partner Chain governance
16	Update(UpdateGovernanceCmd),
17	/// Prints JSON summary of the current governance policy of a chain. Prints null if governance policy has not been set for given genesis utxo.
18	GetPolicy(GetPolicyCmd),
19}
20
21impl GovernanceCmd {
22	/// Executes the internal command
23	pub async fn execute(self) -> crate::SubCmdResult {
24		match self {
25			Self::Init(cmd) => cmd.execute().await,
26			Self::Update(cmd) => cmd.execute().await,
27			Self::GetPolicy(cmd) => cmd.execute().await,
28		}
29	}
30}
31
32#[derive(Clone, Debug, clap::Parser)]
33/// Command for initializing the governance of the partner chain
34pub struct InitGovernanceCmd {
35	#[clap(flatten)]
36	common_arguments: crate::CommonArguments,
37	#[arg(short = 'g', long, num_args = 1.., value_delimiter = ' ')]
38	/// Governance authority to be set, list of hex encoded, space delimited public key hashes, order does not matter
39	governance_authority: Vec<MainchainKeyHash>,
40	#[arg(short = 't', long)]
41	/// Minimum number of authorities required to sign a transaction
42	threshold: u8,
43	#[clap(flatten)]
44	/// Path to the payment key file
45	payment_key_file: PaymentFilePath,
46	/// Genesis UTXO of the new chain, it will be spent durning initialization. If not set, then one will be selected from UTXOs of the payment key.
47	#[arg(long, short = 'c')]
48	genesis_utxo: Option<UtxoId>,
49}
50
51impl InitGovernanceCmd {
52	/// Initializes the governance of the partner chain.
53	pub async fn execute(self) -> crate::SubCmdResult {
54		let payment_key = self.payment_key_file.read_key()?;
55		let client = self.common_arguments.get_ogmios_client().await?;
56
57		let multisig_parameters =
58			MultiSigParameters::new(&self.governance_authority, self.threshold)?;
59
60		let result = run_init_governance(
61			&multisig_parameters,
62			&payment_key,
63			self.genesis_utxo,
64			&client,
65			self.common_arguments.retries(),
66		)
67		.await?;
68		Ok(serde_json::json!(result))
69	}
70}
71
72#[derive(Clone, Debug, clap::Parser)]
73/// Command for updating the governance of the partner chain
74pub struct UpdateGovernanceCmd {
75	#[clap(flatten)]
76	common_arguments: crate::CommonArguments,
77	/// New governance authorities keys hashes, hex encoded, space delimited, order does not matter
78	#[arg(short = 'g', long, num_args = 1.., value_delimiter = ' ')]
79	governance_authority: Vec<MainchainKeyHash>,
80	/// Minimum number of authorities required to sign a transaction
81	#[arg(short = 't', long)]
82	threshold: u8,
83	#[clap(flatten)]
84	/// Path to the payment key file
85	payment_key_file: PaymentFilePath,
86	#[clap(flatten)]
87	/// Genesis UTXO. This has to be the same as the one used during governance initialization.
88	genesis_utxo: GenesisUtxo,
89}
90
91impl UpdateGovernanceCmd {
92	/// Updates the governance of the partner chain.
93	pub async fn execute(self) -> crate::SubCmdResult {
94		let payment_key = self.payment_key_file.read_key()?;
95		let client = self.common_arguments.get_ogmios_client().await?;
96
97		let multisig_parameters =
98			MultiSigParameters::new(&self.governance_authority, self.threshold)?;
99
100		let result = run_update_governance(
101			&multisig_parameters,
102			&payment_key,
103			self.genesis_utxo.into(),
104			&client,
105			self.common_arguments.retries(),
106		)
107		.await?;
108		Ok(serde_json::json!(result))
109	}
110}
111
112#[derive(Clone, Debug, clap::Parser)]
113/// Command for getting the current governance authorities and threshold.
114pub struct GetPolicyCmd {
115	#[clap(flatten)]
116	common_arguments: crate::CommonArguments,
117	#[arg(long, short = 'c')]
118	/// Genesis UTXO that identifies the partner chain.
119	genesis_utxo: UtxoId,
120}
121
122impl GetPolicyCmd {
123	/// Gets the current governance authorities and threshold.
124	pub async fn execute(self) -> crate::SubCmdResult {
125		let client = self.common_arguments.get_ogmios_client().await?;
126		let summary = get_governance_policy_summary(self.genesis_utxo, &client).await?;
127		Ok(option_to_json(summary))
128	}
129}