Skip to content

Commit

Permalink
use REVEAL_OUTPUT_AMOUNT replace magic number (#815)
Browse files Browse the repository at this point in the history
* use REVEAL_OUTPUT_AMOUNT replace magic number

* cargo fmt

* fix lint

---------

Co-authored-by: nk_ysg <[email protected]>
  • Loading branch information
nkysggsy and nkysg authored Jun 28, 2024
1 parent 07fd364 commit 6b04b96
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
13 changes: 8 additions & 5 deletions crates/bitcoin-da/src/helpers/builders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use tracing::instrument;

use crate::helpers::{BODY_TAG, PUBLICKEY_TAG, RANDOM_TAG, ROLLUP_NAME_TAG, SIGNATURE_TAG};
use crate::spec::utxo::UTXO;
use crate::REVEAL_OUTPUT_AMOUNT;

pub fn compress_blob(blob: &[u8]) -> Vec<u8> {
let mut writer = CompressorWriter::new(Vec::new(), 4096, 11, 22);
Expand Down Expand Up @@ -209,7 +210,7 @@ fn build_commit_transaction(
let input_total = output_value + fee;

let (chosen_utxos, sum) = choose_utxos(required_utxo.clone(), &utxos, input_total)?;
let has_change = (sum - input_total) >= 546;
let has_change = (sum - input_total) >= REVEAL_OUTPUT_AMOUNT;
let direct_return = !has_change;

let outputs = if !has_change {
Expand Down Expand Up @@ -302,7 +303,8 @@ fn build_reveal_transaction(

let input_total = output_value + fee;

if input_utxo.value < Amount::from_sat(546) || input_utxo.value < Amount::from_sat(input_total)
if input_utxo.value < Amount::from_sat(REVEAL_OUTPUT_AMOUNT)
|| input_utxo.value < Amount::from_sat(input_total)
{
return Err(anyhow::anyhow!("input UTXO not big enough"));
}
Expand Down Expand Up @@ -557,6 +559,7 @@ mod tests {
use crate::helpers::builders::{compress_blob, decompress_blob};
use crate::helpers::parsers::parse_transaction;
use crate::spec::utxo::UTXO;
use crate::REVEAL_OUTPUT_AMOUNT;

#[test]
fn compression_decompression() {
Expand Down Expand Up @@ -933,7 +936,7 @@ mod tests {
utxo.tx_id,
utxo.vout,
address.clone(),
546,
REVEAL_OUTPUT_AMOUNT,
8.0,
&script,
&control_block,
Expand All @@ -949,7 +952,7 @@ mod tests {
assert_eq!(tx.input[0].previous_output.vout, utxo.vout);

assert_eq!(tx.output.len(), 1);
assert_eq!(tx.output[0].value, Amount::from_sat(546));
assert_eq!(tx.output[0].value, Amount::from_sat(REVEAL_OUTPUT_AMOUNT));
assert_eq!(tx.output[0].script_pubkey, address.script_pubkey());

let utxo = utxos.get(2).unwrap();
Expand All @@ -962,7 +965,7 @@ mod tests {
utxo.tx_id,
utxo.vout,
address.clone(),
546,
REVEAL_OUTPUT_AMOUNT,
75.0,
&script,
&control_block,
Expand Down
2 changes: 1 addition & 1 deletion crates/bitcoin-da/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ impl BitcoinService {

let utxos: Vec<UTXO> = utxos
.into_iter()
.filter(|utxo| utxo.spendable && utxo.solvable && utxo.amount > 546)
.filter(|utxo| utxo.spendable && utxo.solvable && utxo.amount > REVEAL_OUTPUT_AMOUNT)
.collect();
if utxos.is_empty() {
return Err(anyhow::anyhow!("There are no spendable UTXOs"));
Expand Down

0 comments on commit 6b04b96

Please sign in to comment.