Skip to content

Commit

Permalink
use rust offchain upsert-permissioned-candidates in wizard
Browse files Browse the repository at this point in the history
Signed-off-by: Nikolaos Dymitriadis <[email protected]>
  • Loading branch information
AmbientTea committed Dec 10, 2024
1 parent 5b122bf commit 882b50c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 55 deletions.
10 changes: 0 additions & 10 deletions toolkit/partner-chains-cli/src/permissioned_candidates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,6 @@ impl ParsedPermissionedCandidatesKeys {
pub fn account_id_32(&self) -> AccountId32 {
sp_runtime::MultiSigner::from(self.sidechain).into_account()
}

/// Outputs smart contracts format of keys: triple of hex strings without 0x prefix and separated by colons
pub fn to_smart_contracts_args_triple(&self) -> String {
format!(
"{}:{}:{}",
hex::encode(self.sidechain.0),
hex::encode(self.aura.0),
hex::encode(self.grandpa.0)
)
}
}

impl TryFrom<&PermissionedCandidateKeys> for ParsedPermissionedCandidatesKeys {
Expand Down
70 changes: 26 additions & 44 deletions toolkit/partner-chains-cli/src/setup_main_chain_state/mod.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
use crate::config::config_fields::{CARDANO_PAYMENT_SIGNING_KEY_FILE, POSTGRES_CONNECTION_STRING};
use crate::config::{
config_fields, get_cardano_network_from_file, ChainConfig, ConfigFieldDefinition,
CHAIN_CONFIG_FILE_PATH, PC_CONTRACTS_CLI_PATH,
config_fields, ChainConfig, ConfigFieldDefinition, CHAIN_CONFIG_FILE_PATH,
PC_CONTRACTS_CLI_PATH,
};
use crate::io::IOContext;
use crate::pc_contracts_cli_resources::{
establish_pc_contracts_cli_configuration, prompt_ogmios_configuration,
};
use crate::pc_contracts_cli_resources::prompt_ogmios_configuration;
use crate::permissioned_candidates::{ParsedPermissionedCandidatesKeys, PermissionedCandidateKeys};
use crate::{cardano_key, smart_contracts, CmdRun};
use crate::{cardano_key, CmdRun};
use anyhow::anyhow;
use anyhow::Context;
use partner_chains_cardano_offchain::d_param::UpsertDParam;
use partner_chains_cardano_offchain::permissioned_candidates::UpsertPermissionedCandidates;
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use sidechain_domain::mainchain_epoch::MainchainEpochDerivation;
Expand Down Expand Up @@ -47,17 +46,6 @@ pub struct PermissionedCandidateData {
pub is_valid: bool,
}

enum InsertOrUpdate {
Insert,
Update,
}

impl InsertOrUpdate {
fn permissioned_candidates_command(&self) -> &'static str {
"update-permissioned-candidates --remove-all-candidates"
}
}

impl TryFrom<PermissionedCandidateData> for ParsedPermissionedCandidatesKeys {
type Error = anyhow::Error;

Expand All @@ -84,6 +72,17 @@ impl SortedPermissionedCandidates {
keys.sort();
Self(keys)
}

pub fn to_candidate_data(&self) -> Vec<sidechain_domain::PermissionedCandidateData> {
self.0
.iter()
.map(|c| sidechain_domain::PermissionedCandidateData {
sidechain_public_key: c.sidechain.into(),
aura_public_key: c.aura.into(),
grandpa_public_key: c.grandpa.into(),
})
.collect()
}
}

impl CmdRun for SetupMainChainStateCmd {
Expand Down Expand Up @@ -119,8 +118,8 @@ impl SetupMainChainStateCmd {
context,
config_initial_authorities,
chain_config.chain_parameters.genesis_utxo,
InsertOrUpdate::Update,
)?;
)
.await?;
}
context.print(&format!(
"D-Parameter on the main chain is: (P={}, R={})",
Expand All @@ -138,8 +137,8 @@ impl SetupMainChainStateCmd {
context,
config_initial_authorities,
chain_config.chain_parameters.genesis_utxo,
InsertOrUpdate::Insert,
)?;
)
.await?;
let default_d_parameter =
DParameter { num_permissioned_candidates: 0, num_registered_candidates: 0 };
set_d_parameter_on_main_chain(
Expand Down Expand Up @@ -247,39 +246,22 @@ fn print_on_chain_and_config_permissioned_candidates<C: IOContext>(
}
}

fn set_candidates_on_main_chain<C: IOContext>(
async fn set_candidates_on_main_chain<C: IOContext>(
context: &C,
candidates: SortedPermissionedCandidates,
genesis_utxo: UtxoId,
insert_or_update: InsertOrUpdate,
) -> anyhow::Result<()> {
let update = context.prompt_yes_no("Do you want to set/update the permissioned candidates on the main chain with values from configuration file?", false);
if update {
let pc_contracts_cli_resources = establish_pc_contracts_cli_configuration(context)?;
let ogmios_config = prompt_ogmios_configuration(context)?;
let payment_signing_key_path =
CARDANO_PAYMENT_SIGNING_KEY_FILE.prompt_with_default_from_file_and_save(context);
let command = insert_or_update.permissioned_candidates_command();
let candidate_keys = candidates
.0
.iter()
.map(|c| format!("--add-candidate {}", c.to_smart_contracts_args_triple()))
.collect::<Vec<_>>()
.join(" ");

let cardano_network = get_cardano_network_from_file(context)?;
let pkey = cardano_key::get_mc_pkey_from_file(&payment_signing_key_path, context)?;

context
.run_command(&format!(
"{PC_CONTRACTS_CLI_PATH} {} --network {} {} {} {}",
command,
cardano_network,
candidate_keys,
smart_contracts::sidechain_params_arguments(genesis_utxo),
smart_contracts::runtime_config_arguments(
&pc_contracts_cli_resources,
&payment_signing_key_path
)
))
.offchain_impl(&ogmios_config)?
.upsert_permissioned_candidates(genesis_utxo, &candidates.to_candidate_data(), pkey.0)
.await
.context("Permissioned candidates update failed")?;
context.print("Permissioned candidates updated. The change will be effective in two main chain epochs.");
}
Expand Down
20 changes: 19 additions & 1 deletion toolkit/primitives/domain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
use plutus::{Datum, ToDatum};
use plutus_datum_derive::*;
use scale_info::TypeInfo;
use sp_core::{bounded::BoundedVec, ed25519, sr25519, ConstU32};
use sp_core::{bounded::BoundedVec, ecdsa, ed25519, sr25519, ConstU32};
#[cfg(feature = "serde")]
use {
derive_more::FromStr,
Expand Down Expand Up @@ -298,6 +298,12 @@ impl ScEpochNumber {
#[byte_string(debug, hex_serialize, hex_deserialize, as_ref)]
pub struct SidechainPublicKey(pub Vec<u8>);

impl From<ecdsa::Public> for SidechainPublicKey {
fn from(value: ecdsa::Public) -> Self {
Self(value.0.to_vec())
}
}

#[derive(Clone, Encode, Decode, TypeInfo, PartialEq, Eq)]
#[byte_string(debug, hex_serialize, hex_deserialize, decode_hex)]
pub struct SidechainSignature(pub Vec<u8>);
Expand Down Expand Up @@ -595,6 +601,12 @@ impl AuraPublicKey {
}
}

impl From<sr25519::Public> for AuraPublicKey {
fn from(value: sr25519::Public) -> Self {
Self(value.0.to_vec())
}
}

#[derive(Clone, PartialEq, Eq, Encode, Decode, TypeInfo, PartialOrd, Ord, Hash)]
#[byte_string(debug, hex_serialize, hex_deserialize)]
pub struct GrandpaPublicKey(pub Vec<u8>);
Expand All @@ -604,6 +616,12 @@ impl GrandpaPublicKey {
}
}

impl From<ed25519::Public> for GrandpaPublicKey {
fn from(value: ed25519::Public) -> Self {
Self(value.0.to_vec())
}
}

#[derive(Debug, Clone, PartialEq, Decode, Encode, MaxEncodedLen, TypeInfo, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct DParameter {
Expand Down

0 comments on commit 882b50c

Please sign in to comment.