refactor: replace reverse with HeadersDirection (#305)

This commit is contained in:
Matthias Seitz
2022-12-01 17:59:33 +01:00
committed by GitHub
parent 53b1593849
commit a0c35f1f48
9 changed files with 100 additions and 48 deletions

View File

@ -269,6 +269,79 @@ impl SealedHeader {
}
}
/// Represents the direction for a headers request depending on the `reverse` field of the request.
///
/// [`HeadersDirection::Rising`] block numbers for `reverse == true`
/// [`HeadersDirection::Falling`] block numbers for `reverse == false`
///
/// See also <https://github.com/ethereum/devp2p/blob/master/caps/eth.md#getblockheaders-0x03>
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Default)]
pub enum HeadersDirection {
/// Falling block number.
#[default]
Falling,
/// Rising block number.
Rising,
}
impl HeadersDirection {
/// Returns true for rising block numbers
pub fn is_rising(&self) -> bool {
matches!(self, HeadersDirection::Rising)
}
/// Returns true for falling block numbers
pub fn is_falling(&self) -> bool {
matches!(self, HeadersDirection::Falling)
}
/// Converts the bool into a direction.
///
/// Returns:
///
/// [`HeadersDirection::Rising`] block numbers for `reverse == true`
/// [`HeadersDirection::Falling`] block numbers for `reverse == false`
pub fn new(reverse: bool) -> Self {
if reverse {
HeadersDirection::Rising
} else {
HeadersDirection::Falling
}
}
}
impl Encodable for HeadersDirection {
fn encode(&self, out: &mut dyn BufMut) {
bool::from(*self).encode(out)
}
fn length(&self) -> usize {
bool::from(*self).length()
}
}
impl Decodable for HeadersDirection {
fn decode(buf: &mut &[u8]) -> Result<Self, reth_rlp::DecodeError> {
let value: bool = Decodable::decode(buf)?;
Ok(value.into())
}
}
impl From<bool> for HeadersDirection {
fn from(reverse: bool) -> Self {
Self::new(reverse)
}
}
impl From<HeadersDirection> for bool {
fn from(value: HeadersDirection) -> Self {
match value {
HeadersDirection::Rising => true,
HeadersDirection::Falling => false,
}
}
}
#[cfg(test)]
mod tests {
use super::{Decodable, Encodable, Header, H256};

View File

@ -36,7 +36,7 @@ pub use constants::{EMPTY_OMMER_ROOT, KECCAK_EMPTY, MAINNET_GENESIS};
pub use ethbloom::Bloom;
pub use forkid::{ForkFilter, ForkHash, ForkId, ValidationError};
pub use hardfork::Hardfork;
pub use header::{Header, SealedHeader};
pub use header::{Header, HeadersDirection, SealedHeader};
pub use hex_bytes::Bytes;
pub use integer_list::IntegerList;
pub use jsonu256::JsonU256;