Skip to content

Commit

Permalink
fix(blueprint-test-utils): fix contract deployment order (#517)
Browse files Browse the repository at this point in the history
* fix(blueprint-test-utils): fix contract deployment order

* fix: MBSM bytecode and deployment

---------

Co-authored-by: Shady Khalifa <[email protected]>
  • Loading branch information
Serial-ATA and shekohex authored Dec 4, 2024
1 parent d59217d commit 7261814
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 34 deletions.
30 changes: 25 additions & 5 deletions blueprint-test-utils/src/tangle/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use alloy_provider::network::{ReceiptResponse, TransactionBuilder};
use alloy_provider::{Provider, WsConnect};
use cargo_tangle::deploy::PrivateKeySigner;
use gadget_sdk::keystore::TanglePairSigner;
use gadget_sdk::tangle_subxt::tangle_testnet_runtime::api::runtime_types::pallet_services::module::Call;
use gadget_sdk::tangle_subxt::tangle_testnet_runtime::api::runtime_types::tangle_testnet_runtime::RuntimeCall;
use sp_core::H160;
use subxt::blocks::ExtrinsicEvents;
use subxt::client::OnlineClientT;
Expand Down Expand Up @@ -37,8 +39,23 @@ pub async fn deploy_new_mbsm_revision(
.await?;

let tx = alloy_rpc_types::TransactionRequest::default().with_deploy_code(bytecode.to_vec());
let send_result = provider.send_transaction(tx).await;
let tx = match send_result {
Ok(tx) => tx,
Err(err) => {
error!("Failed to send transaction: {err}");
return Err("Failed to deploy MBSM Contract".into());
}
};
// Deploy the contract.
let receipt = provider.send_transaction(tx).await?.get_receipt().await?;
let tx_result = tx.get_receipt().await;
let receipt = match tx_result {
Ok(receipt) => receipt,
Err(err) => {
error!("Failed to deploy MBSM Contract: {err}");
return Err("Failed to deploy MBSM Contract".into());
}
};
// Check the receipt status.
let mbsm_address = if receipt.status() {
ReceiptResponse::contract_address(&receipt).unwrap()
Expand All @@ -47,12 +64,15 @@ pub async fn deploy_new_mbsm_revision(
error!("Receipt: {receipt:#?}");
return Err("MBSM Contract deployment failed!".into());
};
let call = api::tx()
.services()
.update_master_blueprint_service_manager(mbsm_address.0 .0.into());
info!("MBSM Contract deployed at: {mbsm_address}");
let sudo_call = api::tx().sudo().sudo(RuntimeCall::Services(
Call::update_master_blueprint_service_manager {
address: mbsm_address.0 .0.into(),
},
));
let res = client
.tx()
.sign_and_submit_then_watch_default(&call, account_id)
.sign_and_submit_then_watch_default(&sudo_call, account_id)
.await?;
let evts = wait_for_in_block_success(res).await?;
let ev = evts
Expand Down
60 changes: 33 additions & 27 deletions blueprint-test-utils/src/test_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,39 @@ pub async fn new_test_ext_blueprint_manager<
let local_tangle_node_ws = opts.ws_rpc_url.clone();
let local_tangle_node_http = opts.http_rpc_url.clone();

let client = get_client(&local_tangle_node_ws, &local_tangle_node_http)
.await
.expect("Failed to create an account-based localhost client");

// Check if the MBSM is already deployed.
let latest_revision = transactions::get_latest_mbsm_revision(&client)
.await
.expect("Get latest MBSM revision");
match latest_revision {
Some((rev, addr)) => debug!("MBSM is deployed at revision #{rev} at address {addr}"),
None => {
let bytecode_hex = include_str!("../tnt-core/MasterBlueprintServiceManager.hex");
let mut raw_hex = bytecode_hex.replace("0x", "").replace("\n", "");
// fix odd length
if raw_hex.len() % 2 != 0 {
raw_hex = format!("0{}", raw_hex);
}
let bytecode = hex::decode(&raw_hex).expect("valid bytecode in hex format");
let ev = transactions::deploy_new_mbsm_revision(
&local_tangle_node_ws,
&client,
handles[0].sr25519_id(),
opts.signer_evm.clone().expect("Signer EVM is set"),
&bytecode,
)
.await
.expect("deploy new MBSM revision");
let rev = ev.revision;
let addr = ev.address;
debug!("Deployed MBSM at revision #{rev} at address {addr}");
}
};

// Step 1: Create the blueprint using alice's identity
let blueprint_id = match cargo_tangle::deploy::deploy_to_tangle(opts.clone()).await {
Ok(id) => id,
Expand All @@ -144,10 +177,6 @@ pub async fn new_test_ext_blueprint_manager<
}
};

let client = get_client(&local_tangle_node_ws, &local_tangle_node_http)
.await
.expect("Failed to create an account-based localhost client");

// Step 2: Have each identity register to a blueprint
let mut futures_ordered = FuturesOrdered::new();
let registration_args = RegistrationArgs::new();
Expand Down Expand Up @@ -208,29 +237,6 @@ pub async fn new_test_ext_blueprint_manager<
.collect::<Vec<BlueprintManagerHandle>>()
.await;

// Check if the MBSM is already deployed.
let latest_revision = transactions::get_latest_mbsm_revision(&client)
.await
.expect("Get latest MBSM revision");
match latest_revision {
Some((rev, addr)) => debug!("MBSM is deployed at revision #{rev} at address {addr}"),
None => {
let bytecode_hex = include_str!("../tnt-core/MasterBlueprintServiceManager.hex");
let bytecode = hex::decode(bytecode_hex).expect("valid bytecode in hex format");
let ev = transactions::deploy_new_mbsm_revision(
&local_tangle_node_http,
&client,
handles[0].sr25519_id(),
opts.signer_evm.clone().expect("Signer EVM is set"),
&bytecode,
)
.await
.expect("deploy new MBSM revision");
let rev = ev.revision;
let addr = ev.address;
debug!("Deployed MBSM at revision #{rev} at address {addr}");
}
};
// Step 3: request a service
let all_nodes = handles
.iter()
Expand Down
Loading

0 comments on commit 7261814

Please sign in to comment.