diff --git a/src/perf/gen_multi.rs b/src/perf/gen_multi.rs index 793206a..0451786 100644 --- a/src/perf/gen_multi.rs +++ b/src/perf/gen_multi.rs @@ -43,10 +43,16 @@ impl MessageGenerator for MultiUser { fid, proto::IdRegisterEventType::Register, vec![], + None, ), )); messages.push(NextMessage::OnChainEvent( - events_factory::create_signer_event(fid, private_key, proto::SignerEventType::Add), + events_factory::create_signer_event( + fid, + private_key, + proto::SignerEventType::Add, + None, + ), )); self.initialized_fids.insert(fid); diff --git a/src/perf/gen_single.rs b/src/perf/gen_single.rs index 1c07c1c..e5d0500 100644 --- a/src/perf/gen_single.rs +++ b/src/perf/gen_single.rs @@ -35,6 +35,7 @@ impl MessageGenerator for SingleUser { fid, proto::IdRegisterEventType::Register, vec![], + None, ), )); messages.push(NextMessage::OnChainEvent( @@ -42,6 +43,7 @@ impl MessageGenerator for SingleUser { fid, self.private_key.clone(), proto::SignerEventType::Add, + None, ), )); } diff --git a/src/storage/store/account/onchain_event_store.rs b/src/storage/store/account/onchain_event_store.rs index 869cfeb..7aadbf0 100644 --- a/src/storage/store/account/onchain_event_store.rs +++ b/src/storage/store/account/onchain_event_store.rs @@ -18,6 +18,7 @@ static PAGE_SIZE: usize = 1000; const LEGACY_STORAGE_UNIT_CUTOFF_TIMESTAMP: u32 = 1724889600; const ONE_YEAR_IN_SECONDS: u32 = 365 * 24 * 60 * 60; +const SUPPORTED_SIGNER_KEY_TYPE: u32 = 1; #[derive(Error, Debug)] pub enum OnchainEventStorageError { @@ -436,7 +437,21 @@ impl OnchainEventStore { signer: Vec, ) -> Result, OnchainEventStorageError> { let signer_key = make_signer_onchain_event_by_signer_key(fid, signer); - get_event_by_secondary_key(&self.db, signer_key) + let signer = get_event_by_secondary_key(&self.db, signer_key) + .map_err(|e| OnchainEventStorageError::from(e))?; + if let Some(signer) = signer { + if let Some(body) = &signer.body { + if let on_chain_event::Body::SignerEventBody(signer_event_body) = body { + // Only return the signer if it's active (not removed) and the key type is supported + if signer_event_body.event_type() == SignerEventType::Add + && signer_event_body.key_type == SUPPORTED_SIGNER_KEY_TYPE + { + return Ok(Some(signer)); + } + } + } + } + Ok(None) } pub fn get_storage_slot_for_fid( diff --git a/src/storage/store/engine_tests.rs b/src/storage/store/engine_tests.rs index 271f0db..0884852 100644 --- a/src/storage/store/engine_tests.rs +++ b/src/storage/store/engine_tests.rs @@ -1049,6 +1049,7 @@ mod tests { FID_FOR_TEST, another_signer.clone(), proto::SignerEventType::Add, + None, ); test_helper::commit_event(&mut engine, &another_signer_event).await; test_helper::register_user( @@ -1080,6 +1081,7 @@ mod tests { FID_FOR_TEST, signer.clone(), proto::SignerEventType::Remove, + Some(timestamp + 3), ); test_helper::commit_event(&mut engine, &revoke_event).await; assert_onchain_hub_event(&event_rx.try_recv().unwrap(), &revoke_event); @@ -1095,6 +1097,15 @@ mod tests { // Different Fid with the same signer is unaffected let messages = engine.get_casts_by_fid(FID_FOR_TEST + 1).unwrap(); assert_eq!(1, messages.messages.len()); + + // Submitting a message from the revoked signer should fail + let post_revoke_message = messages_factory::casts::create_cast_add( + FID_FOR_TEST, + "after revoke", + Some(timestamp + 5), + Some(&signer), + ); + assert_commit_fails(&mut engine, &post_revoke_message).await; } #[tokio::test] @@ -1223,6 +1234,7 @@ mod tests { FID_FOR_TEST, test_helper::default_signer(), proto::SignerEventType::Add, + None, ), ) .await; @@ -1233,6 +1245,7 @@ mod tests { FID_FOR_TEST, proto::IdRegisterEventType::Register, vec![], + None, ); test_helper::commit_event(&mut engine, &id_register).await; commit_message(&mut engine, &default_message("msg1")).await; @@ -1254,6 +1267,7 @@ mod tests { FID_FOR_TEST, proto::IdRegisterEventType::Register, vec![], + None, ), ) .await; @@ -1266,6 +1280,7 @@ mod tests { FID_FOR_TEST, test_helper::default_signer(), proto::SignerEventType::Add, + None, ), ) .await; diff --git a/src/storage/store/test_helper.rs b/src/storage/store/test_helper.rs index 0a34d84..3bd7018 100644 --- a/src/storage/store/test_helper.rs +++ b/src/storage/store/test_helper.rs @@ -236,10 +236,11 @@ pub async fn register_user( fid, proto::IdRegisterEventType::Register, custody_address, + None, ); commit_event(engine, &id_register_event).await; let signer_event = - events_factory::create_signer_event(fid, signer, proto::SignerEventType::Add); + events_factory::create_signer_event(fid, signer, proto::SignerEventType::Add, None); commit_event(engine, &signer_event).await; } diff --git a/src/utils/factory.rs b/src/utils/factory.rs index 6e653d9..87965d3 100644 --- a/src/utils/factory.rs +++ b/src/utils/factory.rs @@ -88,10 +88,13 @@ pub mod events_factory { units: rent_units, payer: rand::random::<[u8; 32]>().to_vec(), }; + let random_number_under_1000 = rand::random::() % 1000; + // Ensure higher timestamp always has higher block number by left shifting the timestamp by 10 bits (1024) + let block_number = timestamp.checked_shl(10).unwrap() + random_number_under_1000; OnChainEvent { r#type: OnChainEventType::EventTypeStorageRent as i32, chain_id: 10, - block_number: rand::random::(), + block_number, block_hash: vec![], block_timestamp: timestamp as u64, transaction_hash: rand::random::<[u8; 32]>().to_vec(), @@ -109,6 +112,7 @@ pub mod events_factory { fid: u64, signer: SigningKey, event_type: proto::SignerEventType, + timestamp: Option, ) -> OnChainEvent { let signer_event_body = proto::SignerEventBody { key: signer.verifying_key().as_bytes().to_vec(), @@ -117,12 +121,16 @@ pub mod events_factory { key_type: 1, metadata_type: 1, }; + let block_timestamp = timestamp.unwrap_or_else(|| time::current_timestamp_with_offset(-10)); + let random_number_under_1000 = rand::random::() % 1000; + // Ensure higher timestamp always has higher block number by left shifting the timestamp by 10 bits (1024) + let block_number = block_timestamp.checked_shl(10).unwrap() + random_number_under_1000; OnChainEvent { r#type: OnChainEventType::EventTypeSigner as i32, chain_id: 10, - block_number: rand::random::(), + block_number, block_hash: vec![], - block_timestamp: time::current_timestamp_with_offset(-10) as u64, + block_timestamp: block_timestamp as u64, transaction_hash: rand::random::<[u8; 32]>().to_vec(), log_index: 0, fid, @@ -138,6 +146,7 @@ pub mod events_factory { fid: u64, event_type: proto::IdRegisterEventType, custody_address: Vec, + timestamp: Option, ) -> OnChainEvent { let id_register_event_body = proto::IdRegisterEventBody { to: custody_address, @@ -145,12 +154,16 @@ pub mod events_factory { from: vec![], recovery_address: vec![], }; + let block_timestamp = timestamp.unwrap_or_else(|| time::current_timestamp_with_offset(-10)); + let random_number_under_1000 = rand::random::() % 1000; + // Ensure higher timestamp always has higher block number by left shifting the timestamp by 10 bits (1024) + let block_number = block_timestamp.checked_shl(10).unwrap() + random_number_under_1000; OnChainEvent { - r#type: OnChainEventType::EventTypeSigner as i32, + r#type: OnChainEventType::EventTypeIdRegister as i32, chain_id: 10, - block_number: rand::random::(), + block_number, block_hash: vec![], - block_timestamp: time::current_timestamp_with_offset(-10) as u64, + block_timestamp: block_timestamp as u64, transaction_hash: rand::random::<[u8; 32]>().to_vec(), log_index: 0, fid,