Skip to content

Commit

Permalink
Merge pull request #147 from Neptune-Crypto/thv/fix-rollback-issue
Browse files Browse the repository at this point in the history
Thv/fix rollback issue
  • Loading branch information
Sword-Smith authored May 10, 2024
2 parents 5f9c9e8 + 0b212d1 commit f1b3d57
Show file tree
Hide file tree
Showing 22 changed files with 478 additions and 800 deletions.
4 changes: 4 additions & 0 deletions src/bin/dashboard_src/receive_screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,13 @@ impl Widget for ReceiveScreen {
let width = max(0, inner.width as isize - 2) as usize;
if width > 0 {
let mut address_lines = vec![];

// TODO: Not sure how to handle this linting problem, as clippy suggestion doesn't work.
#[allow(clippy::assigning_clones)]
while address.len() > width {
let (line, remainder) = address.split_at(width);
address_lines.push(line.to_owned());

address = remainder.to_owned();
}
address_lines.push(address);
Expand Down
5 changes: 4 additions & 1 deletion src/bin/dashboard_src/send_screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ impl SendScreen {
}
DashboardEvent::ConsoleMode(ConsoleIO::InputSupplied(string)) => {
if let Ok(mut own_focus) = self.focus.try_lock() {
self.address = string.trim().to_owned();
string.trim().clone_into(&mut self.address);
*own_focus = SendScreenWidget::Amount;
escalate_event = Some(DashboardEvent::RefreshScreen);
} else {
Expand Down Expand Up @@ -350,6 +350,9 @@ impl Widget for SendScreen {
self.address.clone()
};
let mut address_lines = vec![];

// TODO: Not sure how to handle this linting problem, as clippy suggestion doesn't work.
#[allow(clippy::assigning_clones)]
while address.len() > width {
let (line, remainder) = address.split_at(width);
address_lines.push(line.to_owned());
Expand Down
6 changes: 3 additions & 3 deletions src/config_models/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub enum Network {

/// Network for individual unit and integration tests. The timestamp for the
/// RegTest genesis block is set to now, rounded down to the first block of
/// 10 minutes. As a result, there is a small probability that tests fail
/// 10 hours. As a result, there is a small probability that tests fail
/// because they generate the genesis block twice on two opposite sides of a
/// round timestamp.
RegTest,
Expand All @@ -38,8 +38,8 @@ impl Network {
.duration_since(UNIX_EPOCH)
.unwrap()
.as_millis() as u64;
let ten_minutes = 1000 * 60 * 10;
let now_rounded = (now / ten_minutes) * ten_minutes;
const TEN_HOURS_AS_MS: u64 = 1000 * 60 * 60 * 10;
let now_rounded = (now / TEN_HOURS_AS_MS) * TEN_HOURS_AS_MS;
match self {
Network::RegTest => Timestamp(BFieldElement::new(now_rounded)),
// 1 July 2024 (might be revised though)
Expand Down
10 changes: 4 additions & 6 deletions src/connect_to_peers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -473,9 +473,8 @@ mod connect_tests {
ConnectionStatus, PeerInfo, PeerMessage, PeerSanctionReason, PeerStanding,
};
use crate::tests::shared::{
get_dummy_handshake_data_for_genesis, get_dummy_latest_block,
get_dummy_peer_connection_data_genesis, get_dummy_socket_address, get_test_genesis_setup,
to_bytes,
get_dummy_handshake_data_for_genesis, get_dummy_peer_connection_data_genesis,
get_dummy_socket_address, get_test_genesis_setup, to_bytes,
};
use crate::{MAGIC_STRING_REQUEST, MAGIC_STRING_RESPONSE};

Expand Down Expand Up @@ -787,8 +786,8 @@ mod connect_tests {
let mut own_handshake = state.get_own_handshakedata().await;

// Set reported versions to something incompatible
own_handshake.version = "0.0.3".to_owned();
other_handshake.version = "0.0.0".to_owned();
"0.0.3".clone_into(&mut own_handshake.version);
"0.0.0".clone_into(&mut other_handshake.version);

let peer_address = get_dummy_socket_address(55);
let connection_status = check_if_connection_is_allowed(
Expand Down Expand Up @@ -873,7 +872,6 @@ mod connect_tests {
cli.max_peers = 2;
state_lock.set_cli(cli).await;

let (_, _, _latest_block_header) = get_dummy_latest_block(None).await;
let answer = answer_peer(
mock,
state_lock.clone(),
Expand Down
2 changes: 0 additions & 2 deletions src/database/storage/storage_vec/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,6 @@ pub trait StorageVecStream<T: Send>: StorageVecBase<T> {

pub trait StorageVec<T: Send>: StorageVecBase<T> + StorageVecStream<T> {}

pub(in super::super) trait StorageVecIterMut<T: Send>: StorageVec<T> {}

#[cfg(test)]
pub(in super::super) mod tests {
use super::*;
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ pub async fn initialize(cli_args: cli_args::Args) -> Result<()> {
.await;

// Get latest block. Use hardcoded genesis block if nothing is in database.
let latest_block: Block = archival_state.get_latest_block().await;
let latest_block: Block = archival_state.get_tip().await;

// Bind socket to port on this machine, to handle incoming connections from peers
let incoming_peer_listener = TcpListener::bind((cli_args.listen_addr, cli_args.peer_port))
Expand Down
4 changes: 2 additions & 2 deletions src/main_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ impl MainLoopHandler {
}

global_state_mut
.store_coinbase_block(
.set_new_self_mined_tip(
new_block.as_ref().clone(),
new_block_info.coinbase_utxo_info.as_ref().clone(),
)
Expand Down Expand Up @@ -418,7 +418,7 @@ impl MainLoopHandler {
new_block.kernel.header.timestamp.standard_format()
);

global_state_mut.store_block(new_block).await?;
global_state_mut.set_new_tip(new_block).await?;
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/mine_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ mod mine_loop_tests {
use crate::{
config_models::network::Network,
models::{consensus::timestamp::Timestamp, state::UtxoReceiverData},
tests::shared::get_mock_global_state,
tests::shared::mock_genesis_global_state,
};

use super::*;
Expand All @@ -515,7 +515,7 @@ mod mine_loop_tests {
// Verify that a block template made with transaction from the mempool is a valid block
let network = Network::RegTest;
let premine_receiver_global_state_lock =
get_mock_global_state(network, 2, WalletSecret::devnet_wallet()).await;
mock_genesis_global_state(network, 2, WalletSecret::devnet_wallet()).await;
let mut premine_receiver_global_state =
premine_receiver_global_state_lock.lock_guard_mut().await;
assert!(
Expand Down Expand Up @@ -635,7 +635,7 @@ mod mine_loop_tests {
async fn mined_block_has_proof_of_work() -> Result<()> {
let network = Network::RegTest;
let global_state_lock =
get_mock_global_state(network, 2, WalletSecret::devnet_wallet()).await;
mock_genesis_global_state(network, 2, WalletSecret::devnet_wallet()).await;

let (worker_thread_tx, worker_thread_rx) = oneshot::channel::<NewBlockFound>();

Expand Down Expand Up @@ -683,7 +683,7 @@ mod mine_loop_tests {
async fn block_timestamp_represents_time_block_found() -> Result<()> {
let network = Network::RegTest;
let global_state_lock =
get_mock_global_state(network, 2, WalletSecret::devnet_wallet()).await;
mock_genesis_global_state(network, 2, WalletSecret::devnet_wallet()).await;

let (worker_thread_tx, worker_thread_rx) = oneshot::channel::<NewBlockFound>();

Expand Down
18 changes: 6 additions & 12 deletions src/models/blockchain/block/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -618,15 +618,7 @@ impl Block {
/// compare the hash of the current block against the difficulty determined by
/// the previous.
pub fn has_proof_of_work(&self, previous_block: &Block) -> bool {
// check that hash is below threshold
if self.hash()
> Self::difficulty_to_digest_threshold(previous_block.kernel.header.difficulty)
{
warn!("Block digest exceeds target difficulty");
return false;
}

true
self.hash() <= Self::difficulty_to_digest_threshold(previous_block.kernel.header.difficulty)
}

/// Converts `difficulty` to type `Digest` so that the hash of a block can be
Expand Down Expand Up @@ -689,7 +681,9 @@ mod block_tests {
blockchain::transaction::PublicAnnouncement, state::wallet::WalletSecret,
state::UtxoReceiverData,
},
tests::shared::{get_mock_global_state, make_mock_block, make_mock_block_with_valid_pow},
tests::shared::{
make_mock_block, make_mock_block_with_valid_pow, mock_genesis_global_state,
},
util_types::mutator_set::archival_mmr::ArchivalMmr,
};
use strum::IntoEnumIterator;
Expand All @@ -705,7 +699,7 @@ mod block_tests {
// has a wallet which receives a premine-UTXO.
let network = Network::RegTest;
let global_state_lock =
get_mock_global_state(network, 2, WalletSecret::devnet_wallet()).await;
mock_genesis_global_state(network, 2, WalletSecret::devnet_wallet()).await;
let spending_key = global_state_lock
.lock_guard()
.await
Expand Down Expand Up @@ -1015,7 +1009,7 @@ mod block_tests {
// note: we have to generate a block becau // TransferBlock::into() will panic if it
// encounters the genesis block.
let global_state_lock =
get_mock_global_state(Network::RegTest, 2, WalletSecret::devnet_wallet()).await;
mock_genesis_global_state(Network::RegTest, 2, WalletSecret::devnet_wallet()).await;
let spending_key = global_state_lock
.lock_guard()
.await
Expand Down
4 changes: 2 additions & 2 deletions src/models/blockchain/transaction/utxo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,10 +307,10 @@ mod utxo_tests {
#[test]
fn utxo_timelock_test() {
let mut rng = thread_rng();
let release_date = rng.gen::<Timestamp>();
let release_date = Timestamp(BFieldElement::new(rng.next_u64() >> 2));
let mut delta = release_date + Timestamp::seconds(1);
while delta > release_date {
delta = Timestamp(BFieldElement::new(rng.next_u64() >> 1));
delta = Timestamp(BFieldElement::new(rng.next_u64() >> 2));
}
let mut utxo = Utxo::new(
LockScript {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,9 @@ impl RemovalRecordsIntegrityWitness {
working_indices.dedup();
for wi in working_indices {
let wi_odd = wi | 1;
if nodes.get(&wi_odd).is_none() {
nodes.insert(wi_odd, rng.gen::<Digest>());
}
nodes.entry(wi_odd).or_insert_with(|| rng.gen::<Digest>());
let wi_even = wi_odd ^ 1;
if nodes.get(&wi_even).is_none() {
nodes.insert(wi_even, rng.gen::<Digest>());
}
nodes.entry(wi_even).or_insert_with(|| rng.gen::<Digest>());
let hash = Hash::hash_pair(nodes[&wi_even], nodes[&wi_odd]);
nodes.insert(wi >> 1, hash);
}
Expand Down
Loading

0 comments on commit f1b3d57

Please sign in to comment.