Skip to content

Commit f5d3fc1

Browse files
bkchrandresilva
authored andcommitted
Send import notification always for re-orgs (paritytech#7118)
* Send import notification always for re-orgs This pr changes the behavior of sending import notifications. Before we only send notifications when importing blocks on the tip of the chain or on similar conditions. However we did not send a notification when we for example being in a state where we import multiple blocks to catch up. If we re-org in this process, systems like the transaction pool would not be notified about this re-org. This means, that we would also not resubmit transactions of these retracted blocks. This pr fixes this, by always sending a notification on a re-org. See https://github.com/substrate-developer-hub/substrate-node-template/issues/82 for some context about the bug. * Update client/service/src/client/client.rs Co-authored-by: André Silva <[email protected]> Co-authored-by: André Silva <[email protected]>
1 parent 83544d4 commit f5d3fc1

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

client/service/src/client/client.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -802,7 +802,8 @@ impl<B, E, Block, RA> Client<B, E, Block, RA> where
802802

803803
operation.op.insert_aux(aux)?;
804804

805-
if make_notifications {
805+
// we only notify when we are already synced to the tip of the chain or if this import triggers a re-org
806+
if make_notifications || tree_route.is_some() {
806807
if finalized {
807808
operation.notify_finalized.push(hash);
808809
}

client/service/test/src/client/mod.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1802,3 +1802,57 @@ fn cleans_up_closed_notification_sinks_on_block_import() {
18021802
assert_eq!(client.finality_notification_sinks().lock().len(), 0);
18031803
}
18041804

1805+
/// Test that ensures that we always send an import notification for re-orgs.
1806+
#[test]
1807+
fn reorg_triggers_a_notification_even_for_sources_that_should_not_trigger_notifications() {
1808+
let mut client = TestClientBuilder::new().build();
1809+
1810+
let mut notification_stream = futures::executor::block_on_stream(
1811+
client.import_notification_stream()
1812+
);
1813+
1814+
let a1 = client.new_block_at(
1815+
&BlockId::Number(0),
1816+
Default::default(),
1817+
false,
1818+
).unwrap().build().unwrap().block;
1819+
client.import(BlockOrigin::NetworkInitialSync, a1.clone()).unwrap();
1820+
1821+
let a2 = client.new_block_at(
1822+
&BlockId::Hash(a1.hash()),
1823+
Default::default(),
1824+
false,
1825+
).unwrap().build().unwrap().block;
1826+
client.import(BlockOrigin::NetworkInitialSync, a2.clone()).unwrap();
1827+
1828+
let mut b1 = client.new_block_at(
1829+
&BlockId::Number(0),
1830+
Default::default(),
1831+
false,
1832+
).unwrap();
1833+
// needed to make sure B1 gets a different hash from A1
1834+
b1.push_transfer(Transfer {
1835+
from: AccountKeyring::Alice.into(),
1836+
to: AccountKeyring::Ferdie.into(),
1837+
amount: 1,
1838+
nonce: 0,
1839+
}).unwrap();
1840+
let b1 = b1.build().unwrap().block;
1841+
client.import(BlockOrigin::NetworkInitialSync, b1.clone()).unwrap();
1842+
1843+
let b2 = client.new_block_at(
1844+
&BlockId::Hash(b1.hash()),
1845+
Default::default(),
1846+
false,
1847+
).unwrap().build().unwrap().block;
1848+
1849+
// Should trigger a notification because we reorg
1850+
client.import_as_best(BlockOrigin::NetworkInitialSync, b2.clone()).unwrap();
1851+
1852+
// There should be one notification
1853+
let notification = notification_stream.next().unwrap();
1854+
1855+
// We should have a tree route of the re-org
1856+
let tree_route = notification.tree_route.unwrap();
1857+
assert_eq!(tree_route.enacted()[0].hash, b1.hash());
1858+
}

0 commit comments

Comments
 (0)