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

doc: clean up doc and example codes #5

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
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

## Usage

[dump-event](https://github.com/Hertarr/ordi/blob/master/src/dump-event/main.rs): use `.env` to export environments.

```rust
export btc_data_dir=
export ordi_data_dir=
Expand All @@ -14,23 +12,26 @@ export btc_rpc_host=
export btc_rpc_user=
export btc_rpc_pass=

// If index_previous_output_value is set true,
// dump-event would reindex utxos at height 767430.
// Else use rpc to get utxo like ord.
export index_previous_output_value=false
use ordi::*;

let mut ordi = ordi::Ordi::new(false)?;
let mut ordi = Ordi::new(Options::default())?;
ordi.when_inscribe(inscribe_callback);
ordi.when_transfer(transfer_callback);
ordi.start()?;
ordi.close();
```

## Example

[dump-event](https://github.com/Hertarr/ordi/blob/master/src/dump-event/main.rs): use `.env` to export environments, check `.env.example`.

You could download [snapshot](https://drive.google.com/file/d/1ngrBDyRONQUFtF8SJtM8ZsJ5CQwy1CaO/view) for utxos at height 767430. Just unzip it into `ordi_data_dir` as folder `output_value`,
And set environment `export index_previous_output_value=false`.

```
ordi_data_dir
|
---output_value
--output_value
```

## Contributing
Expand Down
31 changes: 9 additions & 22 deletions src/dump-event/main.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
use std::fs::File;
use std::path::PathBuf;

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

fn main() -> anyhow::Result<()> {
let _ = dotenv::dotenv();
let inscribe = |entry: InscribeEntry| {

let mut ordi = Ordi::new(Options::default())?;

ordi.when_inscribe(|entry| {
println!(
"inscribe {}, {} at {}:{}.",
entry.id, &entry.inscription_id, &entry.txid, entry.vout
);
};
});

let transfer = |entry: TransferEntry| {
ordi.when_transfer(|entry| {
println!(
"transfer {} from {}:{} to {}:{}:{}.",
entry.inscription_id,
Expand All @@ -24,22 +22,11 @@ fn main() -> anyhow::Result<()> {
entry.vout,
entry.offset
);
};

let ordi_data_dir = PathBuf::from(std::env::var("ordi_data_dir")?.as_str());
CombinedLogger::init(vec![WriteLogger::new(
LevelFilter::Info,
Config::default(),
File::create(ordi_data_dir.join("debug.log")).unwrap(),
)])?;

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

// If index_previous_output_value is set true,
// dump-event would reindex utxos at height 767430.
// Else use rpc to get utxo like ord.
// dump-event would reindex utxos until height 767430.
// else use rpc to get utxo like ord.
if std::env::var("index_previous_output_value")? == "true" {
ordi.index_output_value()?;
}
Expand Down
21 changes: 13 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use std::env::VarError;
use std::{path::PathBuf, thread, time::Duration};
use std::{fs, path::PathBuf, thread, time::Duration};

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

use crate::bitcoin::index::IndexError;
Expand All @@ -30,15 +29,17 @@ const ORDI_OUTPUT_TO_INSCRIPTION: &str = "output_inscription";
#[derive(Error, Debug)]
pub enum OrdiError {
#[error("Var error: `{0}`")]
VarError(#[from] VarError),
VarError(#[from] std::env::VarError),
#[error("Open leveldb error: `{0}`")]
OpenLevelDBError(#[from] Status),
OpenLevelDBError(#[from] rusty_leveldb::Status),
#[error("Bitcoin rpc errpr: `{0}`")]
BitcoinRpcError(#[from] Error),
BitcoinRpcError(#[from] bitcoincore_rpc::Error),
#[error("Index error: `{0}`")]
IndexError(#[from] IndexError),
#[error("BlockUpdater error: `{0}`")]
BlockUpdaterError(#[from] BlockUpdaterError),
#[error("Create Ordi data directory error: `{0}`")]
CreateOrdiDataDirError(#[from] std::io::Error),
}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -76,12 +77,16 @@ pub struct Ordi {

impl Ordi {
pub fn new(options: Options) -> Result<Ordi, OrdiError> {
let ordi_data_dir = PathBuf::from(options.ordi_data_dir);
if !ordi_data_dir.exists() {
fs::create_dir(ordi_data_dir.as_path())?;
}

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),
Expand Down
Loading