Skip to content

Commit

Permalink
Use median epoch number from not-catch-up peers. (#1335)
Browse files Browse the repository at this point in the history
* Only use median epoch number from not-catch-up peers.

* nit.
  • Loading branch information
peilun-conflux authored and Peilun Li committed Apr 23, 2020
1 parent 2b6cef8 commit 96e6d63
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 21 deletions.
8 changes: 4 additions & 4 deletions core/src/sync/message/capability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ use rlp_derive::{RlpDecodableWrapper, RlpEncodableWrapper};

#[derive(Debug, Eq, PartialEq, Clone, Copy)]
pub enum DynamicCapability {
TxRelay(bool), // provide tx relay
NormalPhase(bool), // provide tx relay
ServeHeaders(bool), // provide block header downloads
}

impl DynamicCapability {
fn code(&self) -> u8 {
match self {
DynamicCapability::TxRelay(_) => 0,
DynamicCapability::NormalPhase(_) => 0,
DynamicCapability::ServeHeaders(_) => 1,
}
}
Expand Down Expand Up @@ -52,7 +52,7 @@ impl Encodable for DynamicCapability {
s.begin_list(2).append(&self.code());

match self {
DynamicCapability::TxRelay(enabled) => s.append(enabled),
DynamicCapability::NormalPhase(enabled) => s.append(enabled),
DynamicCapability::ServeHeaders(enabled) => s.append(enabled),
};
}
Expand All @@ -65,7 +65,7 @@ impl Decodable for DynamicCapability {
}

match rlp.val_at::<u8>(0)? {
0 => Ok(DynamicCapability::TxRelay(rlp.val_at(1)?)),
0 => Ok(DynamicCapability::NormalPhase(rlp.val_at(1)?)),
1 => Ok(DynamicCapability::ServeHeaders(rlp.val_at(1)?)),
_ => Err(DecoderError::Custom("invalid capability code")),
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/sync/message/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ impl Handleable for Status {

peer_state
.capabilities
.insert(DynamicCapability::TxRelay(true));
.insert(DynamicCapability::NormalPhase(true));

debug!(
"New peer (pv={:?}, gh={:?})",
Expand Down
4 changes: 2 additions & 2 deletions core/src/sync/message/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl Handleable for Transactions {
let mut peer_info = peer_info.write();
if peer_info
.notified_capabilities
.contains(DynamicCapability::TxRelay(false))
.contains(DynamicCapability::NormalPhase(false))
{
peer_info.received_transaction_count += transactions.len();
peer_info.received_transaction_count
Expand Down Expand Up @@ -96,7 +96,7 @@ impl Handleable for TransactionDigests {
let mut peer_info = peer_info.write();
if peer_info
.notified_capabilities
.contains(DynamicCapability::TxRelay(false))
.contains(DynamicCapability::NormalPhase(false))
{
peer_info.received_transaction_count +=
self.short_ids.len() + self.tx_hashes.len();
Expand Down
4 changes: 2 additions & 2 deletions core/src/sync/synchronization_phases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ impl SynchronizationPhaseTrait for CatchUpSyncBlockHeaderPhase {
) -> SyncPhaseType
{
// FIXME: use target_height instead.
let middle_epoch = self.syn.get_middle_epoch();
let middle_epoch = self.syn.median_epoch_from_normal_peers();
if middle_epoch.is_none() {
return self.phase_type();
}
Expand Down Expand Up @@ -603,7 +603,7 @@ impl SynchronizationPhaseTrait for CatchUpSyncBlockPhase {
) -> SyncPhaseType
{
// FIXME: use target_height instead.
let middle_epoch = self.syn.get_middle_epoch();
let middle_epoch = self.syn.median_epoch_from_normal_peers();
if middle_epoch.is_none() {
if self.syn.is_dev_or_test_mode() {
return SyncPhaseType::Normal;
Expand Down
8 changes: 4 additions & 4 deletions core/src/sync/synchronization_protocol_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1132,7 +1132,7 @@ impl SynchronizationProtocolHandler {
if !peer_info
.read()
.capabilities
.contains(DynamicCapability::TxRelay(true))
.contains(DynamicCapability::NormalPhase(true))
{
return None;
}
Expand Down Expand Up @@ -1367,12 +1367,12 @@ impl SynchronizationProtocolHandler {
let mut state = state.write();
if !state
.notified_capabilities
.contains(DynamicCapability::TxRelay(!catch_up_mode))
.contains(DynamicCapability::NormalPhase(!catch_up_mode))
{
state.received_transaction_count = 0;
state
.notified_capabilities
.insert(DynamicCapability::TxRelay(!catch_up_mode));
.insert(DynamicCapability::NormalPhase(!catch_up_mode));
need_notify.push(*peer);
}
}
Expand All @@ -1382,7 +1382,7 @@ impl SynchronizationProtocolHandler {
self.graph.consensus.best_epoch_number()
);

DynamicCapability::TxRelay(!catch_up_mode)
DynamicCapability::NormalPhase(!catch_up_mode)
.broadcast_with_peers(io, need_notify);
}

Expand Down
20 changes: 12 additions & 8 deletions core/src/sync/synchronization_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,17 +148,21 @@ impl SynchronizationState {

pub fn is_dev_or_test_mode(&self) -> bool { self.is_dev_or_test_mode }

// FIXME: use median instead, because it's so confusing without context.
// FIXME: median_chain_height_from_peers.
// FIXME: it lead to more questions but these are questions on the
// FIXME: algorithm side.
pub fn get_middle_epoch(&self) -> Option<u64> {
let mut peer_best_epoches = {
let peers = self.peers.read();
peers
.iter()
.map(|(_, state)| state.read().best_epoch)
.collect::<Vec<_>>()
pub fn median_epoch_from_normal_peers(&self) -> Option<u64> {
let mut peer_best_epoches = Vec::new();
{
for (_, state_lock) in &*self.peers.read() {
let state = state_lock.read();
if state
.capabilities
.contains(DynamicCapability::NormalPhase(true))
{
peer_best_epoches.push(state.best_epoch);
}
}
};

if peer_best_epoches.is_empty() {
Expand Down

0 comments on commit 96e6d63

Please sign in to comment.