Skip to content

Commit

Permalink
add upsert-d-param to offchain trait bounds, add mock
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 9959afa commit 587b864
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 5 deletions.
3 changes: 2 additions & 1 deletion toolkit/partner-chains-cli/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use anyhow::{anyhow, Context};
use jsonrpsee::http_client::HttpClient;
use partner_chains_cardano_offchain::d_param::UpsertDParam;
use partner_chains_cardano_offchain::init_governance::InitGovernance;
use partner_chains_cardano_offchain::permissioned_candidates::UpsertPermissionedCandidates;
use partner_chains_cardano_offchain::scripts_data::GetScriptsData;
use sp_core::offchain::Timestamp;
use std::path::PathBuf;
Expand All @@ -16,7 +17,7 @@ use tempfile::{TempDir, TempPath};

pub trait IOContext {
/// It should implement all the required traits for offchain operations
type Offchain: GetScriptsData + InitGovernance + UpsertDParam;
type Offchain: GetScriptsData + InitGovernance + UpsertDParam + UpsertPermissionedCandidates;

fn run_command(&self, cmd: &str) -> anyhow::Result<String>;
fn print(&self, msg: &str);
Expand Down
39 changes: 39 additions & 0 deletions toolkit/partner-chains-cli/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use anyhow::anyhow;
use ogmios_client::types::OgmiosTx;
use partner_chains_cardano_offchain::d_param::UpsertDParam;
use partner_chains_cardano_offchain::init_governance::InitGovernance;
use partner_chains_cardano_offchain::permissioned_candidates::UpsertPermissionedCandidates;
use partner_chains_cardano_offchain::scripts_data::{GetScriptsData, ScriptsData};
use partner_chains_cardano_offchain::OffchainError;
use pretty_assertions::assert_eq;
Expand Down Expand Up @@ -256,6 +257,10 @@ pub struct OffchainMock {
Result<OgmiosTx, OffchainError>,
>,
pub upsert_d_param: HashMap<(UtxoId, DParameter, [u8; 32]), Result<Option<McTxHash>, String>>,
pub upsert_permissioned_candidates: HashMap<
(UtxoId, Vec<sidechain_domain::PermissionedCandidateData>, [u8; 32]),
Result<Option<McTxHash>, String>,
>,
}

impl OffchainMock {
Expand Down Expand Up @@ -295,6 +300,23 @@ impl OffchainMock {
) -> Self {
Self { upsert_d_param: [((genesis_utxo, d_param, payment_key.0), result)].into(), ..self }
}

pub(crate) fn with_upsert_permissioned_candidates(
self,
genesis_utxo: UtxoId,
candidates: &[sidechain_domain::PermissionedCandidateData],
payment_key: MainchainPrivateKey,
result: Result<Option<McTxHash>, String>,
) -> Self {
Self {
upsert_permissioned_candidates: [(
(genesis_utxo, candidates.to_vec(), payment_key.0),
result,
)]
.into(),
..self
}
}
}

impl GetScriptsData for OffchainMock {
Expand Down Expand Up @@ -338,6 +360,23 @@ impl UpsertDParam for OffchainMock {
}
}

impl UpsertPermissionedCandidates for OffchainMock {
async fn upsert_permissioned_candidates(
&self,
genesis_utxo: UtxoId,
candidates: &[sidechain_domain::PermissionedCandidateData],
payment_signing_key: [u8; 32],
) -> anyhow::Result<Option<McTxHash>> {
self.upsert_permissioned_candidates
.get(&(genesis_utxo, candidates.to_vec(), payment_signing_key))
.cloned()
.unwrap_or_else(|| {
Err(format!("No mock for upsert_permissioned_candidates({genesis_utxo:?}, {candidates:?}, {payment_signing_key:?})\n defined mocks:{:?}", self.upsert_permissioned_candidates))
})
.map_err(|err| anyhow!("{err}"))
}
}

impl Drop for MockIOContext {
fn drop(&mut self) {
if std::thread::panicking() {
Expand Down
8 changes: 4 additions & 4 deletions toolkit/primitives/domain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ impl ScEpochNumber {
}
}

#[derive(Clone, PartialEq, Eq, Encode, Decode, ToDatum, TypeInfo, PartialOrd, Ord)]
#[derive(Clone, PartialEq, Eq, Encode, Decode, ToDatum, TypeInfo, PartialOrd, Ord, Hash)]
#[byte_string(debug, hex_serialize, hex_deserialize, as_ref)]
pub struct SidechainPublicKey(pub Vec<u8>);

Expand Down Expand Up @@ -586,7 +586,7 @@ impl SidechainPublicKeysSorted {
}
}

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

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

#[derive(Debug, Clone, PartialEq, Eq, Decode, Encode, TypeInfo, PartialOrd, Ord)]
#[derive(Debug, Clone, PartialEq, Eq, Decode, Encode, TypeInfo, PartialOrd, Ord, Hash)]
pub struct PermissionedCandidateData {
pub sidechain_public_key: SidechainPublicKey,
pub aura_public_key: AuraPublicKey,
Expand Down

0 comments on commit 587b864

Please sign in to comment.