From 7afe8b89e6138906776e4fc7a4e6d0661d094017 Mon Sep 17 00:00:00 2001 From: Nikolaos Dymitriadis Date: Tue, 10 Dec 2024 11:30:42 +0100 Subject: [PATCH] use rust offchain upsert-permissioned-candidates in wizard Signed-off-by: Nikolaos Dymitriadis --- .../src/permissioned_candidates.rs | 10 --- .../src/setup_main_chain_state/mod.rs | 70 +++++++------------ toolkit/primitives/domain/src/lib.rs | 20 +++++- 3 files changed, 45 insertions(+), 55 deletions(-) diff --git a/toolkit/partner-chains-cli/src/permissioned_candidates.rs b/toolkit/partner-chains-cli/src/permissioned_candidates.rs index 8ab1eaafd..90d73910a 100644 --- a/toolkit/partner-chains-cli/src/permissioned_candidates.rs +++ b/toolkit/partner-chains-cli/src/permissioned_candidates.rs @@ -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 { diff --git a/toolkit/partner-chains-cli/src/setup_main_chain_state/mod.rs b/toolkit/partner-chains-cli/src/setup_main_chain_state/mod.rs index 084890ad1..40e3d194f 100644 --- a/toolkit/partner-chains-cli/src/setup_main_chain_state/mod.rs +++ b/toolkit/partner-chains-cli/src/setup_main_chain_state/mod.rs @@ -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; @@ -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 for ParsedPermissionedCandidatesKeys { type Error = anyhow::Error; @@ -84,6 +72,17 @@ impl SortedPermissionedCandidates { keys.sort(); Self(keys) } + + pub fn to_candidate_data(&self) -> Vec { + 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 { @@ -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={})", @@ -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( @@ -247,39 +246,22 @@ fn print_on_chain_and_config_permissioned_candidates( } } -fn set_candidates_on_main_chain( +async fn set_candidates_on_main_chain( 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::>() - .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."); } diff --git a/toolkit/primitives/domain/src/lib.rs b/toolkit/primitives/domain/src/lib.rs index effceaf31..fc6a95174 100644 --- a/toolkit/primitives/domain/src/lib.rs +++ b/toolkit/primitives/domain/src/lib.rs @@ -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, @@ -298,6 +298,12 @@ impl ScEpochNumber { #[byte_string(debug, hex_serialize, hex_deserialize, as_ref)] pub struct SidechainPublicKey(pub Vec); +impl From 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); @@ -595,6 +601,12 @@ impl AuraPublicKey { } } +impl From 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); @@ -604,6 +616,12 @@ impl GrandpaPublicKey { } } +impl From 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 {