1use crate::scenario_format::{
2 interpret_trait::{InterpretableFrom, InterpreterContext, IntoRaw},
3 serde_raw::StepRaw,
4};
5
6use crate::scenario::model::{
7 Account, AddressKey, BlockInfo, BytesValue, CheckAccounts, NewAddress, TxCall, TxDeploy,
8 TxExpect, TxQuery, TxTransfer, TxValidatorReward,
9};
10
11use super::{
12 CheckStateStep, DumpStateStep, ScCallStep, ScDeployStep, ScQueryStep, SetStateStep,
13 TransferStep, ValidatorRewardStep,
14};
15
16#[derive(Debug, Clone)]
17pub struct ExternalStepsStep {
18 pub comment: Option<String>,
19 pub path: String,
20}
21
22#[derive(Debug, Clone)]
23pub enum Step {
24 ExternalSteps(ExternalStepsStep),
25 SetState(SetStateStep),
26 ScCall(ScCallStep),
27 ScQuery(ScQueryStep),
28 ScDeploy(ScDeployStep),
29 Transfer(TransferStep),
30 ValidatorReward(ValidatorRewardStep),
31 CheckState(CheckStateStep),
32 DumpState(DumpStateStep),
33}
34
35impl InterpretableFrom<StepRaw> for Step {
36 fn interpret_from(from: StepRaw, context: &InterpreterContext) -> Self {
37 match from {
38 StepRaw::ExternalSteps { comment, path } => {
39 Step::ExternalSteps(ExternalStepsStep { comment, path })
40 }
41 StepRaw::SetState {
42 comment,
43 accounts,
44 new_addresses,
45 new_token_identifiers,
46 block_hashes,
47 previous_block_info,
48 current_block_info,
49 } => Step::SetState(SetStateStep {
50 comment,
51 accounts: accounts
52 .into_iter()
53 .map(|(k, v)| {
54 (
55 AddressKey::interpret_from(k, context),
56 Account::interpret_from(v, context),
57 )
58 })
59 .collect(),
60 new_addresses: new_addresses
61 .into_iter()
62 .map(|t| NewAddress::interpret_from(t, context))
63 .collect(),
64 new_token_identifiers,
65 block_hashes: block_hashes
66 .into_iter()
67 .map(|t| BytesValue::interpret_from(t, context))
68 .collect(),
69 previous_block_info: Box::new(
70 previous_block_info.map(|v| BlockInfo::interpret_from(v, context)),
71 ),
72 current_block_info: Box::new(
73 current_block_info.map(|v| BlockInfo::interpret_from(v, context)),
74 ),
75 epoch_start_block_info: Box::new(None), block_round_time_ms: None, }),
78 StepRaw::ScCall {
79 id,
80 tx_id,
81 comment,
82 display_logs: _,
83 tx,
84 expect,
85 } => Step::ScCall(ScCallStep {
86 id,
87 tx_id,
88 comment,
89 tx: Box::new(TxCall::interpret_from(tx, context)),
90 expect: expect.map(|v| TxExpect::interpret_from(v, context)),
91 ..Default::default()
92 }),
93 StepRaw::ScQuery {
94 id,
95 tx_id,
96 comment,
97 display_logs: _,
98 tx,
99 expect,
100 } => Step::ScQuery(ScQueryStep {
101 id,
102 tx_id,
103 comment,
104 tx: Box::new(TxQuery::interpret_from(tx, context)),
105 expect: expect.map(|v| TxExpect::interpret_from(v, context)),
106 ..Default::default()
107 }),
108 StepRaw::ScDeploy {
109 id,
110 tx_id,
111 comment,
112 display_logs: _,
113 tx,
114 expect,
115 } => Step::ScDeploy(ScDeployStep {
116 id,
117 tx_id,
118 comment,
119 tx: Box::new(TxDeploy::interpret_from(tx, context)),
120 expect: expect.map(|v| TxExpect::interpret_from(v, context)),
121 ..Default::default()
122 }),
123 StepRaw::Transfer {
124 id,
125 tx_id,
126 comment,
127 tx,
128 } => Step::Transfer(TransferStep {
129 id,
130 tx_id,
131 comment,
132 tx: Box::new(TxTransfer::interpret_from(tx, context)),
133 }),
134 StepRaw::ValidatorReward {
135 id,
136 tx_id,
137 comment,
138 tx,
139 } => Step::ValidatorReward(ValidatorRewardStep {
140 id,
141 tx_id,
142 comment,
143 tx: Box::new(TxValidatorReward::interpret_from(tx, context)),
144 }),
145 StepRaw::CheckState { comment, accounts } => Step::CheckState(CheckStateStep {
146 comment,
147 accounts: CheckAccounts::interpret_from(accounts, context),
148 }),
149 StepRaw::DumpState { comment } => Step::DumpState(DumpStateStep { comment }),
150 }
151 }
152}
153
154impl IntoRaw<StepRaw> for Step {
155 fn into_raw(self) -> StepRaw {
156 match self {
157 Step::ExternalSteps(s) => StepRaw::ExternalSteps {
158 comment: s.comment,
159 path: s.path,
160 },
161 Step::SetState(s) => StepRaw::SetState {
162 comment: s.comment,
163 accounts: s
164 .accounts
165 .into_iter()
166 .map(|(address, account)| (address.into_raw(), account.into_raw()))
167 .collect(),
168 new_addresses: s
169 .new_addresses
170 .into_iter()
171 .map(|na| na.into_raw())
172 .collect(),
173 new_token_identifiers: s.new_token_identifiers,
174 block_hashes: s.block_hashes.into_iter().map(|bh| bh.original).collect(),
175 previous_block_info: s.previous_block_info.map(|bi| bi.into_raw()),
176 current_block_info: s.current_block_info.map(|bi| bi.into_raw()),
177 },
178 Step::ScCall(s) => StepRaw::ScCall {
179 id: s.id,
180 tx_id: s.tx_id,
181 comment: s.comment,
182 display_logs: None,
183 tx: s.tx.into_raw(),
184 expect: s.expect.map(|expect| expect.into_raw()),
185 },
186 Step::ScQuery(s) => StepRaw::ScQuery {
187 id: s.id,
188 tx_id: s.tx_id,
189 comment: s.comment,
190 display_logs: None,
191 tx: s.tx.into_raw(),
192 expect: s.expect.map(|expect| expect.into_raw()),
193 },
194 Step::ScDeploy(s) => StepRaw::ScDeploy {
195 id: s.id,
196 tx_id: s.tx_id,
197 comment: s.comment,
198 display_logs: None,
199 tx: s.tx.into_raw(),
200 expect: s.expect.map(|expect| expect.into_raw()),
201 },
202 Step::Transfer(s) => StepRaw::Transfer {
203 id: s.id,
204 tx_id: s.tx_id,
205 comment: s.comment,
206 tx: s.tx.into_raw(),
207 },
208 Step::ValidatorReward(s) => StepRaw::ValidatorReward {
209 id: s.id,
210 tx_id: s.tx_id,
211 comment: s.comment,
212 tx: s.tx.into_raw(),
213 },
214 Step::CheckState(s) => StepRaw::CheckState {
215 comment: s.comment,
216 accounts: s.accounts.into_raw(),
217 },
218 Step::DumpState(s) => StepRaw::DumpState { comment: s.comment },
219 }
220 }
221}