This repository was archived by the owner on Jul 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 839
/
Copy pathbuild.rs
263 lines (242 loc) · 9.45 KB
/
build.rs
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
use ethers::{
abi::Contract,
core::types::Bytes,
solc::{
artifacts::{CompactContractRef, Error as SolcArtifactError},
CompilerInput, CompilerOutput, EvmVersion, Solc,
},
};
use ethers_contract_abigen::Abigen;
use serde::{Deserialize, Serialize};
use std::{fs::File, path::Path};
use thiserror::Error;
#[derive(Serialize, Deserialize, Debug)]
pub struct CompiledContract {
/// Contract path
pub path: String,
/// Contract name
pub name: String,
/// ABI
pub abi: Contract,
/// Bytecode
pub bin: Bytes,
/// Runtime Bytecode
pub bin_runtime: Bytes,
}
#[derive(Error, Debug)]
enum BuildError {
/// Failed to pull OZ submodule from github
#[error("FailedToPullSubmodule({0:})")]
FailedToPullSubmodule(String),
/// Failed to detect solidity compiler or compiler version.
#[error("FailedToGetSolidity({0:})")]
FailedToGetSolidity(String),
/// Failed to create CompilerInput artifact for given contract
#[error("FailedToComposeCompilerInputs(path: {path:}, {reason:}))")]
FailedToComposeCompilerInputs { path: String, reason: String },
/// Errors or non-ignored warnings exist in the CompilerOutpu struct
#[error("CompilerOutputContainsErrors(path: {path:}, errors: {errors:#?})")]
CompilerOutputContainsErrors {
path: String,
errors: Vec<SolcArtifactError>,
},
/// Vec<CompilerInput> is empty
#[error("ArtifactError")]
ArtifactError,
/// Function compile_output failed to encode CompilerInput to Vec<u8>
#[error("CompileOutputFailure({0:})")]
CompileOutputFailure(String),
/// Could not convert Vec<u8> to CompilerOutput
#[error("CompilerOutputDeSerError({0:})")]
CompilerOutputDeSerError(String),
/// Failed loading Abi from CompactContractRef
#[error("ErrorLoadingContractAbi")]
ErrorLoadingContractAbi,
/// Failed loading Bin from CompactContractRef
#[error("ErrorLoadingContractBin({0:})")]
ErrorLoadingContractBin(String),
/// Failed loading BinRuntime from CompactContractRef
#[error("ErrorLoadingContractBinRuntime({0:})")]
ErrorLoadingContractBinRuntime(String),
/// Failed to convert PathBuff to String
#[error("StringConversionError({0:})")]
StringConversionError(String),
/// Failed to create CompactContractRef from path + name
#[error("FailedToLoadCompactContractRef")]
FailedToLoadCompactContractRef,
/// Failed Bytecode to Bytes conversion
#[error("ByteCodeToBytesError({0:})")]
ByteCodeToBytesError(String),
/// Error serializing json to file
#[error("JsonSerializatonError({0:})")]
JsonSerializatonError(String),
/// Errors from Abigen
#[error("GenerateContractBindingsError({0:})")]
GenerateContractBindingsError(String),
}
/// Path to the test contracts
const CONTRACTS_PATH: &str = "contracts";
/// Solidity compilation warnings to ignore (by error code)
/// 2018: Warning - "Function state mutability can be restricted to pure"
/// 5667: Warning - "Unused function parameter. Remove or comment out the
/// variable name to silence this warning."
/// For smart contracts that are optimized for worst case block generation, we want to allow
/// contracts that do not interfere with state, without setting state mutability to view. otherwise
/// compiler optimizations will not allow recursive execution of targeted opcodes
const WARN: &[u64] = &[2018, 5667];
/// List of contracts as (ContractName, ContractSolidityFile)
const CONTRACTS: &[(&str, &str)] = &[
("Greeter", "Greeter.sol"),
(
"OpenZeppelinERC20TestToken",
"OpenZeppelinERC20TestToken.sol",
),
// Contract to test worst-case usage of opcodes.
("Benchmarks", "BENCHMARKS.sol"),
];
/// Target directory for rust contract bingings
const BINDINGS_DR: &str = "src";
fn main() -> Result<(), BuildError> {
println!("cargo:rerun-if-changed=build.rs");
std::process::Command::new("git")
.args([
"submodule",
"update",
"--init",
"--recursive",
"--checkout",
"contracts/vendor",
])
.spawn()
.and_then(|mut child| child.wait())
.map_err(|err| BuildError::FailedToPullSubmodule(err.to_string()))?;
let solc: Solc = Solc::default();
let _solc_version = solc
.version()
.map_err(|err| BuildError::FailedToGetSolidity(err.to_string()))?;
for (name, contract_path) in CONTRACTS {
let path_sol = Path::new(CONTRACTS_PATH).join(contract_path);
let p = path_sol.to_str().ok_or_else(|| {
BuildError::StringConversionError("Failed to convert provided path to &str".to_string())
})?;
let input = CompilerInput::new(&path_sol)
.map_err(|err| BuildError::FailedToComposeCompilerInputs {
path: p.to_string(),
reason: err.to_string(),
})?
.pop()
.ok_or(BuildError::ArtifactError)?
// ethers-solc: explicitly indicate the EvmVersion that corresponds to the zkevm
// circuit's supported Upgrade, e.g. `London/Shanghai/...` specifications.
.evm_version(EvmVersion::London);
// compilation will either fail with Err variant or return Ok(CompilerOutput)
// which may contain Errors or Warnings
let output: Vec<u8> = solc
.compile_output(&input)
.map_err(|err| BuildError::CompileOutputFailure(err.to_string()))?;
let mut deserializer: serde_json::Deserializer<serde_json::de::SliceRead<'_>> =
serde_json::Deserializer::from_slice(&output);
// The contracts to test the worst-case usage of certain opcodes, such as SDIV, MLOAD, and
// EXTCODESIZE, generate big JSON compilation outputs. We disable the recursion limit to
// avoid parsing failure.
deserializer.disable_recursion_limit();
let compiled = CompilerOutput::deserialize(&mut deserializer)
.map_err(|err| BuildError::CompilerOutputDeSerError(err.to_string()))?;
let compiled_binding = compiled.clone();
let error_free = !{ compiled.has_error() || compiled.has_warning(WARN) };
if !error_free {
return Err(BuildError::CompilerOutputContainsErrors {
path: p.to_string(),
errors: compiled.errors,
});
}
let _ = error_free.then_some(|| ()).ok_or_else(|| {
BuildError::CompilerOutputContainsErrors {
path: p.to_string(),
errors: compiled.errors,
}
})?;
let contract: CompactContractRef = compiled_binding
.get(p, name)
.ok_or(BuildError::FailedToLoadCompactContractRef)?;
let abi = contract
.abi
.ok_or(BuildError::ErrorLoadingContractAbi)?
.clone();
let bin = contract
.bin
.ok_or_else(|| {
BuildError::ErrorLoadingContractBin("Failed to get contract Bin".to_string())
})?
.clone()
.into_bytes()
.ok_or_else(|| {
BuildError::ByteCodeToBytesError("Could not convert bin to bytes".to_string())
})?;
let bin_runtime = contract
.bin_runtime
.ok_or_else(|| {
BuildError::ErrorLoadingContractBinRuntime(
"Failed to get contract bin-runtime".to_string(),
)
})?
.clone()
.into_bytes()
.ok_or_else(|| {
BuildError::ByteCodeToBytesError(
"Could not convert bin-runtime to bytes".to_string(),
)
})?;
let compiled_contract = CompiledContract {
path: p.to_string(),
name: name.to_string(),
abi,
bin,
bin_runtime,
};
// Save CompiledContract object to json files in "contracts" folder
let mut path_json = path_sol.clone();
path_json.set_extension("json");
serde_json::to_writer(
&File::create(&path_json).expect("cannot create file"),
&compiled_contract,
)
.map_err(|err| BuildError::JsonSerializatonError(err.to_string()))?;
}
// Generate contract binding for compiled contracts
for entry in glob::glob("./contracts/*.json").unwrap() {
match entry {
Ok(path) => {
let _ = generate_rust_contract_bindings(BINDINGS_DR, &path);
}
Err(e) => eprintln!("{:#?}", e),
}
}
Ok(())
}
fn generate_rust_contract_bindings(bindings_dir: &str, file: &Path) -> Result<(), BuildError> {
let abi_source = file.to_path_buf();
let tempbinding = abi_source.clone();
let contractname = tempbinding
.file_stem()
.ok_or(BuildError::ArtifactError)?
.to_str()
.ok_or(BuildError::StringConversionError(
"Could not convert &OsStr to &str".to_string(),
))?
.to_lowercase();
let destpath = format!("{}{}{}", "bindings_", contractname, ".rs");
let destpath = Path::new(&bindings_dir).join(destpath);
let _ = Abigen::new(
contractname,
abi_source
.into_os_string()
.into_string()
.expect("FAILED CONVERSION TO STRING"),
)
.map_err(|err| BuildError::GenerateContractBindingsError(err.to_string()))?
.generate()
.map_err(|err| BuildError::GenerateContractBindingsError(err.to_string()))?
.write_to_file(destpath);
Ok(())
}