/ tests / cross_chain.rs
cross_chain.rs
  1  // Copyright (c) 2025 ALPHA/DELTA Network
  2  
  3  //! Cross-chain integration tests.
  4  //!
  5  //! Tests the lock/unlock flows between ALPHA and DELTA chains:
  6  //! - AX → sAX (lock on ALPHA, mint on DELTA)
  7  //! - sAX → AX (burn on DELTA, unlock on ALPHA)
  8  //!
  9  //! These tests verify the IPC message passing and cross-chain attestation system.
 10  
 11  use adnet_test_utils::{init_test_logging, TestRuntime, TestRuntimeBuilder};
 12  use std::time::Duration;
 13  
 14  /// Test that the test runtime can be created and started.
 15  #[tokio::test]
 16  async fn test_runtime_startup() {
 17      init_test_logging();
 18  
 19      let mut runtime = TestRuntimeBuilder::new()
 20          .validators(1)
 21          .both_chains()
 22          .build()
 23          .await
 24          .expect("Failed to create test runtime");
 25  
 26      // Start should succeed
 27      runtime.start().await.expect("Failed to start runtime");
 28  
 29      // Give it a moment to initialize
 30      tokio::time::sleep(Duration::from_millis(500)).await;
 31  
 32      // Shutdown should succeed
 33      runtime.shutdown().await.expect("Failed to shutdown runtime");
 34  }
 35  
 36  /// Test that multiple validators can be started.
 37  #[tokio::test]
 38  async fn test_multi_validator_startup() {
 39      init_test_logging();
 40  
 41      let mut runtime = TestRuntime::three_validators()
 42          .await
 43          .expect("Failed to create test runtime");
 44  
 45      runtime.start().await.expect("Failed to start runtime");
 46  
 47      // Verify we have 3 validators
 48      assert_eq!(runtime.validators().len(), 3);
 49  
 50      // All should be running
 51      for handle in runtime.validators() {
 52          assert!(handle.is_running().await);
 53      }
 54  
 55      runtime.shutdown().await.expect("Failed to shutdown runtime");
 56  }
 57  
 58  /// Test ALPHA-only mode.
 59  #[tokio::test]
 60  async fn test_alpha_only_mode() {
 61      init_test_logging();
 62  
 63      let mut runtime = TestRuntimeBuilder::new()
 64          .validators(1)
 65          .alpha_only()
 66          .build()
 67          .await
 68          .expect("Failed to create test runtime");
 69  
 70      runtime.start().await.expect("Failed to start runtime");
 71  
 72      // Verify configuration
 73      assert!(runtime.config.enable_alpha);
 74      assert!(!runtime.config.enable_delta);
 75  
 76      runtime.shutdown().await.expect("Failed to shutdown runtime");
 77  }
 78  
 79  /// Test DELTA-only mode.
 80  #[tokio::test]
 81  async fn test_delta_only_mode() {
 82      init_test_logging();
 83  
 84      let mut runtime = TestRuntimeBuilder::new()
 85          .validators(1)
 86          .delta_only()
 87          .build()
 88          .await
 89          .expect("Failed to create test runtime");
 90  
 91      runtime.start().await.expect("Failed to start runtime");
 92  
 93      // Verify configuration
 94      assert!(!runtime.config.enable_alpha);
 95      assert!(runtime.config.enable_delta);
 96  
 97      runtime.shutdown().await.expect("Failed to shutdown runtime");
 98  }
 99  
100  /// Test stopping and restarting a validator.
101  #[tokio::test]
102  async fn test_validator_restart() {
103      init_test_logging();
104  
105      let mut runtime = TestRuntime::three_validators()
106          .await
107          .expect("Failed to create test runtime");
108  
109      runtime.start().await.expect("Failed to start runtime");
110  
111      // Stop validator 1
112      runtime.stop_validator(1).await.expect("Failed to stop validator");
113  
114      // Verify it's stopped
115      assert!(!runtime.validator(1).unwrap().handle.is_running().await);
116  
117      // Other validators still running
118      assert!(runtime.validator(0).unwrap().handle.is_running().await);
119      assert!(runtime.validator(2).unwrap().handle.is_running().await);
120  
121      // Restart validator 1
122      runtime.restart_validator(1).await.expect("Failed to restart validator");
123  
124      // Verify it's running again
125      assert!(runtime.validator(1).unwrap().handle.is_running().await);
126  
127      runtime.shutdown().await.expect("Failed to shutdown runtime");
128  }
129  
130  /// Test with validators and clients.
131  #[tokio::test]
132  async fn test_validators_with_clients() {
133      init_test_logging();
134  
135      let mut runtime = TestRuntime::with_clients(2, 1)
136          .await
137          .expect("Failed to create test runtime");
138  
139      runtime.start().await.expect("Failed to start runtime");
140  
141      // Verify counts
142      assert_eq!(runtime.validators().len(), 2);
143      assert_eq!(runtime.clients().len(), 1);
144  
145      runtime.shutdown().await.expect("Failed to shutdown runtime");
146  }
147  
148  // The following tests are placeholders for when the actual node integration is complete.
149  // They document the intended test scenarios.
150  
151  /// Test AX lock and sAX mint flow.
152  ///
153  /// This test will verify:
154  /// 1. User locks AX on ALPHA chain (calls locked_pool.alpha::lock)
155  /// 2. Lock event is attested and sent to DELTA via IPC
156  /// 3. DELTA chain mints sAX to user (calls delta.sax::mint)
157  /// 4. User's sAX balance reflects the locked amount
158  #[tokio::test]
159  #[ignore = "Requires full node integration - placeholder for Phase 5 completion"]
160  async fn test_ax_to_sax_lock_flow() {
161      init_test_logging();
162  
163      let mut runtime = TestRuntime::three_validators()
164          .await
165          .expect("Failed to create test runtime");
166  
167      runtime.start().await.expect("Failed to start runtime");
168  
169      // Wait for nodes to sync
170      // runtime.wait_for_sync().await.expect("Nodes failed to sync");
171  
172      // 1. Submit lock transaction on ALPHA
173      // let lock_tx = create_lock_transaction(user_account, 1000);
174      // runtime.submit_alpha_tx(lock_tx).await.expect("Failed to submit lock tx");
175  
176      // 2. Wait for ALPHA finality (3 blocks)
177      // runtime.wait_blocks(3).await.expect("Failed to wait for blocks");
178  
179      // 3. Verify sAX minted on DELTA
180      // let sax_balance = runtime.get_delta_sax_balance(user_account).await;
181      // assert_eq!(sax_balance, 1000);
182  
183      runtime.shutdown().await.expect("Failed to shutdown runtime");
184  }
185  
186  /// Test sAX burn and AX unlock flow.
187  ///
188  /// This test will verify:
189  /// 1. User burns sAX on DELTA chain (calls delta.sax::burn)
190  /// 2. Burn event is attested and sent to ALPHA via IPC
191  /// 3. ALPHA chain unlocks AX to user (calls locked_pool.alpha::unlock)
192  /// 4. User's AX balance reflects the unlocked amount
193  #[tokio::test]
194  #[ignore = "Requires full node integration - placeholder for Phase 5 completion"]
195  async fn test_sax_to_ax_unlock_flow() {
196      init_test_logging();
197  
198      let mut runtime = TestRuntime::three_validators()
199          .await
200          .expect("Failed to create test runtime");
201  
202      runtime.start().await.expect("Failed to start runtime");
203  
204      // Setup: First lock some AX
205      // ... lock flow ...
206  
207      // 1. Submit burn transaction on DELTA
208      // let burn_tx = create_burn_transaction(user_account, 500);
209      // runtime.submit_delta_tx(burn_tx).await.expect("Failed to submit burn tx");
210  
211      // 2. Wait for DELTA finality
212      // runtime.wait_for_delta_blocks(5).await.expect("Failed to wait for blocks");
213  
214      // 3. Wait for ALPHA to process unlock
215      // runtime.wait_blocks(1).await.expect("Failed to wait for blocks");
216  
217      // 4. Verify AX unlocked on ALPHA
218      // let ax_balance = runtime.get_alpha_ax_balance(user_account).await;
219      // assert_eq!(ax_balance, initial_balance + 500);
220  
221      runtime.shutdown().await.expect("Failed to shutdown runtime");
222  }
223  
224  /// Test cross-chain attestation timing with different block times.
225  ///
226  /// With ALPHA=15s and DELTA=3s, there are 5 DELTA blocks per ALPHA block.
227  /// This test verifies attestations are processed correctly across this ratio.
228  #[tokio::test]
229  #[ignore = "Requires full node integration - placeholder for Phase 5 completion"]
230  async fn test_attestation_timing() {
231      init_test_logging();
232  
233      let mut runtime = TestRuntimeBuilder::new()
234          .validators(3)
235          .block_time_ms(1000) // Fast for testing
236          .build()
237          .await
238          .expect("Failed to create test runtime");
239  
240      runtime.start().await.expect("Failed to start runtime");
241  
242      // Verify block time relationship
243      let delta_per_alpha = runtime.config.delta_blocks_per_alpha_block();
244      assert_eq!(delta_per_alpha, 1); // In fast mode, 1:1
245  
246      // With phase2() config, it would be 5:1
247      // assert_eq!(RuntimeConfig::phase2().delta_blocks_per_alpha_block(), 5);
248  
249      runtime.shutdown().await.expect("Failed to shutdown runtime");
250  }