Skip to content

Commit 0bd3fca

Browse files
authored
Better map UtxoErrors to Substrate's InvalidTransaction type (#214)
* impl From trait for conversion * Use the conversion (and add clearer logs)
1 parent eed8882 commit 0bd3fca

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

tuxedo-core/src/executive.rs

+10-7
Original file line numberDiff line numberDiff line change
@@ -278,9 +278,15 @@ where
278278
extrinsics.push(extrinsic.encode());
279279
sp_io::storage::set(EXTRINSIC_KEY, &extrinsics.encode());
280280

281-
// Now actually
282-
Self::apply_tuxedo_transaction(extrinsic)
283-
.map_err(|_| TransactionValidityError::Invalid(InvalidTransaction::Custom(0)))?;
281+
// Now actually apply the extrinsic
282+
Self::apply_tuxedo_transaction(extrinsic).map_err(|e| {
283+
log::warn!(
284+
target: LOG_TARGET,
285+
"Tuxedo Transaction did not apply successfully: {:?}",
286+
e,
287+
);
288+
TransactionValidityError::Invalid(e.into())
289+
})?;
284290

285291
Ok(Ok(()))
286292
}
@@ -401,16 +407,13 @@ where
401407
let r = if tx.checker.is_inherent() {
402408
Err(TransactionValidityError::Invalid(InvalidTransaction::Call))
403409
} else {
404-
// TODO, we need a good way to map our UtxoError into the supposedly generic InvalidTransaction
405-
// https://paritytech.github.io/substrate/master/sp_runtime/transaction_validity/enum.InvalidTransaction.html
406-
// For now, I just make them all custom zero, and log the error variant
407410
Self::validate_tuxedo_transaction(&tx).map_err(|e| {
408411
log::warn!(
409412
target: LOG_TARGET,
410413
"Tuxedo Transaction did not validate (in the pool): {:?}",
411414
e,
412415
);
413-
TransactionValidityError::Invalid(InvalidTransaction::Custom(0))
416+
TransactionValidityError::Invalid(e.into())
414417
})
415418
};
416419

tuxedo-core/src/types.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ use parity_scale_codec::{Decode, Encode};
55
use scale_info::TypeInfo;
66
use serde::{Deserialize, Serialize};
77
use sp_core::H256;
8-
use sp_runtime::traits::{BlakeTwo256, Extrinsic, Hash as HashT};
8+
use sp_runtime::{
9+
traits::{BlakeTwo256, Extrinsic, Hash as HashT},
10+
transaction_validity::InvalidTransaction,
11+
};
912
use sp_std::vec::Vec;
1013

1114
// All Tuxedo chains use the same BlakeTwo256 hash.
@@ -197,6 +200,20 @@ pub enum UtxoError<ConstraintCheckerError> {
197200
MissingInput,
198201
}
199202

203+
// Substrate requires this supposedly reusable error type, but it is actually tied pretty tightly
204+
// to the accounts model and some specific FRAME signed extensions. We map it the best we can.
205+
impl<ConstraintCheckerError> From<UtxoError<ConstraintCheckerError>> for InvalidTransaction {
206+
fn from(utxo_error: UtxoError<ConstraintCheckerError>) -> Self {
207+
match utxo_error {
208+
UtxoError::DuplicateInput => InvalidTransaction::Custom(255),
209+
UtxoError::PreExistingOutput => InvalidTransaction::Custom(254),
210+
UtxoError::ConstraintCheckerError(_) => InvalidTransaction::Custom(0),
211+
UtxoError::VerifierError => InvalidTransaction::BadProof,
212+
UtxoError::MissingInput => InvalidTransaction::Future,
213+
}
214+
}
215+
}
216+
200217
/// The Result of dispatching a UTXO transaction.
201218
pub type DispatchResult<ConstraintCheckerError> = Result<(), UtxoError<ConstraintCheckerError>>;
202219

0 commit comments

Comments
 (0)