Skip to content

Commit

Permalink
integration: cache contract blob artifacts after compilation
Browse files Browse the repository at this point in the history
Signed-off-by: xermicus <[email protected]>
  • Loading branch information
xermicus committed Jun 4, 2024
1 parent 2d0a0e2 commit d9a304d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 6 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/integration/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ alloy-sol-types = { workspace = true }
hex = { workspace = true }
env_logger = { workspace = true }
log = { workspace = true }
once_cell = { workspace = true }

revive-solidity = { path = "../solidity" }
revive-differential = { path = "../differential" }
Expand Down
54 changes: 48 additions & 6 deletions crates/integration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,27 @@ use mock_runtime::{CallOutput, State};

use crate::mock_runtime::{Event, ReturnFlags};

use once_cell::sync::Lazy;
use std::{collections::HashMap, sync::Mutex};

pub mod cases;
pub mod mock_runtime;

#[cfg(test)]
mod tests;

pub(crate) static PVM_BLOB_CACHE: Lazy<Mutex<HashMap<CompiledBlobId, Vec<u8>>>> =
Lazy::new(Default::default);
pub(crate) static EVM_BLOB_CACHE: Lazy<Mutex<HashMap<CompiledBlobId, Vec<u8>>>> =
Lazy::new(Default::default);

#[derive(Hash, PartialEq, Eq)]
struct CompiledBlobId {
contract_name: String,
solc_optimizer_enabled: bool,
pipeline: revive_solidity::SolcPipeline,
}

/// Compile the blob of `contract_name` found in given `source_code`.
/// The `solc` optimizer will be enabled
pub fn compile_blob(contract_name: &str, source_code: &str) -> Vec<u8> {
Expand All @@ -24,22 +39,37 @@ pub fn compile_blob(contract_name: &str, source_code: &str) -> Vec<u8> {
/// Compile the EVM bin-runtime of `contract_name` found in given `source_code`.
/// The `solc` optimizer will be enabled
pub fn compile_evm_bin_runtime(contract_name: &str, source_code: &str) -> Vec<u8> {
let file_name = "contract.sol";
let pipeline = revive_solidity::SolcPipeline::Yul;
let solc_optimizer_enabled = true;
let id = CompiledBlobId {
contract_name: contract_name.to_owned(),
pipeline,
solc_optimizer_enabled,
};

if let Some(blob) = EVM_BLOB_CACHE.lock().unwrap().get(&id) {
return blob.clone();
}

let file_name = "contract.sol";
let contracts = revive_solidity::test_utils::build_solidity_with_options_evm(
[(file_name.into(), source_code.into())].into(),
Default::default(),
None,
revive_solidity::SolcPipeline::Yul,
true,
pipeline,
solc_optimizer_enabled,
)
.expect("source should compile");
let bin_runtime = &contracts
.get(contract_name)
.unwrap_or_else(|| panic!("contract '{}' didn't produce bin-runtime", contract_name))
.object;

hex::decode(bin_runtime).expect("bin-runtime shold be hex encoded")
let blob = hex::decode(bin_runtime).expect("bin-runtime shold be hex encoded");

EVM_BLOB_CACHE.lock().unwrap().insert(id, blob.clone());

blob
}

/// Compile the blob of `contract_name` found in given `source_code`.
Expand All @@ -49,8 +79,17 @@ pub fn compile_blob_with_options(
solc_optimizer_enabled: bool,
pipeline: revive_solidity::SolcPipeline,
) -> Vec<u8> {
let file_name = "contract.sol";
let id = CompiledBlobId {
contract_name: contract_name.to_owned(),
solc_optimizer_enabled,
pipeline,
};

if let Some(blob) = PVM_BLOB_CACHE.lock().unwrap().get(&id) {
return blob.clone();
}

let file_name = "contract.sol";
let contracts = revive_solidity::test_utils::build_solidity_with_options(
[(file_name.into(), source_code.into())].into(),
Default::default(),
Expand All @@ -72,8 +111,11 @@ pub fn compile_blob_with_options(
.expect("source should produce assembly text")
.object
.as_str();
let blob = hex::decode(bytecode).expect("hex encoding should always be valid");

PVM_BLOB_CACHE.lock().unwrap().insert(id, blob.clone());

hex::decode(bytecode).expect("hex encoding should always be valid")
blob
}

pub fn assert_success(contract: &Contract, differential: bool) -> (State, CallOutput) {
Expand Down

0 comments on commit d9a304d

Please sign in to comment.