pop-chains 0.14.0

Library for generating, building and running parachains.
Documentation
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
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
// SPDX-License-Identifier: GPL-3.0

use crate::errors::{Error, handle_command_error};
use anyhow::{Result, anyhow};
use duct::cmd;
use pop_common::{Profile, account_id::convert_to_evm_accounts, manifest::from_path};
use sc_chain_spec::{GenericChainSpec, NoExtension};
use serde_json::{Value, json};
use sp_core::bytes::to_hex;
use std::{
	fs,
	io::Write,
	path::{Path, PathBuf},
	str::FromStr,
};

/// Build the deterministic runtime.
pub mod runtime;

/// A builder for generating chain specifications.
///
/// This enum represents two different ways to build a chain specification:
/// - Using an existing node.
/// - Using a runtime.
pub enum ChainSpecBuilder {
	/// A node-based chain specification builder.
	Node {
		/// Path to the node directory.
		node_path: PathBuf,
		/// Whether to include a default bootnode in the specification.
		default_bootnode: bool,
		/// The build profile to use (debug, release, production, etc).
		profile: Profile,
	},
	/// A runtime-based chain specification builder.
	Runtime {
		/// Path to the runtime directory.
		runtime_path: PathBuf,
		/// The build profile to use (debug, release, production, etc).
		profile: Profile,
	},
}

impl ChainSpecBuilder {
	/// Builds the chain specification using the provided profile and features.
	///
	/// # Arguments
	/// * `features` - A list of cargo features to enable during the build
	///
	/// # Returns
	/// The path to the built artifact
	pub fn build(&self, features: &[String], redirect_output_to_stderr: bool) -> Result<PathBuf> {
		build_project(
			&self.path(),
			None,
			&self.profile(),
			features,
			None,
			redirect_output_to_stderr,
		)?;
		// Check the artifact is found after being built
		self.artifact_path()
	}

	/// Gets the path associated with this chain specification builder.
	///
	/// # Returns
	/// The path to either the node or runtime directory.
	pub fn path(&self) -> PathBuf {
		match self {
			ChainSpecBuilder::Node { node_path, .. } => node_path,
			ChainSpecBuilder::Runtime { runtime_path, .. } => runtime_path,
		}
		.clone()
	}

	/// Gets the build profile associated with this chain specification builder.
	///
	/// # Returns
	/// The build profile (debug, release, production, etc.) to use when building the chain.
	pub fn profile(&self) -> Profile {
		*match self {
			ChainSpecBuilder::Node { profile, .. } => profile,
			ChainSpecBuilder::Runtime { profile, .. } => profile,
		}
	}

	/// Gets the path to the built artifact.
	///
	/// # Returns
	/// The path to the built artifact (node binary or runtime WASM).
	pub fn artifact_path(&self) -> Result<PathBuf> {
		let manifest = from_path(&self.path())?;
		let package = manifest.package().name();
		let root_folder = rustilities::manifest::find_workspace_manifest(self.path())
			.ok_or(anyhow::anyhow!("Not inside a workspace"))?
			.parent()
			.expect("Path to Cargo.toml workspace root folder must exist")
			.to_path_buf();
		let path = match self {
			ChainSpecBuilder::Node { profile, .. } =>
				profile.target_directory(&root_folder).join(package),
			ChainSpecBuilder::Runtime { profile, .. } => {
				let base = profile.target_directory(&root_folder).join("wbuild").join(package);
				let wasm_file = package.replace("-", "_");
				let compact_compressed = base.join(format!("{wasm_file}.compact.compressed.wasm"));
				let raw = base.join(format!("{wasm_file}.wasm"));
				if compact_compressed.is_file() {
					compact_compressed
				} else if raw.is_file() {
					raw
				} else {
					return Err(anyhow::anyhow!("No runtime found"));
				}
			},
		};
		Ok(path.canonicalize()?)
	}

	/// Generates a plain (human readable) chain specification file.
	///
	/// # Arguments
	/// * `chain_or_preset` - The chain (when using a node) or preset (when using a runtime) name.
	/// * `output_file` - The path where the chain spec should be written.
	/// * `name` - The name to be used on the chain spec if specified.
	/// * `id` - The ID to be used on the chain spec if specified.
	pub fn generate_plain_chain_spec(
		&self,
		chain_or_preset: &str,
		output_file: &Path,
		name: Option<&str>,
		id: Option<&str>,
	) -> Result<(), Error> {
		match self {
			ChainSpecBuilder::Node { default_bootnode, .. } => generate_plain_chain_spec_with_node(
				&self.artifact_path()?,
				output_file,
				*default_bootnode,
				chain_or_preset,
			),
			ChainSpecBuilder::Runtime { .. } => generate_plain_chain_spec_with_runtime(
				fs::read(self.artifact_path()?)?,
				output_file,
				chain_or_preset,
				name,
				id,
			),
		}
	}

	/// Generates a raw (encoded) chain specification file from a plain one.
	///
	/// # Arguments
	/// * `plain_chain_spec` - The path to the plain chain spec file.
	/// * `raw_chain_spec_name` - The name for the generated raw chain spec file.
	///
	/// # Returns
	/// The path to the generated raw chain spec file.
	pub fn generate_raw_chain_spec(
		&self,
		plain_chain_spec: &Path,
		raw_chain_spec_name: &str,
	) -> Result<PathBuf, Error> {
		match self {
			ChainSpecBuilder::Node { .. } => generate_raw_chain_spec_with_node(
				&self.artifact_path()?,
				plain_chain_spec,
				raw_chain_spec_name,
			),
			ChainSpecBuilder::Runtime { .. } =>
				generate_raw_chain_spec_with_runtime(plain_chain_spec, raw_chain_spec_name),
		}
	}

	/// Extracts and exports the WebAssembly runtime code from a raw chain specification.
	///
	/// # Arguments
	/// * `raw_chain_spec` - Path to the raw chain specification file to extract the runtime from.
	/// * `wasm_file_name` - Name for the file where the extracted runtime will be saved.
	///
	/// # Returns
	/// The path to the generated WASM runtime file.
	///
	/// # Errors
	/// Returns an error if:
	/// - The chain specification file cannot be read or parsed.
	/// - The runtime cannot be extracted from the chain spec.
	/// - The runtime cannot be written to the output file.
	pub fn export_wasm_file(
		&self,
		raw_chain_spec: &Path,
		wasm_file_name: &str,
	) -> Result<PathBuf, Error> {
		match self {
			ChainSpecBuilder::Node { .. } =>
				export_wasm_file_with_node(&self.artifact_path()?, raw_chain_spec, wasm_file_name),
			ChainSpecBuilder::Runtime { .. } =>
				export_wasm_file_with_runtime(raw_chain_spec, wasm_file_name),
		}
	}
}

/// Build the chain and returns the path to the binary.
///
/// # Arguments
/// * `path` - The path to the chain manifest.
/// * `package` - The optional package to be built.
/// * `profile` - Whether the chain should be built without any debugging functionality.
/// * `node_path` - An optional path to the node directory. Defaults to the `node` subdirectory of
///   the project path if not provided.
/// * `features` - A set of features the project is built with.
pub fn build_chain(
	path: &Path,
	package: Option<String>,
	profile: &Profile,
	node_path: Option<&Path>,
	features: &[String],
	redirect_output_to_stderr: bool,
) -> Result<PathBuf, Error> {
	build_project(path, package, profile, features, None, redirect_output_to_stderr)?;
	binary_path(&profile.target_directory(path), node_path.unwrap_or(&path.join("node")))
}

/// Pre-fetches dependencies so users see download progress before compilation begins.
fn fetch_dependencies(path: &Path) -> Result<(), Error> {
	cmd("cargo", ["fetch"]).dir(path).stdout_null().run()?;
	Ok(())
}

/// Build the Rust project.
///
/// # Arguments
/// * `path` - The optional path to the project manifest, defaulting to the current directory if not
///   specified.
/// * `package` - The optional package to be built.
/// * `profile` - Whether the project should be built without any debugging functionality.
/// * `features` - A set of features the project is built with.
/// * `target` - The optional target to be specified.
pub fn build_project(
	path: &Path,
	package: Option<String>,
	profile: &Profile,
	features: &[String],
	target: Option<&str>,
	redirect_output_to_stderr: bool,
) -> Result<(), Error> {
	fetch_dependencies(path)?;
	let mut args = vec!["build"];
	if let Some(package) = package.as_deref() {
		args.push("--package");
		args.push(package)
	}
	if profile == &Profile::Release {
		args.push("--release");
	} else if profile == &Profile::Production {
		args.push("--profile=production");
	}

	let feature_args = features.join(",");
	if !features.is_empty() {
		args.push("--features");
		args.push(&feature_args);
	}

	if let Some(target) = target {
		args.push("--target");
		args.push(target);
	}

	if redirect_output_to_stderr {
		let output = cmd("cargo", args)
			.dir(path)
			.stdout_capture()
			.stderr_capture()
			.unchecked()
			.run()?;
		let combined = combine_streams_to_string(&output);
		if !combined.is_empty() {
			let _ = std::io::stderr().write_all(combined.as_bytes());
			if !combined.ends_with('\n') {
				let _ = std::io::stderr().write_all(b"\n");
			}
		}
		if !output.status.success() {
			let details =
				if combined.is_empty() { "cargo build failed".to_string() } else { combined };
			return Err(Error::AnyhowError(anyhow!("cargo build failed:\n{details}")));
		}
	} else {
		cmd("cargo", args).dir(path).run()?;
	}
	Ok(())
}

fn combine_streams_to_string(output: &std::process::Output) -> String {
	let mut combined = String::new();
	let stdout = String::from_utf8_lossy(&output.stdout);
	let stderr = String::from_utf8_lossy(&output.stderr);
	if !stdout.is_empty() {
		combined.push_str(&stdout);
	}
	if !stderr.is_empty() {
		combined.push_str(&stderr);
	}
	combined
}

/// Determines whether the manifest at the supplied path is a supported chain project.
///
/// # Arguments
/// * `path` - The optional path to the manifest, defaulting to the current directory if not
///   specified.
pub fn is_supported(path: &Path) -> bool {
	let manifest = match from_path(path) {
		Ok(m) => m,
		Err(_) => return false,
	};
	// Simply check for a chain dependency
	const DEPENDENCIES: [&str; 4] =
		["cumulus-client-collator", "cumulus-primitives-core", "parachains-common", "polkadot-sdk"];
	DEPENDENCIES.into_iter().any(|d| {
		manifest.dependencies.contains_key(d) ||
			manifest.workspace.as_ref().is_some_and(|w| w.dependencies.contains_key(d))
	})
}

/// Constructs the node binary path based on the target path and the node directory path.
///
/// # Arguments
/// * `target_path` - The path where the binaries are expected to be found.
/// * `node_path` - The path to the node from which the node name will be parsed.
pub fn binary_path(target_path: &Path, node_path: &Path) -> Result<PathBuf, Error> {
	build_binary_path(node_path, |node_name| target_path.join(node_name))
}

/// Constructs the runtime binary path based on the target path and the directory path.
///
/// # Arguments
/// * `target_path` - The path where the binaries are expected to be found.
/// * `runtime_path` - The path to the runtime from which the runtime name will be parsed.
pub fn runtime_binary_path(target_path: &Path, runtime_path: &Path) -> Result<PathBuf, Error> {
	build_binary_path(runtime_path, |runtime_name| {
		target_path.join(format!("{runtime_name}/{}.wasm", runtime_name.replace("-", "_")))
	})
}

fn build_binary_path<F>(project_path: &Path, path_builder: F) -> Result<PathBuf, Error>
where
	F: Fn(&str) -> PathBuf,
{
	let manifest = from_path(project_path)?;
	let project_name = manifest.package().name();
	let release = path_builder(project_name);
	if !release.exists() {
		return Err(Error::MissingBinary(project_name.to_string()));
	}
	Ok(release)
}

/// Generates a raw chain specification file from a plain chain specification for a runtime.
///
/// # Arguments
/// * `plain_chain_spec` - Location of the plain chain specification file.
/// * `raw_chain_spec_name` - The name of the raw chain specification file to be generated.
///
/// # Returns
/// The path to the generated raw chain specification file.
pub fn generate_raw_chain_spec_with_runtime(
	plain_chain_spec: &Path,
	raw_chain_spec_name: &str,
) -> Result<PathBuf, Error> {
	let chain_spec = GenericChainSpec::<Option<()>>::from_json_file(plain_chain_spec.to_path_buf())
		.map_err(|e| anyhow::anyhow!(e))?;
	let raw_chain_spec = chain_spec.as_json(true).map_err(|e| anyhow::anyhow!(e))?;
	let raw_chain_spec_file = plain_chain_spec.with_file_name(raw_chain_spec_name);
	fs::write(&raw_chain_spec_file, raw_chain_spec)?;
	Ok(raw_chain_spec_file)
}

/// Generates a plain chain specification file for a runtime.
///
/// # Arguments
/// * `wasm` - The WebAssembly runtime bytes.
/// * `plain_chain_spec` - The path where the plain chain specification should be written.
/// * `preset` - Preset name for genesis configuration.
/// * `name` - The name to be used on the chain spec if specified.
/// * `id` - The ID to be used on the chain spec if specified.
pub fn generate_plain_chain_spec_with_runtime(
	wasm: Vec<u8>,
	plain_chain_spec: &Path,
	preset: &str,
	name: Option<&str>,
	id: Option<&str>,
) -> Result<(), Error> {
	let mut chain_spec = GenericChainSpec::<NoExtension>::builder(&wasm[..], None)
		.with_genesis_config_preset_name(preset.trim());

	if let Some(name) = name {
		chain_spec = chain_spec.with_name(name);
	}

	if let Some(id) = id {
		chain_spec = chain_spec.with_id(id);
	}

	let chain_spec = chain_spec.build().as_json(false).map_err(|e| anyhow::anyhow!(e))?;
	fs::write(plain_chain_spec, chain_spec)?;

	Ok(())
}

/// Extracts and exports the WebAssembly runtime from a raw chain specification.
///
/// # Arguments
/// * `raw_chain_spec` - The path to the raw chain specification file to extract the runtime from.
/// * `wasm_file_name` - The name of the file where the extracted runtime will be saved.
///
/// # Returns
/// The path to the generated WASM runtime file wrapped in a Result.
///
/// # Errors
/// Returns an error if:
/// - The chain specification file cannot be read or parsed.
/// - The runtime cannot be extracted from the chain spec.
/// - The runtime cannot be written to the output file.
pub fn export_wasm_file_with_runtime(
	raw_chain_spec: &Path,
	wasm_file_name: &str,
) -> Result<PathBuf, Error> {
	let chain_spec = GenericChainSpec::<Option<()>>::from_json_file(raw_chain_spec.to_path_buf())
		.map_err(|e| anyhow::anyhow!(e))?;
	let raw_wasm_blob =
		cumulus_client_cli::extract_genesis_wasm(&chain_spec).map_err(|e| anyhow::anyhow!(e))?;
	let wasm_file = raw_chain_spec.parent().unwrap_or(Path::new("./")).join(wasm_file_name);
	fs::write(&wasm_file, raw_wasm_blob)?;
	Ok(wasm_file)
}

/// Generates the plain text chain specification for a chain with its own node.
///
/// # Arguments
/// * `binary_path` - The path to the node binary executable that contains the `build-spec` command.
/// * `plain_chain_spec` - Location of the plain_chain_spec file to be generated.
/// * `default_bootnode` - Whether to include localhost as a bootnode.
/// * `chain` - The chain specification. It can be one of the predefined ones (e.g. dev, local or a
///   custom one) or the path to an existing chain spec.
pub fn generate_plain_chain_spec_with_node(
	binary_path: &Path,
	plain_chain_spec: &Path,
	default_bootnode: bool,
	chain: &str,
) -> Result<(), Error> {
	check_command_exists(binary_path, "build-spec")?;
	let mut args = vec!["build-spec", "--chain", chain];
	if !default_bootnode {
		args.push("--disable-default-bootnode");
	}
	// Create a temporary file.
	let temp_file = tempfile::NamedTempFile::new_in(std::env::temp_dir())?;
	// Run the command and redirect output to the temporary file.
	let output = cmd(binary_path, args)
		.stdout_path(temp_file.path())
		.stderr_capture()
		.unchecked()
		.run()?;
	// Check if the command failed.
	handle_command_error(&output, Error::BuildSpecError)?;
	// Atomically replace the chain spec file with the temporary file.
	temp_file.persist(plain_chain_spec).map_err(|e| {
		Error::AnyhowError(anyhow!(
			"Failed to replace the chain spec file with the temporary file: {e}"
		))
	})?;
	Ok(())
}

/// Generates a raw chain specification file for a chain.
///
/// # Arguments
/// * `binary_path` - The path to the node binary executable that contains the `build-spec` command.
/// * `plain_chain_spec` - Location of the plain chain specification file.
/// * `chain_spec_file_name` - The name of the chain specification file to be generated.
pub fn generate_raw_chain_spec_with_node(
	binary_path: &Path,
	plain_chain_spec: &Path,
	chain_spec_file_name: &str,
) -> Result<PathBuf, Error> {
	if !plain_chain_spec.exists() {
		return Err(Error::MissingChainSpec(plain_chain_spec.display().to_string()));
	}
	check_command_exists(binary_path, "build-spec")?;
	let raw_chain_spec = plain_chain_spec.with_file_name(chain_spec_file_name);
	let output = cmd(
		binary_path,
		vec![
			"build-spec",
			"--chain",
			&plain_chain_spec.display().to_string(),
			"--disable-default-bootnode",
			"--raw",
		],
	)
	.stdout_path(&raw_chain_spec)
	.stderr_capture()
	.unchecked()
	.run()?;
	handle_command_error(&output, Error::BuildSpecError)?;
	Ok(raw_chain_spec)
}

/// Export the WebAssembly runtime for the chain.
///
/// # Arguments
/// * `binary_path` - The path to the node binary executable that contains the `export-genesis-wasm`
///   command.
/// * `raw_chain_spec` - Location of the raw chain specification file.
/// * `wasm_file_name` - The name of the wasm runtime file to be generated.
pub fn export_wasm_file_with_node(
	binary_path: &Path,
	raw_chain_spec: &Path,
	wasm_file_name: &str,
) -> Result<PathBuf, Error> {
	if !raw_chain_spec.exists() {
		return Err(Error::MissingChainSpec(raw_chain_spec.display().to_string()));
	}
	check_command_exists(binary_path, "export-genesis-wasm")?;
	let wasm_file = raw_chain_spec.parent().unwrap_or(Path::new("./")).join(wasm_file_name);
	let output = cmd(
		binary_path,
		vec![
			"export-genesis-wasm",
			"--chain",
			&raw_chain_spec.display().to_string(),
			&wasm_file.display().to_string(),
		],
	)
	.stdout_null()
	.stderr_capture()
	.unchecked()
	.run()?;
	handle_command_error(&output, Error::BuildSpecError)?;
	Ok(wasm_file)
}

/// Generate the chain genesis state.
///
/// # Arguments
/// * `binary_path` - The path to the node binary executable that contains the
///   `export-genesis-state` command.
/// * `raw_chain_spec` - Location of the raw chain specification file.
/// * `genesis_file_name` - The name of the genesis state file to be generated.
pub fn generate_genesis_state_file_with_node(
	binary_path: &Path,
	raw_chain_spec: &Path,
	genesis_file_name: &str,
) -> Result<PathBuf, Error> {
	if !raw_chain_spec.exists() {
		return Err(Error::MissingChainSpec(raw_chain_spec.display().to_string()));
	}
	check_command_exists(binary_path, "export-genesis-state")?;
	let genesis_file = raw_chain_spec.parent().unwrap_or(Path::new("./")).join(genesis_file_name);
	let output = cmd(
		binary_path,
		vec![
			"export-genesis-state",
			"--chain",
			&raw_chain_spec.display().to_string(),
			&genesis_file.display().to_string(),
		],
	)
	.stdout_null()
	.stderr_capture()
	.unchecked()
	.run()?;
	handle_command_error(&output, Error::BuildSpecError)?;
	Ok(genesis_file)
}

/// Checks if a given command exists and can be executed by running it with the "--help" argument.
fn check_command_exists(binary_path: &Path, command: &str) -> Result<(), Error> {
	cmd(binary_path, vec![command, "--help"]).stdout_null().run().map_err(|_err| {
		Error::MissingCommand {
			command: command.to_string(),
			binary: binary_path.display().to_string(),
		}
	})?;
	Ok(())
}

/// A chain specification.
pub struct ChainSpec(Value);
impl ChainSpec {
	/// Parses a chain specification from a path.
	///
	/// # Arguments
	/// * `path` - The path to a chain specification file.
	pub fn from(path: &Path) -> Result<ChainSpec> {
		Ok(ChainSpec(Value::from_str(&fs::read_to_string(path)?)?))
	}

	/// Get the chain type from the chain specification.
	pub fn get_chain_type(&self) -> Option<&str> {
		self.0.get("chainType").and_then(|v| v.as_str())
	}

	/// Get the name from the chain specification.
	pub fn get_name(&self) -> Option<&str> {
		self.0.get("name").and_then(|v| v.as_str())
	}

	/// Get the chain ID from the chain specification.
	pub fn get_chain_id(&self) -> Option<u64> {
		self.0.get("para_id").and_then(|v| v.as_u64())
	}

	/// Get the property `basedOn` from the chain specification.
	pub fn get_property_based_on(&self) -> Option<&str> {
		self.0.get("properties").and_then(|v| v.get("basedOn")).and_then(|v| v.as_str())
	}

	/// Get the protocol ID from the chain specification.
	pub fn get_protocol_id(&self) -> Option<&str> {
		self.0.get("protocolId").and_then(|v| v.as_str())
	}

	/// Get the relay chain from the chain specification.
	pub fn get_relay_chain(&self) -> Option<&str> {
		self.0.get("relay_chain").and_then(|v| v.as_str())
	}

	/// Get the sudo key from the chain specification.
	pub fn get_sudo_key(&self) -> Option<&str> {
		self.0
			.get("genesis")
			.and_then(|genesis| genesis.get("runtimeGenesis"))
			.and_then(|runtime_genesis| runtime_genesis.get("patch"))
			.and_then(|patch| patch.get("sudo"))
			.and_then(|sudo| sudo.get("key"))
			.and_then(|key| key.as_str())
	}

	/// Replaces the chain id with the provided `para_id`.
	///
	/// # Arguments
	/// * `para_id` - The new value for the para_id.
	pub fn replace_para_id(&mut self, para_id: u32) -> Result<(), Error> {
		// Replace para_id
		let root = self
			.0
			.as_object_mut()
			.ok_or_else(|| Error::Config("expected root object".into()))?;
		root.insert("para_id".to_string(), json!(para_id));

		// Replace genesis.runtimeGenesis.patch.parachainInfo.parachainId
		let replace = self.0.pointer_mut("/genesis/runtimeGenesis/patch/parachainInfo/parachainId");
		// If this fails, it means it is a raw chainspec
		if let Some(replace) = replace {
			*replace = json!(para_id);
		}
		Ok(())
	}

	/// Replaces the relay chain name with the given one.
	///
	/// # Arguments
	/// * `relay_name` - The new value for the relay chain field in the specification.
	pub fn replace_relay_chain(&mut self, relay_name: &str) -> Result<(), Error> {
		// Replace relay_chain
		let root = self
			.0
			.as_object_mut()
			.ok_or_else(|| Error::Config("expected root object".into()))?;
		root.insert("relay_chain".to_string(), json!(relay_name));
		Ok(())
	}

	/// Replaces the chain type with the given one.
	///
	/// # Arguments
	/// * `chain_type` - The new value for the chain type.
	pub fn replace_chain_type(&mut self, chain_type: &str) -> Result<(), Error> {
		// Replace chainType
		let replace = self
			.0
			.get_mut("chainType")
			.ok_or_else(|| Error::Config("expected `chainType`".into()))?;
		*replace = json!(chain_type);
		Ok(())
	}

	/// Replaces the protocol ID with the given one.
	///
	/// # Arguments
	/// * `protocol_id` - The new value for the protocolId of the given specification.
	pub fn replace_protocol_id(&mut self, protocol_id: &str) -> Result<(), Error> {
		// Replace protocolId
		let replace = self
			.0
			.get_mut("protocolId")
			.ok_or_else(|| Error::Config("expected `protocolId`".into()))?;
		*replace = json!(protocol_id);
		Ok(())
	}

	/// Replaces the properties with the given ones.
	///
	/// # Arguments
	/// * `raw_properties` - Comma-separated, key-value pairs. Example: "KEY1=VALUE1,KEY2=VALUE2".
	pub fn replace_properties(&mut self, raw_properties: &str) -> Result<(), Error> {
		// Replace properties
		let replace = self
			.0
			.get_mut("properties")
			.ok_or_else(|| Error::Config("expected `properties`".into()))?;
		let mut properties = serde_json::Map::new();
		let mut iter = raw_properties
			.split(',')
			.flat_map(|s| s.split('=').map(|p| p.trim()).collect::<Vec<_>>())
			.collect::<Vec<_>>()
			.into_iter();
		while let Some(key) = iter.next() {
			let value = iter.next().expect("Property value expected but not found");
			properties.insert(key.to_string(), Value::String(value.to_string()));
		}
		*replace = Value::Object(properties);
		Ok(())
	}

	/// Replaces the invulnerables session keys in the chain specification with the provided
	/// `collator_keys`.
	///
	/// # Arguments
	/// * `collator_keys` - A list of new collator keys.
	pub fn replace_collator_keys(&mut self, collator_keys: Vec<String>) -> Result<(), Error> {
		let uses_evm_keys = self
			.0
			.get("properties")
			.and_then(|p| p.get("isEthereum"))
			.and_then(|v| v.as_bool())
			.unwrap_or(false);

		let keys = if uses_evm_keys {
			convert_to_evm_accounts(collator_keys.clone())?
		} else {
			collator_keys.clone()
		};

		let invulnerables = self
			.0
			.get_mut("genesis")
			.ok_or_else(|| Error::Config("expected `genesis`".into()))?
			.get_mut("runtimeGenesis")
			.ok_or_else(|| Error::Config("expected `runtimeGenesis`".into()))?
			.get_mut("patch")
			.ok_or_else(|| Error::Config("expected `patch`".into()))?
			.get_mut("collatorSelection")
			.ok_or_else(|| Error::Config("expected `collatorSelection`".into()))?
			.get_mut("invulnerables")
			.ok_or_else(|| Error::Config("expected `invulnerables`".into()))?;

		*invulnerables = json!(keys);

		let session_keys = keys
			.iter()
			.zip(collator_keys.iter())
			.map(|(address, original_address)| {
				json!([
					address,
					address,
					{ "aura": original_address } // Always the original address
				])
			})
			.collect::<Vec<_>>();

		let session_keys_field = self
			.0
			.get_mut("genesis")
			.ok_or_else(|| Error::Config("expected `genesis`".into()))?
			.get_mut("runtimeGenesis")
			.ok_or_else(|| Error::Config("expected `runtimeGenesis`".into()))?
			.get_mut("patch")
			.ok_or_else(|| Error::Config("expected `patch`".into()))?
			.get_mut("session")
			.ok_or_else(|| Error::Config("expected `session`".into()))?
			.get_mut("keys")
			.ok_or_else(|| Error::Config("expected `session.keys`".into()))?;

		*session_keys_field = json!(session_keys);

		Ok(())
	}

	/// Converts the chain specification to a string.
	pub fn to_string(&self) -> Result<String> {
		Ok(serde_json::to_string_pretty(&self.0)?)
	}

	/// Writes the chain specification to a file.
	///
	/// # Arguments
	/// * `path` - The path to the chain specification file.
	pub fn to_file(&self, path: &Path) -> Result<()> {
		fs::write(path, self.to_string()?)?;
		Ok(())
	}

	/// Updates the runtime code in the chain specification.
	///
	/// # Arguments
	/// * `bytes` - The new runtime code.
	pub fn update_runtime_code(&mut self, bytes: &[u8]) -> Result<(), Error> {
		// Replace `genesis.runtimeGenesis.code`
		let code = self
			.0
			.get_mut("genesis")
			.ok_or_else(|| Error::Config("expected `genesis`".into()))?
			.get_mut("runtimeGenesis")
			.ok_or_else(|| Error::Config("expected `runtimeGenesis`".into()))?
			.get_mut("code")
			.ok_or_else(|| Error::Config("expected `runtimeGenesis.code`".into()))?;
		let hex = to_hex(bytes, true);
		*code = json!(hex);
		Ok(())
	}
}

#[cfg(test)]
mod tests {
	use super::*;
	use crate::{
		Config, Error, new_chain::instantiate_standard_template, templates::ChainTemplate,
		up::Zombienet,
	};
	use anyhow::Result;
	use pop_common::{
		manifest::{Dependency, add_feature},
		set_executable_permission,
	};
	use sp_core::bytes::from_hex;
	use std::{
		fs::{self, write},
		io::Write,
		path::Path,
	};
	use strum::VariantArray;
	use tempfile::{Builder, TempDir, tempdir};

	static MOCK_WASM: &[u8] = include_bytes!("../../../../tests/runtimes/base_parachain.wasm");

	fn setup_template_and_instantiate() -> Result<TempDir> {
		let temp_dir = tempdir().expect("Failed to create temp dir");
		let config = Config {
			symbol: "DOT".to_string(),
			decimals: 18,
			initial_endowment: "1000000".to_string(),
		};
		instantiate_standard_template(&ChainTemplate::Standard, temp_dir.path(), config, None)?;
		Ok(temp_dir)
	}

	// Function that mocks the build process generating the target dir and release.
	fn mock_build_process(temp_dir: &Path) -> Result<(), Error> {
		// Create a target directory
		let target_dir = temp_dir.join("target");
		fs::create_dir(&target_dir)?;
		fs::create_dir(target_dir.join("release"))?;
		// Create a release file
		fs::File::create(target_dir.join("release/parachain-template-node"))?;
		Ok(())
	}

	// Function create a mocked node directory with Cargo.toml
	fn mock_node(temp_dir: &Path) -> Result<(), Error> {
		let node_dir = temp_dir.join("node");
		fs::create_dir(&node_dir)?;
		fs::write(
			node_dir.join("Cargo.toml"),
			r#"[package]
name = "parachain-template-node"
version = "0.1.0"
edition = "2021"
"#,
		)?;
		Ok(())
	}

	// Function that mocks the build process of WASM runtime generating the target dir and release.
	fn mock_build_runtime_process(temp_dir: &Path) -> Result<(), Error> {
		let runtime = "parachain-template-runtime";
		// Create a target directory
		let target_dir = temp_dir.join("target");
		fs::create_dir(&target_dir)?;
		fs::create_dir(target_dir.join("release"))?;
		fs::create_dir(target_dir.join("release/wbuild"))?;
		fs::create_dir(target_dir.join(format!("release/wbuild/{runtime}")))?;
		// Create a WASM binary file
		fs::File::create(
			target_dir.join(format!("release/wbuild/{runtime}/{}.wasm", runtime.replace("-", "_"))),
		)?;
		Ok(())
	}

	// Function that generates a Cargo.toml inside node directory for testing.
	fn generate_mock_node(temp_dir: &Path, name: Option<&str>) -> Result<PathBuf, Error> {
		// Create a node directory
		let target_dir = temp_dir.join(name.unwrap_or("node"));
		fs::create_dir(&target_dir)?;
		// Create a Cargo.toml file
		let mut toml_file = fs::File::create(target_dir.join("Cargo.toml"))?;
		writeln!(
			toml_file,
			r#"
			[package]
			name = "parachain_template_node"
			version = "0.1.0"

			[dependencies]

			"#
		)?;
		Ok(target_dir)
	}

	// Function that fetch a binary from pop network
	async fn fetch_binary(cache: &Path) -> Result<String, Error> {
		let config = Builder::new().suffix(".toml").tempfile()?;
		writeln!(
			config.as_file(),
			r#"
            [relaychain]
            chain = "paseo-local"

			[[parachains]]
			id = 4385
			default_command = "pop-node"
			"#
		)?;
		let mut zombienet = Zombienet::new(
			cache,
			config.path().try_into()?,
			None,
			None,
			None,
			None,
			Some(&vec!["https://github.com/r0gue-io/pop-node#node-v0.3.0".to_string()]),
		)
		.await?;
		let mut binary_name: String = "".to_string();
		for binary in zombienet.binaries().filter(|b| !b.exists() && b.name() == "pop-node") {
			binary_name = format!("{}-{}", binary.name(), binary.version().unwrap());
			binary.source(true, &(), true).await?;
		}
		Ok(binary_name)
	}

	// Replace the binary fetched with the mocked binary
	fn replace_mock_with_binary(temp_dir: &Path, binary_name: String) -> Result<PathBuf, Error> {
		let binary_path = temp_dir.join(binary_name);
		let content = fs::read(&binary_path)?;
		write(temp_dir.join("target/release/parachain-template-node"), content)?;
		// Make executable
		set_executable_permission(temp_dir.join("target/release/parachain-template-node"))?;
		Ok(binary_path)
	}

	fn add_production_profile(project: &Path) -> Result<()> {
		let root_toml_path = project.join("Cargo.toml");
		let mut root_toml_content = fs::read_to_string(&root_toml_path)?;
		root_toml_content.push_str(
			r#"
			[profile.production]
			codegen-units = 1
			inherits = "release"
			lto = true
			"#,
		);
		// Write the updated content back to the file
		write(&root_toml_path, root_toml_content)?;
		Ok(())
	}

	#[test]
	fn build_chain_works() -> Result<()> {
		let name = "parachain_template_node";
		let temp_dir = tempdir()?;
		cmd("cargo", ["new", name, "--bin"]).dir(temp_dir.path()).run()?;
		let project = temp_dir.path().join(name);
		add_production_profile(&project)?;
		add_feature(&project, ("dummy-feature".to_string(), vec![]))?;
		for node in [None, Some("custom_node")] {
			let node_path = generate_mock_node(&project, node)?;
			for package in [None, Some(String::from("parachain_template_node"))] {
				for profile in Profile::VARIANTS {
					let node_path = node.map(|_| node_path.as_path());
					let binary = build_chain(
						&project,
						package.clone(),
						profile,
						node_path,
						&["dummy-feature".to_string()],
						false,
					)?;
					let target_directory = profile.target_directory(&project);
					assert!(target_directory.exists());
					assert!(target_directory.join("parachain_template_node").exists());
					assert_eq!(
						binary.display().to_string(),
						target_directory.join("parachain_template_node").display().to_string()
					);
				}
			}
		}
		Ok(())
	}

	#[test]
	fn build_project_works() -> Result<()> {
		let name = "example_project";
		let temp_dir = tempdir()?;
		cmd("cargo", ["new", name, "--bin"]).dir(temp_dir.path()).run()?;
		let project = temp_dir.path().join(name);
		add_production_profile(&project)?;
		add_feature(&project, ("dummy-feature".to_string(), vec![]))?;
		for package in [None, Some(String::from(name))] {
			for profile in Profile::VARIANTS {
				build_project(
					&project,
					package.clone(),
					profile,
					&["dummy-feature".to_string()],
					None,
					false,
				)?;
				let target_directory = profile.target_directory(&project);
				let binary = build_binary_path(&project, |runtime_name| {
					target_directory.join(runtime_name)
				})?;
				assert!(target_directory.exists());
				assert!(target_directory.join(name).exists());
				assert_eq!(
					binary.display().to_string(),
					target_directory.join(name).display().to_string()
				);
			}
		}
		Ok(())
	}

	#[test]
	fn binary_path_of_node_works() -> Result<()> {
		let temp_dir =
			setup_template_and_instantiate().expect("Failed to setup template and instantiate");
		mock_build_process(temp_dir.path())?;
		mock_node(temp_dir.path())?;
		let release_path =
			binary_path(&temp_dir.path().join("target/release"), &temp_dir.path().join("node"))?;
		assert_eq!(
			release_path.display().to_string(),
			format!("{}/target/release/parachain-template-node", temp_dir.path().display())
		);
		Ok(())
	}

	#[test]
	fn binary_path_of_runtime_works() -> Result<()> {
		let temp_dir =
			setup_template_and_instantiate().expect("Failed to setup template and instantiate");
		// Ensure binary path works for the runtime.
		let runtime = "parachain-template-runtime";
		mock_build_runtime_process(temp_dir.path())?;
		let release_path = runtime_binary_path(
			&temp_dir.path().join("target/release/wbuild"),
			&temp_dir.path().join("runtime"),
		)?;
		assert_eq!(
			release_path.display().to_string(),
			format!(
				"{}/target/release/wbuild/{runtime}/{}.wasm",
				temp_dir.path().display(),
				runtime.replace("-", "_")
			)
		);

		Ok(())
	}

	#[test]
	fn binary_path_fails_missing_binary() -> Result<()> {
		let temp_dir =
			setup_template_and_instantiate().expect("Failed to setup template and instantiate");
		mock_node(temp_dir.path())?;
		assert!(matches!(
			binary_path(&temp_dir.path().join("target/release"), &temp_dir.path().join("node")),
			Err(Error::MissingBinary(error)) if error == "parachain-template-node"
		));
		Ok(())
	}

	#[tokio::test]
	async fn generate_files_works() -> Result<()> {
		let temp_dir =
			setup_template_and_instantiate().expect("Failed to setup template and instantiate");
		mock_build_process(temp_dir.path())?;
		let binary_name = fetch_binary(temp_dir.path()).await?;
		let binary_path = replace_mock_with_binary(temp_dir.path(), binary_name)?;
		// Test generate chain spec
		let plain_chain_spec = &temp_dir.path().join("plain-parachain-chainspec.json");
		generate_plain_chain_spec_with_node(
			&binary_path,
			&temp_dir.path().join("plain-parachain-chainspec.json"),
			false,
			"local",
		)?;
		assert!(plain_chain_spec.exists());
		{
			let mut chain_spec = ChainSpec::from(plain_chain_spec)?;
			chain_spec.replace_para_id(2001)?;
			chain_spec.to_file(plain_chain_spec)?;
		}
		let raw_chain_spec = generate_raw_chain_spec_with_node(
			&binary_path,
			plain_chain_spec,
			"raw-parachain-chainspec.json",
		)?;
		assert!(raw_chain_spec.exists());
		let content = fs::read_to_string(raw_chain_spec.clone()).expect("Could not read file");
		assert!(content.contains("\"para_id\": 2001"));
		assert!(content.contains("\"bootNodes\": []"));
		// Test export wasm file
		let wasm_file =
			export_wasm_file_with_node(&binary_path, &raw_chain_spec, "para-2001-wasm")?;
		assert!(wasm_file.exists());
		// Test generate chain state file
		let genesis_file = generate_genesis_state_file_with_node(
			&binary_path,
			&raw_chain_spec,
			"para-2001-genesis-state",
		)?;
		assert!(genesis_file.exists());
		Ok(())
	}

	#[test]
	fn generate_plain_chain_spec_with_runtime_works_with_name_and_id_override() -> Result<()> {
		let temp_dir = tempdir()?;
		// Test generate chain spec
		let plain_chain_spec = &temp_dir.path().join("plain-parachain-chainspec.json");
		generate_plain_chain_spec_with_runtime(
			Vec::from(MOCK_WASM),
			plain_chain_spec,
			"local_testnet",
			Some("POP Chain Spec"),
			Some("pop-chain-spec"),
		)?;
		assert!(plain_chain_spec.exists());
		let raw_chain_spec =
			generate_raw_chain_spec_with_runtime(plain_chain_spec, "raw-parachain-chainspec.json")?;
		assert!(raw_chain_spec.exists());
		let content = fs::read_to_string(raw_chain_spec.clone()).expect("Could not read file");
		assert!(content.contains("\"name\": \"POP Chain Spec\""));
		assert!(content.contains("\"id\": \"pop-chain-spec\""));
		assert!(content.contains("\"bootNodes\": []"));
		Ok(())
	}

	#[test]
	fn generate_plain_chain_spec_with_runtime_works_without_name_and_id_override() -> Result<()> {
		let temp_dir = tempdir()?;
		// Test generate chain spec
		let plain_chain_spec = &temp_dir.path().join("plain-parachain-chainspec.json");
		generate_plain_chain_spec_with_runtime(
			Vec::from(MOCK_WASM),
			plain_chain_spec,
			"local_testnet",
			None,
			None,
		)?;
		assert!(plain_chain_spec.exists());
		let raw_chain_spec =
			generate_raw_chain_spec_with_runtime(plain_chain_spec, "raw-parachain-chainspec.json")?;
		assert!(raw_chain_spec.exists());
		let content = fs::read_to_string(raw_chain_spec.clone()).expect("Could not read file");
		assert!(content.contains("\"name\": \"Development\""));
		assert!(content.contains("\"id\": \"dev\""));
		assert!(content.contains("\"bootNodes\": []"));
		Ok(())
	}

	#[tokio::test]
	async fn fails_to_generate_plain_chain_spec_when_file_missing() -> Result<()> {
		let temp_dir =
			setup_template_and_instantiate().expect("Failed to setup template and instantiate");
		mock_build_process(temp_dir.path())?;
		let binary_name = fetch_binary(temp_dir.path()).await?;
		let binary_path = replace_mock_with_binary(temp_dir.path(), binary_name)?;
		assert!(matches!(
			generate_plain_chain_spec_with_node(
				&binary_path,
				&temp_dir.path().join("plain-parachain-chainspec.json"),
				false,
				&temp_dir.path().join("plain-parachain-chainspec.json").display().to_string(),
			),
			Err(Error::BuildSpecError(message)) if message.contains("No such file or directory")
		));
		assert!(!temp_dir.path().join("plain-parachain-chainspec.json").exists());
		Ok(())
	}

	#[test]
	fn raw_chain_spec_fails_wrong_chain_spec() -> Result<()> {
		assert!(matches!(
			generate_raw_chain_spec_with_node(
				Path::new("./binary"),
				Path::new("./plain-parachain-chainspec.json"),
				"plain-parachain-chainspec.json"
			),
			Err(Error::MissingChainSpec(..))
		));
		Ok(())
	}

	#[test]
	fn export_wasm_file_fails_wrong_chain_spec() -> Result<()> {
		assert!(matches!(
			export_wasm_file_with_node(
				Path::new("./binary"),
				Path::new("./raw-parachain-chainspec"),
				"para-2001-wasm"
			),
			Err(Error::MissingChainSpec(..))
		));
		Ok(())
	}

	#[test]
	fn generate_genesis_state_file_wrong_chain_spec() -> Result<()> {
		assert!(matches!(
			generate_genesis_state_file_with_node(
				Path::new("./binary"),
				Path::new("./raw-parachain-chainspec"),
				"para-2001-genesis-state",
			),
			Err(Error::MissingChainSpec(..))
		));
		Ok(())
	}

	#[test]
	fn get_chain_type_works() -> Result<()> {
		let chain_spec = ChainSpec(json!({
			"chainType": "test",
		}));
		assert_eq!(chain_spec.get_chain_type(), Some("test"));
		Ok(())
	}

	#[test]
	fn get_chain_name_works() -> Result<()> {
		assert_eq!(ChainSpec(json!({})).get_name(), None);
		let chain_spec = ChainSpec(json!({
			"name": "test",
		}));
		assert_eq!(chain_spec.get_name(), Some("test"));
		Ok(())
	}

	#[test]
	fn get_chain_id_works() -> Result<()> {
		let chain_spec = ChainSpec(json!({
			"para_id": 2002,
		}));
		assert_eq!(chain_spec.get_chain_id(), Some(2002));
		Ok(())
	}

	#[test]
	fn get_property_based_on_works() -> Result<()> {
		assert_eq!(ChainSpec(json!({})).get_property_based_on(), None);
		let chain_spec = ChainSpec(json!({
			"properties": {
				"basedOn": "test",
			}
		}));
		assert_eq!(chain_spec.get_property_based_on(), Some("test"));
		Ok(())
	}

	#[test]
	fn get_protocol_id_works() -> Result<()> {
		let chain_spec = ChainSpec(json!({
			"protocolId": "test",
		}));
		assert_eq!(chain_spec.get_protocol_id(), Some("test"));
		Ok(())
	}

	#[test]
	fn get_relay_chain_works() -> Result<()> {
		let chain_spec = ChainSpec(json!({
			"relay_chain": "test",
		}));
		assert_eq!(chain_spec.get_relay_chain(), Some("test"));
		Ok(())
	}

	#[test]
	fn get_sudo_key_works() -> Result<()> {
		assert_eq!(ChainSpec(json!({})).get_sudo_key(), None);
		let chain_spec = ChainSpec(json!({
			"para_id": 1000,
			"genesis": {
				"runtimeGenesis": {
					"patch": {
						"sudo": {
							"key": "sudo-key"
						}
					}
				}
			},
		}));
		assert_eq!(chain_spec.get_sudo_key(), Some("sudo-key"));
		Ok(())
	}

	#[test]
	fn replace_para_id_works() -> Result<()> {
		let mut chain_spec = ChainSpec(json!({
			"para_id": 1000,
			"genesis": {
				"runtimeGenesis": {
					"patch": {
						"parachainInfo": {
							"parachainId": 1000
						}
					}
				}
			},
		}));
		chain_spec.replace_para_id(2001)?;
		assert_eq!(
			chain_spec.0,
			json!({
				"para_id": 2001,
				"genesis": {
					"runtimeGenesis": {
						"patch": {
							"parachainInfo": {
								"parachainId": 2001
							}
						}
					}
				},
			})
		);
		Ok(())
	}

	#[test]
	fn replace_para_id_fails() -> Result<()> {
		let mut chain_spec = ChainSpec(json!({
			"para_id": 2001,
			"": {
				"runtimeGenesis": {
					"patch": {
						"parachainInfo": {
							"parachainId": 1000
						}
					}
				}
			},
		}));
		assert!(chain_spec.replace_para_id(2001).is_ok());
		chain_spec = ChainSpec(json!({
			"para_id": 2001,
			"genesis": {
				"": {
					"patch": {
						"parachainInfo": {
							"parachainId": 1000
						}
					}
				}
			},
		}));
		assert!(chain_spec.replace_para_id(2001).is_ok());
		chain_spec = ChainSpec(json!({
			"para_id": 2001,
			"genesis": {
				"runtimeGenesis": {
					"": {
						"parachainInfo": {
							"parachainId": 1000
						}
					}
				}
			},
		}));
		assert!(chain_spec.replace_para_id(2001).is_ok());
		chain_spec = ChainSpec(json!({
			"para_id": 2001,
			"genesis": {
				"runtimeGenesis": {
					"patch": {
						"": {
							"parachainId": 1000
						}
					}
				}
			},
		}));
		assert!(chain_spec.replace_para_id(2001).is_ok());
		chain_spec = ChainSpec(json!({
			"para_id": 2001,
			"genesis": {
				"runtimeGenesis": {
					"patch": {
						"parachainInfo": {
						}
					}
				}
			},
		}));
		assert!(chain_spec.replace_para_id(2001).is_ok());
		Ok(())
	}

	#[test]
	fn replace_relay_chain_works() -> Result<()> {
		let mut chain_spec = ChainSpec(json!({"relay_chain": "old-relay"}));
		chain_spec.replace_relay_chain("new-relay")?;
		assert_eq!(chain_spec.0, json!({"relay_chain": "new-relay"}));
		Ok(())
	}

	#[test]
	fn replace_chain_type_works() -> Result<()> {
		let mut chain_spec = ChainSpec(json!({"chainType": "old-chainType"}));
		chain_spec.replace_chain_type("new-chainType")?;
		assert_eq!(chain_spec.0, json!({"chainType": "new-chainType"}));
		Ok(())
	}

	#[test]
	fn replace_chain_type_fails() -> Result<()> {
		let mut chain_spec = ChainSpec(json!({"": "old-chainType"}));
		assert!(
			matches!(chain_spec.replace_chain_type("new-chainType"), Err(Error::Config(error)) if error == "expected `chainType`")
		);
		Ok(())
	}

	#[test]
	fn replace_protocol_id_works() -> Result<()> {
		let mut chain_spec = ChainSpec(json!({"protocolId": "old-protocolId"}));
		chain_spec.replace_protocol_id("new-protocolId")?;
		assert_eq!(chain_spec.0, json!({"protocolId": "new-protocolId"}));
		Ok(())
	}

	#[test]
	fn replace_protocol_id_fails() -> Result<()> {
		let mut chain_spec = ChainSpec(json!({"": "old-protocolId"}));
		assert!(
			matches!(chain_spec.replace_protocol_id("new-protocolId"), Err(Error::Config(error)) if error == "expected `protocolId`")
		);
		Ok(())
	}

	#[test]
	fn replace_collator_keys_works() -> Result<()> {
		let mut chain_spec = ChainSpec(json!({
			"para_id": 1000,
			"genesis": {
				"runtimeGenesis": {
					"patch": {
						"collatorSelection": {
							"invulnerables": [
							  "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY",
							  "5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty"
							]
						  },
						  "session": {
							"keys": [
							  [
								"5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY",
								"5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY",
								{
								  "aura": "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY"
								}
							  ],
							  [
								"5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty",
								"5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty",
								{
								  "aura": "5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty"
								}
							  ]
							]
						  },
					}
				}
			},
		}));
		chain_spec.replace_collator_keys(vec![
			"5Gw3s7q4QLkSWwknsi8jj5P1K79e5N4b6pfsNUzS97H1DXYF".to_string(),
		])?;
		assert_eq!(
			chain_spec.0,
			json!({
				"para_id": 1000,
				"genesis": {
				"runtimeGenesis": {
					"patch": {
						"collatorSelection": {
							"invulnerables": [
							  "5Gw3s7q4QLkSWwknsi8jj5P1K79e5N4b6pfsNUzS97H1DXYF",
							]
						  },
						  "session": {
							"keys": [
							  [
								"5Gw3s7q4QLkSWwknsi8jj5P1K79e5N4b6pfsNUzS97H1DXYF",
								"5Gw3s7q4QLkSWwknsi8jj5P1K79e5N4b6pfsNUzS97H1DXYF",
								{
								  "aura": "5Gw3s7q4QLkSWwknsi8jj5P1K79e5N4b6pfsNUzS97H1DXYF"
								}
							  ],
							]
						  },
					}
				}
			},
			})
		);
		Ok(())
	}

	#[test]
	fn replace_use_evm_collator_keys_works() -> Result<()> {
		let mut chain_spec = ChainSpec(json!({
			"para_id": 1000,
			"properties": {
				"isEthereum": true
			},
			"genesis": {
				"runtimeGenesis": {
					"patch": {
						"collatorSelection": {
							"invulnerables": [
							  "5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty"
							]
						  },
						  "session": {
							"keys": [
							  [
								"5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty",
								"5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty",
								{
								  "aura": "5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty"
								}
							  ]
							]
						  },
					}
				}
			},
		}));
		chain_spec.replace_collator_keys(vec![
			"5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY".to_string(),
		])?;
		assert_eq!(
			chain_spec.0,
			json!({
				"para_id": 1000,
				"properties": {
					"isEthereum": true
				},
				"genesis": {
				"runtimeGenesis": {
					"patch": {
						"collatorSelection": {
							"invulnerables": [
							  "0x9621dde636de098b43efb0fa9b61facfe328f99d",
							]
						  },
						  "session": {
							"keys": [
							  [
								"0x9621dde636de098b43efb0fa9b61facfe328f99d",
								"0x9621dde636de098b43efb0fa9b61facfe328f99d",
								{
								  "aura": "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY"
								}
							  ],
							]
						  },
					}
				}
			},
			})
		);
		Ok(())
	}

	#[test]
	fn update_runtime_code_works() -> Result<()> {
		let mut chain_spec =
			ChainSpec(json!({"genesis": {"runtimeGenesis" : {  "code": "0x00" }}}));

		chain_spec.update_runtime_code(&from_hex("0x1234")?)?;
		assert_eq!(chain_spec.0, json!({"genesis": {"runtimeGenesis" : {  "code": "0x1234" }}}));
		Ok(())
	}

	#[test]
	fn update_runtime_code_fails() -> Result<()> {
		let mut chain_spec =
			ChainSpec(json!({"invalidKey": {"runtimeGenesis" : {  "code": "0x00" }}}));
		assert!(
			matches!(chain_spec.update_runtime_code(&from_hex("0x1234")?), Err(Error::Config(error)) if error == "expected `genesis`")
		);

		chain_spec = ChainSpec(json!({"genesis": {"invalidKey" : {  "code": "0x00" }}}));
		assert!(
			matches!(chain_spec.update_runtime_code(&from_hex("0x1234")?), Err(Error::Config(error)) if error == "expected `runtimeGenesis`")
		);

		chain_spec = ChainSpec(json!({"genesis": {"runtimeGenesis" : {  "invalidKey": "0x00" }}}));
		assert!(
			matches!(chain_spec.update_runtime_code(&from_hex("0x1234")?), Err(Error::Config(error)) if error == "expected `runtimeGenesis.code`")
		);
		Ok(())
	}

	#[test]
	fn check_command_exists_fails() -> Result<()> {
		let binary_path = PathBuf::from("/bin");
		let cmd = "nonexistent_command";
		assert!(matches!(
			check_command_exists(&binary_path, cmd),
			Err(Error::MissingCommand {command, binary })
			if command == cmd && binary == binary_path.display().to_string()
		));
		Ok(())
	}

	#[test]
	fn is_supported_works() -> Result<()> {
		let temp_dir = tempdir()?;
		let path = temp_dir.path();

		// Standard rust project
		let name = "hello_world";
		cmd("cargo", ["new", name]).dir(path).run()?;
		assert!(!is_supported(&path.join(name)));

		// Chain
		let mut manifest = from_path(&path.join(name))?;
		manifest
			.dependencies
			.insert("cumulus-client-collator".into(), Dependency::Simple("^0.14.0".into()));
		let manifest = toml_edit::ser::to_string_pretty(&manifest)?;
		write(path.join(name).join("Cargo.toml"), manifest)?;
		assert!(is_supported(&path.join(name)));
		Ok(())
	}

	#[test]
	fn chain_spec_builder_node_path_works() -> Result<()> {
		let node_path = PathBuf::from("/test/node");
		let builder = ChainSpecBuilder::Node {
			node_path: node_path.clone(),
			default_bootnode: true,
			profile: Profile::Release,
		};
		assert_eq!(builder.path(), node_path);
		Ok(())
	}

	#[test]
	fn chain_spec_builder_runtime_path_works() -> Result<()> {
		let runtime_path = PathBuf::from("/test/runtime");
		let builder = ChainSpecBuilder::Runtime {
			runtime_path: runtime_path.clone(),
			profile: Profile::Release,
		};
		assert_eq!(builder.path(), runtime_path);
		Ok(())
	}

	#[test]
	fn chain_spec_builder_node_profile_works() -> Result<()> {
		for profile in Profile::VARIANTS {
			let builder = ChainSpecBuilder::Node {
				node_path: PathBuf::from("/test/node"),
				default_bootnode: true,
				profile: *profile,
			};
			assert_eq!(builder.profile(), *profile);
		}
		Ok(())
	}

	#[test]
	fn chain_spec_builder_runtime_profile_works() -> Result<()> {
		for profile in Profile::VARIANTS {
			let builder = ChainSpecBuilder::Runtime {
				runtime_path: PathBuf::from("/test/runtime"),
				profile: *profile,
			};
			assert_eq!(builder.profile(), *profile);
		}
		Ok(())
	}

	#[test]
	fn chain_spec_builder_node_artifact_path_works() -> Result<()> {
		let temp_dir =
			setup_template_and_instantiate().expect("Failed to setup template and instantiate");
		mock_build_process(temp_dir.path())?;
		mock_node(temp_dir.path())?;
		let builder = ChainSpecBuilder::Node {
			node_path: temp_dir.path().join("node"),
			default_bootnode: true,
			profile: Profile::Release,
		};
		let artifact_path = builder.artifact_path()?;
		assert!(artifact_path.exists());
		assert!(artifact_path.ends_with("parachain-template-node"));
		Ok(())
	}

	#[test]
	fn chain_spec_builder_runtime_artifact_path_works() -> Result<()> {
		let temp_dir =
			setup_template_and_instantiate().expect("Failed to setup template and instantiate");
		mock_build_runtime_process(temp_dir.path())?;

		let builder = ChainSpecBuilder::Runtime {
			runtime_path: temp_dir.path().join("runtime"),
			profile: Profile::Release,
		};
		let artifact_path = builder.artifact_path()?;
		assert!(artifact_path.is_file());
		assert!(artifact_path.ends_with("parachain_template_runtime.wasm"));
		Ok(())
	}

	#[test]
	fn chain_spec_builder_node_artifact_path_fails() -> Result<()> {
		let temp_dir =
			setup_template_and_instantiate().expect("Failed to setup template and instantiate");

		let builder = ChainSpecBuilder::Node {
			node_path: temp_dir.path().join("node"),
			default_bootnode: true,
			profile: Profile::Release,
		};
		assert!(builder.artifact_path().is_err());
		Ok(())
	}

	#[test]
	fn chain_spec_builder_runtime_artifact_path_fails() -> Result<()> {
		let temp_dir =
			setup_template_and_instantiate().expect("Failed to setup template and instantiate");

		let builder = ChainSpecBuilder::Runtime {
			runtime_path: temp_dir.path().join("runtime"),
			profile: Profile::Release,
		};
		let result = builder.artifact_path();
		assert!(result.is_err());
		assert!(matches!(result, Err(e) if e.to_string().contains("No runtime found")));
		Ok(())
	}

	#[test]
	fn chain_spec_builder_generate_raw_chain_spec_works() -> Result<()> {
		let temp_dir = tempdir()?;
		let builder = ChainSpecBuilder::Runtime {
			runtime_path: temp_dir.path().join("runtime"),
			profile: Profile::Release,
		};
		let original_chain_spec_path =
			PathBuf::from("artifacts/passet-hub-spec.json").canonicalize()?;
		assert!(original_chain_spec_path.exists());
		let chain_spec_path = temp_dir.path().join(original_chain_spec_path.file_name().unwrap());
		fs::copy(&original_chain_spec_path, &chain_spec_path)?;
		let raw_chain_spec_path = temp_dir.path().join("raw.json");
		let final_raw_path = builder.generate_raw_chain_spec(
			&chain_spec_path,
			raw_chain_spec_path.file_name().unwrap().to_str().unwrap(),
		)?;
		assert!(final_raw_path.is_file());
		assert_eq!(final_raw_path, raw_chain_spec_path);

		// Check raw chain spec contains expected fields
		let raw_content = fs::read_to_string(&raw_chain_spec_path)?;
		let raw_json: Value = serde_json::from_str(&raw_content)?;
		assert!(raw_json.get("genesis").is_some());
		assert!(raw_json.get("genesis").unwrap().get("raw").is_some());
		assert!(raw_json.get("genesis").unwrap().get("raw").unwrap().get("top").is_some());
		Ok(())
	}

	#[test]
	fn chain_spec_builder_export_wasm_works() -> Result<()> {
		let temp_dir = tempdir()?;
		let builder = ChainSpecBuilder::Runtime {
			runtime_path: temp_dir.path().join("runtime"),
			profile: Profile::Release,
		};
		let original_chain_spec_path =
			PathBuf::from("artifacts/passet-hub-spec.json").canonicalize()?;
		let chain_spec_path = temp_dir.path().join(original_chain_spec_path.file_name().unwrap());
		fs::copy(&original_chain_spec_path, &chain_spec_path)?;
		let final_wasm_path = temp_dir.path().join("runtime.wasm");
		let final_raw_path = builder.generate_raw_chain_spec(&chain_spec_path, "raw.json")?;
		let wasm_path = builder.export_wasm_file(
			&final_raw_path,
			final_wasm_path.file_name().unwrap().to_str().unwrap(),
		)?;
		assert!(wasm_path.is_file());
		assert_eq!(final_wasm_path, wasm_path);
		Ok(())
	}

	#[test]
	fn fetch_dependencies_works() -> Result<()> {
		let name = "fetch_test";
		let temp_dir = tempdir()?;
		cmd("cargo", ["new", name, "--bin"]).dir(temp_dir.path()).run()?;
		let project = temp_dir.path().join(name);
		fetch_dependencies(&project)?;
		Ok(())
	}

	#[test]
	fn fetch_dependencies_handles_invalid_path() {
		assert!(fetch_dependencies(Path::new("/nonexistent/path")).is_err());
	}
}