#![allow(dead_code)] use essential_hash::content_addr; use essential_types::{ contract::Contract, predicate::Predicate, solution::{Solution, SolutionData}, Block, ConstraintBytecode, ContentAddress, PredicateAddress, StateReadBytecode, Word, }; use std::time::Duration; pub fn test_blocks(n: Word) -> Vec { (0..n) .map(|i| test_block(i, Duration::from_secs(i as _))) .collect() } pub fn test_block(number: Word, timestamp: Duration) -> Block { let seed = number * 79; Block { number, timestamp, solutions: (0..3).map(|i| test_solution(seed * (1 + i))).collect(), } } pub fn test_solution(seed: Word) -> Solution { Solution { data: vec![test_solution_data(seed)], } } pub fn test_solution_data(seed: Word) -> SolutionData { let contract = test_contract(seed); let predicate = essential_hash::content_addr(&contract.predicates[0]); let contract = essential_hash::content_addr(&contract); SolutionData { predicate_to_solve: PredicateAddress { contract, predicate, }, decision_variables: vec![], state_mutations: vec![], } } pub fn test_pred_addr() -> PredicateAddress { PredicateAddress { contract: [0xAA; 32].into(), predicate: [0xAA; 32].into(), } } pub fn test_contract(seed: Word) -> Contract { let n = (1 + seed % 2) as usize; Contract { // Make sure each predicate is unique, or contract will have a different // number of entries after insertion when multiple predicates have same CA. predicates: (0..n) .map(|ix| test_predicate(seed * (1 + ix as i64))) .collect(), salt: essential_types::convert::u8_32_from_word_4([seed; 4]), } } pub fn test_predicate(seed: Word) -> Predicate { Predicate { state_read: test_state_reads(seed), constraints: test_constraints(seed), } } // Resulting bytecode is invalid, but this is just for testing DB behaviour, not validation. pub fn test_state_reads(seed: Word) -> Vec { let n = (1 + seed % 3) as usize; let b = (seed % u8::MAX as Word) as u8; vec![vec![b; 10]; n] } // Resulting bytecode is invalid, but this is just for testing DB behaviour, not validation. pub fn test_constraints(seed: Word) -> Vec { let n = (1 + seed % 3) as usize; let b = (seed % u8::MAX as Word) as u8; vec![vec![b; 10]; n] } pub fn get_block_address(i: Word) -> ContentAddress { content_addr(&Block { number: i, timestamp: Default::default(), solutions: Default::default(), }) }