Skip to content

Commit 3dd8be3

Browse files
committed
return db_tx from async operations
- return the db_tx from async operations in order for it to be committed or rolled back properly
1 parent 799e098 commit 3dd8be3

File tree

7 files changed

+314
-210
lines changed

7 files changed

+314
-210
lines changed

wallet/src/signer/mod.rs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -112,39 +112,42 @@ type SignerResult<T> = Result<T, SignerError>;
112112
#[async_trait]
113113
pub trait Signer {
114114
/// Sign a partially signed transaction and return the before and after signature statuses.
115-
async fn sign_tx(
115+
async fn sign_tx<T: WalletStorageReadUnlocked + Send>(
116116
&mut self,
117117
tx: PartiallySignedTransaction,
118118
tokens_additional_info: &TokensAdditionalInfo,
119119
key_chain: &(impl AccountKeyChains + Sync),
120-
db_tx: impl WalletStorageReadUnlocked + Send,
120+
db_tx: T,
121121
block_height: BlockHeight,
122-
) -> SignerResult<(
123-
PartiallySignedTransaction,
124-
Vec<SignatureStatus>,
125-
Vec<SignatureStatus>,
126-
)>;
122+
) -> (
123+
T,
124+
SignerResult<(
125+
PartiallySignedTransaction,
126+
Vec<SignatureStatus>,
127+
Vec<SignatureStatus>,
128+
)>,
129+
);
127130

128131
/// Sign an arbitrary message for a destination known to this key chain.
129-
async fn sign_challenge(
132+
async fn sign_challenge<T: WalletStorageReadUnlocked + Send>(
130133
&mut self,
131134
message: &[u8],
132135
destination: &Destination,
133136
key_chain: &(impl AccountKeyChains + Sync),
134-
db_tx: impl WalletStorageReadUnlocked + Send,
135-
) -> SignerResult<ArbitraryMessageSignature>;
137+
db_tx: T,
138+
) -> (T, SignerResult<ArbitraryMessageSignature>);
136139

137140
/// Sign a transaction intent. The number of `input_destinations` must be the same as
138141
/// the number of inputs in the transaction; all of the destinations must be known
139142
/// to this key chain.
140-
async fn sign_transaction_intent(
143+
async fn sign_transaction_intent<T: WalletStorageReadUnlocked + Send>(
141144
&mut self,
142145
transaction: &Transaction,
143146
input_destinations: &[Destination],
144147
intent: &str,
145148
key_chain: &(impl AccountKeyChains + Sync),
146-
db_tx: impl WalletStorageReadUnlocked + Send,
147-
) -> SignerResult<SignedTransactionIntent>;
149+
db_tx: T,
150+
) -> (T, SignerResult<SignedTransactionIntent>);
148151
}
149152

150153
pub trait SignerProvider {

wallet/src/signer/software_signer/mod.rs

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -287,16 +287,12 @@ impl SoftwareSigner {
287287

288288
Ok((current_signatures, previous_status, final_status))
289289
}
290-
}
291290

292-
#[async_trait]
293-
impl Signer for SoftwareSigner {
294-
async fn sign_tx(
291+
fn sign_tx_impl<T: WalletStorageReadUnlocked + Send>(
295292
&mut self,
296293
ptx: PartiallySignedTransaction,
297-
_tokens_additional_info: &TokensAdditionalInfo,
298294
key_chain: &(impl AccountKeyChains + Sync),
299-
db_tx: impl WalletStorageReadUnlocked + Send,
295+
db_tx: &T,
300296
block_height: BlockHeight,
301297
) -> SignerResult<(
302298
PartiallySignedTransaction,
@@ -353,7 +349,7 @@ impl Signer for SoftwareSigner {
353349
&input_commitments,
354350
sig_components,
355351
key_chain,
356-
&db_tx,
352+
db_tx,
357353
)?;
358354

359355
let signature =
@@ -389,7 +385,7 @@ impl Signer for SoftwareSigner {
389385
&input_commitments,
390386
key_chain,
391387
htlc_secret,
392-
&db_tx,
388+
db_tx,
393389
)?;
394390
Ok((sig, SignatureStatus::NotSigned, status))
395391
}
@@ -403,37 +399,66 @@ impl Signer for SoftwareSigner {
403399

404400
Ok((ptx.with_witnesses(witnesses)?, prev_statuses, new_statuses))
405401
}
402+
}
406403

407-
async fn sign_challenge(
404+
#[async_trait]
405+
impl Signer for SoftwareSigner {
406+
async fn sign_tx<T: WalletStorageReadUnlocked + Send>(
407+
&mut self,
408+
ptx: PartiallySignedTransaction,
409+
_tokens_additional_info: &TokensAdditionalInfo,
410+
key_chain: &(impl AccountKeyChains + Sync),
411+
db_tx: T,
412+
block_height: BlockHeight,
413+
) -> (
414+
T,
415+
SignerResult<(
416+
PartiallySignedTransaction,
417+
Vec<SignatureStatus>,
418+
Vec<SignatureStatus>,
419+
)>,
420+
) {
421+
let res = self.sign_tx_impl(ptx, key_chain, &db_tx, block_height);
422+
(db_tx, res)
423+
}
424+
425+
async fn sign_challenge<T: WalletStorageReadUnlocked + Send>(
408426
&mut self,
409427
message: &[u8],
410428
destination: &Destination,
411429
key_chain: &(impl AccountKeyChains + Sync),
412-
db_tx: impl WalletStorageReadUnlocked + Send,
413-
) -> SignerResult<ArbitraryMessageSignature> {
414-
let private_key = self
415-
.get_private_key_for_destination(destination, key_chain, &db_tx)?
416-
.ok_or(SignerError::DestinationNotFromThisWallet)?;
430+
db_tx: T,
431+
) -> (T, SignerResult<ArbitraryMessageSignature>) {
432+
let private_key = match self.get_private_key_for_destination(destination, key_chain, &db_tx)
433+
{
434+
Ok(pk) => pk,
435+
Err(e) => return (db_tx, Err(e)),
436+
};
437+
438+
let private_key = match private_key.ok_or(SignerError::DestinationNotFromThisWallet) {
439+
Ok(pk) => pk,
440+
Err(e) => return (db_tx, Err(e)),
441+
};
417442

418443
let sig = ArbitraryMessageSignature::produce_uniparty_signature(
419444
&private_key,
420445
destination,
421446
message,
422447
self.sig_aux_data_provider.lock().expect("poisoned mutex").as_mut(),
423-
)?;
448+
);
424449

425-
Ok(sig)
450+
(db_tx, sig.map_err(Into::into))
426451
}
427452

428-
async fn sign_transaction_intent(
453+
async fn sign_transaction_intent<T: WalletStorageReadUnlocked + Send>(
429454
&mut self,
430455
transaction: &Transaction,
431456
input_destinations: &[Destination],
432457
intent: &str,
433458
key_chain: &(impl AccountKeyChains + Sync),
434-
db_tx: impl WalletStorageReadUnlocked + Send,
435-
) -> SignerResult<SignedTransactionIntent> {
436-
SignedTransactionIntent::produce_from_transaction(
459+
db_tx: T,
460+
) -> (T, SignerResult<SignedTransactionIntent>) {
461+
let res = SignedTransactionIntent::produce_from_transaction(
437462
transaction,
438463
input_destinations,
439464
intent,
@@ -442,7 +467,9 @@ impl Signer for SoftwareSigner {
442467
.ok_or(SignerError::DestinationNotFromThisWallet)
443468
},
444469
self.sig_aux_data_provider.lock().expect("poisoned mutex").as_mut(),
445-
)
470+
);
471+
472+
(db_tx, res)
446473
}
447474
}
448475

wallet/src/signer/tests/generic_fixed_signature_tests.rs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -373,16 +373,17 @@ pub async fn test_fixed_signatures_generic<MkS, S>(
373373
let orig_ptx = req.into_partially_signed_tx(ptx_additional_info).unwrap();
374374

375375
let mut signer = make_signer(chain_config.clone(), account.account_index());
376-
let (ptx, _, _) = signer
376+
let (db_tx, res) = signer
377377
.sign_tx(
378378
orig_ptx,
379379
&tokens_additional_info,
380380
account.key_chain(),
381381
db_tx,
382382
tx_block_height,
383383
)
384-
.await
385-
.unwrap();
384+
.await;
385+
let (ptx, _, _) = res.unwrap();
386+
db_tx.commit().unwrap();
386387
assert!(ptx.all_signatures_available());
387388

388389
let input_commitments = ptx
@@ -916,34 +917,32 @@ pub async fn test_fixed_signatures_generic2<MkS, S>(
916917
.map(|comm| comm.deep_clone())
917918
.collect_vec();
918919

919-
db_tx.commit().unwrap();
920-
let db_tx = db.transaction_ro_unlocked().await.unwrap();
921920
let mut signer = make_signer(chain_config.clone(), account1.account_index());
922-
let (ptx, _, _) = signer
921+
let (db_tx, res) = signer
923922
.sign_tx(
924923
ptx,
925924
&tokens_additional_info,
926925
account1.key_chain(),
927926
db_tx,
928927
tx_block_height,
929928
)
930-
.await
931-
.unwrap();
929+
.await;
930+
let (ptx, _, _) = res.unwrap();
932931
assert!(ptx.all_signatures_available());
933932

934933
// Fully sign multisig inputs.
935-
let db_tx = db.transaction_ro_unlocked().await.unwrap();
936934
let mut signer = make_signer(chain_config.clone(), account2.account_index());
937-
let (ptx, _, _) = signer
935+
let (db_tx, res) = signer
938936
.sign_tx(
939937
ptx,
940938
&tokens_additional_info,
941939
account2.key_chain(),
942940
db_tx,
943941
tx_block_height,
944942
)
945-
.await
946-
.unwrap();
943+
.await;
944+
let (ptx, _, _) = res.unwrap();
945+
db_tx.commit().unwrap();
947946
assert!(ptx.all_signatures_available());
948947

949948
for (i, dest) in destinations.iter().enumerate() {

0 commit comments

Comments
 (0)