Skip to content

Commit cd4ddaf

Browse files
committed
reworked locking notes approach
1 parent 60eba1e commit cd4ddaf

File tree

1 file changed

+46
-22
lines changed

1 file changed

+46
-22
lines changed

mm2src/coins/z_coin.rs

+46-22
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,13 @@ pub struct ZcoinTxDetails {
266266
internal_id: i64,
267267
}
268268

269+
struct GenTxData<'a> {
270+
tx: ZTransaction,
271+
data: AdditionalTxData,
272+
sync_guard: SaplingSyncGuard<'a>,
273+
rseeds: Vec<String>,
274+
}
275+
269276
impl ZCoin {
270277
#[inline]
271278
pub fn utxo_rpc_client(&self) -> &UtxoRpcClientEnum { &self.utxo_arc.rpc_client }
@@ -375,11 +382,7 @@ impl ZCoin {
375382
}
376383
}
377384

378-
async fn gen_tx(
379-
&self,
380-
t_outputs: Vec<TxOut>,
381-
z_outputs: Vec<ZOutput>,
382-
) -> Result<(ZTransaction, AdditionalTxData, SaplingSyncGuard<'_>), MmError<GenTxError>> {
385+
async fn gen_tx(&self, t_outputs: Vec<TxOut>, z_outputs: Vec<ZOutput>) -> Result<GenTxData, MmError<GenTxError>> {
383386
const MAX_RETRIES: usize = 40;
384387
const RETRY_DELAY: f64 = 15.0;
385388
let mut retries = 0;
@@ -439,7 +442,7 @@ impl ZCoin {
439442
&self,
440443
t_outputs: Vec<TxOut>,
441444
z_outputs: Vec<ZOutput>,
442-
) -> Result<(ZTransaction, AdditionalTxData, SaplingSyncGuard<'_>), MmError<GenTxError>> {
445+
) -> Result<GenTxData, MmError<GenTxError>> {
443446
let sync_guard = self.wait_for_gen_tx_blockchain_sync().await?;
444447

445448
let tx_fee = self.get_one_kbyte_tx_fee().await?;
@@ -462,15 +465,15 @@ impl ZCoin {
462465

463466
let mut tx_builder = ZTxBuilder::new(self.consensus_params(), sync_guard.respawn_guard.current_block());
464467

465-
let mut selected_notes_rseed: Vec<String> = vec![];
468+
let mut rseeds: Vec<String> = vec![];
466469
for spendable_note in spendable_notes {
467470
let rseed = match &spendable_note.rseed {
468471
Rseed::BeforeZip212(rcm) => rcm.to_string(),
469472
Rseed::AfterZip212(rseed) => {
470473
jubjub::Fr::from_bytes_wide(prf_expand(rseed, &[0x04]).as_array()).to_string()
471474
},
472475
};
473-
selected_notes_rseed.push(rseed);
476+
rseeds.push(rseed);
474477

475478
total_input_amount += big_decimal_from_sat_unsigned(spendable_note.note_value.into(), self.decimals());
476479

@@ -547,31 +550,33 @@ impl ZCoin {
547550
.await?
548551
.tx_result?;
549552

550-
info!("rseeds :{:?}", selected_notes_rseed);
551-
for rseed in selected_notes_rseed {
552-
info!("saving tx notes rseed!");
553-
self.z_fields
554-
.locked_notes_db
555-
.insert_note(tx.txid().to_string(), rseed)
556-
.await?;
557-
}
558-
559-
let additional_data = AdditionalTxData {
553+
let data = AdditionalTxData {
560554
received_by_me,
561555
spent_by_me: sat_from_big_decimal(&total_input_amount, self.decimals())?,
562556
fee_amount: sat_from_big_decimal(&tx_fee, self.decimals())?,
563557
unused_change: 0,
564558
kmd_rewards: None,
565559
};
566-
Ok((tx, additional_data, sync_guard))
560+
561+
Ok(GenTxData {
562+
tx,
563+
data,
564+
sync_guard,
565+
rseeds,
566+
})
567567
}
568568

569569
pub async fn send_outputs(
570570
&self,
571571
t_outputs: Vec<TxOut>,
572572
z_outputs: Vec<ZOutput>,
573573
) -> Result<ZTransaction, MmError<SendOutputsErr>> {
574-
let (tx, _, mut sync_guard) = self.gen_tx(t_outputs, z_outputs).await?;
574+
let GenTxData {
575+
tx,
576+
rseeds,
577+
mut sync_guard,
578+
..
579+
} = self.gen_tx(t_outputs, z_outputs).await?;
575580
let mut tx_bytes = Vec::with_capacity(1024);
576581
tx.write(&mut tx_bytes).expect("Write should not fail");
577582

@@ -580,6 +585,15 @@ impl ZCoin {
580585
.compat()
581586
.await?;
582587

588+
for rseed in rseeds {
589+
info!("saving tx notes rseed for {}!", tx.txid().to_string());
590+
self.z_fields
591+
.locked_notes_db
592+
.insert_note(tx.txid().to_string(), rseed)
593+
.await
594+
.mm_err(|err| SendOutputsErr::InternalError(err.to_string()))?;
595+
}
596+
583597
sync_guard.respawn_guard.watch_for_tx(tx.txid());
584598
Ok(tx)
585599
}
@@ -1987,7 +2001,7 @@ impl InitWithdrawCoin for ZCoin {
19872001
memo,
19882002
};
19892003

1990-
let (tx, data, _sync_guard) = self.gen_tx(vec![], vec![z_output]).await?;
2004+
let GenTxData { tx, data, rseeds, .. } = self.gen_tx(vec![], vec![z_output]).await?;
19912005
let mut tx_bytes = Vec::with_capacity(1024);
19922006
tx.write(&mut tx_bytes)
19932007
.map_to_mm(|e| WithdrawError::InternalError(e.to_string()))?;
@@ -1997,8 +2011,18 @@ impl InitWithdrawCoin for ZCoin {
19972011
let received_by_me = big_decimal_from_sat_unsigned(data.received_by_me, self.decimals());
19982012
let spent_by_me = big_decimal_from_sat_unsigned(data.spent_by_me, self.decimals());
19992013

2014+
let tx_hash_hex = hex::encode(&tx_hash);
2015+
for rseed in rseeds {
2016+
info!("saving tx notes rseed for {}!", tx_hash_hex);
2017+
self.z_fields
2018+
.locked_notes_db
2019+
.insert_note(tx.txid().to_string(), rseed)
2020+
.await
2021+
.mm_err(|err| WithdrawError::InternalError(err.to_string()))?;
2022+
}
2023+
20002024
Ok(TransactionDetails {
2001-
tx: TransactionData::new_signed(tx_bytes.into(), hex::encode(&tx_hash)),
2025+
tx: TransactionData::new_signed(tx_bytes.into(), tx_hash_hex),
20022026
from: vec![self.z_fields.my_z_addr_encoded.clone()],
20032027
to: vec![req.to],
20042028
my_balance_change: &received_by_me - &spent_by_me,

0 commit comments

Comments
 (0)