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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913
//! Queries and methods for interfacing with foreign clients.
//!
//! The term "foreign client" refers to IBC light clients that are running on-chain,
//! i.e. they are *foreign* to the relayer. In contrast, the term "local client"
//! refers to light clients running *locally* as part of the relayer.
use core::{fmt, time::Duration};
use std::thread;
use std::time::Instant;
use ibc_proto::google::protobuf::Any;
use itertools::Itertools;
use tracing::{debug, error, info, instrument, trace, warn};
use flex_error::define_error;
use ibc_relayer_types::applications::ics28_ccv::msgs::ccv_misbehaviour::MsgSubmitIcsConsumerMisbehaviour;
use ibc_relayer_types::core::ics02_client::client_state::ClientState;
use ibc_relayer_types::core::ics02_client::error::Error as ClientError;
use ibc_relayer_types::core::ics02_client::events::UpdateClient;
use ibc_relayer_types::core::ics02_client::header::{AnyHeader, Header};
use ibc_relayer_types::core::ics02_client::msgs::create_client::MsgCreateClient;
use ibc_relayer_types::core::ics02_client::msgs::misbehaviour::MsgSubmitMisbehaviour;
use ibc_relayer_types::core::ics02_client::msgs::update_client::MsgUpdateClient;
use ibc_relayer_types::core::ics02_client::msgs::upgrade_client::MsgUpgradeClient;
use ibc_relayer_types::core::ics02_client::trust_threshold::TrustThreshold;
use ibc_relayer_types::core::ics24_host::identifier::{ChainId, ClientId};
use ibc_relayer_types::downcast;
use ibc_relayer_types::events::{IbcEvent, IbcEventType, WithBlockDataType};
use ibc_relayer_types::timestamp::{Timestamp, TimestampOverflowError};
use ibc_relayer_types::tx_msg::Msg;
use ibc_relayer_types::Height;
use crate::chain::client::ClientSettings;
use crate::chain::handle::ChainHandle;
use crate::chain::requests::*;
use crate::chain::tracking::TrackedMsgs;
use crate::client_state::AnyClientState;
use crate::consensus_state::AnyConsensusState;
use crate::error::Error as RelayerError;
use crate::event::IbcEventWithHeight;
use crate::misbehaviour::{AnyMisbehaviour, MisbehaviourEvidence};
use crate::telemetry;
use crate::util::collate::CollatedIterExt;
use crate::util::pretty::{PrettyDuration, PrettySlice};
const MAX_MISBEHAVIOUR_CHECK_DURATION: Duration = Duration::from_secs(120);
const MAX_RETRIES: usize = 5;
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum ExpiredOrFrozen {
    Expired,
    Frozen,
}
impl fmt::Display for ExpiredOrFrozen {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ExpiredOrFrozen::Expired => write!(f, "expired"),
            ExpiredOrFrozen::Frozen => write!(f, "frozen"),
        }
    }
}
define_error! {
    ForeignClientError {
        ClientCreate
            {
                chain_id: ChainId,
                description: String
            }
            [ RelayerError ]
            |e| {
                format_args!("error raised while creating client for chain {0}: {1}",
                    e.chain_id, e.description)
            },
        Client
            [ ClientError ]
            |_| { "ICS02 client error" },
        HeaderInTheFuture
            {
                src_chain_id: ChainId,
                src_header_height: Height,
                src_header_time: Timestamp,
                dst_chain_id: ChainId,
                dst_latest_header_height: Height,
                dst_latest_header_time: Timestamp,
                max_drift: Duration
            }
            |e| {
                format_args!("update header from {} with height {} and time {} is in the future compared with latest header on {} with height {} and time {}, adjusted with drift {:?}",
                 e.src_chain_id, e.src_header_height, e.src_header_time, e.dst_chain_id, e.dst_latest_header_height, e.dst_latest_header_time, e.max_drift)
            },
        ClientUpdate
            {
                chain_id: ChainId,
                description: String
            }
            [ RelayerError ]
            |e| {
                format_args!("error raised while updating client on chain {0}: {1}", e.chain_id, e.description)
            },
        ClientUpdateTiming
            {
                chain_id: ChainId,
                clock_drift: Duration,
                description: String
            }
            [ TimestampOverflowError ]
            |e| {
                format_args!("error raised while updating client on chain {0}: {1}", e.chain_id, e.description)
            },
        ClientAlreadyUpToDate
            {
                client_id: ClientId,
                chain_id: ChainId,
                height: Height,
            }
            |e| {
                format_args!("client {} is already up-to-date with chain {} at height {}",
                    e.client_id, e.chain_id, e.height)
            },
        MissingSmallerTrustedHeight
            {
                chain_id: ChainId,
                target_height: Height,
            }
            |e| {
                format_args!("chain {} is missing trusted state smaller than target height {}",
                    e.chain_id, e.target_height)
            },
        MissingTrustedHeight
            {
                chain_id: ChainId,
                target_height: Height,
            }
            |e| {
                format_args!("chain {} is missing trusted state at target height {}",
                    e.chain_id, e.target_height)
            },
        ClientRefresh
            {
                client_id: ClientId,
                reason: String
            }
            [ RelayerError ]
            |e| {
                format_args!("error raised while trying to refresh client {0}: {1}",
                    e.client_id, e.reason)
            },
        ClientQuery
            {
                client_id: ClientId,
                chain_id: ChainId,
            }
            [ RelayerError ]
            |e| {
                format_args!("failed while querying for client {0} on chain id {1}",
                    e.client_id, e.chain_id)
            },
        ClientConsensusQuery
            {
                client_id: ClientId,
                chain_id: ChainId,
                height: Height
            }
            [ RelayerError ]
            |e| {
                format_args!("failed while querying for client consensus state {0} on chain id {1} for height {2}",
                    e.client_id, e.chain_id, e.height)
            },
        ClientUpgrade
            {
                client_id: ClientId,
                chain_id: ChainId,
                description: String,
            }
            [ RelayerError ]
            |e| {
                format_args!("failed while trying to upgrade client id {0} for chain {1}: {2}: {3}",
                    e.client_id, e.chain_id, e.description, e.source)
            },
        ClientUpgradeNoSource
        {
            client_id: ClientId,
            chain_id: ChainId,
            description: String,
        }
        |e| {
            format_args!("failed while trying to upgrade client id {0} for chain {1}: {2}",
                    e.client_id, e.chain_id, e.description)
        },
        ClientEventQuery
            {
                client_id: ClientId,
                chain_id: ChainId,
                consensus_height: Height
            }
            [ RelayerError ]
            |e| {
                format_args!("failed while querying Tx for client {0} on chain id {1} at consensus height {2}",
                    e.client_id, e.chain_id, e.consensus_height)
            },
        UnexpectedEvent
            {
                client_id: ClientId,
                chain_id: ChainId,
                event: String,
            }
            |e| {
                format_args!("failed while querying Tx for client {0} on chain id {1}: query Tx-es returned unexpected event: {2}",
                    e.client_id, e.chain_id, e.event)
            },
        MismatchChainId
            {
                client_id: ClientId,
                expected_chain_id: ChainId,
                actual_chain_id: ChainId,
            }
            |e| {
                format_args!("failed while finding client {0}: expected chain_id in client state: {1}; actual chain_id: {2}",
                    e.client_id, e.expected_chain_id, e.actual_chain_id)
            },
        ExpiredOrFrozen
            {
                status: ExpiredOrFrozen,
                client_id: ClientId,
                chain_id: ChainId,
                description: String,
            }
            |e| {
                format_args!("client {0} on chain id {1} is {2}: {3}",
                    e.client_id, e.chain_id, e.status, e.description)
            },
        ConsensusStateNotTrusted
            {
                height: Height,
                elapsed: Duration,
            }
            |e| {
                format_args!("the consensus state at height {} is outside of trusting period: elapsed {:?}",
                    e.height, e.elapsed)
            },
        Misbehaviour
            {
                description: String,
            }
            [ RelayerError ]
            |e| {
                format_args!("error raised while checking for misbehaviour evidence: {0}", e.description)
            },
        MisbehaviourDesc
            {
                description: String,
            }
            |e| {
                format_args!("error raised while checking for misbehaviour evidence: {0}", e.description)
            },
        MisbehaviourExit
            { reason: String }
            |e| {
                format_args!("cannot run misbehaviour: {0}", e.reason)
            },
        SameChainId
            {
                chain_id: ChainId
            }
            |e| {
                format_args!("the chain ID ({}) at the source and destination chains must be different", e.chain_id)
            },
        MissingClientIdFromEvent
            { event: IbcEvent }
            |e| {
                format_args!("cannot extract client_id from result: {:?}",
                    e.event)
            },
        ChainErrorEvent
            {
                chain_id: ChainId,
                event: IbcEvent
            }
            |e| {
                format_args!("failed to update client on destination {} because of error event: {}",
                    e.chain_id, e.event)
            },
    }
}
pub trait HasExpiredOrFrozenError {
    fn is_expired_error(&self) -> bool;
    fn is_frozen_error(&self) -> bool;
    fn is_expired_or_frozen_error(&self) -> bool {
        self.is_expired_error() || self.is_frozen_error()
    }
}
impl HasExpiredOrFrozenError for ForeignClientErrorDetail {
    fn is_expired_error(&self) -> bool {
        if let Self::ExpiredOrFrozen(e) = self {
            e.status == ExpiredOrFrozen::Expired
        } else {
            false
        }
    }
    fn is_frozen_error(&self) -> bool {
        if let Self::ExpiredOrFrozen(e) = self {
            e.status == ExpiredOrFrozen::Frozen
        } else {
            false
        }
    }
}
impl HasExpiredOrFrozenError for ForeignClientError {
    fn is_expired_error(&self) -> bool {
        self.detail().is_expired_error()
    }
    fn is_frozen_error(&self) -> bool {
        self.detail().is_frozen_error()
    }
}
/// User-supplied options for the [`ForeignClient::build_create_client`] operation.
///
/// Currently, the parameters are specific to the Tendermint-based chains.
/// A future revision will bring differentiated options for other chain types.
#[derive(Debug, Default)]
pub struct CreateOptions {
    pub max_clock_drift: Option<Duration>,
    pub trusting_period: Option<Duration>,
    pub trust_threshold: Option<TrustThreshold>,
}
/// Captures the diagnostic of verifying whether a certain
/// consensus state is within the trusting period (i.e., trusted)
/// or it's not within the trusting period (not trusted).
pub enum ConsensusStateTrusted {
    NotTrusted {
        elapsed: Duration,
        network_timestamp: Timestamp,
        consensus_state_timestmap: Timestamp,
    },
    Trusted {
        elapsed: Duration,
    },
}
#[derive(Clone, Debug)]
pub struct ForeignClient<DstChain: ChainHandle, SrcChain: ChainHandle> {
    /// The identifier of this client. The host chain determines this id upon client creation,
    /// so we may be using the default value temporarily.
    pub id: ClientId,
    /// A handle to the chain hosting this client, i.e., destination chain.
    pub dst_chain: DstChain,
    /// A handle to the chain whose headers this client is verifying, aka the source chain.
    pub src_chain: SrcChain,
}
/// Used in Output messages.
/// Provides a concise description of a [`ForeignClient`],
/// using the format:
///     {CHAIN-ID}->{CHAIN-ID}:{CLIENT}
/// where the first chain identifier is for the source
/// chain, and the second chain identifier is the
/// destination (which hosts the client) chain.
impl<DstChain: ChainHandle, SrcChain: ChainHandle> fmt::Display
    for ForeignClient<DstChain, SrcChain>
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{}->{}:{}",
            self.src_chain.id(),
            self.dst_chain.id(),
            self.id
        )
    }
}
impl<DstChain: ChainHandle, SrcChain: ChainHandle> ForeignClient<DstChain, SrcChain> {
    /// Creates a new foreign client on `dst_chain`. Blocks until the client is created, or
    /// an error occurs.
    /// Post-condition: `dst_chain` hosts an IBC client for `src_chain`.
    pub fn new(
        dst_chain: DstChain,
        src_chain: SrcChain,
    ) -> Result<ForeignClient<DstChain, SrcChain>, ForeignClientError> {
        // Sanity check
        if src_chain.id().eq(&dst_chain.id()) {
            return Err(ForeignClientError::same_chain_id(src_chain.id()));
        }
        let mut client = ForeignClient {
            id: ClientId::default(),
            dst_chain,
            src_chain,
        };
        client.create()?;
        Ok(client)
    }
    pub fn restore(
        id: ClientId,
        dst_chain: DstChain,
        src_chain: SrcChain,
    ) -> ForeignClient<DstChain, SrcChain> {
        ForeignClient {
            id,
            dst_chain,
            src_chain,
        }
    }
    /// Queries `host_chain` to verify that a client with identifier `client_id` exists.
    /// If the client does not exist, returns an error. If the client exists, cross-checks that the
    /// identifier for the target chain of this client (i.e., the chain whose headers this client is
    /// verifying) is consistent with `expected_target_chain`, and if so, return a new
    /// `ForeignClient` representing this client.
    pub fn find(
        expected_target_chain: SrcChain,
        host_chain: DstChain,
        client_id: &ClientId,
    ) -> Result<ForeignClient<DstChain, SrcChain>, ForeignClientError> {
        match host_chain.query_client_state(
            QueryClientStateRequest {
                client_id: client_id.clone(),
                height: QueryHeight::Latest,
            },
            IncludeProof::No,
        ) {
            Ok((cs, _)) => {
                if cs.chain_id() != expected_target_chain.id() {
                    Err(ForeignClientError::mismatch_chain_id(
                        client_id.clone(),
                        expected_target_chain.id(),
                        cs.chain_id(),
                    ))
                } else {
                    // TODO: Any additional checks?
                    Ok(ForeignClient::restore(
                        client_id.clone(),
                        host_chain,
                        expected_target_chain,
                    ))
                }
            }
            Err(e) => Err(ForeignClientError::client_query(
                client_id.clone(),
                host_chain.id(),
                e,
            )),
        }
    }
    /// Create and send a transaction to perform a client upgrade.
    /// src_upgrade_height: The height on the source chain at which the chain will halt for the upgrade.
    #[instrument(
        name = "foreign_client.upgrade",
        level = "error",
        skip(self),
        fields(client = %self)
    )]
    pub fn upgrade(&self, src_upgrade_height: Height) -> Result<Vec<IbcEvent>, ForeignClientError> {
        let msgs = self
            .build_update_client_with_trusted(src_upgrade_height, None)
            .map_err(|_| {
                ForeignClientError::client_upgrade_no_source(
                    self.id.clone(),
                    self.dst_chain.id(),
                    format!(
                        "is chain {} halted at height {}?",
                        self.src_chain().id(),
                        src_upgrade_height
                    ),
                )
            })?;
        let mut msgs: Vec<Any> = msgs.into_iter().map(Msg::to_any).collect();
        // Query the host chain for the upgraded client state, consensus state & their proofs.
        let (client_state, proof_upgrade_client) = self
            .src_chain
            .query_upgraded_client_state(QueryUpgradedClientStateRequest {
                upgrade_height: src_upgrade_height,
            })
            .map_err(|e| {
                ForeignClientError::client_upgrade(
                    self.id.clone(),
                    self.src_chain.id(),
                    format!("failed while fetching from chain the upgraded client state. The upgrade height `{src_upgrade_height}` might be too low"),
                    e,
                )
            })?;
        debug!("upgraded client state {:?}", client_state);
        let (consensus_state, proof_upgrade_consensus_state) = self
            .src_chain
            .query_upgraded_consensus_state(QueryUpgradedConsensusStateRequest {
                upgrade_height: src_upgrade_height,
            })
            .map_err(|e| {
                ForeignClientError::client_upgrade(
                    self.id.clone(),
                    self.src_chain.id(),
                    "failed while fetching from chain the upgraded client consensus state"
                        .to_string(),
                    e,
                )
            })?;
        debug!("upgraded client consensus state {:?}", consensus_state);
        // Get signer
        let signer = self.dst_chain.get_signer().map_err(|e| {
            ForeignClientError::client_upgrade(
                self.id.clone(),
                self.dst_chain.id(),
                "failed while fetching the destination chain signer".to_string(),
                e,
            )
        })?;
        let msg_upgrade = MsgUpgradeClient {
            client_id: self.id.clone(),
            client_state: client_state.into(),
            consensus_state: consensus_state.into(),
            proof_upgrade_client: proof_upgrade_client.into(),
            proof_upgrade_consensus_state: proof_upgrade_consensus_state.into(),
            signer,
        }
        .to_any();
        msgs.push(msg_upgrade);
        let tm = TrackedMsgs::new_static(msgs, "upgrade client");
        let res = self
            .dst_chain
            .send_messages_and_wait_commit(tm)
            .map_err(|e| {
                ForeignClientError::client_upgrade(
                    self.id.clone(),
                    self.dst_chain.id(),
                    "failed while sending message to destination chain".to_string(),
                    e,
                )
            })?;
        Ok(res
            .into_iter()
            .map(|ev_with_height| ev_with_height.event)
            .collect())
    }
    /// Returns a handle to the chain hosting this client.
    pub fn dst_chain(&self) -> DstChain {
        self.dst_chain.clone()
    }
    /// Returns a handle to the chain whose headers this client is sourcing (the source chain).
    pub fn src_chain(&self) -> SrcChain {
        self.src_chain.clone()
    }
    pub fn id(&self) -> &ClientId {
        &self.id
    }
    /// Lower-level interface for preparing a message to create a client.
    pub fn build_create_client(
        &self,
        options: CreateOptions,
    ) -> Result<MsgCreateClient, ForeignClientError> {
        // Get signer
        let signer = self.dst_chain.get_signer().map_err(|e| {
            ForeignClientError::client_create(
                self.src_chain.id(),
                format!(
                    "failed while fetching the dst chain ({}) signer",
                    self.dst_chain.id()
                ),
                e,
            )
        })?;
        // Build client create message with the data from source chain at latest height.
        let latest_height = self.src_chain.query_latest_height().map_err(|e| {
            ForeignClientError::client_create(
                self.src_chain.id(),
                "failed while querying src chain for latest height".to_string(),
                e,
            )
        })?;
        // Calculate client state settings from the chain configurations and
        // optional user overrides.
        let src_config = self.src_chain.config().map_err(|e| {
            ForeignClientError::client_create(
                self.src_chain.id(),
                "failed while querying the source chain for configuration".to_string(),
                e,
            )
        })?;
        let dst_config = self.dst_chain.config().map_err(|e| {
            ForeignClientError::client_create(
                self.dst_chain.id(),
                "failed while querying the destination chain for configuration".to_string(),
                e,
            )
        })?;
        let settings = ClientSettings::for_create_command(options, &src_config, &dst_config);
        let client_state: AnyClientState = self
            .src_chain
            .build_client_state(latest_height, settings)
            .map_err(|e| {
                ForeignClientError::client_create(
                    self.src_chain.id(),
                    "failed when building client state".to_string(),
                    e,
                )
            })?;
        let consensus_state = self
            .src_chain
            .build_consensus_state(
                client_state.latest_height(),
                latest_height,
                client_state.clone(),
            )
            .map_err(|e| {
                ForeignClientError::client_create(
                    self.src_chain.id(),
                    "failed while building client consensus state from src chain".to_string(),
                    e,
                )
            })?;
        //TODO Get acct_prefix
        let msg = MsgCreateClient::new(client_state.into(), consensus_state.into(), signer)
            .map_err(ForeignClientError::client)?;
        Ok(msg)
    }
    /// Returns the identifier of the newly created client.
    pub fn build_create_client_and_send(
        &self,
        options: CreateOptions,
    ) -> Result<IbcEventWithHeight, ForeignClientError> {
        let new_msg = self.build_create_client(options)?;
        let res = self
            .dst_chain
            .send_messages_and_wait_commit(TrackedMsgs::new_single(
                new_msg.to_any(),
                "create client",
            ))
            .map_err(|e| {
                ForeignClientError::client_create(
                    self.dst_chain.id(),
                    "failed sending message to dst chain ".to_string(),
                    e,
                )
            })?;
        assert!(!res.is_empty());
        Ok(res[0].clone())
    }
    /// Sends the client creation transaction & subsequently sets the id of this ForeignClient
    #[instrument(
        name = "foreign_client.create",
        level = "error",
        skip(self),
        fields(client = %self)
    )]
    fn create(&mut self) -> Result<(), ForeignClientError> {
        let event_with_height = self
            .build_create_client_and_send(CreateOptions::default())
            .map_err(|e| {
                error!("failed to create client: {}", e);
                e
            })?;
        self.id = extract_client_id(&event_with_height.event)?.clone();
        info!(id = %self.id, "🍭 client was created successfully");
        debug!(id = %self.id, ?event_with_height.event, "event emitted after creation");
        Ok(())
    }
    #[instrument(
        name = "foreign_client.validated_client_state",
        level = "error",
        skip(self),
        fields(client = %self)
    )]
    pub fn validated_client_state(
        &self,
    ) -> Result<(AnyClientState, Option<Duration>), ForeignClientError> {
        let (client_state, _) = {
            self.dst_chain
                .query_client_state(
                    QueryClientStateRequest {
                        client_id: self.id().clone(),
                        height: QueryHeight::Latest,
                    },
                    IncludeProof::No,
                )
                .map_err(|e| {
                    ForeignClientError::client_refresh(
                        self.id().clone(),
                        "failed querying client state on dst chain".to_string(),
                        e,
                    )
                })?
        };
        if client_state.is_frozen() {
            return Err(ForeignClientError::expired_or_frozen(
                ExpiredOrFrozen::Frozen,
                self.id().clone(),
                self.dst_chain.id(),
                "client state reports that client is frozen".into(),
            ));
        }
        match self
            .check_consensus_state_trusting_period(&client_state, &client_state.latest_height())?
        {
            ConsensusStateTrusted::NotTrusted {
                elapsed,
                network_timestamp,
                consensus_state_timestmap,
            } => {
                error!(
                    latest_height = %client_state.latest_height(),
                    network_timestmap = %network_timestamp,
                    consensus_state_timestamp = %consensus_state_timestmap,
                    elapsed = ?elapsed,
                    "client state is not valid: latest height is outside of trusting period!",
                );
                return Err(ForeignClientError::expired_or_frozen(
                    ExpiredOrFrozen::Expired,
                    self.id().clone(),
                    self.dst_chain.id(),
                    format!("time elapsed since last client update: {elapsed:?}"),
                ));
            }
            ConsensusStateTrusted::Trusted { elapsed } => Ok((client_state, Some(elapsed))),
        }
    }
    /// Verifies if the consensus state at given [`Height`]
    /// is within or outside of the client's trusting period.
    #[instrument(
        name = "foreign_client.check_consensus_state_trusting_period",
        level = "error",
        skip_all,
        fields(client = %self, %height)
    )]
    fn check_consensus_state_trusting_period(
        &self,
        client_state: &AnyClientState,
        height: &Height,
    ) -> Result<ConsensusStateTrusted, ForeignClientError> {
        // Safety check
        if client_state.chain_id() != self.src_chain.id() {
            warn!("the chain id in the client state ('{}') is inconsistent with the client's source chain id ('{}')",
            client_state.chain_id(), self.src_chain.id());
        }
        let consensus_state_timestamp = self.fetch_consensus_state(*height)?.timestamp();
        let current_src_network_time = self
            .src_chain
            .query_application_status()
            .map_err(|e| {
                ForeignClientError::client_refresh(
                    self.id().clone(),
                    "failed querying the application status of source chain".to_string(),
                    e,
                )
            })?
            .timestamp;
        // Compute the duration of time elapsed since this consensus state was installed
        let elapsed = current_src_network_time
            .duration_since(&consensus_state_timestamp)
            .unwrap_or_default();
        if client_state.expired(elapsed) {
            Ok(ConsensusStateTrusted::NotTrusted {
                elapsed,
                network_timestamp: current_src_network_time,
                consensus_state_timestmap: consensus_state_timestamp,
            })
        } else {
            Ok(ConsensusStateTrusted::Trusted { elapsed })
        }
    }
    pub fn is_frozen(&self) -> bool {
        match self.validated_client_state() {
            Ok(_) => false,
            Err(e) => e.is_frozen_error(),
        }
    }
    pub fn is_expired(&self) -> bool {
        match self.validated_client_state() {
            Ok(_) => false,
            Err(e) => e.is_expired_error(),
        }
    }
    pub fn is_expired_or_frozen(&self) -> bool {
        match self.validated_client_state() {
            Ok(_) => false,
            Err(e) => e.is_expired_or_frozen_error(),
        }
    }
    #[instrument(
        name = "foreign_client.refresh",
        level = "error",
        skip_all,
        fields(client = %self)
    )]
    pub fn refresh(&mut self) -> Result<Option<Vec<IbcEvent>>, ForeignClientError> {
        fn check_no_errors(
            ibc_events: &[IbcEvent],
            dst_chain_id: ChainId,
        ) -> Result<(), ForeignClientError> {
            // The assumption is that only one IbcEventType::ChainError will be
            // in the resulting Vec<IbcEvent> if an error occurred.
            let chain_error = ibc_events
                .iter()
                .find(|&e| e.event_type() == IbcEventType::ChainError);
            match chain_error {
                None => Ok(()),
                Some(ev) => Err(ForeignClientError::chain_error_event(
                    dst_chain_id,
                    ev.to_owned(),
                )),
            }
        }
        // If elapsed < refresh_window for the client, `try_refresh()` will
        // be successful with an empty vector.
        if let Some(events) = self.try_refresh()? {
            check_no_errors(&events, self.dst_chain().id())?;
            Ok(Some(events))
        } else {
            Ok(None)
        }
    }
    fn try_refresh(&mut self) -> Result<Option<Vec<IbcEvent>>, ForeignClientError> {
        let (client_state, elapsed) = self.validated_client_state()?;
        // The refresh_window is the maximum duration
        // we can backoff between subsequent client updates.
        let refresh_window = client_state.refresh_period();
        match (elapsed, refresh_window) {
            (None, _) | (_, None) => Ok(None),
            (Some(elapsed), Some(refresh_window)) => {
                if elapsed > refresh_window {
                    info!(?elapsed, ?refresh_window, "client needs to be refreshed");
                    self.build_latest_update_client_and_send()
                        .map_or_else(Err, |ev| Ok(Some(ev)))
                } else {
                    Ok(None)
                }
            }
        }
    }
    /// Wrapper for build_update_client_with_trusted.
    pub fn wait_and_build_update_client(
        &self,
        target_height: Height,
    ) -> Result<Vec<Any>, ForeignClientError> {
        self.wait_and_build_update_client_with_trusted(target_height, None)
    }
    /// Returns a trusted height that is lower than the target height, so
    /// that the relayer can update the client to the target height based
    /// on the returned trusted height.
    #[instrument(
        name = "foreign_client.solve_trusted_height",
        level = "error",
        skip_all,
        fields(client = %self, %target_height)
    )]
    fn solve_trusted_height(
        &self,
        target_height: Height,
        client_state: &AnyClientState,
    ) -> Result<Height, ForeignClientError> {
        let client_latest_height = client_state.latest_height();
        if client_latest_height < target_height {
            // If the latest height of the client is already lower than the
            // target height, we can simply use it.
            Ok(client_latest_height)
        } else {
            // The only time when the above is false is when for some reason,
            // the command line user wants to submit a client update at an
            // older height even when the relayer already have an up-to-date
            // client at a newer height.
            // In production, this should rarely happen unless there is another
            // relayer racing to update the client state, and that we so happen
            // to get the the latest client state that was updated between
            // the time the target height was determined, and the time
            // the client state was fetched.
            warn!(
                "resolving trusted height from the full list of consensus state \
                 heights for target height {}; this may take a while",
                target_height
            );
            // Potential optimization: cache the list of consensus heights
            // so that subsequent fetches can be fast.
            let cs_heights = self.fetch_consensus_state_heights()?;
            // Iterate through the available consesnsus heights and find one
            // that is lower than the target height.
            cs_heights
                .into_iter()
                .find(|h| h < &target_height)
                .ok_or_else(|| {
                    ForeignClientError::missing_smaller_trusted_height(
                        self.dst_chain().id(),
                        target_height,
                    )
                })
        }
    }
    /// Validate a non-zero trusted height to make sure that there is a corresponding
    /// consensus state at the given trusted height on the destination chain's client.
    #[instrument(
        name = "foreign_client.validate_trusted_height",
        level = "error",
        skip_all,
        fields(client = %self, %trusted_height)
    )]
    fn validate_trusted_height(
        &self,
        trusted_height: Height,
        client_state: &AnyClientState,
    ) -> Result<(), ForeignClientError> {
        if client_state.latest_height() != trusted_height {
            // There should be no need to validate a trusted height in production,
            // Since it is always fetched from some client state. The only use is
            // from the command line when the trusted height is manually specified.
            // We should consider skipping the validation entirely and only validate
            // it from the command line itself.
            self.fetch_consensus_state(trusted_height)?;
        }
        Ok(())
    }
    /// Given a client state and header it adds, if required, a delay such that the header will
    /// not be considered in the future when submitted in an update client:
    /// - determine the `dst_timestamp` as the time of the latest block on destination chain
    /// - return if `header.timestamp <= dst_timestamp + client_state.max_clock_drift`
    /// - wait for the destination chain to reach `dst_timestamp + 1`
    ///    Note: This is mostly to help with existing clients where the `max_clock_drift` did
    ///    not take into account the block time.
    /// - return error if header.timestamp < dst_timestamp + client_state.max_clock_drift
    ///
    /// Ref: https://github.com/informalsystems/hermes/issues/1445.
    #[instrument(
        name = "foreign_client.wait_for_header_validation_delay",
        level = "error",
        skip_all,
        fields(client = %self)
    )]
    fn wait_for_header_validation_delay(
        &self,
        client_state: &AnyClientState,
        header: &AnyHeader,
    ) -> Result<(), ForeignClientError> {
        crate::time!(
            "wait_for_header_validation_delay",
            {
                "src_chain": self.src_chain().id(),
                "dst_chain": self.dst_chain().id(),
                "header_height": header.height(),
                "header_timestamp": header.timestamp()
            }
        );
        // Get latest height and time on destination chain
        let mut status = self.dst_chain().query_application_status().map_err(|e| {
            ForeignClientError::client_update(
                self.dst_chain.id(),
                "failed querying latest status of the destination chain".to_string(),
                e,
            )
        })?;
        let ts_adjusted = (status.timestamp + client_state.max_clock_drift()).map_err(|e| {
            ForeignClientError::client_update_timing(
                self.dst_chain.id(),
                client_state.max_clock_drift(),
                "failed to adjust timestamp of destination chain with clock drift".to_string(),
                e,
            )
        })?;
        if header.timestamp().after(&ts_adjusted) {
            // Header would be considered in the future, wait for destination chain to
            // advance to the next height.
            warn!(
                "src header {} is after dst latest header {} + client state drift {},\
                 wait for next height on {}",
                header.timestamp(),
                status.timestamp,
                PrettyDuration(&client_state.max_clock_drift()),
                self.dst_chain().id()
            );
            let target_dst_height = status.height.increment();
            loop {
                thread::sleep(Duration::from_millis(300));
                status = self.dst_chain().query_application_status().map_err(|e| {
                    ForeignClientError::client_update(
                        self.dst_chain.id(),
                        "failed querying latest status of the destination chain".to_string(),
                        e,
                    )
                })?;
                if status.height >= target_dst_height {
                    break;
                }
            }
        }
        let next_ts_adjusted =
            (status.timestamp + client_state.max_clock_drift()).map_err(|e| {
                ForeignClientError::client_update_timing(
                    self.dst_chain.id(),
                    client_state.max_clock_drift(),
                    "failed to adjust timestamp of destination chain with clock drift".to_string(),
                    e,
                )
            })?;
        if header.timestamp().after(&next_ts_adjusted) {
            // The header is still in the future
            Err(ForeignClientError::header_in_the_future(
                self.src_chain.id(),
                header.height(),
                header.timestamp(),
                self.dst_chain.id(),
                status.height,
                status.timestamp,
                client_state.max_clock_drift(),
            ))
        } else {
            Ok(())
        }
    }
    /// Wait for the source chain application to reach height `target_height`
    /// before building the update client messages.
    ///
    /// Returns a vector with a message for updating the client to height
    /// `target_height`. If the client already stores a consensus state for this
    /// height, returns an empty vector.
    #[instrument(
        name = "foreign_client.wait_and_build_update_client_with_trusted",
        level = "error",
        skip_all,
        fields(client = %self, %target_height)
    )]
    pub fn wait_and_build_update_client_with_trusted(
        &self,
        target_height: Height,
        trusted_height: Option<Height>,
    ) -> Result<Vec<Any>, ForeignClientError> {
        crate::time!(
            "wait_and_build_update_client_with_trusted",
            {
                "src_chain": self.src_chain().id(),
                "dst_chain": self.dst_chain().id(),
            }
        );
        let consensus_state = self.dst_chain().query_consensus_state(
            QueryConsensusStateRequest {
                client_id: self.id().clone(),
                consensus_height: target_height,
                query_height: QueryHeight::Latest,
            },
            IncludeProof::No,
        );
        if let Ok((consensus_state, _)) = consensus_state {
            debug!("consensus state already exists at height {target_height}, skipping update");
            trace!(?consensus_state, "consensus state");
            // If the client already stores a consensus state for the target height,
            // there is no need to update the client
            telemetry!(
                client_updates_skipped,
                &self.src_chain.id(),
                &self.dst_chain.id(),
                &self.id,
                1,
            );
            return Ok(vec![]);
        }
        let src_application_latest_height = || {
            self.src_chain().query_latest_height().map_err(|e| {
                ForeignClientError::client_create(
                    self.src_chain.id(),
                    "failed fetching src network latest height with error".to_string(),
                    e,
                )
            })
        };
        {
            crate::time!(
                "wait_and_build_update_client_with_trusted_sleep",
                {
                    "src_chain": self.src_chain().id(),
                    "dst_chain": self.dst_chain().id(),
                }
            );
            // Wait for the source network to produce block(s) & reach `target_height`.
            while src_application_latest_height()? < target_height {
                thread::sleep(Duration::from_millis(100));
            }
        }
        let messages = self.build_update_client_with_trusted(target_height, trusted_height)?;
        let encoded_messages = messages.into_iter().map(Msg::to_any).collect();
        Ok(encoded_messages)
    }
    #[instrument(
        name = "foreign_client.build_update_client_with_trusted",
        level = "error",
        skip_all,
        fields(client = %self, %target_height)
    )]
    pub fn build_update_client_with_trusted(
        &self,
        target_height: Height,
        maybe_trusted_height: Option<Height>,
    ) -> Result<Vec<MsgUpdateClient>, ForeignClientError> {
        // Get the latest client state on destination.
        let (client_state, _) = self.validated_client_state()?;
        let trusted_height = match maybe_trusted_height {
            Some(trusted_height) => {
                self.validate_trusted_height(trusted_height, &client_state)?;
                trusted_height
            }
            None => self.solve_trusted_height(target_height, &client_state)?,
        };
        if trusted_height != client_state.latest_height() {
            // If we're using a trusted height that is different from the client latest height,
            // then check if the consensus state at `trusted_height` is within trusting period
            if let ConsensusStateTrusted::NotTrusted {
                elapsed,
                consensus_state_timestmap,
                network_timestamp,
            } = self.check_consensus_state_trusting_period(&client_state, &trusted_height)?
            {
                error!(
                    %trusted_height,
                    %network_timestamp,
                    %consensus_state_timestmap,
                    ?elapsed,
                    "cannot build client update message because the provided trusted height is outside of trusting period!",
                );
                return Err(ForeignClientError::consensus_state_not_trusted(
                    trusted_height,
                    elapsed,
                ));
            }
        }
        if trusted_height >= target_height {
            warn!(
                "skipping update: trusted height ({}) >= chain target height ({})",
                trusted_height, target_height
            );
            return Ok(vec![]);
        }
        let (header, support) = self
            .src_chain()
            .build_header(trusted_height, target_height, client_state.clone())
            .map_err(|e| {
                ForeignClientError::client_update(
                    self.dst_chain.id(),
                    "failed building header with error".to_string(),
                    e,
                )
            })?;
        let signer = self.dst_chain().get_signer().map_err(|e| {
            ForeignClientError::client_update(
                self.dst_chain.id(),
                "failed getting signer for dst chain".to_string(),
                e,
            )
        })?;
        self.wait_for_header_validation_delay(&client_state, &header)?;
        let mut msgs = vec![];
        for header in support {
            debug!(
                "building a MsgUpdateAnyClient for intermediate height {}",
                header.height(),
            );
            msgs.push(MsgUpdateClient {
                header: header.into(),
                client_id: self.id.clone(),
                signer: signer.clone(),
            });
        }
        debug!(
            "building a MsgUpdateAnyClient from trusted height {} to target height {}",
            trusted_height,
            header.height(),
        );
        msgs.push(MsgUpdateClient {
            header: header.into(),
            signer,
            client_id: self.id.clone(),
        });
        telemetry!(
            client_updates_submitted,
            &self.src_chain.id(),
            &self.dst_chain.id(),
            &self.id,
            msgs.len() as u64
        );
        Ok(msgs)
    }
    pub fn build_latest_update_client_and_send(&self) -> Result<Vec<IbcEvent>, ForeignClientError> {
        self.build_update_client_and_send(QueryHeight::Latest, None)
    }
    #[instrument(
        name = "foreign_client.build_update_client_and_send",
        level = "error",
        skip_all,
        fields(client = %self, %target_query_height)
    )]
    pub fn build_update_client_and_send(
        &self,
        target_query_height: QueryHeight,
        trusted_height: Option<Height>,
    ) -> Result<Vec<IbcEvent>, ForeignClientError> {
        let target_height = match target_query_height {
            QueryHeight::Latest => self.src_chain.query_latest_height().map_err(|e| {
                ForeignClientError::client_update(
                    self.src_chain.id(),
                    "failed while querying src chain ({}) for latest height".to_string(),
                    e,
                )
            })?,
            QueryHeight::Specific(height) => height,
        };
        let new_msgs =
            self.wait_and_build_update_client_with_trusted(target_height, trusted_height)?;
        if new_msgs.is_empty() {
            return Err(ForeignClientError::client_already_up_to_date(
                self.id.clone(),
                self.src_chain.id(),
                target_height,
            ));
        }
        let tm = TrackedMsgs::new_static(new_msgs, "update client");
        let events = self
            .dst_chain()
            .send_messages_and_wait_commit(tm)
            .map_err(|e| {
                ForeignClientError::client_update(
                    self.dst_chain.id(),
                    "failed sending message to dst chain".to_string(),
                    e,
                )
            })?;
        Ok(events.into_iter().map(|ev| ev.event).collect())
    }
    /// Attempts to update a client using header from the latest height of its source chain.
    #[instrument(
        name = "foreign_client.update",
        level = "error",
        skip_all,
        fields(client = %self)
    )]
    pub fn update(&self) -> Result<(), ForeignClientError> {
        let events = self.build_latest_update_client_and_send()?;
        debug!(?events, "client updated");
        Ok(())
    }
    /// Retrieves the client update event that was emitted when a consensus state at the
    /// specified height was created on chain.
    /// It is possible that the event cannot be retrieved if the information is not yet available
    /// on the full node. To handle this the query is retried a few times.
    #[instrument(
        name = "foreign_client.fetch_update_client_event",
        level = "error",
        skip_all,
        fields(client = %self, %consensus_height)
    )]
    pub fn fetch_update_client_event(
        &self,
        consensus_height: Height,
    ) -> Result<Option<UpdateClient>, ForeignClientError> {
        crate::time!(
            "fetch_update_client_event",
            {
                "src_chain": self.src_chain().id(),
                "dst_chain": self.dst_chain().id(),
            }
        );
        let mut events_with_heights = vec![];
        for i in 0..MAX_RETRIES {
            thread::sleep(Duration::from_millis(200));
            let result = self
                .dst_chain
                .query_txs(QueryTxRequest::Client(QueryClientEventRequest {
                    query_height: QueryHeight::Latest,
                    event_id: WithBlockDataType::UpdateClient,
                    client_id: self.id.clone(),
                    consensus_height,
                }))
                .map_err(|e| {
                    ForeignClientError::client_event_query(
                        self.id().clone(),
                        self.dst_chain.id(),
                        consensus_height,
                        e,
                    )
                });
            match result {
                Err(e) => {
                    error!(
                        "query_tx failed with error {}, retry {}/{}",
                        e,
                        i + 1,
                        MAX_RETRIES
                    );
                    continue;
                }
                Ok(result) => {
                    events_with_heights = result;
                    // Should break to prevent retrying uselessly.
                    break;
                }
            }
        }
        if events_with_heights.is_empty() {
            return Ok(None);
        }
        // It is possible in theory that `query_txs` returns multiple client update events for the
        // same consensus height. This could happen when multiple client updates with same header
        // were submitted to chain. However this is not what it's observed during testing.
        // Regardless, just take the event from the first update.
        let event = &events_with_heights[0].event;
        let update = downcast!(event.clone() => IbcEvent::UpdateClient).ok_or_else(|| {
            ForeignClientError::unexpected_event(
                self.id().clone(),
                self.dst_chain.id(),
                event.to_json(),
            )
        })?;
        Ok(Some(update))
    }
    /// Returns the consensus state at `height` or error if not found.
    #[instrument(
        name = "foreign_client.fetch_consensus_state",
        level = "error",
        skip_all,
        fields(client = %self, %height)
    )]
    fn fetch_consensus_state(
        &self,
        height: Height,
    ) -> Result<AnyConsensusState, ForeignClientError> {
        let (consensus_state, _) = self
            .dst_chain
            .query_consensus_state(
                QueryConsensusStateRequest {
                    client_id: self.id.clone(),
                    consensus_height: height,
                    query_height: QueryHeight::Latest,
                },
                IncludeProof::No,
            )
            .map_err(|e| {
                ForeignClientError::client_consensus_query(
                    self.id.clone(),
                    self.dst_chain.id(),
                    height,
                    e,
                )
            })?;
        Ok(consensus_state)
    }
    /// Retrieves all consensus heights for this client sorted in descending order.
    #[instrument(
        name = "foreign_client.fetch_consensus_state_heights",
        level = "error",
        skip_all,
        fields(client = %self)
    )]
    fn fetch_consensus_state_heights(&self) -> Result<Vec<Height>, ForeignClientError> {
        let mut heights = self
            .dst_chain
            .query_consensus_state_heights(QueryConsensusStateHeightsRequest {
                client_id: self.id.clone(),
                pagination: Some(PageRequest::all()),
            })
            .map_err(|e| {
                ForeignClientError::client_query(self.id().clone(), self.src_chain.id(), e)
            })?;
        // This is necessary because the results are sorted in lexicographic order instead of
        // numeric order, and we cannot therefore rely on setting the `reverse = true` in the
        // `PageRequest` setting. Since we are asking for all heights anyway, we can sort them
        // ourselves.
        //
        // For more context, see:
        // - https://github.com/informalsystems/hermes/pull/2950#issuecomment-1373733744
        // - https://github.com/cosmos/ibc-go/issues/1399
        heights.sort_by_key(|&h| core::cmp::Reverse(h));
        Ok(heights)
    }
    /// Checks for evidence of misbehaviour.
    /// The check starts with and `update_event` emitted by chain B (`dst_chain`) for a client update
    /// with a header from chain A (`src_chain`). The algorithm goes backwards through the headers
    /// until it gets to the first misbehaviour.
    ///
    /// The following cases are covered:
    /// 1 - fork:
    /// Assumes at least one consensus state before the fork point exists.
    /// Let existing consensus states on chain B be: [Sn,.., Sf, Sf-1, S0] with `Sf-1` being
    /// the most recent state before fork.
    /// Chain A is queried for a header `Hf'` at `Sf.height` and if it is different than the `Hf`
    /// in the event for the client update (the one that has generated `Sf` on chain), then the two
    /// headers are included in the evidence and submitted.
    /// Note that in this case the headers are different but have the same height.
    ///
    /// 2 - BFT time violation for unavailable header (a.k.a. Future Lunatic Attack or FLA):
    /// Some header with a height that is higher than the latest
    /// height on A has been accepted and a consensus state was created on B. Note that this implies
    /// that the timestamp of this header must be within the `clock_drift` of the client.
    /// Assume the client on B has been updated with `h2`(not present on/ produced by chain A)
    /// and it has a timestamp of `t2` that is at most `clock_drift` in the future.
    /// Then the latest header from A is fetched, let it be `h1`, with a timestamp of `t1`.
    /// If `t1 >= t2` then evidence of misbehavior is submitted to A.
    ///
    /// 3 - BFT time violation for existing headers (TODO):
    /// Ensure that consensus state times are monotonically increasing with height.
    ///
    /// Other notes:
    /// - the algorithm builds misbehavior at each consensus height, starting with the
    /// highest height assuming the previous one is trusted. It submits the first constructed
    /// evidence (the one with the highest height)
    /// - a lot of the logic here is derived from the behavior of the only implemented client
    /// (ics07-tendermint) and might not be general enough.
    ///
    #[instrument(
        name = "foreign_client.detect_misbehaviour",
        level = "error",
        skip_all,
        fields(client = %self)
    )]
    pub fn detect_misbehaviour(
        &self,
        mut update: Option<&UpdateClient>,
    ) -> Result<Option<MisbehaviourEvidence>, ForeignClientError> {
        // FIXME(romac): Why do we need this, and shouldn't we wait somewhere else up the call stack?
        thread::sleep(Duration::from_millis(200));
        // Get the latest client state on destination.
        let (client_state, _) = {
            self.dst_chain()
                .query_client_state(
                    QueryClientStateRequest {
                        client_id: self.id().clone(),
                        height: QueryHeight::Latest,
                    },
                    IncludeProof::No,
                )
                .map_err(|e| {
                    ForeignClientError::misbehaviour(
                        format!("failed querying client state on dst chain {}", self.id),
                        e,
                    )
                })?
        };
        let consensus_state_heights = if let Some(event) = update {
            vec![event.consensus_height()]
        } else {
            // Get the list of consensus state heights in descending order.
            // Note: If chain does not prune consensus states then the last consensus state is
            // the one installed by the `CreateClient` which does not include a header.
            // For chains that do support pruning, it is possible that the last consensus state
            // was installed by an `UpdateClient` and an event and header will be found.
            self.fetch_consensus_state_heights()?
        };
        trace!(
            total = %consensus_state_heights.len(),
            heights = %consensus_state_heights.iter().copied().collated().format(", "),
            "checking misbehaviour for consensus state heights",
        );
        let start_time = Instant::now();
        for target_height in consensus_state_heights {
            // Start with specified update event or the one for latest consensus height
            let update_event = if let Some(event) = update {
                // we are here only on the first iteration when called with `Some` update event
                event.clone()
            } else if let Some(event) = self.fetch_update_client_event(target_height)? {
                // we are here either on the first iteration with `None` initial update event or
                // subsequent iterations
                event
            } else {
                // we are here if the consensus state was installed on-chain when client was
                // created, therefore there will be no update client event
                break;
            };
            // Skip over heights higher than the update event one.
            // This can happen if a client update happened with a lower height than latest.
            if target_height > update_event.consensus_height() {
                continue;
            }
            // Ensure consensus height of the event is same as target height. This should be the
            // case as we either
            // - got the `update_event` from the `target_height` above, or
            // - an `update_event` was specified and we should eventually find a consensus state
            //   at that height
            // We break here in case we got a bogus event.
            if target_height < update_event.consensus_height() {
                break;
            }
            // No header in events, cannot run misbehavior.
            // May happen on chains running older SDKs (e.g., Akash)
            if update_event.header.is_none() {
                return Err(ForeignClientError::misbehaviour_exit(format!(
                    "could not extract header from update client event {:?} emitted by chain {}",
                    update_event,
                    self.dst_chain.id()
                )));
            }
            // Check for misbehaviour according to the specific source chain type.
            // In case of Tendermint client, this will also check the BFT time violation if
            // a header for the event height cannot be retrieved from the witness.
            let result = self
                .src_chain
                .check_misbehaviour(update_event.clone(), client_state.clone());
            let misbehavior = match result {
                // Misbehavior check passed.
                Ok(evidence_opt) => evidence_opt,
                // Predictable error occurred which we'll wrap.
                // This error means we cannot check for misbehavior with the provided `target_height`
                Err(e) if e.is_trusted_state_outside_trusting_period_error() => {
                    debug!(
                        target = %target_height,
                        "exhausted checking trusted consensus states for this client; no evidence found"
                    );
                    // It's safe to stop checking for misbehavior past this `target_height`.
                    break;
                }
                // Unknown error occurred in the light client `check_misbehaviour` method.
                // Propagate.
                Err(e) => {
                    return Err(ForeignClientError::misbehaviour(
                        format!(
                            "failed to check misbehaviour for {} at consensus height {}",
                            update_event.client_id(),
                            update_event.consensus_height(),
                        ),
                        e,
                    ))
                }
            };
            if misbehavior.is_some() {
                return Ok(misbehavior);
            }
            // Exit the loop if more than MAX_MISBEHAVIOUR_CHECK_DURATION was spent here.
            if start_time.elapsed() > MAX_MISBEHAVIOUR_CHECK_DURATION {
                trace!(
                    "finished misbehaviour verification after {:?}",
                    start_time.elapsed()
                );
                return Ok(None);
            }
            // Clear the update
            update = None;
            // slight backoff
            thread::sleep(Duration::from_millis(100));
        }
        trace!(
            "finished misbehaviour verification after {:?}",
            start_time.elapsed()
        );
        Ok(None)
    }
    #[instrument(
        name = "foreign_client.submit_evidence",
        level = "error",
        skip_all,
        fields(client = %self)
    )]
    fn submit_evidence(
        &self,
        evidence: MisbehaviourEvidence,
    ) -> Result<Vec<IbcEvent>, ForeignClientError> {
        let signer = self.dst_chain().get_signer().map_err(|e| {
            ForeignClientError::misbehaviour(
                format!(
                    "failed getting signer for destination chain ({})",
                    self.dst_chain.id()
                ),
                e,
            )
        })?;
        let is_ccv_consumer_chain = self
            .src_chain()
            .config()
            .map_err(|e| {
                ForeignClientError::misbehaviour(
                    format!("failed querying configuration of src chain {}", self.id),
                    e,
                )
            })?
            .ccv_consumer_chain;
        let mut msgs = vec![];
        for header in evidence.supporting_headers {
            msgs.push(
                MsgUpdateClient {
                    header: header.into(),
                    client_id: self.id.clone(),
                    signer: signer.clone(),
                }
                .to_any(),
            );
        }
        let tm_misbehaviour = match &evidence.misbehaviour {
            AnyMisbehaviour::Tendermint(tm_misbehaviour) => Some(tm_misbehaviour.clone()),
            #[cfg(test)]
            _ => None,
        }
        .ok_or_else(|| {
            ForeignClientError::misbehaviour_desc(format!(
                "underlying evidence is not a Tendermint misbehaviour: {:?}",
                evidence.misbehaviour
            ))
        })?;
        // If the misbehaving chain is a CCV consumer chain, we need to add
        // the corresponding CCV message for the provider.
        if is_ccv_consumer_chain {
            msgs.push(
                MsgSubmitIcsConsumerMisbehaviour {
                    submitter: signer.clone(),
                    misbehaviour: tm_misbehaviour,
                }
                .to_any(),
            );
        }
        msgs.push(
            MsgSubmitMisbehaviour {
                misbehaviour: evidence.misbehaviour.into(),
                client_id: self.id.clone(),
                signer,
            }
            .to_any(),
        );
        let tm = TrackedMsgs::new_static(msgs, "evidence");
        let events = self
            .dst_chain()
            .send_messages_and_wait_commit(tm)
            .map_err(|e| {
                ForeignClientError::misbehaviour(
                    format!(
                        "failed sending evidence to destination chain ({})",
                        self.dst_chain.id(),
                    ),
                    e,
                )
            })?;
        Ok(events.into_iter().map(|ev| ev.event).collect())
    }
    #[instrument(
        name = "foreign_client.detect_misbehaviour_and_submit_evidence",
        level = "error",
        skip_all,
        fields(client = %self)
    )]
    pub fn detect_misbehaviour_and_submit_evidence(
        &self,
        update_event: Option<UpdateClient>,
    ) -> MisbehaviourResults {
        // check evidence of misbehaviour for all updates or one
        let result = match self.detect_misbehaviour(update_event.as_ref()) {
            Err(e) => Err(e),
            Ok(None) => Ok(vec![]), // no evidence found
            Ok(Some(detected)) => {
                error!(
                    misbehaviour = %detected.misbehaviour,
                    "misbehaviour detected, sending evidence"
                );
                telemetry!(
                    client_misbehaviours_submitted,
                    &self.src_chain.id(),
                    &self.dst_chain.id(),
                    &self.id,
                    1
                );
                self.submit_evidence(detected)
            }
        };
        // Filter the errors if the detection was run for all consensus states.
        // Even if some states may have failed to verify, e.g. if they were expired, just
        // warn the user and continue.
        match result {
            Ok(events) => {
                if !events.is_empty() {
                    info!("evidence submission result: {}", PrettySlice(&events));
                    MisbehaviourResults::EvidenceSubmitted(events)
                } else {
                    info!("client is valid");
                    MisbehaviourResults::ValidClient
                }
            }
            Err(e) => match e.detail() {
                ForeignClientErrorDetail::MisbehaviourExit(s) => {
                    error!("misbehaviour checking is being disabled, reason: {s}");
                    MisbehaviourResults::CannotExecute
                }
                ForeignClientErrorDetail::ExpiredOrFrozen(_) => {
                    error!("cannot check misbehavior on frozen or expired client");
                    MisbehaviourResults::CannotExecute
                }
                // FIXME: This is fishy
                e if update_event.is_some() => {
                    error!("encountered unexpected error while checking misbehaviour: {e}");
                    debug!("update event: {}", update_event.unwrap());
                    MisbehaviourResults::CannotExecute
                }
                // FIXME: This is fishy
                _ => {
                    warn!("misbehaviour checking result: {e}");
                    MisbehaviourResults::ValidClient
                }
            },
        }
    }
    pub fn map_chain<DstChain2: ChainHandle, SrcChain2: ChainHandle>(
        self,
        map_dst: impl Fn(DstChain) -> DstChain2,
        map_src: impl Fn(SrcChain) -> SrcChain2,
    ) -> ForeignClient<DstChain2, SrcChain2> {
        ForeignClient {
            id: self.id,
            dst_chain: map_dst(self.dst_chain),
            src_chain: map_src(self.src_chain),
        }
    }
}
#[derive(Clone, Debug)]
pub enum MisbehaviourResults {
    CannotExecute,
    EvidenceSubmitted(Vec<IbcEvent>),
    ValidClient,
    VerificationError,
}
pub fn extract_client_id(event: &IbcEvent) -> Result<&ClientId, ForeignClientError> {
    match event {
        IbcEvent::CreateClient(ev) => Ok(ev.client_id()),
        IbcEvent::UpdateClient(ev) => Ok(ev.client_id()),
        _ => Err(ForeignClientError::missing_client_id_from_event(
            event.clone(),
        )),
    }
}