1use cardano_sdk::chain::Transaction;
2use cardano_sdk::protocol::{
3 TxHashWithSize, TxHashes, TxHashesWithSizes, TxSubmit, TxSubmitKind, Txs,
4};
5
6use super::channel::Channel;
7use super::packet::ProtocolError;
8
9pub struct SubmitWait(Channel<TxSubmit>);
10
11pub struct SubmitTxIds {
12 channel: Channel<TxSubmit>,
13 pub blocking: bool,
14 pub ack_outstanding_txs: u16,
15 pub max_outstanding_txs_requested: u16,
16}
17
18pub struct SubmitTxBodies {
19 channel: Channel<TxSubmit>,
20 pub txhashes: TxHashes,
21}
22
23impl Channel<TxSubmit> {
24 pub async fn init(self) -> Result<SubmitWait, ProtocolError> {
25 self.tx(false, TxSubmit::Init).await?;
26 Ok(SubmitWait(self))
27 }
28}
29
30pub enum SubmitNext {
31 RequestId(SubmitTxIds),
32 RequestTx(SubmitTxBodies),
33}
34
35impl SubmitWait {
36 pub async fn recv_next(mut self) -> Result<SubmitNext, ProtocolError> {
37 match self.0.rx().await? {
38 TxSubmit::RequestIds {
39 blocking,
40 ack_outstanding_txs,
41 max_outstanding_txs_requested,
42 } => Ok(SubmitNext::RequestId(SubmitTxIds {
43 channel: self.0,
44 blocking,
45 ack_outstanding_txs,
46 max_outstanding_txs_requested,
47 })),
48 TxSubmit::RequestTxs(txhashes) => Ok(SubmitNext::RequestTx(SubmitTxBodies {
49 channel: self.0,
50 txhashes,
51 })),
52 cs => Err(ProtocolError::UnexpectedVariant(
53 "tx-submit (recv-next)".to_string(),
54 format!("{:?}", TxSubmitKind::from(&cs)),
55 )),
56 }
57 }
58
59 pub async fn done(self) -> Result<Channel<TxSubmit>, ProtocolError> {
60 self.0.tx(false, TxSubmit::Done).await?;
61 Ok(self.0)
62 }
63}
64
65impl SubmitTxIds {
66 pub async fn reply(self, hashes: &[TxHashWithSize]) -> Result<SubmitWait, ProtocolError> {
67 self.channel
68 .tx(
69 true,
70 TxSubmit::ReplyIds(TxHashesWithSizes::from(
71 hashes.into_iter().cloned().collect::<Vec<_>>(),
72 )),
73 )
74 .await?;
75 Ok(SubmitWait(self.channel))
76 }
77}
78
79impl SubmitTxBodies {
80 pub async fn reply(self, txs: &[Transaction]) -> Result<SubmitWait, ProtocolError> {
81 self.channel
82 .tx(
83 true,
84 TxSubmit::ReplyTxs(Txs::from(txs.into_iter().cloned().collect::<Vec<_>>())),
85 )
86 .await?;
87 Ok(SubmitWait(self.channel))
88 }
89}