Skip to content

Commit

Permalink
ledger: transactions: Do not use Option as create_txout parameter.
Browse files Browse the repository at this point in the history
  • Loading branch information
ceyhunsen committed Jul 3, 2024
1 parent 167804c commit 3f41671
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 38 deletions.
28 changes: 12 additions & 16 deletions src/client/rpc_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,7 @@ impl RpcApi for Client {
_confirmation_target: Option<u32>,
_estimate_mode: Option<json::EstimateMode>,
) -> bitcoincore_rpc::Result<bitcoin::Txid> {
let target_txout = self
.ledger
.create_txout(amount, Some(address.script_pubkey()));
let target_txout = self.ledger.create_txout(amount, address.script_pubkey());

let tx = self.ledger.create_transaction(vec![], vec![target_txout]);

Expand All @@ -171,7 +169,7 @@ impl RpcApi for Client {
// Block reward is 1 BTC regardless of how many block is mined.
let txout = self.ledger.create_txout(
Amount::from_sat(100_000_000 * block_num),
Some(address.script_pubkey()),
address.script_pubkey(),
);
let tx = self.ledger.create_transaction(vec![], vec![txout]);

Expand Down Expand Up @@ -206,26 +204,24 @@ mod tests {
// First, add some funds to user, for free.
let txout = rpc
.ledger
.create_txout(Amount::from_sat(100_000_000), Some(address.script_pubkey()));
.create_txout(Amount::from_sat(100_000_000), address.script_pubkey());
let tx = rpc.ledger.create_transaction(vec![], vec![txout]);
let txid = rpc.ledger.add_transaction_unconditionally(tx).unwrap();

// Create a new raw transactions that is valid.
let txin = rpc.ledger._create_txin(txid, 0);
let txout = rpc
.ledger
.create_txout(Amount::from_sat(0x45), Some(address.script_pubkey()));
.create_txout(Amount::from_sat(0x45), address.script_pubkey());
let inserted_tx1 = rpc.ledger.create_transaction(vec![txin], vec![txout]);
rpc.send_raw_transaction(&inserted_tx1).unwrap();

let txin = rpc.ledger._create_txin(inserted_tx1.compute_txid(), 0);
let txout = rpc.ledger.create_txout(
Amount::from_sat(0x45),
Some(
Ledger::generate_credential_from_witness()
.address
.script_pubkey(),
),
Ledger::generate_credential_from_witness()
.address
.script_pubkey(),
);
let inserted_tx2 = rpc.ledger.create_transaction(vec![txin], vec![txout]);
rpc.send_raw_transaction(&inserted_tx2).unwrap();
Expand Down Expand Up @@ -255,15 +251,15 @@ mod tests {
// First, add some funds to user, for free.
let txout = rpc
.ledger
.create_txout(Amount::from_sat(100_000_000), Some(address.script_pubkey()));
.create_txout(Amount::from_sat(100_000_000), address.script_pubkey());
let tx = rpc.ledger.create_transaction(vec![], vec![txout]);
let txid = rpc.ledger.add_transaction_unconditionally(tx).unwrap();

// Insert raw transactions to Bitcoin.
let txin = rpc.ledger._create_txin(txid, 0);
let txout = rpc
.ledger
.create_txout(Amount::from_sat(0x1F), Some(address.script_pubkey()));
.create_txout(Amount::from_sat(0x1F), address.script_pubkey());
let tx = rpc.ledger.create_transaction(vec![txin], vec![txout]);
rpc.send_raw_transaction(&tx).unwrap();

Expand Down Expand Up @@ -346,7 +342,7 @@ mod tests {
// Empty wallet should reject transaction.
let txout = rpc
.ledger
.create_txout(Amount::from_sat(1), Some(address.script_pubkey()));
.create_txout(Amount::from_sat(1), address.script_pubkey());
let tx = rpc.ledger.create_transaction(vec![], vec![txout]);
if let Ok(()) = rpc.ledger.check_transaction(&tx) {
assert!(false);
Expand All @@ -366,7 +362,7 @@ mod tests {
);
let txout = rpc
.ledger
.create_txout(Amount::from_sat(1), Some(address.script_pubkey()));
.create_txout(Amount::from_sat(1), address.script_pubkey());
let tx = rpc.ledger.create_transaction(vec![txin], vec![txout]);
if let Err(e) = rpc.ledger.check_transaction(&tx) {
assert!(false, "{:?}", e);
Expand All @@ -385,7 +381,7 @@ mod tests {

let txout = rpc
.ledger
.create_txout(Amount::from_sat(0x45), Some(address.script_pubkey()));
.create_txout(Amount::from_sat(0x45), address.script_pubkey());
let tx = rpc.ledger.create_transaction(vec![], vec![txout]);
rpc.ledger.add_transaction_unconditionally(tx).unwrap();

Expand Down
24 changes: 9 additions & 15 deletions src/ledger/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,10 @@ impl Ledger {
}
}
/// Creates a `TxOut` with some defaults.
pub fn create_txout(&self, amount: Amount, script_pubkey: Option<ScriptBuf>) -> TxOut {
pub fn create_txout(&self, value: Amount, script_pubkey: ScriptBuf) -> TxOut {
TxOut {
value: amount,
script_pubkey: match script_pubkey {
Some(script_pubkey) => script_pubkey,
None => ScriptBuf::new(),
},
value,
script_pubkey,
}
}
/// Creates a `Transaction` with some defaults.
Expand All @@ -187,7 +184,7 @@ mod tests {

assert_eq!(ledger._get_transactions().len(), 0);

let txout = ledger.create_txout(Amount::from_sat(0x45), Some(ScriptBuf::new()));
let txout = ledger.create_txout(Amount::from_sat(0x45), ScriptBuf::new());
let tx = ledger.create_transaction(vec![], vec![txout]);
let txid = tx.compute_txid();

Expand Down Expand Up @@ -219,7 +216,7 @@ mod tests {
// First, add some funds to user, for free.
let txout = ledger.create_txout(
Amount::from_sat(0x45 * 0x45),
Some(credential.address.script_pubkey()),
credential.address.script_pubkey(),
);
let tx = ledger.create_transaction(vec![], vec![txout.clone()]);
let txid = tx.compute_txid();
Expand All @@ -238,7 +235,7 @@ mod tests {
let txin = ledger._create_txin(txid, 0);
let txout = ledger.create_txout(
Amount::from_sat(0x44 * 0x45),
Some(credential.address.script_pubkey()),
credential.address.script_pubkey(),
);
let tx = ledger.create_transaction(vec![txin], vec![txout]);
let txid = tx.compute_txid();
Expand All @@ -262,10 +259,7 @@ mod tests {
ledger.add_credential(credential.clone());

// Add some funds.
let txout = ledger.create_txout(
Amount::from_sat(0x45),
Some(credential.address.script_pubkey()),
);
let txout = ledger.create_txout(Amount::from_sat(0x45), credential.address.script_pubkey());
let tx = ledger.create_transaction(vec![], vec![txout.clone()]);
let txid = tx.compute_txid();
assert_eq!(
Expand All @@ -291,14 +285,14 @@ mod tests {
fn calculate_transaction_output_value() {
let ledger = Ledger::new();

let txout1 = ledger.create_txout(Amount::from_sat(0x45), None);
let txout1 = ledger.create_txout(Amount::from_sat(0x45), ScriptBuf::new());
let tx = ledger.create_transaction(vec![], vec![txout1.clone()]);
assert_eq!(
ledger.calculate_transaction_output_value(tx),
Amount::from_sat(0x45)
);

let txout2 = ledger.create_txout(Amount::from_sat(0x1F), None);
let txout2 = ledger.create_txout(Amount::from_sat(0x1F), ScriptBuf::new());
let tx = ledger.create_transaction(vec![], vec![txout1, txout2]);
assert_eq!(
ledger.calculate_transaction_output_value(tx),
Expand Down
14 changes: 7 additions & 7 deletions src/ledger/utxo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ impl Ledger {
#[cfg(test)]
mod tests {
use crate::{add_item_to_vec, add_utxo_to_address, ledger::Ledger, remove_utxo_from_address};
use bitcoin::{Amount, OutPoint};
use bitcoin::{Amount, OutPoint, ScriptBuf};

#[test]
fn add_get_utxos() {
Expand All @@ -74,7 +74,7 @@ mod tests {

assert_eq!(ledger.get_utxos(address.clone()).len(), 0);

let txout = ledger.create_txout(Amount::from_sat(0x45), Some(address.script_pubkey()));
let txout = ledger.create_txout(Amount::from_sat(0x45), address.script_pubkey());
let tx = ledger.create_transaction(vec![], vec![txout]);
let txid = tx.compute_txid();

Expand Down Expand Up @@ -108,7 +108,7 @@ mod tests {

assert_eq!(ledger.get_user_utxos().len(), 0);

let txout = ledger.create_txout(Amount::from_sat(0x45), Some(address0.script_pubkey()));
let txout = ledger.create_txout(Amount::from_sat(0x45), address0.script_pubkey());
let tx = ledger.create_transaction(vec![], vec![txout]);
let txid = tx.compute_txid();
let utxo = OutPoint { txid, vout: 0 };
Expand Down Expand Up @@ -175,22 +175,22 @@ mod tests {

assert_eq!(ledger.calculate_balance().unwrap(), Amount::from_sat(0));

let txout = ledger.create_txout(Amount::from_sat(100 - 0x1F), None);
let txout = ledger.create_txout(Amount::from_sat(100 - 0x1F), ScriptBuf::new());
let tx = ledger.create_transaction(vec![], vec![txout]);
let txid = tx.compute_txid();
let utxo = OutPoint { txid, vout: 0 };
add_utxo_to_address!(ledger.utxos, address.clone(), utxo);
add_item_to_vec!(ledger.transactions, tx);

let txout = ledger.create_txout(Amount::from_sat(0x1F), None);
let txout = ledger.create_txout(Amount::from_sat(0x1F), ScriptBuf::new());
let tx = ledger.create_transaction(vec![], vec![txout]);
let txid = tx.compute_txid();
let utxo = OutPoint { txid, vout: 0 };
add_utxo_to_address!(ledger.utxos, address.clone(), utxo);
add_item_to_vec!(ledger.transactions, tx);

let txout1 = ledger.create_txout(Amount::from_sat(100 - 0x1F), None);
let txout2 = ledger.create_txout(Amount::from_sat(0x1F), None);
let txout1 = ledger.create_txout(Amount::from_sat(100 - 0x1F), ScriptBuf::new());
let txout2 = ledger.create_txout(Amount::from_sat(0x1F), ScriptBuf::new());
let tx = ledger.create_transaction(vec![], vec![txout1, txout2]);
let txid = tx.compute_txid();
let utxo1 = OutPoint { txid, vout: 0 };
Expand Down

0 comments on commit 3f41671

Please sign in to comment.