-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Remove blocking sync -> import-queue operations #1818
Remove blocking sync -> import-queue operations #1818
Conversation
8fc7ce7
to
728b5cd
Compare
One of the issue I am running into with the related update to the testnet code, is that I've ended up adding the
so I've tried moving the default EDIT: solved, see f0dece3 |
@andresilva It seems that the Edit: it was me for hadn't applied the change I did to the other testnets, see 5754c89 |
ae932eb
to
978b446
Compare
add specialization to testnet remove add peer default impl on TestNetFactory
978b446
to
543d75c
Compare
Does this also address #1787 ? |
core/consensus/aura/src/lib.rs
Outdated
let client = Arc::new(test_client::new()); | ||
let verifier = self.make_verifier(client.clone(), config); | ||
let (block_import, justification_import, data) = self.make_block_import(client.clone()); | ||
let specialization = DummySpecialization {}; |
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.
braces aren't necessary here, are they? (braced empty struct is considered bad style usually and should only be used in auto-generated code)
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.
I'm getting the below when I remove then:
error[E0423]: expected value, found struct `DummySpecialization`
--> core/network/src/test/mod.rs:729:24
|
729 | let specialization = DummySpecialization;
| ^^^^^^^^^^^^^^^^^^^ did you mean `DummySpecialization { /* fields */ }`?
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.
probably the error traces back to the struct definition then and we could address it there.
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.
You're right, the empty brackets had to be removed from the struct definition.
@@ -389,28 +333,11 @@ impl<B: BlockT> BlockImporter<B> { | |||
}; | |||
} | |||
if let Some(link) = self.link.as_ref() { | |||
link.maintain_sync(); | |||
link.blocks_processed(hashes, has_error); |
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.
fn blocks_processed
doesn't maintain sync anymore. Where is that call done?
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.
The call is made at https://github.com/paritytech/substrate/pull/1818/files#diff-9c1b34b73c6680d46fc07cae4591547fR347 as part of handling blocks_processed
. I removed the maintain_sync
method on Link
and replaced it with blocks_processed
, which still does the "maintaining sync" as part of it...
core/consensus/aura/src/lib.rs
Outdated
let (block_import, justification_import, data) = self.make_block_import(client.clone()); | ||
let specialization = DummySpecialization; | ||
let peer = create_peer(client, block_import, justification_import, data, verifier, specialization, config); | ||
self.mut_peers(|peers| { |
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.
Why do you need to clone
the peer? And self.peers.push(peer);
is much shorter.
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.
Why do you need to
clone
the peer?
error[E0507]: cannot move out of captured variable in an `Fn` closure
--> core/network/src/test/mod.rs:803:15
|
801 | let peer = create_peer(client, block_import, justification_import, data, verifier, specialization, config);
| ---- captured outer variable
802 | self.mut_peers(|peers| {
803 | peers.push(peer)
| ^^^^ cannot move out of captured variable in an `Fn` closure
self.peers.push(peer);
is much shorter.
The self.mut_peers
allows composition of testnets, for example the JustificationTestnet
below doesn't have self.peers
, yet it owns a TestNet
and it can access the peers
of it via self.mut_peers
.
Here we could probably do self.peers
since its the TestNet
implementation itself..
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.
Then mut_peers
should take FnOnce
.
core/finality-grandpa/src/tests.rs
Outdated
@@ -114,6 +113,17 @@ impl TestNetFactory for GrandpaTestNet { | |||
(shared_import.clone(), Some(shared_import), Mutex::new(Some(link))) | |||
} | |||
|
|||
fn add_peer(&mut self, config: &ProtocolConfig) { |
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.
Code duplication. If this is the same implementation, can we not just move this as default implementation into the trait definition?
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.
Since I added NetworkSpecialization
as a type on the TestNetFactory
, we cannot have a default add_peer
method on the factory because it contains let specialization = DummySpecialization;
, which requires an implementation of the TesnetFactory trait to specificy type Specialization = DummySpecialization;
.
I did remove as much duplication as possible by adding the create_peer
function, which is used in each implementation.
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.
Introduce a trait
trait Specialization {
fn create() -> Self;
}
And then Self::Specialization::create()
.
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.
I'm not convinced an additional trait is better than some duplication. I also expect most testnet implementations will have their own setup for peer creation and this duplication will prove a non-issue.
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.
You already repeated the same code 4 times.
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.
Sounds better than what I proposed! Nice @andresilva 👍
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.
Ok, you're right the proposed trait is a better solution. It's not that complicated.
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.
You can remove this implementation now :)
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.
@andresilva just seeing your comment, feel free to push your version if it is better than what I just committed...
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.
Nope, yours is fine 👍
core/network/src/sync.rs
Outdated
.unwrap_or_else(|| Zero::zero()); | ||
self.queue_blocks | ||
.extend(new_blocks.iter().map(|b| b.hash.clone())); | ||
if new_best_importing_number > self.best_importing_number { |
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.
Could be self.best_importing_number = max(self.best_importing_number, new_best_importing_number);
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.
For some reason, this results in a test failure:
---- test::sync::sync_after_fork_works stdout ----
thread 'test::sync::sync_after_fork_works' panicked at 'assertion failed: net.peer(0).client.backend().as_in_memory().blockchain().canon_equals_to(&peer1_chain)', core/network/src/test/sync.rs:149:2
note: Run with `RUST_BACKTRACE=1` environment variable to display a backtrace.
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.
I've also seen this test fail so it's probably some non-determinism being introduced (not sure if by this PR or if it was already like that). Can you try re-running the test to see if it passes?
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.
Yes it seems to fail quite often regardless of the syntax used. I've changed it to max(new_best_importing_number, self.best_importing_number);
(note: "Returns the second argument if the comparison determines them to be equal." https://doc.rust-lang.org/std/cmp/fn.max.html)
core/network/src/test/mod.rs
Outdated
data:D, | ||
verifier: Arc<V>, | ||
specialization: S, | ||
config: &ProtocolConfig) |
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.
config: &ProtocolConfig) | |
config: &ProtocolConfig | |
) -> Arc<Peer<D, S>> { |
core/network/src/test/mod.rs
Outdated
verifier: Arc<V>, | ||
specialization: S, | ||
config: &ProtocolConfig) | ||
-> Arc<Peer<D, S>> { |
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.
-> Arc<Peer<D, S>> { |
core/network/src/test/mod.rs
Outdated
None, | ||
tx_pool, | ||
specialization, | ||
).unwrap(); |
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.
Please use expect
core/network/src/test/mod.rs
Outdated
@@ -655,21 +722,32 @@ impl TestNetFactory for TestNet { | |||
} | |||
} | |||
|
|||
fn add_peer(&mut self, config: &ProtocolConfig) { | |||
let client = Arc::new(test_client::new()); |
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.
Again code duplication.
core/network/src/test/mod.rs
Outdated
@@ -714,15 +793,26 @@ impl TestNetFactory for JustificationTestNet { | |||
self.0.make_verifier(client, config) | |||
} | |||
|
|||
fn peer(&self, i: usize) -> &Peer<Self::PeerData> { | |||
fn add_peer(&mut self, config: &ProtocolConfig) { | |||
let client = Arc::new(test_client::new()); |
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.
Again.
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.
LGTM. Just a minor nit.
core/network/src/protocol.rs
Outdated
@@ -210,8 +210,8 @@ pub enum ProtocolMsg<B: BlockT, S: NetworkSpecialization<B>,> { | |||
Peers(Sender<Vec<(NodeIndex, PeerInfo<B>)>>), | |||
/// Let protocol know a peer is currenlty clogged. | |||
PeerClogged(NodeIndex, Option<Message<B>>), | |||
/// Tell protocol to maintain sync. | |||
MaintainSync, | |||
/// A batch of blocks have been processed, with or without errors. |
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.
/// A batch of blocks have been processed, with or without errors. | |
/// A batch of blocks has been processed, with or without errors. |
core/network/src/sync.rs
Outdated
.unwrap_or_else(|| Zero::zero()); | ||
self.queue_blocks | ||
.extend(new_blocks.iter().map(|b| b.hash.clone())); | ||
if new_best_importing_number > self.best_importing_number { |
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.
I've also seen this test fail so it's probably some non-determinism being introduced (not sure if by this PR or if it was already like that). Can you try re-running the test to see if it passes?
core/finality-grandpa/src/tests.rs
Outdated
@@ -114,6 +113,17 @@ impl TestNetFactory for GrandpaTestNet { | |||
(shared_import.clone(), Some(shared_import), Mutex::new(Some(link))) | |||
} | |||
|
|||
fn add_peer(&mut self, config: &ProtocolConfig) { |
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.
You can remove this implementation now :)
core/finality-grandpa/src/tests.rs
Outdated
@@ -114,6 +113,17 @@ impl TestNetFactory for GrandpaTestNet { | |||
(shared_import.clone(), Some(shared_import), Mutex::new(Some(link))) | |||
} | |||
|
|||
fn add_peer(&mut self, config: &ProtocolConfig) { |
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.
Nope, yours is fine 👍
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.
Please fix the merge conflict and then we are good to merge this.
Restore old behavior of not sleeping in a test.
* remove blocking sync -> import-queue operations add specialization to testnet remove add peer default impl on TestNetFactory * remove empty brackets from dummy specialization * nits * make mut_peers take an fn once * add SpecializationFactory trait in test * remove add_peer imple in grandpa, fix typo * use cmp::max for best importing number comparison * remove import of non-existent create_peer * add sender to start message
Fix #1817