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
// Copyright 2019-2024 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT

use crate::auth::generate_priv_key;
use crate::chain::ChainStore;
use crate::chain_sync::{SyncConfig, SyncStage};
use crate::cli_shared::snapshot::TrustedVendor;
use crate::daemon::db_util::{download_to, populate_eth_mappings};
use crate::db::{car::ManyCar, MemoryDB};
use crate::genesis::{get_network_name_from_genesis, read_genesis_header};
use crate::key_management::{KeyStore, KeyStoreConfig};
use crate::message_pool::{MessagePool, MpoolRpcProvider};
use crate::networks::{ChainConfig, NetworkChain};
use crate::rpc::eth::filter::EthEventHandler;
use crate::rpc::{start_rpc, RPCState};
use crate::shim::address::{CurrentNetwork, Network};
use crate::state_manager::StateManager;
use crate::JWT_IDENTIFIER;
use anyhow::Context as _;
use fvm_ipld_blockstore::Blockstore;
use std::{
    net::{IpAddr, Ipv4Addr, SocketAddr},
    path::PathBuf,
    sync::Arc,
};
use tokio::{
    signal::{
        ctrl_c,
        unix::{signal, SignalKind},
    },
    sync::{mpsc, RwLock},
    task::JoinSet,
};
use tracing::{info, warn};

pub async fn start_offline_server(
    snapshot_files: Vec<PathBuf>,
    chain: NetworkChain,
    rpc_port: u16,
    auto_download_snapshot: bool,
    height: i64,
    genesis: Option<PathBuf>,
    save_jwt_token: Option<PathBuf>,
) -> anyhow::Result<()> {
    info!("Configuring Offline RPC Server");

    let db = Arc::new(ManyCar::new(MemoryDB::default()));

    let snapshot_files = handle_snapshots(
        snapshot_files,
        &chain,
        auto_download_snapshot,
        genesis.clone(),
    )
    .await?;

    db.read_only_files(snapshot_files.iter().cloned())?;
    let chain_config = Arc::new(handle_chain_config(&chain)?);
    let sync_config = Arc::new(SyncConfig::default());
    let genesis_header = read_genesis_header(
        genesis.as_deref(),
        chain_config.genesis_bytes(&db).await?.as_deref(),
        &db,
    )
    .await?;
    let chain_store = Arc::new(ChainStore::new(
        db.clone(),
        db.clone(),
        db.clone(),
        chain_config.clone(),
        genesis_header.clone(),
    )?);
    let state_manager = Arc::new(StateManager::new(
        chain_store.clone(),
        chain_config,
        sync_config,
    )?);
    let head_ts = Arc::new(db.heaviest_tipset()?);

    state_manager
        .chain_store()
        .set_heaviest_tipset(head_ts.clone())?;

    populate_eth_mappings(&state_manager, &head_ts)?;

    let (network_send, _) = flume::bounded(5);
    let (tipset_send, _) = flume::bounded(5);
    let network_name = get_network_name_from_genesis(&genesis_header, &state_manager)?;
    let message_pool = MessagePool::new(
        MpoolRpcProvider::new(chain_store.publisher().clone(), state_manager.clone()),
        network_name.clone(),
        network_send.clone(),
        Default::default(),
        state_manager.chain_config().clone(),
        &mut JoinSet::new(),
    )?;

    // Validate tipsets since the {height} EPOCH when `height >= 0`,
    // or valiadte the last {-height} EPOCH(s) when `height < 0`
    let n_ts_to_validate = if height > 0 {
        (head_ts.epoch() - height).max(0)
    } else {
        -height
    } as usize;
    if n_ts_to_validate > 0 {
        state_manager.validate_tipsets(head_ts.chain_arc(&db).take(n_ts_to_validate))?;
    }

    let (shutdown, shutdown_recv) = mpsc::channel(1);

    let mut keystore = KeyStore::new(KeyStoreConfig::Memory)?;
    keystore.put(JWT_IDENTIFIER, generate_priv_key())?;
    let ki = keystore.get(JWT_IDENTIFIER)?;
    // Lotus admin tokens do not expire but Forest requires all JWT tokens to
    // have an expiration date. So we set the expiration date to 100 years in
    // the future to match user-visible behavior of Lotus.
    let token_exp = chrono::Duration::days(365 * 100);
    let token = crate::auth::create_token(
        crate::auth::ADMIN.iter().map(ToString::to_string).collect(),
        ki.private_key(),
        token_exp,
    )?;
    info!("Admin token: {token}");
    if let Some(path) = save_jwt_token {
        std::fs::write(path, token)?;
    }

    let rpc_state = RPCState {
        state_manager,
        keystore: Arc::new(RwLock::new(keystore)),
        mpool: Arc::new(message_pool),
        bad_blocks: Default::default(),
        sync_state: Arc::new(parking_lot::RwLock::new(Default::default())),
        eth_event_handler: Arc::new(EthEventHandler::new()),
        network_send,
        network_name,
        start_time: chrono::Utc::now(),
        shutdown,
        tipset_send,
    };
    rpc_state.sync_state.write().set_stage(SyncStage::Idle);
    start_offline_rpc(rpc_state, rpc_port, shutdown_recv).await?;

    Ok(())
}

async fn start_offline_rpc<DB>(
    state: RPCState<DB>,
    rpc_port: u16,
    mut shutdown_recv: mpsc::Receiver<()>,
) -> anyhow::Result<()>
where
    DB: Blockstore + Send + Sync + 'static,
{
    info!("Starting offline RPC Server");
    let rpc_address = SocketAddr::new(IpAddr::V4(Ipv4Addr::UNSPECIFIED), rpc_port);
    let mut terminate = signal(SignalKind::terminate())?;

    let result = tokio::select! {
        ret = start_rpc(state, rpc_address) => ret,
        _ = ctrl_c() => {
            info!("Keyboard interrupt.");
            Ok(())
        },
        _ = terminate.recv() => {
            info!("Received SIGTERM.");
            Ok(())
        },
        _ = shutdown_recv.recv() => {
            info!("Client requested a shutdown.");
            Ok(())
        },
    };
    crate::utils::io::terminal_cleanup();
    result
}

async fn handle_snapshots(
    snapshot_files: Vec<PathBuf>,
    chain: &NetworkChain,
    auto_download_snapshot: bool,
    genesis: Option<PathBuf>,
) -> anyhow::Result<Vec<PathBuf>> {
    if !snapshot_files.is_empty() {
        return Ok(snapshot_files);
    }

    if snapshot_files.is_empty() && chain.is_devnet() {
        return Ok(vec![genesis.context("missing genesis file")?]);
    }

    let (snapshot_url, num_bytes, path) =
        crate::cli_shared::snapshot::peek(TrustedVendor::default(), chain)
            .await
            .context("couldn't get snapshot size")?;
    if !auto_download_snapshot {
        warn!("Automatic snapshot download is disabled.");
        let message = format!(
            "Fetch a {} snapshot to the current directory? (denying will exit the program). ",
            indicatif::HumanBytes(num_bytes)
        );
        let have_permission =
            dialoguer::Confirm::with_theme(&dialoguer::theme::ColorfulTheme::default())
                .with_prompt(message)
                .default(false)
                .interact()
                .unwrap_or(false);
        if !have_permission {
            anyhow::bail!("No snapshot provided, exiting offline RPC setup.");
        }
    }
    info!(
        "Downloading latest snapshot for {} size {}",
        chain,
        indicatif::HumanBytes(num_bytes)
    );
    let downloaded_snapshot_path = std::env::current_dir()?.join(path);
    download_to(&snapshot_url, &downloaded_snapshot_path).await?;
    info!("Snapshot downloaded");
    Ok(vec![downloaded_snapshot_path])
}

fn handle_chain_config(chain: &NetworkChain) -> anyhow::Result<ChainConfig> {
    info!("Using chain config for {chain}");
    let chain_config = ChainConfig::from_chain(chain);
    if chain_config.is_testnet() {
        CurrentNetwork::set_global(Network::Testnet);
    }
    Ok(chain_config)
}