Move just the Network enum to -chain, keep everything else in -network

This commit is contained in:
Deirdre Connolly 2020-03-12 19:34:34 -04:00 committed by Deirdre Connolly
parent 380d622b37
commit a5f4db7528
8 changed files with 58 additions and 62 deletions

View File

@ -11,9 +11,9 @@ use sha2::Sha256;
use proptest_derive::Arbitrary;
use crate::{
network::Network,
serialization::{SerializationError, ZcashDeserialize, ZcashSerialize},
types::Script,
Network,
};
/// A hash of a redeem script, as used in transparent

View File

@ -14,7 +14,6 @@ pub mod addresses;
pub mod block;
pub mod equihash_solution;
pub mod keys;
pub mod network;
pub mod note_commitment_tree;
pub mod note_encryption;
pub mod proofs;
@ -23,3 +22,12 @@ pub mod transaction;
pub mod types;
pub use redjubjub;
/// An enum describing the possible network choices.
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)]
pub enum Network {
/// The production mainnet.
Mainnet,
/// The testnet.
Testnet,
}

View File

@ -1,31 +0,0 @@
//! Network-specific types.
use crate::types::Magic;
/// An enum describing the possible network choices.
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)]
pub enum Network {
/// The production mainnet.
Mainnet,
/// The testnet.
Testnet,
}
impl Network {
/// Get the magic value associated to this `Network`.
pub fn magic(self) -> Magic {
match self {
Network::Mainnet => magics::MAINNET,
Network::Testnet => magics::TESTNET,
}
}
}
/// Magic numbers used to identify different Zcash networks.
pub mod magics {
use super::*;
/// The production mainnet.
pub const MAINNET: Magic = Magic([0x24, 0xe9, 0x27, 0x64]);
/// The testnet.
pub const TESTNET: Magic = Magic([0xfa, 0x1a, 0xf9, 0xbf]);
}

View File

@ -15,17 +15,6 @@ use crate::serialization::{
ReadZcashExt, SerializationError, WriteZcashExt, ZcashDeserialize, ZcashSerialize,
};
/// A magic number identifying the network.
#[derive(Copy, Clone, Eq, PartialEq)]
#[cfg_attr(test, derive(Arbitrary))]
pub struct Magic(pub [u8; 4]);
impl fmt::Debug for Magic {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_tuple("Magic").field(&hex::encode(&self.0)).finish()
}
}
/// A 4-byte checksum using truncated double-SHA256 (two rounds of SHA256).
#[derive(Copy, Clone, Eq, PartialEq)]
pub struct Sha256dChecksum(pub [u8; 4]);

View File

@ -4,7 +4,7 @@ use std::{
time::Duration,
};
use zebra_chain::network::Network;
use zebra_chain::Network;
/// Configuration for networking code.
#[derive(Clone, Debug, Deserialize, Serialize)]

View File

@ -40,6 +40,15 @@ pub const CURRENT_VERSION: Version = Version(170_009);
/// The minimum version supported for peer connections.
pub const MIN_VERSION: Version = Version(170_009);
/// Magic numbers used to identify different Zcash networks.
pub mod magics {
use super::*;
/// The production mainnet.
pub const MAINNET: Magic = Magic([0x24, 0xe9, 0x27, 0x64]);
/// The testnet.
pub const TESTNET: Magic = Magic([0xfa, 0x1a, 0xf9, 0xbf]);
}
#[cfg(test)]
mod tests {

View File

@ -10,12 +10,12 @@ use tokio_util::codec::{Decoder, Encoder};
use zebra_chain::{
block::{Block, BlockHeaderHash},
network::Network,
serialization::{
ReadZcashExt, SerializationError as Error, WriteZcashExt, ZcashDeserialize, ZcashSerialize,
},
transaction::Transaction,
types::{BlockHeight, Magic, Sha256dChecksum},
types::{BlockHeight, Sha256dChecksum},
Network,
};
use crate::constants;
@ -136,7 +136,7 @@ impl Encoder for Codec {
// but leave it for now until we fix the issue above.
let mut header = [0u8; HEADER_LEN];
let mut header_writer = Cursor::new(&mut header[..]);
header_writer.write_all(&self.builder.network.magic().0)?;
header_writer.write_all(&Magic::from(self.builder.network).0[..])?;
header_writer.write_all(command)?;
header_writer.write_u32::<LittleEndian>(body.len() as u32)?;
header_writer.write_all(&Sha256dChecksum::from(&body[..]).0)?;
@ -309,7 +309,7 @@ impl Decoder for Codec {
"read header from src buffer"
);
if magic != self.builder.network.magic() {
if magic != Magic::from(self.builder.network) {
return Err(Parse("supplied magic did not meet expectations"));
}
if body_len >= self.builder.max_len {

View File

@ -3,6 +3,31 @@ use std::fmt;
#[cfg(test)]
use proptest_derive::Arbitrary;
use zebra_chain::Network;
use crate::constants::magics;
/// A magic number identifying the network.
#[derive(Copy, Clone, Eq, PartialEq)]
#[cfg_attr(test, derive(Arbitrary))]
pub struct Magic(pub [u8; 4]);
impl fmt::Debug for Magic {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_tuple("Magic").field(&hex::encode(&self.0)).finish()
}
}
impl From<Network> for Magic {
/// Get the magic value associated to this `Network`.
fn from(network: Network) -> Self {
match network {
Network::Mainnet => magics::MAINNET,
Network::Testnet => magics::TESTNET,
}
}
}
/// A protocol version number.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct Version(pub u32);
@ -48,18 +73,6 @@ impl Default for Tweak {
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Filter(pub Vec<u8>);
#[cfg(test)]
mod tests {
use zebra_chain::network::magics;
#[test]
fn magic_debug() {
assert_eq!(format!("{:?}", magics::MAINNET), "Magic(\"24e92764\")");
assert_eq!(format!("{:?}", magics::TESTNET), "Magic(\"fa1af9bf\")");
}
}
#[cfg(test)]
mod proptest {
@ -67,7 +80,15 @@ mod proptest {
use proptest::prelude::*;
use zebra_chain::types::Magic;
use super::Magic;
use crate::constants::magics;
#[test]
fn magic_debug() {
assert_eq!(format!("{:?}", magics::MAINNET), "Magic(\"24e92764\")");
assert_eq!(format!("{:?}", magics::TESTNET), "Magic(\"fa1af9bf\")");
}
proptest! {