Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 37 additions & 33 deletions sync/src/block/extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,38 @@ impl Extension {
}
}

fn send_status(&mut self, id: &NodeId) {
let chain_info = self.client.chain_info();
self.api.send(
id,
Arc::new(
Message::Status {
total_score: chain_info.best_proposal_score,
best_hash: chain_info.best_proposal_block_hash,
genesis_hash: chain_info.genesis_hash,
}
.rlp_bytes(),
),
);
}

fn send_status_broadcast(&mut self) {
let chain_info = self.client.chain_info();
for id in self.connected_nodes.iter() {
self.api.send(
id,
Arc::new(
Message::Status {
total_score: chain_info.best_proposal_score,
best_hash: chain_info.best_proposal_block_hash,
genesis_hash: chain_info.genesis_hash,
}
.rlp_bytes(),
),
);
}
}

fn send_header_request(&mut self, id: &NodeId, request: RequestMessage) {
if let Some(requests) = self.requests.get_mut(id) {
ctrace!(SYNC, "Send header request to {}", id);
Expand Down Expand Up @@ -212,18 +244,8 @@ impl NetworkExtension<Event> for Extension {

fn on_node_added(&mut self, id: &NodeId, _version: u64) {
cinfo!(SYNC, "New peer detected #{}", id);
let chain_info = self.client.chain_info();
self.api.send(
id,
Arc::new(
Message::Status {
total_score: chain_info.best_proposal_score,
best_hash: chain_info.best_proposal_block_hash,
genesis_hash: chain_info.genesis_hash,
}
.rlp_bytes(),
),
);
self.send_status(id);

let t = self.connected_nodes.insert(*id);
debug_assert!(t, "{} is already added to peer list", id);

Expand Down Expand Up @@ -386,11 +408,8 @@ pub enum Event {

impl Extension {
fn new_headers(&mut self, imported: Vec<BlockHash>, enacted: Vec<BlockHash>, retracted: Vec<BlockHash>) {
let peer_ids: Vec<_> = self.header_downloaders.keys().cloned().collect();
for id in peer_ids {
if let Some(peer) = self.header_downloaders.get_mut(&id) {
peer.mark_as_imported(imported.clone());
}
for peer in self.header_downloaders.values_mut() {
peer.mark_as_imported(imported.clone());
}
let mut headers_to_download: Vec<_> = enacted
.into_iter()
Expand Down Expand Up @@ -420,22 +439,7 @@ impl Extension {
self.body_downloader.remove_target(&imported);
self.body_downloader.remove_target(&invalid);


let chain_info = self.client.chain_info();

for id in &self.connected_nodes {
self.api.send(
id,
Arc::new(
Message::Status {
total_score: chain_info.best_proposal_score,
best_hash: chain_info.best_proposal_block_hash,
genesis_hash: chain_info.genesis_hash,
}
.rlp_bytes(),
),
);
}
self.send_status_broadcast();
}
}

Expand Down
4 changes: 2 additions & 2 deletions test/src/e2e.dynval/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ interface TermWaiter {
target: number;
termPeriods: number;
}
): Promise<void>;
): Promise<stake.TermMetadata>;
}

export function setTermTestTimeout(
Expand Down Expand Up @@ -483,7 +483,7 @@ export function setTermTestTimeout(
termPeriods: number;
}
) {
await node.waitForTermChange(
return await node.waitForTermChange(
waiterParams.target,
termPeriodsToTime(waiterParams.termPeriods, 0.5)
);
Expand Down
10 changes: 6 additions & 4 deletions test/src/helper/spawn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -859,14 +859,16 @@ export default class CodeChain {
public async waitForTermChange(target: number, timeout?: number) {
const start = Date.now();
while (true) {
const termMetadata = await stake.getTermMetadata(this.sdk);
if (termMetadata && termMetadata.currentTermId >= target) {
break;
const termMetadata = (await stake.getTermMetadata(this.sdk))!;
if (termMetadata.currentTermId >= target) {
return termMetadata;
}
await wait(1000);
if (timeout) {
if (Date.now() - start > timeout * 1000) {
throw new Error(`Term didn't changed in ${timeout} s`);
throw new Error(
`Term didn't changed to ${target} in ${timeout} s. It is ${termMetadata.currentTermId} now`
);
}
}
}
Expand Down