Skip to content
This repository has been archived by the owner on Aug 28, 2024. It is now read-only.

Commit

Permalink
fix: regtest, example update [ci skip]
Browse files Browse the repository at this point in the history
  • Loading branch information
steph-rs committed Aug 21, 2024
1 parent 917d7f3 commit bb8c3b2
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 147 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions core/lib/via_btc_client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,17 @@ inquire = "0.7.5"
anyhow.workspace = true
serde.workspace = true
tracing.workspace = true
tracing-subscriber = { workspace = true, optional = true }

[dev-dependencies]
mockall = "0.13.0"

[features]
regtest = []
regtest = ["tracing-subscriber"]

[[example]]
name = "bitcoin_client_example"
path = "examples/bitcoin_client_example.rs"
name = "indexer"
path = "examples/indexer_init_example.rs"
required-features = ["regtest"]

[[example]]
Expand Down
100 changes: 0 additions & 100 deletions core/lib/via_btc_client/examples/bitcoin_client_example.rs

This file was deleted.

22 changes: 22 additions & 0 deletions core/lib/via_btc_client/examples/indexer_init_example.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use anyhow::Result;
use tracing_subscriber;
use via_btc_client::{indexer::BitcoinInscriptionIndexer, regtest::BitcoinRegtest, types::Network};

#[tokio::main]
async fn main() -> Result<()> {
tracing_subscriber::fmt()
.with_max_level(tracing::Level::DEBUG)
.init();

tracing::info!("starting Bitcoin client example");
let context = BitcoinRegtest::new()?;
let miner = context.get_miner_address()?;
tracing::info!("miner address: {}", miner);
let indexer =
BitcoinInscriptionIndexer::new(&context.get_url(), Network::Regtest, vec![]).await;

if let Err(e) = indexer {
tracing::error!("Failed to create indexer: {:?}", e);
}
Ok(())
}
15 changes: 7 additions & 8 deletions core/lib/via_btc_client/src/indexer/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::collections::HashMap;

use async_trait::async_trait;
use bitcoin::{Address, BlockHash, KnownHrp, Network, Txid};
use bitcoincore_rpc::Auth;
use tracing::{debug, error, info, instrument, warn};
Expand All @@ -10,7 +9,7 @@ use parser::MessageParser;

use crate::{
client::BitcoinClient,
traits::{BitcoinIndexerOpt, BitcoinOps},
traits::BitcoinOps,
types,
types::{BitcoinIndexerResult, CommonFields, FullInscriptionMessage, L1ToL2Message, Vote},
};
Expand Down Expand Up @@ -68,10 +67,9 @@ pub struct BitcoinInscriptionIndexer {
network: Network,
}

#[async_trait]
impl BitcoinIndexerOpt for BitcoinInscriptionIndexer {
impl BitcoinInscriptionIndexer {
#[instrument(skip(rpc_url, network, bootstrap_txids), target = "bitcoin_indexer")]
async fn new(
pub async fn new(
rpc_url: &str,
network: Network,
bootstrap_txids: Vec<Txid>,
Expand Down Expand Up @@ -103,7 +101,7 @@ impl BitcoinIndexerOpt for BitcoinInscriptionIndexer {
}

#[instrument(skip(self), target = "bitcoin_indexer")]
async fn process_blocks(
pub async fn process_blocks(
&self,
starting_block: u32,
ending_block: u32,
Expand All @@ -121,7 +119,7 @@ impl BitcoinIndexerOpt for BitcoinInscriptionIndexer {
}

#[instrument(skip(self), target = "bitcoin_indexer")]
async fn process_block(
pub async fn process_block(
&self,
block_height: u32,
) -> BitcoinIndexerResult<Vec<FullInscriptionMessage>> {
Expand Down Expand Up @@ -149,7 +147,7 @@ impl BitcoinIndexerOpt for BitcoinInscriptionIndexer {
}

#[instrument(skip(self), target = "bitcoin_indexer")]
async fn are_blocks_connected(
pub async fn are_blocks_connected(
&self,
parent_hash: &BlockHash,
child_hash: &BlockHash,
Expand Down Expand Up @@ -320,6 +318,7 @@ impl BitcoinInscriptionIndexer {
mod tests {
use std::str::FromStr;

use async_trait::async_trait;
use bitcoin::{
block::Header, hashes::Hash, Amount, Block, OutPoint, ScriptBuf, Transaction, TxMerkleNode,
TxOut,
Expand Down
3 changes: 2 additions & 1 deletion core/lib/via_btc_client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub mod types;
pub(crate) mod client;
pub mod indexer;
pub mod inscriber;
pub(crate) mod regtest;
#[cfg(feature = "regtest")]
pub mod regtest;
pub(crate) mod signer;
pub(crate) mod utils;
26 changes: 16 additions & 10 deletions core/lib/via_btc_client/src/regtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ use std::{
use anyhow::Result;
use bitcoin::{address::NetworkUnchecked, Address, Network, PrivateKey};

const COMPOSE_FILE_PATH: &str =
concat!(env!("CARGO_MANIFEST_DIR"), "/tests/docker-compose-btc.yml");
const CLI_CONTAINER_NAME: &str = "tests-bitcoin-cli-1";
const COMPOSE_FILE_PATH: &str = concat!(
env!("CARGO_MANIFEST_DIR"),
"/resources/docker-compose-btc.yml"
);
const CLI_CONTAINER_NAME: &str = "resources-bitcoin-cli-1";

pub struct BitcoinRegtest {
private_key: PrivateKey,
Expand Down Expand Up @@ -105,23 +107,27 @@ mod tests {
use secp256k1::Secp256k1;

use super::*;
use crate::{client::BitcoinRpcClient, traits::BitcoinRpc};
use crate::{client::BitcoinClient, traits::BitcoinOps};

#[tokio::test]
async fn test_bitcoin_regtest() {
let regtest = BitcoinRegtest::new().expect("Failed to create BitcoinRegtest");
let rpc = BitcoinRpcClient::new(&regtest.get_url(), Auth::UserPass("rpcuser".to_string(), "rpcpassword".to_string()))
.expect("Failed create rpc client");

let block_count = rpc
.get_block_count()
let client = BitcoinClient::new(
&regtest.get_url(),
Network::Regtest,
Auth::UserPass("rpcuser".to_string(), "rpcpassword".to_string()),
)
.expect("Failed create rpc client");

let block_count = client
.fetch_block_height()
.await
.expect("Failed to get block count");
assert!(block_count > 100);

let address = regtest.get_address();
let private_key = regtest.get_private_key();
let balance = rpc
let balance = client
.get_balance(address)
.await
.expect("Failed to get balance of test address");
Expand Down
25 changes: 0 additions & 25 deletions core/lib/via_btc_client/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,28 +83,3 @@ pub(crate) trait BitcoinSigner: Send + Sync {

fn get_public_key(&self) -> PublicKey;
}

#[async_trait]
pub(crate) trait BitcoinIndexerOpt: Send + Sync {
async fn new(
rpc_url: &str,
network: Network,
bootstrap_txids: Vec<Txid>,
) -> BitcoinIndexerResult<Self>
where
Self: Sized;
async fn process_blocks(
&self,
starting_block: u32,
ending_block: u32,
) -> BitcoinIndexerResult<Vec<FullInscriptionMessage>>;
async fn process_block(
&self,
block_height: u32,
) -> BitcoinIndexerResult<Vec<FullInscriptionMessage>>;
async fn are_blocks_connected(
&self,
parent_hash: &BlockHash,
child_hash: &BlockHash,
) -> BitcoinIndexerResult<bool>;
}

0 comments on commit bb8c3b2

Please sign in to comment.