Skip to content

Commit

Permalink
fix tests
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 b2969b6 commit 7cc10d5
Showing 1 changed file with 90 additions and 40 deletions.
130 changes: 90 additions & 40 deletions toolkit/partner-chains-cli/src/setup_main_chain_state/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ use crate::tests::{MockIO, MockIOContext, OffchainMock, OffchainMocks};
use crate::CmdRun;
use hex_literal::hex;
use serde_json::json;
use sidechain_domain::{DParameter, MainchainPrivateKey, McTxHash, UtxoId};
use sidechain_domain::{
AuraPublicKey, DParameter, GrandpaPublicKey, MainchainPrivateKey, McTxHash, SidechainPublicKey,
UtxoId,
};
use sp_core::offchain::Timestamp;

#[test]
Expand All @@ -39,12 +42,19 @@ fn no_ariadne_parameters_on_main_chain_no_updates() {

#[test]
fn no_ariadne_parameters_on_main_chain_do_updates() {
let offchain_mock = OffchainMock::new().with_upsert_d_param(
UtxoId::default(),
new_d_parameter(),
payment_signing_key(),
Ok(Some(McTxHash::default())),
);
let offchain_mock = OffchainMock::new()
.with_upsert_d_param(
UtxoId::default(),
new_d_parameter(),
payment_signing_key(),
Ok(Some(McTxHash::default())),
)
.with_upsert_permissioned_candidates(
genesis_utxo(),
&initial_permissioned_candidates(),
payment_signing_key(),
Ok(Some(McTxHash::default())),
);
let mock_context = MockIOContext::new()
.with_file(PC_CONTRACTS_CLI_PATH, "<mock executable>")
.with_json_file(CHAIN_CONFIG_FILE_PATH, test_chain_config_content())
Expand All @@ -58,7 +68,7 @@ fn no_ariadne_parameters_on_main_chain_do_updates() {
get_ariadne_parameters_io(ariadne_parameters_not_found_response()),
print_ariadne_parameters_not_found_io(),
prompt_permissioned_candidates_update_io(true),
insert_permissioned_candidates_io(),
upsert_permissioned_candidates_io(),
prompt_d_parameter_update_io(true),
insert_d_parameter_io(),
print_post_update_info_io(),
Expand Down Expand Up @@ -90,12 +100,19 @@ fn ariadne_parameters_are_on_main_chain_no_updates() {

#[test]
fn ariadne_parameters_are_on_main_chain_do_update() {
let offchain_mock = OffchainMock::new().with_upsert_d_param(
UtxoId::default(),
new_d_parameter(),
payment_signing_key(),
Ok(Some(McTxHash::default())),
);
let offchain_mock = OffchainMock::new()
.with_upsert_d_param(
UtxoId::default(),
new_d_parameter(),
payment_signing_key(),
Ok(Some(McTxHash::default())),
)
.with_upsert_permissioned_candidates(
genesis_utxo(),
&initial_permissioned_candidates(),
payment_signing_key(),
Ok(Some(McTxHash::default())),
);
let mock_context = MockIOContext::new()
.with_file(PC_CONTRACTS_CLI_PATH, "<mock executable>")
.with_json_file(CHAIN_CONFIG_FILE_PATH, test_chain_config_content())
Expand All @@ -109,7 +126,7 @@ fn ariadne_parameters_are_on_main_chain_do_update() {
get_ariadne_parameters_io(ariadne_parameters_found_response()),
print_main_chain_and_configuration_candidates_difference_io(),
prompt_permissioned_candidates_update_io(true),
update_permissioned_candidates_io(),
upsert_permissioned_candidates_io(),
print_d_param_from_main_chain_io(),
prompt_d_parameter_update_io(true),
update_d_parameter_io(),
Expand All @@ -121,7 +138,14 @@ fn ariadne_parameters_are_on_main_chain_do_update() {

#[test]
fn fails_if_update_permissioned_candidates_fail() {
let offchain_mock = OffchainMock::new().with_upsert_permissioned_candidates(
genesis_utxo(),
&initial_permissioned_candidates(),
payment_signing_key(),
Err("something went wrong".into()),
);
let mock_context = MockIOContext::new()
.with_offchain_mocks(OffchainMocks::new_with_mock("http://localhost:1337", offchain_mock))
.with_file(PC_CONTRACTS_CLI_PATH, "<mock executable>")
.with_json_file(CHAIN_CONFIG_FILE_PATH, test_chain_config_content())
.with_json_file(RESOURCES_CONFIG_FILE_PATH, test_resources_config_content())
Expand All @@ -132,7 +156,7 @@ fn fails_if_update_permissioned_candidates_fail() {
get_ariadne_parameters_io(ariadne_parameters_found_response()),
print_main_chain_and_configuration_candidates_difference_io(),
prompt_permissioned_candidates_update_io(true),
update_permissioned_candidates_failed_io(),
upsert_permissioned_candidates_failed_io(),
]);
let result = SetupMainChainStateCmd.run(&mock_context);
result.expect_err("should return error");
Expand Down Expand Up @@ -230,40 +254,35 @@ fn prompt_permissioned_candidates_update_io(choice: bool) -> MockIO {
MockIO::prompt_yes_no("Do you want to set/update the permissioned candidates on the main chain with values from configuration file?", false, choice)
}

fn insert_permissioned_candidates_io() -> MockIO {
fn upsert_permissioned_candidates_io() -> MockIO {
MockIO::Group(vec![
establish_pc_contracts_cli_config_io(),
MockIO::file_read("partner-chains-cli-chain-config.json"),
MockIO::run_command(
"./pc-contracts-cli update-permissioned-candidates --remove-all-candidates --network testnet --add-candidate 020a1091341fe5664bfa1782d5e04779689068c916b04cb365ec3153755684d9a1:d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d:88dc3417d5058ec4b4503e0c12ea1a0a89be200fe98922423d4334014fa6b0ee --add-candidate 0390084fdbf27d2b79d26a4f13f0ccd982cb755a661969143c37cbc49ef5b91f27:8eaf04151687736326c9fea17e25fc5287613693c912909cb226aa4794f26a48:d17c2d7823ebf260fd138f2d7e27d114c0145d968b5ff5006125f2414fadae69 --genesis-utxo 0000000000000000000000000000000000000000000000000000000000000000#0 --kupo-host localhost --kupo-port 1442 --ogmios-host localhost --ogmios-port 1337 --payment-signing-key-file payment.skey",
"{\"endpoint\":\"UpdatePermissionedCandidates\",\"transactionId\":\"bbb5ab1232fde32884678333b44aa4f92a2229d7ba7a65d9eae4cb8b8c87d735\"}"
prompt_ogmios_configuration_io(
&default_ogmios_service_config(),
&default_ogmios_service_config(),
),
prompt_with_default_and_save_to_existing_file(
CARDANO_PAYMENT_SIGNING_KEY_FILE,
Some("payment.skey"),
"payment.skey",
),
MockIO::file_read("payment.skey"),
MockIO::print(
"Permissioned candidates updated. The change will be effective in two main chain epochs.",
)])
}

fn update_permissioned_candidates_io() -> MockIO {
fn upsert_permissioned_candidates_failed_io() -> MockIO {
MockIO::Group(vec![
establish_pc_contracts_cli_config_io(),
MockIO::file_read("partner-chains-cli-chain-config.json"),
MockIO::run_command(
"./pc-contracts-cli update-permissioned-candidates --remove-all-candidates --network testnet --add-candidate 020a1091341fe5664bfa1782d5e04779689068c916b04cb365ec3153755684d9a1:d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d:88dc3417d5058ec4b4503e0c12ea1a0a89be200fe98922423d4334014fa6b0ee --add-candidate 0390084fdbf27d2b79d26a4f13f0ccd982cb755a661969143c37cbc49ef5b91f27:8eaf04151687736326c9fea17e25fc5287613693c912909cb226aa4794f26a48:d17c2d7823ebf260fd138f2d7e27d114c0145d968b5ff5006125f2414fadae69 --genesis-utxo 0000000000000000000000000000000000000000000000000000000000000000#0 --kupo-host localhost --kupo-port 1442 --ogmios-host localhost --ogmios-port 1337 --payment-signing-key-file payment.skey",
"{\"endpoint\":\"UpdatePermissionedCandidates\",\"transactionId\":\"bbb5ab1232fde32884678333b44aa4f92a2229d7ba7a65d9eae4cb8b8c87d735\"}"
prompt_ogmios_configuration_io(
&default_ogmios_service_config(),
&default_ogmios_service_config(),
),
MockIO::print(
"Permissioned candidates updated. The change will be effective in two main chain epochs.",
)])
}

fn update_permissioned_candidates_failed_io() -> MockIO {
MockIO::Group(vec![
establish_pc_contracts_cli_config_io(),
MockIO::file_read("partner-chains-cli-chain-config.json"),
MockIO::run_command_with_result(
"./pc-contracts-cli update-permissioned-candidates --remove-all-candidates --network testnet --add-candidate 020a1091341fe5664bfa1782d5e04779689068c916b04cb365ec3153755684d9a1:d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d:88dc3417d5058ec4b4503e0c12ea1a0a89be200fe98922423d4334014fa6b0ee --add-candidate 0390084fdbf27d2b79d26a4f13f0ccd982cb755a661969143c37cbc49ef5b91f27:8eaf04151687736326c9fea17e25fc5287613693c912909cb226aa4794f26a48:d17c2d7823ebf260fd138f2d7e27d114c0145d968b5ff5006125f2414fadae69 --genesis-utxo 0000000000000000000000000000000000000000000000000000000000000000#0 --kupo-host localhost --kupo-port 1442 --ogmios-host localhost --ogmios-port 1337 --payment-signing-key-file payment.skey",
Err(anyhow::anyhow!("Something went wrong"))
prompt_with_default_and_save_to_existing_file(
CARDANO_PAYMENT_SIGNING_KEY_FILE,
Some("payment.skey"),
"payment.skey",
),
MockIO::file_read("payment.skey"),
])
}

Expand Down Expand Up @@ -403,6 +422,37 @@ fn test_chain_config_content() -> serde_json::Value {
})
}

fn initial_permissioned_candidates() -> Vec<sidechain_domain::PermissionedCandidateData> {
vec![
sidechain_domain::PermissionedCandidateData {
sidechain_public_key: SidechainPublicKey(
hex!("020a1091341fe5664bfa1782d5e04779689068c916b04cb365ec3153755684d9a1").to_vec(),
),
aura_public_key: AuraPublicKey(
hex!("d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d").to_vec(),
),
grandpa_public_key: GrandpaPublicKey(
hex!("88dc3417d5058ec4b4503e0c12ea1a0a89be200fe98922423d4334014fa6b0ee").to_vec(),
),
},
sidechain_domain::PermissionedCandidateData {
sidechain_public_key: SidechainPublicKey(
hex!("0390084fdbf27d2b79d26a4f13f0ccd982cb755a661969143c37cbc49ef5b91f27").to_vec(),
),
aura_public_key: AuraPublicKey(
hex!("8eaf04151687736326c9fea17e25fc5287613693c912909cb226aa4794f26a48").to_vec(),
),
grandpa_public_key: GrandpaPublicKey(
hex!("d17c2d7823ebf260fd138f2d7e27d114c0145d968b5ff5006125f2414fadae69").to_vec(),
),
},
]
}

fn genesis_utxo() -> UtxoId {
UtxoId::new(hex!("0000000000000000000000000000000000000000000000000000000000000000"), 0)
}

fn chain_parameters_json() -> serde_json::Value {
json!({
"genesis_utxo": "0000000000000000000000000000000000000000000000000000000000000000#0"
Expand Down

0 comments on commit 7cc10d5

Please sign in to comment.