forked from bnb-chain/reth-bsc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.rs
More file actions
92 lines (79 loc) · 3.19 KB
/
build.rs
File metadata and controls
92 lines (79 loc) · 3.19 KB
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
use std::{collections::BTreeMap, fs};
fn main() {
// Define hardforks that contain system contracts (matching hardfork_to_dir_name function)
let hardforks = vec![
"bruno",
"euler",
"feynman",
"feynman_fix",
"gibbs",
"kepler",
"luban",
"mirror_sync",
"moran",
"niels",
"planck",
"plato",
"ramanujan",
"haber_fix",
"bohr",
"pascal",
"lorentz",
"maxwell",
];
// Rerun if any of the hardfork directories change
for hardfork in &hardforks {
println!("cargo:rerun-if-changed=src/system_contracts/{hardfork}");
}
let contracts_dir = "src/system_contracts";
let mut contract_data: BTreeMap<String, String> = BTreeMap::new();
// Get all hardfork directories
if let Ok(entries) = fs::read_dir(contracts_dir) {
for entry in entries.flatten() {
let path = entry.path();
if path.is_dir() {
let hardfork_name = path.file_name().unwrap().to_string_lossy();
// Skip the abi.rs file and other non-hardfork directories
if hardfork_name == "abi" || hardfork_name.starts_with('.') {
continue;
}
// Process mainnet, chapel and rialto subdirectories
for network in ["mainnet", "chapel", "rialto"] {
let network_path = path.join(network);
if network_path.exists() {
if let Ok(contract_files) = fs::read_dir(&network_path) {
for contract_file in contract_files.flatten() {
let contract_path = contract_file.path();
if contract_path.is_file() {
let contract_name =
contract_path.file_name().unwrap().to_string_lossy();
// Read the contract hex data
if let Ok(hex_data) = fs::read_to_string(&contract_path) {
let hex_data = hex_data.trim();
// Store the contract data
let key =
format!("{hardfork_name}_{network}_{contract_name}");
contract_data.insert(key, hex_data.to_string());
}
}
}
}
}
}
}
}
}
// Generate the Rust code
let mut code = String::new();
code.push_str("// Auto-generated by build.rs - DO NOT EDIT\n");
code.push_str("use phf::phf_map;\n\n");
code.push_str(
"pub static EMBEDDED_CONTRACTS: phf::Map<&'static str, &'static str> = phf_map! {\n",
);
for (key, hex_data) in &contract_data {
code.push_str(&format!(" \"{key}\" => \"{hex_data}\",\n"));
}
code.push_str("};\n");
// Write the generated code
fs::write("src/system_contracts/embedded_contracts.rs", code).unwrap();
}