rpc-v2/tx: Remove the broadcast event from transaction_submitAndWatch (#3321)

This PR backports the changes from the rpc-v2 spec:
https://github.com/paritytech/json-rpc-interface-spec/pull/134

The `Broadcasted` event has been removed:
- it is hard to enforce a `Dropped { broadcasted: bool }` event in cases
of a load-balancer being placed in front of an RPC server
- when the server exists, it is impossible to guarantee this field if
the server did not previously send a `Broadcasted` event
- the number of peers reported by this event does not guarantee that
peers are unique
- the same peer can disconnect and reconnect, increasing this metric
number
- the number of peers that receive this transaction offers no guarantee
about the transaction being included in the chain at a later time


cc @paritytech/subxt-team

---------

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>
Co-authored-by: James Wilson <james@jsdw.me>
This commit is contained in:
Alexandru Vasile
2024-02-21 18:31:49 +02:00
committed by GitHub
parent 165d075a5f
commit 1b624c5072
3 changed files with 38 additions and 123 deletions
@@ -22,10 +22,7 @@ use crate::{
transaction::{
api::TransactionApiServer,
error::Error,
event::{
TransactionBlock, TransactionBroadcasted, TransactionDropped, TransactionError,
TransactionEvent,
},
event::{TransactionBlock, TransactionDropped, TransactionError, TransactionEvent},
},
SubscriptionTaskExecutor,
};
@@ -113,9 +110,7 @@ where
match submit.await {
Ok(stream) => {
let mut state = TransactionState::new();
let stream =
stream.filter_map(move |event| async move { state.handle_event(event) });
let stream = stream.filter_map(move |event| async move { handle_event(event) });
pipe_from_stream(pending, stream.boxed()).await;
},
Err(err) => {
@@ -131,66 +126,34 @@ where
}
}
/// The transaction's state that needs to be preserved between
/// multiple events generated by the transaction-pool.
///
/// # Note
///
/// In the future, the RPC server can submit only the last event when multiple
/// identical events happen in a row.
#[derive(Clone, Copy)]
struct TransactionState {
/// True if the transaction was previously broadcasted.
broadcasted: bool,
}
impl TransactionState {
/// Construct a new [`TransactionState`].
pub fn new() -> Self {
TransactionState { broadcasted: false }
}
/// Handle events generated by the transaction-pool and convert them
/// to the new API expected state.
#[inline]
pub fn handle_event<Hash: Clone, BlockHash: Clone>(
&mut self,
event: TransactionStatus<Hash, BlockHash>,
) -> Option<TransactionEvent<BlockHash>> {
match event {
TransactionStatus::Ready | TransactionStatus::Future =>
Some(TransactionEvent::<BlockHash>::Validated),
TransactionStatus::Broadcast(peers) => {
// Set the broadcasted flag once if we submitted the transaction to
// at least one peer.
self.broadcasted = self.broadcasted || !peers.is_empty();
Some(TransactionEvent::Broadcasted(TransactionBroadcasted {
num_peers: peers.len(),
}))
},
TransactionStatus::InBlock((hash, index)) =>
Some(TransactionEvent::BestChainBlockIncluded(Some(TransactionBlock {
hash,
index,
}))),
TransactionStatus::Retracted(_) => Some(TransactionEvent::BestChainBlockIncluded(None)),
TransactionStatus::FinalityTimeout(_) =>
Some(TransactionEvent::Dropped(TransactionDropped {
broadcasted: self.broadcasted,
error: "Maximum number of finality watchers has been reached".into(),
})),
TransactionStatus::Finalized((hash, index)) =>
Some(TransactionEvent::Finalized(TransactionBlock { hash, index })),
TransactionStatus::Usurped(_) => Some(TransactionEvent::Invalid(TransactionError {
error: "Extrinsic was rendered invalid by another extrinsic".into(),
/// Handle events generated by the transaction-pool and convert them
/// to the new API expected state.
#[inline]
pub fn handle_event<Hash: Clone, BlockHash: Clone>(
event: TransactionStatus<Hash, BlockHash>,
) -> Option<TransactionEvent<BlockHash>> {
match event {
TransactionStatus::Ready | TransactionStatus::Future =>
Some(TransactionEvent::<BlockHash>::Validated),
TransactionStatus::InBlock((hash, index)) =>
Some(TransactionEvent::BestChainBlockIncluded(Some(TransactionBlock { hash, index }))),
TransactionStatus::Retracted(_) => Some(TransactionEvent::BestChainBlockIncluded(None)),
TransactionStatus::FinalityTimeout(_) =>
Some(TransactionEvent::Dropped(TransactionDropped {
error: "Maximum number of finality watchers has been reached".into(),
})),
TransactionStatus::Dropped => Some(TransactionEvent::Invalid(TransactionError {
error: "Extrinsic dropped from the pool due to exceeding limits".into(),
})),
TransactionStatus::Invalid => Some(TransactionEvent::Invalid(TransactionError {
error: "Extrinsic marked as invalid".into(),
})),
}
TransactionStatus::Finalized((hash, index)) =>
Some(TransactionEvent::Finalized(TransactionBlock { hash, index })),
TransactionStatus::Usurped(_) => Some(TransactionEvent::Invalid(TransactionError {
error: "Extrinsic was rendered invalid by another extrinsic".into(),
})),
TransactionStatus::Dropped => Some(TransactionEvent::Invalid(TransactionError {
error: "Extrinsic dropped from the pool due to exceeding limits".into(),
})),
TransactionStatus::Invalid => Some(TransactionEvent::Invalid(TransactionError {
error: "Extrinsic marked as invalid".into(),
})),
// These are the events that are not supported by the new API.
TransactionStatus::Broadcast(_) => None,
}
}