Skip to content

Commit

Permalink
chore: pick release changes to 1.1 (#4341)
Browse files Browse the repository at this point in the history
Co-authored-by: kylezs <zsembery.kyle@gmail.com>
Co-authored-by: Martin Rieke <martin@chainflip.io>
Co-authored-by: Jamie Ford <jamie@chainflip.io>
Co-authored-by: Maxim Shishmarev <msgmaxim@gmail.com>
Co-authored-by: Alastair Holmes <42404303+AlastairHolmes@users.noreply.github.com>
Co-authored-by: Martin Rieke <121793148+martin-chainflip@users.noreply.github.com>
Co-authored-by: dandanlen <3168260+dandanlen@users.noreply.github.com>
Fix: CFE Witnessing, use parent block metadata when decoding events (#4331)
fix: sweeping before withdrawal (#4337)
  • Loading branch information
8 people committed Dec 12, 2023
1 parent 93a16bc commit df4ced9
Show file tree
Hide file tree
Showing 48 changed files with 1,428 additions and 367 deletions.
23 changes: 17 additions & 6 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion api/bin/chainflip-broker-api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
authors = ["Chainflip team <https://github.com/chainflip-io>"]
name = "chainflip-broker-api"
version = "1.1.0"
version = '1.1.1'
edition = "2021"

[package.metadata.deb]
Expand Down
2 changes: 1 addition & 1 deletion api/bin/chainflip-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ authors = ["Chainflip team <https://github.com/chainflip-io>"]
edition = '2021'
build = 'build.rs'
name = "chainflip-cli"
version = "1.1.0"
version = '1.1.1'

[dependencies]
anyhow = "1.0"
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
2 changes: 1 addition & 1 deletion api/bin/chainflip-lp-api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
authors = ["Chainflip team <https://github.com/chainflip-io>"]
name = "chainflip-lp-api"
version = "1.1.0"
version = '1.1.1'
edition = "2021"

[package.metadata.deb]
Expand Down
6 changes: 5 additions & 1 deletion bouncer/shared/lp_api_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
sleep,
observeBalanceIncrease,
chainFromAsset,
isWithinOnePercent,
} from './utils';
import { jsonRpc } from './json_rpc';
import { provideLiquidity } from './provide_liquidity';
Expand Down Expand Up @@ -107,7 +108,10 @@ async function testLiquidityDeposit() {
chainflip,
(event) =>
event.data.asset.toUpperCase() === testAsset.toUpperCase() &&
Number(event.data.amountCredited.replace(/,/g, '')) === testAssetAmount,
isWithinOnePercent(
BigInt(event.data.amountCredited.replace(/,/g, '')),
BigInt(testAssetAmount),
),
);
await sendEth(liquidityDepositAddress, String(testAmount));
await observeAccountCreditedEvent;
Expand Down
7 changes: 5 additions & 2 deletions bouncer/shared/provide_liquidity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
lpMutex,
assetToChain,
amountToFineAmount,
isWithinOnePercent,
} from '../shared/utils';
import { send } from '../shared/send';

Expand Down Expand Up @@ -64,8 +65,10 @@ export async function provideLiquidity(ccy: Asset, amount: number, waitForFinali
chainflip,
(event) =>
event.data.asset.toUpperCase() === ccy &&
Number(event.data.amountCredited.replace(/,/g, '')) ===
Number(amountToFineAmount(String(amount), assetDecimals[ccy])),
isWithinOnePercent(
BigInt(event.data.amountCredited.replace(/,/g, '')),
BigInt(amountToFineAmount(String(amount), assetDecimals[ccy])),
),
undefined,
waitForFinalization,
);
Expand Down
10 changes: 10 additions & 0 deletions bouncer/shared/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,16 @@ export function isValidEthAddress(address: string): boolean {
return ethRegex.test(address);
}

export function isWithinOnePercent(value1: bigint, value2: bigint): boolean {
if (value1 < value2) {
return value2 - value1 <= value2 / BigInt(100);
}
if (value2 < value1) {
return value1 - value2 <= value1 / BigInt(100);
}
return true;
}

// "v1 is greater than v2" -> "greater"
export function compareSemVer(version1: string, version2: string) {
const v1 = version1.split('.').map(Number);
Expand Down
3 changes: 2 additions & 1 deletion engine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ authors = ["Chainflip team <https://github.com/chainflip-io>"]
build = 'build.rs'
edition = '2021'
name = "chainflip-engine"
version = '1.1.0'
version = '1.1.1'

[package.metadata.deb]
depends = "$auto, systemd"
Expand Down 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 df4ced9

Please sign in to comment.