Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/consensus/ restart #507

Draft
wants to merge 11 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
version = "0.1.5"
edition = "2021"
authors = ["Broxus Team"]
rust-version = "1.81.0"
rust-version = "1.83.0"
repository = "https://github.com/broxus/tycho"
license = "MIT OR Apache-2.0"

Expand Down
37 changes: 20 additions & 17 deletions consensus/src/dag/commit/back.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::sync::atomic;
use std::{array, mem};

use futures_util::FutureExt;
use itertools::Itertools;
use rand::prelude::SliceRandom;
use rand::SeedableRng;
use tycho_network::PeerId;
Expand Down Expand Up @@ -137,7 +138,7 @@ impl DagBack {
if stage.is_used.load(atomic::Ordering::Relaxed) {
break;
};
match Self::any_ready_valid_point(dag_round, &stage.leader) {
match Self::any_ready_valid_trigger(dag_round, &stage.leader) {
Ok(trigger) => {
// iter is from newest to oldest, restore historical order
triggers.push_front(trigger.info);
Expand Down Expand Up @@ -208,10 +209,11 @@ impl DagBack {
.info;

let Some(anchor_id) = proof.prev_id() else {
panic!(
"verify() is broken: anchor proof without prev id; proof id {:?}",
proof.id()
)
last_proof = proof.anchor_id(AnchorStageRole::Proof);
if last_proof.round <= bottom_round {
return Ok(last_proof.round);
}
continue;
};

let Some((_, anchor_dag_round)) = rev_iter.next() else {
Expand Down Expand Up @@ -326,10 +328,8 @@ impl DagBack {
.info;

let Some(anchor_id) = proof.prev_id() else {
panic!(
"verify() is broken: anchor proof without prev id; proof id {:?}",
proof.id()
)
lookup_proof_id = proof.anchor_id(AnchorStageRole::Proof);
continue;
};

let Some((_, anchor_dag_round)) = rev_iter.next() else {
Expand Down Expand Up @@ -466,7 +466,7 @@ impl DagBack {
Ok(uncommitted)
}

fn any_ready_valid_point(
fn any_ready_valid_trigger(
dag_round: &DagRound,
author: &PeerId,
) -> Result<ValidPoint, SyncError> {
Expand All @@ -477,10 +477,14 @@ impl DagBack {
// better try later than wait now if some point is still downloading
.filter_map(|version| version.clone().now_or_never())
// take any suitable
.find_map(move |dag_point| match dag_point {
DagPoint::Trusted(valid)
| DagPoint::Suspicious(valid)
| DagPoint::Certified(valid) => Some(Ok(valid)),
.filter_map(move |dag_point| match dag_point {
DagPoint::Valid(valid) => {
if valid.info.data().anchor_trigger == Link::ToSelf {
Some(Ok(valid))
} else {
None
}
}
DagPoint::Invalid(cert) if cert.is_certified => {
Some(Err(SyncError::Impossible(cert.inner.round())))
}
Expand All @@ -491,6 +495,7 @@ impl DagBack {
None
}
})
.find_or_first(|result| result.is_ok())
})
.flatten()
.unwrap_or(Err(SyncError::TryLater))
Expand All @@ -512,9 +517,7 @@ impl DagBack {
return Err(SyncError::TryLater);
}; // not yet resolved;
match dag_point {
DagPoint::Trusted(valid) | DagPoint::Suspicious(valid) | DagPoint::Certified(valid) => {
Ok(valid)
}
DagPoint::Valid(valid) => Ok(valid),
DagPoint::Invalid(cert) if cert.is_certified => {
Err(SyncError::Impossible(cert.inner.round()))
}
Expand Down
21 changes: 15 additions & 6 deletions consensus/src/dag/commit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ impl std::fmt::Display for AltFmt<'_, Committer> {
mod test {
use std::array;
use std::io::Write;
use std::sync::Arc;

use everscale_crypto::ed25519::{KeyPair, SecretKey};
use tycho_network::PeerId;
Expand All @@ -293,16 +294,23 @@ mod test {

let (genesis, _) = CachedConfig::init(&default_test_config());

let peers: [(PeerId, KeyPair); PEER_COUNT] = array::from_fn(|i| {
let engine_ctx = EngineCtx::new(genesis.round());

let peers: [(PeerId, Arc<KeyPair>); PEER_COUNT] = array::from_fn(|i| {
let keys = KeyPair::from(&SecretKey::from_bytes([i as u8; 32]));
(PeerId::from(keys.public_key), keys)
(PeerId::from(keys.public_key), Arc::new(keys))
});
let local_keys = &peers[0].1;

let (genesis_round, peer_schedule, stub_downloader) =
test_utils::make_dag_parts(&peers, &genesis, &stub_store);
let mut round_ctx = RoundCtx::new(&engine_ctx, genesis.round());

let engine_ctx = EngineCtx::new(genesis.round());
let mut round_ctx;
let (peer_schedule, stub_downloader) =
test_utils::make_dag_parts(&peers, local_keys, &genesis);

let genesis_round = DagRound::new_bottom(genesis.round(), &peer_schedule);
genesis_round
.add_local(&genesis, Some(local_keys), &stub_store, &round_ctx)
.await;

let mut dag = DagFront::default();
let mut committer = dag.init(genesis_round);
Expand All @@ -325,6 +333,7 @@ mod test {
test_utils::populate_points(
dag.top(),
&peers,
local_keys,
&peer_schedule,
&stub_downloader,
&stub_store,
Expand Down
Loading
Loading