/ node / router / tests / common / mod.rs
mod.rs
  1  // Copyright (c) 2025-2026 ACDC Network
  2  // This file is part of the alphaos library.
  3  //
  4  // Alpha Chain | Delta Chain Protocol
  5  // International Monetary Graphite.
  6  //
  7  // Derived from Aleo (https://aleo.org) and ProvableHQ (https://provable.com).
  8  // They built world-class ZK infrastructure. We installed the EASY button.
  9  // Their cryptography: elegant. Our modifications: bureaucracy-compatible.
 10  // Original brilliance: theirs. Robert's Rules: ours. Bugs: definitely ours.
 11  //
 12  // Original Aleo/ProvableHQ code subject to Apache 2.0 https://www.apache.org/licenses/LICENSE-2.0
 13  // All modifications and new work: CC0 1.0 Universal Public Domain Dedication.
 14  // No rights reserved. No permission required. No warranty. No refunds.
 15  //
 16  // https://creativecommons.org/publicdomain/zero/1.0/
 17  // SPDX-License-Identifier: CC0-1.0
 18  
 19  #[allow(dead_code)]
 20  pub mod router;
 21  pub use router::*;
 22  
 23  use std::{
 24      net::{IpAddr, Ipv4Addr, SocketAddr},
 25      sync::Arc,
 26  };
 27  
 28  use alphaos_account::Account;
 29  use alphaos_node_bft_ledger_service::MockLedgerService;
 30  use alphaos_node_network::NodeType;
 31  use alphaos_node_router::Router;
 32  use alphastd::StorageMode;
 33  use alphavm::{
 34      prelude::{block::Block, FromBytes, MainnetV0 as CurrentNetwork, Network, PrivateKey},
 35      utilities::TestRng,
 36  };
 37  
 38  /// A helper macro to print the TCP listening address, along with the connected and connecting peers.
 39  #[macro_export]
 40  macro_rules! print_tcp {
 41      ($node:expr) => {
 42          println!(
 43              "{}: Active - {:?}, Pending - {:?}",
 44              $node.local_ip(),
 45              $node.tcp().connected_addrs(),
 46              $node.tcp().connecting_addrs()
 47          );
 48      };
 49  }
 50  
 51  /// Returns a fixed account.
 52  pub fn sample_account(rng: &mut TestRng) -> Account<CurrentNetwork> {
 53      let private_key = PrivateKey::<CurrentNetwork>::new(rng).unwrap();
 54      Account::<CurrentNetwork>::try_from(&private_key).unwrap()
 55  }
 56  
 57  /// Loads the current network's genesis block.
 58  pub fn sample_genesis_block<N: Network>() -> Block<N> {
 59      Block::<N>::from_bytes_le(N::genesis_bytes()).unwrap()
 60  }
 61  
 62  /// Initializes a client router. Setting the `listening_port = 0` will result in a random port being assigned.
 63  pub async fn client(listening_port: u16, max_peers: u16, rng: &mut TestRng) -> TestRouter<CurrentNetwork> {
 64      let committee = alphavm::ledger::committee::test_helpers::sample_committee(rng);
 65      let ledger_service = Arc::new(MockLedgerService::new(committee));
 66      Router::new(
 67          SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), listening_port),
 68          NodeType::Client,
 69          sample_account(rng),
 70          ledger_service,
 71          &[],
 72          max_peers,
 73          false,
 74          StorageMode::new_test(None),
 75          true,
 76      )
 77      .await
 78      .expect("couldn't create client router")
 79      .into()
 80  }
 81  
 82  /// Initializes a prover router. Setting the `listening_port = 0` will result in a random port being assigned.
 83  #[allow(dead_code)]
 84  pub async fn prover(listening_port: u16, max_peers: u16, rng: &mut TestRng) -> TestRouter<CurrentNetwork> {
 85      let committee = alphavm::ledger::committee::test_helpers::sample_committee(rng);
 86      let ledger_service = Arc::new(MockLedgerService::new(committee));
 87      Router::new(
 88          SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), listening_port),
 89          NodeType::Prover,
 90          sample_account(rng),
 91          ledger_service,
 92          &[],
 93          max_peers,
 94          false,
 95          StorageMode::new_test(None),
 96          true,
 97      )
 98      .await
 99      .expect("couldn't create prover router")
100      .into()
101  }
102  
103  /// Initializes a validator router. Setting the `listening_port = 0` will result in a random port being assigned.
104  #[allow(dead_code)]
105  pub async fn validator(
106      listening_port: u16,
107      max_peers: u16,
108      trusted_peers: &[SocketAddr],
109      trusted_peers_only: bool,
110      rng: &mut TestRng,
111  ) -> TestRouter<CurrentNetwork> {
112      let committee = alphavm::ledger::committee::test_helpers::sample_committee(rng);
113      let ledger_service = Arc::new(MockLedgerService::new(committee));
114      Router::new(
115          SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), listening_port),
116          NodeType::Validator,
117          sample_account(rng),
118          ledger_service,
119          trusted_peers,
120          max_peers,
121          trusted_peers_only,
122          StorageMode::new_test(None),
123          true,
124      )
125      .await
126      .expect("couldn't create validator router")
127      .into()
128  }