From 0d8ffc367e31025cf5351395780863bc4d456445 Mon Sep 17 00:00:00 2001 From: teor Date: Fri, 23 Apr 2021 13:19:33 +1000 Subject: [PATCH] Move LedgerState into the block module --- zebra-chain/src/block.rs | 3 ++ zebra-chain/src/block/arbitrary.rs | 40 ++++++++++++++++++++++-- zebra-chain/src/lib.rs | 39 +---------------------- zebra-chain/src/transaction/arbitrary.rs | 3 +- 4 files changed, 43 insertions(+), 42 deletions(-) diff --git a/zebra-chain/src/block.rs b/zebra-chain/src/block.rs index 4487cbe9..76f3dcfb 100644 --- a/zebra-chain/src/block.rs +++ b/zebra-chain/src/block.rs @@ -22,6 +22,9 @@ pub use header::{BlockTimeError, CountedHeader, Header}; pub use height::Height; pub use serialize::MAX_BLOCK_BYTES; +#[cfg(any(test, feature = "proptest-impl"))] +pub use arbitrary::LedgerState; + use serde::{Deserialize, Serialize}; use crate::{ diff --git a/zebra-chain/src/block/arbitrary.rs b/zebra-chain/src/block/arbitrary.rs index cb269574..ba3f3de8 100644 --- a/zebra-chain/src/block/arbitrary.rs +++ b/zebra-chain/src/block/arbitrary.rs @@ -7,13 +7,49 @@ use chrono::{TimeZone, Utc}; use std::sync::Arc; use crate::{ - parameters::Network, + parameters::{Network, NetworkUpgrade}, work::{difficulty::CompactDifficulty, equihash}, - LedgerState, }; use super::*; +#[derive(Debug, Clone, Copy)] +#[non_exhaustive] +/// The configuration data for proptest when generating arbitrary chains +pub struct LedgerState { + /// The tip height of the block or start of the chain + pub tip_height: Height, + /// The network to generate fake blocks for + pub network: Network, + + /// Make this fake transaction a coinbase transaction + pub(crate) is_coinbase: bool, +} + +impl LedgerState { + /// Construct a new ledger state for generating arbitrary chains via proptest + pub fn new(tip_height: Height, network: Network) -> Self { + Self { + tip_height, + is_coinbase: true, + network, + } + } +} + +impl Default for LedgerState { + fn default() -> Self { + let network = Network::Mainnet; + let tip_height = NetworkUpgrade::Canopy.activation_height(network).unwrap(); + + Self { + tip_height, + is_coinbase: true, + network, + } + } +} + impl Arbitrary for Block { type Parameters = LedgerState; diff --git a/zebra-chain/src/lib.rs b/zebra-chain/src/lib.rs index f8cfcd40..81ea3ae5 100644 --- a/zebra-chain/src/lib.rs +++ b/zebra-chain/src/lib.rs @@ -34,42 +34,5 @@ pub mod transaction; pub mod transparent; pub mod work; -#[derive(Debug, Clone, Copy)] #[cfg(any(test, feature = "proptest-impl"))] -#[non_exhaustive] -/// The configuration data for proptest when generating arbitrary chains -pub struct LedgerState { - /// The tip height of the block or start of the chain - pub tip_height: block::Height, - is_coinbase: bool, - /// The network to generate fake blocks for - pub network: parameters::Network, -} - -#[cfg(any(test, feature = "proptest-impl"))] -impl LedgerState { - /// Construct a new ledger state for generating arbitrary chains via proptest - pub fn new(tip_height: block::Height, network: parameters::Network) -> Self { - Self { - tip_height, - is_coinbase: true, - network, - } - } -} - -#[cfg(any(test, feature = "proptest-impl"))] -impl Default for LedgerState { - fn default() -> Self { - let network = parameters::Network::Mainnet; - let tip_height = parameters::NetworkUpgrade::Canopy - .activation_height(network) - .unwrap(); - - Self { - tip_height, - is_coinbase: true, - network, - } - } -} +pub use block::LedgerState; diff --git a/zebra-chain/src/transaction/arbitrary.rs b/zebra-chain/src/transaction/arbitrary.rs index d460d5c7..ab582382 100644 --- a/zebra-chain/src/transaction/arbitrary.rs +++ b/zebra-chain/src/transaction/arbitrary.rs @@ -3,13 +3,12 @@ use std::{convert::TryInto, sync::Arc}; use chrono::{TimeZone, Utc}; use proptest::{arbitrary::any, array, collection::vec, option, prelude::*}; -use crate::LedgerState; use crate::{ amount::Amount, block, parameters::NetworkUpgrade, primitives::{Bctv14Proof, Groth16Proof, ZkSnarkProof}, - sapling, sprout, transparent, + sapling, sprout, transparent, LedgerState, }; use super::{FieldNotPresent, JoinSplitData, LockTime, Memo, Transaction};