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
use crate::{config::IndexerConfig, defaults};
use anyhow::Result;
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use std::{
    env,
    fs::canonicalize,
    future::Future,
    net::{SocketAddr, ToSocketAddrs},
    path::Path,
    str::FromStr,
};
use tokio::time::{sleep, Duration};
use tracing::{debug, info, warn};
use tracing_subscriber::filter::EnvFilter;

const RUST_LOG: &str = "RUST_LOG";
const HUMAN_LOGGING: &str = "HUMAN_LOGGING";

const ROOT_DIRECTORY_NAME: &str = "fuel-indexer";

/// Serialize a generic byte array reference.
pub fn serialize(obj: &impl Serialize) -> Vec<u8> {
    bincode::serialize(obj).expect("Serialize failed")
}

/// Deserialize a generic byte array reference.
pub fn deserialize<'a, T: Deserialize<'a>>(bytes: &'a [u8]) -> Result<T, String> {
    match bincode::deserialize(bytes) {
        Ok(obj) => Ok(obj),
        Err(e) => Err(format!("Bincode serde error {e:?}")),
    }
}

// Trybuild test indexers reference the manifest using an implicit $CARGO_MANIFEST_DIR, since these
// tests are a part of cargo's workspace.
//
// However, the `#[indexer]` macro is meant to use either relative paths or absolute paths, not
// paths that require a $CARGO_MANIFEST_DIR prefix.
//
// Given this, we have to prefix `#[indexer]` manifest paths in this cargo workspace with some
// additional path info, so that `#[indexer]` macros _within this project's workspace_ can load
// manifests correctly.
//
// If we can successfully find the local project root, then we're in the repository,
// and should prefix all relative manifest paths with the absolute path to the project root.
// If we can't find the project root, then it's assumed we're not in a local repository, thus no prefix.
//
// This is specifically required for the trybuild test suite and the examples.
pub fn workspace_manifest_prefix() -> Option<String> {
    let curr_filepath = canonicalize(file!()).unwrap();
    let mut curr_dir = Path::new(&curr_filepath);

    // 4 = -> src (1) -> fuel-indexer-lib (2) -> packages -> (3) -> fuel-indexer (4)
    let mut depth = 4;
    while depth > 0 {
        match curr_dir.parent() {
            Some(p) => {
                curr_dir = p;
                depth -= 1;
            }
            None => {
                return None;
            }
        }
    }

    if !curr_dir.is_dir() || curr_dir.file_name().unwrap() != ROOT_DIRECTORY_NAME {
        return None;
    }

    let root_dir = curr_dir.as_os_str().to_str().unwrap().to_string();

    Some(root_dir)
}

/// Request to reload the specified indexer executor using this indexer's current
/// assets in the database.
///
/// Sent from API server to indexer service.
#[derive(Debug)]
pub struct ReloadRequest {
    /// The namespace of the indexer being removed.
    pub namespace: String,
    /// The identifier of the indexer being removed.
    pub identifier: String,
}

/// Request to remove the specified indexer executor from the indexer service.
///
/// Sent from API server to indexer service.
#[derive(Debug)]
pub struct StopRequest {
    /// The namespace of the indexer being removed.
    pub namespace: String,
    /// The identifier of the indexer being removed.
    pub identifier: String,
}

/// A general request sent from the API server to the indexer service.
#[derive(Debug)]
pub enum ServiceRequest {
    Reload(ReloadRequest),
    Stop(StopRequest),
}

/// Returns the lower hex representation of a [`sha2::SHA256`] digest of the provided input.
pub fn sha256_digest<T: AsRef<[u8]>>(b: &T) -> String {
    let mut hasher = Sha256::new();
    hasher.update(b);
    format!("{:x}", hasher.finalize())
}

/// Trim the leading '$' or '${' and trailing '}' from an environment variable.
pub fn trim_opt_env_key(key: &str) -> &str {
    // Abmiguous key: $FOO, non-ambiguous key: ${FOO}
    let not_ambiguous = key.starts_with("${");
    match not_ambiguous {
        false => &key[1..],
        true => &key[2..key.len() - 1],
    }
}

/// Determine whether a given key is an environment variable.
pub fn is_opt_env_var(k: &str) -> bool {
    k.starts_with('$') || (k.starts_with("${") && k.ends_with('}'))
}

/// Derive the [`std::net::SocketAddr`] from a given host and port, falling back
/// to a DNS lookup using [`std::net::ToSocketAddrs`] if the host is not a valid IP address.
pub fn derive_socket_addr(host: &str, port: &str) -> SocketAddr {
    let host = format!("{host}:{port}");
    match SocketAddr::from_str(&host) {
        Ok(v) => v,
        Err(e) => {
            debug!("Failed to parse '{host}': {e}. Retrying...");
            let mut addrs: Vec<_> = host
                .to_socket_addrs()
                .unwrap_or_else(|e| panic!("Unable to resolve domain: {e}"))
                .collect();

            let addr = addrs.pop().expect("Could not derive SocketAddr from '{}'");

            info!("Parsed SocketAddr '{addr:?}' from '{host}'");

            addr
        }
    }
}

/// Attempt to connect to a database, with retries.
///
/// This function takes a closure with a database connection
/// function as an argument; said function should return a future that
/// resolves to a final value of type `Result<T, sqlx::Error>`.
pub async fn attempt_database_connection<F, Fut, T, U>(mut fut: F) -> T
where
    F: FnMut() -> Fut,
    Fut: Future<Output = Result<T, U>>,
    U: std::error::Error,
{
    let mut remaining_retries = defaults::MAX_DATABASE_CONNECTION_ATTEMPTS;
    let mut delay = defaults::INITIAL_RETRY_DELAY_SECS;
    loop {
        match fut().await {
            Ok(t) => break t,
            Err(_) => {
                if remaining_retries > 0 {
                    warn!(
                        "Could not connect to database. Retrying in {delay} seconds...",
                    );
                    remaining_retries -= 1;
                    sleep(Duration::from_secs(delay)).await;
                    delay *= 2;
                } else {
                    panic!("Retry attempts exceeded. Could not connect to database!")
                }
            }
        }
    }
}

/// Denotes the status of a service for the service health check.
#[derive(Debug, Serialize, Deserialize)]
pub enum ServiceStatus {
    /// The service is healthy.
    OK,

    /// The service is not healthy.
    NotOk,
}

impl From<FuelClientHealthResponse> for ServiceStatus {
    fn from(r: FuelClientHealthResponse) -> Self {
        match r.up {
            true => ServiceStatus::OK,
            _ => ServiceStatus::NotOk,
        }
    }
}

/// Response from the Fuel client health check.
#[derive(Serialize, Deserialize, Default, Debug)]
pub struct FuelClientHealthResponse {
    /// `true` if the client is available, `false` otherwise.
    up: bool,
}

/// Initialize the logging context for the indexer service.
pub async fn init_logging(config: &IndexerConfig) -> anyhow::Result<()> {
    let level = env::var_os(RUST_LOG)
        .map(|x| x.into_string().unwrap())
        .unwrap_or("info".to_string());

    // We manually suppress some of the more verbose crate logging.
    if !config.verbose {
        std::env::set_var(
            RUST_LOG,
            format!("{level},wasmer_compiler_cranelift=warn,regalloc=warn,cranelift_codegen=warn"),
        );
    }

    let filter = match env::var_os(RUST_LOG) {
        Some(_) => {
            EnvFilter::try_from_default_env().expect("Invalid `RUST_LOG` provided")
        }
        None => EnvFilter::new("info"),
    };

    let human_logging = env::var_os(HUMAN_LOGGING)
        .map(|s| {
            bool::from_str(s.to_str().unwrap())
                .expect("Expected `true` or `false` to be provided for `HUMAN_LOGGING`")
        })
        .unwrap_or(true);

    let sub = tracing_subscriber::fmt::Subscriber::builder()
        .with_writer(std::io::stderr)
        .with_env_filter(filter);

    if human_logging {
        sub.with_ansi(true)
            .with_level(true)
            .with_line_number(true)
            .init();
    } else {
        sub.with_ansi(false)
            .with_level(true)
            .with_line_number(true)
            .json()
            .init();
    }
    Ok(())
}

/// Format a SQL query for logging.
pub fn format_sql_query(s: String) -> String {
    s.replace('\n', " ")
}