Skip to content

Commit 57ea60d

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 57ea60d

File tree

1 file changed

+31
-35
lines changed
  • cmd/soroban-rpc/lib/preflight/src

1 file changed

+31
-35
lines changed

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

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,10 @@ pub struct CLedgerInfo {
3636
pub autobump_ledgers: u32,
3737
}
3838

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 {
39+
impl From<CLedgerInfo> for LedgerInfo {
40+
fn from(c: CLedgerInfo) -> Self {
41+
let network_passphrase = from_c_string(c.network_passphrase);
42+
Self {
4543
protocol_version: c.protocol_version,
4644
sequence_number: c.sequence_number,
4745
timestamp: c.timestamp,
@@ -51,7 +49,7 @@ impl TryFrom<CLedgerInfo> for LedgerInfo {
5149
min_persistent_entry_expiration: c.min_persistent_entry_expiration,
5250
max_entry_expiration: c.max_entry_expiration,
5351
autobump_ledgers: c.autobump_ledgers,
54-
})
52+
}
5553
}
5654
}
5755

@@ -69,30 +67,28 @@ pub struct CPreflightResult {
6967
pub pre_restore_min_fee: i64, // Minimum recommended resource fee for a prerequired RestoreFootprint operation
7068
}
7169

72-
impl TryFrom<PreflightResult> for CPreflightResult {
73-
type Error = anyhow::Error;
74-
75-
fn try_from(p: PreflightResult) -> Result<Self> {
70+
impl From<PreflightResult> for CPreflightResult {
71+
fn from(p: PreflightResult) -> Self {
7672
let mut result = Self {
7773
error: null_mut(),
78-
auth: xdr_vec_to_base64_c_null_terminated_char_array(p.auth)?,
74+
auth: xdr_vec_to_base64_c_null_terminated_char_array(p.auth),
7975
result: match p.result {
8076
None => null_mut(),
81-
Some(v) => xdr_to_base64_c(v)?,
77+
Some(v) => xdr_to_base64_c(v),
8278
},
83-
transaction_data: xdr_to_base64_c(p.transaction_data)?,
79+
transaction_data: xdr_to_base64_c(p.transaction_data),
8480
min_fee: p.min_fee,
85-
events: xdr_vec_to_base64_c_null_terminated_char_array(p.events)?,
81+
events: xdr_vec_to_base64_c_null_terminated_char_array(p.events),
8682
cpu_instructions: p.cpu_instructions,
8783
memory_bytes: p.memory_bytes,
8884
pre_restore_transaction_data: null_mut(),
8985
pre_restore_min_fee: 0,
9086
};
9187
if let Some(p) = p.restore_preamble {
9288
result.pre_restore_min_fee = p.min_fee;
93-
result.pre_restore_transaction_data = xdr_to_base64_c(p.transaction_data)?;
89+
result.pre_restore_transaction_data = xdr_to_base64_c(p.transaction_data);
9490
};
95-
Ok(result)
91+
result
9692
}
9793
}
9894

@@ -122,8 +118,8 @@ fn preflight_invoke_hf_op_or_maybe_panic(
122118
source_account: *const libc::c_char, // AccountId XDR in base64
123119
ledger_info: CLedgerInfo,
124120
) -> 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)?)?;
121+
let invoke_hf_op = InvokeHostFunctionOp::from_xdr_base64(from_c_string(invoke_hf_op)).unwrap();
122+
let source_account = AccountId::from_xdr_base64(from_c_string(source_account)).unwrap();
127123
let ledger_storage = LedgerStorage::with_restore_tracking(handle, ledger_info.sequence_number)
128124
.context("cannot create LedgerStorage")?;
129125
let result = preflight::preflight_invoke_hf_op(
@@ -133,7 +129,7 @@ fn preflight_invoke_hf_op_or_maybe_panic(
133129
source_account,
134130
ledger_info.try_into()?,
135131
)?;
136-
result.try_into()
132+
Ok(result.into())
137133
}
138134

139135
#[no_mangle]
@@ -162,8 +158,8 @@ fn preflight_footprint_expiration_op_or_maybe_panic(
162158
footprint: *const libc::c_char,
163159
current_ledger_seq: u32,
164160
) -> 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)?)?;
161+
let op_body = OperationBody::from_xdr_base64(from_c_string(op_body)).unwrap();
162+
let footprint = LedgerFootprint::from_xdr_base64(from_c_string(footprint)).unwrap();
167163
let ledger_storage = &LedgerStorage::new(handle);
168164
let result = preflight::preflight_footprint_expiration_op(
169165
ledger_storage,
@@ -172,7 +168,7 @@ fn preflight_footprint_expiration_op_or_maybe_panic(
172168
footprint,
173169
current_ledger_seq,
174170
)?;
175-
result.try_into()
171+
Ok(result.into())
176172
}
177173

178174
fn preflight_error(str: String) -> CPreflightResult {
@@ -209,35 +205,35 @@ fn catch_preflight_panic(op: Box<dyn Fn() -> Result<CPreflightResult>>) -> *mut
209205
Box::into_raw(Box::new(c_preflight_result))
210206
}
211207

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

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

220216
fn xdr_vec_to_base64_c_null_terminated_char_array(
221217
payloads: Vec<impl WriteXdr>,
222-
) -> Result<*mut *mut libc::c_char> {
218+
) -> *mut *mut libc::c_char {
223219
let xdr_base64_vec: Vec<String> = payloads
224220
.iter()
225-
.map(WriteXdr::to_xdr_base64)
226-
.collect::<Result<Vec<_>, _>>()?;
221+
.map(|a| WriteXdr::to_xdr_base64(a).unwrap())
222+
.collect();
227223
string_vec_to_c_null_terminated_char_array(xdr_base64_vec)
228224
}
229225

230-
fn string_vec_to_c_null_terminated_char_array(v: Vec<String>) -> Result<*mut *mut libc::c_char> {
226+
fn string_vec_to_c_null_terminated_char_array(v: Vec<String>) -> *mut *mut libc::c_char {
231227
let mut out_vec: Vec<*mut libc::c_char> = Vec::new();
232228
for s in &v {
233-
let c_str = string_to_c(s.clone())?;
229+
let c_str = string_to_c(s.clone());
234230
out_vec.push(c_str);
235231
}
236232

237233
// Add the ending NULL
238234
out_vec.push(null_mut());
239235

240-
Ok(vec_to_c_array(out_vec))
236+
vec_to_c_array(out_vec)
241237
}
242238

243239
fn vec_to_c_array<T>(mut v: Vec<T>) -> *mut T {
@@ -305,7 +301,7 @@ fn free_c_null_terminated_char_array(array: *mut *mut libc::c_char) {
305301
_ = Vec::from_raw_parts(array, len, len);
306302
}
307303
}
308-
fn from_c_string(str: *const libc::c_char) -> Result<String> {
304+
fn from_c_string(str: *const libc::c_char) -> String {
309305
let c_str = unsafe { CStr::from_ptr(str) };
310-
Ok(c_str.to_str()?.to_string())
306+
c_str.to_str().unwrap().to_string()
311307
}

0 commit comments

Comments
 (0)