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
// Copyright (C) 2019-2024 Aleo Systems Inc.
// This file is part of the Leo library.

// The Leo library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// The Leo library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.

use super::*;
use snarkvm::prelude::{CanaryV0, MainnetV0, TestnetV0};

mod block;
use block::Block;

pub mod program;
pub use program::Program;

mod state_root;
use state_root::StateRoot;

mod committee;
use committee::Committee;

mod mempool;
use mempool::Mempool;

mod peers;
use peers::Peers;

mod transaction;
use transaction::Transaction;

mod utils;
use utils::*;

use leo_errors::UtilError;
use leo_retriever::{NetworkName, fetch_from_network, verify_valid_program};

///  Query live data from the Aleo network.
#[derive(Parser, Debug)]
pub struct Query {
    #[clap(short, long, global = true, help = "Endpoint to retrieve network state from. Defaults to entry in `.env`.")]
    pub endpoint: Option<String>,
    #[clap(short, long, global = true, help = "Network to use. Defaults to entry in `.env`.")]
    pub(crate) network: Option<String>,
    #[clap(subcommand)]
    pub command: QueryCommands,
}

impl Command for Query {
    type Input = ();
    type Output = String;

    fn log_span(&self) -> Span {
        tracing::span!(tracing::Level::INFO, "Leo")
    }

    fn prelude(&self, _context: Context) -> Result<Self::Input> {
        Ok(())
    }

    fn apply(self, context: Context, _: Self::Input) -> Result<Self::Output> {
        // Parse the network.
        let network = NetworkName::try_from(context.get_network(&self.network)?)?;
        let endpoint = context.get_endpoint(&self.endpoint)?;
        match network {
            NetworkName::MainnetV0 => handle_query::<MainnetV0>(self, context, &network.to_string(), &endpoint),
            NetworkName::TestnetV0 => handle_query::<TestnetV0>(self, context, &network.to_string(), &endpoint),
            NetworkName::CanaryV0 => handle_query::<CanaryV0>(self, context, &network.to_string(), &endpoint),
        }
    }
}

// A helper function to handle the `query` command.
fn handle_query<N: Network>(
    query: Query,
    context: Context,
    network: &str,
    endpoint: &str,
) -> Result<<Query as Command>::Output> {
    let recursive = context.recursive;
    let (program, output) = match query.command {
        QueryCommands::Block { command } => (None, command.apply(context, ())?),
        QueryCommands::Transaction { command } => (None, command.apply(context, ())?),
        QueryCommands::Program { command } => {
            // Check if querying for program source code.
            let program =
                if command.mappings || command.mapping_value.is_some() { None } else { Some(command.name.clone()) };
            (program, command.apply(context, ())?)
        }
        QueryCommands::Stateroot { command } => (None, command.apply(context, ())?),
        QueryCommands::Committee { command } => (None, command.apply(context, ())?),
        QueryCommands::Mempool { command } => {
            if endpoint == "https://api.explorer.provable.com/v1" {
                tracing::warn!(
                    "⚠️  `leo query mempool` is only valid when using a custom endpoint. Specify one using `--endpoint`."
                );
            }
            (None, command.apply(context, ())?)
        }
        QueryCommands::Peers { command } => {
            if endpoint == "https://api.explorer.provable.com/v1" {
                tracing::warn!(
                    "⚠️  `leo query peers` is only valid when using a custom endpoint. Specify one using `--endpoint`."
                );
            }
            (None, command.apply(context, ())?)
        }
    };

    // Make GET request to retrieve on-chain state.
    let url = format!("{}/{}/{output}", endpoint, network);
    let result = fetch_from_network(&url)?;
    if !recursive {
        tracing::info!("✅ Successfully retrieved data from '{url}'.\n");
        println!("{}\n", result);
    }

    // Verify that the source file parses into a valid Aleo program.
    if let Some(name) = program {
        verify_valid_program::<N>(&name, &result)?;
    }

    Ok(result)
}

#[derive(Parser, Debug)]
pub enum QueryCommands {
    #[clap(about = "Query block information")]
    Block {
        #[clap(flatten)]
        command: Block,
    },
    #[clap(about = "Query transaction information")]
    Transaction {
        #[clap(flatten)]
        command: Transaction,
    },
    #[clap(about = "Query program source code and live mapping values")]
    Program {
        #[clap(flatten)]
        command: Program,
    },
    #[clap(about = "Query the latest stateroot")]
    Stateroot {
        #[clap(flatten)]
        command: StateRoot,
    },
    #[clap(about = "Query the current committee")]
    Committee {
        #[clap(flatten)]
        command: Committee,
    },
    #[clap(about = "Query transactions and transmissions from the memory pool")]
    Mempool {
        #[clap(flatten)]
        command: Mempool,
    },
    #[clap(about = "Query peer information")]
    Peers {
        #[clap(flatten)]
        command: Peers,
    },
}