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

Commit

Permalink
Merge pull request #13 from vianetwork/bitcoin-indexer-lib
Browse files Browse the repository at this point in the history
feat: library indexer implementation
  • Loading branch information
irnb authored Aug 19, 2024
2 parents 39cef62 + 79962cb commit 8b4a3ab
Show file tree
Hide file tree
Showing 12 changed files with 745 additions and 121 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.

1 change: 1 addition & 0 deletions core/lib/via_btc_client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ jsonrpsee = { workspace = true, features = [
"client",
"macros",
] }
lazy_static.workspace = true
tokio.workspace = true
bitcoin = "0.32.2"
bitcoincore-rpc = "0.19.0"
Expand Down
5 changes: 3 additions & 2 deletions core/lib/via_btc_client/DEV.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ Write integration tests in the `examples` directory.
- Only make methods public that are needed by external users.

## Taproot Script witness data for via inscription standard

```
Witness Structure for each message type
in our case da_identifier is b"celestia"
Expand Down Expand Up @@ -101,7 +102,7 @@ Votable : No
(2)
Propose Sequencer
Propose Sequencer
verifier should sent attestation to network to validate this message
Sender Validation: one of the verifiers
Votable: Yes
Expand Down Expand Up @@ -150,7 +151,7 @@ Sender Validation: only valid sequencer
| OP_FALSE |
| OP_IF |
| OP_PUSHBYTES_32 b"Str('via_inscription_protocol')" |
| OP_PUSHBYTES_32 b"Str('L1BatchDAReference')" |
| OP_PUSHBYTES_32 b"Str('L1BatchDAReferenceMessage')"|
| OP_PUSHBYTES_32 b"l1_batch_hash" |
| OP_PUSHBYTES_32 b"l1_batch_index" |
| OP_PUSHBYTES_32 b"celestia" |
Expand Down
32 changes: 19 additions & 13 deletions core/lib/via_btc_client/src/client/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use async_trait::async_trait;
use bitcoin::{Address, Network, OutPoint, TxOut, Txid};
use bitcoin::{Address, Block, BlockHash, OutPoint, Transaction, TxOut, Txid};
use bitcoincore_rpc::json::EstimateMode;

use crate::{
Expand All @@ -8,8 +8,8 @@ use crate::{
};

mod rpc_client;
#[allow(unused)]
pub use rpc_client::BitcoinRpcClient;
pub use bitcoin::Network;
pub use rpc_client::{Auth, BitcoinRpcClient};

use crate::types::BitcoinError;

Expand All @@ -21,12 +21,11 @@ pub struct BitcoinClient {

#[async_trait]
impl BitcoinOps for BitcoinClient {
async fn new(rpc_url: &str, network: Network) -> BitcoinClientResult<Self>
async fn new(rpc_url: &str, network: Network, auth: Auth) -> BitcoinClientResult<Self>
where
Self: Sized,
{
// TODO: change rpc_user & rpc_password here, move it to args
let rpc = Box::new(BitcoinRpcClient::new(rpc_url, "rpcuser", "rpcpassword")?);
let rpc = Box::new(BitcoinRpcClient::new(rpc_url, auth)?);

Ok(Self { rpc, network })
}
Expand Down Expand Up @@ -75,9 +74,12 @@ impl BitcoinOps for BitcoinClient {
Ok(height as u128)
}

async fn fetch_and_parse_block(&self, block_height: u128) -> BitcoinClientResult<String> {
let _block = self.rpc.get_block(block_height).await?;
todo!()
async fn fetch_block(&self, block_height: u128) -> BitcoinClientResult<Block> {
self.rpc.get_block_by_height(block_height).await
}

async fn fetch_block_by_hash(&self, block_hash: &BlockHash) -> BitcoinClientResult<Block> {
self.rpc.get_block_by_hash(block_hash).await
}

async fn get_fee_rate(&self, conf_target: u16) -> BitcoinClientResult<u64> {
Expand All @@ -98,6 +100,10 @@ impl BitcoinOps for BitcoinClient {
}
}

async fn get_transaction(&self, txid: &Txid) -> BitcoinClientResult<Transaction> {
self.rpc.get_transaction(txid).await
}

fn get_network(&self) -> Network {
self.network
}
Expand All @@ -124,7 +130,7 @@ mod tests {
#[tokio::test]
async fn test_get_balance() {
let context = BitcoinRegtest::new().expect("Failed to create BitcoinRegtest");
let _client = BitcoinClient::new(&context.get_url(), Network::Regtest)
let _client = BitcoinClient::new(&context.get_url(), Network::Regtest, Auth::None)
.await
.expect("Failed to create BitcoinClient");

Expand All @@ -141,7 +147,7 @@ mod tests {
#[tokio::test]
async fn test_fetch_utxos() {
let context = BitcoinRegtest::new().expect("Failed to create BitcoinRegtest");
let _client = BitcoinClient::new(&context.get_url(), Network::Regtest)
let _client = BitcoinClient::new(&context.get_url(), Network::Regtest, Auth::None)
.await
.expect("Failed to create BitcoinClient");

Expand All @@ -157,7 +163,7 @@ mod tests {
#[tokio::test]
async fn test_fetch_block_height() {
let context = BitcoinRegtest::new().expect("Failed to create BitcoinRegtest");
let client = BitcoinClient::new(&context.get_url(), Network::Regtest)
let client = BitcoinClient::new(&context.get_url(), Network::Regtest, Auth::None)
.await
.expect("Failed to create BitcoinClient");

Expand All @@ -173,7 +179,7 @@ mod tests {
#[tokio::test]
async fn test_estimate_fee() {
let context = BitcoinRegtest::new().expect("Failed to create BitcoinRegtest");
let client = BitcoinClient::new(&context.get_url(), Network::Regtest)
let client = BitcoinClient::new(&context.get_url(), Network::Regtest, Auth::None)
.await
.expect("Failed to create BitcoinClient");

Expand Down
18 changes: 9 additions & 9 deletions core/lib/via_btc_client/src/client/rpc_client.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use async_trait::async_trait;
use bitcoin::{Address, Block, OutPoint, Transaction, Txid};
use bitcoin::{Address, Block, BlockHash, OutPoint, Transaction, Txid};
pub use bitcoincore_rpc::Auth;
use bitcoincore_rpc::{
bitcoincore_rpc_json::EstimateMode,
json::{EstimateSmartFeeResult, ScanTxOutRequest},
Auth, Client, RpcApi,
Client, RpcApi,
};

use crate::{traits::BitcoinRpc, types::BitcoinRpcResult};
Expand All @@ -14,12 +15,7 @@ pub struct BitcoinRpcClient {

#[allow(unused)]
impl BitcoinRpcClient {
pub fn new(
url: &str,
rpc_user: &str,
rpc_password: &str,
) -> Result<Self, bitcoincore_rpc::Error> {
let auth = Auth::UserPass(rpc_user.to_string(), rpc_password.to_string());
pub fn new(url: &str, auth: Auth) -> Result<Self, bitcoincore_rpc::Error> {
let client = Client::new(url, auth)?;
Ok(Self { client })
}
Expand Down Expand Up @@ -71,11 +67,15 @@ impl BitcoinRpc for BitcoinRpcClient {
self.client.get_block_count().map_err(|e| e.into())
}

async fn get_block(&self, block_height: u128) -> BitcoinRpcResult<Block> {
async fn get_block_by_height(&self, block_height: u128) -> BitcoinRpcResult<Block> {
let block_hash = self.client.get_block_hash(block_height as u64)?;
self.client.get_block(&block_hash).map_err(|e| e.into())
}

async fn get_block_by_hash(&self, block_hash: &BlockHash) -> BitcoinRpcResult<Block> {
self.client.get_block(block_hash).map_err(|e| e.into())
}

async fn get_best_block_hash(&self) -> BitcoinRpcResult<bitcoin::BlockHash> {
self.client.get_best_block_hash().map_err(|e| e.into())
}
Expand Down
Loading

0 comments on commit 8b4a3ab

Please sign in to comment.