Skip to content
Snippets Groups Projects
Verified Commit a511601f authored by Michal Kucharczyk's avatar Michal Kucharczyk
Browse files

mvl: finality_timeout call added + renaming

parent 1a3a1284
No related merge requests found
......@@ -84,22 +84,27 @@ enum TransactionStatusUpdate<ChainApi: graph::ChainApi> {
/// Marks a transaction as invalidated.
///
/// If all pre-conditions are met, an external invalid event will be sent out.
TransactionInvalidated(ExtrinsicHash<ChainApi>),
Invalidated(ExtrinsicHash<ChainApi>),
/// Notifies that a transaction was finalized in a specific block hash and transaction index.
///
/// Send out an external finalized event.
TransactionFinalized(ExtrinsicHash<ChainApi>, BlockHash<ChainApi>, TxIndex),
Finalized(ExtrinsicHash<ChainApi>, BlockHash<ChainApi>, TxIndex),
/// Notifies that a transaction was broadcasted with a list of peer addresses.
///
/// Sends out an external broadcasted event.
TransactionBroadcasted(ExtrinsicHash<ChainApi>, Vec<String>),
Broadcasted(ExtrinsicHash<ChainApi>, Vec<String>),
/// Notifies that a transaction was dropped from the pool.
///
/// If all preconditions are met, an external dropped event will be sent out.
TransactionDropped(ExtrinsicHash<ChainApi>, DroppedReason<ExtrinsicHash<ChainApi>>),
Dropped(ExtrinsicHash<ChainApi>, DroppedReason<ExtrinsicHash<ChainApi>>),
/// Notifies that a finality watcher timed out.
///
/// An external finality timed out event will be sent out.
FinalityTimeout(ExtrinsicHash<ChainApi>, BlockHash<ChainApi>),
}
impl<ChainApi> TransactionStatusUpdate<ChainApi>
......@@ -108,10 +113,11 @@ where
{
fn hash(&self) -> ExtrinsicHash<ChainApi> {
match self {
Self::TransactionInvalidated(hash) |
Self::TransactionFinalized(hash, _, _) |
Self::TransactionBroadcasted(hash, _) |
Self::TransactionDropped(hash, _) => *hash,
Self::Invalidated(hash) |
Self::Finalized(hash, _, _) |
Self::Broadcasted(hash, _) |
Self::Dropped(hash, _) => *hash,
Self::FinalityTimeout(hash, _) => *hash,
}
}
}
......@@ -123,17 +129,19 @@ where
{
fn into(self) -> TransactionStatus<ExtrinsicHash<ChainApi>, BlockHash<ChainApi>> {
match self {
TransactionStatusUpdate::TransactionInvalidated(_) => TransactionStatus::Invalid,
TransactionStatusUpdate::TransactionFinalized(_, hash, index) =>
TransactionStatusUpdate::Invalidated(_) => TransactionStatus::Invalid,
TransactionStatusUpdate::Finalized(_, hash, index) =>
TransactionStatus::Finalized((*hash, *index)),
TransactionStatusUpdate::TransactionBroadcasted(_, peers) =>
TransactionStatusUpdate::Broadcasted(_, peers) =>
TransactionStatus::Broadcast(peers.clone()),
TransactionStatusUpdate::TransactionDropped(_, DroppedReason::Usurped(by)) =>
TransactionStatusUpdate::Dropped(_, DroppedReason::Usurped(by)) =>
TransactionStatus::Usurped(*by),
TransactionStatusUpdate::TransactionDropped(_, DroppedReason::LimitsEnforced) =>
TransactionStatusUpdate::Dropped(_, DroppedReason::LimitsEnforced) =>
TransactionStatus::Dropped,
TransactionStatusUpdate::TransactionDropped(_, DroppedReason::Invalid) =>
TransactionStatusUpdate::Dropped(_, DroppedReason::Invalid) =>
TransactionStatus::Invalid,
TransactionStatusUpdate::FinalityTimeout(_, block_hash) =>
TransactionStatus::FinalityTimeout(*block_hash),
}
}
}
......@@ -144,17 +152,20 @@ where
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::TransactionInvalidated(h) => {
write!(f, "TransactionInvalidated({h})")
Self::Invalidated(h) => {
write!(f, "Invalidated({h})")
},
Self::Finalized(h, b, i) => {
write!(f, "Finalized({h},{b},{i})")
},
Self::TransactionFinalized(h, b, i) => {
write!(f, "FinalizeTransaction({h},{b},{i})")
Self::Broadcasted(h, _) => {
write!(f, "Broadcasted({h})")
},
Self::TransactionBroadcasted(h, _) => {
write!(f, "TransactionBroadcasted({h})")
Self::Dropped(h, r) => {
write!(f, "Dropped({h},{r:?})")
},
Self::TransactionDropped(h, r) => {
write!(f, "TransactionDropped({h},{r:?})")
Self::FinalityTimeout(h, b) => {
write!(f, "FinalityTimeout({h},{b:?})")
},
}
}
......@@ -174,45 +185,54 @@ where
}
}
}
impl<ChainApi> ControllerCommand<ChainApi>
where
ChainApi: graph::ChainApi,
{
/// Creates new instance of a command requesting [`TransactionStatus::Invalid`] transaction
/// status.
fn new_transaction_invalidated(tx_hash: ExtrinsicHash<ChainApi>) -> Self {
ControllerCommand::TransactionStatusRequest(
TransactionStatusUpdate::TransactionInvalidated(tx_hash),
)
fn new_invalidated(tx_hash: ExtrinsicHash<ChainApi>) -> Self {
ControllerCommand::TransactionStatusRequest(TransactionStatusUpdate::Invalidated(tx_hash))
}
/// Creates new instance of a command requesting [`TransactionStatus::Broadcast`] transaction
/// status.
fn new_transaction_broadcasted(tx_hash: ExtrinsicHash<ChainApi>, peers: Vec<String>) -> Self {
ControllerCommand::TransactionStatusRequest(
TransactionStatusUpdate::TransactionBroadcasted(tx_hash, peers),
)
fn new_broadcasted(tx_hash: ExtrinsicHash<ChainApi>, peers: Vec<String>) -> Self {
ControllerCommand::TransactionStatusRequest(TransactionStatusUpdate::Broadcasted(
tx_hash, peers,
))
}
/// Creates new instance of a command requesting [`TransactionStatus::Finalized`] transaction
/// status.
fn new_transaction_finalized(
fn new_finalized(
tx_hash: ExtrinsicHash<ChainApi>,
block_hash: BlockHash<ChainApi>,
index: TxIndex,
) -> Self {
ControllerCommand::TransactionStatusRequest(TransactionStatusUpdate::TransactionFinalized(
ControllerCommand::TransactionStatusRequest(TransactionStatusUpdate::Finalized(
tx_hash, block_hash, index,
))
}
/// Creates new instance of a command requesting [`TransactionStatus::Dropped`] transaction
/// status.
fn new_transaction_dropped(
fn new_dropped(
tx_hash: ExtrinsicHash<ChainApi>,
reason: DroppedReason<ExtrinsicHash<ChainApi>>,
) -> Self {
ControllerCommand::TransactionStatusRequest(TransactionStatusUpdate::TransactionDropped(
ControllerCommand::TransactionStatusRequest(TransactionStatusUpdate::Dropped(
tx_hash, reason,
))
}
/// Creates new instance of a command requesting [`TransactionStatus::FinalityTimeout`]
/// transaction status.
fn new_finality_timeout(
tx_hash: ExtrinsicHash<ChainApi>,
block_hash: BlockHash<ChainApi>,
) -> Self {
ControllerCommand::TransactionStatusRequest(TransactionStatusUpdate::FinalityTimeout(
tx_hash, block_hash,
))
}
}
/// This struct allows to create and control listener for multiple transactions.
......@@ -366,11 +386,11 @@ where
Some(status)
}
},
TransactionStatus::FinalityTimeout(_) => Some(status),
TransactionStatus::Finalized(_) => {
self.terminate = true;
Some(status)
},
TransactionStatus::FinalityTimeout(_) |
TransactionStatus::Retracted(_) |
TransactionStatus::Broadcast(_) |
TransactionStatus::Usurped(_) |
......@@ -667,9 +687,8 @@ where
pub(crate) fn transactions_invalidated(&self, invalid_hashes: &[ExtrinsicHash<ChainApi>]) {
log_xt_trace!(target: LOG_TARGET, invalid_hashes, "transactions_invalidated");
for tx_hash in invalid_hashes {
if let Err(error) = self
.controller
.unbounded_send(ControllerCommand::new_transaction_invalidated(*tx_hash))
if let Err(error) =
self.controller.unbounded_send(ControllerCommand::new_invalidated(*tx_hash))
{
trace!(
target: LOG_TARGET,
......@@ -692,7 +711,7 @@ where
for (tx_hash, peers) in propagated {
if let Err(error) = self
.controller
.unbounded_send(ControllerCommand::new_transaction_broadcasted(tx_hash, peers))
.unbounded_send(ControllerCommand::new_broadcasted(tx_hash, peers))
{
trace!(
target: LOG_TARGET,
......@@ -711,9 +730,8 @@ where
pub(crate) fn transaction_dropped(&self, dropped: DroppedTransaction<ExtrinsicHash<ChainApi>>) {
let DroppedTransaction { tx_hash, reason } = dropped;
trace!(target: LOG_TARGET, ?tx_hash, ?reason, "transaction_dropped");
if let Err(error) = self
.controller
.unbounded_send(ControllerCommand::new_transaction_dropped(tx_hash, reason))
if let Err(error) =
self.controller.unbounded_send(ControllerCommand::new_dropped(tx_hash, reason))
{
trace!(
target: LOG_TARGET,
......@@ -736,7 +754,7 @@ where
trace!(target: LOG_TARGET, ?tx_hash, "transaction_finalized");
if let Err(error) = self
.controller
.unbounded_send(ControllerCommand::new_transaction_finalized(tx_hash, block, idx))
.unbounded_send(ControllerCommand::new_finalized(tx_hash, block, idx))
{
trace!(
target: LOG_TARGET,
......@@ -747,6 +765,30 @@ where
};
}
/// Send `FinalityTimeout` event for given transactions at given block.
///
/// This will trigger `FinalityTimeout` event to the external watcher.
pub(crate) fn transactions_finality_timeout(
&self,
tx_hashes: &[ExtrinsicHash<ChainApi>],
block: BlockHash<ChainApi>,
) {
for tx_hash in tx_hashes {
trace!(target: LOG_TARGET, ?tx_hash, "transaction_finality_timeout");
if let Err(error) = self
.controller
.unbounded_send(ControllerCommand::new_finality_timeout(*tx_hash, block))
{
trace!(
target: LOG_TARGET,
?tx_hash,
%error,
"transaction_finality_timeout: send message failed"
);
};
}
}
/// Removes stale controllers.
pub(crate) fn remove_stale_controllers(&self) {
self.external_controllers.write().retain(|_, c| !c.is_closed());
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment