Skip to content

Commit d4be8bb

Browse files
committed
Use unwrap() in errors due to bugs in the Go<->Rust interface
* panicking will show the line number where the problem happens whereas otherwise we will get an error with no context at all * We anyhow capture the panics before returning to Go and thus, the process won't stop. Further context on why it's OK to use unwrap in these cases https://blog.burntsushi.net/unwrap/ Ideally we would have simply have a way to attach a backtrace/line-number to these errors instead, but I don't think that will be possible until rust-lang/rust#53487 is ready.
1 parent fc92f08 commit d4be8bb

File tree

1 file changed

+32
-37
lines changed
  • cmd/soroban-rpc/lib/preflight/src

1 file changed

+32
-37
lines changed

cmd/soroban-rpc/lib/preflight/src/lib.rs

Lines changed: 32 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ use soroban_env_host::xdr::{
1616
AccountId, InvokeHostFunctionOp, LedgerFootprint, OperationBody, ReadXdr, WriteXdr,
1717
};
1818
use soroban_env_host::LedgerInfo;
19-
use std::convert::{TryFrom, TryInto};
2019
use std::ffi::{CStr, CString};
2120
use std::mem;
2221
use std::panic;
@@ -36,12 +35,10 @@ pub struct CLedgerInfo {
3635
pub autobump_ledgers: u32,
3736
}
3837

39-
impl TryFrom<CLedgerInfo> for LedgerInfo {
40-
type Error = anyhow::Error;
41-
42-
fn try_from(c: CLedgerInfo) -> Result<Self> {
43-
let network_passphrase = from_c_string(c.network_passphrase)?;
44-
Ok(Self {
38+
impl From<CLedgerInfo> for LedgerInfo {
39+
fn from(c: CLedgerInfo) -> Self {
40+
let network_passphrase = from_c_string(c.network_passphrase);
41+
Self {
4542
protocol_version: c.protocol_version,
4643
sequence_number: c.sequence_number,
4744
timestamp: c.timestamp,
@@ -51,7 +48,7 @@ impl TryFrom<CLedgerInfo> for LedgerInfo {
5148
min_persistent_entry_expiration: c.min_persistent_entry_expiration,
5249
max_entry_expiration: c.max_entry_expiration,
5350
autobump_ledgers: c.autobump_ledgers,
54-
})
51+
}
5552
}
5653
}
5754

@@ -69,30 +66,28 @@ pub struct CPreflightResult {
6966
pub pre_restore_min_fee: i64, // Minimum recommended resource fee for a prerequired RestoreFootprint operation
7067
}
7168

72-
impl TryFrom<PreflightResult> for CPreflightResult {
73-
type Error = anyhow::Error;
74-
75-
fn try_from(p: PreflightResult) -> Result<Self> {
69+
impl From<PreflightResult> for CPreflightResult {
70+
fn from(p: PreflightResult) -> Self {
7671
let mut result = Self {
7772
error: null_mut(),
78-
auth: xdr_vec_to_base64_c_null_terminated_char_array(p.auth)?,
73+
auth: xdr_vec_to_base64_c_null_terminated_char_array(p.auth),
7974
result: match p.result {
8075
None => null_mut(),
81-
Some(v) => xdr_to_base64_c(v)?,
76+
Some(v) => xdr_to_base64_c(v),
8277
},
83-
transaction_data: xdr_to_base64_c(p.transaction_data)?,
78+
transaction_data: xdr_to_base64_c(p.transaction_data),
8479
min_fee: p.min_fee,
85-
events: xdr_vec_to_base64_c_null_terminated_char_array(p.events)?,
80+
events: xdr_vec_to_base64_c_null_terminated_char_array(p.events),
8681
cpu_instructions: p.cpu_instructions,
8782
memory_bytes: p.memory_bytes,
8883
pre_restore_transaction_data: null_mut(),
8984
pre_restore_min_fee: 0,
9085
};
9186
if let Some(p) = p.restore_preamble {
9287
result.pre_restore_min_fee = p.min_fee;
93-
result.pre_restore_transaction_data = xdr_to_base64_c(p.transaction_data)?;
88+
result.pre_restore_transaction_data = xdr_to_base64_c(p.transaction_data);
9489
};
95-
Ok(result)
90+
result
9691
}
9792
}
9893

@@ -122,18 +117,18 @@ fn preflight_invoke_hf_op_or_maybe_panic(
122117
source_account: *const libc::c_char, // AccountId XDR in base64
123118
ledger_info: CLedgerInfo,
124119
) -> Result<CPreflightResult> {
125-
let invoke_hf_op = InvokeHostFunctionOp::from_xdr_base64(from_c_string(invoke_hf_op)?)?;
126-
let source_account = AccountId::from_xdr_base64(from_c_string(source_account)?)?;
120+
let invoke_hf_op = InvokeHostFunctionOp::from_xdr_base64(from_c_string(invoke_hf_op)).unwrap();
121+
let source_account = AccountId::from_xdr_base64(from_c_string(source_account)).unwrap();
127122
let ledger_storage = LedgerStorage::with_restore_tracking(handle, ledger_info.sequence_number)
128123
.context("cannot create LedgerStorage")?;
129124
let result = preflight::preflight_invoke_hf_op(
130125
ledger_storage,
131126
bucket_list_size,
132127
invoke_hf_op,
133128
source_account,
134-
ledger_info.try_into()?,
129+
LedgerInfo::from(ledger_info),
135130
)?;
136-
result.try_into()
131+
Ok(result.into())
137132
}
138133

139134
#[no_mangle]
@@ -162,8 +157,8 @@ fn preflight_footprint_expiration_op_or_maybe_panic(
162157
footprint: *const libc::c_char,
163158
current_ledger_seq: u32,
164159
) -> Result<CPreflightResult> {
165-
let op_body = OperationBody::from_xdr_base64(from_c_string(op_body)?)?;
166-
let footprint = LedgerFootprint::from_xdr_base64(from_c_string(footprint)?)?;
160+
let op_body = OperationBody::from_xdr_base64(from_c_string(op_body)).unwrap();
161+
let footprint = LedgerFootprint::from_xdr_base64(from_c_string(footprint)).unwrap();
167162
let ledger_storage = &LedgerStorage::new(handle);
168163
let result = preflight::preflight_footprint_expiration_op(
169164
ledger_storage,
@@ -172,7 +167,7 @@ fn preflight_footprint_expiration_op_or_maybe_panic(
172167
footprint,
173168
current_ledger_seq,
174169
)?;
175-
result.try_into()
170+
Ok(result.into())
176171
}
177172

178173
fn preflight_error(str: String) -> CPreflightResult {
@@ -209,35 +204,35 @@ fn catch_preflight_panic(op: Box<dyn Fn() -> Result<CPreflightResult>>) -> *mut
209204
Box::into_raw(Box::new(c_preflight_result))
210205
}
211206

212-
fn xdr_to_base64_c(v: impl WriteXdr) -> Result<*mut libc::c_char> {
213-
string_to_c(v.to_xdr_base64()?)
207+
fn xdr_to_base64_c(v: impl WriteXdr) -> *mut libc::c_char {
208+
string_to_c(v.to_xdr_base64().unwrap())
214209
}
215210

216-
fn string_to_c(str: String) -> Result<*mut libc::c_char> {
217-
Ok(CString::new(str)?.into_raw())
211+
fn string_to_c(str: String) -> *mut libc::c_char {
212+
CString::new(str).unwrap().into_raw()
218213
}
219214

220215
fn xdr_vec_to_base64_c_null_terminated_char_array(
221216
payloads: Vec<impl WriteXdr>,
222-
) -> Result<*mut *mut libc::c_char> {
217+
) -> *mut *mut libc::c_char {
223218
let xdr_base64_vec: Vec<String> = payloads
224219
.iter()
225-
.map(WriteXdr::to_xdr_base64)
226-
.collect::<Result<Vec<_>, _>>()?;
220+
.map(|a| WriteXdr::to_xdr_base64(a).unwrap())
221+
.collect();
227222
string_vec_to_c_null_terminated_char_array(xdr_base64_vec)
228223
}
229224

230-
fn string_vec_to_c_null_terminated_char_array(v: Vec<String>) -> Result<*mut *mut libc::c_char> {
225+
fn string_vec_to_c_null_terminated_char_array(v: Vec<String>) -> *mut *mut libc::c_char {
231226
let mut out_vec: Vec<*mut libc::c_char> = Vec::new();
232227
for s in &v {
233-
let c_str = string_to_c(s.clone())?;
228+
let c_str = string_to_c(s.clone());
234229
out_vec.push(c_str);
235230
}
236231

237232
// Add the ending NULL
238233
out_vec.push(null_mut());
239234

240-
Ok(vec_to_c_array(out_vec))
235+
vec_to_c_array(out_vec)
241236
}
242237

243238
fn vec_to_c_array<T>(mut v: Vec<T>) -> *mut T {
@@ -305,7 +300,7 @@ fn free_c_null_terminated_char_array(array: *mut *mut libc::c_char) {
305300
_ = Vec::from_raw_parts(array, len, len);
306301
}
307302
}
308-
fn from_c_string(str: *const libc::c_char) -> Result<String> {
303+
fn from_c_string(str: *const libc::c_char) -> String {
309304
let c_str = unsafe { CStr::from_ptr(str) };
310-
Ok(c_str.to_str()?.to_string())
305+
c_str.to_str().unwrap().to_string()
311306
}

0 commit comments

Comments
 (0)