essential_types/solution.rs
1//! # Solutions
2//! Data types that are used to create solutions to predicates.
3
4use serde::{Deserialize, Serialize};
5
6use crate::{Key, PredicateAddress, Value};
7
8#[cfg(feature = "schema")]
9use schemars::JsonSchema;
10
11/// An index into a [`SolutionSet`]'s `solutions` slice.
12///
13/// Note that this type is purely provided as a utility. Implementations should not depend on the
14/// order of `Solution`s within a `SolutionSet` as it must be possible for `SolutionSet`s to be
15/// merged.
16pub type SolutionIndex = u16;
17
18#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
19#[cfg_attr(feature = "schema", derive(JsonSchema))]
20/// A set of [`Solution`]s.
21///
22/// A `SolutionSet`'s `ContentAddress` is the same regardless of the ordering of its solutions.
23///
24/// `SolutionSet`s may be safely merged with one another in the case that there are no [`Key`]
25/// conflicts in the proposed [`state_mutations`][Solution::state_mutations] and/or post-state
26/// reads within the [`predicate_to_solve`][Solution::predicate_to_solve].
27pub struct SolutionSet {
28 /// The input data for each predicate.
29 // Support deserializing the old `data` name.
30 #[serde(alias = "data")]
31 pub solutions: Vec<Solution>,
32}
33
34#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
35#[cfg_attr(feature = "schema", derive(JsonSchema))]
36/// A solution for a single contract predicate.
37pub struct Solution {
38 /// The predicate that the solution attempts to solve.
39 pub predicate_to_solve: PredicateAddress,
40 /// The input data required by the predicate.
41 // Support deserializing the old `decision_variables` name.
42 #[serde(alias = "decision_variables")]
43 pub predicate_data: Vec<Value>,
44 /// The state mutations proposed by the solution.
45 pub state_mutations: Vec<Mutation>,
46}
47
48#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
49#[cfg_attr(feature = "schema", derive(JsonSchema))]
50/// A mutation to a single [`Key`] in state.
51pub struct Mutation {
52 /// Key to data.
53 pub key: Key,
54 /// The new value.
55 ///
56 /// Empty value means the value is being deleted.
57 pub value: Value,
58}
59
60impl SolutionSet {
61 /// Get the sum of all state mutations within the set of solutions.
62 pub fn state_mutations_len(&self) -> usize {
63 self.solutions.iter().map(|d| d.state_mutations.len()).sum()
64 }
65}