diff --git a/zebra-consensus/src/verify.rs b/zebra-consensus/src/verify.rs index 1f389dacb..3ba763839 100644 --- a/zebra-consensus/src/verify.rs +++ b/zebra-consensus/src/verify.rs @@ -20,6 +20,7 @@ use tower::{buffer::Buffer, Service}; use zebra_chain::block::{Block, BlockHeaderHash}; +mod block; mod script; mod transaction; @@ -31,50 +32,6 @@ struct BlockVerifier { // TODO(jlusby): Error = Report ? type Error = Box; -/// Block validity checks -mod block { - use super::Error; - - use chrono::{DateTime, Duration, Utc}; - use std::sync::Arc; - - use zebra_chain::block::Block; - - /// Helper function for `node_time_check()`, see that function for details. - fn node_time_check_helper( - block_header_time: DateTime, - now: DateTime, - ) -> Result<(), Error> { - let two_hours_in_the_future = now - .checked_add_signed(Duration::hours(2)) - .ok_or("overflow when calculating 2 hours in the future")?; - - if block_header_time <= two_hours_in_the_future { - Ok(()) - } else { - Err("block header time is more than 2 hours in the future".into()) - } - } - - /// Check if the block header time is less than or equal to - /// 2 hours in the future, according to the node's local clock. - /// - /// This is a non-deterministic rule, as clocks vary over time, and - /// between different nodes. - /// - /// "In addition, a full validator MUST NOT accept blocks with nTime - /// more than two hours in the future according to its clock. This - /// is not strictly a consensus rule because it is nondeterministic, - /// and clock time varies between nodes. Also note that a block that - /// is rejected by this rule at a given point in time may later be - /// accepted."[S 7.5][7.5] - /// - /// [7.5]: https://zips.z.cash/protocol/protocol.pdf#blockheader - pub(super) fn node_time_check(block: Arc) -> Result<(), Error> { - node_time_check_helper(block.header.time, Utc::now()) - } -} - /// The BlockVerifier service implementation. /// /// After verification, blocks are added to the underlying state service. diff --git a/zebra-consensus/src/verify/block.rs b/zebra-consensus/src/verify/block.rs new file mode 100644 index 000000000..4543cff96 --- /dev/null +++ b/zebra-consensus/src/verify/block.rs @@ -0,0 +1,42 @@ +//! Block validity checks + +use super::Error; + +use chrono::{DateTime, Duration, Utc}; +use std::sync::Arc; + +use zebra_chain::block::Block; + +/// Helper function for `node_time_check()`, see that function for details. +fn node_time_check_helper( + block_header_time: DateTime, + now: DateTime, +) -> Result<(), Error> { + let two_hours_in_the_future = now + .checked_add_signed(Duration::hours(2)) + .ok_or("overflow when calculating 2 hours in the future")?; + + if block_header_time <= two_hours_in_the_future { + Ok(()) + } else { + Err("block header time is more than 2 hours in the future".into()) + } +} + +/// Check if the block header time is less than or equal to +/// 2 hours in the future, according to the node's local clock. +/// +/// This is a non-deterministic rule, as clocks vary over time, and +/// between different nodes. +/// +/// "In addition, a full validator MUST NOT accept blocks with nTime +/// more than two hours in the future according to its clock. This +/// is not strictly a consensus rule because it is nondeterministic, +/// and clock time varies between nodes. Also note that a block that +/// is rejected by this rule at a given point in time may later be +/// accepted."[S 7.5][7.5] +/// +/// [7.5]: https://zips.z.cash/protocol/protocol.pdf#blockheader +pub(super) fn node_time_check(block: Arc) -> Result<(), Error> { + node_time_check_helper(block.header.time, Utc::now()) +}