Skip to content

Commit

Permalink
Better map UtxoErrors to Substrate's InvalidTransaction type (#214)
Browse files Browse the repository at this point in the history
* impl From trait for conversion

* Use the conversion (and add clearer logs)
  • Loading branch information
JoshOrndorff authored Apr 23, 2024
1 parent eed8882 commit 0bd3fca
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
17 changes: 10 additions & 7 deletions tuxedo-core/src/executive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,9 +278,15 @@ where
extrinsics.push(extrinsic.encode());
sp_io::storage::set(EXTRINSIC_KEY, &extrinsics.encode());

// Now actually
Self::apply_tuxedo_transaction(extrinsic)
.map_err(|_| TransactionValidityError::Invalid(InvalidTransaction::Custom(0)))?;
// Now actually apply the extrinsic
Self::apply_tuxedo_transaction(extrinsic).map_err(|e| {
log::warn!(
target: LOG_TARGET,
"Tuxedo Transaction did not apply successfully: {:?}",
e,
);
TransactionValidityError::Invalid(e.into())
})?;

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

Expand Down
19 changes: 18 additions & 1 deletion tuxedo-core/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ use parity_scale_codec::{Decode, Encode};
use scale_info::TypeInfo;
use serde::{Deserialize, Serialize};
use sp_core::H256;
use sp_runtime::traits::{BlakeTwo256, Extrinsic, Hash as HashT};
use sp_runtime::{
traits::{BlakeTwo256, Extrinsic, Hash as HashT},
transaction_validity::InvalidTransaction,
};
use sp_std::vec::Vec;

// All Tuxedo chains use the same BlakeTwo256 hash.
Expand Down Expand Up @@ -197,6 +200,20 @@ pub enum UtxoError<ConstraintCheckerError> {
MissingInput,
}

// Substrate requires this supposedly reusable error type, but it is actually tied pretty tightly
// to the accounts model and some specific FRAME signed extensions. We map it the best we can.
impl<ConstraintCheckerError> From<UtxoError<ConstraintCheckerError>> for InvalidTransaction {
fn from(utxo_error: UtxoError<ConstraintCheckerError>) -> Self {
match utxo_error {
UtxoError::DuplicateInput => InvalidTransaction::Custom(255),
UtxoError::PreExistingOutput => InvalidTransaction::Custom(254),
UtxoError::ConstraintCheckerError(_) => InvalidTransaction::Custom(0),
UtxoError::VerifierError => InvalidTransaction::BadProof,
UtxoError::MissingInput => InvalidTransaction::Future,
}
}
}

/// The Result of dispatching a UTXO transaction.
pub type DispatchResult<ConstraintCheckerError> = Result<(), UtxoError<ConstraintCheckerError>>;

Expand Down

0 comments on commit 0bd3fca

Please sign in to comment.