Skip to content

Commit

Permalink
change(chain): Remove Copy trait impl from Network (#8354)
Browse files Browse the repository at this point in the history
* removed derive copy from network, address and ledgerstate

* changed is_max_block_time_enforced to accept ref

* changed NetworkUpgrade::Current to accept ref

* changed NetworkUpgrade::Next to accept ref

* changed NetworkUpgrade::IsActivationHeight to accept ref

* changed NetworkUpgrade::TargetSpacingForHeight to accept ref

* changed NetworkUpgrade::TargetSpacings to accept ref

* changed NetworkUpgrade::MinimumDifficultySpacing_forHeight to accept ref

* changed NetworkUpgrade::IsTestnetMinDifficultyBlock to accept ref

* changed NetworkUpgrade::AveragingWindowTimespanForHeight to accept ref

* changed NetworkUpgrade::ActivationHeight to accept ref

* changed sapling_activation_height to accept ref

* fixed lifetime for target_spacings

* fixed sapling_activation_height

* changed transaction_to_fake_v5 and fake_v5_transactions_for_network to accept ref to network

* changed Input::vec_strategy to accept ref to network

* changed functions in zebra-chain/src/primitives/zcash_history.rs to accept ref to network

* changed functions in zebra-chain/src/history_tree.rs to accept ref to network

* changed functions in zebra-chain/src/history_tree.rs to accept ref to network

* changed functions in zebra-chain/src/primitives/address.rs to accept ref to network

* changed functions in zebra-chain/src/primitives/viewing_key* to accept ref to network

* changed functions in zebra-chain/src/transparent/address.rs to accept ref to network

* changed functions in zebra-chain/src/primitives/zcash_primitives.rs to accept ref to network

* changed functions in zebra-chain/src/primitives/zcash_note_encryption.rs to accept ref to network

* changed functions in zebra-chain/src/primitives/history_tree* to accept ref to network

* changed functions in zebra-chain/src/block* to accept ref to network

* fixed errors in zebra-chain::parameters::network

* fixed errors in zebra-chain::parameters::network

* fixed errors in zebra-chain

* changed NonEmptyHistoryTree and InnerHistoryTree to hold value instead of ref

* changed NonEmptyHistoryTree and InnerHistoryTree to hold value instead of ref

* fixed errors in zebra-chain/src/block/arbitrary.rs

* finished fixing errors in zebra-chain - all crate tests pass

* changed functions in zebra-state::service::finalized_state to accept &Network

* changed functions in zebra-state::service::non_finalized_state to accept &Network

* zebra-state tests run but fail with overflow error

* zebra-state tests all pass

* converted zebra-network -- all crate tests pass

* applied all requested changes from review

* converted zebra-consensus -- all crate tests pass

* converted zebra-scan -- all crate tests pass

* converted zebra-rpc -- all crate tests pass

* converted zebra-grpc -- all crate tests pass

* converted zebrad -- all crate tests pass

* applied all requested changes from review

* fixed all clippy errors

* fixed build error in zebrad/src/components/mempool/crawler.rs
  • Loading branch information
idky137 authored Mar 19, 2024
1 parent 0c77b40 commit 4579722
Show file tree
Hide file tree
Showing 165 changed files with 1,052 additions and 993 deletions.
4 changes: 2 additions & 2 deletions zebra-chain/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ impl Block {
///
/// Returns an error if this block does not have a block height,
/// or if the commitment value is structurally invalid.
pub fn commitment(&self, network: Network) -> Result<Commitment, CommitmentError> {
pub fn commitment(&self, network: &Network) -> Result<Commitment, CommitmentError> {
match self.coinbase_height() {
None => Err(CommitmentError::MissingBlockHeight {
block_hash: self.hash(),
Expand All @@ -121,7 +121,7 @@ impl Block {
#[allow(clippy::unwrap_in_result)]
pub fn check_transaction_network_upgrade_consistency(
&self,
network: Network,
network: &Network,
) -> Result<(), error::BlockError> {
let block_nu =
NetworkUpgrade::current(network, self.coinbase_height().expect("a valid height"));
Expand Down
36 changes: 19 additions & 17 deletions zebra-chain/src/block/arbitrary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl Arbitrary for Height {
type Strategy = BoxedStrategy<Self>;
}

#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone)]
#[non_exhaustive]
/// The configuration data for proptest when generating arbitrary chains
pub struct LedgerState {
Expand Down Expand Up @@ -245,7 +245,7 @@ impl LedgerState {
if let Some(network_upgrade_override) = self.network_upgrade_override {
network_upgrade_override
} else {
NetworkUpgrade::current(self.network, self.height)
NetworkUpgrade::current(&self.network, self.height)
}
}

Expand All @@ -268,9 +268,9 @@ impl Default for LedgerState {
let default_network = Network::default();
let default_override = LedgerStateOverride::default();

let most_recent_nu = NetworkUpgrade::current(default_network, Height::MAX);
let most_recent_nu = NetworkUpgrade::current(&default_network, Height::MAX);
let most_recent_activation_height =
most_recent_nu.activation_height(default_network).unwrap();
most_recent_nu.activation_height(&default_network).unwrap();

LedgerState {
height: most_recent_activation_height,
Expand All @@ -290,7 +290,7 @@ impl Default for LedgerStateOverride {
let default_network = Network::default();

// TODO: dynamically select any future network upgrade (#1974)
let nu5_activation_height = Nu5.activation_height(default_network);
let nu5_activation_height = Nu5.activation_height(&default_network);
let nu5_override = if nu5_activation_height.is_some() {
None
} else {
Expand Down Expand Up @@ -348,12 +348,14 @@ impl Arbitrary for Block {
type Parameters = LedgerState;

fn arbitrary_with(ledger_state: Self::Parameters) -> Self::Strategy {
let transactions_strategy =
let transactions_strategy = {
let ledger_state = ledger_state.clone();
// Generate a random number transactions. A coinbase tx is always generated, so if
// `transaction_count` is zero, the block will contain only the coinbase tx.
(0..MAX_ARBITRARY_ITEMS).prop_flat_map(move |transaction_count| {
Transaction::vec_strategy(ledger_state, transaction_count)
});
Transaction::vec_strategy(ledger_state.clone(), transaction_count)
})
};

// TODO: if needed, fixup:
// - history and authorizing data commitments
Expand Down Expand Up @@ -411,7 +413,7 @@ impl Block {

// generate block strategies with the correct heights
for _ in 0..count {
vec.push((Just(current.height), Block::arbitrary_with(current)));
vec.push((Just(current.height), Block::arbitrary_with(current.clone())));
current.height.0 += 1;
}

Expand Down Expand Up @@ -473,9 +475,9 @@ impl Block {
if generate_valid_commitments {
let current_height = block.coinbase_height().unwrap();
let heartwood_height = NetworkUpgrade::Heartwood
.activation_height(current.network)
.activation_height(&current.network)
.unwrap();
let nu5_height = NetworkUpgrade::Nu5.activation_height(current.network);
let nu5_height = NetworkUpgrade::Nu5.activation_height(&current.network);

match current_height.cmp(&heartwood_height) {
std::cmp::Ordering::Less => {
Expand Down Expand Up @@ -520,16 +522,16 @@ impl Block {
if let Some(history_tree) = history_tree.as_mut() {
history_tree
.push(
current.network,
&current.network,
Arc::new(block.clone()),
sapling_tree.root(),
orchard_tree.root(),
&sapling_tree.root(),
&orchard_tree.root(),
)
.unwrap();
} else {
history_tree = Some(
HistoryTree::from_block(
current.network,
&current.network,
Arc::new(block.clone()),
&sapling_tree.root(),
&orchard_tree.root(),
Expand Down Expand Up @@ -703,10 +705,10 @@ impl Arbitrary for Commitment {
fn arbitrary_with(_args: ()) -> Self::Strategy {
(any::<[u8; 32]>(), any::<Network>(), any::<Height>())
.prop_map(|(commitment_bytes, network, block_height)| {
if block_height == Heartwood.activation_height(network).unwrap() {
if block_height == Heartwood.activation_height(&network).unwrap() {
Commitment::ChainHistoryActivationReserved
} else {
Commitment::from_bytes(commitment_bytes, network, block_height)
Commitment::from_bytes(commitment_bytes, &network, block_height)
.expect("unexpected failure in from_bytes parsing")
}
})
Expand Down
2 changes: 1 addition & 1 deletion zebra-chain/src/block/commitment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ impl Commitment {
// TODO: rename as from_bytes_in_serialized_order()
pub(super) fn from_bytes(
bytes: [u8; 32],
network: Network,
network: &Network,
height: block::Height,
) -> Result<Commitment, CommitmentError> {
use Commitment::*;
Expand Down
4 changes: 2 additions & 2 deletions zebra-chain/src/block/tests/prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ proptest! {

// just skip the test if the bytes don't parse, because there's nothing
// to compare with
if let Ok(commitment) = Commitment::from_bytes(bytes, network, block_height) {
if let Ok(commitment) = Commitment::from_bytes(bytes, &network, block_height) {
let other_bytes = commitment.to_bytes();

prop_assert_eq![bytes, other_bytes];
Expand All @@ -104,7 +104,7 @@ proptest! {
let bytes = block.zcash_serialize_to_vec()?;

// Check the block commitment
let commitment = block.commitment(network);
let commitment = block.commitment(&network);
if let Ok(commitment) = commitment {
let commitment_bytes = commitment.to_bytes();
prop_assert_eq![block.header.commitment_bytes.0, commitment_bytes];
Expand Down
4 changes: 2 additions & 2 deletions zebra-chain/src/block/tests/vectors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ fn block_test_vectors_height(network: Network) {

if height
>= Sapling
.activation_height(network)
.activation_height(&network)
.expect("sapling activation height is set")
.0
{
Expand Down Expand Up @@ -260,7 +260,7 @@ fn block_commitment(network: Network) {
.zcash_deserialize_into::<Block>()
.expect("block is structurally valid");

let commitment = block.commitment(network).unwrap_or_else(|_| {
let commitment = block.commitment(&network).unwrap_or_else(|_| {
panic!("unexpected structurally invalid block commitment at {network} {height}")
});

Expand Down
4 changes: 2 additions & 2 deletions zebra-chain/src/chain_tip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ pub trait ChainTip {
/// and the height of the best tip.
fn estimate_network_chain_tip_height(
&self,
network: Network,
network: &Network,
now: DateTime<Utc>,
) -> Option<block::Height> {
let (current_height, current_block_time) = self.best_tip_height_and_block_time()?;
Expand All @@ -110,7 +110,7 @@ pub trait ChainTip {
/// Returns `None` if the state is empty.
fn estimate_distance_to_network_chain_tip(
&self,
network: Network,
network: &Network,
) -> Option<(block::HeightDiff, block::Height)> {
let (current_height, current_block_time) = self.best_tip_height_and_block_time()?;

Expand Down
2 changes: 1 addition & 1 deletion zebra-chain/src/chain_tip/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ impl ChainTip for MockChainTip {

fn estimate_distance_to_network_chain_tip(
&self,
_network: Network,
_network: &Network,
) -> Option<(block::HeightDiff, block::Height)> {
self.estimated_distance_to_network_chain_tip
.borrow()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl NetworkChainTipHeightEstimator {
pub fn new(
current_block_time: DateTime<Utc>,
current_height: block::Height,
network: Network,
network: &Network,
) -> Self {
let mut target_spacings = NetworkUpgrade::target_spacings(network);
let (_genesis_height, initial_target_spacing) =
Expand Down
6 changes: 3 additions & 3 deletions zebra-chain/src/chain_tip/tests/prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ proptest! {
) {
let (chain_tip, mock_chain_tip_sender) = MockChainTip::new();
let blossom_activation_height = NetworkUpgrade::Blossom
.activation_height(network)
.activation_height(&network)
.expect("Blossom activation height is missing");

block_heights.sort();
Expand All @@ -50,13 +50,13 @@ proptest! {

let time_displacement = calculate_time_displacement(
time_displacement_factor,
NetworkUpgrade::current(network, network_height),
NetworkUpgrade::current(&network, network_height),
);

let mock_local_time = current_block_time + estimated_time_difference + time_displacement;

assert_eq!(
chain_tip.estimate_network_chain_tip_height(network, mock_local_time),
chain_tip.estimate_network_chain_tip_height(&network, mock_local_time),
Some(network_height)
);
}
Expand Down
40 changes: 20 additions & 20 deletions zebra-chain/src/history_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl NonEmptyHistoryTree {
/// The parameters must come from the values of [`NonEmptyHistoryTree::size`],
/// [`NonEmptyHistoryTree::peaks`] and [`NonEmptyHistoryTree::current_height`] of a HistoryTree.
pub fn from_cache(
network: Network,
network: &Network,
size: u32,
peaks: BTreeMap<u32, Entry>,
current_height: Height,
Expand Down Expand Up @@ -114,7 +114,7 @@ impl NonEmptyHistoryTree {
}
};
Ok(Self {
network,
network: network.clone(),
network_upgrade,
inner,
size,
Expand All @@ -130,7 +130,7 @@ impl NonEmptyHistoryTree {
/// (ignored for pre-Orchard blocks).
#[allow(clippy::unwrap_in_result)]
pub fn from_block(
network: Network,
network: &Network,
block: Arc<Block>,
sapling_root: &sapling::tree::Root,
orchard_root: &orchard::tree::Root,
Expand Down Expand Up @@ -169,7 +169,7 @@ impl NonEmptyHistoryTree {
let mut peaks = BTreeMap::new();
peaks.insert(0u32, entry);
Ok(NonEmptyHistoryTree {
network,
network: network.clone(),
network_upgrade,
inner: tree,
size: 1,
Expand Down Expand Up @@ -208,11 +208,11 @@ impl NonEmptyHistoryTree {
self.current_height
);

let network_upgrade = NetworkUpgrade::current(self.network, height);
let network_upgrade = NetworkUpgrade::current(&self.network, height);
if network_upgrade != self.network_upgrade {
// This is the activation block of a network upgrade.
// Create a new tree.
let new_tree = Self::from_block(self.network, block, sapling_root, orchard_root)?;
let new_tree = Self::from_block(&self.network, block, sapling_root, orchard_root)?;
// Replaces self with the new tree
*self = new_tree;
assert_eq!(self.network_upgrade, network_upgrade);
Expand Down Expand Up @@ -328,7 +328,7 @@ impl NonEmptyHistoryTree {
self.inner = match self.inner {
InnerHistoryTree::PreOrchard(_) => {
InnerHistoryTree::PreOrchard(Tree::<PreOrchard>::new_from_cache(
self.network,
&self.network,
self.network_upgrade,
self.size,
&self.peaks,
Expand All @@ -337,7 +337,7 @@ impl NonEmptyHistoryTree {
}
InnerHistoryTree::OrchardOnward(_) => {
InnerHistoryTree::OrchardOnward(Tree::<OrchardOnward>::new_from_cache(
self.network,
&self.network,
self.network_upgrade,
self.size,
&self.peaks,
Expand Down Expand Up @@ -373,7 +373,7 @@ impl NonEmptyHistoryTree {

/// Return the network where this tree is used.
pub fn network(&self) -> Network {
self.network
self.network.clone()
}
}

Expand All @@ -382,7 +382,7 @@ impl Clone for NonEmptyHistoryTree {
let tree = match self.inner {
InnerHistoryTree::PreOrchard(_) => InnerHistoryTree::PreOrchard(
Tree::<PreOrchard>::new_from_cache(
self.network,
&self.network,
self.network_upgrade,
self.size,
&self.peaks,
Expand All @@ -392,7 +392,7 @@ impl Clone for NonEmptyHistoryTree {
),
InnerHistoryTree::OrchardOnward(_) => InnerHistoryTree::OrchardOnward(
Tree::<OrchardOnward>::new_from_cache(
self.network,
&self.network,
self.network_upgrade,
self.size,
&self.peaks,
Expand All @@ -402,7 +402,7 @@ impl Clone for NonEmptyHistoryTree {
),
};
NonEmptyHistoryTree {
network: self.network,
network: self.network.clone(),
network_upgrade: self.network_upgrade,
inner: tree,
size: self.size,
Expand All @@ -423,7 +423,7 @@ impl HistoryTree {
/// If the block is pre-Heartwood, it returns an empty history tree.
#[allow(clippy::unwrap_in_result)]
pub fn from_block(
network: Network,
network: &Network,
block: Arc<Block>,
sapling_root: &sapling::tree::Root,
orchard_root: &orchard::tree::Root,
Expand All @@ -450,10 +450,10 @@ impl HistoryTree {
#[allow(clippy::unwrap_in_result)]
pub fn push(
&mut self,
network: Network,
network: &Network,
block: Arc<Block>,
sapling_root: sapling::tree::Root,
orchard_root: orchard::tree::Root,
sapling_root: &sapling::tree::Root,
orchard_root: &orchard::tree::Root,
) -> Result<(), HistoryTreeError> {
let heartwood_height = NetworkUpgrade::Heartwood
.activation_height(network)
Expand All @@ -472,9 +472,9 @@ impl HistoryTree {
std::cmp::Ordering::Equal => {
let tree = Some(NonEmptyHistoryTree::from_block(
network,
block.clone(),
&sapling_root,
&orchard_root,
block,
sapling_root,
orchard_root,
)?);
// Replace the current object with the new tree
*self = HistoryTree(tree);
Expand All @@ -483,7 +483,7 @@ impl HistoryTree {
self.0
.as_mut()
.expect("history tree must exist Heartwood-onward")
.push(block.clone(), &sapling_root, &orchard_root)?;
.push(block.clone(), sapling_root, orchard_root)?;
}
};
Ok(())
Expand Down
Loading

0 comments on commit 4579722

Please sign in to comment.