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
+39
View File
@@ -0,0 +1,39 @@
---
name: Bug report
about: Use this template for reporting issues
title: ''
labels: bug
assignees: ''
---
### 🐛 Bug Report
#### 📝 Description
Provide a clear and concise description of the bug.
#### 🔄 Reproduction Steps
Steps to reproduce the behaviour
#### 🤔 Expected Behavior
Describe what you expected to happen.
#### 😯 Current Behavior
Describe what actually happened.
#### 🖥️ Environment
Any relevant environment details.
#### 📋 Additional Context
Add any other context about the problem here. If applicable, add screenshots to help explain.
#### 📎 Log Output
```
Paste any relevant log output here.
```
+21
View File
@@ -0,0 +1,21 @@
---
name: Feature request
about: Use this template for requesting features
title: ''
labels: feat
assignees: ''
---
### 🌟 Feature Request
#### 📝 Description
Provide a clear and concise description of the feature you'd like to see.
#### 🤔 Rationale
Explain why this feature is important and how it benefits the project.
#### 📋 Additional Context
Add any other context or information about the feature request here.
+20
View File
@@ -0,0 +1,20 @@
# What ❔
<!-- What are the changes this PR brings about? -->
<!-- Example: This PR adds a PR template to the repo. -->
<!-- (For bigger PRs adding more context is appreciated) -->
## Why ❔
<!-- Why are these changes done? What goal do they contribute to? What are the principles behind them? -->
<!-- Example: PR templates ensure PR reviewers, observers, and future iterators are in context about the evolution of repos. -->
## Checklist
<!-- Check your PR fulfills the following items. -->
<!-- For draft PRs check the boxes as you complete them. -->
- [ ] PR title corresponds to the body of PR.
- [ ] Tests for the changes have been added / updated.
- [ ] Documentation comments have been added / updated.
- [ ] Code has been formatted via `cargo fmt` and checked with `cargo clippy`.
+9
View File
@@ -0,0 +1,9 @@
name: Cargo license check
on: pull_request
jobs:
cargo-deny:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: EmbarkStudios/cargo-deny-action@v1
+23
View File
@@ -0,0 +1,23 @@
name: "Rust CI"
on:
pull_request:
jobs:
build:
name: cargo build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions-rust-lang/setup-rust-toolchain@v1
- run: cargo build --verbose
formatting:
name: cargo fmt
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions-rust-lang/setup-rust-toolchain@v1
with:
components: rustfmt
- name: Rustfmt Check
uses: actions-rust-lang/rustfmt@v1
+17
View File
@@ -0,0 +1,17 @@
name: Leaked Secrets Scan
on: [pull_request]
jobs:
TruffleHog:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@ac593985615ec2ede58e132d2e21d2b1cbd6127c # v3
with:
fetch-depth: 0
- name: TruffleHog OSS
uses: trufflesecurity/trufflehog@0c66d30c1f4075cee1aada2e1ab46dabb1b0071a
with:
path: ./
base: ${{ github.event.repository.default_branch }}
head: HEAD
extra_args: --debug --only-verified
+14
View File
@@ -0,0 +1,14 @@
# Generated by Cargo
# will have compiled files and executables
/target/
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Cargo.lock
# These are backup files generated by rustfmt
**/*.rs.bk
# IDE
/.idea/
/.vscode/
+19
View File
@@ -0,0 +1,19 @@
[package]
name = "revive-common"
version = "0.1.0"
authors = [
"Cyrill Leutwiler <cyrill@parity.io>",
"Oleksandr Zarudnyi <a.zarudnyy@matterlabs.dev>",
]
license = "MIT OR Apache-2.0"
edition = "2021"
description = "Shared constants of the revive compiler"
[lib]
doctest = false
[dependencies]
anyhow = { workspace = true }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true, features = [ "arbitrary_precision", "unbounded_depth" ] }
serde_stacker = { workspace = true }
+35
View File
@@ -0,0 +1,35 @@
# zkSync Era: Compiler Common
[![Logo](eraLogo.svg)](https://zksync.io/)
zkSync Era is a layer 2 rollup that uses zero-knowledge proofs to scale Ethereum without compromising on security
or decentralization. As it's EVM-compatible (with Solidity/Vyper), 99% of Ethereum projects can redeploy without
needing to refactor or re-audit any code. zkSync Era also uses an LLVM-based compiler that will eventually enable
developers to write smart contracts in popular languages such as C++ and Rust.
This repository contains the common compiler constants.
## License
This library is distributed under the terms of either
- Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or <http://www.apache.org/licenses/LICENSE-2.0>)
- MIT license ([LICENSE-MIT](LICENSE-MIT) or <http://opensource.org/licenses/MIT>)
at your option.
## Official Links
- [Website](https://zksync.io/)
- [GitHub](https://github.com/matter-labs)
- [Twitter](https://twitter.com/zksync)
- [Twitter for Devs](https://twitter.com/zkSyncDevs)
- [Discord](https://join.zksync.dev/)
## Disclaimer
zkSync Era has been through extensive testing and audits, and although it is live, it is still in alpha state and
will undergo further audits and bug bounty programs. We would love to hear our community's thoughts and suggestions
about it!
It's important to note that forking it now could potentially lead to missing important
security updates, critical features, and performance improvements.
+15
View File
@@ -0,0 +1,15 @@
//!
//! The number base constants.
//!
/// The binary number base.
pub const BASE_BINARY: u32 = 2;
/// The octal number base.
pub const BASE_OCTAL: u32 = 8;
/// The decimal number base.
pub const BASE_DECIMAL: u32 = 10;
/// The hexadecimal number base.
pub const BASE_HEXADECIMAL: u32 = 16;
+22
View File
@@ -0,0 +1,22 @@
//!
//! The common sizes in bits.
//!
/// The `bool` type bit-length.
pub const BIT_LENGTH_BOOLEAN: usize = 1;
/// The `u8` type or byte bit-length.
pub const BIT_LENGTH_BYTE: usize = 8;
/// The x86 word type (usually `u32`) bit-length.
pub const BIT_LENGTH_X32: usize = crate::byte_length::BYTE_LENGTH_X32 * BIT_LENGTH_BYTE;
/// The x86_64 word type (usually `u64`) bit-length.
pub const BIT_LENGTH_X64: usize = crate::byte_length::BYTE_LENGTH_X64 * BIT_LENGTH_BYTE;
/// The ETH address (usually `u160`) bit-length.
pub const BIT_LENGTH_ETH_ADDRESS: usize =
crate::byte_length::BYTE_LENGTH_ETH_ADDRESS * BIT_LENGTH_BYTE;
/// The field (usually `u256` or `i256`) bit-length.
pub const BIT_LENGTH_FIELD: usize = crate::byte_length::BYTE_LENGTH_FIELD * BIT_LENGTH_BYTE;
+18
View File
@@ -0,0 +1,18 @@
//!
//! The common sizes in bytes.
//!
/// The byte-length.
pub const BYTE_LENGTH_BYTE: usize = 1;
/// The x86 word byte-length.
pub const BYTE_LENGTH_X32: usize = 4;
/// The x86_64 word byte-length.
pub const BYTE_LENGTH_X64: usize = 8;
/// The ETH address byte-length.
pub const BYTE_LENGTH_ETH_ADDRESS: usize = 20;
/// The field byte-length.
pub const BYTE_LENGTH_FIELD: usize = 32;
+120
View File
@@ -0,0 +1,120 @@
//!
//! The EraVM address constants.
//!
/// The corresponding simulation predefined address.
pub const ERAVM_ADDRESS_TO_L1: u16 = 0xFFFF;
/// The corresponding simulation predefined address.
pub const ERAVM_ADDRESS_CODE_ADDRESS: u16 = 0xFFFE;
/// The corresponding simulation predefined address.
pub const ERAVM_ADDRESS_PRECOMPILE: u16 = 0xFFFD;
/// The corresponding simulation predefined address.
pub const ERAVM_ADDRESS_META: u16 = 0xFFFC;
/// The corresponding simulation predefined address.
pub const ERAVM_ADDRESS_MIMIC_CALL: u16 = 0xFFFB;
/// The corresponding simulation predefined address.
pub const ERAVM_ADDRESS_SYSTEM_MIMIC_CALL: u16 = 0xFFFA;
/// The corresponding simulation predefined address.
pub const ERAVM_ADDRESS_MIMIC_CALL_BYREF: u16 = 0xFFF9;
/// The corresponding simulation predefined address.
pub const ERAVM_ADDRESS_SYSTEM_MIMIC_CALL_BYREF: u16 = 0xFFF8;
/// The corresponding simulation predefined address.
pub const ERAVM_ADDRESS_RAW_FAR_CALL: u16 = 0xFFF7;
/// The corresponding simulation predefined address.
pub const ERAVM_ADDRESS_RAW_FAR_CALL_BYREF: u16 = 0xFFF6;
/// The corresponding simulation predefined address.
pub const ERAVM_ADDRESS_SYSTEM_CALL: u16 = 0xFFF5;
/// The corresponding simulation predefined address.
pub const ERAVM_ADDRESS_SYSTEM_CALL_BYREF: u16 = 0xFFF4;
/// The corresponding simulation predefined address.
pub const ERAVM_ADDRESS_SET_CONTEXT_VALUE_CALL: u16 = 0xFFF3;
/// The corresponding simulation predefined address.
pub const ERAVM_ADDRESS_SET_PUBDATA_PRICE: u16 = 0xFFF2;
/// The corresponding simulation predefined address.
pub const ERAVM_ADDRESS_INCREMENT_TX_COUNTER: u16 = 0xFFF1;
/// The corresponding simulation predefined address.
pub const ERAVM_ADDRESS_GET_GLOBAL_PTR_CALLDATA: u16 = 0xFFF0;
/// The corresponding simulation predefined address.
pub const ERAVM_ADDRESS_GET_GLOBAL_CALL_FLAGS: u16 = 0xFFEF;
/// The corresponding simulation predefined address.
pub const ERAVM_ADDRESS_GET_GLOBAL_PTR_RETURN_DATA: u16 = 0xFFEE;
/// The corresponding simulation predefined address.
pub const ERAVM_ADDRESS_EVENT_INITIALIZE: u16 = 0xFFED;
/// The corresponding simulation predefined address.
pub const ERAVM_ADDRESS_EVENT_WRITE: u16 = 0xFFEC;
/// The corresponding simulation predefined address.
pub const ERAVM_ADDRESS_ACTIVE_PTR_LOAD_CALLDATA: u16 = 0xFFEB;
/// The corresponding simulation predefined address.
pub const ERAVM_ADDRESS_ACTIVE_PTR_LOAD_RETURN_DATA: u16 = 0xFFEA;
/// The corresponding simulation predefined address.
pub const ERAVM_ADDRESS_ACTIVE_PTR_ADD: u16 = 0xFFE9;
/// The corresponding simulation predefined address.
pub const ERAVM_ADDRESS_ACTIVE_PTR_SHRINK: u16 = 0xFFE8;
/// The corresponding simulation predefined address.
pub const ERAVM_ADDRESS_ACTIVE_PTR_PACK: u16 = 0xFFE7;
/// The corresponding simulation predefined address.
pub const ERAVM_ADDRESS_MULTIPLICATION_HIGH_REGISTER: u16 = 0xFFE6;
/// The corresponding simulation predefined address.
pub const ERAVM_ADDRESS_GET_GLOBAL_EXTRA_ABI_DATA: u16 = 0xFFE5;
/// The corresponding simulation predefined address.
pub const ERAVM_ADDRESS_ACTIVE_PTR_DATA_LOAD: u16 = 0xFFE4;
/// The corresponding simulation predefined address.
pub const ERAVM_ADDRESS_ACTIVE_PTR_DATA_COPY: u16 = 0xFFE3;
/// The corresponding simulation predefined address.
pub const ERAVM_ADDRESS_ACTIVE_PTR_DATA_SIZE: u16 = 0xFFE2;
/// The corresponding simulation predefined address.
pub const ERAVM_ADDRESS_CONST_ARRAY_DECLARE: u16 = 0xFFE1;
/// The corresponding simulation predefined address.
pub const ERAVM_ADDRESS_CONST_ARRAY_SET: u16 = 0xFFE0;
/// The corresponding simulation predefined address.
pub const ERAVM_ADDRESS_CONST_ARRAY_FINALIZE: u16 = 0xFFDF;
/// The corresponding simulation predefined address.
pub const ERAVM_ADDRESS_CONST_ARRAY_GET: u16 = 0xFFDE;
/// The corresponding simulation predefined address.
pub const ERAVM_ADDRESS_DECOMMIT: u16 = 0xFFDD;
/// The corresponding simulation predefined address.
pub const ERAVM_ADDRESS_ACTIVE_PTR_LOAD_DECOMMIT: u16 = 0xFFDC;
/// The corresponding simulation predefined address.
pub const ERAVM_ADDRESS_RETURN_FORWARD: u16 = 0xFFDB;
/// The corresponding simulation predefined address.
pub const ERAVM_ADDRESS_REVERT_FORWARD: u16 = 0xFFDA;
/// The corresponding simulation predefined address.
pub const ERAVM_ADDRESS_ACTIVE_PTR_SWAP: u16 = 0xFFD9;
+5
View File
@@ -0,0 +1,5 @@
//!
//! The EraVM constants.
//!
pub mod address;
+91
View File
@@ -0,0 +1,91 @@
//!
//! The EVM version.
//!
use serde::Deserialize;
use serde::Serialize;
///
/// The EVM version.
///
#[derive(Debug, Serialize, Deserialize, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[serde(rename_all = "camelCase")]
pub enum EVMVersion {
/// The corresponding EVM version.
#[serde(rename = "homestead")]
Homestead,
/// The corresponding EVM version.
#[serde(rename = "tangerineWhistle")]
TangerineWhistle,
/// The corresponding EVM version.
#[serde(rename = "spuriousDragon")]
SpuriousDragon,
/// The corresponding EVM version.
#[serde(rename = "byzantium")]
Byzantium,
/// The corresponding EVM version.
#[serde(rename = "constantinople")]
Constantinople,
/// The corresponding EVM version.
#[serde(rename = "petersburg")]
Petersburg,
/// The corresponding EVM version.
#[serde(rename = "istanbul")]
Istanbul,
/// The corresponding EVM version.
#[serde(rename = "berlin")]
Berlin,
/// The corresponding EVM version.
#[serde(rename = "london")]
London,
/// The corresponding EVM version.
#[serde(rename = "paris")]
Paris,
/// The corresponding EVM version.
#[serde(rename = "shanghai")]
Shanghai,
/// The corresponding EVM version.
#[serde(rename = "cancun")]
Cancun,
}
impl TryFrom<&str> for EVMVersion {
type Error = anyhow::Error;
fn try_from(value: &str) -> Result<Self, Self::Error> {
Ok(match value {
"homestead" => Self::Homestead,
"tangerineWhistle" => Self::TangerineWhistle,
"spuriousDragon" => Self::SpuriousDragon,
"byzantium" => Self::Byzantium,
"constantinople" => Self::Constantinople,
"petersburg" => Self::Petersburg,
"istanbul" => Self::Istanbul,
"berlin" => Self::Berlin,
"london" => Self::London,
"paris" => Self::Paris,
"shanghai" => Self::Shanghai,
"cancun" => Self::Cancun,
_ => anyhow::bail!("Invalid EVM version: {}", value),
})
}
}
impl std::fmt::Display for EVMVersion {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Homestead => write!(f, "homestead"),
Self::TangerineWhistle => write!(f, "tangerineWhistle"),
Self::SpuriousDragon => write!(f, "spuriousDragon"),
Self::Byzantium => write!(f, "byzantium"),
Self::Constantinople => write!(f, "constantinople"),
Self::Petersburg => write!(f, "petersburg"),
Self::Istanbul => write!(f, "istanbul"),
Self::Berlin => write!(f, "berlin"),
Self::London => write!(f, "london"),
Self::Paris => write!(f, "paris"),
Self::Shanghai => write!(f, "shanghai"),
Self::Cancun => write!(f, "cancun"),
}
}
}
+9
View File
@@ -0,0 +1,9 @@
//!
//! The exit code constants.
//!
/// The common application success exit code.
pub const EXIT_CODE_SUCCESS: i32 = 0;
/// The common application failure exit code.
pub const EXIT_CODE_FAILURE: i32 = 1;
+48
View File
@@ -0,0 +1,48 @@
//!
//! The file extensions.
//!
/// The manifest file extension.
pub static EXTENSION_MANIFEST: &str = "toml";
/// The JSON data file extension.
pub static EXTENSION_JSON: &str = "json";
/// The ABI file extension.
pub static EXTENSION_ABI: &str = "abi";
/// The Yul IR file extension.
pub static EXTENSION_YUL: &str = "yul";
/// The EVM legacy assembly IR file extension.
pub static EXTENSION_EVMLA: &str = "evmla";
/// The Ethereal IR file extension.
pub static EXTENSION_ETHIR: &str = "ethir";
/// The EVM file extension.
pub static EXTENSION_EVM: &str = "evm";
/// The EVM bytecode file extension.
pub static EXTENSION_EVM_BINARY: &str = "bin";
/// The Solidity file extension.
pub static EXTENSION_SOLIDITY: &str = "sol";
/// The LLL IR file extension.
pub static EXTENSION_LLL: &str = "lll";
/// The Vyper file extension.
pub static EXTENSION_VYPER: &str = "vy";
/// The LLVM source code file extension.
pub static EXTENSION_LLVM_SOURCE: &str = "ll";
/// The LLVM bitcode file extension.
pub static EXTENSION_LLVM_BINARY: &str = "bc";
/// The EraVM assembly file extension.
pub static EXTENSION_ERAVM_ASSEMBLY: &str = "zasm";
/// The EraVM bytecode file extension.
pub static EXTENSION_ERAVM_BINARY: &str = "zbin";
+21
View File
@@ -0,0 +1,21 @@
//!
//! The compiler common library.
//!
pub(crate) mod base;
pub(crate) mod bit_length;
pub(crate) mod byte_length;
pub(crate) mod eravm;
pub(crate) mod evm_version;
pub(crate) mod exit_code;
pub(crate) mod extension;
pub(crate) mod utils;
pub use self::base::*;
pub use self::bit_length::*;
pub use self::byte_length::*;
pub use self::eravm::address::*;
pub use self::evm_version::EVMVersion;
pub use self::exit_code::*;
pub use self::extension::*;
pub use self::utils::*;
+35
View File
@@ -0,0 +1,35 @@
//!
//! The compiler common utils.
//!
///
/// Deserializes a `serde_json` object from slice with the recursion limit disabled.
///
/// Must be used for all JSON I/O to avoid crashes due to the aforementioned limit.
///
pub fn deserialize_from_slice<O>(input: &[u8]) -> anyhow::Result<O>
where
O: serde::de::DeserializeOwned,
{
let mut deserializer = serde_json::Deserializer::from_slice(input);
deserializer.disable_recursion_limit();
let deserializer = serde_stacker::Deserializer::new(&mut deserializer);
let result = O::deserialize(deserializer)?;
Ok(result)
}
///
/// Deserializes a `serde_json` object from string with the recursion limit disabled.
///
/// Must be used for all JSON I/O to avoid crashes due to the aforementioned limit.
///
pub fn deserialize_from_str<O>(input: &str) -> anyhow::Result<O>
where
O: serde::de::DeserializeOwned,
{
let mut deserializer = serde_json::Deserializer::from_str(input);
deserializer.disable_recursion_limit();
let deserializer = serde_stacker::Deserializer::new(&mut deserializer);
let result = O::deserialize(deserializer)?;
Ok(result)
}