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
use std::time::Duration;

use hedera_proto::services;
use hedera_proto::services::crypto_service_client::CryptoServiceClient;

use crate::entity_id::ValidateChecksums;
use crate::execute::{
    execute,
    Execute,
};
use crate::protobuf::ToProtobuf;
use crate::query::response_header;
use crate::{
    AccountId,
    Client,
};

/// Internal "query" to ping a specific node.
///
/// This is *here* so that it can change implementation at will.
/// `PingQuery` is an `AccountBalanceQuery`-ish for now,
/// but it doesn't have to stay that way.
///
/// It's also ideally smaller/faster than any other query, by virtue of just...
pub(crate) struct PingQuery {
    node_account_id: AccountId,
}

impl PingQuery {
    pub(crate) fn new(node_account_id: AccountId) -> Self {
        Self { node_account_id }
    }

    pub(crate) async fn execute(
        &self,
        client: &Client,
        timeout: Option<Duration>,
    ) -> crate::Result<()> {
        execute(client, self, timeout).await
    }
}

impl ValidateChecksums for PingQuery {
    fn validate_checksums(&self, ledger_id: &crate::LedgerId) -> Result<(), crate::Error> {
        self.node_account_id.validate_checksums(ledger_id)
    }
}

impl Execute for PingQuery {
    type GrpcRequest = services::Query;

    type GrpcResponse = services::Response;

    type Context = ();

    type Response = ();

    fn node_account_ids(&self) -> Option<&[AccountId]> {
        Some(std::slice::from_ref(&self.node_account_id))
    }

    fn transaction_id(&self) -> Option<crate::TransactionId> {
        None
    }

    fn operator_account_id(&self) -> Option<&AccountId> {
        None
    }

    fn requires_transaction_id(&self) -> bool {
        false
    }

    fn make_request(
        &self,
        _transaction_id: Option<&crate::TransactionId>,
        node_account_id: AccountId,
    ) -> crate::Result<(Self::GrpcRequest, Self::Context)> {
        const HEADER: services::QueryHeader = services::QueryHeader {
            payment: None,
            response_type: services::ResponseType::AnswerOnly as i32,
        };

        debug_assert_eq!(node_account_id, self.node_account_id);

        let query = services::Query {
            query: Some(services::query::Query::CryptogetAccountBalance(
                services::CryptoGetAccountBalanceQuery {
                    balance_source: Some(
                        services::crypto_get_account_balance_query::BalanceSource::AccountId(
                            self.node_account_id.to_protobuf(),
                        ),
                    ),
                    header: Some(HEADER),
                },
            )),
        };

        Ok((query, ()))
    }

    fn execute(
        &self,
        channel: tonic::transport::Channel,
        request: Self::GrpcRequest,
    ) -> crate::BoxGrpcFuture<Self::GrpcResponse> {
        Box::pin(async { CryptoServiceClient::new(channel).crypto_get_balance(request).await })
    }

    fn make_response(
        &self,
        _response: Self::GrpcResponse,
        _context: Self::Context,
        _node_account_id: AccountId,
        _transaction_id: Option<&crate::TransactionId>,
    ) -> crate::Result<Self::Response> {
        Ok(())
    }

    fn make_error_pre_check(
        &self,
        status: hedera_proto::services::ResponseCodeEnum,
        _transaction_id: Option<&crate::TransactionId>,
    ) -> crate::Error {
        crate::Error::QueryNoPaymentPreCheckStatus { status }
    }

    fn response_pre_check_status(response: &Self::GrpcResponse) -> crate::Result<i32> {
        Ok(response_header(&response.response)?.node_transaction_precheck_code)
    }
}