From c848f7907ae9c895e27f9ebaabc056e288eab8a6 Mon Sep 17 00:00:00 2001 From: Ahmad Kaouk Date: Fri, 26 Jan 2024 10:41:03 +0100 Subject: [PATCH] fix format --- .../evm/precompile/clear-storage/src/tests.rs | 648 +++++++++--------- 1 file changed, 324 insertions(+), 324 deletions(-) diff --git a/frame/evm/precompile/clear-storage/src/tests.rs b/frame/evm/precompile/clear-storage/src/tests.rs index 17b5ce675a..f9670bed29 100644 --- a/frame/evm/precompile/clear-storage/src/tests.rs +++ b/frame/evm/precompile/clear-storage/src/tests.rs @@ -23,374 +23,374 @@ use sp_core::{keccak_256, H160, H256}; // Helper function that calculates the contract address pub fn contract_address(sender: H160, nonce: u64) -> H160 { - let mut rlp = RlpStream::new_list(2); - rlp.append(&sender); - rlp.append(&nonce); + let mut rlp = RlpStream::new_list(2); + rlp.append(&sender); + rlp.append(&nonce); - H160::from_slice(&keccak_256(&rlp.out())[12..]) + H160::from_slice(&keccak_256(&rlp.out())[12..]) } fn precompiles() -> Precompiles { - PrecompilesValue::get() + PrecompilesValue::get() } // Helper function that creates a contract with `num_entries` storage entries fn mock_contract_with_entries(nonce: u64, num_entries: u32) -> H160 { - let contract_address = contract_address(Alice.into(), nonce); - let account_id = - ::AddressMapping::into_account_id(contract_address); - let _ = frame_system::Pallet::::inc_sufficients(&account_id); - - // Add num_entries storage entries to the suicided contract - for i in 0..num_entries { - pallet_evm::AccountStorages::::insert( - contract_address, - H256::from_low_u64_be(i as u64), - H256::from_low_u64_be(i as u64), - ); - } - - contract_address + let contract_address = contract_address(Alice.into(), nonce); + let account_id = + ::AddressMapping::into_account_id(contract_address); + let _ = frame_system::Pallet::::inc_sufficients(&account_id); + + // Add num_entries storage entries to the suicided contract + for i in 0..num_entries { + pallet_evm::AccountStorages::::insert( + contract_address, + H256::from_low_u64_be(i as u64), + H256::from_low_u64_be(i as u64), + ); + } + + contract_address } #[test] fn test_clear_suicided_contract_succesfull() { - ExtBuilder::default() - .with_balances(vec![(Alice.into(), 10000000000000000000)]) - .build() - .execute_with(|| { - let contract_address = mock_contract_with_entries(1, 10); - // Add contract to the suicided contracts - pallet_evm::Suicided::::insert(contract_address, ()); - precompiles() - .prepare_test( - Alice, - Precompile1, - PCall::clear_suicided_storage { - addresses: vec![contract_address.into()].into(), - limit: u32::MAX, - }, - ) - .execute_returns(()); - - assert_eq!( - pallet_evm::AccountStorages::::iter_prefix(contract_address).count(), - 0 - ); - - assert!( - pallet_evm::Suicided::::contains_key(contract_address) - ); - }) + ExtBuilder::default() + .with_balances(vec![(Alice.into(), 10000000000000000000)]) + .build() + .execute_with(|| { + let contract_address = mock_contract_with_entries(1, 10); + // Add contract to the suicided contracts + pallet_evm::Suicided::::insert(contract_address, ()); + precompiles() + .prepare_test( + Alice, + Precompile1, + PCall::clear_suicided_storage { + addresses: vec![contract_address.into()].into(), + limit: u32::MAX, + }, + ) + .execute_returns(()); + + assert_eq!( + pallet_evm::AccountStorages::::iter_prefix(contract_address).count(), + 0 + ); + + assert!(pallet_evm::Suicided::::contains_key( + contract_address + )); + }) } // Test that the precompile fails if the contract is not suicided #[test] fn test_clear_suicided_contract_failed() { - ExtBuilder::default() - .with_balances(vec![(Alice.into(), 10000000000000000000)]) - .build() - .execute_with(|| { - let contract_address = mock_contract_with_entries(1, 10); - - // Ensure that the contract is not suicided - assert!( - pallet_evm::Suicided::::contains_key(contract_address) - ); - - precompiles() - .prepare_test( - Alice, - Precompile1, - PCall::clear_suicided_storage { - addresses: vec![contract_address.into()].into(), - limit: u32::MAX, - }, - ) - .execute_reverts(|output| { - output == format!("NotSuicided: {}", contract_address).as_bytes() - }); - - assert_eq!( - pallet_evm::AccountStorages::::iter_prefix(contract_address).count(), - 10 - ); - }) + ExtBuilder::default() + .with_balances(vec![(Alice.into(), 10000000000000000000)]) + .build() + .execute_with(|| { + let contract_address = mock_contract_with_entries(1, 10); + + // Ensure that the contract is not suicided + assert!(pallet_evm::Suicided::::contains_key( + contract_address + )); + + precompiles() + .prepare_test( + Alice, + Precompile1, + PCall::clear_suicided_storage { + addresses: vec![contract_address.into()].into(), + limit: u32::MAX, + }, + ) + .execute_reverts(|output| { + output == format!("NotSuicided: {}", contract_address).as_bytes() + }); + + assert_eq!( + pallet_evm::AccountStorages::::iter_prefix(contract_address).count(), + 10 + ); + }) } // Test that the precompile can handle an empty input #[test] fn test_clear_suicided_empty_input() { - ExtBuilder::default() - .with_balances(vec![(Alice.into(), 10000000000000000000)]) - .build() - .execute_with(|| { - let contract_address = mock_contract_with_entries(1, 10); - // Add contract to the suicided contracts - pallet_evm::Suicided::::insert(contract_address, ()); - - precompiles() - .prepare_test( - Alice, - Precompile1, - PCall::clear_suicided_storage { - addresses: vec![].into(), - limit: u32::MAX, - }, - ) - .execute_returns(()); - - assert_eq!( - pallet_evm::AccountStorages::::iter_prefix(contract_address).count(), - 10 - ); - assert!( - pallet_evm::Suicided::::contains_key(contract_address) - ); - }) + ExtBuilder::default() + .with_balances(vec![(Alice.into(), 10000000000000000000)]) + .build() + .execute_with(|| { + let contract_address = mock_contract_with_entries(1, 10); + // Add contract to the suicided contracts + pallet_evm::Suicided::::insert(contract_address, ()); + + precompiles() + .prepare_test( + Alice, + Precompile1, + PCall::clear_suicided_storage { + addresses: vec![].into(), + limit: u32::MAX, + }, + ) + .execute_returns(()); + + assert_eq!( + pallet_evm::AccountStorages::::iter_prefix(contract_address).count(), + 10 + ); + assert!(pallet_evm::Suicided::::contains_key( + contract_address + )); + }) } // Test with multiple suicided contracts ensuring that the precompile can handle multiple addresses at once. #[test] fn test_clear_suicided_contract_multiple_addresses() { - ExtBuilder::default() - .with_balances(vec![(Alice.into(), 10000000000000000000)]) - .build() - .execute_with(|| { - let contract_address1 = mock_contract_with_entries(1, 10); - let contract_address2 = mock_contract_with_entries(2, 20); - let contract_address3 = mock_contract_with_entries(3, 30); - - // Add contract to the suicided contracts - pallet_evm::Suicided::::insert(contract_address1, ()); - pallet_evm::Suicided::::insert(contract_address2, ()); - pallet_evm::Suicided::::insert(contract_address3, ()); - - precompiles() - .prepare_test( - Alice, - Precompile1, - PCall::clear_suicided_storage { - addresses: vec![ - contract_address1.into(), - contract_address2.into(), - contract_address3.into(), - ] - .into(), - limit: u32::MAX, - }, - ) - .execute_returns(()); - - assert_eq!( - pallet_evm::AccountStorages::::iter_prefix(contract_address1).count(), - 0 - ); - assert_eq!( - pallet_evm::AccountStorages::::iter_prefix(contract_address2).count(), - 0 - ); - assert_eq!( - pallet_evm::AccountStorages::::iter_prefix(contract_address3).count(), - 0 - ); - - assert!( - pallet_evm::Suicided::::contains_key(contract_address1) - ); - assert!( - pallet_evm::Suicided::::contains_key(contract_address2) - ); - assert!( - pallet_evm::Suicided::::contains_key(contract_address3) - ); - }) + ExtBuilder::default() + .with_balances(vec![(Alice.into(), 10000000000000000000)]) + .build() + .execute_with(|| { + let contract_address1 = mock_contract_with_entries(1, 10); + let contract_address2 = mock_contract_with_entries(2, 20); + let contract_address3 = mock_contract_with_entries(3, 30); + + // Add contract to the suicided contracts + pallet_evm::Suicided::::insert(contract_address1, ()); + pallet_evm::Suicided::::insert(contract_address2, ()); + pallet_evm::Suicided::::insert(contract_address3, ()); + + precompiles() + .prepare_test( + Alice, + Precompile1, + PCall::clear_suicided_storage { + addresses: vec![ + contract_address1.into(), + contract_address2.into(), + contract_address3.into(), + ] + .into(), + limit: u32::MAX, + }, + ) + .execute_returns(()); + + assert_eq!( + pallet_evm::AccountStorages::::iter_prefix(contract_address1).count(), + 0 + ); + assert_eq!( + pallet_evm::AccountStorages::::iter_prefix(contract_address2).count(), + 0 + ); + assert_eq!( + pallet_evm::AccountStorages::::iter_prefix(contract_address3).count(), + 0 + ); + + assert!(pallet_evm::Suicided::::contains_key( + contract_address1 + )); + assert!(pallet_evm::Suicided::::contains_key( + contract_address2 + )); + assert!(pallet_evm::Suicided::::contains_key( + contract_address3 + )); + }) } // Test a combination of Suicided and non-suicided contracts #[test] fn test_clear_suicided_mixed_suicided_and_non_suicided() { - ExtBuilder::default() - .with_balances(vec![(Alice.into(), 10000000000000000000)]) - .build() - .execute_with(|| { - let contract_address1 = mock_contract_with_entries(1, 10); - let contract_address2 = mock_contract_with_entries(2, 10); - let contract_address3 = mock_contract_with_entries(3, 10); - let contract_address4 = mock_contract_with_entries(4, 10); - - // Add contract to the suicided contracts - pallet_evm::Suicided::::insert(contract_address1, ()); - pallet_evm::Suicided::::insert(contract_address2, ()); - pallet_evm::Suicided::::insert(contract_address4, ()); - - precompiles() - .prepare_test( - Alice, - Precompile1, - PCall::clear_suicided_storage { - addresses: vec![ - contract_address1.into(), - contract_address2.into(), - contract_address3.into(), - contract_address4.into(), - ] - .into(), - limit: u32::MAX, - }, - ) - .execute_reverts(|output| { - output == format!("NotSuicided: {}", contract_address3).as_bytes() - }); - }) + ExtBuilder::default() + .with_balances(vec![(Alice.into(), 10000000000000000000)]) + .build() + .execute_with(|| { + let contract_address1 = mock_contract_with_entries(1, 10); + let contract_address2 = mock_contract_with_entries(2, 10); + let contract_address3 = mock_contract_with_entries(3, 10); + let contract_address4 = mock_contract_with_entries(4, 10); + + // Add contract to the suicided contracts + pallet_evm::Suicided::::insert(contract_address1, ()); + pallet_evm::Suicided::::insert(contract_address2, ()); + pallet_evm::Suicided::::insert(contract_address4, ()); + + precompiles() + .prepare_test( + Alice, + Precompile1, + PCall::clear_suicided_storage { + addresses: vec![ + contract_address1.into(), + contract_address2.into(), + contract_address3.into(), + contract_address4.into(), + ] + .into(), + limit: u32::MAX, + }, + ) + .execute_reverts(|output| { + output == format!("NotSuicided: {}", contract_address3).as_bytes() + }); + }) } // Test that the precompile can handle suicided contracts that have no storage entries #[test] fn test_clear_suicided_no_storage_entries() { - ExtBuilder::default() - .with_balances(vec![(Alice.into(), 10000000000000000000)]) - .build() - .execute_with(|| { - let contract_address1 = mock_contract_with_entries(1, 0); - let contract_address2 = mock_contract_with_entries(1, 500); - let contract_address3 = mock_contract_with_entries(1, 0); - let contract_address4 = mock_contract_with_entries(1, 400); - let contract_address5 = mock_contract_with_entries(1, 100); - - // Add contract to the suicided contracts - pallet_evm::Suicided::::insert(contract_address1, ()); - pallet_evm::Suicided::::insert(contract_address2, ()); - pallet_evm::Suicided::::insert(contract_address3, ()); - pallet_evm::Suicided::::insert(contract_address4, ()); - pallet_evm::Suicided::::insert(contract_address5, ()); - - precompiles() - .prepare_test( - Alice, - Precompile1, - PCall::clear_suicided_storage { - addresses: vec![contract_address1.into()].into(), - limit: u32::MAX, - }, - ) - .execute_returns(()); - - assert_eq!( - pallet_evm::AccountStorages::::iter_prefix(contract_address1).count(), - 0 - ); - assert!( - pallet_evm::Suicided::::contains_key(contract_address1) - ); - assert_eq!( - pallet_evm::AccountStorages::::iter_prefix(contract_address2).count(), - 0 - ); - assert!( - pallet_evm::Suicided::::contains_key(contract_address2) - ); - assert_eq!( - pallet_evm::AccountStorages::::iter_prefix(contract_address3).count(), - 0 - ); - assert!( - pallet_evm::Suicided::::contains_key(contract_address3) - ); - assert_eq!( - pallet_evm::AccountStorages::::iter_prefix(contract_address4).count(), - 0 - ); - assert!( - pallet_evm::Suicided::::contains_key(contract_address4) - ); - assert_eq!( - pallet_evm::AccountStorages::::iter_prefix(contract_address4).count(), - 0 - ); - assert!( - pallet_evm::Suicided::::contains_key(contract_address4) - ); - assert_eq!( - pallet_evm::AccountStorages::::iter_prefix(contract_address5).count(), - 0 - ); - assert!( - pallet_evm::Suicided::::contains_key(contract_address5) - ); - }) + ExtBuilder::default() + .with_balances(vec![(Alice.into(), 10000000000000000000)]) + .build() + .execute_with(|| { + let contract_address1 = mock_contract_with_entries(1, 0); + let contract_address2 = mock_contract_with_entries(1, 500); + let contract_address3 = mock_contract_with_entries(1, 0); + let contract_address4 = mock_contract_with_entries(1, 400); + let contract_address5 = mock_contract_with_entries(1, 100); + + // Add contract to the suicided contracts + pallet_evm::Suicided::::insert(contract_address1, ()); + pallet_evm::Suicided::::insert(contract_address2, ()); + pallet_evm::Suicided::::insert(contract_address3, ()); + pallet_evm::Suicided::::insert(contract_address4, ()); + pallet_evm::Suicided::::insert(contract_address5, ()); + + precompiles() + .prepare_test( + Alice, + Precompile1, + PCall::clear_suicided_storage { + addresses: vec![contract_address1.into()].into(), + limit: u32::MAX, + }, + ) + .execute_returns(()); + + assert_eq!( + pallet_evm::AccountStorages::::iter_prefix(contract_address1).count(), + 0 + ); + assert!(pallet_evm::Suicided::::contains_key( + contract_address1 + )); + assert_eq!( + pallet_evm::AccountStorages::::iter_prefix(contract_address2).count(), + 0 + ); + assert!(pallet_evm::Suicided::::contains_key( + contract_address2 + )); + assert_eq!( + pallet_evm::AccountStorages::::iter_prefix(contract_address3).count(), + 0 + ); + assert!(pallet_evm::Suicided::::contains_key( + contract_address3 + )); + assert_eq!( + pallet_evm::AccountStorages::::iter_prefix(contract_address4).count(), + 0 + ); + assert!(pallet_evm::Suicided::::contains_key( + contract_address4 + )); + assert_eq!( + pallet_evm::AccountStorages::::iter_prefix(contract_address4).count(), + 0 + ); + assert!(pallet_evm::Suicided::::contains_key( + contract_address4 + )); + assert_eq!( + pallet_evm::AccountStorages::::iter_prefix(contract_address5).count(), + 0 + ); + assert!(pallet_evm::Suicided::::contains_key( + contract_address5 + )); + }) } // Test that the precompile deletes a maximum of `ENTRY_LIMIT` entries #[test] fn test_clear_suicided_entry_limit() { - ExtBuilder::default() - .with_balances(vec![(Alice.into(), 10000000000000000000)]) - .build() - .execute_with(|| { - let contract_address1 = mock_contract_with_entries(1, 4); - let contract_address2 = mock_contract_with_entries(2, 3); - // Add contract to the suicided contracts - pallet_evm::Suicided::::insert(contract_address1, ()); - pallet_evm::Suicided::::insert(contract_address2, ()); - - precompiles() - .prepare_test( - Alice, - Precompile1, - PCall::clear_suicided_storage { - addresses: vec![contract_address1.into(), contract_address2.into()].into(), - limit: 4, - }, - ) - .execute_returns(()); - - assert_eq!( - pallet_evm::AccountStorages::::iter_prefix(contract_address1).count(), - 0 - ); - assert!( - pallet_evm::Suicided::::contains_key(contract_address1) - ); - - assert_eq!( - pallet_evm::AccountStorages::::iter_prefix(contract_address2).count(), - 3 - ); - - assert!( - pallet_evm::Suicided::::contains_key(contract_address2) - ); - }) + ExtBuilder::default() + .with_balances(vec![(Alice.into(), 10000000000000000000)]) + .build() + .execute_with(|| { + let contract_address1 = mock_contract_with_entries(1, 4); + let contract_address2 = mock_contract_with_entries(2, 3); + // Add contract to the suicided contracts + pallet_evm::Suicided::::insert(contract_address1, ()); + pallet_evm::Suicided::::insert(contract_address2, ()); + + precompiles() + .prepare_test( + Alice, + Precompile1, + PCall::clear_suicided_storage { + addresses: vec![contract_address1.into(), contract_address2.into()].into(), + limit: 4, + }, + ) + .execute_returns(()); + + assert_eq!( + pallet_evm::AccountStorages::::iter_prefix(contract_address1).count(), + 0 + ); + assert!(pallet_evm::Suicided::::contains_key( + contract_address1 + )); + + assert_eq!( + pallet_evm::AccountStorages::::iter_prefix(contract_address2).count(), + 3 + ); + + assert!(pallet_evm::Suicided::::contains_key( + contract_address2 + )); + }) } #[test] fn test_clear_suicided_entry_limit_1() { - ExtBuilder::default() - .with_balances(vec![(Alice.into(), 10000000000000000000)]) - .build() - .execute_with(|| { - let contract_address1 = mock_contract_with_entries(1, 5); - // Add contract to the suicided contracts - pallet_evm::Suicided::::insert(contract_address1, ()); - - precompiles() - .prepare_test( - Alice, - Precompile1, - PCall::clear_suicided_storage { - addresses: vec![contract_address1.into()].into(), - limit: 4, - }, - ) - .execute_returns(()); - - assert_eq!( - pallet_evm::AccountStorages::::iter_prefix(contract_address1).count(), - 1 - ); - assert!( - pallet_evm::Suicided::::contains_key(contract_address1) - ); - }) + ExtBuilder::default() + .with_balances(vec![(Alice.into(), 10000000000000000000)]) + .build() + .execute_with(|| { + let contract_address1 = mock_contract_with_entries(1, 5); + // Add contract to the suicided contracts + pallet_evm::Suicided::::insert(contract_address1, ()); + + precompiles() + .prepare_test( + Alice, + Precompile1, + PCall::clear_suicided_storage { + addresses: vec![contract_address1.into()].into(), + limit: 4, + }, + ) + .execute_returns(()); + + assert_eq!( + pallet_evm::AccountStorages::::iter_prefix(contract_address1).count(), + 1 + ); + assert!(pallet_evm::Suicided::::contains_key( + contract_address1 + )); + }) }