1#![deny(missing_docs)]
2
3use std::collections::BTreeMap;
6
7use essential_types::{
8 contract::Contract,
9 predicate::Predicate,
10 solution::{Solution, SolutionData, SolutionDataIndex},
11 ContentAddress, Key, PredicateAddress, StateReadBytecode, Value,
12};
13
14const ZEROED_PREDICATE: PredicateAddress = PredicateAddress {
15 contract: ContentAddress([0; 32]),
16 predicate: ContentAddress([0; 32]),
17};
18
19pub mod ser;
20
21#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, PartialEq)]
23pub struct CheckSolutionOutput {
24 pub utility: f64,
26 pub gas: u64,
28}
29
30#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, PartialEq)]
35pub enum SolutionOutcome {
36 Success(u64),
38 Fail(String),
40}
41
42#[derive(serde::Serialize, serde::Deserialize)]
44pub struct CheckSolution {
45 pub solution: Solution,
47 pub contracts: Vec<Contract>,
49}
50
51#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, PartialEq)]
57pub struct QueryStateReads {
58 #[serde(
60 serialize_with = "essential_types::serde::bytecode::serialize_vec",
61 deserialize_with = "essential_types::serde::bytecode::deserialize_vec"
62 )]
63 pub state_read: Vec<StateReadBytecode>,
64 pub index: SolutionDataIndex,
66 pub solution: Solution,
68 pub request_type: StateReadRequestType,
70}
71
72#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, PartialEq)]
74pub enum StateReadRequestType {
75 All(SlotsRequest),
77 Slots(SlotsRequest),
79 Reads,
81}
82
83#[derive(Default, Debug, Clone, serde::Serialize, serde::Deserialize, PartialEq)]
85pub enum SlotsRequest {
86 #[default]
88 All,
89 Pre,
91 Post,
93}
94
95#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, PartialEq)]
97pub enum QueryStateReadsOutput {
98 #[serde(
99 serialize_with = "ser::serialize_map",
100 deserialize_with = "ser::deserialize_map"
101 )]
102 Reads(BTreeMap<ContentAddress, BTreeMap<Key, Value>>),
104 Slots(Slots),
106 All(
108 #[serde(
109 serialize_with = "ser::serialize_map",
110 deserialize_with = "ser::deserialize_map"
111 )]
112 BTreeMap<ContentAddress, BTreeMap<Key, Value>>,
113 Slots,
114 ),
115 Failure(String),
117}
118
119#[derive(Default, Debug, Clone, serde::Serialize, serde::Deserialize, PartialEq)]
121pub struct Slots {
122 pub pre: Vec<Value>,
124 pub post: Vec<Value>,
126}
127
128impl QueryStateReads {
129 pub fn from_solution(
134 mut solution: Solution,
135 index: SolutionDataIndex,
136 predicate: &Predicate,
137 request_type: StateReadRequestType,
138 ) -> Self {
139 for (i, d) in solution.data.iter_mut().enumerate() {
140 if i as SolutionDataIndex == index {
141 continue;
142 }
143 d.decision_variables = Default::default();
144 }
145 Self {
146 state_read: predicate.state_read.clone(),
147 index,
148 solution,
149 request_type,
150 }
151 }
152
153 pub fn inline_empty(
156 state_read: Vec<StateReadBytecode>,
157 request_type: StateReadRequestType,
158 ) -> Self {
159 let data = SolutionData {
160 predicate_to_solve: ZEROED_PREDICATE,
161 decision_variables: Default::default(),
162 transient_data: Default::default(),
163 state_mutations: Default::default(),
164 };
165
166 Self::inline(state_read, data, request_type)
167 }
168
169 pub fn inline(
171 state_read: Vec<StateReadBytecode>,
172 data: SolutionData,
173 request_type: StateReadRequestType,
174 ) -> Self {
175 Self {
176 state_read,
177 index: 0,
178 solution: Solution { data: vec![data] },
179 request_type,
180 }
181 }
182}
183
184impl Default for StateReadRequestType {
185 fn default() -> Self {
186 Self::All(SlotsRequest::default())
187 }
188}