scrtlabs / enigma-core

Enigma Core library. The domain: Trusted and Untrusted App in Rust.
https://enigma.co
GNU Affero General Public License v3.0
94 stars 21 forks source link

Check validity of inputs and gracefully handle the error #109

Open lacabra opened 5 years ago

lacabra commented 5 years ago

Is your feature request related to a problem? Please describe.

Core throws an exception when processing a request to deploy a SecretContract that includes a PreCode field with other characters that are not hex, for example, just prepending the string 0x to a hex string causes the following error:

core_1        | 03:54:44 [INFO] LOG DERIVE: Err(Invalid character 'x' at position 1
core_1        | 
core_1        | stack backtrace:
core_1        |    0: failure::backtrace::internal::InternalBacktrace::new::h0c85aeebdcdd7156 (0x557efe8fa8ae)
core_1        |              at /root/.cargo/registry/src/github.com-1ecc6299db9ec823/failure-0.1.5/src/backtrace/internal.rs:44
core_1        |    1: failure::backtrace::Backtrace::new::hb2072042fb7e0676 (0x557efe8fa37d)
core_1        |              at /root/.cargo/registry/src/github.com-1ecc6299db9ec823/failure-0.1.5/src/backtrace/mod.rs:111
core_1        |    2: <failure::error::error_impl::ErrorImpl as core::convert::From<F>>::from::h362099f92da07e9e (0x557efe3af1f6)
core_1        |              at /root/.cargo/registry/src/github.com-1ecc6299db9ec823/failure-0.1.5/src/error/error_impl.rs:19
core_1        |    3: <failure::error::Error as core::convert::From<F>>::from::h6ccb66125461f3af (0x557efe3a744a)
core_1        |              at /root/.cargo/registry/src/github.com-1ecc6299db9ec823/failure-0.1.5/src/error/mod.rs:36
core_1        |    4: enigma_core_app::networking::ipc_listener::handling::deploy_contract::{{closure}}::he9bae77b29db796c (0x557efe114e09)
core_1        |              at src/networking/ipc_listener.rs:307
core_1        |    5: enigma_core_app::networking::ipc_listener::handling::deploy_contract::h2667e8963674a2af (0x557efe1c1e4d)
core_1        |              at src/networking/ipc_listener.rs:307
core_1        |    6: enigma_core_app::networking::ipc_listener::handle_message::h1750df4bf26872f3 (0x557efe106c40)
core_1        |              at src/networking/ipc_listener.rs:48
core_1        |    7: enigma_core_app::main::{{closure}}::hf35b7eb5da2211fb (0x557efe09e944)
core_1        |              at src/main.rs:35
core_1        |    8: core::ops::function::impls::<impl core::ops::function::FnOnce<A> for &'a mut F>::call_once::h0a1b12463a3b99c4 (0x557efe09e80a)
core_1        |              at libcore/ops/function.rs:286
core_1        |    9: <core::option::Option<T>>::map::h32de77ba92ca6014 (0x557efe09d0ec)
core_1        |              at libcore/option.rs:424
core_1        |   10: <futures::stream::map::Map<S, F> as futures::stream::Stream>::poll::h020ec7812c81f167 (0x557efe0beeba)
core_1        |              at /root/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-0.1.26/src/stream/map.rs:79
core_1        |   11: <futures::stream::forward::Forward<T, U> as futures::future::Future>::poll::h9f24ac433f8d0533 (0x557efe0ac563)
core_1        |              at /root/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-0.1.26/src/stream/forward.rs:94
core_1        |   12: <futures::future::map::Map<A, F> as futures::future::Future>::poll::hedf6c26bce1e517a (0x557efe0b65fd)
core_1        |              at /root/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-0.1.26/src/future/map.rs:30
core_1        |   13: <futures::future::chain::Chain<A, B, C>>::poll::h9a56683a803d98e6 (0x557efe0c2dd5)
core_1        |              at /root/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-0.1.26/src/future/chain.rs:32
core_1        |   14: <futures::future::and_then::AndThen<A, B, F> as futures::future::Future>::poll::h476e6dd9a7af70c0 (0x557efe0a9929)
core_1        |              at /root/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-0.1.26/src/future/and_then.rs:32
core_1        |   15: <futures::task_impl::Spawn<T>>::poll_future_notify::{{closure}}::h8aac65f6c2b28787 (0x557efe09a899)
core_1        |              at /root/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-0.1.26/src/task_impl/mod.rs:329
core_1        |   16: <futures::task_impl::Spawn<T>>::enter::{{closure}}::h0dcbd4cb7cf055a1 (0x557efe09aa16)
core_1        |              at /root/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-0.1.26/src/task_impl/mod.rs:399
core_1        |   17: futures::task_impl::std::set::h42825b196f346936 (0x557efe0be11c)
core_1        |              at /root/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-0.1.26/src/task_impl/std/mod.rs:78
core_1        |   18: <futures::task_impl::Spawn<T>>::enter::h73545ec010aa3483 (0x557efe09a9a2)
core_1        |              at /root/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-0.1.26/src/task_impl/mod.rs:399
core_1        |   19: <futures::task_impl::Spawn<T>>::poll_fn_notify::hbab32881f8f24cdd (0x557efe09a7c4)
core_1        |              at /root/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-0.1.26/src/task_impl/mod.rs:291
core_1        |   20: <futures::task_impl::Spawn<T>>::poll_future_notify::h7d6ea848a1928f74 (0x557efe09a86d)
core_1        |              at /root/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-0.1.26/src/task_impl/mod.rs:329
core_1        |   21: futures::task_impl::std::<impl futures::task_impl::Spawn<F>>::wait_future::{{closure}}::h007f83165af89296 (0x557efe0be1f8)
core_1        |              at /root/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-0.1.26/src/task_impl/std/mod.rs:231
core_1        |   22: futures::task_impl::std::ThreadNotify::with_current::{{closure}}::h2a26e648c9ac6231 (0x557efe0be02e)
core_1        |              at /root/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-0.1.26/src/task_impl/std/mod.rs:478
core_1        |   23: <std::thread::local::LocalKey<T>>::try_with::h2a5cc9a7429097dd (0x557efe09ca23)
core_1        |              at libstd/thread/local.rs:294
core_1        |   24: <std::thread::local::LocalKey<T>>::with::hc1a5d9c54e7a9872 (0x557efe09c820)
core_1        |              at libstd/thread/local.rs:248
core_1        |   25: futures::task_impl::std::ThreadNotify::with_current::hd3630610c1e4cdc4 (0x557efe0bdfed)
core_1        |              at /root/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-0.1.26/src/task_impl/std/mod.rs:478
core_1        |   26: futures::task_impl::std::<impl futures::task_impl::Spawn<F>>::wait_future::h9b449006e78c92e2 (0x557efe09aa43)
core_1        |              at /root/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-0.1.26/src/task_impl/std/mod.rs:228
core_1        |   27: futures::future::Future::wait::h65ec1379a489e9f4 (0x557efe0a9b21)
core_1        |              at /root/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-0.1.26/src/future/mod.rs:299
core_1        |   28: enigma_core_app::main::h51e5e13b67620211 (0x557efe0a7164)
core_1        |              at src/main.rs:34
core_1        |   29: std::rt::lang_start::{{closure}}::hf50adf6ebd355f61 (0x557efe0b04ef)
core_1        |              at libstd/rt.rs:74
core_1        |   30: std::rt::lang_start_internal::{{closure}}::hdc2a896aeffb5179 (0x557efedc62d2)
core_1        |              at libstd/rt.rs:59
core_1        |       std::panicking::try::do_call::h5a4eb2ce70a501f5
core_1        |              at libstd/panicking.rs:310
core_1        |   31: __rust_maybe_catch_panic (0x557efede7229)
core_1        |              at libpanic_unwind/lib.rs:102
core_1        |   32: std::panicking::try::h97436c380f30f437 (0x557efedc8795)
core_1        |              at libstd/panicking.rs:289
core_1        |       std::panic::catch_unwind::h9c28ef6e0c478c5d
core_1        |              at libstd/panic.rs:392
core_1        |       std::rt::lang_start_internal::h6abd6befa9748e41
core_1        |              at libstd/rt.rs:58
core_1        |   33: std::rt::lang_start::h28de98d2c46239d3 (0x557efe0b04c7)
core_1        |              at libstd/rt.rs:74
core_1        |   34: main (0x557efe0a72f9)
core_1        |   35: __libc_start_main (0x7f124ae30b96)
core_1        |   36: _start (0x557efe099b09)
core_1        |   37: <unknown> (0x0))
core_1        | 03:54:44 [ERROR] Unwrapped p2p Message failed: Invalid character 'x' at position 1
core_1        | 
core_1        | stack backtrace:
core_1        |    0: failure::backtrace::internal::InternalBacktrace::new::h0c85aeebdcdd7156 (0x557efe8fa8ae)
core_1        |              at /root/.cargo/registry/src/github.com-1ecc6299db9ec823/failure-0.1.5/src/backtrace/internal.rs:44
core_1        |    1: failure::backtrace::Backtrace::new::hb2072042fb7e0676 (0x557efe8fa37d)
core_1        |              at /root/.cargo/registry/src/github.com-1ecc6299db9ec823/failure-0.1.5/src/backtrace/mod.rs:111
core_1        |    2: <failure::error::error_impl::ErrorImpl as core::convert::From<F>>::from::h362099f92da07e9e (0x557efe3af1f6)
core_1        |              at /root/.cargo/registry/src/github.com-1ecc6299db9ec823/failure-0.1.5/src/error/error_impl.rs:19
core_1        |    3: <failure::error::Error as core::convert::From<F>>::from::h6ccb66125461f3af (0x557efe3a744a)
core_1        |              at /root/.cargo/registry/src/github.com-1ecc6299db9ec823/failure-0.1.5/src/error/mod.rs:36
core_1        |    4: enigma_core_app::networking::ipc_listener::handling::deploy_contract::{{closure}}::he9bae77b29db796c (0x557efe114e09)
core_1        |              at src/networking/ipc_listener.rs:307
core_1        |    5: enigma_core_app::networking::ipc_listener::handling::deploy_contract::h2667e8963674a2af (0x557efe1c1e4d)
core_1        |              at src/networking/ipc_listener.rs:307
core_1        |    6: enigma_core_app::networking::ipc_listener::handle_message::h1750df4bf26872f3 (0x557efe106c40)
core_1        |              at src/networking/ipc_listener.rs:48
core_1        |    7: enigma_core_app::main::{{closure}}::hf35b7eb5da2211fb (0x557efe09e944)
core_1        |              at src/main.rs:35
core_1        |    8: core::ops::function::impls::<impl core::ops::function::FnOnce<A> for &'a mut F>::call_once::h0a1b12463a3b99c4 (0x557efe09e80a)
core_1        |              at libcore/ops/function.rs:286
core_1        |    9: <core::option::Option<T>>::map::h32de77ba92ca6014 (0x557efe09d0ec)
core_1        |              at libcore/option.rs:424
core_1        |   10: <futures::stream::map::Map<S, F> as futures::stream::Stream>::poll::h020ec7812c81f167 (0x557efe0beeba)
core_1        |              at /root/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-0.1.26/src/stream/map.rs:79
core_1        |   11: <futures::stream::forward::Forward<T, U> as futures::future::Future>::poll::h9f24ac433f8d0533 (0x557efe0ac563)
core_1        |              at /root/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-0.1.26/src/stream/forward.rs:94
core_1        |   12: <futures::future::map::Map<A, F> as futures::future::Future>::poll::hedf6c26bce1e517a (0x557efe0b65fd)
core_1        |              at /root/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-0.1.26/src/future/map.rs:30
core_1        |   13: <futures::future::chain::Chain<A, B, C>>::poll::h9a56683a803d98e6 (0x557efe0c2dd5)
core_1        |              at /root/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-0.1.26/src/future/chain.rs:32
core_1        |   14: <futures::future::and_then::AndThen<A, B, F> as futures::future::Future>::poll::h476e6dd9a7af70c0 (0x557efe0a9929)
core_1        |              at /root/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-0.1.26/src/future/and_then.rs:32
core_1        |   15: <futures::task_impl::Spawn<T>>::poll_future_notify::{{closure}}::h8aac65f6c2b28787 (0x557efe09a899)
core_1        |              at /root/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-0.1.26/src/task_impl/mod.rs:329
core_1        |   16: <futures::task_impl::Spawn<T>>::enter::{{closure}}::h0dcbd4cb7cf055a1 (0x557efe09aa16)
core_1        |              at /root/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-0.1.26/src/task_impl/mod.rs:399
core_1        |   17: futures::task_impl::std::set::h42825b196f346936 (0x557efe0be11c)
core_1        |              at /root/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-0.1.26/src/task_impl/std/mod.rs:78
core_1        |   18: <futures::task_impl::Spawn<T>>::enter::h73545ec010aa3483 (0x557efe09a9a2)
core_1        |              at /root/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-0.1.26/src/task_impl/mod.rs:399
core_1        |   19: <futures::task_impl::Spawn<T>>::poll_fn_notify::hbab32881f8f24cdd (0x557efe09a7c4)
core_1        |              at /root/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-0.1.26/src/task_impl/mod.rs:291
core_1        |   20: <futures::task_impl::Spawn<T>>::poll_future_notify::h7d6ea848a1928f74 (0x557efe09a86d)
core_1        |              at /root/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-0.1.26/src/task_impl/mod.rs:329
core_1        |   21: futures::task_impl::std::<impl futures::task_impl::Spawn<F>>::wait_future::{{closure}}::h007f83165af89296 (0x557efe0be1f8)
core_1        |              at /root/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-0.1.26/src/task_impl/std/mod.rs:231
core_1        |   22: futures::task_impl::std::ThreadNotify::with_current::{{closure}}::h2a26e648c9ac6231 (0x557efe0be02e)
core_1        |              at /root/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-0.1.26/src/task_impl/std/mod.rs:478
core_1        |   23: <std::thread::local::LocalKey<T>>::try_with::h2a5cc9a7429097dd (0x557efe09ca23)
core_1        |              at libstd/thread/local.rs:294
core_1        |   24: <std::thread::local::LocalKey<T>>::with::hc1a5d9c54e7a9872 (0x557efe09c820)
core_1        |              at libstd/thread/local.rs:248
core_1        |   25: futures::task_impl::std::ThreadNotify::with_current::hd3630610c1e4cdc4 (0x557efe0bdfed)
core_1        |              at /root/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-0.1.26/src/task_impl/std/mod.rs:478
core_1        |   26: futures::task_impl::std::<impl futures::task_impl::Spawn<F>>::wait_future::h9b449006e78c92e2 (0x557efe09aa43)
core_1        |              at /root/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-0.1.26/src/task_impl/std/mod.rs:228
core_1        |   27: futures::future::Future::wait::h65ec1379a489e9f4 (0x557efe0a9b21)
core_1        |              at /root/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-0.1.26/src/future/mod.rs:299
core_1        |   28: enigma_core_app::main::h51e5e13b67620211 (0x557efe0a7164)
core_1        |              at src/main.rs:34
core_1        |   29: std::rt::lang_start::{{closure}}::hf50adf6ebd355f61 (0x557efe0b04ef)
core_1        |              at libstd/rt.rs:74
core_1        |   30: std::rt::lang_start_internal::{{closure}}::hdc2a896aeffb5179 (0x557efedc62d2)
core_1        |              at libstd/rt.rs:59
core_1        |       std::panicking::try::do_call::h5a4eb2ce70a501f5
core_1        |              at libstd/panicking.rs:310
core_1        |   31: __rust_maybe_catch_panic (0x557efede7229)
core_1        |              at libpanic_unwind/lib.rs:102
core_1        |   32: std::panicking::try::h97436c380f30f437 (0x557efedc8795)
core_1        |              at libstd/panicking.rs:289
core_1        |       std::panic::catch_unwind::h9c28ef6e0c478c5d
core_1        |              at libstd/panic.rs:392
core_1        |       std::rt::lang_start_internal::h6abd6befa9748e41
core_1        |              at libstd/rt.rs:58
core_1        |   33: std::rt::lang_start::h28de98d2c46239d3 (0x557efe0b04c7)
core_1        |              at libstd/rt.rs:74
core_1        |   34: main (0x557efe0a72f9)
core_1        |   35: __libc_start_main (0x7f124ae30b96)
core_1        |   36: _start (0x557efe099b09)
core_1        |   37: <unknown> (0x0)

Describe the solution you'd like

It should gracefully return an error indicating that the input is invalid, more like:

core_1        | Error in deployment of smart contract function: Invalid input
core_1        | 04:02:04 [INFO] LOG DERIVE: Ok(FailedTask { result: FailedTask { output: "fede52db9527ba010fc2edd2635df8a1dd6435c83b9cec25b420e3e83014ec51970643341fabb8f6d354681c3783ba714edc569404decbf9d4ae316123f68d541a8c5ad2b4579b05c9e7866fbd4e182272b99cc284505b8f3dd150ca097c3cf22d64fcd6661b19a15656e61c847cf02957c075eec278280b45", used_gas: 0, signature: "2aec98dc09a440e52050d82c84e255774d97d3faf02a6a376ea42a2714c10ed139256eb165a0d69a9393fe2ee33c64a1832dd148aa56030725185e164851bbcf1b" } })
elichai commented 5 years ago

That's weird. can you give me a sample of data? I can't see any unwraps there.

moriaab commented 5 years ago

If I try to add "0x" to a pre_code, there is no exception. The following happens: to_hex fails here https://github.com/enigmampc/enigma-core/blob/develop/enigma-core/app/src/networking/ipc_listener.rs#L309 and then IpcResponse::Error is created (as expected).

@elichai, as far as I understand, we have 2 relevant unwraps: https://github.com/enigmampc/enigma-core/blob/develop/enigma-core/app/src/networking/messages.rs#L219 https://github.com/enigmampc/enigma-core/blob/develop/enigma-core/app/src/main.rs#L37 But I don't see any problems with them in this case. This defect reveals some issue. Although the problem is wrong user input, system error is returned. Is this what we want?

moriaab commented 5 years ago

This defect is planned to be fixed as part of general improvement of treating wrong inputs for deployment and computation of secret contracts.