chain: move BlockHeight into block
This commit is contained in:
parent
5f71bcd0d1
commit
dad6340cd3
|
@ -4,6 +4,7 @@
|
|||
mod difficulty;
|
||||
mod hash;
|
||||
mod header;
|
||||
mod height;
|
||||
mod light_client;
|
||||
mod serialize;
|
||||
|
||||
|
@ -17,11 +18,11 @@ use std::{error, sync::Arc};
|
|||
use proptest_derive::Arbitrary;
|
||||
|
||||
use crate::transaction::Transaction;
|
||||
use crate::types::BlockHeight;
|
||||
use crate::Network;
|
||||
|
||||
pub use hash::BlockHeaderHash;
|
||||
pub use header::BlockHeader;
|
||||
pub use height::BlockHeight;
|
||||
pub use light_client::LightClientRootHash;
|
||||
|
||||
/// A block in your blockchain.
|
||||
|
|
|
@ -0,0 +1,61 @@
|
|||
use crate::serialization::SerializationError;
|
||||
|
||||
/// A u32 which represents a block height value.
|
||||
///
|
||||
/// # Invariants
|
||||
///
|
||||
/// Users should not construct block heights greater than `BlockHeight::MAX`.
|
||||
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
|
||||
pub struct BlockHeight(pub u32);
|
||||
|
||||
impl std::str::FromStr for BlockHeight {
|
||||
type Err = SerializationError;
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
match s.parse() {
|
||||
Ok(h) if (BlockHeight(h) <= BlockHeight::MAX) => Ok(BlockHeight(h)),
|
||||
Ok(_) => Err(SerializationError::Parse(
|
||||
"BlockHeight exceeds maximum height",
|
||||
)),
|
||||
Err(_) => Err(SerializationError::Parse(
|
||||
"BlockHeight(u32) integer parse error",
|
||||
)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl BlockHeight {
|
||||
/// The minimum BlockHeight.
|
||||
///
|
||||
/// Due to the underlying type, it is impossible to construct block heights
|
||||
/// less than `BlockHeight::MIN`.
|
||||
///
|
||||
/// Style note: Sometimes, `BlockHeight::MIN` is less readable than
|
||||
/// `BlockHeight(0)`. Use whichever makes sense in context.
|
||||
pub const MIN: BlockHeight = BlockHeight(0);
|
||||
|
||||
/// The maximum BlockHeight.
|
||||
///
|
||||
/// Users should not construct block heights greater than `BlockHeight::MAX`.
|
||||
pub const MAX: BlockHeight = BlockHeight(499_999_999);
|
||||
|
||||
/// The maximum BlockHeight as a u32, for range patterns.
|
||||
///
|
||||
/// `BlockHeight::MAX.0` can't be used in match range patterns, use this
|
||||
/// alias instead.
|
||||
pub const MAX_AS_U32: u32 = Self::MAX.0;
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
use proptest::prelude::*;
|
||||
#[cfg(test)]
|
||||
impl Arbitrary for BlockHeight {
|
||||
type Parameters = ();
|
||||
|
||||
fn arbitrary_with(_args: ()) -> Self::Strategy {
|
||||
(BlockHeight::MIN.0..=BlockHeight::MAX.0)
|
||||
.prop_map(BlockHeight)
|
||||
.boxed()
|
||||
}
|
||||
|
||||
type Strategy = BoxedStrategy<Self>;
|
||||
}
|
|
@ -1,9 +1,10 @@
|
|||
//! The LightClientRootHash enum, used for the corresponding block header field.
|
||||
|
||||
use crate::treestate::note_commitment_tree::SaplingNoteTreeRootHash;
|
||||
use crate::types::BlockHeight;
|
||||
use crate::{Network, NetworkUpgrade, NetworkUpgrade::*};
|
||||
|
||||
use super::BlockHeight;
|
||||
|
||||
/// Light client root hashes.
|
||||
///
|
||||
/// The `BlockHeader.light_client_root_bytes` field is interpreted differently,
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
use NetworkUpgrade::*;
|
||||
|
||||
use crate::types::BlockHeight;
|
||||
use crate::block::BlockHeight;
|
||||
use crate::{Network, Network::*};
|
||||
|
||||
use std::collections::{BTreeMap, HashMap};
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
use super::*;
|
||||
use NetworkUpgrade::*;
|
||||
|
||||
use crate::types::BlockHeight;
|
||||
use crate::block::BlockHeight;
|
||||
use crate::{Network, Network::*};
|
||||
|
||||
use std::collections::HashSet;
|
||||
|
|
|
@ -19,8 +19,8 @@ pub use shielded_data::{Output, ShieldedData, Spend};
|
|||
pub use transparent::{CoinbaseData, OutPoint, TransparentInput, TransparentOutput};
|
||||
|
||||
use crate::amount::Amount;
|
||||
use crate::block::BlockHeight;
|
||||
use crate::proofs::{Bctv14Proof, Groth16Proof};
|
||||
use crate::types::BlockHeight;
|
||||
|
||||
/// A Zcash transaction.
|
||||
///
|
||||
|
|
|
@ -3,8 +3,8 @@ use std::io;
|
|||
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
|
||||
use chrono::{DateTime, TimeZone, Utc};
|
||||
|
||||
use crate::block::BlockHeight;
|
||||
use crate::serialization::{SerializationError, ZcashDeserialize, ZcashSerialize};
|
||||
use crate::types::BlockHeight;
|
||||
|
||||
/// A Bitcoin-style `locktime`, representing either a block height or an epoch
|
||||
/// time.
|
||||
|
|
|
@ -4,6 +4,7 @@ use proptest::{arbitrary::any, array, collection::vec, option, prelude::*};
|
|||
|
||||
use crate::{
|
||||
amount::{Amount, NonNegative},
|
||||
block::BlockHeight,
|
||||
commitments, keys,
|
||||
notes::{sapling, sprout},
|
||||
proofs::{Bctv14Proof, Groth16Proof, ZkSnarkProof},
|
||||
|
@ -12,7 +13,7 @@ use crate::{
|
|||
Transaction, TransparentInput, TransparentOutput,
|
||||
},
|
||||
treestate::{self, note_commitment_tree::SaplingNoteTreeRootHash},
|
||||
types::{BlockHeight, Script},
|
||||
types::Script,
|
||||
};
|
||||
|
||||
impl Transaction {
|
||||
|
|
|
@ -6,7 +6,8 @@ use proptest_derive::Arbitrary;
|
|||
|
||||
use crate::{
|
||||
amount::{Amount, NonNegative},
|
||||
types::{BlockHeight, Script},
|
||||
block::BlockHeight,
|
||||
types::Script,
|
||||
};
|
||||
|
||||
use super::TransactionHash;
|
||||
|
|
|
@ -8,64 +8,6 @@ use std::{
|
|||
io::{self, Read},
|
||||
};
|
||||
|
||||
/// A u32 which represents a block height value.
|
||||
///
|
||||
/// # Invariants
|
||||
///
|
||||
/// Users should not construct block heights greater than `BlockHeight::MAX`.
|
||||
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
|
||||
pub struct BlockHeight(pub u32);
|
||||
|
||||
impl std::str::FromStr for BlockHeight {
|
||||
type Err = SerializationError;
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
match s.parse() {
|
||||
Ok(h) if (BlockHeight(h) <= BlockHeight::MAX) => Ok(BlockHeight(h)),
|
||||
Ok(_) => Err(SerializationError::Parse(
|
||||
"BlockHeight exceeds maximum height",
|
||||
)),
|
||||
Err(_) => Err(SerializationError::Parse(
|
||||
"BlockHeight(u32) integer parse error",
|
||||
)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
impl Arbitrary for BlockHeight {
|
||||
type Parameters = ();
|
||||
|
||||
fn arbitrary_with(_args: ()) -> Self::Strategy {
|
||||
(BlockHeight::MIN.0..=BlockHeight::MAX.0)
|
||||
.prop_map(BlockHeight)
|
||||
.boxed()
|
||||
}
|
||||
|
||||
type Strategy = BoxedStrategy<Self>;
|
||||
}
|
||||
|
||||
impl BlockHeight {
|
||||
/// The minimum BlockHeight.
|
||||
///
|
||||
/// Due to the underlying type, it is impossible to construct block heights
|
||||
/// less than `BlockHeight::MIN`.
|
||||
///
|
||||
/// Style note: Sometimes, `BlockHeight::MIN` is less readable than
|
||||
/// `BlockHeight(0)`. Use whichever makes sense in context.
|
||||
pub const MIN: BlockHeight = BlockHeight(0);
|
||||
|
||||
/// The maximum BlockHeight.
|
||||
///
|
||||
/// Users should not construct block heights greater than `BlockHeight::MAX`.
|
||||
pub const MAX: BlockHeight = BlockHeight(499_999_999);
|
||||
|
||||
/// The maximum BlockHeight as a u32, for range patterns.
|
||||
///
|
||||
/// `BlockHeight::MAX.0` can't be used in match range patterns, use this
|
||||
/// alias instead.
|
||||
pub const MAX_AS_U32: u32 = Self::MAX.0;
|
||||
}
|
||||
|
||||
/// A sequence of message authentication tags ...
|
||||
///
|
||||
/// binding h_sig to each a_sk of the JoinSplit description, computed as
|
||||
|
@ -118,9 +60,6 @@ impl ZcashDeserialize for Script {
|
|||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
use proptest::prelude::*;
|
||||
|
||||
#[cfg(test)]
|
||||
mod proptests {
|
||||
use std::io::Cursor;
|
||||
|
|
|
@ -25,8 +25,8 @@ use std::{
|
|||
use tokio::time;
|
||||
use tower::{buffer::Buffer, Service, ServiceExt};
|
||||
|
||||
use zebra_chain::block::BlockHeight;
|
||||
use zebra_chain::block::{Block, BlockHeaderHash};
|
||||
use zebra_chain::types::BlockHeight;
|
||||
|
||||
/// A service that verifies blocks.
|
||||
#[derive(Debug)]
|
||||
|
|
|
@ -27,8 +27,8 @@ use std::{
|
|||
use tower::{buffer::Buffer, Service, ServiceExt};
|
||||
use tracing_futures::Instrument;
|
||||
|
||||
use zebra_chain::block::BlockHeight;
|
||||
use zebra_chain::block::{Block, BlockHeaderHash};
|
||||
use zebra_chain::types::BlockHeight;
|
||||
use zebra_chain::{Network, NetworkUpgrade::Sapling};
|
||||
|
||||
/// The maximum expected gap between blocks.
|
||||
|
|
|
@ -38,8 +38,8 @@ use std::{
|
|||
use tokio::sync::oneshot;
|
||||
use tower::Service;
|
||||
|
||||
use zebra_chain::block::BlockHeight;
|
||||
use zebra_chain::block::{Block, BlockHeaderHash};
|
||||
use zebra_chain::types::BlockHeight;
|
||||
use zebra_chain::Network;
|
||||
|
||||
/// The inner error type for CheckpointVerifier.
|
||||
|
|
|
@ -18,7 +18,7 @@ use std::{
|
|||
};
|
||||
|
||||
use zebra_chain::block::BlockHeaderHash;
|
||||
use zebra_chain::types::BlockHeight;
|
||||
use zebra_chain::block::BlockHeight;
|
||||
use zebra_chain::Network::{self, *};
|
||||
|
||||
const MAINNET_CHECKPOINTS: &str = include_str!("main-checkpoints.txt");
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
use std::cmp::Ordering;
|
||||
|
||||
use zebra_chain::types::BlockHeight;
|
||||
use zebra_chain::block::BlockHeight;
|
||||
|
||||
use Progress::*;
|
||||
use Target::*;
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
//! The minimum difficulty block rule for Zcash.
|
||||
|
||||
use zebra_chain::types::BlockHeight;
|
||||
use zebra_chain::block::BlockHeight;
|
||||
use zebra_chain::{Network, Network::*};
|
||||
|
||||
/// The testnet block height when minimum difficulty blocks start being
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
use super::*;
|
||||
|
||||
use zebra_chain::types::BlockHeight;
|
||||
use zebra_chain::block::BlockHeight;
|
||||
use zebra_chain::{Network, Network::*};
|
||||
|
||||
#[test]
|
||||
|
|
|
@ -18,7 +18,7 @@ use tower::Service;
|
|||
use tracing::{span, Level};
|
||||
use tracing_futures::Instrument;
|
||||
|
||||
use zebra_chain::types::BlockHeight;
|
||||
use zebra_chain::block::BlockHeight;
|
||||
|
||||
use crate::{
|
||||
constants,
|
||||
|
|
|
@ -9,13 +9,13 @@ use chrono::{TimeZone, Utc};
|
|||
use tokio_util::codec::{Decoder, Encoder};
|
||||
|
||||
use zebra_chain::{
|
||||
block::BlockHeight,
|
||||
block::{Block, BlockHeaderHash},
|
||||
serialization::{
|
||||
sha256d, ReadZcashExt, SerializationError as Error, WriteZcashExt, ZcashDeserialize,
|
||||
ZcashSerialize,
|
||||
},
|
||||
transaction::Transaction,
|
||||
types::BlockHeight,
|
||||
Network,
|
||||
};
|
||||
|
||||
|
|
|
@ -6,7 +6,7 @@ use std::{net, sync::Arc};
|
|||
use chrono::{DateTime, Utc};
|
||||
|
||||
use zebra_chain::block::{Block, BlockHeader, BlockHeaderHash};
|
||||
use zebra_chain::{transaction::Transaction, types::BlockHeight};
|
||||
use zebra_chain::{block::BlockHeight, transaction::Transaction};
|
||||
|
||||
use super::inv::InventoryHash;
|
||||
use super::types::*;
|
||||
|
|
|
@ -4,7 +4,7 @@ use crate::constants::magics;
|
|||
|
||||
use std::fmt;
|
||||
|
||||
use zebra_chain::types::BlockHeight;
|
||||
use zebra_chain::block::BlockHeight;
|
||||
use zebra_chain::Network::{self, *};
|
||||
use zebra_chain::NetworkUpgrade::{self, *};
|
||||
|
||||
|
|
|
@ -4,8 +4,8 @@ use std::{
|
|||
sync::Arc,
|
||||
};
|
||||
use zebra_chain::{
|
||||
block::BlockHeight,
|
||||
block::{Block, BlockHeaderHash},
|
||||
types::BlockHeight,
|
||||
};
|
||||
#[derive(Default)]
|
||||
pub(super) struct BlockIndex {
|
||||
|
|
|
@ -22,8 +22,8 @@ use std::{error, iter, sync::Arc};
|
|||
use tower::{Service, ServiceExt};
|
||||
|
||||
use zebra_chain::{
|
||||
block::BlockHeight,
|
||||
block::{Block, BlockHeaderHash},
|
||||
types::BlockHeight,
|
||||
Network,
|
||||
Network::*,
|
||||
};
|
||||
|
|
|
@ -13,8 +13,8 @@ use tower::{buffer::Buffer, util::BoxService, Service};
|
|||
use tracing::instrument;
|
||||
use zebra_chain::serialization::{SerializationError, ZcashDeserialize, ZcashSerialize};
|
||||
use zebra_chain::{
|
||||
block::BlockHeight,
|
||||
block::{Block, BlockHeaderHash},
|
||||
types::BlockHeight,
|
||||
Network,
|
||||
};
|
||||
|
||||
|
|
|
@ -18,7 +18,7 @@ use structopt::StructOpt;
|
|||
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
|
||||
|
||||
use zebra_chain::block::BlockHeaderHash;
|
||||
use zebra_chain::types::BlockHeight;
|
||||
use zebra_chain::block::BlockHeight;
|
||||
|
||||
#[cfg(unix)]
|
||||
use std::os::unix::process::ExitStatusExt;
|
||||
|
|
Loading…
Reference in New Issue