Skip to content

Commit

Permalink
fuel-client: default to Unknown transaction type
Browse files Browse the repository at this point in the history
  • Loading branch information
hal3e committed Sep 3, 2024
1 parent 5a6eed0 commit df9f96d
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 16 deletions.
2 changes: 1 addition & 1 deletion crates/client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1106,7 +1106,7 @@ impl FuelClient {
pub async fn transparent_transaction(
&self,
id: &TxId,
) -> io::Result<Option<Transaction>> {
) -> io::Result<Option<types::TransactionType>> {
let query = schema::tx::TransactionQuery::build(TxIdArgs { id: (*id).into() });

let transaction = self.query(query).await?.transaction;
Expand Down
56 changes: 53 additions & 3 deletions crates/client/src/client/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,14 @@ use crate::client::schema::{
};
use fuel_core_types::{
fuel_tx::{
Blob as BlobTx,
Create,
Mint,
Receipt,
Script,
Transaction,
Upgrade,
Upload,
},
fuel_types::{
canonical::Deserialize,
Expand Down Expand Up @@ -90,10 +96,53 @@ pub mod primitives {

#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct TransactionResponse {
pub transaction: Transaction,
pub transaction: TransactionType,
pub status: TransactionStatus,
}

#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[allow(clippy::large_enum_variant)]
pub enum TransactionType {
Script(Script),
Create(Create),
Mint(Mint),
Upgrade(Upgrade),
Upload(Upload),
Blob(BlobTx),
Unknown,
}

impl From<Transaction> for TransactionType {
fn from(value: Transaction) -> Self {
match value {
Transaction::Script(tx) => Self::Script(tx),
Transaction::Create(tx) => Self::Create(tx),
Transaction::Mint(tx) => Self::Mint(tx),
Transaction::Upgrade(tx) => Self::Upgrade(tx),
Transaction::Upload(tx) => Self::Upload(tx),
Transaction::Blob(tx) => Self::Blob(tx),
}
}
}

impl TryFrom<TransactionType> for Transaction {
type Error = ConversionError;

fn try_from(value: TransactionType) -> Result<Self, ConversionError> {
match value {
TransactionType::Script(tx) => Ok(Self::Script(tx)),
TransactionType::Create(tx) => Ok(Self::Create(tx)),
TransactionType::Mint(tx) => Ok(Self::Mint(tx)),
TransactionType::Upgrade(tx) => Ok(Self::Upgrade(tx)),
TransactionType::Upload(tx) => Ok(Self::Upload(tx)),
TransactionType::Blob(tx) => Ok(Self::Blob(tx)),
TransactionType::Unknown => {
Err(ConversionError::UnknownVariant("Transaction"))
}
}
}
}

#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum TransactionStatus {
Submitted {
Expand Down Expand Up @@ -177,8 +226,9 @@ impl TryFrom<OpaqueTransactionWithStatus> for TransactionResponse {

fn try_from(value: OpaqueTransactionWithStatus) -> Result<Self, Self::Error> {
let bytes = value.raw_payload.0 .0;
let tx: Transaction = Transaction::from_bytes(bytes.as_slice())
.map_err(ConversionError::TransactionFromBytesError)?;
let tx: TransactionType = Transaction::from_bytes(bytes.as_slice())
.map(Into::into)
.unwrap_or(TransactionType::Unknown);
let status = value
.status
.ok_or_else(|| ConversionError::MissingField("status".to_string()))?
Expand Down
6 changes: 4 additions & 2 deletions tests/tests/tx/predicates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,15 @@ async fn transaction_with_valid_predicate_is_executed() {
.unwrap();

// check transaction change amount to see if predicate was spent
let transaction = context
let transaction: Transaction = context
.client
.transaction(&predicate_tx.id(&ChainId::default()))
.await
.unwrap()
.unwrap()
.transaction;
.transaction
.try_into()
.unwrap();

assert!(
matches!(transaction.as_script().unwrap().outputs()[0], Output::Change { amount: change_amount, .. } if change_amount == amount)
Expand Down
26 changes: 18 additions & 8 deletions tests/tests/tx/tx_pointer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use fuel_core_types::{
Input,
Output,
Script,
Transaction,
TransactionBuilder,
TxPointer,
UniqueIdentifier,
Expand Down Expand Up @@ -80,12 +81,14 @@ async fn tx_pointer_set_from_genesis_for_coin_and_contract_inputs() {
client.submit_and_await_commit(&tx).await.unwrap();

// verify that the tx returned from the api has tx pointers set matching the genesis config
let ret_tx = client
let ret_tx: Transaction = client
.transaction(&tx.id(&Default::default()))
.await
.unwrap()
.unwrap()
.transaction;
.transaction
.try_into()
.unwrap();
let ret_script = ret_tx.as_script().unwrap();

let coin_input = &ret_script.inputs()[0];
Expand Down Expand Up @@ -136,12 +139,14 @@ async fn tx_pointer_set_from_previous_block() {
let tx1 = tx1.into();
client.submit_and_await_commit(&tx1).await.unwrap();
let next_block_height_after_genesis = new_genesis_block_height + 1;
let ret_tx1 = client
let ret_tx1: Transaction = client
.transaction(&tx1.id(&Default::default()))
.await
.unwrap()
.unwrap()
.transaction;
.transaction
.try_into()
.unwrap();
let ret_tx1 = ret_tx1.as_script().unwrap();

// setup a second transaction that uses UTXOs from tx1
Expand All @@ -155,12 +160,14 @@ async fn tx_pointer_set_from_previous_block() {
let tx2 = tx2.into();
client.submit_and_await_commit(&tx2).await.unwrap();

let ret_tx2 = client
let ret_tx2: Transaction = client
.transaction(&tx2.id(&Default::default()))
.await
.unwrap()
.unwrap()
.transaction;
.transaction
.try_into()
.unwrap();

let ret_tx2 = ret_tx2.as_script().unwrap();

Expand Down Expand Up @@ -204,12 +211,15 @@ async fn tx_pointer_unset_when_utxo_validation_disabled() {
let tx = script.into();
client.submit_and_await_commit(&tx).await.unwrap();

let ret_tx = client
let ret_tx: Transaction = client
.transaction(&tx.id(&Default::default()))
.await
.unwrap()
.unwrap()
.transaction;
.transaction
.try_into()
.unwrap();

let ret_tx = ret_tx.as_script().unwrap();
// verify coin input tx_pointer is null
assert_eq!(
Expand Down
6 changes: 4 additions & 2 deletions tests/tests/tx/utxo_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,14 @@ async fn submit_utxo_verified_tx_with_min_gas_price() {
let tx = tx.into();
client.submit_and_await_commit(&tx).await.unwrap();
// verify that the tx returned from the api matches the submitted tx
let ret_tx = client
let ret_tx: Transaction = client
.transaction(&tx.id(&ChainId::default()))
.await
.unwrap()
.unwrap()
.transaction;
.transaction
.try_into()
.unwrap();

let transaction_result = client
.transaction_status(&ret_tx.id(&ChainId::default()))
Expand Down

0 comments on commit df9f96d

Please sign in to comment.