Skip to content

Commit

Permalink
Merge pull request #250 from bennyhodl/bitcoin-amount
Browse files Browse the repository at this point in the history
Use `bitcoin::Amount` over u64
  • Loading branch information
Tibo-lg authored Dec 17, 2024
2 parents f026fc1 + b939cc1 commit 0ea7c72
Show file tree
Hide file tree
Showing 38 changed files with 531 additions and 466 deletions.
6 changes: 4 additions & 2 deletions bitcoin-rpc-provider/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use bitcoin::hashes::serde;
use bitcoin::psbt::Psbt;
use bitcoin::secp256k1::rand::thread_rng;
use bitcoin::secp256k1::SecretKey;
use bitcoin::Amount;
use bitcoin::{consensus::Decodable, Network, PrivateKey, Transaction, Txid};
use bitcoin::{secp256k1::PublicKey, Address, OutPoint, ScriptBuf, TxOut};
use bitcoincore_rpc::jsonrpc::serde_json;
Expand Down Expand Up @@ -276,7 +277,7 @@ impl Wallet for BitcoinCoreProvider {

fn get_utxos_for_amount(
&self,
amount: u64,
amount: Amount,
_fee_rate: u64,
lock_utxos: bool,
) -> Result<Vec<Utxo>, ManagerError> {
Expand Down Expand Up @@ -312,7 +313,8 @@ impl Wallet for BitcoinCoreProvider {
})
.collect::<Result<Vec<UtxoWrap>, Error>>()?;
// TODO(tibo): properly compute the cost of change
let selection = select_coins(amount, 20, &mut utxo_pool).ok_or(Error::NotEnoughCoins)?;
let selection =
select_coins(amount.to_sat(), 20, &mut utxo_pool).ok_or(Error::NotEnoughCoins)?;

if lock_utxos {
let outputs: Vec<_> = selection.iter().map(|x| x.0.outpoint).collect();
Expand Down
35 changes: 22 additions & 13 deletions dlc-manager/benches/benchmarks.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use bitcoin::hashes::Hash;
use bitcoin::Amount;
use bitcoin::OutPoint;
use bitcoin::ScriptBuf;
use bitcoin::WPubkeyHash;
Expand Down Expand Up @@ -49,7 +50,7 @@ const THRESHOLD: usize = 2;
/// The ID of the event.
const EVENT_ID: &str = "Test";
/// The total collateral value locked in the contract.
const TOTAL_COLLATERAL: u64 = 200000000;
const TOTAL_COLLATERAL: Amount = Amount::from_sat(200000000);

fn max_value() -> u32 {
BASE.pow(NB_DIGITS as u32) - 1
Expand All @@ -71,12 +72,12 @@ fn create_contract_descriptor() -> ContractDescriptor {
PolynomialPayoutCurvePiece::new(vec![
PayoutPoint {
event_outcome: 0,
outcome_payout: 0,
outcome_payout: Amount::ZERO,
extra_precision: 0,
},
PayoutPoint {
event_outcome: FLOOR,
outcome_payout: 0,
outcome_payout: Amount::ZERO,
extra_precision: 0,
},
])
Expand All @@ -86,7 +87,7 @@ fn create_contract_descriptor() -> ContractDescriptor {
PolynomialPayoutCurvePiece::new(vec![
PayoutPoint {
event_outcome: FLOOR,
outcome_payout: 0,
outcome_payout: Amount::ZERO,
extra_precision: 0,
},
PayoutPoint {
Expand Down Expand Up @@ -189,8 +190,8 @@ fn create_transactions(payouts: &[Payout]) -> DlcTransactions {
payout_script_pubkey: get_p2wpkh_script_pubkey(),
payout_serial_id: 1,
inputs: create_txinputinfo_vec(),
input_amount: 300000000,
collateral: 100000000,
input_amount: Amount::from_sat(300000000),
collateral: Amount::from_sat(100000000),
};

let accept_params = PartyParams {
Expand All @@ -200,8 +201,8 @@ fn create_transactions(payouts: &[Payout]) -> DlcTransactions {
payout_script_pubkey: get_p2wpkh_script_pubkey(),
payout_serial_id: 1,
inputs: create_txinputinfo_vec(),
input_amount: 300000000,
collateral: 100000000,
input_amount: Amount::from_sat(300000000),
collateral: Amount::from_sat(100000000),
};
create_dlc_transactions(&offer_params, &accept_params, payouts, 1000, 2, 0, 1000, 3).unwrap()
}
Expand All @@ -221,7 +222,11 @@ fn offer_seckey() -> SecretKey {
/// Benchmark to measure the adaptor signature creation time.
pub fn sign_bench(c: &mut Criterion) {
let contract_info = create_contract_info();
let dlc_transactions = create_transactions(&contract_info.get_payouts(200000000).unwrap());
let dlc_transactions = create_transactions(
&contract_info
.get_payouts(Amount::from_sat(200000000))
.unwrap(),
);
let fund_output_value = dlc_transactions.get_fund_output().value;

let seckey = accept_seckey();
Expand All @@ -234,7 +239,7 @@ pub fn sign_bench(c: &mut Criterion) {
TOTAL_COLLATERAL,
&seckey,
&dlc_transactions.funding_script_pubkey,
fund_output_value.to_sat(),
fund_output_value,
&dlc_transactions.cets,
0,
)
Expand All @@ -247,7 +252,11 @@ pub fn sign_bench(c: &mut Criterion) {
/// Benchmark to measure the adaptor signature verification time.
pub fn verify_bench(c: &mut Criterion) {
let contract_info = create_contract_info();
let dlc_transactions = create_transactions(&contract_info.get_payouts(200000000).unwrap());
let dlc_transactions = create_transactions(
&contract_info
.get_payouts(Amount::from_sat(200000000))
.unwrap(),
);
let fund_output_value = dlc_transactions.get_fund_output().value;

let seckey = accept_seckey();
Expand All @@ -258,7 +267,7 @@ pub fn verify_bench(c: &mut Criterion) {
TOTAL_COLLATERAL,
&seckey,
&dlc_transactions.funding_script_pubkey,
fund_output_value.to_sat(),
fund_output_value,
&dlc_transactions.cets,
0,
)
Expand All @@ -272,7 +281,7 @@ pub fn verify_bench(c: &mut Criterion) {
SECP256K1,
&pubkey,
&dlc_transactions.funding_script_pubkey,
fund_output_value.to_sat(),
fund_output_value,
&dlc_transactions.cets,
adaptor_signatures,
0,
Expand Down
36 changes: 18 additions & 18 deletions dlc-manager/src/channel/signed_channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//! transaction inputs. This module contains the model for a signed channel,
//! the possible states in which it can be as well as methods to work with it.
use bitcoin::{ScriptBuf, Transaction};
use bitcoin::{Amount, ScriptBuf, Transaction};
use dlc::PartyParams;
use lightning::ln::chan_utils::CounterpartyCommitmentSecrets;
use secp256k1_zkp::{ecdsa::Signature, EcdsaAdaptorSignature, PublicKey};
Expand Down Expand Up @@ -108,15 +108,15 @@ typed_enum!(
/// state change.
is_offer: bool,
/// The total amount of collateral in the channel
total_collateral: u64,
total_collateral: Amount,
/// Keys Id for generating the signers
keys_id: KeysId,
},
/// A [`SignedChannel`] is in `SettledOffered` state when the local party
/// has sent a [`dlc_messages::channel::SettleOffer`] message.
SettledOffered {
/// The payout that was proposed to the counter party.
counter_payout: u64,
counter_payout: Amount,
/// The per update point that the local party would use for the next
/// channel state.
next_per_update_point: PublicKey,
Expand All @@ -130,9 +130,9 @@ typed_enum!(
/// has received a [`dlc_messages::channel::SettleOffer`] message.
SettledReceived {
/// The payout that was proposed to the local party to settle the channel.
own_payout: u64,
own_payout: Amount,
/// The payout that was proposed to the counter party.
counter_payout: u64,
counter_payout: Amount,
/// The per update point to be used by the counter party for the setup
/// of the next channel state.
counter_next_per_update_point: PublicKey,
Expand All @@ -157,9 +157,9 @@ typed_enum!(
/// unresponsive and the channel will be forced closed.
timeout: u64,
/// The payout to the local party after settling the channel.
own_payout: u64,
own_payout: Amount,
/// The payout that was proposed to the counter party.
counter_payout: u64,
counter_payout: Amount,
/// Keys Id for generating the signers
keys_id: KeysId,
},
Expand All @@ -184,9 +184,9 @@ typed_enum!(
/// unresponsive and the channel will be forced closed.
timeout: u64,
/// The payout to the local party after settling the channel.
own_payout: u64,
own_payout: Amount,
/// The payout that was proposed to the counter party.
counter_payout: u64,
counter_payout: Amount,
/// Keys Id for generating the signers
keys_id: KeysId,
},
Expand All @@ -203,9 +203,9 @@ typed_enum!(
/// local party.
own_settle_adaptor_signature: EcdsaAdaptorSignature,
/// The amount the local party holds in the channel.
own_payout: u64,
own_payout: Amount,
/// The amount the counter party holds in the channel.
counter_payout: u64,
counter_payout: Amount,
/// Keys Id for generating the signers
keys_id: KeysId,
},
Expand All @@ -215,7 +215,7 @@ typed_enum!(
/// The temporary [`crate::ContractId`] of the offered contract.
offered_contract_id: ContractId,
/// The payout offered to settle the previous channel state.
counter_payout: u64,
counter_payout: Amount,
/// The per update point to be used by the offer party for the setup
/// of the next channel state.
offer_next_per_update_point: PublicKey,
Expand Down Expand Up @@ -246,7 +246,7 @@ typed_enum!(
/// unresponsive and the channel will be forced closed.
timeout: u64,
/// The payout to the local party attributed for closing the previous state.
own_payout: u64,
own_payout: Amount,
/// Keys Id for generating the signers
keys_id: KeysId,
},
Expand All @@ -272,9 +272,9 @@ typed_enum!(
/// unresponsive and the channel will be forced closed.
timeout: u64,
/// The payout to the local party attributed for closing the previous state.
own_payout: u64,
own_payout: Amount,
/// The total amount of collateral in the channel.
total_collateral: u64,
total_collateral: Amount,
/// Keys Id for generating the signers
keys_id: KeysId,
},
Expand All @@ -299,9 +299,9 @@ typed_enum!(
/// unresponsive and the channel will be forced closed.
timeout: u64,
/// The payout to the local party attributed for closing the previous state.
own_payout: u64,
own_payout: Amount,
/// The total amount of collateral in the channel.
total_collateral: u64,
total_collateral: Amount,
/// Keys Id for generating the signers
keys_id: KeysId,
},
Expand All @@ -323,7 +323,7 @@ typed_enum!(
/// has sent a [`dlc_messages::channel::CollaborativeCloseOffer`] message.
CollaborativeCloseOffered {
/// The payout offered to the counter party to close the channel.
counter_payout: u64,
counter_payout: Amount,
/// The signature of the local party for the closing transaction.
offer_signature: Signature,
/// The closing transaction.
Expand Down
Loading

0 comments on commit 0ea7c72

Please sign in to comment.