Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit b9aa695

Browse files
committed
token-client: Check simulation results for error
#### Problem If the simulation for compute unit limit fails in the token-cli, it may continue to try to broadcast a transaction since the error falls through. Although that transaction should fail too, at least we don't run the risk of broadcasting something known to be bad. #### Summary of changes Add an `err()` function on the simulation results, and abort if the simulation for compute units fails.
1 parent 4349310 commit b9aa695

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

token/client/src/client.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ pub trait SimulateTransaction {
2626
/// Trait for the output of a simulation
2727
pub trait SimulationResult {
2828
fn get_compute_units_consumed(&self) -> ProgramClientResult<u64>;
29+
fn err(self) -> Option<ProgramClientError>;
2930
}
3031

3132
/// Extends basic `SendTransaction` trait with function `send` where client is
@@ -78,6 +79,9 @@ impl SimulationResult for BanksTransactionResultWithSimulation {
7879
.map(|x| x.units_consumed)
7980
.ok_or("No simulation results found".into())
8081
}
82+
fn err(self) -> Option<ProgramClientError> {
83+
self.result.and_then(|r| r.err().map(|e| e.into()))
84+
}
8185
}
8286

8387
impl SimulateTransaction for ProgramBanksClientProcessTransaction {
@@ -165,6 +169,15 @@ impl SimulationResult for RpcClientResponse {
165169
.ok_or("No simulation results found".into()),
166170
}
167171
}
172+
fn err(self) -> Option<ProgramClientError> {
173+
match self {
174+
// `Transaction` is the result of an offline simulation. The error
175+
// should be properly handled by a caller that supports offline
176+
// signing
177+
Self::Signature(_) | Self::Transaction(_) => Some("Not a simulation result".into()),
178+
Self::Simulation(simulation_result) => simulation_result.err.map(|e| e.into()),
179+
}
180+
}
168181
}
169182

170183
impl SimulateTransaction for ProgramRpcClientSendTransaction {

token/client/src/token.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,9 @@ where
588588
let units_consumed = simulation_result
589589
.get_compute_units_consumed()
590590
.map_err(TokenError::Client)?;
591+
if let Some(err) = simulation_result.err() {
592+
return Err(TokenError::Client(err));
593+
}
591594
// Overwrite the compute unit limit instruction with the actual units consumed
592595
let compute_unit_limit =
593596
u32::try_from(units_consumed).map_err(|x| TokenError::Client(x.into()))?;

0 commit comments

Comments
 (0)