This repository has been archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Add transactions from retracted blocks back to the pool #3562
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
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 |
---|---|---|
|
@@ -832,7 +832,7 @@ ServiceBuilder< | |
rpc_extensions | ||
)) | ||
}, | ||
|h, c, tx| maintain_transaction_pool(h, c, tx), | ||
|h, c, tx, r| maintain_transaction_pool(h, c, tx, r), | ||
|n, o, p, ns, v| offchain_workers(n, o, p, ns, v), | ||
|c, ssb, si, te, tp, ext, ks| start_rpc(&rpc_builder, c, ssb, si, te, tp, ext, ks), | ||
) | ||
|
@@ -885,6 +885,7 @@ pub(crate) fn maintain_transaction_pool<Api, Backend, Block, Executor, PoolApi>( | |
id: &BlockId<Block>, | ||
client: &Client<Backend, Executor, Block, Api>, | ||
transaction_pool: &TransactionPool<PoolApi>, | ||
retracted: &[Block::Hash], | ||
) -> error::Result<()> where | ||
Block: BlockT<Hash = <Blake2Hasher as primitives::Hasher>::Out>, | ||
Backend: client::backend::Backend<Block, Blake2Hasher>, | ||
|
@@ -893,6 +894,16 @@ pub(crate) fn maintain_transaction_pool<Api, Backend, Block, Executor, PoolApi>( | |
Executor: client::CallExecutor<Block, Blake2Hasher>, | ||
PoolApi: txpool::ChainApi<Hash = Block::Hash, Block = Block>, | ||
{ | ||
// Put transactions from retracted blocks back into the pool. | ||
for r in retracted { | ||
if let Some(block) = client.block(&BlockId::hash(*r))? { | ||
let extrinsics = block.block.extrinsics(); | ||
if let Err(e) = transaction_pool.submit_at(id, extrinsics.iter().cloned(), true) { | ||
warn!("Error re-submitting transactions: {:?}", e); | ||
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. This might be triggered quite often in case of re-orgs. I would either lower this to 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.
|
||
} | ||
} | ||
} | ||
|
||
// Avoid calling into runtime if there is nothing to prune from the pool anyway. | ||
if transaction_pool.status().is_empty() { | ||
return Ok(()) | ||
|
@@ -968,10 +979,67 @@ mod tests { | |
&id, | ||
&client, | ||
&pool, | ||
&[] | ||
).unwrap(); | ||
|
||
// then | ||
assert_eq!(pool.status().ready, 0); | ||
assert_eq!(pool.status().future, 0); | ||
} | ||
|
||
#[test] | ||
fn should_add_reverted_transactions_to_the_pool() { | ||
let (client, longest_chain) = TestClientBuilder::new().build_with_longest_chain(); | ||
let client = Arc::new(client); | ||
let pool = TransactionPool::new(Default::default(), ::transaction_pool::ChainApi::new(client.clone())); | ||
let transaction = Transfer { | ||
amount: 5, | ||
nonce: 0, | ||
from: AccountKeyring::Alice.into(), | ||
to: Default::default(), | ||
}.into_signed_tx(); | ||
let best = longest_chain.best_chain().unwrap(); | ||
|
||
// store the transaction in the pool | ||
pool.submit_one(&BlockId::hash(best.hash()), transaction.clone()).unwrap(); | ||
|
||
// import the block | ||
let mut builder = client.new_block(Default::default()).unwrap(); | ||
builder.push(transaction.clone()).unwrap(); | ||
let block = builder.bake().unwrap(); | ||
let block1_hash = block.header().hash(); | ||
let id = BlockId::hash(block1_hash.clone()); | ||
client.import(BlockOrigin::Own, block).unwrap(); | ||
|
||
// fire notification - this should clean up the queue | ||
assert_eq!(pool.status().ready, 1); | ||
maintain_transaction_pool( | ||
&id, | ||
&client, | ||
&pool, | ||
&[] | ||
).unwrap(); | ||
|
||
// then | ||
assert_eq!(pool.status().ready, 0); | ||
assert_eq!(pool.status().future, 0); | ||
|
||
// import second block | ||
let builder = client.new_block_at(&BlockId::hash(best.hash()), Default::default()).unwrap(); | ||
let block = builder.bake().unwrap(); | ||
let id = BlockId::hash(block.header().hash()); | ||
client.import(BlockOrigin::Own, block).unwrap(); | ||
|
||
// fire notification - this should add the transaction back to the pool. | ||
maintain_transaction_pool( | ||
&id, | ||
&client, | ||
&pool, | ||
&[block1_hash] | ||
).unwrap(); | ||
|
||
// then | ||
assert_eq!(pool.status().ready, 1); | ||
assert_eq!(pool.status().future, 0); | ||
} | ||
} |
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.
Does it makes sense to change to
retracted.into_iter().rev()
, so that depending transactions are imported in-order and not moving to/from future/ready queues? I mean if block #5 had a TX1 that provides OUT1 and block #6 had a TX2 that requires OUT1, then if we first import TX2, then it'll be inserted to future queue (because we miss OUT1). And after we import TX1, it'll be moved to ready queue (because TX1 provides OUT1). If we import TX1 first, then both transactions will be inserted in ready queue immediately.(and hashes in
retracted
are in reverse order, afaik)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.
Changes
retracted
in notification to be in ascending order, as documented.