2026-01-21 12:34:59 +01:00
|
|
|
// Copyright (C) 2024-2026 Whiterun LLC
|
2024-02-05 16:32:42 +01:00
|
|
|
//
|
|
|
|
|
// This software is licensed under the GNU Affero General Public License (AGPL), version 3.0 or later.
|
|
|
|
|
// A copy of the license can be found in the LICENSE file or at https://www.gnu.org/licenses/agpl-3.0.html
|
|
|
|
|
|
2024-10-25 15:22:44 +02:00
|
|
|
use anyhow::{bail, Result};
|
2024-09-10 22:37:44 +02:00
|
|
|
use bcmr::wellknowndowloader::WellKnownDownloader;
|
2026-01-05 15:40:01 +01:00
|
|
|
use bitcoincash::{consensus::deserialize, Block, BlockHash, Network};
|
2024-10-21 12:48:47 +02:00
|
|
|
use crc20::crc20fetcher::CRC20Fetcher;
|
2026-01-05 15:40:01 +01:00
|
|
|
use db::DB;
|
2024-05-09 11:25:27 +02:00
|
|
|
use electrum::electrum_get_tip;
|
2024-10-08 22:13:54 +02:00
|
|
|
use electrum_client_netagnostic::{Client, ElectrumApi, Param};
|
2024-05-09 11:25:27 +02:00
|
|
|
use log::{error, info, warn};
|
|
|
|
|
use rocket::{launch, routes};
|
2024-01-19 14:38:31 +01:00
|
|
|
use rocket_cors::{AllowedHeaders, AllowedOrigins};
|
2024-05-29 08:45:50 +02:00
|
|
|
use rpc::ResponseCache;
|
2026-02-02 11:30:45 +01:00
|
|
|
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
|
2023-11-29 12:25:04 +01:00
|
|
|
use std::{
|
|
|
|
|
backtrace::Backtrace,
|
2024-05-29 08:45:50 +02:00
|
|
|
collections::HashMap,
|
2026-01-05 15:40:01 +01:00
|
|
|
panic, process,
|
2024-05-09 11:25:27 +02:00
|
|
|
sync::{Arc, Mutex},
|
2026-01-05 15:40:01 +01:00
|
|
|
time::Duration,
|
2023-11-29 12:25:04 +01:00
|
|
|
};
|
2024-02-14 11:11:16 +01:00
|
|
|
use stderrlog::LogLevelNum;
|
2023-11-29 12:25:04 +01:00
|
|
|
|
2026-02-02 11:30:45 +01:00
|
|
|
/// State tracking for Initial Block Download (IBD).
|
|
|
|
|
/// Used to return 503 errors while the indexer is catching up.
|
|
|
|
|
pub struct IbdState {
|
|
|
|
|
/// Set to true once the initial sync completes
|
|
|
|
|
pub initial_sync_complete: AtomicBool,
|
|
|
|
|
/// Current block height being indexed
|
|
|
|
|
pub current_height: AtomicU64,
|
|
|
|
|
/// Target chain tip height
|
|
|
|
|
pub target_height: AtomicU64,
|
|
|
|
|
/// If true, serve requests even during IBD (for debugging)
|
|
|
|
|
pub serve_during_ibd: bool,
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-09 11:25:27 +02:00
|
|
|
use crate::bcmr::bcmrdownloader::BCMRDownloader;
|
|
|
|
|
use crate::db::cauldron::header::load_all_headers;
|
2025-09-05 13:53:10 +00:00
|
|
|
use crate::db::cauldron::tokenlist::db_utils::create_cached_token_metrics_table;
|
|
|
|
|
use crate::db::cauldron::tokenlist::metrics_cache::spawn_token_metrics_updater;
|
2026-02-10 14:48:45 +01:00
|
|
|
use crate::db::init::{initialize_databases, ReadSlots};
|
2024-05-09 11:25:27 +02:00
|
|
|
use crate::index::{index_blocks, update_mempool};
|
2023-11-29 12:25:04 +01:00
|
|
|
|
2024-10-25 15:22:44 +02:00
|
|
|
#[macro_use]
|
|
|
|
|
extern crate configure_me;
|
|
|
|
|
|
|
|
|
|
include_config!();
|
|
|
|
|
|
2024-02-14 11:11:16 +01:00
|
|
|
// The block where first cauldron contract was deployed. (Block 799870)
|
2024-05-09 11:25:27 +02:00
|
|
|
#[allow(dead_code)]
|
2024-02-14 11:11:16 +01:00
|
|
|
const RIFTEN_LABS_GENESIS_BLOCK: &str =
|
|
|
|
|
"000000000000000000ed24c811077f7268a21ecf25cb437655aaba33d8ff4997";
|
|
|
|
|
|
2024-05-09 11:25:27 +02:00
|
|
|
// Start parsing for BCMR data from this height
|
|
|
|
|
const CASHTOKEN_ACTIVATION_HEIGHT: &str =
|
|
|
|
|
"000000000000000002b678c471841c3e404ec7ae9ca9c32026fe27eb6e3a1ed1";
|
|
|
|
|
|
2026-01-05 15:40:01 +01:00
|
|
|
// Chipnet genesis
|
|
|
|
|
const CHIPNET_START_BLOCK: &str =
|
|
|
|
|
"000000001dd410c49a788668ce26751718cc797474d3152a5fc073dd44fd9f7b";
|
|
|
|
|
|
2023-11-29 12:25:04 +01:00
|
|
|
// Last indexed block height.
|
|
|
|
|
const KEY_LAST_INDEXED: &str = "last_indexed";
|
|
|
|
|
|
2024-05-09 11:25:27 +02:00
|
|
|
mod bcmr;
|
2024-04-03 21:40:33 +02:00
|
|
|
mod cashaddr;
|
2024-02-14 11:11:16 +01:00
|
|
|
mod chain;
|
2024-10-21 12:48:47 +02:00
|
|
|
mod crc20;
|
2023-11-29 12:25:04 +01:00
|
|
|
mod db;
|
2024-11-13 11:16:24 +01:00
|
|
|
mod def;
|
2024-02-14 11:11:16 +01:00
|
|
|
mod electrum;
|
2024-05-09 11:25:27 +02:00
|
|
|
mod index;
|
2024-03-04 16:40:50 +01:00
|
|
|
mod rpc;
|
2026-01-21 12:23:08 +01:00
|
|
|
mod signal;
|
2024-04-03 09:49:44 +02:00
|
|
|
mod timeutil;
|
2024-10-31 10:55:29 +00:00
|
|
|
mod utiltest;
|
2024-10-21 12:48:47 +02:00
|
|
|
mod utiltoken;
|
2024-10-21 15:13:16 +02:00
|
|
|
mod utiltx;
|
2023-11-29 12:25:04 +01:00
|
|
|
|
|
|
|
|
fn set_panic_hook() {
|
|
|
|
|
panic::set_hook(Box::new(|panic_info| {
|
2024-03-04 16:40:50 +01:00
|
|
|
error!("A thread panicked, terminating the program.");
|
|
|
|
|
if let Some(error) = panic_info.payload().downcast_ref::<anyhow::Error>() {
|
2025-07-16 09:31:14 +02:00
|
|
|
error!("Panic occurred: {error:?}");
|
2024-03-04 16:40:50 +01:00
|
|
|
error!("Anyhow backtrace:\n{}", error.backtrace());
|
|
|
|
|
let mut source = error.source();
|
|
|
|
|
while let Some(cause) = source {
|
2025-07-16 09:31:14 +02:00
|
|
|
error!("Caused by: {cause:?}");
|
2024-03-04 16:40:50 +01:00
|
|
|
source = cause.source();
|
|
|
|
|
}
|
|
|
|
|
} else if let Some(message) = panic_info.payload().downcast_ref::<&str>() {
|
2025-07-16 09:31:14 +02:00
|
|
|
error!("Panic occurred: {message}");
|
2024-10-21 12:48:47 +02:00
|
|
|
} else if let Some(message) = panic_info.payload().downcast_ref::<String>() {
|
2025-07-16 09:31:14 +02:00
|
|
|
error!("Panic occurred: {message}");
|
2024-02-14 11:11:16 +01:00
|
|
|
} else {
|
2025-07-16 09:31:14 +02:00
|
|
|
error!("Panic info: {panic_info:?}");
|
2023-11-29 12:25:04 +01:00
|
|
|
}
|
|
|
|
|
|
2024-03-04 16:40:50 +01:00
|
|
|
let backtrace = Backtrace::capture();
|
2025-07-16 09:31:14 +02:00
|
|
|
error!("Backtrace (if RUST_BACKTRACE=1):\n{backtrace}");
|
2023-11-29 12:25:04 +01:00
|
|
|
process::exit(1);
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-25 15:22:44 +02:00
|
|
|
fn start_program(
|
|
|
|
|
config: Config,
|
2026-02-02 11:30:45 +01:00
|
|
|
) -> Result<(
|
|
|
|
|
DB,
|
|
|
|
|
BCMRDownloader,
|
|
|
|
|
WellKnownDownloader,
|
|
|
|
|
CRC20Fetcher,
|
|
|
|
|
Arc<IbdState>,
|
|
|
|
|
)> {
|
2026-01-05 15:40:01 +01:00
|
|
|
// Parse network parameter
|
|
|
|
|
let network = match config.network.to_lowercase().as_str() {
|
|
|
|
|
"mainnet" => Network::Bitcoin,
|
|
|
|
|
"chipnet" => Network::Chipnet,
|
|
|
|
|
_ => bail!(
|
|
|
|
|
"Invalid network '{}'. Must be 'mainnet' or 'chipnet'",
|
|
|
|
|
config.network
|
|
|
|
|
),
|
2024-05-09 11:25:27 +02:00
|
|
|
};
|
|
|
|
|
|
2026-01-05 15:40:01 +01:00
|
|
|
info!("Using network: {:?}", network);
|
2023-11-29 12:25:04 +01:00
|
|
|
|
2026-01-05 15:40:01 +01:00
|
|
|
// Set default rostrum address based on network if not explicitly provided
|
|
|
|
|
let rostrum_addr = if config.rostrum_addr.is_empty() {
|
|
|
|
|
// User didn't provide --rostrum-addr, use network-specific default
|
|
|
|
|
match network {
|
|
|
|
|
Network::Chipnet => "127.0.0.1:64001".to_string(),
|
|
|
|
|
_ => "127.0.0.1:50001".to_string(),
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// User explicitly provided a value, use it as-is
|
|
|
|
|
config.rostrum_addr
|
|
|
|
|
};
|
2024-10-21 12:48:47 +02:00
|
|
|
|
2026-02-03 09:37:24 +01:00
|
|
|
// Normalize address: if no scheme, default to tcp:// for backwards compatibility.
|
|
|
|
|
// Supported schemes: tcp://, ssl://, ws://, wss://
|
|
|
|
|
let electrum_url = {
|
|
|
|
|
let s = rostrum_addr.trim();
|
|
|
|
|
if s.starts_with("tcp://")
|
|
|
|
|
|| s.starts_with("ssl://")
|
|
|
|
|
|| s.starts_with("ws://")
|
|
|
|
|
|| s.starts_with("wss://")
|
|
|
|
|
{
|
|
|
|
|
s.to_string()
|
|
|
|
|
} else {
|
|
|
|
|
format!("tcp://{}", s)
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2026-01-05 15:40:01 +01:00
|
|
|
// Initialize all databases
|
|
|
|
|
let network_str = match network {
|
|
|
|
|
Network::Bitcoin => "mainnet",
|
|
|
|
|
Network::Chipnet => "chipnet",
|
|
|
|
|
_ => "mainnet",
|
|
|
|
|
};
|
2026-02-10 14:48:45 +01:00
|
|
|
let db = initialize_databases(
|
|
|
|
|
network_str,
|
|
|
|
|
ReadSlots {
|
|
|
|
|
cauldron: config.cauldron_read_slots,
|
|
|
|
|
bcmr: config.bcmr_read_slots,
|
|
|
|
|
crc20: config.crc20_read_slots,
|
|
|
|
|
oracle: config.oracle_read_slots,
|
|
|
|
|
},
|
|
|
|
|
)?;
|
2025-08-15 20:44:30 +02:00
|
|
|
|
|
|
|
|
// Create a shared flag for indexing status
|
|
|
|
|
let indexing_in_progress = Arc::new(AtomicBool::new(false));
|
|
|
|
|
|
2026-02-02 11:30:45 +01:00
|
|
|
// Create IBD state for tracking initial sync progress
|
|
|
|
|
let ibd_state = Arc::new(IbdState {
|
|
|
|
|
initial_sync_complete: AtomicBool::new(false),
|
|
|
|
|
current_height: AtomicU64::new(0),
|
|
|
|
|
target_height: AtomicU64::new(0),
|
|
|
|
|
serve_during_ibd: config.serve_during_ibd,
|
|
|
|
|
});
|
|
|
|
|
|
2026-02-03 09:37:24 +01:00
|
|
|
let client = Arc::new(Mutex::new(match Client::new(&electrum_url) {
|
|
|
|
|
Ok(server) => server,
|
|
|
|
|
Err(e) => {
|
|
|
|
|
error!(
|
|
|
|
|
"Failed to connect to {}: {}. See --help for setting a different server.",
|
|
|
|
|
electrum_url, e
|
|
|
|
|
);
|
|
|
|
|
bail!(e)
|
|
|
|
|
}
|
|
|
|
|
}));
|
2024-02-14 11:11:16 +01:00
|
|
|
|
|
|
|
|
let genesis = client
|
|
|
|
|
.lock()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.raw_call("blockchain.block.get", vec![Param::U32(0)])
|
|
|
|
|
.unwrap();
|
|
|
|
|
let genesis: Block = deserialize(&hex::decode(genesis.as_str().unwrap()).unwrap()).unwrap();
|
|
|
|
|
let chain = Arc::new(Mutex::new(chain::Chain::new(genesis.header)));
|
|
|
|
|
|
2024-10-21 15:13:16 +02:00
|
|
|
// initialize insert sequence for pool history
|
2026-01-05 15:40:01 +01:00
|
|
|
db::cauldron::pool::initialize_seq(&db.cauldron_r.get().unwrap());
|
2024-10-21 15:13:16 +02:00
|
|
|
|
2024-02-14 11:11:16 +01:00
|
|
|
info!("Loading block headers...");
|
2026-01-05 15:40:01 +01:00
|
|
|
let all_headers = load_all_headers(&db.cauldron_r.get().unwrap()).unwrap();
|
2024-02-14 11:11:16 +01:00
|
|
|
info!("Initializing {} headers...", all_headers.len());
|
|
|
|
|
chain.lock().unwrap().load(all_headers).unwrap();
|
|
|
|
|
info!("Headers loaded.");
|
|
|
|
|
|
2024-05-09 11:25:27 +02:00
|
|
|
let db_cpy = db.clone();
|
2024-10-21 12:48:47 +02:00
|
|
|
|
|
|
|
|
let mut crc20fetcher = CRC20Fetcher::new();
|
2025-08-15 20:44:30 +02:00
|
|
|
crc20fetcher.start(
|
|
|
|
|
db.crc20_w.clone(),
|
2026-02-02 21:03:11 +01:00
|
|
|
db.bcmr_r.clone(),
|
2025-08-15 20:44:30 +02:00
|
|
|
client.clone(),
|
|
|
|
|
indexing_in_progress.clone(),
|
|
|
|
|
)?;
|
2023-11-29 12:25:04 +01:00
|
|
|
|
2025-05-20 16:17:32 +02:00
|
|
|
{
|
|
|
|
|
// clear oracle mempool
|
|
|
|
|
let conn = db.oracle_w.get().unwrap();
|
|
|
|
|
db::oracle::clear_mempool(&conn).unwrap();
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-15 20:44:30 +02:00
|
|
|
let indexing_in_progress_clone = indexing_in_progress.clone();
|
2026-02-02 11:30:45 +01:00
|
|
|
let ibd_state_clone = ibd_state.clone();
|
2025-08-15 20:44:30 +02:00
|
|
|
|
2025-10-03 13:32:37 +00:00
|
|
|
std::thread::spawn(move || {
|
2024-05-09 11:25:27 +02:00
|
|
|
let db = db_cpy;
|
2024-02-14 11:11:16 +01:00
|
|
|
|
2026-02-02 11:30:45 +01:00
|
|
|
// Get initial tip height for progress tracking
|
|
|
|
|
if let Ok(tip_info) = electrum_get_tip(&client.lock().unwrap()) {
|
|
|
|
|
let tip_height = chain.lock().unwrap().height();
|
|
|
|
|
ibd_state_clone
|
|
|
|
|
.target_height
|
|
|
|
|
.store(tip_height.max(tip_info.1), Ordering::Relaxed);
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-03 13:32:37 +00:00
|
|
|
// Initial full index
|
2024-05-09 11:25:27 +02:00
|
|
|
let mut tip: BlockHash = loop {
|
2026-01-21 12:23:08 +01:00
|
|
|
if signal::shutdown_requested() {
|
|
|
|
|
info!("Shutdown requested, exiting indexing thread during initial index");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-10-03 13:32:37 +00:00
|
|
|
indexing_in_progress_clone.store(true, Ordering::Relaxed);
|
2026-01-05 15:40:01 +01:00
|
|
|
break match index_blocks(
|
|
|
|
|
chain.clone(),
|
|
|
|
|
db.clone(),
|
|
|
|
|
client.clone(),
|
|
|
|
|
true,
|
|
|
|
|
Some(network),
|
2026-02-02 11:30:45 +01:00
|
|
|
Some(ibd_state_clone.clone()),
|
2026-01-05 15:40:01 +01:00
|
|
|
) {
|
2024-05-09 11:25:27 +02:00
|
|
|
Ok(tip) => tip,
|
|
|
|
|
Err(e) => {
|
|
|
|
|
if e.to_string().contains("database is locked") {
|
2025-07-16 09:31:14 +02:00
|
|
|
warn!("initial index error, trying again: {e}");
|
2024-05-09 11:25:27 +02:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
panic!("Initial index failed: {}\n {}", e, e.backtrace());
|
|
|
|
|
}
|
|
|
|
|
};
|
2024-02-14 11:11:16 +01:00
|
|
|
};
|
2025-10-03 13:32:37 +00:00
|
|
|
indexing_in_progress_clone.store(false, Ordering::Relaxed);
|
2025-08-15 20:44:30 +02:00
|
|
|
|
2026-02-02 11:30:45 +01:00
|
|
|
// Mark initial sync as complete
|
|
|
|
|
ibd_state_clone
|
|
|
|
|
.initial_sync_complete
|
|
|
|
|
.store(true, Ordering::Relaxed);
|
|
|
|
|
info!("Initial block download complete");
|
|
|
|
|
|
2025-10-03 13:32:37 +00:00
|
|
|
// Follow chain
|
2023-11-29 12:25:04 +01:00
|
|
|
loop {
|
2026-01-21 12:23:08 +01:00
|
|
|
if signal::shutdown_requested() {
|
|
|
|
|
info!("Shutdown requested, exiting indexing thread");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-09 11:25:27 +02:00
|
|
|
let new_tip = match electrum_get_tip(&client.lock().unwrap()) {
|
|
|
|
|
Ok(t) => t.0.block_hash(),
|
|
|
|
|
Err(e) => {
|
2025-10-03 13:32:37 +00:00
|
|
|
warn!("Failed to get chain tip from electrum: {e}");
|
|
|
|
|
std::thread::sleep(Duration::from_secs(5));
|
2024-05-09 11:25:27 +02:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2023-11-29 12:25:04 +01:00
|
|
|
if new_tip != tip {
|
2025-10-03 13:32:37 +00:00
|
|
|
indexing_in_progress_clone.store(true, Ordering::Relaxed);
|
2026-01-05 15:40:01 +01:00
|
|
|
tip = match index_blocks(
|
|
|
|
|
chain.clone(),
|
|
|
|
|
db.clone(),
|
|
|
|
|
client.clone(),
|
|
|
|
|
true,
|
|
|
|
|
Some(network),
|
2026-02-02 11:30:45 +01:00
|
|
|
None, // No IBD state tracking needed after initial sync
|
2026-01-05 15:40:01 +01:00
|
|
|
) {
|
2024-05-09 11:25:27 +02:00
|
|
|
Ok(t) => t,
|
|
|
|
|
Err(e) => {
|
|
|
|
|
warn!("Indexing block failed: {} {}", e, e.backtrace());
|
|
|
|
|
tip
|
|
|
|
|
}
|
2025-08-15 20:44:30 +02:00
|
|
|
};
|
2025-10-03 13:32:37 +00:00
|
|
|
indexing_in_progress_clone.store(false, Ordering::Relaxed);
|
2023-11-29 12:25:04 +01:00
|
|
|
}
|
2025-10-03 13:32:37 +00:00
|
|
|
|
|
|
|
|
// Avoid overlapping writer while indexer is on
|
|
|
|
|
if !indexing_in_progress_clone.load(Ordering::Relaxed) {
|
|
|
|
|
if let Err(e) =
|
|
|
|
|
update_mempool(db.cauldron_w.clone(), db.oracle_w.clone(), client.clone())
|
|
|
|
|
{
|
|
|
|
|
error!("Failed to update mempool: {e}");
|
|
|
|
|
}
|
2024-02-16 09:11:40 +01:00
|
|
|
}
|
2025-10-03 13:32:37 +00:00
|
|
|
std::thread::sleep(Duration::from_secs(5));
|
2023-11-29 12:25:04 +01:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2024-05-09 11:25:27 +02:00
|
|
|
let mut bcmrdownloader = BCMRDownloader::new(db.bcmr_w.clone());
|
|
|
|
|
bcmrdownloader.start()?;
|
|
|
|
|
|
2024-09-10 22:37:44 +02:00
|
|
|
let mut wellknowndownloader = WellKnownDownloader::new(db.bcmr_w.clone());
|
|
|
|
|
wellknowndownloader.start()?;
|
|
|
|
|
|
2025-10-03 13:32:37 +00:00
|
|
|
spawn_token_metrics_updater(db.clone(), indexing_in_progress.clone());
|
|
|
|
|
|
2026-02-02 11:30:45 +01:00
|
|
|
Ok((
|
|
|
|
|
db,
|
|
|
|
|
bcmrdownloader,
|
|
|
|
|
wellknowndownloader,
|
|
|
|
|
crc20fetcher,
|
|
|
|
|
ibd_state,
|
|
|
|
|
))
|
2024-02-14 11:11:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[launch]
|
|
|
|
|
fn launch() -> _ {
|
|
|
|
|
stderrlog::new()
|
2024-05-09 11:25:27 +02:00
|
|
|
.verbosity(LogLevelNum::Info)
|
2024-02-14 11:11:16 +01:00
|
|
|
.init()
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
|
set_panic_hook();
|
|
|
|
|
|
2024-10-25 15:22:44 +02:00
|
|
|
let (config, _extra) =
|
|
|
|
|
Config::including_optional_config_files(std::iter::empty::<std::ffi::OsString>())
|
|
|
|
|
.unwrap_or_exit();
|
|
|
|
|
|
2026-02-02 11:30:45 +01:00
|
|
|
let (dbpool, bcmrdownloader, wellknowndownloader, crc20fetcher, ibd_state) =
|
|
|
|
|
match start_program(config) {
|
|
|
|
|
Ok(db) => db,
|
|
|
|
|
Err(e) => {
|
|
|
|
|
let backtrace = Backtrace::capture();
|
|
|
|
|
error!("Backtrace (if RUST_BACKTRACE=1):\n{backtrace}");
|
|
|
|
|
error!("Error: {e}");
|
|
|
|
|
panic!("Failed at program startup")
|
|
|
|
|
}
|
|
|
|
|
};
|
2024-01-19 14:38:31 +01:00
|
|
|
let allowed_origins = AllowedOrigins::all();
|
|
|
|
|
|
|
|
|
|
let cors = rocket_cors::CorsOptions {
|
|
|
|
|
allowed_origins,
|
|
|
|
|
allowed_methods: vec![rocket::http::Method::Get]
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(From::from)
|
|
|
|
|
.collect(),
|
|
|
|
|
allowed_headers: AllowedHeaders::some(&["Authorization", "Accept"]),
|
|
|
|
|
allow_credentials: true,
|
|
|
|
|
..Default::default()
|
|
|
|
|
}
|
|
|
|
|
.to_cors()
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
2024-05-29 08:45:50 +02:00
|
|
|
let response_cache: ResponseCache = Arc::new(Mutex::new(HashMap::default()));
|
|
|
|
|
|
2025-09-05 13:53:10 +00:00
|
|
|
{
|
|
|
|
|
let conn = dbpool.cauldron_w.get().expect("get write conn");
|
|
|
|
|
// Ensure the table exists on both fresh and existing DBs
|
|
|
|
|
create_cached_token_metrics_table(&conn).expect("ensure cached_token_metrics exists");
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-05 14:21:50 +01:00
|
|
|
rocket::build()
|
2026-01-21 12:23:08 +01:00
|
|
|
.attach(signal::ShutdownFairing)
|
2026-02-02 11:30:45 +01:00
|
|
|
.attach(signal::IbdCheckFairing)
|
2024-03-14 12:14:13 +01:00
|
|
|
.manage(dbpool)
|
2024-05-29 08:45:50 +02:00
|
|
|
.manage(response_cache)
|
2026-02-02 11:30:45 +01:00
|
|
|
.manage(ibd_state)
|
2024-05-09 11:25:27 +02:00
|
|
|
// give rocket ownership of downloader to ensure thread isn't dropped
|
|
|
|
|
.manage(bcmrdownloader)
|
2024-09-10 22:37:44 +02:00
|
|
|
.manage(wellknowndownloader)
|
2024-10-21 12:48:47 +02:00
|
|
|
.manage(crc20fetcher)
|
2024-03-04 16:40:50 +01:00
|
|
|
.mount(
|
|
|
|
|
"/cauldron/",
|
2024-03-18 12:34:46 +01:00
|
|
|
routes![
|
2024-04-03 09:49:44 +02:00
|
|
|
rpc::tvl::deprecated_tvl,
|
|
|
|
|
rpc::tvl::valuelocked_token,
|
|
|
|
|
rpc::tvl::valuelocked_all,
|
2025-08-15 22:22:07 +02:00
|
|
|
rpc::volume::volume_all,
|
|
|
|
|
rpc::volume::volume_token,
|
2024-05-09 11:25:27 +02:00
|
|
|
rpc::tokens::list_by_volume,
|
2024-10-31 10:55:29 +00:00
|
|
|
rpc::tokens::search_by_volume,
|
2025-09-05 13:53:10 +00:00
|
|
|
rpc::tokens::search_cached,
|
|
|
|
|
rpc::tokens::list_cached,
|
|
|
|
|
rpc::tokens::list_cached_by_ids,
|
2024-04-03 11:39:49 +02:00
|
|
|
rpc::price::price_history,
|
2025-06-17 13:22:46 +00:00
|
|
|
rpc::candlesticks::price_candlesticks,
|
2024-04-03 11:39:49 +02:00
|
|
|
rpc::price::price_current,
|
2024-10-14 11:18:50 +00:00
|
|
|
rpc::price::price_at,
|
2024-04-03 21:40:33 +02:00
|
|
|
rpc::pool::list_pools_by_apy,
|
|
|
|
|
rpc::pool::list_active_pools,
|
2024-11-13 11:16:24 +01:00
|
|
|
rpc::pool::pool_history,
|
2026-01-09 15:28:36 +01:00
|
|
|
rpc::pool::pool_id_from_utxo,
|
2024-10-21 15:13:16 +02:00
|
|
|
rpc::apy::aggregate_apy,
|
2024-04-03 11:39:49 +02:00
|
|
|
rpc::contract::contract_count_token,
|
|
|
|
|
rpc::contract::contract_count_all,
|
2024-09-04 11:11:43 +02:00
|
|
|
rpc::contract::contract_volume,
|
2024-09-25 11:56:55 +02:00
|
|
|
rpc::user::unique_addresses,
|
|
|
|
|
rpc::tx::tx_latest,
|
2025-09-17 11:11:21 +02:00
|
|
|
rpc::tokens::first_pool_creation,
|
2024-03-18 12:34:46 +01:00
|
|
|
],
|
2024-03-04 16:40:50 +01:00
|
|
|
)
|
2024-09-10 22:37:44 +02:00
|
|
|
.mount(
|
|
|
|
|
"/bcmr",
|
|
|
|
|
routes![rpc::bcmr::token_bcmr, rpc::bcmr::token_bcmr_all],
|
|
|
|
|
)
|
2025-05-20 16:17:32 +02:00
|
|
|
.mount(
|
|
|
|
|
"/oracle",
|
|
|
|
|
routes![
|
|
|
|
|
rpc::oracle::oracle_get_closest,
|
2025-06-17 14:04:26 +00:00
|
|
|
rpc::oracle::oracle_get_range,
|
|
|
|
|
rpc::oracle::oracle_get_history
|
2025-05-20 16:17:32 +02:00
|
|
|
],
|
|
|
|
|
)
|
2026-02-10 15:36:04 +01:00
|
|
|
.mount("/", routes![rpc::health::health])
|
2024-01-19 14:38:31 +01:00
|
|
|
.attach(cors)
|
2023-11-29 12:25:04 +01:00
|
|
|
}
|