use massa_models::{
block_id::BlockId,
operation::{OperationId, SecureShareOperation},
};
use massa_signature::{PublicKey, Signature};
use serde::{Deserialize, Serialize};
use crate::{display_if_true, display_option_bool};
#[derive(Serialize, Deserialize, Debug)]
pub struct OperationInput {
pub creator_public_key: PublicKey,
pub signature: Signature,
pub serialized_content: Vec<u8>,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct OperationInfo {
pub id: OperationId,
pub in_pool: bool,
pub in_blocks: Vec<BlockId>,
pub is_operation_final: Option<bool>,
pub thread: u8,
pub operation: SecureShareOperation,
pub op_exec_status: Option<bool>,
}
impl std::fmt::Display for OperationInfo {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
writeln!(
f,
"Operation {}{}{}{}",
self.id,
display_if_true(self.in_pool, "in pool"),
display_option_bool(
self.is_operation_final,
"operation is final",
"operation is not final",
"finality unknown"
),
display_option_bool(self.op_exec_status, "success", "failed", "status unknown")
)?;
writeln!(f, "In blocks:")?;
for block_id in &self.in_blocks {
writeln!(f, "\t- {}", block_id)?;
}
writeln!(f, "{}", self.operation)?;
Ok(())
}
}
#[cfg(test)]
mod tests {
use jsonrpsee::core::__reexports::serde_json::{self, Value};
use massa_models::{amount::Amount, operation::OperationType};
use serial_test::serial;
use std::collections::BTreeMap;
use std::str::FromStr;
#[test]
#[serial]
fn test_execute_sc_with_datastore() {
let expected_op = OperationType::ExecuteSC {
max_gas: 123,
max_coins: Amount::from_str("5000000").unwrap(),
data: vec![23u8, 123u8, 44u8],
datastore: BTreeMap::from([
(vec![1, 2, 3], vec![4, 5, 6, 7, 8, 9]),
(vec![22, 33, 44, 55, 66, 77], vec![11]),
(vec![2, 3, 4, 5, 6, 7], vec![1]),
]),
};
let op_json_str = serde_json::to_string(&expected_op).unwrap();
let op_json_value: Value = serde_json::from_str(&op_json_str).unwrap();
let datastore = op_json_value["ExecuteSC"]
.as_object()
.unwrap()
.get("datastore")
.unwrap()
.as_array()
.unwrap();
assert_eq!(datastore.len(), 3);
let first_entry = datastore[0].as_array().unwrap();
assert_eq!(first_entry.len(), 2);
let first_key = first_entry[0].as_array().unwrap();
let first_value = first_entry[1].as_array().unwrap();
assert_eq!(first_key.len(), 3);
assert_eq!(first_value.len(), 6);
let actual_op: OperationType = serde_json::from_str(&op_json_str).unwrap();
assert_eq!(actual_op, expected_op);
}
}