depolymerization

wire gateway for Bitcoin/Ethereum
Log | Files | Refs | Submodules | README | LICENSE

setup.rs (3257B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 2025-2026 Taler Systems SA
      4 
      5   TALER is free software; you can redistribute it and/or modify it under the
      6   terms of the GNU Affero General Public License as published by the Free Software
      7   Foundation; either version 3, or (at your option) any later version.
      8 
      9   TALER is distributed in the hope that it will be useful, but WITHOUT ANY
     10   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
     11   A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more details.
     12 
     13   You should have received a copy of the GNU Affero General Public License along with
     14   TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
     15 */
     16 
     17 use anyhow::bail;
     18 use taler_common::config::Config;
     19 use tokio::try_join;
     20 use tracing::{info, warn};
     21 
     22 use crate::{
     23     config::WorkerCfg,
     24     db::{self, pool},
     25     payto::BtcWallet,
     26     rpc::{Rpc, RpcApi as _, Scanning},
     27 };
     28 
     29 pub async fn setup(cfg: &Config, reset: bool) -> anyhow::Result<()> {
     30     info!(target: "setup", "Check bitcoind RPC connection");
     31     let worker_cfg = WorkerCfg::parse(cfg)?;
     32     let mut rpc = Rpc::new(&worker_cfg.rpc_cfg).await?;
     33     let info = rpc.get_blockchain_info().await?;
     34     info!(target: "setup", "Running on {} chain", info.chain);
     35 
     36     let genesis_hash = rpc.get_genesis().await.unwrap();
     37 
     38     info!(target: "setup", "Check wallet");
     39     rpc.load_wallet(&worker_cfg.wallet_cfg.name).await?;
     40     if let Some(password) = &worker_cfg.wallet_cfg.password {
     41         rpc.unlock_wallet(password).await?;
     42     }
     43     let rpc = &mut rpc.wallet(&worker_cfg.wallet_cfg.name);
     44 
     45     info!(target: "setup", "Check address");
     46     let wallet: BtcWallet = cfg
     47         .section("depolymerizer-bitcoin")
     48         .parse("bitcoin wallet address", "WALLET")
     49         .require()?;
     50     let addr = wallet.0;
     51     let addr_info = rpc.addr_info(&addr).await?;
     52     if !addr_info.ismine {
     53         bail!(
     54             "Address {addr} does not belong to wallet '{}'",
     55             worker_cfg.wallet_cfg.name
     56         );
     57     } else if addr_info.iswatchonly {
     58         bail!("Address {addr} is watchonly");
     59     } else if !addr_info.solvable {
     60         bail!("Address {addr} is not solvable");
     61     }
     62 
     63     info!(target: "setup", "Setup database state");
     64     let pool = pool(cfg).await?;
     65 
     66     // Init status to true
     67     try_join!(
     68         db::init_status(&pool),
     69         db::init_sync_state(&pool, &genesis_hash, reset)
     70     )?;
     71 
     72     let info = rpc.get_blockchain_info().await?;
     73 
     74     if info.verification_progress < 1.0 {
     75         if info.initial_block_download {
     76             warn!(target: "setup", "node is initializing behind at {:.2}% of validation, you should wait for the validation to end before starting the worker", info.verification_progress);
     77         } else {
     78             warn!(target: "setup", "node is lagging behind at {:.2}% of validation, you should wait for the validation to end before starting the worker", info.verification_progress);
     79         }
     80     } else {
     81         let wallet = rpc.get_wallet_info().await?;
     82         if let Scanning::Active { progress, .. } = wallet.scanning {
     83             warn!(target: "setup", "worker wallet is scanning at {progress:.2}%")
     84         }
     85     }
     86 
     87     info!(target: "setup", "Worker setup");
     88     Ok(())
     89 }