Skip to content

Commit

Permalink
bug: fix revert id (#1368)
Browse files Browse the repository at this point in the history
simulating now propagates the correct revert reason
  • Loading branch information
segfault-magnet authored May 10, 2024
1 parent f28f609 commit d5e74fc
Show file tree
Hide file tree
Showing 5 changed files with 240 additions and 166 deletions.
9 changes: 7 additions & 2 deletions packages/fuels-accounts/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ description = "Fuel Rust SDK accounts."
async-trait = { workspace = true, default-features = false }
chrono = { workspace = true }
elliptic-curve = { workspace = true, default-features = false }
eth-keystore = { workspace = true, optional = true }
eth-keystore = { workspace = true, optional = true }
fuel-core-client = { workspace = true, optional = true }
fuel-core-types = { workspace = true }
fuel-crypto = { workspace = true, features = ["random"] }
Expand All @@ -34,4 +34,9 @@ tokio = { workspace = true, features = ["test-util"] }
[features]
default = ["std"]
coin-cache = ["tokio?/time"]
std = ["fuels-core/std", "dep:tokio", "fuel-core-client/default", "dep:eth-keystore"]
std = [
"fuels-core/std",
"dep:tokio",
"fuel-core-client/default",
"dep:eth-keystore",
]
33 changes: 6 additions & 27 deletions packages/fuels-accounts/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use fuel_core_client::client::{
gas_price::{EstimateGasPrice, LatestGasPrice},
},
};
use fuel_core_types::services::executor::{TransactionExecutionResult, TransactionExecutionStatus};
use fuel_tx::{
AssetId, ConsensusParameters, Receipt, Transaction as FuelTransaction, TxId, UtxoId,
};
Expand Down Expand Up @@ -256,12 +255,12 @@ impl Provider {
}

pub async fn dry_run(&self, tx: impl Transaction) -> Result<TxStatus> {
let [(_, tx_status)] = self
let [tx_status] = self
.client
.dry_run(Transactions::new().insert(tx).as_slice())
.await?
.into_iter()
.map(Self::tx_status_from_execution_status)
.map(Into::into)
.collect::<Vec<_>>()
.try_into()
.expect("should have only one element");
Expand All @@ -278,37 +277,17 @@ impl Provider {
.dry_run(transactions.as_slice())
.await?
.into_iter()
.map(Self::tx_status_from_execution_status)
.map(|execution_status| (execution_status.id, execution_status.into()))
.collect())
}

fn tx_status_from_execution_status(
tx_execution_status: TransactionExecutionStatus,
) -> (TxId, TxStatus) {
(
tx_execution_status.id,
match tx_execution_status.result {
TransactionExecutionResult::Success { receipts, .. } => {
TxStatus::Success { receipts }
}
TransactionExecutionResult::Failed {
receipts, result, ..
} => TxStatus::Revert {
reason: TransactionExecutionResult::reason(&receipts, &result),
receipts,
revert_id: 0,
},
},
)
}

pub async fn dry_run_no_validation(&self, tx: impl Transaction) -> Result<TxStatus> {
let [(_, tx_status)] = self
let [tx_status] = self
.client
.dry_run_opt(Transactions::new().insert(tx).as_slice(), Some(false))
.await?
.into_iter()
.map(Self::tx_status_from_execution_status)
.map(Into::into)
.collect::<Vec<_>>()
.try_into()
.expect("should have only one element");
Expand All @@ -325,7 +304,7 @@ impl Provider {
.dry_run_opt(transactions.as_slice(), Some(false))
.await?
.into_iter()
.map(Self::tx_status_from_execution_status)
.map(|execution_status| (execution_status.id, execution_status.into()))
.collect())
}

Expand Down
1 change: 1 addition & 0 deletions packages/fuels-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ fuel-core-client = { workspace = true, optional = true }
fuel-crypto = { workspace = true }
fuel-tx = { workspace = true }
fuel-types = { workspace = true, features = ["default"] }
fuel-core-types = { workspace = true }
fuel-vm = { workspace = true }
fuels-macros = { workspace = true }
hex = { workspace = true, features = ["std"] }
Expand Down
27 changes: 27 additions & 0 deletions packages/fuels-core/src/types/tx_status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use fuel_abi_types::error_codes::{
};
#[cfg(feature = "std")]
use fuel_core_client::client::types::TransactionStatus as ClientTransactionStatus;
#[cfg(feature = "std")]
use fuel_core_types::services::executor::{TransactionExecutionResult, TransactionExecutionStatus};
use fuel_tx::Receipt;
#[cfg(feature = "std")]
use fuel_vm::state::ProgramState;
Expand Down Expand Up @@ -118,3 +120,28 @@ impl From<ClientTransactionStatus> for TxStatus {
}
}
}

#[cfg(feature = "std")]
impl From<TransactionExecutionStatus> for TxStatus {
fn from(value: TransactionExecutionStatus) -> Self {
match value.result {
TransactionExecutionResult::Success { receipts, .. } => Self::Success { receipts },
TransactionExecutionResult::Failed {
result, receipts, ..
} => {
let revert_id = result
.and_then(|result| match result {
ProgramState::Revert(revert_id) => Some(revert_id),
_ => None,
})
.expect("Transaction failed without a `revert_id`");
let reason = TransactionExecutionResult::reason(&receipts, &result);
Self::Revert {
receipts,
reason,
revert_id,
}
}
}
}
}
Loading

0 comments on commit d5e74fc

Please sign in to comment.