1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
use core::fmt;

use tendermint_rpc::endpoint::broadcast::tx_sync;
use tracing::info;

use ibc::events::{IbcEvent, PrettyEvents};

use crate::chain::handle::ChainHandle;
use crate::chain::tracking::TrackedMsgs;
use crate::link::error::LinkError;
use crate::link::RelaySummary;

pub trait SubmitReply {
    /// Creates a new, empty instance, i.e., comprising zero replies.
    fn empty() -> Self;

    /// Counts the number of replies that this instance contains.
    fn len(&self) -> usize;
}

impl SubmitReply for RelaySummary {
    fn empty() -> Self {
        RelaySummary::empty()
    }

    fn len(&self) -> usize {
        self.events.len()
    }
}

/// Captures the ability to submit messages to a chain.
pub trait Submit {
    type Reply: SubmitReply;

    fn submit(target: &impl ChainHandle, msgs: TrackedMsgs) -> Result<Self::Reply, LinkError>;
}

/// Synchronous sender
pub struct SyncSender;

impl Submit for SyncSender {
    type Reply = RelaySummary;

    // TODO: Switch from the `Chain::send_msgs` interface in this method
    //  to use `Chain::submit_msgs` instead; implement waiting for block
    //  commits directly here (instead of blocking in the chain runtime).
    fn submit(target: &impl ChainHandle, msgs: TrackedMsgs) -> Result<Self::Reply, LinkError> {
        let tx_events = target
            .send_messages_and_wait_commit(msgs)
            .map_err(LinkError::relayer)?;

        info!(
            "[Sync->{}] result {}\n",
            target.id(),
            PrettyEvents(&tx_events)
        );

        let ev = tx_events
            .clone()
            .into_iter()
            .find(|event| matches!(event, IbcEvent::ChainError(_)));

        match ev {
            Some(ev) => Err(LinkError::send(ev)),
            None => Ok(RelaySummary::from_events(tx_events)),
        }
    }
}

pub struct AsyncReply {
    pub responses: Vec<tx_sync::Response>,
}

impl SubmitReply for AsyncReply {
    fn empty() -> Self {
        Self { responses: vec![] }
    }

    fn len(&self) -> usize {
        self.responses.len()
    }
}

// TODO(Adi): Consider removing the senders and keep only a generic
//     send/submit method.
pub struct AsyncSender;

impl Submit for AsyncSender {
    type Reply = AsyncReply;

    fn submit(target: &impl ChainHandle, msgs: TrackedMsgs) -> Result<Self::Reply, LinkError> {
        let a = target
            .send_messages_and_wait_check_tx(msgs)
            .map_err(LinkError::relayer)?;
        let reply = AsyncReply { responses: a };
        info!("[Async~>{}] {}\n", target.id(), reply);

        Ok(reply)
    }
}

impl fmt::Display for AsyncReply {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "response(s): {}", self.responses.len())?;
        self.responses
            .iter()
            .try_for_each(|r| write!(f, "; {:?}:{}", r.code, r.hash))
    }
}