This repository has been archived by the owner on Nov 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Stop adding transactions to queue while not fully synced #751
Merged
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
03ca9d2
adding message from client to sync and disabling sending transactions…
NikVolf 5c4edbd
Merge branch 'master' into tq-stop-sync
NikVolf 83f5cc6
adding helper can_sync
NikVolf a285fba
overhaul to flag
NikVolf 0b35a36
Merge branch 'master' into tq-stop-sync
NikVolf 0f96ce9
no flag also
NikVolf 7929af6
propagation is out
NikVolf 85b08e6
get rid of the function
NikVolf File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -215,6 +215,8 @@ pub struct ChainSync { | |
network_id: U256, | ||
/// Miner | ||
miner: Arc<Miner>, | ||
/// Fully-synced flag | ||
is_fully_synced: bool, | ||
} | ||
|
||
type RlpResponseResult = Result<Option<(PacketId, RlpStream)>, PacketDecodeError>; | ||
|
@@ -241,6 +243,7 @@ impl ChainSync { | |
max_download_ahead_blocks: max(MAX_HEADERS_TO_REQUEST, config.max_download_ahead_blocks), | ||
network_id: config.network_id, | ||
miner: miner, | ||
is_fully_synced: true, | ||
} | ||
} | ||
|
||
|
@@ -625,6 +628,13 @@ impl ChainSync { | |
self.state = SyncState::Waiting; | ||
} | ||
|
||
fn can_sync(&self) -> bool { | ||
match self.state { | ||
SyncState::Idle | SyncState::NotSynced => true, | ||
_ => false | ||
} | ||
} | ||
|
||
/// Find something to do for a peer. Called for a new peer or when a peer is done with it's task. | ||
fn sync_peer(&mut self, io: &mut SyncIo, peer_id: PeerId, force: bool) { | ||
let (peer_latest, peer_difficulty) = { | ||
|
@@ -644,7 +654,7 @@ impl ChainSync { | |
if force || peer_difficulty > syncing_difficulty { | ||
// start sync | ||
self.syncing_difficulty = peer_difficulty; | ||
if self.state == SyncState::Idle || self.state == SyncState::NotSynced { | ||
if self.can_sync() { | ||
self.state = SyncState::Blocks; | ||
} | ||
trace!(target: "sync", "Starting sync with better chain"); | ||
|
@@ -937,6 +947,11 @@ impl ChainSync { | |
} | ||
/// Called when peer sends us new transactions | ||
fn on_peer_transactions(&mut self, io: &mut SyncIo, peer_id: PeerId, r: &UntrustedRlp) -> Result<(), PacketDecodeError> { | ||
// accepting transactions once only fully synced | ||
if !self.is_fully_synced { | ||
return Ok(()); | ||
} | ||
|
||
let item_count = r.item_count(); | ||
trace!(target: "sync", "{} -> Transactions ({} entries)", peer_id, item_count); | ||
|
||
|
@@ -1281,9 +1296,13 @@ impl ChainSync { | |
} | ||
|
||
/// called when block is imported to chain, updates transactions queue and propagates the blocks | ||
pub fn chain_new_blocks(&mut self, io: &mut SyncIo, imported: &[H256], invalid: &[H256], enacted: &[H256], retracted: &[H256]) { | ||
// Notify miner | ||
self.miner.chain_new_blocks(io.chain(), imported, invalid, enacted, retracted); | ||
pub fn chain_new_blocks(&mut self, io: &mut SyncIo, imported: &[H256], invalid: &[H256], enacted: &[H256], retracted: &[H256], is_last: bool) { | ||
// Set the state in which it can accept transactions from the net | ||
self.is_fully_synced = is_last; | ||
if self.is_fully_synced { | ||
// Notify miner | ||
self.miner.chain_new_blocks(io.chain(), imported, invalid, enacted, retracted); | ||
} | ||
// Propagate latests blocks | ||
self.propagate_latest_blocks(io); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think propagation should also happen only if we are fully synced. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yup |
||
// TODO [todr] propagate transactions? | ||
|
@@ -1643,17 +1662,45 @@ mod tests { | |
let mut io = TestIo::new(&mut client, &mut queue, None); | ||
|
||
// when | ||
sync.chain_new_blocks(&mut io, &[], &[], &[], &good_blocks); | ||
sync.chain_new_blocks(&mut io, &[], &[], &[], &good_blocks, true); | ||
assert_eq!(sync.miner.status().transactions_in_future_queue, 0); | ||
assert_eq!(sync.miner.status().transactions_in_pending_queue, 1); | ||
sync.chain_new_blocks(&mut io, &good_blocks, &[], &[], &retracted_blocks); | ||
sync.chain_new_blocks(&mut io, &good_blocks, &[], &[], &retracted_blocks, true); | ||
|
||
|
||
// then | ||
let status = sync.miner.status(); | ||
assert_eq!(status.transactions_in_pending_queue, 1); | ||
assert_eq!(status.transactions_in_future_queue, 0); | ||
} | ||
|
||
#[test] | ||
fn should_not_add_transactions_to_queue_if_not_synced() { | ||
// given | ||
let mut client = TestBlockChainClient::new(); | ||
client.add_blocks(98, EachBlockWith::Uncle); | ||
client.add_blocks(1, EachBlockWith::UncleAndTransaction); | ||
client.add_blocks(1, EachBlockWith::Transaction); | ||
let mut sync = dummy_sync_with_peer(client.block_hash_delta_minus(5)); | ||
|
||
let good_blocks = vec![client.block_hash_delta_minus(2)]; | ||
let retracted_blocks = vec![client.block_hash_delta_minus(1)]; | ||
|
||
let mut queue = VecDeque::new(); | ||
let mut io = TestIo::new(&mut client, &mut queue, None); | ||
|
||
// when | ||
sync.chain_new_blocks(&mut io, &[], &[], &[], &good_blocks, false); | ||
assert_eq!(sync.miner.status().transactions_in_future_queue, 0); | ||
assert_eq!(sync.miner.status().transactions_in_pending_queue, 0); | ||
sync.chain_new_blocks(&mut io, &good_blocks, &[], &[], &retracted_blocks, false); | ||
|
||
// then | ||
let status = sync.miner.status(); | ||
assert_eq!(status.transactions_in_pending_queue, 0); | ||
assert_eq!(status.transactions_in_future_queue, 0); | ||
} | ||
|
||
#[test] | ||
fn returns_requested_block_headers() { | ||
let mut client = TestBlockChainClient::new(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't it be better to avoid sending the event instead?