Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use options instead of env #4

Merged
merged 1 commit into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/dump-event/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::fs::File;
use std::path::PathBuf;

use ordi::block::{InscribeEntry, TransferEntry};
use ordi::*;
use simplelog::*;

fn main() -> anyhow::Result<()> {
Expand Down Expand Up @@ -32,7 +33,7 @@ fn main() -> anyhow::Result<()> {
File::create(ordi_data_dir.join("debug.log")).unwrap(),
)])?;

let mut ordi = ordi::Ordi::new(false)?;
let mut ordi = Ordi::new(Options::default())?;
ordi.when_inscribe(inscribe);
ordi.when_transfer(transfer);

Expand Down
68 changes: 45 additions & 23 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{path::PathBuf, thread, time::Duration};

use bitcoincore_rpc::{Client, Error, RpcApi};
use log::info;
use rusty_leveldb::{Options, Status, WriteBatch, DB};
use rusty_leveldb::{Status, WriteBatch, DB};
use thiserror::Error;

use crate::bitcoin::index::IndexError;
Expand Down Expand Up @@ -41,6 +41,27 @@ pub enum OrdiError {
BlockUpdaterError(#[from] BlockUpdaterError),
}

#[derive(Debug, Clone)]
pub struct Options {
pub btc_data_dir: String,
pub ordi_data_dir: String,
pub btc_rpc_host: String,
pub btc_rpc_user: String,
pub btc_rpc_pass: String,
}

impl Default for Options {
fn default() -> Options {
Options {
btc_data_dir: std::env::var("btc_data_dir").unwrap_or_default(),
ordi_data_dir: std::env::var("ordi_data_dir").unwrap_or_default(),
btc_rpc_host: std::env::var("btc_rpc_host").unwrap_or_default(),
btc_rpc_user: std::env::var("btc_rpc_user").unwrap_or_default(),
btc_rpc_pass: std::env::var("btc_rpc_pass").unwrap_or_default(),
}
}
}

pub struct Ordi {
pub btc_rpc_client: Client,
pub status: DB,
Expand All @@ -54,34 +75,35 @@ pub struct Ordi {
}

impl Ordi {
pub fn new(in_memory: bool) -> Result<Ordi, OrdiError> {
let mut options = if in_memory {
rusty_leveldb::in_memory()
} else {
Options::default()
};
options.max_file_size = 2 << 25;

let base_path = PathBuf::from(std::env::var("ordi_data_dir")?.as_str());
let status = DB::open(base_path.join(ORDI_STATUS), options.clone())?;
let output_value = DB::open(base_path.join(ORDI_OUTPUT_VALUE), options.clone())?;
let id_inscription = DB::open(base_path.join(ORDI_ID_TO_INSCRIPTION), options.clone())?;
let inscription_output =
DB::open(base_path.join(ORDI_INSCRIPTION_TO_OUTPUT), options.clone())?;
pub fn new(options: Options) -> Result<Ordi, OrdiError> {
let index = Index::new(PathBuf::from(options.btc_data_dir))?;

let mut leveldb_options = rusty_leveldb::Options::default();
leveldb_options.max_file_size = 2 << 25;

let ordi_data_dir = PathBuf::from(options.ordi_data_dir);
let status = DB::open(ordi_data_dir.join(ORDI_STATUS), leveldb_options.clone())?;
let output_value = DB::open(
ordi_data_dir.join(ORDI_OUTPUT_VALUE),
leveldb_options.clone(),
)?;
let id_inscription = DB::open(
ordi_data_dir.join(ORDI_ID_TO_INSCRIPTION),
leveldb_options.clone(),
)?;
let inscription_output = DB::open(
ordi_data_dir.join(ORDI_INSCRIPTION_TO_OUTPUT),
leveldb_options.clone(),
)?;
let output_inscription = DB::open(
base_path.join(ORDI_OUTPUT_TO_INSCRIPTION),
ordi_data_dir.join(ORDI_OUTPUT_TO_INSCRIPTION),
rusty_leveldb::in_memory(),
)?;

let btc_data_dir = std::env::var("btc_data_dir")?;
let btc_rpc_client = Client::new(
std::env::var("btc_rpc_host")?.as_str(),
bitcoincore_rpc::Auth::UserPass(
std::env::var("btc_rpc_user")?,
std::env::var("btc_rpc_pass")?,
),
options.btc_rpc_host.as_str(),
bitcoincore_rpc::Auth::UserPass(options.btc_rpc_user, options.btc_rpc_pass),
)?;
let index = Index::new(PathBuf::from(btc_data_dir))?;

Ok(Ordi {
btc_rpc_client,
Expand Down
Loading