From 399d2d88f9d71a70148b9c62d41e901250a33475 Mon Sep 17 00:00:00 2001 From: peg Date: Fri, 23 Aug 2024 14:09:47 +0200 Subject: [PATCH] Add negative test to try to remove a program which is in use --- crates/client/src/tests.rs | 81 +++++++++++++++++++++++++++++++++++--- 1 file changed, 75 insertions(+), 6 deletions(-) diff --git a/crates/client/src/tests.rs b/crates/client/src/tests.rs index 284902efa..68667b841 100644 --- a/crates/client/src/tests.rs +++ b/crates/client/src/tests.rs @@ -1,21 +1,28 @@ use crate::{ chain_api::{ entropy::{ - self, runtime_types::pallet_staking_extension::pallet::ServerInfo, + self, + runtime_types::{ + bounded_collections::bounded_vec::BoundedVec, + pallet_registry::pallet::ProgramInstance, + pallet_staking_extension::pallet::ServerInfo, + }, staking_extension::events, }, - get_api, get_rpc, + get_api, get_rpc, EntropyConfig, }, - change_endpoint, change_threshold_accounts, remove_program, store_program, + change_endpoint, change_threshold_accounts, register, remove_program, store_program, substrate::query_chain, + update_programs, }; use entropy_testing_utils::{ - constants::TEST_PROGRAM_WASM_BYTECODE, substrate_context::test_context_stationary, + constants::TEST_PROGRAM_WASM_BYTECODE, jump_start_network, + substrate_context::test_context_stationary, test_node_process_testing_state, }; use serial_test::serial; -use sp_core::Pair; +use sp_core::{sr25519, Pair, H256}; use sp_keyring::AccountKeyring; -use subxt::utils::AccountId32; +use subxt::{tx::PairSigner, utils::AccountId32}; #[tokio::test] #[serial] @@ -110,3 +117,65 @@ async fn test_store_and_remove_program() { // Removing program fails because program has already been removed assert!(remove_program(&api, &rpc, &program_owner, program_hash).await.is_err()); } + +#[tokio::test] +#[serial] +async fn test_remove_program_reference_counter() { + let program_owner = AccountKeyring::Ferdie.pair(); + + let force_authoring = true; + let substrate_context = test_node_process_testing_state(force_authoring).await; + let api = get_api(&substrate_context.ws_url).await.unwrap(); + let rpc = get_rpc(&substrate_context.ws_url).await.unwrap(); + + // Jumpstart the network + let alice = AccountKeyring::Alice; + let signer = PairSigner::::new(alice.clone().into()); + jump_start_network(&api, &rpc, &signer).await; + + // Store a program + let program_pointer = store_program( + &api, + &rpc, + &program_owner, + TEST_PROGRAM_WASM_BYTECODE.to_owned(), + vec![], + vec![], + vec![], + ) + .await + .unwrap(); + + // Register, using that program + let register_on_chain = true; + let (verifying_key, _registered_info) = register( + &api, + &rpc, + program_owner.clone(), + AccountId32(program_owner.public().0), + BoundedVec(vec![ProgramInstance { program_pointer, program_config: vec![] }]), + register_on_chain, + ) + .await + .unwrap(); + + // Removing program fails because program is being used + assert!(remove_program(&api, &rpc, &program_owner, program_pointer).await.is_err()); + + // Now stop using the program + update_programs( + &api, + &rpc, + verifying_key, + &program_owner, + BoundedVec(vec![ProgramInstance { + program_pointer: H256([0; 32]), + program_config: vec![], + }]), + ) + .await + .unwrap(); + + // We can now remove the program because no-one is using it + remove_program(&api, &rpc, &program_owner, program_pointer).await.unwrap(); +}