Skip to content

Commit

Permalink
feat: track fees using verbose block rpc
Browse files Browse the repository at this point in the history
  • Loading branch information
kylezs committed Dec 12, 2023
1 parent 39bf0cf commit e6de2dd
Show file tree
Hide file tree
Showing 8 changed files with 399 additions and 287 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
174 changes: 102 additions & 72 deletions api/bin/chainflip-ingress-egress-tracker/src/witnessing/btc_mempool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{
};

use anyhow::anyhow;
use bitcoin::{address::NetworkUnchecked, Amount, BlockHash, ScriptBuf, Transaction, Txid};
use bitcoin::{address::NetworkUnchecked, BlockHash, ScriptBuf, Txid};
use chainflip_engine::{
btc::rpc::{BtcRpcApi, BtcRpcClient},
settings::HttpBasicAuthEndpoint,
Expand Down Expand Up @@ -90,20 +90,19 @@ async fn get_updated_cache<T: BtcRpcApi>(btc: &T, previous_cache: Cache) -> anyh
})
.collect();

let transactions: Vec<Transaction> =
btc.get_raw_transactions(unknown_mempool_transactions).await?;
let transactions = btc.get_raw_transactions(unknown_mempool_transactions).await?;

for tx in transactions {
let txid = tx.txid();
for txout in tx.output {
let txid = tx.txid;
for txout in tx.vout {
new_known_tx_hashes.insert(txid);

new_transactions.insert(
txout.script_pubkey.clone(),
QueryResult {
destination: txout.script_pubkey,
confirmations: 0,
value: Amount::from_sat(txout.value).to_btc(),
value: txout.value.to_btc(),
tx_hash: txid,
},
);
Expand All @@ -123,20 +122,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,44 +223,46 @@ 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},
};
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>,
mempool: Vec<VerboseTransaction>,
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> {
Ok(self.latest_block_hash)
}
async fn get_raw_mempool(&self) -> anyhow::Result<Vec<Txid>> {
Ok(self.mempool.iter().map(|x| x.txid()).collect())
Ok(self.mempool.iter().map(|x| x.txid).collect())
}

async fn get_raw_transactions(
&self,
tx_hashes: Vec<Txid>,
) -> anyhow::Result<Vec<Transaction>> {
let mut result: Vec<Transaction> = Default::default();
) -> anyhow::Result<Vec<VerboseTransaction>> {
let mut result: Vec<VerboseTransaction> = Default::default();
for hash in tx_hashes {
for tx in self.mempool.clone() {
if tx.txid() == hash {
if tx.txid == hash {
result.push(tx)
}
}
Expand Down Expand Up @@ -300,57 +301,99 @@ 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(),
}
}

fn tx_with_outs(tx_outs: Vec<TxOut>) -> Transaction {
Transaction {
output: tx_outs,
version: 0,
lock_time: LockTime::from_consensus(0),
input: Default::default(),
}
fn tx_with_outs(tx_outs: Vec<(u64, ScriptBuf)>) -> VerboseTransaction {
verbose_transaction(verbose_vouts(tx_outs), None)
}

#[tokio::test]
Expand All @@ -362,15 +405,10 @@ mod tests {
let a2_script = address::Address::from_str(&address2).unwrap().payload.script_pubkey();

let mempool = vec![tx_with_outs(vec![
TxOut {
value: Amount::from_btc(0.8).unwrap().to_sat(),
script_pubkey: a1_script.clone(),
},
TxOut {
value: Amount::from_btc(1.2).unwrap().to_sat(),
script_pubkey: a2_script.clone(),
},
(Amount::from_btc(0.8).unwrap().to_sat(), a1_script.clone()),
(Amount::from_btc(1.2).unwrap().to_sat(), a2_script.clone()),
])];

let blocks = init_blocks();
let btc = MockBtcRpc { mempool, latest_block_hash: i_to_block_hash(15), blocks };
let cache: Cache = Default::default();
Expand All @@ -391,14 +429,8 @@ mod tests {
let a3_script = address::Address::from_str(&address3).unwrap().payload.script_pubkey();

let mempool = vec![
tx_with_outs(vec![TxOut {
value: Amount::from_btc(0.8).unwrap().to_sat(),
script_pubkey: a1_script.clone(),
}]),
tx_with_outs(vec![TxOut {
value: Amount::from_btc(0.8).unwrap().to_sat(),
script_pubkey: a2_script.clone(),
}]),
tx_with_outs(vec![(Amount::from_btc(0.8).unwrap().to_sat(), a1_script.clone())]),
tx_with_outs(vec![(Amount::from_btc(0.8).unwrap().to_sat(), a2_script.clone())]),
];
let blocks = init_blocks();
let mut rpc: MockBtcRpc =
Expand All @@ -412,10 +444,10 @@ mod tests {
assert_eq!(result[1].as_ref().unwrap().destination, a2_script);
assert!(result[2].is_none());

rpc.mempool.append(&mut vec![tx_with_outs(vec![TxOut {
value: Amount::from_btc(0.8).unwrap().to_sat(),
script_pubkey: a3_script.clone(),
}])]);
rpc.mempool.append(&mut vec![tx_with_outs(vec![(
Amount::from_btc(0.8).unwrap().to_sat(),
a3_script.clone(),
)])]);

let cache = get_updated_cache(&rpc, cache.clone()).await.unwrap();
let result =
Expand All @@ -440,7 +472,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 Expand Up @@ -471,10 +503,8 @@ mod tests {

let tx_value: Amount = Amount::from_btc(12.5).unwrap();

let mempool = vec![tx_with_outs(vec![TxOut {
value: Amount::from_btc(0.8).unwrap().to_sat(),
script_pubkey: a1_script.clone(),
}])];
let mempool =
vec![tx_with_outs(vec![(Amount::from_btc(0.8).unwrap().to_sat(), a1_script.clone())])];

let mut blocks = init_blocks();

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
Loading

0 comments on commit e6de2dd

Please sign in to comment.