Skip to content

Commit

Permalink
minimize op_return usage
Browse files Browse the repository at this point in the history
  • Loading branch information
ekrembal committed Aug 29, 2024
1 parent e74e523 commit bde257a
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 10 deletions.
5 changes: 4 additions & 1 deletion core/src/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::{script_builder, utils, EVMAddress, UTXO};
use bitcoin::address::NetworkUnchecked;
use bitcoin::consensus::deserialize;
use bitcoin::hashes::Hash;
use bitcoin::script::PushBytesBuf;
use bitcoin::sighash::SighashCache;
use bitcoin::{Address, OutPoint, TapSighash, Transaction, TxOut, Txid};
use bitcoin_mock_rpc::RpcApiWrapper;
Expand Down Expand Up @@ -266,7 +267,9 @@ where
&Message::from_digest_slice(sighash.as_byte_array()).expect("should be hash"),
&user_xonly_pk,
)?;
let op_return_txout = script_builder::op_return_txout(self.idx.to_be_bytes());
let mut push_bytes = PushBytesBuf::new();
push_bytes.extend_from_slice(&utils::usize_to_var_len_bytes(self.idx)).unwrap();
let op_return_txout = script_builder::op_return_txout(push_bytes);
tx.output.push(op_return_txout.clone());
let funded_tx = self
.rpc
Expand Down
8 changes: 1 addition & 7 deletions core/src/transaction_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,13 +290,7 @@ impl TransactionBuilder {
network,
);
let mut op_return_script: Vec<u8> = hex::decode(move_txid.to_string()).unwrap();
let usize_bytes = (usize::BITS / 8) as usize;
let bits = operator_idx.max(1).ilog2() + 1;
let len = ((bits + 7) / 8) as usize;
let empty = usize_bytes - len;
let op_idx_bytes = operator_idx.to_be_bytes();
let op_idx_bytes = &op_idx_bytes[empty..];
op_return_script.extend(op_idx_bytes);
op_return_script.extend(utils::usize_to_var_len_bytes(operator_idx));
let mut push_bytes = PushBytesBuf::new();
push_bytes.extend_from_slice(&op_return_script).unwrap();
let op_return_txout = script_builder::op_return_txout(push_bytes);
Expand Down
10 changes: 10 additions & 0 deletions core/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ pub fn parse_hex_to_btc_tx(
}
}

pub fn usize_to_var_len_bytes(x: usize) -> Vec<u8> {
let usize_bytes = (usize::BITS / 8) as usize;
let bits = x.max(1).ilog2() + 1;
let len = ((bits + 7) / 8) as usize;
let empty = usize_bytes - len;
let op_idx_bytes = x.to_be_bytes();
let op_idx_bytes = &op_idx_bytes[empty..];
op_idx_bytes.to_vec()
}

pub fn handle_taproot_witness_new<T: AsRef<[u8]>>(
tx: &mut TxHandler,
witness_elements: &[T],
Expand Down
6 changes: 4 additions & 2 deletions core/tests/flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,17 @@ async fn test_honest_operator_takes_refund() {
.new_withdrawal_sig_rpc(0, user_sig, empty_utxo, withdrawal_tx_out)
.await
.unwrap();
println!("{:?}", withdrawal_provide_txid);
println!("Withdrawal provide: {:?}", withdrawal_provide_txid);
let txs_to_be_sent = operators[0]
.0
.withdrawal_proved_on_citrea_rpc(0, deposit_outpoint)
.await
.unwrap();
tracing::debug!("txs_to_be_sent: {:#?}", txs_to_be_sent);

for tx in txs_to_be_sent.iter().take(txs_to_be_sent.len() - 1) {
rpc.send_raw_transaction(tx.clone()).unwrap();
let outpoint = rpc.send_raw_transaction(tx.clone()).unwrap();
tracing::debug!("outpoint: {:#?}", outpoint);
}
rpc.mine_blocks(OPERATOR_TAKES_AFTER as u64).unwrap();
// send the last tx
Expand Down

0 comments on commit bde257a

Please sign in to comment.