add common crate

Signed-off-by: xermicus <cyrill@parity.io>
This commit is contained in:
xermicus
2024-03-22 16:19:18 +01:00
parent 50f2dd9b74
commit c52a8d0f20
65 changed files with 839 additions and 271 deletions
+2 -10
View File
@@ -63,11 +63,7 @@ impl Contract {
let file_name = Self::short_path(self.path.as_str());
if output_assembly {
let file_name = format!(
"{}.{}",
file_name,
era_compiler_common::EXTENSION_ERAVM_ASSEMBLY
);
let file_name = format!("{}.{}", file_name, revive_common::EXTENSION_ERAVM_ASSEMBLY);
let mut file_path = path.to_owned();
file_path.push(file_name);
@@ -88,11 +84,7 @@ impl Contract {
}
if output_binary {
let file_name = format!(
"{}.{}",
file_name,
era_compiler_common::EXTENSION_ERAVM_BINARY
);
let file_name = format!("{}.{}", file_name, revive_common::EXTENSION_ERAVM_BINARY);
let mut file_path = path.to_owned();
file_path.push(file_name);
+3 -3
View File
@@ -11,10 +11,10 @@ pub static DEFAULT_EXECUTABLE_NAME: &str = "zksolc";
pub const OFFSET_SCRATCH_SPACE: usize = 0;
/// The memory pointer offset.
pub const OFFSET_MEMORY_POINTER: usize = 2 * era_compiler_common::BYTE_LENGTH_FIELD;
pub const OFFSET_MEMORY_POINTER: usize = 2 * revive_common::BYTE_LENGTH_FIELD;
/// The empty slot offset.
pub const OFFSET_EMPTY_SLOT: usize = 3 * era_compiler_common::BYTE_LENGTH_FIELD;
pub const OFFSET_EMPTY_SLOT: usize = 3 * revive_common::BYTE_LENGTH_FIELD;
/// The non-reserved memory offset.
pub const OFFSET_NON_RESERVED: usize = 4 * era_compiler_common::BYTE_LENGTH_FIELD;
pub const OFFSET_NON_RESERVED: usize = 4 * revive_common::BYTE_LENGTH_FIELD;
@@ -16,7 +16,7 @@ where
let offset = context.builder().build_int_add(
offset,
context.field_const(
(era_compiler_common::BYTE_LENGTH_X32 + era_compiler_common::BYTE_LENGTH_FIELD) as u64,
(revive_common::BYTE_LENGTH_X32 + revive_common::BYTE_LENGTH_FIELD) as u64,
),
"datacopy_contract_hash_offset",
)?;
@@ -61,12 +61,12 @@ where
for (index, chunk) in source
.chars()
.collect::<Vec<char>>()
.chunks(era_compiler_common::BYTE_LENGTH_FIELD * 2)
.chunks(revive_common::BYTE_LENGTH_FIELD * 2)
.enumerate()
{
let mut value_string = chunk.iter().collect::<String>();
value_string.push_str(
"0".repeat((era_compiler_common::BYTE_LENGTH_FIELD * 2) - chunk.len())
"0".repeat((revive_common::BYTE_LENGTH_FIELD * 2) - chunk.len())
.as_str(),
);
@@ -309,7 +309,7 @@ impl Instruction {
..
} => {
let mut key_extended =
"0".repeat(era_compiler_common::BYTE_LENGTH_FIELD * 2 - value.len());
"0".repeat(revive_common::BYTE_LENGTH_FIELD * 2 - value.len());
key_extended.push_str(value.as_str());
*value = mapping.get(key_extended.as_str()).cloned().ok_or_else(|| {
+4 -6
View File
@@ -103,7 +103,7 @@ impl Assembly {
hash_data_mapping: &BTreeMap<String, String>,
) -> anyhow::Result<BTreeMap<String, String>> {
let mut index_path_mapping = BTreeMap::new();
let index = "0".repeat(era_compiler_common::BYTE_LENGTH_FIELD * 2);
let index = "0".repeat(revive_common::BYTE_LENGTH_FIELD * 2);
index_path_mapping.insert(index, full_path.to_owned());
let dependencies = match self.data.as_mut() {
@@ -115,8 +115,7 @@ impl Assembly {
continue;
}
let mut index_extended =
"0".repeat(era_compiler_common::BYTE_LENGTH_FIELD * 2 - index.len());
let mut index_extended = "0".repeat(revive_common::BYTE_LENGTH_FIELD * 2 - index.len());
index_extended.push_str(index.as_str());
*data = match data {
@@ -154,7 +153,7 @@ impl Assembly {
hash_data_mapping: &BTreeMap<String, String>,
) -> anyhow::Result<BTreeMap<String, String>> {
let mut index_path_mapping = BTreeMap::new();
let index = "0".repeat(era_compiler_common::BYTE_LENGTH_FIELD * 2);
let index = "0".repeat(revive_common::BYTE_LENGTH_FIELD * 2);
index_path_mapping.insert(index, full_path.to_owned());
let dependencies = match self
@@ -168,8 +167,7 @@ impl Assembly {
None => return Ok(index_path_mapping),
};
for (index, data) in dependencies.iter_mut() {
let mut index_extended =
"0".repeat(era_compiler_common::BYTE_LENGTH_FIELD * 2 - index.len());
let mut index_extended = "0".repeat(revive_common::BYTE_LENGTH_FIELD * 2 - index.len());
index_extended.push_str(index.as_str());
*data = match data {
@@ -41,10 +41,10 @@ where
.declaration();
let is_deploy_code = match self.code_type {
era_compiler_llvm_context::EraVMCodeType::Deploy => context
.integer_type(era_compiler_common::BIT_LENGTH_BOOLEAN)
.integer_type(revive_common::BIT_LENGTH_BOOLEAN)
.const_int(1, false),
era_compiler_llvm_context::EraVMCodeType::Runtime => context
.integer_type(era_compiler_common::BIT_LENGTH_BOOLEAN)
.integer_type(revive_common::BIT_LENGTH_BOOLEAN)
.const_int(0, false),
};
context.build_invoke(
@@ -170,7 +170,7 @@ where
.value
.ok_or_else(|| anyhow::anyhow!("Instruction value missing"))?;
if value.len() > era_compiler_common::BYTE_LENGTH_FIELD * 2 {
if value.len() > revive_common::BYTE_LENGTH_FIELD * 2 {
Ok(Some(context.field_const(0).as_basic_value_enum()))
} else {
crate::evmla::assembly::instruction::stack::push(context, value).map(Some)
@@ -1319,10 +1319,8 @@ where
pointer,
&[
context.field_const(0),
context.integer_const(
era_compiler_common::BIT_LENGTH_X32,
index as u64,
),
context
.integer_const(revive_common::BIT_LENGTH_X32, index as u64),
],
context.field_type(),
format!("return_value_pointer_element_{}", index).as_str(),
@@ -593,7 +593,7 @@ impl Function {
} => (
vec![num::BigUint::from_str_radix(
constant.as_str(),
era_compiler_common::BASE_HEXADECIMAL,
revive_common::BASE_HEXADECIMAL,
)
.map(StackElement::Constant)?],
None,
@@ -784,7 +784,7 @@ impl Function {
let result = match (&operands[0], &operands[1]) {
(Element::Tag(tag), Element::Constant(offset)) => {
let offset = offset % era_compiler_common::BIT_LENGTH_FIELD;
let offset = offset % revive_common::BIT_LENGTH_FIELD;
let offset = offset.to_u64().expect("Always valid");
let result = tag << offset;
if Self::is_tag_value_valid(blocks, &result) {
@@ -794,7 +794,7 @@ impl Function {
}
}
(Element::Constant(constant), Element::Constant(offset)) => {
let offset = offset % era_compiler_common::BIT_LENGTH_FIELD;
let offset = offset % revive_common::BIT_LENGTH_FIELD;
let offset = offset.to_u64().expect("Always valid");
Element::Constant(constant << offset)
}
@@ -811,7 +811,7 @@ impl Function {
let result = match (&operands[0], &operands[1]) {
(Element::Tag(tag), Element::Constant(offset)) => {
let offset = offset % era_compiler_common::BIT_LENGTH_FIELD;
let offset = offset % revive_common::BIT_LENGTH_FIELD;
let offset = offset.to_u64().expect("Always valid");
let result = tag >> offset;
if Self::is_tag_value_valid(blocks, &result) {
@@ -821,7 +821,7 @@ impl Function {
}
}
(Element::Constant(constant), Element::Constant(offset)) => {
let offset = offset % era_compiler_common::BIT_LENGTH_FIELD;
let offset = offset % revive_common::BIT_LENGTH_FIELD;
let offset = offset.to_u64().expect("Always valid");
Element::Constant(constant >> offset)
}
@@ -1170,7 +1170,7 @@ where
let output_size = 0;
let r#type = context.function_type(
vec![context
.integer_type(era_compiler_common::BIT_LENGTH_BOOLEAN)
.integer_type(revive_common::BIT_LENGTH_BOOLEAN)
.as_basic_type_enum()],
output_size,
false,
@@ -1185,7 +1185,7 @@ where
let r#type = context.function_type(
vec![
context
.integer_type(era_compiler_common::BIT_LENGTH_FIELD)
.integer_type(revive_common::BIT_LENGTH_FIELD)
.as_basic_type_enum();
input_size
],
+2 -2
View File
@@ -165,7 +165,7 @@ pub fn standard_output(
input_files: &[PathBuf],
libraries: Vec<String>,
solc: &mut SolcCompiler,
evm_version: Option<era_compiler_common::EVMVersion>,
evm_version: Option<revive_common::EVMVersion>,
solc_optimizer_enabled: bool,
optimizer_settings: era_compiler_llvm_context::OptimizerSettings,
force_evmla: bool,
@@ -341,7 +341,7 @@ pub fn combined_json(
input_files: &[PathBuf],
libraries: Vec<String>,
solc: &mut SolcCompiler,
evm_version: Option<era_compiler_common::EVMVersion>,
evm_version: Option<revive_common::EVMVersion>,
solc_optimizer_enabled: bool,
optimizer_settings: era_compiler_llvm_context::OptimizerSettings,
force_evmla: bool,
+3 -3
View File
@@ -29,7 +29,7 @@ pub fn run() -> anyhow::Result<()> {
let mut buffer = Vec::with_capacity(16384);
stdin.read_to_end(&mut buffer).expect("Stdin reading error");
let input: Input = era_compiler_common::deserialize_from_slice(buffer.as_slice())?;
let input: Input = revive_common::deserialize_from_slice(buffer.as_slice())?;
if input.enable_test_encoding {
todo!()
}
@@ -96,8 +96,8 @@ pub fn call(input: Input) -> anyhow::Result<Output> {
);
}
let output: Output = era_compiler_common::deserialize_from_slice(output.stdout.as_slice())
.map_err(|error| {
let output: Output =
revive_common::deserialize_from_slice(output.stdout.as_slice()).map_err(|error| {
anyhow::anyhow!(
"{:?} subprocess output parsing error: {}",
executable,
+8 -8
View File
@@ -39,7 +39,7 @@ impl Contract {
///
pub fn new(
path: String,
source_hash: [u8; era_compiler_common::BYTE_LENGTH_FIELD],
source_hash: [u8; revive_common::BYTE_LENGTH_FIELD],
source_version: SolcVersion,
ir: IR,
metadata_json: Option<serde_json::Value>,
@@ -110,13 +110,13 @@ impl Contract {
optimizer.settings().to_owned(),
);
let metadata_json = serde_json::to_value(&metadata).expect("Always valid");
let metadata_hash: Option<[u8; era_compiler_common::BYTE_LENGTH_FIELD]> =
if include_metadata_hash {
let metadata_string = serde_json::to_string(&metadata).expect("Always valid");
Some(sha3::Keccak256::digest(metadata_string.as_bytes()).into())
} else {
None
};
let metadata_hash: Option<[u8; revive_common::BYTE_LENGTH_FIELD]> = if include_metadata_hash
{
let metadata_string = serde_json::to_string(&metadata).expect("Always valid");
Some(sha3::Keccak256::digest(metadata_string.as_bytes()).into())
} else {
None
};
let module = match self.ir {
IR::LLVMIR(ref llvm_ir) => {
@@ -67,7 +67,7 @@ impl Contract {
.find_map(|(contract_entry, hash)| {
if contract_entry.starts_with(entry) {
Some(
u32::from_str_radix(hash.as_str(), era_compiler_common::BASE_HEXADECIMAL)
u32::from_str_radix(hash.as_str(), revive_common::BASE_HEXADECIMAL)
.expect("Test hash is always valid"),
)
} else {
@@ -89,7 +89,7 @@ impl CombinedJson {
overwrite: bool,
) -> anyhow::Result<()> {
let mut file_path = output_directory.to_owned();
file_path.push(format!("combined.{}", era_compiler_common::EXTENSION_JSON));
file_path.push(format!("combined.{}", revive_common::EXTENSION_JSON));
if file_path.exists() && !overwrite {
eprintln!(
+28 -28
View File
@@ -122,21 +122,21 @@ impl Compiler {
);
}
let mut output: StandardJsonOutput = era_compiler_common::deserialize_from_slice(
output.stdout.as_slice(),
)
.map_err(|error| {
anyhow::anyhow!(
"{} subprocess output parsing error: {}\n{}",
self.executable,
error,
era_compiler_common::deserialize_from_slice::<serde_json::Value>(
output.stdout.as_slice()
let mut output: StandardJsonOutput =
revive_common::deserialize_from_slice(output.stdout.as_slice()).map_err(|error| {
anyhow::anyhow!(
"{} subprocess output parsing error: {}\n{}",
self.executable,
error,
revive_common::deserialize_from_slice::<serde_json::Value>(
output.stdout.as_slice()
)
.map(|json| serde_json::to_string_pretty(&json).expect("Always valid"))
.unwrap_or_else(
|_| String::from_utf8_lossy(output.stdout.as_slice()).to_string()
),
)
.map(|json| serde_json::to_string_pretty(&json).expect("Always valid"))
.unwrap_or_else(|_| String::from_utf8_lossy(output.stdout.as_slice()).to_string()),
)
})?;
})?;
output.preprocess_ast(&version, pipeline, suppressed_warnings.as_slice())?;
output.remove_evm();
@@ -183,21 +183,21 @@ impl Compiler {
);
}
let mut combined_json: CombinedJson = era_compiler_common::deserialize_from_slice(
output.stdout.as_slice(),
)
.map_err(|error| {
anyhow::anyhow!(
"{} subprocess output parsing error: {}\n{}",
self.executable,
error,
era_compiler_common::deserialize_from_slice::<serde_json::Value>(
output.stdout.as_slice()
let mut combined_json: CombinedJson =
revive_common::deserialize_from_slice(output.stdout.as_slice()).map_err(|error| {
anyhow::anyhow!(
"{} subprocess output parsing error: {}\n{}",
self.executable,
error,
revive_common::deserialize_from_slice::<serde_json::Value>(
output.stdout.as_slice()
)
.map(|json| serde_json::to_string_pretty(&json).expect("Always valid"))
.unwrap_or_else(
|_| String::from_utf8_lossy(output.stdout.as_slice()).to_string()
),
)
.map(|json| serde_json::to_string_pretty(&json).expect("Always valid"))
.unwrap_or_else(|_| String::from_utf8_lossy(output.stdout.as_slice()).to_string()),
)
})?;
})?;
for filtered_flag in filtered_flags.into_iter() {
for (_path, contract) in combined_json.contracts.iter_mut() {
match filtered_flag {
@@ -62,7 +62,7 @@ impl Input {
#[allow(clippy::too_many_arguments)]
pub fn try_from_paths(
language: Language,
evm_version: Option<era_compiler_common::EVMVersion>,
evm_version: Option<revive_common::EVMVersion>,
paths: &[PathBuf],
library_map: Vec<String>,
remappings: Option<BTreeSet<String>>,
@@ -107,7 +107,7 @@ impl Input {
///
#[allow(clippy::too_many_arguments)]
pub fn try_from_sources(
evm_version: Option<era_compiler_common::EVMVersion>,
evm_version: Option<revive_common::EVMVersion>,
sources: BTreeMap<String, String>,
libraries: BTreeMap<String, BTreeMap<String, String>>,
remappings: Option<BTreeSet<String>>,
@@ -24,7 +24,7 @@ use self::selection::Selection;
pub struct Settings {
/// The target EVM version.
#[serde(skip_serializing_if = "Option::is_none")]
pub evm_version: Option<era_compiler_common::EVMVersion>,
pub evm_version: Option<revive_common::EVMVersion>,
/// The linker library addresses.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub libraries: Option<BTreeMap<String, BTreeMap<String, String>>>,
@@ -53,7 +53,7 @@ impl Settings {
/// A shortcut constructor.
///
pub fn new(
evm_version: Option<era_compiler_common::EVMVersion>,
evm_version: Option<revive_common::EVMVersion>,
libraries: BTreeMap<String, BTreeMap<String, String>>,
remappings: Option<BTreeSet<String>>,
output_selection: Selection,
@@ -83,7 +83,7 @@ impl Integer {
/// Checks whether the character can continue a decimal number.
///
pub fn can_continue_decimal(character: char) -> bool {
character.is_digit(era_compiler_common::BASE_DECIMAL)
character.is_digit(revive_common::BASE_DECIMAL)
}
///
@@ -97,7 +97,7 @@ impl Integer {
/// Checks whether the character can continue a hexadecimal number.
///
pub fn can_continue_hexadecimal(character: char) -> bool {
character.is_digit(era_compiler_common::BASE_HEXADECIMAL)
character.is_digit(revive_common::BASE_HEXADECIMAL)
}
///
@@ -155,7 +155,7 @@ where
&[
context.field_const(0),
context
.integer_type(era_compiler_common::BIT_LENGTH_X32)
.integer_type(revive_common::BIT_LENGTH_X32)
.const_int(index as u64, false),
],
context.field_type().as_basic_type_enum(),
@@ -894,9 +894,7 @@ impl FunctionCall {
let offset = context.builder().build_int_add(
arguments[0].into_int_value(),
context.field_const(
(era_compiler_common::BYTE_LENGTH_X32
+ era_compiler_common::BYTE_LENGTH_FIELD)
as u64,
(revive_common::BYTE_LENGTH_X32 + revive_common::BYTE_LENGTH_FIELD) as u64,
),
"datacopy_contract_hash_offset",
)?;
@@ -125,13 +125,12 @@ impl Literal {
.as_basic_value_enum();
let constant = match inner {
IntegerLiteral::Decimal { ref inner } => num::BigUint::from_str_radix(
inner.as_str(),
era_compiler_common::BASE_DECIMAL,
),
IntegerLiteral::Decimal { ref inner } => {
num::BigUint::from_str_radix(inner.as_str(), revive_common::BASE_DECIMAL)
}
IntegerLiteral::Hexadecimal { ref inner } => num::BigUint::from_str_radix(
&inner["0x".len()..],
era_compiler_common::BASE_HEXADECIMAL,
revive_common::BASE_HEXADECIMAL,
),
}
.expect("Always valid");
@@ -148,7 +147,7 @@ impl Literal {
string.clone()
} else {
let mut hex_string =
String::with_capacity(era_compiler_common::BYTE_LENGTH_FIELD * 2);
String::with_capacity(revive_common::BYTE_LENGTH_FIELD * 2);
let mut index = 0;
loop {
if index >= string.len() {
@@ -165,7 +164,7 @@ impl Literal {
let codepoint_str = &string[index + 1..index + 5];
let codepoint = u32::from_str_radix(
codepoint_str,
era_compiler_common::BASE_HEXADECIMAL,
revive_common::BASE_HEXADECIMAL,
)
.map_err(|error| {
anyhow::anyhow!(
@@ -209,16 +208,16 @@ impl Literal {
hex_string
};
if hex_string.len() > era_compiler_common::BYTE_LENGTH_FIELD * 2 {
if hex_string.len() > revive_common::BYTE_LENGTH_FIELD * 2 {
return Ok(era_compiler_llvm_context::EraVMArgument::new_with_original(
r#type.const_zero().as_basic_value_enum(),
string,
));
}
if hex_string.len() < era_compiler_common::BYTE_LENGTH_FIELD * 2 {
if hex_string.len() < revive_common::BYTE_LENGTH_FIELD * 2 {
hex_string.push_str(
"0".repeat((era_compiler_common::BYTE_LENGTH_FIELD * 2) - hex_string.len())
"0".repeat((revive_common::BYTE_LENGTH_FIELD * 2) - hex_string.len())
.as_str(),
);
}
@@ -297,7 +297,7 @@ where
&[
context.field_const(0),
context
.integer_type(era_compiler_common::BIT_LENGTH_X32)
.integer_type(revive_common::BIT_LENGTH_X32)
.const_int(index as u64, false),
],
context.field_type(),
@@ -195,7 +195,7 @@ where
&[
context.field_const(0),
context
.integer_type(era_compiler_common::BIT_LENGTH_X32)
.integer_type(revive_common::BIT_LENGTH_X32)
.const_int(index as u64, false),
],
binding.r#type.unwrap_or_default().into_llvm(context),
+2 -2
View File
@@ -31,7 +31,7 @@ pub enum Type {
impl Default for Type {
fn default() -> Self {
Self::UInt(era_compiler_common::BIT_LENGTH_FIELD)
Self::UInt(revive_common::BIT_LENGTH_FIELD)
}
}
@@ -79,7 +79,7 @@ impl Type {
D: era_compiler_llvm_context::EraVMDependency + Clone,
{
match self {
Self::Bool => context.integer_type(era_compiler_common::BIT_LENGTH_BOOLEAN),
Self::Bool => context.integer_type(revive_common::BIT_LENGTH_BOOLEAN),
Self::Int(bitlength) => context.integer_type(bitlength),
Self::UInt(bitlength) => context.integer_type(bitlength),
Self::Custom(_) => context.field_type(),
+3 -5
View File
@@ -20,10 +20,10 @@ static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
///
fn main() {
std::process::exit(match main_inner() {
Ok(()) => era_compiler_common::EXIT_CODE_SUCCESS,
Ok(()) => revive_common::EXIT_CODE_SUCCESS,
Err(error) => {
eprintln!("{error}");
era_compiler_common::EXIT_CODE_FAILURE
revive_common::EXIT_CODE_FAILURE
}
})
}
@@ -82,9 +82,7 @@ fn main_inner() -> anyhow::Result<()> {
)?;
let evm_version = match arguments.evm_version {
Some(evm_version) => Some(era_compiler_common::EVMVersion::try_from(
evm_version.as_str(),
)?),
Some(evm_version) => Some(revive_common::EVMVersion::try_from(evm_version.as_str())?),
None => None,
};