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
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
// This file is part of Substrate.

// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

//! Substrate transaction pool implementation.

use crate::LOG_TARGET;
use sc_transaction_pool_api::ChainEvent;
use sp_blockchain::TreeRoute;
use sp_runtime::traits::{Block as BlockT, NumberFor, Saturating};

/// The threshold since the last update where we will skip any maintenance for blocks.
///
/// This includes tracking re-orgs and sending out certain notifications. In general this shouldn't
/// happen and may only happen when the node is doing a full sync.
const SKIP_MAINTENANCE_THRESHOLD: u16 = 20;

/// Helper struct for keeping track of the current state of processed new best
/// block and finalized events. The main purpose of keeping track of this state
/// is to figure out which phases (enactment / finalization) of transaction pool
/// maintenance are needed.
///
/// Given the following chain:
///
///   B1-C1-D1-E1
///  /
/// A
///  \
///   B2-C2-D2-E2
///
/// Some scenarios and expected behavior for sequence of `NewBestBlock` (`nbb`) and `Finalized`
/// (`f`) events:
///
/// - `nbb(C1)`, `f(C1)` -> false (enactment was already performed in `nbb(C1))`
/// - `f(C1)`, `nbb(C1)` -> false (enactment was already performed in `f(C1))`
/// - `f(C1)`, `nbb(D2)` -> false (enactment was already performed in `f(C1)`,
/// we should not retract finalized block)
/// - `f(C1)`, `f(C2)`, `nbb(C1)` -> false
/// - `nbb(C1)`, `nbb(C2)` -> true (switching fork is OK)
/// - `nbb(B1)`, `nbb(B2)` -> true
/// - `nbb(B1)`, `nbb(C1)`, `f(C1)` -> false (enactment was already performed in `nbb(B1)`)
/// - `nbb(C1)`, `f(B1)` -> false (enactment was already performed in `nbb(B2)`)
pub struct EnactmentState<Block>
where
	Block: BlockT,
{
	recent_best_block: Block::Hash,
	recent_finalized_block: Block::Hash,
}

/// Enactment action that should be performed after processing the `ChainEvent`
#[derive(Debug)]
pub enum EnactmentAction<Block: BlockT> {
	/// Both phases of maintenance shall be skipped
	Skip,
	/// Both phases of maintenance shall be performed
	HandleEnactment(TreeRoute<Block>),
	/// Enactment phase of maintenance shall be skipped
	HandleFinalization,
}

impl<Block> EnactmentState<Block>
where
	Block: BlockT,
{
	/// Returns a new `EnactmentState` initialized with the given parameters.
	pub fn new(recent_best_block: Block::Hash, recent_finalized_block: Block::Hash) -> Self {
		EnactmentState { recent_best_block, recent_finalized_block }
	}

	/// Returns the recently finalized block.
	pub fn recent_finalized_block(&self) -> Block::Hash {
		self.recent_finalized_block
	}

	/// Updates the state according to the given `ChainEvent`, returning
	/// `Some(tree_route)` with a tree route including the blocks that need to
	/// be enacted/retracted. If no enactment is needed then `None` is returned.
	pub fn update<TreeRouteF, BlockNumberF>(
		&mut self,
		event: &ChainEvent<Block>,
		tree_route: &TreeRouteF,
		hash_to_number: &BlockNumberF,
	) -> Result<EnactmentAction<Block>, String>
	where
		TreeRouteF: Fn(Block::Hash, Block::Hash) -> Result<TreeRoute<Block>, String>,
		BlockNumberF: Fn(Block::Hash) -> Result<Option<NumberFor<Block>>, String>,
	{
		let new_hash = event.hash();
		let finalized = event.is_finalized();

		// do not proceed with txpool maintain if block distance is to high
		let skip_maintenance =
			match (hash_to_number(new_hash), hash_to_number(self.recent_best_block)) {
				(Ok(Some(new)), Ok(Some(current))) =>
					new.saturating_sub(current) > SKIP_MAINTENANCE_THRESHOLD.into(),
				_ => true,
			};

		if skip_maintenance {
			log::debug!(target: LOG_TARGET, "skip maintain: tree_route would be too long");
			self.force_update(event);
			return Ok(EnactmentAction::Skip)
		}

		// block was already finalized
		if self.recent_finalized_block == new_hash {
			log::debug!(target: LOG_TARGET, "handle_enactment: block already finalized");
			return Ok(EnactmentAction::Skip)
		}

		// compute actual tree route from best_block to notified block, and use
		// it instead of tree_route provided with event
		let tree_route = tree_route(self.recent_best_block, new_hash)?;

		log::debug!(
			target: LOG_TARGET,
			"resolve hash: {new_hash:?} finalized: {finalized:?} \
			 tree_route: (common {:?}, last {:?}) best_block: {:?} finalized_block:{:?}",
			tree_route.common_block(),
			tree_route.last(),
			self.recent_best_block,
			self.recent_finalized_block
		);

		// check if recently finalized block is on retracted path. this could be
		// happening if we first received a finalization event and then a new
		// best event for some old stale best head.
		if tree_route.retracted().iter().any(|x| x.hash == self.recent_finalized_block) {
			log::debug!(
				target: LOG_TARGET,
				"Recently finalized block {} would be retracted by ChainEvent {}, skipping",
				self.recent_finalized_block,
				new_hash
			);
			return Ok(EnactmentAction::Skip)
		}

		if finalized {
			self.recent_finalized_block = new_hash;

			// if there are no enacted blocks in best_block -> hash tree_route,
			// it means that block being finalized was already enacted (this
			// case also covers best_block == new_hash), recent_best_block
			// remains valid.
			if tree_route.enacted().is_empty() {
				log::trace!(
					target: LOG_TARGET,
					"handle_enactment: no newly enacted blocks since recent best block"
				);
				return Ok(EnactmentAction::HandleFinalization)
			}

			// otherwise enacted finalized block becomes best block...
		}

		self.recent_best_block = new_hash;

		Ok(EnactmentAction::HandleEnactment(tree_route))
	}

	/// Forces update of the state according to the given `ChainEvent`. Intended to be used as a
	/// fallback when tree_route cannot be computed.
	pub fn force_update(&mut self, event: &ChainEvent<Block>) {
		match event {
			ChainEvent::NewBestBlock { hash, .. } => self.recent_best_block = *hash,
			ChainEvent::Finalized { hash, .. } => self.recent_finalized_block = *hash,
		};
		log::debug!(
			target: LOG_TARGET,
			"forced update: {:?}, {:?}",
			self.recent_best_block,
			self.recent_finalized_block,
		);
	}
}

#[cfg(test)]
mod enactment_state_tests {
	use super::{EnactmentAction, EnactmentState};
	use sc_transaction_pool_api::ChainEvent;
	use sp_blockchain::{HashAndNumber, TreeRoute};
	use sp_runtime::traits::NumberFor;
	use std::sync::Arc;
	use substrate_test_runtime_client::runtime::{Block, Hash};

	// some helpers for convenient blocks' hash naming
	fn a() -> HashAndNumber<Block> {
		HashAndNumber { number: 1, hash: Hash::from([0xAA; 32]) }
	}
	fn b1() -> HashAndNumber<Block> {
		HashAndNumber { number: 2, hash: Hash::from([0xB1; 32]) }
	}
	fn c1() -> HashAndNumber<Block> {
		HashAndNumber { number: 3, hash: Hash::from([0xC1; 32]) }
	}
	fn d1() -> HashAndNumber<Block> {
		HashAndNumber { number: 4, hash: Hash::from([0xD1; 32]) }
	}
	fn e1() -> HashAndNumber<Block> {
		HashAndNumber { number: 5, hash: Hash::from([0xE1; 32]) }
	}
	fn x1() -> HashAndNumber<Block> {
		HashAndNumber { number: 22, hash: Hash::from([0x1E; 32]) }
	}
	fn b2() -> HashAndNumber<Block> {
		HashAndNumber { number: 2, hash: Hash::from([0xB2; 32]) }
	}
	fn c2() -> HashAndNumber<Block> {
		HashAndNumber { number: 3, hash: Hash::from([0xC2; 32]) }
	}
	fn d2() -> HashAndNumber<Block> {
		HashAndNumber { number: 4, hash: Hash::from([0xD2; 32]) }
	}
	fn e2() -> HashAndNumber<Block> {
		HashAndNumber { number: 5, hash: Hash::from([0xE2; 32]) }
	}
	fn x2() -> HashAndNumber<Block> {
		HashAndNumber { number: 22, hash: Hash::from([0x2E; 32]) }
	}

	fn test_chain() -> Vec<HashAndNumber<Block>> {
		vec![x1(), e1(), d1(), c1(), b1(), a(), b2(), c2(), d2(), e2(), x2()]
	}

	fn block_hash_to_block_number(hash: Hash) -> Result<Option<NumberFor<Block>>, String> {
		Ok(test_chain().iter().find(|x| x.hash == hash).map(|x| x.number))
	}

	/// mock tree_route computing function for simple two-forks chain
	fn tree_route(from: Hash, to: Hash) -> Result<TreeRoute<Block>, String> {
		let chain = test_chain();
		let pivot = chain.iter().position(|x| x.number == a().number).unwrap();

		let from = chain
			.iter()
			.position(|bn| bn.hash == from)
			.ok_or("existing block should be given")?;
		let to = chain
			.iter()
			.position(|bn| bn.hash == to)
			.ok_or("existing block should be given")?;

		//    B1-C1-D1-E1-..-X1
		//   /
		//  A
		//   \
		//    B2-C2-D2-E2-..-X2
		//
		//  [X1 E1 D1 C1 B1 A B2 C2 D2 E2 X2]

		let vec: Vec<HashAndNumber<Block>> = if from < to {
			chain.into_iter().skip(from).take(to - from + 1).collect()
		} else {
			chain.into_iter().skip(to).take(from - to + 1).rev().collect()
		};

		let pivot = if from <= pivot && to <= pivot {
			if from < to {
				to - from
			} else {
				0
			}
		} else if from >= pivot && to >= pivot {
			if from < to {
				0
			} else {
				from - to
			}
		} else {
			if from < to {
				pivot - from
			} else {
				from - pivot
			}
		};

		TreeRoute::new(vec, pivot)
	}

	mod mock_tree_route_tests {
		use super::*;

		/// asserts that tree routes are equal
		fn assert_treeroute_eq(
			expected: Result<TreeRoute<Block>, String>,
			result: Result<TreeRoute<Block>, String>,
		) {
			let expected = expected.unwrap();
			let result = result.unwrap();
			assert_eq!(result.common_block().hash, expected.common_block().hash);
			assert_eq!(result.enacted().len(), expected.enacted().len());
			assert_eq!(result.retracted().len(), expected.retracted().len());
			assert!(result
				.enacted()
				.iter()
				.zip(expected.enacted().iter())
				.all(|(a, b)| a.hash == b.hash));
			assert!(result
				.retracted()
				.iter()
				.zip(expected.retracted().iter())
				.all(|(a, b)| a.hash == b.hash));
		}

		// some tests for mock tree_route function

		#[test]
		fn tree_route_mock_test_01() {
			let result = tree_route(b1().hash, a().hash);
			let expected = TreeRoute::new(vec![b1(), a()], 1);
			assert_treeroute_eq(result, expected);
		}

		#[test]
		fn tree_route_mock_test_02() {
			let result = tree_route(a().hash, b1().hash);
			let expected = TreeRoute::new(vec![a(), b1()], 0);
			assert_treeroute_eq(result, expected);
		}

		#[test]
		fn tree_route_mock_test_03() {
			let result = tree_route(a().hash, c2().hash);
			let expected = TreeRoute::new(vec![a(), b2(), c2()], 0);
			assert_treeroute_eq(result, expected);
		}

		#[test]
		fn tree_route_mock_test_04() {
			let result = tree_route(e2().hash, a().hash);
			let expected = TreeRoute::new(vec![e2(), d2(), c2(), b2(), a()], 4);
			assert_treeroute_eq(result, expected);
		}

		#[test]
		fn tree_route_mock_test_05() {
			let result = tree_route(d1().hash, b1().hash);
			let expected = TreeRoute::new(vec![d1(), c1(), b1()], 2);
			assert_treeroute_eq(result, expected);
		}

		#[test]
		fn tree_route_mock_test_06() {
			let result = tree_route(d2().hash, b2().hash);
			let expected = TreeRoute::new(vec![d2(), c2(), b2()], 2);
			assert_treeroute_eq(result, expected);
		}

		#[test]
		fn tree_route_mock_test_07() {
			let result = tree_route(b1().hash, d1().hash);
			let expected = TreeRoute::new(vec![b1(), c1(), d1()], 0);
			assert_treeroute_eq(result, expected);
		}

		#[test]
		fn tree_route_mock_test_08() {
			let result = tree_route(b2().hash, d2().hash);
			let expected = TreeRoute::new(vec![b2(), c2(), d2()], 0);
			assert_treeroute_eq(result, expected);
		}

		#[test]
		fn tree_route_mock_test_09() {
			let result = tree_route(e2().hash, e1().hash);
			let expected =
				TreeRoute::new(vec![e2(), d2(), c2(), b2(), a(), b1(), c1(), d1(), e1()], 4);
			assert_treeroute_eq(result, expected);
		}

		#[test]
		fn tree_route_mock_test_10() {
			let result = tree_route(e1().hash, e2().hash);
			let expected =
				TreeRoute::new(vec![e1(), d1(), c1(), b1(), a(), b2(), c2(), d2(), e2()], 4);
			assert_treeroute_eq(result, expected);
		}
		#[test]
		fn tree_route_mock_test_11() {
			let result = tree_route(b1().hash, c2().hash);
			let expected = TreeRoute::new(vec![b1(), a(), b2(), c2()], 1);
			assert_treeroute_eq(result, expected);
		}

		#[test]
		fn tree_route_mock_test_12() {
			let result = tree_route(d2().hash, b1().hash);
			let expected = TreeRoute::new(vec![d2(), c2(), b2(), a(), b1()], 3);
			assert_treeroute_eq(result, expected);
		}

		#[test]
		fn tree_route_mock_test_13() {
			let result = tree_route(c2().hash, e1().hash);
			let expected = TreeRoute::new(vec![c2(), b2(), a(), b1(), c1(), d1(), e1()], 2);
			assert_treeroute_eq(result, expected);
		}

		#[test]
		fn tree_route_mock_test_14() {
			let result = tree_route(b1().hash, b1().hash);
			let expected = TreeRoute::new(vec![b1()], 0);
			assert_treeroute_eq(result, expected);
		}

		#[test]
		fn tree_route_mock_test_15() {
			let result = tree_route(b2().hash, b2().hash);
			let expected = TreeRoute::new(vec![b2()], 0);
			assert_treeroute_eq(result, expected);
		}

		#[test]
		fn tree_route_mock_test_16() {
			let result = tree_route(a().hash, a().hash);
			let expected = TreeRoute::new(vec![a()], 0);
			assert_treeroute_eq(result, expected);
		}

		#[test]
		fn tree_route_mock_test_17() {
			let result = tree_route(x2().hash, b1().hash);
			let expected = TreeRoute::new(vec![x2(), e2(), d2(), c2(), b2(), a(), b1()], 5);
			assert_treeroute_eq(result, expected);
		}
	}

	fn trigger_new_best_block(
		state: &mut EnactmentState<Block>,
		from: HashAndNumber<Block>,
		acted_on: HashAndNumber<Block>,
	) -> EnactmentAction<Block> {
		let (from, acted_on) = (from.hash, acted_on.hash);

		let event_tree_route = tree_route(from, acted_on).expect("Tree route exists");

		state
			.update(
				&ChainEvent::NewBestBlock {
					hash: acted_on,
					tree_route: Some(Arc::new(event_tree_route)),
				},
				&tree_route,
				&block_hash_to_block_number,
			)
			.unwrap()
	}

	fn trigger_finalized(
		state: &mut EnactmentState<Block>,
		from: HashAndNumber<Block>,
		acted_on: HashAndNumber<Block>,
	) -> EnactmentAction<Block> {
		let (from, acted_on) = (from.hash, acted_on.hash);

		let v = tree_route(from, acted_on)
			.expect("Tree route exists")
			.enacted()
			.iter()
			.map(|h| h.hash)
			.collect::<Vec<_>>();

		state
			.update(
				&ChainEvent::Finalized { hash: acted_on, tree_route: v.into() },
				&tree_route,
				&block_hash_to_block_number,
			)
			.unwrap()
	}

	fn assert_es_eq(
		es: &EnactmentState<Block>,
		expected_best_block: HashAndNumber<Block>,
		expected_finalized_block: HashAndNumber<Block>,
	) {
		assert_eq!(es.recent_best_block, expected_best_block.hash);
		assert_eq!(es.recent_finalized_block, expected_finalized_block.hash);
	}

	#[test]
	fn test_enactment_helper() {
		sp_tracing::try_init_simple();
		let mut es = EnactmentState::new(a().hash, a().hash);

		//   B1-C1-D1-E1
		//  /
		// A
		//  \
		//   B2-C2-D2-E2

		let result = trigger_new_best_block(&mut es, a(), d1());
		assert!(matches!(result, EnactmentAction::HandleEnactment { .. }));
		assert_es_eq(&es, d1(), a());

		let result = trigger_new_best_block(&mut es, d1(), e1());
		assert!(matches!(result, EnactmentAction::HandleEnactment { .. }));
		assert_es_eq(&es, e1(), a());

		let result = trigger_finalized(&mut es, a(), d2());
		assert!(matches!(result, EnactmentAction::HandleEnactment { .. }));
		assert_es_eq(&es, d2(), d2());

		let result = trigger_new_best_block(&mut es, d2(), e1());
		assert!(matches!(result, EnactmentAction::Skip));
		assert_es_eq(&es, d2(), d2());

		let result = trigger_finalized(&mut es, a(), b2());
		assert!(matches!(result, EnactmentAction::Skip));
		assert_es_eq(&es, d2(), d2());

		let result = trigger_finalized(&mut es, a(), b1());
		assert!(matches!(result, EnactmentAction::Skip));
		assert_es_eq(&es, d2(), d2());

		let result = trigger_new_best_block(&mut es, a(), d2());
		assert!(matches!(result, EnactmentAction::Skip));
		assert_es_eq(&es, d2(), d2());

		let result = trigger_finalized(&mut es, a(), d2());
		assert!(matches!(result, EnactmentAction::Skip));
		assert_es_eq(&es, d2(), d2());

		let result = trigger_new_best_block(&mut es, a(), c2());
		assert!(matches!(result, EnactmentAction::Skip));
		assert_es_eq(&es, d2(), d2());

		let result = trigger_new_best_block(&mut es, a(), c1());
		assert!(matches!(result, EnactmentAction::Skip));
		assert_es_eq(&es, d2(), d2());

		let result = trigger_new_best_block(&mut es, d2(), e2());
		assert!(matches!(result, EnactmentAction::HandleEnactment { .. }));
		assert_es_eq(&es, e2(), d2());

		let result = trigger_finalized(&mut es, d2(), e2());
		assert!(matches!(result, EnactmentAction::HandleFinalization));
		assert_es_eq(&es, e2(), e2());
	}

	#[test]
	fn test_enactment_helper_2() {
		sp_tracing::try_init_simple();
		let mut es = EnactmentState::new(a().hash, a().hash);

		// A-B1-C1-D1-E1

		let result = trigger_new_best_block(&mut es, a(), b1());
		assert!(matches!(result, EnactmentAction::HandleEnactment { .. }));
		assert_es_eq(&es, b1(), a());

		let result = trigger_new_best_block(&mut es, b1(), c1());
		assert!(matches!(result, EnactmentAction::HandleEnactment { .. }));
		assert_es_eq(&es, c1(), a());

		let result = trigger_new_best_block(&mut es, c1(), d1());
		assert!(matches!(result, EnactmentAction::HandleEnactment { .. }));
		assert_es_eq(&es, d1(), a());

		let result = trigger_new_best_block(&mut es, d1(), e1());
		assert!(matches!(result, EnactmentAction::HandleEnactment { .. }));
		assert_es_eq(&es, e1(), a());

		let result = trigger_finalized(&mut es, a(), c1());
		assert!(matches!(result, EnactmentAction::HandleFinalization));
		assert_es_eq(&es, e1(), c1());

		let result = trigger_finalized(&mut es, c1(), e1());
		assert!(matches!(result, EnactmentAction::HandleFinalization));
		assert_es_eq(&es, e1(), e1());
	}

	#[test]
	fn test_enactment_helper_3() {
		sp_tracing::try_init_simple();
		let mut es = EnactmentState::new(a().hash, a().hash);

		// A-B1-C1-D1-E1

		let result = trigger_new_best_block(&mut es, a(), e1());
		assert!(matches!(result, EnactmentAction::HandleEnactment { .. }));
		assert_es_eq(&es, e1(), a());

		let result = trigger_finalized(&mut es, a(), b1());
		assert!(matches!(result, EnactmentAction::HandleFinalization));
		assert_es_eq(&es, e1(), b1());
	}

	#[test]
	fn test_enactment_helper_4() {
		sp_tracing::try_init_simple();
		let mut es = EnactmentState::new(a().hash, a().hash);

		// A-B1-C1-D1-E1

		let result = trigger_finalized(&mut es, a(), e1());
		assert!(matches!(result, EnactmentAction::HandleEnactment { .. }));
		assert_es_eq(&es, e1(), e1());

		let result = trigger_finalized(&mut es, e1(), b1());
		assert!(matches!(result, EnactmentAction::Skip));
		assert_es_eq(&es, e1(), e1());
	}

	#[test]
	fn test_enactment_helper_5() {
		sp_tracing::try_init_simple();
		let mut es = EnactmentState::new(a().hash, a().hash);

		//   B1-C1-D1-E1
		//  /
		// A
		//  \
		//   B2-C2-D2-E2

		let result = trigger_finalized(&mut es, a(), e1());
		assert!(matches!(result, EnactmentAction::HandleEnactment { .. }));
		assert_es_eq(&es, e1(), e1());

		let result = trigger_finalized(&mut es, e1(), e2());
		assert!(matches!(result, EnactmentAction::Skip));
		assert_es_eq(&es, e1(), e1());
	}

	#[test]
	fn test_enactment_helper_6() {
		sp_tracing::try_init_simple();
		let mut es = EnactmentState::new(a().hash, a().hash);

		// A-B1-C1-D1-E1

		let result = trigger_new_best_block(&mut es, a(), b1());
		assert!(matches!(result, EnactmentAction::HandleEnactment { .. }));
		assert_es_eq(&es, b1(), a());

		let result = trigger_finalized(&mut es, a(), d1());
		assert!(matches!(result, EnactmentAction::HandleEnactment { .. }));
		assert_es_eq(&es, d1(), d1());

		let result = trigger_new_best_block(&mut es, a(), e1());
		assert!(matches!(result, EnactmentAction::HandleEnactment { .. }));
		assert_es_eq(&es, e1(), d1());

		let result = trigger_new_best_block(&mut es, a(), c1());
		assert!(matches!(result, EnactmentAction::Skip));
		assert_es_eq(&es, e1(), d1());
	}

	#[test]
	fn test_enactment_forced_update_best_block() {
		sp_tracing::try_init_simple();
		let mut es = EnactmentState::new(a().hash, a().hash);

		es.force_update(&ChainEvent::NewBestBlock { hash: b1().hash, tree_route: None });
		assert_es_eq(&es, b1(), a());
	}

	#[test]
	fn test_enactment_forced_update_finalize() {
		sp_tracing::try_init_simple();
		let mut es = EnactmentState::new(a().hash, a().hash);

		es.force_update(&ChainEvent::Finalized { hash: b1().hash, tree_route: Arc::from([]) });
		assert_es_eq(&es, a(), b1());
	}

	#[test]
	fn test_enactment_skip_long_enacted_path() {
		sp_tracing::try_init_simple();
		let mut es = EnactmentState::new(a().hash, a().hash);

		// A-B1-C1-..-X1
		let result = trigger_new_best_block(&mut es, a(), x1());
		assert!(matches!(result, EnactmentAction::Skip));
		assert_es_eq(&es, x1(), a());
	}

	#[test]
	fn test_enactment_proceed_with_enacted_path_at_threshold() {
		sp_tracing::try_init_simple();
		let mut es = EnactmentState::new(b1().hash, b1().hash);

		// A-B1-C1-..-X1
		let result = trigger_new_best_block(&mut es, b1(), x1());
		assert!(matches!(result, EnactmentAction::HandleEnactment { .. }));
		assert_es_eq(&es, x1(), b1());
	}
}