59 lines
1.9 KiB
Rust
Raw Normal View History

/*
* Copyright 2020 Fluence Labs Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2020-12-17 21:44:24 +03:00
mod outcome;
2020-10-16 12:47:46 +03:00
2021-01-15 00:38:58 +03:00
use crate::execution::ExecutableInstruction;
use crate::preparation::prepare;
use crate::preparation::PreparationDescriptor;
2020-12-17 21:44:24 +03:00
2021-02-17 23:36:36 +03:00
use aqua_interpreter_interface::InterpreterOutcome;
2021-02-17 23:36:36 +03:00
pub fn execute_aqua(init_peer_id: String, aqua: String, prev_data: Vec<u8>, data: Vec<u8>) -> InterpreterOutcome {
2021-01-15 00:38:58 +03:00
use std::convert::identity;
log::trace!(
2020-10-30 20:29:05 +03:00
"aquamarine version is {}, init user id is {}",
env!("CARGO_PKG_VERSION"),
2020-11-11 14:31:53 +03:00
init_peer_id
);
2020-12-17 21:44:24 +03:00
execute_aqua_impl(init_peer_id, aqua, prev_data, data).unwrap_or_else(identity)
}
2020-12-17 21:44:24 +03:00
fn execute_aqua_impl(
init_peer_id: String,
aqua: String,
2021-01-15 00:38:58 +03:00
prev_data: Vec<u8>,
data: Vec<u8>,
2021-02-17 23:36:36 +03:00
) -> Result<InterpreterOutcome, InterpreterOutcome> {
2021-01-15 00:38:58 +03:00
let PreparationDescriptor {
2020-12-17 21:44:24 +03:00
mut exec_ctx,
2021-01-15 00:38:58 +03:00
mut trace_ctx,
2020-12-17 21:44:24 +03:00
aqua,
2021-01-15 00:38:58 +03:00
} = prepare(&prev_data, &data, aqua.as_str(), init_peer_id)
2020-12-17 21:44:24 +03:00
// return the initial data in case of errors
2021-01-15 00:38:58 +03:00
.map_err(|e| outcome::from_preparation_error(data, e))?;
2020-12-17 21:44:24 +03:00
2021-01-15 00:38:58 +03:00
aqua.execute(&mut exec_ctx, &mut trace_ctx)
// return new collected trace in case of errors
.map_err(|e| outcome::from_execution_error(&trace_ctx.new_trace, exec_ctx.next_peer_pks.clone(), e))?;
2020-12-17 21:44:24 +03:00
2021-01-15 00:38:58 +03:00
let outcome = outcome::from_path_and_peers(&trace_ctx.new_trace, exec_ctx.next_peer_pks);
2020-12-17 21:44:24 +03:00
Ok(outcome)
}