-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
715 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
use super::Error; | ||
use crate::DynBitcoinCoreApi; | ||
use async_trait::async_trait; | ||
use crate::{serialize, BitcoinCoreApi, Error as BitcoinError}; | ||
|
||
#[async_trait] | ||
pub trait Backing { | ||
/// Returns the height of the longest chain | ||
async fn get_block_count(&self) -> Result<u32, Error>; | ||
|
||
/// Returns the raw header of a block in storage | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `height` - The height of the block to fetch | ||
async fn get_block_header(&self, height: u32) -> Result<Option<Vec<u8>>, Error>; | ||
|
||
/// Returns the (little endian) hash of a block | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `height` - The height of the block to fetch | ||
async fn get_block_hash(&self, height: u32) -> Result<Vec<u8>, Error>; | ||
} | ||
|
||
#[async_trait] | ||
impl Backing for DynBitcoinCoreApi { | ||
async fn get_block_count(&self) -> Result<u32, Error> { | ||
let count = BitcoinCoreApi::get_block_count(&**self).await?; | ||
return Ok(count as u32); | ||
} | ||
|
||
async fn get_block_header(&self, height: u32) -> Result<Option<Vec<u8>>, Error> { | ||
let block_hash = match BitcoinCoreApi::get_block_hash(&**self, height).await { | ||
Ok(h) => h, | ||
Err(BitcoinError::InvalidBitcoinHeight) => { | ||
return Ok(None); | ||
} | ||
Err(err) => return Err(err.into()), | ||
}; | ||
let block_header = BitcoinCoreApi::get_block_header(&**self, &block_hash).await?; | ||
Ok(Some(serialize(&block_header))) | ||
} | ||
|
||
async fn get_block_hash(&self, height: u32) -> Result<Vec<u8>, Error> { | ||
let block_hash = BitcoinCoreApi::get_block_hash(&**self, height) | ||
.await | ||
.map(|hash| serialize(&hash))?; | ||
Ok(block_hash) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#![allow(clippy::enum_variant_names)] | ||
|
||
use crate::Error as BitcoinError; | ||
use thiserror::Error; | ||
|
||
#[cfg(test)] | ||
use std::mem::discriminant; | ||
|
||
#[derive(Error, Debug)] | ||
pub enum Error { | ||
#[error("Client already initialized")] | ||
AlreadyInitialized, | ||
#[error("Client has not been initialized")] | ||
NotInitialized, | ||
#[error("Block already submitted")] | ||
BlockExists, | ||
#[error("Cannot read the best height")] | ||
CannotFetchBestHeight, | ||
#[error("Block hash not found for the given height")] | ||
BlockHashNotFound, | ||
#[error("Block header not found for the given height")] | ||
BlockHeaderNotFound, | ||
#[error("Failed to decode hash")] | ||
DecodeHash, | ||
#[error("Failed to serialize block header")] | ||
SerializeHeader, | ||
|
||
#[error("BitcoinError: {0}")] | ||
BitcoinError(#[from] BitcoinError), | ||
} | ||
|
||
#[cfg(test)] | ||
impl PartialEq for Error { | ||
fn eq(&self, other: &Self) -> bool { | ||
discriminant(self) == discriminant(other) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
use super::Error; | ||
use async_trait::async_trait; | ||
use crate::{sha256, Hash}; | ||
use std::sync::Arc; | ||
|
||
#[async_trait] | ||
pub trait Issuing { | ||
/// Returns true if the light client is initialized | ||
async fn is_initialized(&self) -> Result<bool, Error>; | ||
|
||
/// Initialize the light client | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `header` - Raw block header | ||
/// * `height` - Starting height | ||
async fn initialize(&self, header: Vec<u8>, height: u32) -> Result<(), Error>; | ||
|
||
/// Submit a block header and wait for inclusion | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `header` - Raw block header | ||
async fn submit_block_header( | ||
&self, | ||
header: Vec<u8>, | ||
) -> Result<(), Error>; | ||
|
||
/// Submit a batch of block headers and wait for inclusion | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `headers` - Raw block headers (multiple of 80 bytes) | ||
async fn submit_block_header_batch(&self, headers: Vec<Vec<u8>>) -> Result<(), Error>; | ||
|
||
/// Returns the light client's chain tip | ||
async fn get_best_height(&self) -> Result<u32, Error>; | ||
|
||
/// Returns the block hash stored at a given height, | ||
/// this is assumed to be in little-endian format | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `height` - Height of the block to fetch | ||
async fn get_block_hash(&self, height: u32) -> Result<Vec<u8>, Error>; | ||
|
||
/// Returns true if the block described by the hash | ||
/// has been stored in the light client | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `hash_le` - Hash (little-endian) of the block | ||
async fn is_block_stored(&self, hash_le: Vec<u8>) -> Result<bool, Error>; | ||
} |
Oops, something went wrong.