test_helpers.rs
1 // Copyright (c) 2019-2025 Alpha-Delta Network Inc. 2 // This file is part of the deltavm 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 pub mod chain_builder; 17 pub use chain_builder::TestChainBuilder; 18 19 use crate::Ledger; 20 use alphastd::StorageMode; 21 use console::{ 22 account::{Address, PrivateKey, ViewKey}, 23 network::MainnetV0, 24 prelude::*, 25 }; 26 27 use deltavm_circuit::AlphaV0; 28 use deltavm_ledger_store::ConsensusStore; 29 use deltavm_synthesizer::vm::VM; 30 31 pub use deltavm_ledger_test_helpers::*; 32 33 pub type CurrentNetwork = MainnetV0; 34 pub type CurrentAlpha = AlphaV0; 35 36 #[cfg(not(feature = "rocks"))] 37 pub type CurrentLedger = Ledger<CurrentNetwork, deltavm_ledger_store::helpers::memory::ConsensusMemory<CurrentNetwork>>; 38 #[cfg(feature = "rocks")] 39 pub type CurrentLedger = Ledger<CurrentNetwork, deltavm_ledger_store::helpers::rocksdb::ConsensusDB<CurrentNetwork>>; 40 41 #[cfg(not(feature = "rocks"))] 42 pub type LedgerType = deltavm_ledger_store::helpers::memory::ConsensusMemory<CurrentNetwork>; 43 #[cfg(feature = "rocks")] 44 pub type LedgerType = deltavm_ledger_store::helpers::rocksdb::ConsensusDB<CurrentNetwork>; 45 46 #[cfg(not(feature = "rocks"))] 47 pub type CurrentConsensusStore = 48 ConsensusStore<CurrentNetwork, deltavm_ledger_store::helpers::memory::ConsensusMemory<CurrentNetwork>>; 49 #[cfg(feature = "rocks")] 50 pub type CurrentConsensusStore = 51 ConsensusStore<CurrentNetwork, deltavm_ledger_store::helpers::rocksdb::ConsensusDB<CurrentNetwork>>; 52 53 #[cfg(not(feature = "rocks"))] 54 pub type CurrentConsensusStorage = deltavm_ledger_store::helpers::memory::ConsensusMemory<CurrentNetwork>; 55 #[cfg(feature = "rocks")] 56 pub type CurrentConsensusStorage = deltavm_ledger_store::helpers::rocksdb::ConsensusDB<CurrentNetwork>; 57 58 pub struct TestEnv { 59 pub ledger: CurrentLedger, 60 pub private_key: PrivateKey<CurrentNetwork>, 61 pub view_key: ViewKey<CurrentNetwork>, 62 pub address: Address<CurrentNetwork>, 63 } 64 65 pub fn sample_test_env(rng: &mut (impl Rng + CryptoRng)) -> TestEnv { 66 // Sample the genesis private key. 67 let private_key = PrivateKey::<CurrentNetwork>::new(rng).unwrap(); 68 let view_key = ViewKey::try_from(&private_key).unwrap(); 69 let address = Address::try_from(&private_key).unwrap(); 70 // Sample the ledger. 71 let ledger = sample_ledger(private_key, rng); 72 // Return the test environment. 73 TestEnv { ledger, private_key, view_key, address } 74 } 75 76 pub fn sample_ledger(private_key: PrivateKey<CurrentNetwork>, rng: &mut (impl Rng + CryptoRng)) -> CurrentLedger { 77 // Initialize the store. 78 let store = CurrentConsensusStore::open(StorageMode::new_test(None)).unwrap(); 79 // Create a genesis block. 80 let genesis = VM::from(store).unwrap().genesis_beacon(&private_key, rng).unwrap(); 81 // Initialize the ledger with the genesis block. 82 let ledger = CurrentLedger::load(genesis.clone(), StorageMode::new_test(None)).unwrap(); 83 // Ensure the genesis block is correct. 84 assert_eq!(genesis, ledger.get_block(0).unwrap()); 85 // Return the ledger. 86 ledger 87 }