From 66fa8409fa06cce105830fff1aee4d76226043df Mon Sep 17 00:00:00 2001 From: Uncle Stinky Date: Tue, 17 Jun 2025 13:50:38 +0300 Subject: [PATCH] additional tests for the pallet Signed-off-by: Uncle Stinky --- pallets/slow-clap/src/lib.rs | 6 ++-- pallets/slow-clap/src/tests.rs | 64 +++++++++++++++++++++------------- 2 files changed, 44 insertions(+), 26 deletions(-) diff --git a/pallets/slow-clap/src/lib.rs b/pallets/slow-clap/src/lib.rs index c001cfc..bdadbfd 100644 --- a/pallets/slow-clap/src/lib.rs +++ b/pallets/slow-clap/src/lib.rs @@ -488,7 +488,7 @@ pub mod pallet { ValidTransaction::with_tag_prefix("SlowClap") .priority(T::UnsignedPriority::get()) - .and_provides(authority) + .and_provides(signature) .longevity(LOCK_BLOCK_EXPIRATION) .propagate(true) .build() @@ -712,7 +712,7 @@ impl Pallet { Ok(match maybe_block_range { Some((from_block, to_block)) => match new_evm_block { - 0 => (to_block, new_evm_block), + 0 => (to_block, to_block), _ => (from_block, estimated_block), }, None => (estimated_block, estimated_block), @@ -785,6 +785,7 @@ impl Pallet { let signature = authority_key.sign(&clap.encode()) .ok_or(OffchainErr::FailedSigning)?; let call = Call::slow_clap { clap, signature }; + SubmitTransaction::>::submit_unsigned_transaction(call.into()) .map_err(|_| OffchainErr::SubmitTransaction)?; } @@ -799,6 +800,7 @@ impl Pallet { let authorities = Authorities::::get(&session_index); let mut local_authorities = T::AuthorityId::all(); local_authorities.sort(); + authorities.into_iter().enumerate().filter_map(move |(index, authority)| { local_authorities .binary_search(&authority) diff --git a/pallets/slow-clap/src/tests.rs b/pallets/slow-clap/src/tests.rs index 7be3ca6..8e1cb6f 100644 --- a/pallets/slow-clap/src/tests.rs +++ b/pallets/slow-clap/src/tests.rs @@ -151,12 +151,13 @@ fn should_make_http_call_for_block_number() { evm_block_response(&mut state.write()); - t.execute_with(|| { + let _: Result<(), OffchainErr> = t.execute_with(|| { let rpc_endpoint = get_rpc_endpoint(); let network_data = prepare_evm_network(Some(1), None); let request_body = SlowClap::prepare_request_body_for_latest_block(&network_data); - let raw_response = SlowClap::fetch_from_remote(&rpc_endpoint, &request_body).unwrap(); + let raw_response = SlowClap::fetch_from_remote(&rpc_endpoint, &request_body)?; assert_eq!(raw_response.len(), 45usize); // precalculated + Ok(()) }); } @@ -168,17 +169,17 @@ fn should_make_http_call_for_logs() { evm_logs_response(&mut state.write()); - t.execute_with(|| { - let from_block: u64 = 420; - let to_block: u64 = 1337; + let _: Result<(), OffchainErr> = t.execute_with(|| { + let from_block: u64 = 20335770; + let to_block: u64 = 20335858; let rpc_endpoint = get_rpc_endpoint(); let network_data = prepare_evm_network(Some(1), None); let request_body = SlowClap::prepare_request_body_for_latest_transfers( from_block, to_block, &network_data); - let raw_response = SlowClap::fetch_from_remote(&rpc_endpoint, &request_body) - .unwrap(); + let raw_response = SlowClap::fetch_from_remote(&rpc_endpoint, &request_body)?; assert_eq!(raw_response.len(), 1805); // precalculated + Ok(()) }); } @@ -190,23 +191,23 @@ fn should_make_http_call_and_parse_block_number() { evm_block_response(&mut state.write()); - t.execute_with(|| { + let _: Result<(), OffchainErr> = t.execute_with(|| { let rpc_endpoint = get_rpc_endpoint(); let network_data = prepare_evm_network(Some(1), None); let request_body = SlowClap::prepare_request_body_for_latest_block(&network_data); - let raw_response = SlowClap::fetch_from_remote(&rpc_endpoint, &request_body).unwrap(); - let evm_block_number = SlowClap::apply_evm_response( - &raw_response, 69, Default::default(), 420, 1).unwrap_or_default(); + let raw_response = SlowClap::fetch_from_remote(&rpc_endpoint, &request_body)?; + let evm_block_number = SlowClap::apply_evm_response(&raw_response, 69, Default::default(), 420, 1)?; assert_eq!(evm_block_number, 20335745); + Ok(()) }); } #[test] fn should_make_http_call_and_parse_logs() { let (offchain, state) = TestOffchainExt::new(); - let (pool, _state) = TestTransactionPoolExt::new(); + let (pool, _) = TestTransactionPoolExt::new(); let mut t = sp_io::TestExternalities::default(); t.register_extension(OffchainDbExt::new(offchain.clone())); @@ -215,19 +216,34 @@ fn should_make_http_call_and_parse_logs() { evm_logs_response(&mut state.write()); - t.execute_with(|| { - let from_block: u64 = 420; - let to_block: u64 = 1337; + let _: Result<(), OffchainErr> = t.execute_with(|| { + let session_index = advance_session_and_get_index(); let rpc_endpoint = get_rpc_endpoint(); - let network_data = prepare_evm_network(Some(1), None); + let from_block: u64 = 20335770; + let to_block: u64 = 20335858; + let network_id: u32 = 1; + + let network_data = prepare_evm_network(Some(network_id), None); let request_body = SlowClap::prepare_request_body_for_latest_transfers( from_block, to_block, &network_data); - let raw_response = SlowClap::fetch_from_remote(&rpc_endpoint, &request_body).unwrap(); + + let raw_response = SlowClap::fetch_from_remote(&rpc_endpoint, &request_body)?; + match SlowClap::parse_evm_response(&raw_response)? { + EvmResponseType::BlockNumber(_) => assert_eq!(1, 0), // force break + EvmResponseType::TransactionLogs(evm_logs) => assert_eq!(evm_logs.len(), 2), + } + let evm_block_number = SlowClap::apply_evm_response( - &raw_response, 69, Default::default(), 420, 1).unwrap_or(20335745); + &raw_response, + 1, + UintAuthorityId::from(2), + session_index, + network_id, + )?; assert_eq!(evm_block_number, 0); + Ok(()) }); } @@ -824,10 +840,6 @@ fn should_not_fail_on_sub_existential_balance() { }); } -// TODO: multiple logs will create multiple records -// TODO: errors should be checked as much as possible -// TODO: offences generated as expected - fn advance_session_and_get_index() -> u32 { advance_session(); assert_eq!(Session::validators(), Vec::::new()); @@ -922,11 +934,11 @@ fn evm_block_response(state: &mut testing::OffchainState) { } fn evm_logs_response(state: &mut testing::OffchainState) { - let expected_body = br#"{"id":0,"jsonrpc":"2.0","method":"eth_getLogs","params":[{"fromBlock":"0x1a4","toBlock":"0x539","address":"0x4d224452801ACEd8B2F0aebE155379bb5D594381","topics":["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"]}]}"#.to_vec(); + let expected_body = br#"{"id":0,"jsonrpc":"2.0","method":"eth_getLogs","params":[{"fromBlock":"0x1364c9a","toBlock":"0x1364cf2","address":"0x4d224452801ACEd8B2F0aebE155379bb5D594381","topics":["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"]}]}"#.to_vec(); let expected_response = br#"{ - "jsonrpc":"2.0", "id":0, + "jsonrpc":"2.0", "result":[ { "address":"0x4d224452801aced8b2f0aebe155379bb5d594381", @@ -968,6 +980,10 @@ fn evm_logs_response(state: &mut testing::OffchainState) { ("Accept".to_string(), "application/json".to_string()), ("Content-Type".to_string(), "application/json".to_string()), ], + response_headers: vec![ + ("Accept".to_string(), "application/json".to_string()), + ("Content-Type".to_string(), "application/json".to_string()), + ], body: expected_body, response: Some(expected_response), sent: true,