Skip to content

Commit

Permalink
feat: use verbose block
Browse files Browse the repository at this point in the history
  • Loading branch information
kylezs committed Dec 12, 2023
1 parent 39bf0cf commit 97afabb
Show file tree
Hide file tree
Showing 8 changed files with 362 additions and 229 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

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

Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@ where
.btc_deposits(witness_call.clone())
.egress_items(scope, state_chain_stream, state_chain_client)
.await
.then(move |epoch, header| {
process_egress(epoch, header, witness_call.clone(), btc_client.clone())
})
.then(move |epoch, header| process_egress(epoch, header, witness_call.clone()))
.logging("witnessing")
.spawn(scope);

Expand Down
110 changes: 80 additions & 30 deletions api/bin/chainflip-ingress-egress-tracker/src/witnessing/btc_mempool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,20 +123,20 @@ async fn get_updated_cache<T: BtcRpcApi>(btc: &T, previous_cache: Cache) -> anyh
for confirmations in 1..SAFETY_MARGIN {
let block = btc.block(block_hash_to_query).await?;
for tx in block.txdata {
let tx_hash = tx.txid();
for txout in tx.output {
let tx_hash = tx.txid;
for txout in tx.vout {
new_transactions.insert(
txout.script_pubkey.clone(),
QueryResult {
destination: txout.script_pubkey,
confirmations,
value: Amount::from_sat(txout.value).to_btc(),
value: txout.value.to_btc(),
tx_hash,
},
);
}
}
block_hash_to_query = block.header.prev_blockhash;
block_hash_to_query = block.header.previous_block_hash.unwrap();
}
}
Ok(Cache {
Expand Down Expand Up @@ -224,27 +224,30 @@ mod tests {
use std::collections::BTreeMap;

use bitcoin::{
absolute::LockTime,
absolute::{Height, LockTime},
address::{self},
block::{Header, Version},
block::Version,
hash_types::TxMerkleNode,
hashes::Hash,
Block, TxOut,
secp256k1::rand::{self, Rng},
TxOut,
};
use chainflip_engine::btc::rpc::{
BlockHeader, Difficulty, VerboseBlock, VerboseTransaction, VerboseTxOut,
};
use chainflip_engine::btc::rpc::BlockHeader;

use super::*;

#[derive(Clone)]
struct MockBtcRpc {
mempool: Vec<Transaction>,
latest_block_hash: BlockHash,
blocks: BTreeMap<BlockHash, Block>,
blocks: BTreeMap<BlockHash, VerboseBlock>,
}

#[async_trait::async_trait]
impl BtcRpcApi for MockBtcRpc {
async fn block(&self, block_hash: BlockHash) -> anyhow::Result<Block> {
async fn block(&self, block_hash: BlockHash) -> anyhow::Result<VerboseBlock> {
self.blocks.get(&block_hash).cloned().ok_or(anyhow!("Block missing"))
}
async fn best_block_hash(&self) -> anyhow::Result<BlockHash> {
Expand Down Expand Up @@ -300,45 +303,92 @@ mod tests {
BlockHash::from_byte_array([i; 32])
}

fn header_with_prev_hash(i: u8) -> Header {
Header {
fn header_with_prev_hash(i: u8) -> BlockHeader {
let hash = i_to_block_hash(i + 1);
BlockHeader {
version: Version::from_consensus(0),
prev_blockhash: i_to_block_hash(i),
previous_block_hash: Some(i_to_block_hash(i)),
merkle_root: TxMerkleNode::from_byte_array([0u8; 32]),
time: 0,
bits: Default::default(),
nonce: 0,
hash,
confirmations: 1,
height: 2000,
version_hex: Default::default(),
median_time: Default::default(),
difficulty: Difficulty::Number(1.0),
chainwork: Default::default(),
n_tx: Default::default(),
next_block_hash: None,
strippedsize: None,
size: None,
weight: None,
}
}

fn init_blocks() -> BTreeMap<BlockHash, Block> {
let mut blocks: BTreeMap<BlockHash, Block> = Default::default();
fn init_blocks() -> BTreeMap<BlockHash, VerboseBlock> {
let mut blocks: BTreeMap<BlockHash, VerboseBlock> = Default::default();
for i in 1..16 {
blocks.insert(
i_to_block_hash(i),
Block { header: header_with_prev_hash(i - 1), txdata: vec![] },
VerboseBlock { header: header_with_prev_hash(i - 1), txdata: vec![] },
);
}
blocks
}

pub fn verbose_transaction(
tx_outs: Vec<VerboseTxOut>,
fee: Option<Amount>,
) -> VerboseTransaction {
let random_number: u8 = rand::thread_rng().gen();
let txid = Txid::from_byte_array([random_number; 32]);
VerboseTransaction {
txid,
version: Version::from_consensus(2),
locktime: LockTime::Blocks(Height::from_consensus(0).unwrap()),
vin: vec![],
vout: tx_outs,
fee,
// not important, we just need to set it to a value.
hash: txid,
size: Default::default(),
vsize: Default::default(),
weight: Default::default(),
hex: Default::default(),
}
}

pub fn verbose_vouts(vals_and_scripts: Vec<(u64, ScriptBuf)>) -> Vec<VerboseTxOut> {
vals_and_scripts
.into_iter()
.enumerate()
.map(|(n, (value, script_pub_key))| VerboseTxOut {
value: Amount::from_sat(value),
n: n as u64,
script_pubkey: script_pub_key,
})
.collect()
}

// This creates one tx out in one transaction for each item in txdata
fn block_prev_hash_tx_outs(i: u8, txdata: Vec<(Amount, String)>) -> Block {
Block {
fn block_prev_hash_tx_outs(i: u8, txdata: Vec<(Amount, String)>) -> VerboseBlock {
VerboseBlock {
header: header_with_prev_hash(i),
txdata: txdata
.into_iter()
.map(|(value, destination)| bitcoin::Transaction {
output: vec![TxOut {
value: value.to_sat(),
script_pubkey: bitcoin::Address::from_str(&destination)
.unwrap()
.payload
.script_pubkey(),
}],
input: Default::default(),
version: 0,
lock_time: LockTime::from_consensus(0),
.map(|(value, destination)| {
verbose_transaction(
verbose_vouts(vec![(
value.to_sat(),
bitcoin::Address::from_str(&destination)
.unwrap()
.payload
.script_pubkey(),
)]),
None,
)
})
.collect(),
}
Expand Down Expand Up @@ -440,7 +490,7 @@ mod tests {

let mempool = vec![];

let mut blocks: BTreeMap<BlockHash, Block> = Default::default();
let mut blocks: BTreeMap<BlockHash, VerboseBlock> = Default::default();
for i in 1..19 {
blocks.insert(i_to_block_hash(i), block_prev_hash_tx_outs(i - 1, vec![]));
}
Expand Down
1 change: 1 addition & 0 deletions engine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ tempfile = "3.7.0"
utilities = { package = "utilities", path = "../utilities", features = [
"test-utils",
] }
serde_path_to_error = "*"

[build-dependencies]
substrate-build-script-utils = { git = "https://github.com/chainflip-io/substrate.git", tag = "chainflip-monthly-2023-08+3" }
Expand Down
27 changes: 5 additions & 22 deletions engine/src/btc/retry_rpc.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use bitcoin::{Block, BlockHash, Transaction, Txid};
use bitcoin::{BlockHash, Txid};
use utilities::task_scope::Scope;

use crate::{
Expand All @@ -11,7 +11,7 @@ use core::time::Duration;

use anyhow::Result;

use super::rpc::{BlockHeader, BtcRpcApi, BtcRpcClient};
use super::rpc::{BlockHeader, BtcRpcApi, BtcRpcClient, VerboseBlock};

#[derive(Clone)]
pub struct BtcRetryRpcClient {
Expand Down Expand Up @@ -51,7 +51,7 @@ impl BtcRetryRpcClient {

#[async_trait::async_trait]
pub trait BtcRetryRpcApi: Clone {
async fn block(&self, block_hash: BlockHash) -> Block;
async fn block(&self, block_hash: BlockHash) -> VerboseBlock;

async fn block_hash(&self, block_number: cf_chains::btc::BlockNumber) -> BlockHash;

Expand All @@ -62,13 +62,11 @@ pub trait BtcRetryRpcApi: Clone {
async fn average_block_fee_rate(&self, block_hash: BlockHash) -> cf_chains::btc::BtcAmount;

async fn best_block_header(&self) -> BlockHeader;

async fn get_raw_transactions(&self, tx_hashes: Vec<Txid>) -> Vec<Transaction>;
}

#[async_trait::async_trait]
impl BtcRetryRpcApi for BtcRetryRpcClient {
async fn block(&self, block_hash: BlockHash) -> Block {
async fn block(&self, block_hash: BlockHash) -> VerboseBlock {
self.retry_client
.request(
Box::pin(move |client| {
Expand Down Expand Up @@ -153,19 +151,6 @@ impl BtcRetryRpcApi for BtcRetryRpcClient {
)
.await
}

async fn get_raw_transactions(&self, tx_hashes: Vec<Txid>) -> Vec<Transaction> {
self.retry_client
.request(
Box::pin(move |client| {
let tx_hashes = tx_hashes.clone();
#[allow(clippy::redundant_async_block)]
Box::pin(async move { client.get_raw_transactions(tx_hashes).await })
}),
RequestLog::new("get_raw_transactions".to_string(), None),
)
.await
}
}

#[async_trait::async_trait]
Expand Down Expand Up @@ -216,7 +201,7 @@ pub mod mocks {

#[async_trait::async_trait]
impl BtcRetryRpcApi for BtcRetryRpcClient {
async fn block(&self, block_hash: BlockHash) -> Block;
async fn block(&self, block_hash: BlockHash) -> VerboseBlock;

async fn block_hash(&self, block_number: cf_chains::btc::BlockNumber) -> BlockHash;

Expand All @@ -227,8 +212,6 @@ pub mod mocks {
async fn average_block_fee_rate(&self, block_hash: BlockHash) -> cf_chains::btc::BtcAmount;

async fn best_block_header(&self) -> BlockHeader;

async fn get_raw_transactions(&self, tx_hashes: Vec<Txid>) -> Vec<Transaction>;
}
}
}
Loading

0 comments on commit 97afabb

Please sign in to comment.