From 2992564793db2910d76513a2be59cdd3f52e325a Mon Sep 17 00:00:00 2001 From: Joshy Orndorff Date: Mon, 22 Apr 2024 11:48:53 -0400 Subject: [PATCH 1/2] impl From trait for conversion --- tuxedo-core/src/types.rs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/tuxedo-core/src/types.rs b/tuxedo-core/src/types.rs index 3ac7cbb7..ef85ae57 100644 --- a/tuxedo-core/src/types.rs +++ b/tuxedo-core/src/types.rs @@ -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. @@ -197,6 +200,20 @@ pub enum UtxoError { 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 From> for InvalidTransaction { + fn from(utxo_error: UtxoError) -> 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 = Result<(), UtxoError>; From d8c91aa3b4ea16fff610c373f9f3df483b80e26e Mon Sep 17 00:00:00 2001 From: Joshy Orndorff Date: Mon, 22 Apr 2024 11:49:05 -0400 Subject: [PATCH 2/2] Use the conversion (and add clearer logs) --- tuxedo-core/src/executive.rs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/tuxedo-core/src/executive.rs b/tuxedo-core/src/executive.rs index 6a21303d..46452d1d 100644 --- a/tuxedo-core/src/executive.rs +++ b/tuxedo-core/src/executive.rs @@ -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(())) } @@ -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()) }) };