Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: track btc fees on success #4334

Merged
merged 3 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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
10 changes: 5 additions & 5 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, 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 @@ -66,7 +66,7 @@ pub trait BtcRetryRpcApi: Clone {

#[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 @@ -201,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 Down
Loading