Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better map UtxoErrors to Substrate's InvalidTransaction type #214

Merged
merged 2 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading