Skip to content

Commit

Permalink
Add negative test to try to remove a program which is in use
Browse files Browse the repository at this point in the history
  • Loading branch information
ameba23 committed Aug 23, 2024
1 parent 3e3db5f commit 399d2d8
Showing 1 changed file with 75 additions and 6 deletions.
81 changes: 75 additions & 6 deletions crates/client/src/tests.rs
Original file line number Diff line number Diff line change
@@ -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]
Expand Down Expand Up @@ -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::<EntropyConfig, sr25519::Pair>::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();
}

0 comments on commit 399d2d8

Please sign in to comment.