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
use eyre::eyre;
use serde_json as json;
use std::time::Duration;

use crate::chain::cli::query::query_cross_chain_query;
use crate::error::Error;
use crate::prelude::{assert_eventually_succeed, handle_generic_error, ChainDriver};
use crate::types::tagged::MonoTagged;

/**
   Number of times (seconds) to try and query the list of cross chain
   queries.

   If you encounter retry error, verify the value of `stride_epoch`in
   the `stride_epoch` configuration in Stride's `genesis.toml` file.
*/
const WAIT_CROSS_CHAIN_QUERY_ATTEMPTS: u16 = 60;

pub trait CrossChainQueryMethodsExt<Chain> {
    fn assert_pending_cross_chain_query(&self) -> Result<(), Error>;

    fn assert_processed_cross_chain_query(&self) -> Result<(), Error>;
}

impl<'a, Chain: Send> CrossChainQueryMethodsExt<Chain> for MonoTagged<Chain, &'a ChainDriver> {
    fn assert_pending_cross_chain_query(&self) -> Result<(), Error> {
        assert_eventually_succeed(
            "waiting for a cross chain query request",
            WAIT_CROSS_CHAIN_QUERY_ATTEMPTS,
            Duration::from_secs(1),
            || {
                let output = query_cross_chain_query(
                    self.0.chain_id.as_str(),
                    &self.0.command_path,
                    &self.0.rpc_listen_address(),
                )?;

                // Verify that there is at least one pending Cross Chain Query.
                let request_sent = json::from_str::<json::Value>(&output)
                    .map_err(handle_generic_error)?
                    .get("pending_queries")
                    .ok_or_else(|| eyre!("no pending cross chain queries"))?
                    .as_array()
                    .ok_or_else(|| eyre!("pending cross chain queries is not an array"))?
                    .first()
                    .ok_or_else(|| eyre!("no pending cross chain queries"))?
                    .as_bool();

                if let Some(sent) = request_sent {
                    if !sent {
                        return Err(Error::generic(eyre!("Request found but not sent")));
                    }
                }

                Ok(())
            },
        )?;

        Ok(())
    }

    fn assert_processed_cross_chain_query(&self) -> Result<(), Error> {
        assert_eventually_succeed(
            "waiting for the cross chain query to be relayed",
            WAIT_CROSS_CHAIN_QUERY_ATTEMPTS,
            Duration::from_secs(1),
            || {
                let output = query_cross_chain_query(
                    self.0.chain_id.as_str(),
                    &self.0.command_path,
                    &self.0.rpc_listen_address(),
                )?;

                // Verify that the there are no more pending Cross Chain Queries.
                if json::from_str::<json::Value>(&output)
                    .map_err(handle_generic_error)?
                    .get("pending_queries")
                    .ok_or_else(|| eyre!("no pending cross chain queries"))?
                    .as_array()
                    .ok_or_else(|| eyre!("pending cross chain queries is not an array"))?
                    .first()
                    .is_some()
                {
                    return Err(Error::generic(eyre!(
                        "Pending query has not been processed"
                    )));
                }

                Ok(())
            },
        )?;

        Ok(())
    }
}