Skip to content

Commit ae8553c

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 2c89063 commit ae8553c

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

@@ -123,8 +119,8 @@ fn preflight_invoke_hf_op_or_maybe_panic(
123119
source_account: *const libc::c_char, // AccountId XDR in base64
124120
ledger_info: CLedgerInfo,
125121
) -> Result<CPreflightResult> {
126-
let invoke_hf_op = InvokeHostFunctionOp::from_xdr_base64(from_c_string(invoke_hf_op)?)?;
127-
let source_account = AccountId::from_xdr_base64(from_c_string(source_account)?)?;
122+
let invoke_hf_op = InvokeHostFunctionOp::from_xdr_base64(from_c_string(invoke_hf_op)).unwrap();
123+
let source_account = AccountId::from_xdr_base64(from_c_string(source_account)).unwrap();
128124
let ledger_storage = LedgerStorage::with_restore_tracking(handle, ledger_info.sequence_number)
129125
.context("cannot create LedgerStorage")?;
130126
let result = preflight::preflight_invoke_hf_op(
@@ -134,7 +130,7 @@ fn preflight_invoke_hf_op_or_maybe_panic(
134130
source_account,
135131
ledger_info.try_into()?,
136132
)?;
137-
result.try_into()
133+
Ok(result.into())
138134
}
139135

140136
#[no_mangle]
@@ -163,8 +159,8 @@ fn preflight_footprint_expiration_op_or_maybe_panic(
163159
footprint: *const libc::c_char,
164160
current_ledger_seq: u32,
165161
) -> Result<CPreflightResult> {
166-
let op_body = OperationBody::from_xdr_base64(from_c_string(op_body)?)?;
167-
let footprint = LedgerFootprint::from_xdr_base64(from_c_string(footprint)?)?;
162+
let op_body = OperationBody::from_xdr_base64(from_c_string(op_body)).unwrap();
163+
let footprint = LedgerFootprint::from_xdr_base64(from_c_string(footprint)).unwrap();
168164
let ledger_storage = &LedgerStorage::new(handle);
169165
let result = preflight::preflight_footprint_expiration_op(
170166
ledger_storage,
@@ -173,7 +169,7 @@ fn preflight_footprint_expiration_op_or_maybe_panic(
173169
footprint,
174170
current_ledger_seq,
175171
)?;
176-
result.try_into()
172+
Ok(result.into())
177173
}
178174

179175
fn preflight_error(str: String) -> CPreflightResult {
@@ -211,35 +207,35 @@ fn catch_preflight_panic(op: Box<dyn Fn() -> Result<CPreflightResult>>) -> *mut
211207
Box::into_raw(Box::new(c_preflight_result))
212208
}
213209

214-
fn xdr_to_base64_c(v: impl WriteXdr) -> Result<*mut libc::c_char> {
215-
string_to_c(v.to_xdr_base64()?)
210+
fn xdr_to_base64_c(v: impl WriteXdr) -> *mut libc::c_char {
211+
string_to_c(v.to_xdr_base64().unwrap())
216212
}
217213

218-
fn string_to_c(str: String) -> Result<*mut libc::c_char> {
219-
Ok(CString::new(str)?.into_raw())
214+
fn string_to_c(str: String) -> *mut libc::c_char {
215+
CString::new(str).unwrap().into_raw()
220216
}
221217

222218
fn xdr_vec_to_base64_c_null_terminated_char_array(
223219
payloads: Vec<impl WriteXdr>,
224-
) -> Result<*mut *mut libc::c_char> {
220+
) -> *mut *mut libc::c_char {
225221
let xdr_base64_vec: Vec<String> = payloads
226222
.iter()
227-
.map(WriteXdr::to_xdr_base64)
228-
.collect::<Result<Vec<_>, _>>()?;
223+
.map(|a| WriteXdr::to_xdr_base64(a).unwrap())
224+
.collect();
229225
string_vec_to_c_null_terminated_char_array(xdr_base64_vec)
230226
}
231227

232-
fn string_vec_to_c_null_terminated_char_array(v: Vec<String>) -> Result<*mut *mut libc::c_char> {
228+
fn string_vec_to_c_null_terminated_char_array(v: Vec<String>) -> *mut *mut libc::c_char {
233229
let mut out_vec: Vec<*mut libc::c_char> = Vec::new();
234230
for s in &v {
235-
let c_str = string_to_c(s.clone())?;
231+
let c_str = string_to_c(s.clone());
236232
out_vec.push(c_str);
237233
}
238234

239235
// Add the ending NULL
240236
out_vec.push(null_mut());
241237

242-
Ok(vec_to_c_array(out_vec))
238+
vec_to_c_array(out_vec)
243239
}
244240

245241
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, i + 1, i + 1);
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)