dev.rs
1 // Copyright (c) 2025 ADnet Contributors 2 // This file is part of the AlphaOS library. 3 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at: 7 8 // http://www.apache.org/licenses/LICENSE-2.0 9 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 16 use alphaos_node::{bft::MEMORY_POOL_PORT, router::DEFAULT_NODE_PORT}; 17 18 use alphavm::{console::network::Network, prelude::PrivateKey}; 19 20 use anyhow::Result; 21 use rand::SeedableRng; 22 use rand_chacha::ChaChaRng; 23 use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4}; 24 25 /// The development mode RNG seed. 26 pub const DEVELOPMENT_MODE_RNG_SEED: u64 = 1234567890u64; 27 28 /// The development mode number of genesis committee members. 29 pub const DEVELOPMENT_MODE_NUM_GENESIS_COMMITTEE_MEMBERS: u16 = 4; 30 31 /// The number of validators a devnet client connects to by default. 32 pub const DEVNET_NUM_VALIDATORS_PER_CLIENT: u16 = 2; 33 34 /// Get the private key for a validator in development mode. 35 pub fn get_development_key<N: Network>(index: u16) -> Result<PrivateKey<N>> { 36 // Sample the private key of this node. 37 // Initialize the (fixed) RNG. 38 let mut rng = ChaChaRng::seed_from_u64(DEVELOPMENT_MODE_RNG_SEED); 39 // Iterate through 'dev' address instances to match the account. 40 for _ in 0..index { 41 let _ = PrivateKey::<N>::new(&mut rng)?; 42 } 43 44 PrivateKey::<N>::new(&mut rng) 45 } 46 47 /// Returns the indicies of validators a particular devnet client will connect to. 48 pub fn get_devnet_validators_for_client(dev: u16, num_validators: u16) -> Vec<u16> { 49 (0..DEVNET_NUM_VALIDATORS_PER_CLIENT).map(|i| (dev + i) % num_validators).collect() 50 } 51 52 /// Returns the gateway address a particular devnet validator will listen on. 53 pub fn get_devnet_gateway_address_for_validator(dev: u16) -> SocketAddr { 54 SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::LOCALHOST, MEMORY_POOL_PORT + dev)) 55 } 56 57 /// Returns the router address a particular devnet validator will list on. 58 pub fn get_devnet_router_address_for_node(dev: u16) -> SocketAddr { 59 SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::LOCALHOST, DEFAULT_NODE_PORT + dev)) 60 }