1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
use crate::{pallet::Error, *};
use frame_support::ensure;

use crate::square_up::SquareUp;

pub mod extra;
pub use extra::*;

#[cfg(test)]
pub mod test;
#[cfg(test)]
pub mod test_extra;
#[cfg(test)]
pub mod test_extra_stress;

use frame_system::pallet_prelude::BlockNumberFor;
use sp_std::marker::PhantomData;
use t3rn_primitives::SpeedMode;

pub struct Machine<T: Config> {
    _phantom: PhantomData<T>,
}

pub enum PrecompileResult<T: Config> {
    TryUpdateFSX(
        Vec<
            FullSideEffect<<T as frame_system::Config>::AccountId, BlockNumberFor<T>, BalanceOf<T>>,
        >,
    ),
    TryBid(
        (
            SideEffectId<T>,
            BalanceOf<T>,
            <T as frame_system::Config>::AccountId,
        ),
    ),
    TryConfirm(
        SideEffectId<T>,
        ConfirmedSideEffect<
            <T as frame_system::Config>::AccountId,
            BlockNumberFor<T>,
            BalanceOf<T>,
        >,
    ),
    TryRequest,
    Continue,
    ForceUpdateStatus(CircuitStatus),
    TryKill(Cause),
    Revert(Cause),
}
// Further Refactors:
// - move all square_up actions to monetary module that always interacts with AccounManager and doesn't lock up balances directly
// - separate executors bidding to a separate pallet with individual storage entries for each bid?
//      - global clock then picks up all collected bids and calls either CircuitMachine::compile or CircuitMachine::kill
// Circuit as Mealy State Machine with finite number of State transitions between CircuitStatus
impl<T: Config> Machine<T> {
    // Only to be called after receiving new batch of SideEffects delivered by:
    // - on_extrinsic_trigger - gateway or user
    // - on_local_trigger - 3vm smart contract
    // Returns fresh LocalXtxContext
    pub fn setup(
        side_effects: &[SideEffect<T::AccountId, BalanceOf<T>>],
        requester: &T::AccountId,
        maybe_adaptive_timeout: Option<AdaptiveTimeout<BlockNumberFor<T>, TargetId>>,
        preferred_security_lvl: &SecurityLvl,
    ) -> Result<LocalXtxCtx<T, BalanceOf<T>>, Error<T>> {
        let (timeouts_at, delay_steps_at): (BlockNumberFor<T>, Option<Vec<BlockNumberFor<T>>>) = (
            T::XtxTimeoutDefault::get() + frame_system::Pallet::<T>::block_number(),
            None,
        );

        let adaptive_timeout = match maybe_adaptive_timeout {
            None => AdaptiveTimeout::<
                frame_system::pallet_prelude::BlockNumberFor<T>,
                TargetId,
            >::new_emergency(timeouts_at),
            Some(adaptive_timeout) => adaptive_timeout,
        };

        let (xtx_id, xtx) = XExecSignal::<T::AccountId, BlockNumberFor<T>>::setup_fresh::<T>(
            requester,
            adaptive_timeout,
            SpeedMode::Finalized,
            delay_steps_at,
        );

        if <pallet::Pallet<T> as Store>::XExecSignals::contains_key(xtx_id) {
            return Err(Error::<T>::SetupFailedDuplicatedXtx)
        }

        let mut local_xtx_ctx = LocalXtxCtx {
            local_state: LocalState::new(),
            xtx_id,
            xtx,
            full_side_effects: vec![],
        };

        pallet::Pallet::<T>::validate(side_effects, &mut local_xtx_ctx, preferred_security_lvl)?;

        Ok(local_xtx_ctx)
    }

    // Infallible attempt to kill Xtx of given Id and cleanup all its state
    //  called by:
    //  - global clock on timeout after exceeding timeout
    //  - global clock after bids weren't collected to all FSX
    // Since infallible (must be bc of global clock is based on on_initialized block hooks returns bool if killed successfully or false if not found
    pub fn kill(
        xtx_id: XtxId<T>,
        cause: Cause,
        infallible_post_update: impl FnOnce(
            (CircuitStatus, CircuitStatus),
            &LocalXtxCtx<T, BalanceOf<T>>,
        ),
    ) -> bool {
        let mut local_ctx = match Self::load_xtx(xtx_id) {
            Ok(ctx) => ctx,
            Err(_err) => return false,
        };
        Self::compile_infallible(
            &mut local_ctx,
            |_, _, _, _, _| -> PrecompileResult<T> { PrecompileResult::TryKill(cause) },
            infallible_post_update,
        )
    }

    pub fn revert(
        xtx_id: XtxId<T>,
        cause: Cause,
        infallible_post_update: impl FnOnce(
            (CircuitStatus, CircuitStatus),
            &LocalXtxCtx<T, BalanceOf<T>>,
        ),
    ) -> bool {
        let mut local_ctx = match Self::load_xtx(xtx_id) {
            Ok(ctx) => ctx,
            Err(_err) => return false,
        };
        Self::compile_infallible(
            &mut local_ctx,
            |_, _, _, _, _| -> PrecompileResult<T> { PrecompileResult::Revert(cause) },
            infallible_post_update,
        )
    }

    pub fn compile_infallible(
        local_ctx: &mut LocalXtxCtx<T, BalanceOf<T>>,
        infallible_precompile: impl FnOnce(
            &mut Vec<
                FullSideEffect<
                    <T as frame_system::Config>::AccountId,
                    BlockNumberFor<T>,
                    BalanceOf<T>,
                >,
            >,
            LocalState,
            // steps count
            (u32, u32),
            CircuitStatus,
            // requester
            T::AccountId,
        ) -> PrecompileResult<T>,
        infallible_post_update: impl FnOnce(
            (CircuitStatus, CircuitStatus),
            &LocalXtxCtx<T, BalanceOf<T>>,
        ),
    ) -> bool {
        Self::compile(
            local_ctx,
            |
                fsx: &mut Vec<
                    FullSideEffect<
                        <T as frame_system::Config>::AccountId,
                        BlockNumberFor<T>,
                        BalanceOf<T>,
                    >,
                >,
                local_state: LocalState,
                steps_count: (u32, u32),
                status: CircuitStatus,
                requester: T::AccountId,
            | -> Result<PrecompileResult<T>, Error<T>> {
                Ok(infallible_precompile(fsx, local_state, steps_count, status, requester))
            },
            |status_change, local_ctx: &LocalXtxCtx<T, BalanceOf<T>>| -> Result<(), Error<T>> {
                infallible_post_update(status_change, local_ctx);
                Ok(())
            }
        ).expect("Expect compile to be infallible when called with infallible precompile and post_update")
    }

    // External interface exposed to all of the that can transition state, multiple FSX at the time i.e:
    // - submit_bid
    // - confirm_side_effect
    // - confirm side effect via XBI
    pub fn compile(
        local_ctx: &mut LocalXtxCtx<T, BalanceOf<T>>,
        precompile: impl FnOnce(
            &mut Vec<
                FullSideEffect<
                    <T as frame_system::Config>::AccountId,
                    BlockNumberFor<T>,
                    BalanceOf<T>,
                >,
            >,
            LocalState,
            // steps count
            (u32, u32),
            CircuitStatus,
            T::AccountId,
        ) -> Result<PrecompileResult<T>, Error<T>>,
        post_update: impl FnOnce(
            (CircuitStatus, CircuitStatus),
            &LocalXtxCtx<T, BalanceOf<T>>,
        ) -> Result<(), Error<T>>,
    ) -> Result<bool, Error<T>> {
        let mut current_fsx = Self::read_current_step_fsx(local_ctx).clone();
        let local_state = local_ctx.local_state.clone();
        let steps_cnt = local_ctx.xtx.steps_cnt;
        let status = local_ctx.xtx.status.clone();
        let requester = local_ctx.xtx.requester.clone();

        let enforced_new_status: Option<CircuitStatus> = match precompile(
            &mut current_fsx,
            local_state,
            steps_cnt,
            status.clone(),
            requester.clone(),
        )? {
            PrecompileResult::TryRequest => {
                // Try deposit from requester
                SquareUp::<T>::try_request(local_ctx)
                    .map_err(|_e| Error::<T>::RequesterNotEnoughBalance)?;
                None
            },
            PrecompileResult::TryUpdateFSX(updated_fsx) => {
                Self::update_current_step_fsx(local_ctx, &updated_fsx);
                None
            },
            PrecompileResult::TryConfirm(sfx_id, confirmed_sfx) => {
                let mut found = false;
                current_fsx.iter_mut().for_each(|fsx| {
                    if fsx.calc_sfx_id::<SystemHashing<T>, T>(local_ctx.xtx_id) == sfx_id {
                        found = true;
                        fsx.confirmed = Some(confirmed_sfx.clone());
                    }
                });
                ensure!(found, Error::<T>::FSXNotFoundById);
                Self::update_current_step_fsx(local_ctx, &current_fsx);
                None
            },
            PrecompileResult::TryBid((sfx_id, bid_amount, bidder)) => {
                match status {
                    CircuitStatus::PendingBidding | CircuitStatus::InBidding => {
                        // Try to replace the current bid with the new one if the amount is lower.
                        // This will also replace a deposit in AccountManager from with required insurance from the new bidder.
                        let updated_fsx = Bids::<T>::try_bid(
                            &mut current_fsx,
                            bid_amount,
                            &bidder,
                            &requester,
                            sfx_id,
                            local_ctx.xtx_id,
                        )?;

                        Self::update_current_step_fsx(local_ctx, &updated_fsx);

                        Some(CircuitStatus::InBidding)
                    },
                    _ => return Err(Error::<T>::BiddingInactive),
                }
            },
            PrecompileResult::Continue => None,
            // Assume kill attempt with fallible post_update to be intended as infallible cleanup to kill op
            //  in case fallible post_update passes, proceed with kill op
            // ToDo: check between allowed status enforcements - kill status / allowed enforced status
            PrecompileResult::TryKill(cause) => Some(CircuitStatus::Killed(cause)),
            PrecompileResult::ForceUpdateStatus(force_status) => {
                if CircuitStatus::InBidding == status && force_status == CircuitStatus::Ready {
                    SquareUp::<T>::bind_bidders(local_ctx);
                }
                Some(force_status)
            },
            PrecompileResult::Revert(cause) => Some(CircuitStatus::Reverted(cause)),
        };
        let status_change = Self::update_status(local_ctx, enforced_new_status)?;
        post_update(status_change.clone(), local_ctx)?;
        Ok(Self::apply(local_ctx, status_change))
    }

    pub fn load_xtx(xtx_id: XtxId<T>) -> Result<LocalXtxCtx<T, BalanceOf<T>>, Error<T>> {
        let xtx = <pallet::Pallet<T> as Store>::XExecSignals::get(xtx_id)
            .ok_or(Error::<T>::XtxDoesNotExist)?;
        let full_side_effects = <pallet::Pallet<T> as Store>::FullSideEffects::get(xtx_id)
            .ok_or(Error::<T>::SetupFailedXtxStorageArtifactsNotFound)?;
        let local_state = <pallet::Pallet<T> as Store>::LocalXtxStates::get(xtx_id)
            .ok_or(Error::<T>::SetupFailedXtxStorageArtifactsNotFound)?;

        Ok(LocalXtxCtx {
            local_state,
            xtx_id,
            xtx,
            full_side_effects,
        })
    }

    fn update_current_step_fsx(
        local_ctx: &mut LocalXtxCtx<T, BalanceOf<T>>,
        updated_fsx: &Vec<
            FullSideEffect<<T as frame_system::Config>::AccountId, BlockNumberFor<T>, BalanceOf<T>>,
        >,
    ) {
        let (current_step, _) = local_ctx.xtx.steps_cnt;

        match local_ctx.full_side_effects.get_mut(current_step as usize) {
            Some(current_step) => {
                *current_step = updated_fsx.to_vec();
            },
            None => {
                *local_ctx
                    .full_side_effects
                    .last_mut()
                    .expect("read_current_step_fsx to have at least one step in FSX steps") =
                    updated_fsx.to_vec();
            },
        };
    }

    pub fn read_current_step_fsx(
        local_ctx: &LocalXtxCtx<T, BalanceOf<T>>,
    ) -> &Vec<FullSideEffect<<T as frame_system::Config>::AccountId, BlockNumberFor<T>, BalanceOf<T>>>
    {
        let (current_step, _) = local_ctx.xtx.steps_cnt;
        local_ctx
            .full_side_effects
            .get(current_step as usize)
            .unwrap_or_else(|| {
                local_ctx
                    .full_side_effects
                    .last()
                    .expect("read_current_step_fsx to have at least one step in FSX steps")
            })
    }

    // Following methods aren't exposed to Pallet - internal use by compile only
    fn check_bump_steps(
        local_ctx: &LocalXtxCtx<T, BalanceOf<T>>,
        status_change: (CircuitStatus, CircuitStatus),
    ) -> (u32, u32) {
        let (prev_status, new_status) = status_change;
        match (prev_status, new_status) {
            (
                CircuitStatus::Requested,
                CircuitStatus::Requested
                | CircuitStatus::Reserved
                | CircuitStatus::PendingBidding
                | CircuitStatus::InBidding,
            ) => (0, local_ctx.full_side_effects.len() as u32),
            (CircuitStatus::Ready | CircuitStatus::PendingExecution, CircuitStatus::Finished) => {
                let (current_step, steps_cnt) = local_ctx.xtx.steps_cnt;
                (current_step + 1, steps_cnt)
            },
            (
                CircuitStatus::Ready | CircuitStatus::PendingExecution | CircuitStatus::Finished,
                CircuitStatus::FinishedAllSteps,
            ) => {
                let (_, steps_cnt) = local_ctx.xtx.steps_cnt;
                (steps_cnt, steps_cnt)
            },
            (_, _) => local_ctx.xtx.steps_cnt,
        }
    }

    // Update should have all of the info accessible in LocalXtxCtx to transition between next states.
    fn update_status(
        local_ctx: &mut LocalXtxCtx<T, BalanceOf<T>>,
        enforce_new_status: Option<CircuitStatus>,
    ) -> Result<(CircuitStatus, CircuitStatus), Error<T>> {
        let current_status = local_ctx.xtx.status.clone();
        // Apply will try to move the status of Xtx from the current to the closest valid one.
        match current_status {
            CircuitStatus::Reverted(_cause) => return Err(Error::<T>::UpdateAttemptDoubleRevert),
            CircuitStatus::Killed(_cause) => return Err(Error::<T>::UpdateAttemptDoubleKill),
            _ => {},
        }

        let mut new_status =
            CircuitStatus::determine_xtx_status::<T, BalanceOf<T>>(&local_ctx.full_side_effects);

        new_status = CircuitStatus::check_transition::<T>(
            current_status.clone(),
            new_status,
            enforce_new_status,
        )
        .map_err(|_e| Error::<T>::UpdateStateTransitionDisallowed)?;

        local_ctx.xtx.steps_cnt =
            Self::check_bump_steps(local_ctx, (current_status.clone(), new_status.clone()));
        local_ctx.xtx.status = new_status.clone();

        Ok((current_status, new_status))
    }

    fn apply(
        local_ctx: &LocalXtxCtx<T, BalanceOf<T>>,
        status_change: (CircuitStatus, CircuitStatus),
    ) -> bool {
        let (old_status, new_status) = (status_change.0, status_change.1);

        // Assume no op. for equal statuses - although this should be caught before apply by disallowed state transitions.
        //  only use case might be for delays of timeout cleaning the storage PendingXtxTimeouts.
        // Also, disallow any downgrade status from Committed.
        if old_status == new_status
            && new_status != CircuitStatus::PendingExecution
            && new_status != CircuitStatus::InBidding
            || old_status == CircuitStatus::Committed
        {
            return false
        }

        match (old_status, new_status) {
            (CircuitStatus::Requested, CircuitStatus::Reserved | CircuitStatus::PendingBidding) => {
                let steps_side_effects_ids: Vec<(
                    usize,
                    SideEffectId<T>,
                    XExecStepSideEffectId<T>,
                )> = local_ctx
                    .full_side_effects
                    .clone()
                    .iter()
                    .enumerate()
                    .flat_map(|(cnt, fsx_step)| {
                        fsx_step
                            .iter()
                            .map(|full_side_effect| {
                                full_side_effect
                                    .calc_sfx_id::<SystemHashing<T>, T>(local_ctx.xtx_id)
                            })
                            .map(|side_effect_hash| {
                                (
                                    cnt,
                                    side_effect_hash,
                                    XExecSignal::<T::AccountId, BlockNumberFor<T>>::generate_step_id::<
                                        T,
                                    >(local_ctx.xtx_id, cnt),
                                )
                            })
                            .collect::<Vec<(usize, SideEffectId<T>, XExecStepSideEffectId<T>)>>()
                    })
                    .collect();
                <pallet::Pallet<T> as Store>::FullSideEffects::insert::<
                    XExecSignalId<T>,
                    Vec<
                        Vec<
                            FullSideEffect<
                                T::AccountId,
                                frame_system::pallet_prelude::BlockNumberFor<T>,
                                BalanceOf<T>,
                            >,
                        >,
                    >,
                >(local_ctx.xtx_id, local_ctx.full_side_effects.clone());

                for (_step_cnt, side_effect_id, _step_side_effect_id) in steps_side_effects_ids {
                    <pallet::Pallet<T> as Store>::SFX2XTXLinksMap::insert::<
                        SideEffectId<T>,
                        XExecSignalId<T>,
                    >(side_effect_id, local_ctx.xtx_id);
                }

                <pallet::Pallet<T> as Store>::LocalXtxStates::insert::<XExecSignalId<T>, LocalState>(
                    local_ctx.xtx_id,
                    local_ctx.local_state.clone(),
                );
                <pallet::Pallet<T> as Store>::PendingXtxTimeoutsMap::insert(
                    local_ctx.xtx_id,
                    &local_ctx.xtx.timeouts_at,
                );
                <pallet::Pallet<T> as Store>::PendingXtxBidsTimeoutsMap::insert::<
                    XExecSignalId<T>,
                    frame_system::pallet_prelude::BlockNumberFor<T>,
                >(
                    local_ctx.xtx_id,
                    T::SFXBiddingPeriod::get() + frame_system::Pallet::<T>::block_number(),
                );
                <pallet::Pallet<T> as Store>::XExecSignals::insert::<
                    XExecSignalId<T>,
                    XExecSignal<T::AccountId, BlockNumberFor<T>>,
                >(local_ctx.xtx_id, local_ctx.xtx.clone());

                true
            },
            (CircuitStatus::PendingBidding, CircuitStatus::InBidding) => {
                <pallet::Pallet<T> as Store>::XExecSignals::mutate(local_ctx.xtx_id, |x| {
                    *x = Some(local_ctx.xtx.clone())
                });

                <pallet::Pallet<T> as Store>::FullSideEffects::mutate(local_ctx.xtx_id, |x| {
                    *x = Some(local_ctx.full_side_effects.clone())
                });

                true
            },
            (CircuitStatus::InBidding, CircuitStatus::InBidding) => {
                <pallet::Pallet<T> as Store>::FullSideEffects::mutate(local_ctx.xtx_id, |x| {
                    *x = Some(local_ctx.full_side_effects.clone())
                });

                true
            },
            (CircuitStatus::InBidding, CircuitStatus::Ready) => {
                <pallet::Pallet<T> as Store>::FullSideEffects::mutate(local_ctx.xtx_id, |x| {
                    *x = Some(local_ctx.full_side_effects.clone())
                });
                <pallet::Pallet<T> as Store>::XExecSignals::mutate(local_ctx.xtx_id, |x| {
                    *x = Some(local_ctx.xtx.clone())
                });
                // Always clean temporary PendingSFXBids and TimeoutsMap after bidding
                <pallet::Pallet<T> as Store>::PendingXtxBidsTimeoutsMap::remove(local_ctx.xtx_id);

                true
            },
            (
                CircuitStatus::Reserved | CircuitStatus::PendingBidding | CircuitStatus::InBidding,
                CircuitStatus::Killed(_cause),
            ) => {
                // Clean all associated Xtx entries
                <pallet::Pallet<T> as Store>::XExecSignals::remove(local_ctx.xtx_id);
                <pallet::Pallet<T> as Store>::PendingXtxTimeoutsMap::remove(local_ctx.xtx_id);
                <pallet::Pallet<T> as Store>::LocalXtxStates::remove(local_ctx.xtx_id);
                <pallet::Pallet<T> as Store>::FullSideEffects::remove(local_ctx.xtx_id);

                let mut fsx_mut_arr = local_ctx.full_side_effects.clone();
                for fsx_step in fsx_mut_arr.iter_mut() {
                    for fsx in fsx_step {
                        let sfx_id = fsx.calc_sfx_id::<SystemHashing<T>, T>(local_ctx.xtx_id);
                        <pallet::Pallet<T> as Store>::SFX2XTXLinksMap::remove(sfx_id);
                    }
                }
                // Always clean temporary PendingXtxBidsTimeoutsMap after bidding
                <pallet::Pallet<T> as Store>::PendingXtxBidsTimeoutsMap::remove(local_ctx.xtx_id);

                SquareUp::<T>::kill(local_ctx);

                true
            },
            (
                CircuitStatus::Ready | CircuitStatus::PendingExecution | CircuitStatus::Finished,
                CircuitStatus::Reverted(_cause),
            ) => {
                <pallet::Pallet<T> as Store>::XExecSignals::remove(local_ctx.xtx_id);
                <pallet::Pallet<T> as Store>::FullSideEffects::remove(local_ctx.xtx_id);
                <pallet::Pallet<T> as Store>::PendingXtxTimeoutsMap::remove(local_ctx.xtx_id);
                <pallet::Pallet<T> as Store>::DLQ::remove(local_ctx.xtx_id);
                <pallet::Pallet<T> as Store>::LocalXtxStates::remove(local_ctx.xtx_id);

                SquareUp::<T>::finalize(local_ctx);

                true
            },
            (CircuitStatus::FinishedAllSteps, CircuitStatus::Committed) => {
                <pallet::Pallet<T> as Store>::XExecSignals::remove(local_ctx.xtx_id);
                <pallet::Pallet<T> as Store>::FullSideEffects::remove(local_ctx.xtx_id);
                <pallet::Pallet<T> as Store>::LocalXtxStates::remove(local_ctx.xtx_id);

                true
            },
            (
                CircuitStatus::Finished | CircuitStatus::Ready | CircuitStatus::PendingExecution,
                CircuitStatus::FinishedAllSteps,
            ) => {
                <pallet::Pallet<T> as Store>::XExecSignals::mutate(local_ctx.xtx_id, |x| {
                    *x = Some(local_ctx.xtx.clone())
                });

                <pallet::Pallet<T> as Store>::PendingXtxTimeoutsMap::remove(local_ctx.xtx_id);
                <pallet::Pallet<T> as Store>::DLQ::remove(local_ctx.xtx_id);

                // Update set of full side effects - only makes sense for Xtx with single SFX.
                //  for the rest FSX are updated in sequence
                //  when reaching CircuitStatus::PendingExecution | CircuitStatus::Finished status.
                <pallet::Pallet<T> as Store>::FullSideEffects::mutate(local_ctx.xtx_id, |x| {
                    *x = Some(local_ctx.full_side_effects.clone())
                });

                <pallet::Pallet<T> as Store>::FinalizedXtx::insert(
                    local_ctx.xtx_id,
                    <frame_system::Pallet<T>>::block_number(),
                );

                SquareUp::<T>::finalize(local_ctx);

                true
            },
            // ongoing execution - update FSX and Xtx status
            (
                CircuitStatus::Ready | CircuitStatus::PendingExecution | CircuitStatus::Finished,
                CircuitStatus::Ready | CircuitStatus::PendingExecution | CircuitStatus::Finished,
            ) => {
                // Update set of full side effects assuming the new confirmed has appeared
                <pallet::Pallet<T> as Store>::FullSideEffects::mutate(local_ctx.xtx_id, |x| {
                    *x = Some(local_ctx.full_side_effects.clone())
                });

                <pallet::Pallet<T> as Store>::XExecSignals::mutate(local_ctx.xtx_id, |x| {
                    *x = Some(local_ctx.xtx.clone())
                });

                true
            },
            (_, _) => false,
        }
    }
}