Skip to main content

snarkos_cli/helpers/
dev.rs

1// Copyright (c) 2019-2026 Provable Inc.
2// This file is part of the snarkOS 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
16use snarkos_node::{bft::MEMORY_POOL_PORT, router::DEFAULT_NODE_PORT};
17
18use snarkvm::{console::network::Network, prelude::PrivateKey};
19
20use anyhow::Result;
21use rand::SeedableRng;
22use rand_chacha::ChaChaRng;
23pub use snarkos_utilities::DEVELOPMENT_MODE_RNG_SEED;
24use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4};
25
26/// The development mode number of genesis committee members.
27pub const DEVELOPMENT_MODE_NUM_GENESIS_COMMITTEE_MEMBERS: u16 = 4;
28
29/// The number of validators a devnet client connects to by default.
30pub const DEVNET_NUM_VALIDATORS_PER_CLIENT: u16 = 2;
31
32/// Get the private key for a validator in development mode.
33pub fn get_development_key<N: Network>(index: u16) -> Result<PrivateKey<N>> {
34    // Sample the private key of this node.
35    // Initialize the (fixed) RNG.
36    let mut rng = ChaChaRng::seed_from_u64(DEVELOPMENT_MODE_RNG_SEED);
37    // Iterate through 'dev' address instances to match the account.
38    for _ in 0..index {
39        let _ = PrivateKey::<N>::new(&mut rng)?;
40    }
41
42    PrivateKey::<N>::new(&mut rng)
43}
44
45/// Returns the indicies of validators a particular devnet client will connect to.
46pub fn get_devnet_validators_for_client(dev: u16, num_validators: u16) -> Vec<u16> {
47    (0..DEVNET_NUM_VALIDATORS_PER_CLIENT).map(|i| (dev + i) % num_validators).collect()
48}
49
50/// Returns the gateway address a particular devnet validator will listen on.
51pub fn get_devnet_gateway_address_for_validator(dev: u16) -> SocketAddr {
52    SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::LOCALHOST, MEMORY_POOL_PORT + dev))
53}
54
55/// Returns the router address a particular devnet validator will list on.
56pub fn get_devnet_router_address_for_node(dev: u16) -> SocketAddr {
57    SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::LOCALHOST, DEFAULT_NODE_PORT + dev))
58}