Skip to content

Commit

Permalink
Merge pull request #292 from nomic-io/pending-fees
Browse files Browse the repository at this point in the history
Push deposit fees to pending queue
  • Loading branch information
mappum authored Sep 6, 2024
2 parents f7c1d3d + 6a041d2 commit b1616c3
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 20 deletions.
10 changes: 7 additions & 3 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ impl InnerApp {
match dest {
Dest::Address(addr) => self.bitcoin.accounts.deposit(addr, nbtc),
Dest::Ibc(dest) => dest.transfer(nbtc, &mut self.bitcoin, &mut self.ibc),
Dest::Fee => Ok(self.bitcoin.give_rewards(nbtc)?),
}
}

Expand Down Expand Up @@ -1015,13 +1016,15 @@ pub struct MsgIbcTransfer {
pub enum Dest {
Address(Address),
Ibc(IbcDest),
Fee,
}

impl Dest {
pub fn to_receiver_addr(&self) -> String {
pub fn to_receiver_addr(&self) -> Option<String> {
match self {
Dest::Address(addr) => addr.to_string(),
Dest::Ibc(dest) => dest.receiver.0.to_string(),
Dest::Address(addr) => Some(addr.to_string()),
Dest::Ibc(dest) => Some(dest.receiver.0.to_string()),
Dest::Fee => None,
}
}
}
Expand Down Expand Up @@ -1112,6 +1115,7 @@ impl Dest {
let bytes = match self {
Address(addr) => addr.bytes().into(),
Ibc(dest) => Sha256::digest(dest.encode()?).to_vec(),
Fee => vec![1],
};

Ok(bytes)
Expand Down
3 changes: 2 additions & 1 deletion src/bin/nomic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1327,7 +1327,8 @@ async fn deposit(
))
})
.await?;
let script = sigset.output_script(dest.commitment_bytes()?.as_slice(), threshold)?;
let commitment_bytes = dest.commitment_bytes()?;
let script = sigset.output_script(&commitment_bytes, threshold)?;
let btc_addr = bitcoin::Address::from_script(&script, nomic::bitcoin::NETWORK).unwrap();

let mut successes = 0;
Expand Down
5 changes: 3 additions & 2 deletions src/bitcoin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,6 @@ impl Bitcoin {

let checkpoint = self.checkpoints.get(sigset_index)?;
let sigset = checkpoint.sigset.clone();

let dest_bytes = dest.commitment_bytes()?;
let expected_script =
sigset.output_script(&dest_bytes, self.checkpoints.config.sigset_threshold)?;
Expand Down Expand Up @@ -662,7 +661,9 @@ impl Bitcoin {
// TODO: keep in excess queue if full

let deposit_fee = nbtc.take(calc_deposit_fee(nbtc.amount.into()))?;
self.give_rewards(deposit_fee)?;
self.checkpoints
.building_mut()?
.insert_pending(Dest::Fee, deposit_fee)?;

self.checkpoints
.building_mut()?
Expand Down
5 changes: 3 additions & 2 deletions src/bitcoin/recovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,18 @@ impl RecoveryTxs {
.output
.get(args.vout as usize)
.ok_or_else(|| Error::Signer("Invalid recovery tx vout".to_string()))?;
let commitment_bytes = args.dest.commitment_bytes()?;

let input = Input::new(
OutPoint::new(args.expired_tx.txid(), args.vout),
args.old_sigset,
&args.dest.commitment_bytes()?,
&commitment_bytes,
expired_output.value,
args.threshold,
)?;
let script_pubkey = args
.new_sigset
.output_script(args.dest.commitment_bytes()?.as_slice(), args.threshold)?;
.output_script(&commitment_bytes, args.threshold)?;
let output = TxOut {
value: expired_output.value,
script_pubkey,
Expand Down
16 changes: 12 additions & 4 deletions src/bitcoin/relayer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,8 +433,12 @@ impl Relayer {
)?;

let mut index = index.lock().await;
let receiver_addr = match dest.to_receiver_addr() {
Some(addr) => addr,
None => continue,
};
index.insert_deposit(
dest.to_receiver_addr(),
receiver_addr,
bitcoin_address,
Deposit::new(txid, vout as u32, output.value, None),
)
Expand Down Expand Up @@ -865,6 +869,10 @@ impl Relayer {
let txid = tx.txid();
let outpoint = (txid.into_inner(), output.vout);
let dest = output.dest.clone();
if dest.to_receiver_addr().is_none() {
return Ok(());
}
let receiver_addr = dest.to_receiver_addr().unwrap();
let vout = output.vout;
let contains_outpoint = app_client(&self.app_client_addr)
.query(|app| app.bitcoin.processed_outpoints.contains(outpoint))
Expand All @@ -877,13 +885,13 @@ impl Relayer {

if contains_outpoint {
let mut index = index.lock().await;
index.remove_deposit(dest.to_receiver_addr(), deposit_address, txid, vout)?;
index.remove_deposit(receiver_addr, deposit_address, txid, vout)?;
return Ok(());
}

let mut index_guard = index.lock().await;
index_guard.insert_deposit(
dest.to_receiver_addr(),
receiver_addr,
deposit_address.clone(),
Deposit::new(
txid,
Expand Down Expand Up @@ -1188,7 +1196,7 @@ impl WatchedScripts {
sigset: &SignatorySet,
threshold: (u64, u64),
) -> Result<::bitcoin::Script> {
sigset.output_script(dest.commitment_bytes()?.as_slice(), threshold)
sigset.output_script(&dest.commitment_bytes()?, threshold)
}
}

Expand Down
16 changes: 8 additions & 8 deletions tests/bitcoin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -458,14 +458,6 @@ async fn bitcoin_test() {
let balance = poll_for_updated_balance(funded_accounts[1].address, expected_balance).await;
assert_eq!(balance, Amount::from(expected_balance));

withdraw_bitcoin(
&funded_accounts[0],
bitcoin::Amount::from_sat(7000),
&withdraw_address,
)
.await
.unwrap();

app_client()
.with_wallet(funded_accounts[0].wallet.clone())
.call(
Expand All @@ -474,6 +466,14 @@ async fn bitcoin_test() {
)
.await?;

withdraw_bitcoin(
&funded_accounts[0],
bitcoin::Amount::from_sat(7000),
&withdraw_address,
)
.await
.unwrap();

btc_client
.generate_to_address(4, &async_wallet_address)
.await
Expand Down

0 comments on commit b1616c3

Please sign in to comment.