-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Move run init-goveranance just after choosing the genesis-utxo,…
… before getting MC addresses
- Loading branch information
Showing
5 changed files
with
142 additions
and
107 deletions.
There are no files selected for viewing
110 changes: 110 additions & 0 deletions
110
toolkit/partner-chains-cli/src/prepare_configuration/init_governance.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
use crate::{ | ||
cardano_key, | ||
config::{config_fields, ServiceConfig}, | ||
IOContext, | ||
}; | ||
use ogmios_client::types::OgmiosTx; | ||
use partner_chains_cardano_offchain::init_governance::InitGovernance; | ||
use sidechain_domain::{MainchainAddressHash, MainchainPrivateKey, UtxoId}; | ||
|
||
pub(crate) fn run_init_governance<C: IOContext>( | ||
genesis_utxo: UtxoId, | ||
ogmios_config: &ServiceConfig, | ||
context: &C, | ||
) -> anyhow::Result<OgmiosTx> { | ||
let offchain = context.offchain_impl(ogmios_config)?; | ||
let (payment_key, governance_authority) = get_private_key_and_key_hash(context)?; | ||
let runtime = tokio::runtime::Runtime::new().map_err(|e| anyhow::anyhow!(e))?; | ||
runtime | ||
.block_on(offchain.init_governance(governance_authority, payment_key, genesis_utxo)) | ||
.map_err(|e| anyhow::anyhow!("Governance initalization failed: {e:?}!")) | ||
} | ||
|
||
fn get_private_key_and_key_hash<C: IOContext>( | ||
context: &C, | ||
) -> Result<(MainchainPrivateKey, MainchainAddressHash), anyhow::Error> { | ||
let cardano_signig_key_file = config_fields::CARDANO_PAYMENT_SIGNING_KEY_FILE | ||
.prompt_with_default_from_file_and_save(context); | ||
let pkey = cardano_key::get_mc_pkey_from_file(&cardano_signig_key_file, context)?; | ||
let addr_hash = pkey.to_pub_key_hash(); | ||
|
||
Ok((pkey, addr_hash)) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::run_init_governance; | ||
use crate::{ | ||
config::{ | ||
config_fields::{CARDANO_PAYMENT_SIGNING_KEY_FILE, GENESIS_UTXO, OGMIOS_PROTOCOL}, | ||
NetworkProtocol, ServiceConfig, | ||
}, | ||
tests::{MockIO, MockIOContext, OffchainMock, OffchainMocks}, | ||
}; | ||
use hex_literal::hex; | ||
use ogmios_client::types::OgmiosTx; | ||
use serde_json::{json, Value}; | ||
use sidechain_domain::{MainchainAddressHash, MainchainPrivateKey, UtxoId}; | ||
|
||
#[test] | ||
fn happy_path() { | ||
let mock_context = MockIOContext::new() | ||
.with_json_file(GENESIS_UTXO.config_file, serde_json::json!({})) | ||
.with_json_file(OGMIOS_PROTOCOL.config_file, serde_json::json!({})) | ||
.with_json_file("payment.skey", payment_key_content()) | ||
.with_offchain_mocks(preprod_offchain_mocks()) | ||
.with_expected_io(vec![ | ||
MockIO::file_read(CARDANO_PAYMENT_SIGNING_KEY_FILE.config_file), | ||
MockIO::prompt( | ||
"path to the payment signing key file", | ||
Some("payment.skey"), | ||
"payment.skey", | ||
), | ||
MockIO::file_read(CARDANO_PAYMENT_SIGNING_KEY_FILE.config_file), | ||
MockIO::file_write_json( | ||
CARDANO_PAYMENT_SIGNING_KEY_FILE.config_file, | ||
test_resources_config(), | ||
), | ||
MockIO::file_read("payment.skey"), | ||
]); | ||
run_init_governance(TEST_GENESIS_UTXO, &ogmios_config(), &mock_context) | ||
.expect("should succeed"); | ||
} | ||
|
||
fn payment_key_content() -> serde_json::Value { | ||
json!({ | ||
"type": "PaymentSigningKeyShelley_ed25519", | ||
"description": "Payment Signing Key", | ||
"cborHex": "5820d0a6c5c921266d15dc8d1ce1e51a01e929a686ed3ec1a9be1145727c224bf386" | ||
}) | ||
} | ||
const TEST_GENESIS_UTXO: UtxoId = UtxoId::new([0u8; 32], 0); | ||
|
||
fn ogmios_config() -> ServiceConfig { | ||
ServiceConfig { | ||
hostname: "localhost".to_string(), | ||
port: 1337, | ||
protocol: NetworkProtocol::Http, | ||
} | ||
} | ||
|
||
fn test_resources_config() -> Value { | ||
serde_json::json!({ | ||
"cardano_payment_signing_key_file": "payment.skey", | ||
}) | ||
} | ||
|
||
fn preprod_offchain_mocks() -> OffchainMocks { | ||
let mock = OffchainMock::new().with_init_governance( | ||
TEST_GENESIS_UTXO, | ||
MainchainAddressHash(hex!("e8c300330fe315531ca89d4a2e7d0c80211bc70b473b1ed4979dff2b")), | ||
MainchainPrivateKey(hex!( | ||
"d0a6c5c921266d15dc8d1ce1e51a01e929a686ed3ec1a9be1145727c224bf386" | ||
)), | ||
Ok(OgmiosTx { | ||
id: hex!("0000000000000000000000000000000000000000000000000000000000000000"), | ||
}), | ||
); | ||
OffchainMocks::new_with_mock("http://localhost:1337", mock) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters