Skip to content
Open
Show file tree
Hide file tree
Changes from 13 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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ rust-bitvmx-storage-backend = { git = "https://github.com/FairgateLabs/rust-bitv
bitvmx-settings = { git = "https://github.com/FairgateLabs/rust-bitvmx-settings.git", tag = "v0.5.1" }
bitvmx-bitcoin-rpc = { git = "https://github.com/FairgateLabs/rust-bitvmx-bitcoin-rpc.git", tag = "v0.5.1" }
bitcoind = { git = "https://github.com/FairgateLabs/rust-bitcoind.git", tag = "v0.5.1" }
bitcoincore-rpc = "0.19"
bitcoincore-rpc-json = "0.19.0"

[dev-dependencies]
Expand Down
51 changes: 38 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,13 @@ The `IndexerApi` trait provides several methods to interact with the Bitcoin Ind

- **get_blockchain_best_height**: Retrieves the current best block height from the Bitcoin blockchain.

- **get_height_to_sync**: Determines the next block height that needs to be synchronized.
- **get_block_by_height**: Retrieves a block by its height.

- **get_tx**: Retrieves transaction information for a given transaction ID.
- **get_block_by_hash**: Retrieves a block by its hash.

- **get_transaction**: Retrieves transaction information for a given transaction ID. Returns a `TransactionInfo` with the transaction status. If the transaction is not found, returns `TransactionInfo` with `TransactionBlockchainStatus::NotFound`.

- **get_estimated_fee_rate**: Retrieves the estimated fee rate from the most recently indexed block.

## Usage
```rust
Expand Down Expand Up @@ -72,43 +76,64 @@ The `IndexerApi` trait provides several methods to interact with the Bitcoin Ind
let blockchain_best_height = indexer.get_blockchain_best_height()?;
println!("Blockchain best height: {}", blockchain_best_height);

// Determine the next block height to sync
let height_to_sync = indexer.get_height_to_sync()?;
println!("Next block height to sync: {}", height_to_sync);

// Example transaction ID (replace with a real one for actual use)
let tx_id = "some_tx_id";
if let Some(tx_info) = indexer.get_tx(&tx_id.parse()?)? {
println!("Transaction info: {:?}", tx_info);
let tx_info = indexer.get_transaction(&tx_id.parse()?)?;
println!("Transaction status: {:?}", tx_info.status);
match tx_info.status {
TransactionBlockchainStatus::InMempool => {
println!("Transaction is in mempool");
}
TransactionBlockchainStatus::NotFound => {
println!("Transaction not found");
}
TransactionBlockchainStatus::Orphan => {
println!("Transaction is orphaned");
}
TransactionBlockchainStatus::Confirmed => {
println!("Transaction is confirmed with {} confirmations",
tx_info.confirmations);
}
TransactionBlockchainStatus::Finalized => {
println!("Transaction is finalized with {} confirmations",
tx_info.confirmations);
}
}

```

## Checkpoint Height Configuration
## Configuration

The `checkpoint_height` is an optional setting in the indexer configuration that specifies a specific block height from which the indexing process should start. This can be useful for syncing from a specific height. Here’s how it works:
### Checkpoint Height

The `checkpoint_height` is an optional setting in the indexer configuration that specifies a specific block height from which the indexing process should start. This can be useful for syncing from a specific height. Here's how it works:

- **With Checkpoint Height**:
- If `checkpoint_height` is set, the indexer will begin syncing from the specified block height.
- If the blockchain height is lower than the `checkpoint_height`, an error will occur as the network has not reached this checkpoint.
- Once a checkpoint is set and indexed, you cannot change it unless the database is cleared.

- **Without Checkpoint Height**:
- If not set, indexing will start from the genesis block or from the last indexed height if there’s an existing index.
- If not set, indexing will start from the genesis block or from the last indexed height if there's an existing index.

### Confirmation Threshold

The `confirmation_threshold` is a setting that determines when a transaction is considered "finalized". When a transaction has at least `confirmation_threshold` confirmations, its status will be set to `TransactionBlockchainStatus::Finalized`. Otherwise, it will be `TransactionBlockchainStatus::Confirmed`. The default value is `0`.

To configure this, set the `checkpoint_height` in your settings:
To configure these settings:

```yaml
# Example configuration in config.yaml
settings:
checkpoint_height: 2000
confirmation_threshold: 6 # Transactions with 6+ confirmations are considered finalized
```

## Development Setup

1. Clone the repository
2. Install dependencies: `cargo build`
3. Run tests: `cargo test -- --ignored --test-threads=1`
3. Run tests: `cargo test -- --test-threads=1`

## Examples
- **get_estimated_fee_rate:**
Expand Down
3 changes: 2 additions & 1 deletion config/development.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ bitcoin:

settings:
checkpoint_height: 1
confirmation_threshold: 6

# possible values: error | warn | info | debug | trace
log_level: info
log_level: info
2 changes: 1 addition & 1 deletion examples/feerate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ fn main() -> Result<(), anyhow::Error> {
info!("Connected to chain {}", network);
info!("Chain best block at {}H", blockchain_height);
let storage = Rc::new(Storage::new(&config.storage)?);
let indexer_store = Rc::new(IndexerStore::new(storage)?);
let indexer_store = Rc::new(IndexerStore::new(storage, 6)?);
let indexer = Indexer::new(bitcoin_client, indexer_store.clone(), config.settings)?;

info!("Starting indexer loop. Press Ctrl+C to stop...");
Expand Down
9 changes: 7 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::settings::DEFAULT_CHECKPOINT_HEIGHT;
use crate::settings::{DEFAULT_CHECKPOINT_HEIGHT, DEFAULT_CONFIRMATION_THRESHOLD};
use bitvmx_bitcoin_rpc::rpc_config::RpcConfig;
use serde::Deserialize;
use storage_backend::storage_config::StorageConfig;
Expand All @@ -14,18 +14,23 @@ pub struct IndexerConfig {
#[derive(Deserialize, Debug, Clone)]
pub struct IndexerSettings {
pub checkpoint_height: Option<u32>,
pub confirmation_threshold: u32,
}

impl IndexerSettings {
pub fn new(checkpoint_height: Option<u32>) -> Self {
Self { checkpoint_height }
Self {
checkpoint_height,
confirmation_threshold: DEFAULT_CONFIRMATION_THRESHOLD,
}
}
}

impl Default for IndexerSettings {
fn default() -> Self {
IndexerSettings {
checkpoint_height: Some(DEFAULT_CHECKPOINT_HEIGHT),
confirmation_threshold: DEFAULT_CONFIRMATION_THRESHOLD,
}
}
}
6 changes: 6 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,10 @@ pub enum IndexerError {

#[error("Checkpoint height is behind indexed height")]
CheckpointHeightBehindIndexedHeight,

#[error("Missing transaction data in tx_status")]
MissingTransactionData,

#[error("Missing block info in tx_status")]
MissingBlockInfo,
}
Loading
Loading