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

bug: change_utxo not always present #5349

Closed
wants to merge 1 commit into from
Closed
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
28 changes: 19 additions & 9 deletions state-chain/runtime/src/chainflip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ use cf_traits::{
ScheduledEgressDetails,
};

use cf_chains::{btc::ScriptPubkey, instances::BitcoinInstance};
use codec::{Decode, Encode};
use eth::Address as EvmAddress;
use frame_support::{
Expand Down Expand Up @@ -815,15 +816,24 @@ impl OnBroadcastReady<Bitcoin> for BroadcastReadyProvider {
match api_call {
BitcoinApi::BatchTransfer(batch_transfer) => {
let tx_id = batch_transfer.bitcoin_transaction.txid();
let outputs = batch_transfer.bitcoin_transaction.outputs.clone();
let output_len = outputs.len();
let vout = output_len - 1;
let change_output = outputs.get(vout).unwrap();
Environment::add_bitcoin_change_utxo(
change_output.amount,
UtxoId { tx_id, vout: vout as u32 },
batch_transfer.change_utxo_key,
);
let outputs = &batch_transfer.bitcoin_transaction.outputs;
let btc_key = pallet_cf_threshold_signature::Pallet::<Runtime, BitcoinInstance>::keys(
pallet_cf_threshold_signature::Pallet::<Runtime, BitcoinInstance>::current_key_epoch()
.expect("We should always have an epoch set")).expect("We should always have a key set for the current epoch");
for (i, output) in outputs.iter().enumerate() {
if [
ScriptPubkey::Taproot(btc_key.previous.unwrap_or_default()),
ScriptPubkey::Taproot(btc_key.current),
]
.contains(&output.script_pubkey)
{
Environment::add_bitcoin_change_utxo(
output.amount,
UtxoId { tx_id, vout: i as u32 },
batch_transfer.change_utxo_key,
);
}
}
},
_ => unreachable!(),
}
Expand Down
28 changes: 26 additions & 2 deletions state-chain/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub use cf_chains::instances::{
use cf_chains::{
arb::api::ArbitrumApi,
assets::any::{AssetMap, ForeignChainAndAsset},
btc::{BitcoinCrypto, BitcoinRetryPolicy},
btc::{api::BitcoinApi, BitcoinCrypto, BitcoinRetryPolicy, ScriptPubkey},
dot::{self, PolkadotAccountId, PolkadotCrypto},
eth::{self, api::EthereumApi, Address as EthereumAddress, Ethereum},
evm::EvmCrypto,
Expand Down Expand Up @@ -2086,8 +2086,32 @@ impl_runtime_apis! {

fn cf_btc_utxos() -> BtcUtxos {
let utxos = pallet_cf_environment::BitcoinAvailableUtxos::<Runtime>::get();
let mut btc_balance = utxos.iter().fold(0, |acc, elem| acc + elem.amount);
//Sum the btc balance contained in the change utxos to the btc "free_balance"
let btc_ceremonies = pallet_cf_threshold_signature::PendingCeremonies::<Runtime,BitcoinInstance>::iter_values().map(|ceremony|{
ceremony.request_context.request_id
}).collect::<Vec<_>>();
let btc_key = pallet_cf_threshold_signature::Pallet::<Runtime, BitcoinInstance>::keys(
pallet_cf_threshold_signature::Pallet::<Runtime, BitcoinInstance>::current_key_epoch()
.expect("We should always have an epoch set")).expect("We should always have a key set for the current epoch");
for ceremony in btc_ceremonies {
if let RuntimeCall::BitcoinBroadcaster(pallet_cf_broadcast::pallet::Call::on_signature_ready{ api_call, ..}) = pallet_cf_threshold_signature::RequestCallback::<Runtime, BitcoinInstance>::get(ceremony).unwrap() {
if let BitcoinApi::BatchTransfer(batch_transfer) = *api_call {
for output in batch_transfer.bitcoin_transaction.outputs {
if [
ScriptPubkey::Taproot(btc_key.previous.unwrap_or_default()),
ScriptPubkey::Taproot(btc_key.current),
]
.contains(&output.script_pubkey)
{
btc_balance += output.amount;
}
}
}
}
}
BtcUtxos {
total_balance: utxos.iter().fold(0, |acc, elem| acc + elem.amount),
total_balance: btc_balance,
count: utxos.len() as u32,
}
}
Expand Down
Loading