1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
use casper_execution_engine::{
    core::{
        engine_state::{
            execution_effect::ExecutionEffect, execution_result::ExecutionResult,
            Error as EngineStateError,
        },
        execution::Error as ExecutionError,
    },
    shared::gas::Gas,
};

use crate::engine_server::ipc::{DeployError_OutOfGasError, DeployResult};

impl From<ExecutionResult> for DeployResult {
    fn from(execution_result: ExecutionResult) -> DeployResult {
        match execution_result {
            ExecutionResult::Success { effect, cost, .. } => {
                detail::execution_success(effect, cost)
            }
            ExecutionResult::Failure {
                error,
                effect,
                cost,
                ..
            } => (error, effect, cost).into(),
        }
    }
}

#[allow(clippy::unnested_or_patterns)]
impl From<(EngineStateError, ExecutionEffect, Gas)> for DeployResult {
    fn from((engine_state_error, effect, cost): (EngineStateError, ExecutionEffect, Gas)) -> Self {
        match engine_state_error {
            // TODO(mateusz.gorski): Fix error model for the storage errors.
            // We don't have separate IPC messages for storage errors so for the time being they are
            // all reported as "wasm errors".
            error @ EngineStateError::InvalidHashLength { .. }
            | error @ EngineStateError::InvalidAccountHashLength { .. }
            | error @ EngineStateError::InvalidProtocolVersion { .. }
            | error @ EngineStateError::InvalidUpgradeConfig
            | error @ EngineStateError::WasmPreprocessing(_)
            | error @ EngineStateError::WasmSerialization(_)
            | error @ EngineStateError::Exec(ExecutionError::DeploymentAuthorizationFailure)
            | error @ EngineStateError::InvalidKeyVariant(_)
            | error @ EngineStateError::Authorization
            | error @ EngineStateError::InvalidDeployItemVariant(_)
            | error @ EngineStateError::InvalidUpgradeResult => {
                detail::precondition_error(error.to_string())
            }
            EngineStateError::Storage(storage_error) => {
                detail::execution_error(storage_error, effect, cost)
            }
            EngineStateError::MissingSystemContract(msg) => {
                detail::execution_error(msg, effect, cost)
            }
            error @ EngineStateError::InsufficientPayment
            | error @ EngineStateError::Deploy
            | error @ EngineStateError::Finalization
            | error @ EngineStateError::Bytesrepr(_)
            | error @ EngineStateError::BincodeSerialization(_)
            | error @ EngineStateError::BincodeDeserialization(_)
            | error @ EngineStateError::Mint(_) => detail::execution_error(error, effect, cost),
            EngineStateError::Exec(exec_error) => (exec_error, effect, cost).into(),
        }
    }
}

impl From<(ExecutionError, ExecutionEffect, Gas)> for DeployResult {
    fn from((exec_error, effect, cost): (ExecutionError, ExecutionEffect, Gas)) -> Self {
        match exec_error {
            ExecutionError::GasLimit => detail::out_of_gas_error(effect, cost),
            ExecutionError::KeyNotFound(key) => {
                detail::execution_error(format!("Key {:?} not found.", key), effect, cost)
            }
            ExecutionError::Revert(status) => {
                detail::execution_error(status.to_string(), effect, cost)
            }
            ExecutionError::Interpreter(error) => detail::execution_error(error, effect, cost),
            // TODO(mateusz.gorski): Be more specific about execution errors
            other => detail::execution_error(format!("{:?}", other), effect, cost),
        }
    }
}

mod detail {
    use super::{DeployError_OutOfGasError, DeployResult, ExecutionEffect, Gas};

    /// Constructs an instance of `DeployResult` with no error set, i.e. a successful
    /// result.
    pub(super) fn execution_success(effect: ExecutionEffect, cost: Gas) -> DeployResult {
        deploy_result(DeployErrorType::None, effect, cost)
    }

    /// Constructs an instance of `DeployResult` with an error set to
    /// `ProtobufPreconditionFailure`.
    pub(super) fn precondition_error(msg: String) -> DeployResult {
        let mut pb_deploy_result = DeployResult::new();
        pb_deploy_result.mut_precondition_failure().set_message(msg);
        pb_deploy_result
    }

    /// Constructs an instance of `DeployResult` with an error set to
    /// `ProtobufExecutionError`.
    pub(super) fn execution_error<T: ToString>(
        msg: T,
        effect: ExecutionEffect,
        cost: Gas,
    ) -> DeployResult {
        deploy_result(DeployErrorType::Exec(msg.to_string()), effect, cost)
    }

    /// Constructs an instance of `DeployResult` with an error set to
    /// `DeployError_OutOfGasError`.
    pub(super) fn out_of_gas_error(effect: ExecutionEffect, cost: Gas) -> DeployResult {
        deploy_result(DeployErrorType::OutOfGas, effect, cost)
    }

    enum DeployErrorType {
        None,
        OutOfGas,
        Exec(String),
    }

    /// Constructs an instance of `DeployResult` with an error set to
    /// `DeployError_OutOfGasError` or `ProtobufExecutionError` or with no error set, depending on
    /// the value of `error_type`.
    fn deploy_result(
        error_type: DeployErrorType,
        effect: ExecutionEffect,
        cost: Gas,
    ) -> DeployResult {
        let mut pb_deploy_result = DeployResult::new();

        let pb_execution_result = pb_deploy_result.mut_execution_result();
        match error_type {
            DeployErrorType::None => (),
            DeployErrorType::OutOfGas => pb_execution_result
                .mut_error()
                .set_gas_error(DeployError_OutOfGasError::new()),
            DeployErrorType::Exec(msg) => pb_execution_result
                .mut_error()
                .mut_exec_error()
                .set_message(msg),
        }
        pb_execution_result.set_effects(effect.into());
        pb_execution_result.set_cost(cost.value().into());

        pb_deploy_result
    }
}

#[cfg(test)]
mod tests {
    use std::convert::TryInto;

    use casper_execution_engine::shared::{additive_map::AdditiveMap, transform::Transform};
    use casper_types::{
        bytesrepr::Error as BytesReprError, AccessRights, ApiError, Key, URef, U512,
    };

    use super::*;

    #[test]
    fn deploy_result_to_ipc_success() {
        let input_transforms: AdditiveMap<Key, Transform> = {
            let mut tmp_map = AdditiveMap::new();
            tmp_map.insert(
                Key::URef(URef::new([1u8; 32], AccessRights::ADD)),
                Transform::AddInt32(10),
            );
            tmp_map
        };
        let execution_effect = ExecutionEffect::new(AdditiveMap::new(), input_transforms.clone());
        let transfers = Vec::default();
        let cost = Gas::new(U512::from(123));
        let execution_result = ExecutionResult::Success {
            effect: execution_effect,
            transfers,
            cost,
        };
        let mut ipc_deploy_result: DeployResult = execution_result.into();
        assert!(ipc_deploy_result.has_execution_result());
        let mut success = ipc_deploy_result.take_execution_result();
        let execution_cost: U512 = success.take_cost().try_into().expect("should map to U512");
        assert_eq!(execution_cost, cost.value());

        // Extract transform map from the IPC message and parse it back to the domain
        let ipc_transforms: AdditiveMap<Key, Transform> = {
            let mut ipc_effects = success.take_effects();
            let ipc_effects_tnfs = ipc_effects.take_transform_map().into_vec();
            ipc_effects_tnfs
                .into_iter()
                .map(TryInto::try_into)
                .collect::<Result<AdditiveMap<Key, Transform>, _>>()
                .unwrap()
        };
        assert_eq!(input_transforms, ipc_transforms);
    }

    fn test_cost<E: Into<EngineStateError>>(expected_cost: Gas, error: E) -> Gas {
        let execution_failure = ExecutionResult::Failure {
            error: error.into(),
            effect: Default::default(),
            transfers: Vec::default(),
            cost: expected_cost,
        };
        let mut ipc_deploy_result: DeployResult = execution_failure.into();
        assert!(ipc_deploy_result.has_execution_result());
        let execution_result = ipc_deploy_result.mut_execution_result();
        let execution_cost: U512 = execution_result
            .take_cost()
            .try_into()
            .expect("should map to U512");
        Gas::new(execution_cost)
    }

    #[test]
    fn storage_error_has_cost() {
        let cost = Gas::new(U512::from(100));
        // TODO: actually create an Rkv error
        // assert_eq!(test_cost(cost, RkvError("Error".to_owned())), cost);
        let bytesrepr_err = BytesReprError::EarlyEndOfStream;
        assert_eq!(
            test_cost(cost, ExecutionError::BytesRepr(bytesrepr_err)),
            cost
        );
    }

    #[test]
    fn exec_err_has_cost() {
        let cost = Gas::new(U512::from(100));
        // GasLimit error is treated differently at the moment so test separately
        assert_eq!(test_cost(cost, ExecutionError::GasLimit), cost);
        // for the time being all other execution errors are treated in the same way
        let forged_ref_error =
            ExecutionError::ForgedReference(URef::new([1u8; 32], AccessRights::READ_ADD_WRITE));
        assert_eq!(test_cost(cost, forged_ref_error), cost);
    }

    #[test]
    fn revert_error_maps_to_execution_error() {
        // TODO: UnexpectedContractRefVariant is no longer used in actual code; in this test it can
        // be replaced with any other error variant
        let expected_revert = ApiError::UnexpectedContractRefVariant;
        let revert_error = ExecutionError::Revert(expected_revert);
        let amount = U512::from(15);
        let exec_result = ExecutionResult::Failure {
            error: EngineStateError::Exec(revert_error),
            effect: Default::default(),
            transfers: Vec::default(),
            cost: Gas::new(amount),
        };
        let mut ipc_result: DeployResult = exec_result.into();
        assert!(
            ipc_result.has_execution_result(),
            "should have execution result"
        );
        let ipc_execution_result = ipc_result.mut_execution_result();
        let execution_cost: U512 = ipc_execution_result
            .take_cost()
            .try_into()
            .expect("should map to U512");
        assert_eq!(execution_cost, amount, "execution cost should equal amount");
        assert_eq!(
            ipc_execution_result
                .get_error()
                .get_exec_error()
                .get_message(),
            expected_revert.to_string(),
        );
    }
}