|
| 1 | +// TODO(andrew): test non-nonce scenarios. |
| 2 | +use std::time::Duration; |
| 3 | + |
| 4 | +use libp2p::identity::Keypair; |
| 5 | +use libp2p::PeerId; |
| 6 | +use starknet_api::staking::StakingWeight; |
| 7 | +use tokio::sync::mpsc; |
| 8 | + |
| 9 | +use crate::config::Config; |
| 10 | +use crate::engine::{Engine, EngineCommand, EngineOutput, MessageKey}; |
| 11 | +use crate::message_processor::EventStateManagerToEngine; |
| 12 | +use crate::types::{CommitteeId, MessageRoot}; |
| 13 | +use crate::{MerkleProof, PropellerUnit, Shard, ShardIndex, ShardsOfPeer}; |
| 14 | + |
| 15 | +const TEST_COMMITTEE_ID: CommitteeId = CommitteeId([1; 32]); |
| 16 | +const BASE_NONCE: u64 = 1_000_000; |
| 17 | + |
| 18 | +fn test_config() -> Config { |
| 19 | + Config { stale_message_timeout: Duration::from_millis(200), ..Config::default() } |
| 20 | +} |
| 21 | + |
| 22 | +struct TestSetup { |
| 23 | + engine: Engine, |
| 24 | + publisher: PeerId, |
| 25 | + _output_rx: mpsc::UnboundedReceiver<EngineOutput>, |
| 26 | + _cmd_tx: mpsc::UnboundedSender<EngineCommand>, |
| 27 | +} |
| 28 | + |
| 29 | +fn setup() -> TestSetup { |
| 30 | + let local_keypair = Keypair::generate_ed25519(); |
| 31 | + let publisher_keypair = Keypair::generate_ed25519(); |
| 32 | + let publisher = PeerId::from(publisher_keypair.public()); |
| 33 | + |
| 34 | + let (cmd_tx, cmd_rx) = mpsc::unbounded_channel(); |
| 35 | + let (output_tx, output_rx) = mpsc::unbounded_channel(); |
| 36 | + |
| 37 | + let mut engine = Engine::new(local_keypair, test_config(), cmd_rx, output_tx, None); |
| 38 | + engine |
| 39 | + .register_committee( |
| 40 | + TEST_COMMITTEE_ID, |
| 41 | + vec![ |
| 42 | + (engine.local_peer_id, StakingWeight(10), None), |
| 43 | + (publisher, StakingWeight(10), Some(publisher_keypair.public())), |
| 44 | + ], |
| 45 | + ) |
| 46 | + .unwrap(); |
| 47 | + |
| 48 | + TestSetup { engine, publisher, _output_rx: output_rx, _cmd_tx: cmd_tx } |
| 49 | +} |
| 50 | + |
| 51 | +fn make_unit(publisher: PeerId, nonce: u64, root: MessageRoot) -> PropellerUnit { |
| 52 | + PropellerUnit::new( |
| 53 | + TEST_COMMITTEE_ID, |
| 54 | + publisher, |
| 55 | + root, |
| 56 | + vec![0; 64], |
| 57 | + ShardIndex(0), |
| 58 | + ShardsOfPeer(vec![Shard(vec![1, 2, 3])]), |
| 59 | + MerkleProof { siblings: vec![] }, |
| 60 | + nonce, |
| 61 | + ) |
| 62 | +} |
| 63 | + |
| 64 | +fn message_key(publisher: PeerId, nonce: u64, root: MessageRoot) -> MessageKey { |
| 65 | + MessageKey { committee_id: TEST_COMMITTEE_ID, publisher, nonce, root } |
| 66 | +} |
| 67 | + |
| 68 | +fn finalize_message(engine: &mut Engine, publisher: PeerId, nonce: u64, root: MessageRoot) { |
| 69 | + engine.handle_state_manager_message(EventStateManagerToEngine::Finalized { |
| 70 | + committee_id: TEST_COMMITTEE_ID, |
| 71 | + publisher, |
| 72 | + nonce, |
| 73 | + message_root: root, |
| 74 | + }); |
| 75 | +} |
| 76 | + |
| 77 | +#[tokio::test] |
| 78 | +async fn reject_unit_of_new_message_with_old_nonce() { |
| 79 | + let mut s = setup(); |
| 80 | + s.engine.peer_nonce.put(s.publisher, BASE_NONCE); |
| 81 | + |
| 82 | + // Nonce earlier than BASE_NONCE are rejected. |
| 83 | + let root_old = MessageRoot([1u8; 32]); |
| 84 | + s.engine.handle_unit(PeerId::random(), make_unit(s.publisher, BASE_NONCE - 1, root_old)); |
| 85 | + assert!( |
| 86 | + !s.engine.message_to_unit_tx.contains_key(&message_key( |
| 87 | + s.publisher, |
| 88 | + BASE_NONCE - 1, |
| 89 | + root_old |
| 90 | + )), |
| 91 | + "unit with nonce < BASE_NONCE must be rejected" |
| 92 | + ); |
| 93 | + |
| 94 | + // Nonce equal to BASE_NONCE: rejected. |
| 95 | + let root_eq = MessageRoot([0u8; 32]); |
| 96 | + s.engine.handle_unit(PeerId::random(), make_unit(s.publisher, BASE_NONCE, root_eq)); |
| 97 | + assert!( |
| 98 | + !s.engine.message_to_unit_tx.contains_key(&message_key(s.publisher, BASE_NONCE, root_eq)), |
| 99 | + "unit with nonce == BASE_NONCE must be rejected" |
| 100 | + ); |
| 101 | +} |
| 102 | + |
| 103 | +#[tokio::test] |
| 104 | +async fn allow_unit_of_new_message_with_fresh_nonce() { |
| 105 | + let mut s = setup(); |
| 106 | + s.engine.peer_nonce.put(s.publisher, BASE_NONCE); |
| 107 | + |
| 108 | + let root = MessageRoot([0u8; 32]); |
| 109 | + s.engine.handle_unit(PeerId::random(), make_unit(s.publisher, BASE_NONCE + 1, root)); |
| 110 | + |
| 111 | + assert!( |
| 112 | + s.engine.message_to_unit_tx.contains_key(&message_key(s.publisher, BASE_NONCE + 1, root)), |
| 113 | + "unit with nonce > BASE_NONCE must be accepted" |
| 114 | + ); |
| 115 | +} |
| 116 | + |
| 117 | +#[tokio::test] |
| 118 | +async fn nonce_updated_on_cache_expiry() { |
| 119 | + tokio::time::pause(); |
| 120 | + let mut s = setup(); |
| 121 | + |
| 122 | + // Finalize two messages from the same publisher with different nonces. |
| 123 | + finalize_message(&mut s.engine, s.publisher, BASE_NONCE + 100, MessageRoot([2u8; 32])); |
| 124 | + tokio::time::advance(Duration::from_millis(100)).await; |
| 125 | + finalize_message(&mut s.engine, s.publisher, BASE_NONCE, MessageRoot([1u8; 32])); |
| 126 | + |
| 127 | + // Before expiry: no nonce is cached (effective nonce = 0), so a unit with any nonce should be |
| 128 | + // accepted. |
| 129 | + let root_before = MessageRoot([10u8; 32]); |
| 130 | + s.engine.handle_unit(PeerId::random(), make_unit(s.publisher, BASE_NONCE - 50, root_before)); |
| 131 | + assert!( |
| 132 | + s.engine.message_to_unit_tx.contains_key(&message_key( |
| 133 | + s.publisher, |
| 134 | + BASE_NONCE - 50, |
| 135 | + root_before |
| 136 | + )), |
| 137 | + "unit must be accepted when nonce cache has no entry for this publisher" |
| 138 | + ); |
| 139 | + |
| 140 | + // Advance past TTL so both messages expire, then trigger cleanup via a third finalization. |
| 141 | + tokio::time::advance(test_config().stale_message_timeout + Duration::from_millis(50)).await; |
| 142 | + finalize_message(&mut s.engine, s.publisher, BASE_NONCE + 200, MessageRoot([3u8; 32])); |
| 143 | + |
| 144 | + // After expiry: nonce advances to max(BASE_NONCE, BASE_NONCE+100) = BASE_NONCE+100. |
| 145 | + // A unit with nonce <= BASE_NONCE+100 must now be rejected. |
| 146 | + let root_stale = MessageRoot([11u8; 32]); |
| 147 | + s.engine.handle_unit(PeerId::random(), make_unit(s.publisher, BASE_NONCE + 50, root_stale)); |
| 148 | + assert!( |
| 149 | + !s.engine.message_to_unit_tx.contains_key(&message_key( |
| 150 | + s.publisher, |
| 151 | + BASE_NONCE + 50, |
| 152 | + root_stale |
| 153 | + )), |
| 154 | + "unit with nonce <= max expired nonce must be rejected after cache expiry" |
| 155 | + ); |
| 156 | + |
| 157 | + // A unit with nonce strictly above BASE_NONCE+100 must still be accepted. |
| 158 | + let root_fresh = MessageRoot([12u8; 32]); |
| 159 | + s.engine.handle_unit(PeerId::random(), make_unit(s.publisher, BASE_NONCE + 101, root_fresh)); |
| 160 | + assert!( |
| 161 | + s.engine.message_to_unit_tx.contains_key(&message_key( |
| 162 | + s.publisher, |
| 163 | + BASE_NONCE + 101, |
| 164 | + root_fresh |
| 165 | + )), |
| 166 | + "unit with nonce > max expired nonce must be accepted after cache expiry" |
| 167 | + ); |
| 168 | +} |
0 commit comments