Skip to content

Commit

Permalink
fix: added conditional for integration test in ci and clean printlns
Browse files Browse the repository at this point in the history
  • Loading branch information
EdsonAlcala committed Sep 26, 2024
1 parent 33cd18e commit ab728d0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 32 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,5 @@ jobs:
- uses: foundry-rs/foundry-toolchain@v1
- name: Run integration tests
run: just test-integration
env:
CI_ENVIRONMENT: true
55 changes: 23 additions & 32 deletions tests/bitcoin_integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,29 @@ pub use utils::bitcoin_utils::*;
const OMNI_SPEND_AMOUNT: OmniAmount = OmniAmount::from_sat(500_000_000);

fn setup_bitcoin_testnet() -> Result<bitcoind::BitcoinD> {
let curr_dir_path = std::env::current_dir().unwrap();

let bitcoind_path = if cfg!(target_os = "macos") {
curr_dir_path.join("tests/bin").join("bitcoind-mac")
} else if cfg!(target_os = "linux") {
curr_dir_path.join("tests/bin").join("bitcoind-linux")
if std::env::var("CI_ENVIRONMENT").is_ok() {
let curr_dir_path = std::env::current_dir().unwrap();

let bitcoind_path = if cfg!(target_os = "macos") {
curr_dir_path.join("tests/bin").join("bitcoind-mac")
} else if cfg!(target_os = "linux") {
curr_dir_path.join("tests/bin").join("bitcoind-linux")
} else {
return Err(
std::io::Error::new(std::io::ErrorKind::Other, "Unsupported platform").into(),
);
};

let temp_dir = TempDir::new().expect("Failed to create temp dir");

let mut conf = bitcoind::Conf::default();
conf.tmpdir = Some(temp_dir.path().to_path_buf());
let bitcoind = bitcoind::BitcoinD::with_conf(bitcoind_path, &conf).unwrap();
Ok(bitcoind)
} else {
return Err(std::io::Error::new(std::io::ErrorKind::Other, "Unsupported platform").into());
};

let temp_dir = TempDir::new().expect("Failed to create temp dir");

let mut conf = bitcoind::Conf::default();
conf.tmpdir = Some(temp_dir.path().to_path_buf());
let bitcoind = bitcoind::BitcoinD::with_conf(bitcoind_path, &conf).unwrap();
Ok(bitcoind)
let bitcoind = bitcoind::BitcoinD::from_downloaded().unwrap();
Ok(bitcoind)
}
}

#[tokio::test]
Expand Down Expand Up @@ -204,24 +211,18 @@ async fn test_send_p2wpkh_using_rust_bitcoin_and_omni_library() -> Result<()> {

let vout = first_unspent["vout"].as_u64().unwrap();

println!("tx id {:?}", omni_txid);
println!("vout {:?}", vout);
// Create inputs using Omni library
let txin: OmniTxIn = OmniTxIn {
previous_output: OmniOutPoint::new(omni_txid, vout as u32),
script_sig: OmniScriptBuf::default(), // For a p2wpkh script_sig is empty.
sequence: OmniSequence::MAX,
witness: OmniWitness::default(),
};

println!("tx in {:?}", txin);

// The spend output is locked to a key controlled by the receiver. In this case to Alice.
let spend_txout = OmniTxOut {
value: OMNI_SPEND_AMOUNT,
script_pubkey: OmniScriptBuf(alice.script_pubkey.as_bytes().to_vec()),
};
println!("spend_txout {:?}", spend_txout);

let utxo_amount =
OmniAmount::from_sat((first_unspent["amount"].as_f64().unwrap() * 100_000_000.0) as u64);
Expand All @@ -235,7 +236,6 @@ async fn test_send_p2wpkh_using_rust_bitcoin_and_omni_library() -> Result<()> {
value: change_amount,
script_pubkey: OmniScriptBuf(script_sig.as_bytes().to_vec()), // Change comes back to us.
};
println!("change_txout {:?}", change_txout);

let mut omni_tx: BitcoinTransaction = TransactionBuilder::new::<BITCOIN>()
.version(OmniVersion::Two)
Expand All @@ -244,8 +244,6 @@ async fn test_send_p2wpkh_using_rust_bitcoin_and_omni_library() -> Result<()> {
.outputs(vec![spend_txout, change_txout])
.build();

println!("omni_tx {:?}", omni_tx);

// Prepare the transaction for signing
let sighash_type = OmniSighashType::All;
let input_index = 0;
Expand All @@ -270,28 +268,21 @@ async fn test_send_p2wpkh_using_rust_bitcoin_and_omni_library() -> Result<()> {
.is_ok();

assert!(is_valid, "The signature should be valid");
// println!("Is Valid {:?}", is_valid);

// Encode the signature
let signature = bitcoin::ecdsa::Signature {
signature: signature_omni,
sighash_type: EcdsaSighashType::All,
};

println!("signature_omni {:?}", sighash_omni);
println!("public key {:?}", &bob.public_key);

// Create the witness
let witness = Witness::p2wpkh(&signature, &bob.public_key);

// Assign script_sig to txin
let encoded_omni_tx = omni_tx.build_with_witness(0, witness.to_vec(), TransactionType::P2WPKH);

println!("encoded omni tx {:?}", encoded_omni_tx);

// Convert the transaction to a hexadecimal string
let hex_omni_tx = hex::encode(encoded_omni_tx);
println!("hex omni tx {:?}", hex_omni_tx);
let _hex_omni_tx = hex::encode(encoded_omni_tx);

// TODO: Fix broadcasting the transaction
// let raw_tx_result: serde_json::Value = client
Expand Down

0 comments on commit ab728d0

Please sign in to comment.