Skip to content

Commit

Permalink
Merge branch 'main' of github.com:lambdaclass/ethrex into fix_loc_rep…
Browse files Browse the repository at this point in the history
…orter
  • Loading branch information
ilitteri committed Nov 25, 2024
2 parents 4e64c9e + ae10d07 commit d12f31f
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 43 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci_levm.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ jobs:
- name: Download EF Tests
run: |
cd crates/vm/levm
make download-ef-tests
make download-evm-ef-tests
- name: Run tests
run: |
cd crates/vm/levm
make run-ef-tests
make run-evm-ef-tests
test:
name: Tests
runs-on: ubuntu-latest
Expand Down
23 changes: 18 additions & 5 deletions .github/workflows/loc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,28 @@ jobs:
uses: Swatinem/rust-cache@v2

- name: Generate the loc report
id: loc-report
run: |
make loc
echo "content=$(cat loc_report.md)" >> $GITHUB_OUTPUT
- name: Post results in summary
run: |
echo "# `ethrex` lines of code report" >> $GITHUB_STEP_SUMMARY
cat loc_report.md >> $GITHUB_STEP_SUMMARY
echo "# `ethrex` lines of code report:\n\n" >> $GITHUB_STEP_SUMMARY
$(cat loc_report.md) >> $GITHUB_STEP_SUMMARY
- name: Post results to slack
env:
url: ${{ secrets.SLACK_WEBHOOK_URL }}
run: sh publish_loc.sh
uses: slackapi/[email protected]
with:
webhook: ${{ secrets.SLACK_WEBHOOK_URL }}
webhook-type: incoming-webhook
payload: |
blocks:
- type: "header"
text:
type: "plain_text"
text: ethrex lines of code report
- type: "section"
text:
type: "mrkdwn"
text: ${{steps.loc-report.outputs.content}}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,5 @@ tests_v3.0.0.tar.gz
.env

levm_ef_tests_report.txt

loc_report.md
4 changes: 2 additions & 2 deletions cmd/ef_tests/levm/runner/revm_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
use ethrex_core::{types::TxKind, Address};
use ethrex_levm::errors::{TransactionReport, TxResult};
use ethrex_storage::{error::StoreError, AccountUpdate};
use ethrex_vm::{db::StoreWrapper, spec_id, EvmState, RevmAddress, RevmU256};
use ethrex_vm::{db::StoreWrapper, EvmState, RevmAddress, RevmU256, SpecId};
use revm::{
db::State,
inspectors::TracerEip3155 as RevmTracerEip3155,
Expand Down Expand Up @@ -135,7 +135,7 @@ pub fn prepare_revm_for_tx<'state>(
.with_block_env(block_env)
.with_tx_env(tx_env)
.modify_cfg_env(|cfg| cfg.chain_id = chain_spec.chain_id)
.with_spec_id(spec_id(&chain_spec, test.env.current_timestamp.as_u64()))
.with_spec_id(SpecId::CANCUN)
.with_external_context(
RevmTracerEip3155::new(Box::new(std::io::stderr())).without_summary(),
);
Expand Down
2 changes: 1 addition & 1 deletion cmd/ethrex_l2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@ ethrex-rlp.workspace = true
ethrex-rpc.workspace = true

[[bin]]
name = "l2"
name = "ethrex_l2"
path = "./src/main.rs"
25 changes: 15 additions & 10 deletions cmd/loc/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use colored::Colorize;
use std::path::PathBuf;
use tokei::{Config, LanguageType, Languages};

Expand All @@ -23,14 +22,20 @@ fn main() {
languages.get_statistics(&[ethrex_l2], &[], &config);
let ethrex_l2_loc = &languages.get(&LanguageType::Rust).unwrap();

println!("{}", "ethrex loc summary".bold());
println!("{}", "====================".bold());
println!(
"{}: {:?}",
"ethrex L1".bold(),
ethrex_loc.code - ethrex_l2_loc.code - levm_loc.code
let report = format!(
r#"```
ethrex loc summary
====================
ethrex L1: {}
ethrex L2: {}
levm: {}
ethrex (total): {}
```"#,
ethrex_loc.code - ethrex_l2_loc.code - levm_loc.code,
ethrex_l2_loc.code,
levm_loc.code,
ethrex_loc.code,
);
println!("{}: {:?}", "ethrex L2".bold(), ethrex_l2_loc.code);
println!("{}: {:?}", "levm".bold(), levm_loc.code);
println!("{}: {:?}", "ethrex (total)".bold(), ethrex_loc.code);

std::fs::write("loc_report.md", report).unwrap();
}
8 changes: 4 additions & 4 deletions crates/vm/levm/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ $(SPECTEST_VECTORS_DIR): $(SPECTEST_ARTIFACT)
tar -xzf $(SPECTEST_ARTIFACT) -C tmp
mv tmp/tests-14.1/GeneralStateTests $(SPECTEST_VECTORS_DIR)

download-ef-tests: $(SPECTEST_VECTORS_DIR) ## 📥 Download EF Tests
download-evm-ef-tests: $(SPECTEST_VECTORS_DIR) ## 📥 Download EF Tests

run-ef-tests: ## 🏃‍♂️ Run EF Tests
run-evm-ef-tests: ## 🏃‍♂️ Run EF Tests
cd ../../../ && \
cargo test -p ef_tests-levm --tests test
time cargo test -p ef_tests-levm --test ef_tests_levm

clean-ef-tests: ## 🗑️ Clean test vectors
clean-evm-ef-tests: ## 🗑️ Clean test vectors
rm -rf $(SPECTEST_VECTORS_DIR)

###### Benchmarks ######
Expand Down
38 changes: 23 additions & 15 deletions crates/vm/levm/src/opcode_handlers/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,15 +314,23 @@ impl VM {

self.increase_consumed_gas(current_call_frame, gas_cost)?;

if size == 0 {
return Ok(OpcodeSuccess::Continue);
}

if !is_cached {
self.cache_from_db(&address);
};

let bytecode = self.get_account(&address).info.bytecode;

let new_memory_size = dest_offset.checked_add(size).ok_or(VMError::Internal(
let new_memory_size = (((!size).checked_add(1).ok_or(VMError::Internal(
InternalError::ArithmeticOperationOverflow,
))?;
))?) & 31)
.checked_add(size)
.ok_or(VMError::Internal(
InternalError::ArithmeticOperationOverflow,
))?;
let current_memory_size = current_call_frame.memory.data.len();
if current_memory_size < new_memory_size {
current_call_frame.memory.data.resize(new_memory_size, 0);
Expand Down Expand Up @@ -393,19 +401,19 @@ impl VM {
}

let sub_return_data_len = current_call_frame.sub_return_data.len();
let data = if returndata_offset < sub_return_data_len {
current_call_frame.sub_return_data.slice(
returndata_offset
..(returndata_offset
.checked_add(size)
.ok_or(VMError::Internal(
InternalError::ArithmeticOperationOverflow,
))?)
.min(sub_return_data_len),
)
} else {
vec![0u8; size].into()
};

if returndata_offset >= sub_return_data_len {
return Err(VMError::VeryLargeNumber); // Maybe can create a new error instead of using this one
}
let data = current_call_frame.sub_return_data.slice(
returndata_offset
..(returndata_offset
.checked_add(size)
.ok_or(VMError::Internal(
InternalError::ArithmeticOperationOverflow,
))?)
.min(sub_return_data_len),
);

current_call_frame.memory.store_bytes(dest_offset, &data)?;

Expand Down
4 changes: 0 additions & 4 deletions crates/vm/levm/src/opcode_handlers/system.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::{
call_frame::CallFrame,
constants::SUCCESS_FOR_RETURN,
errors::{InternalError, OpcodeSuccess, ResultReason, VMError},
gas_cost,
vm::{word_to_address, VM},
Expand Down Expand Up @@ -184,9 +183,6 @@ impl VM {

let return_data = current_call_frame.memory.load_range(offset, size)?.into();
current_call_frame.returndata = return_data;
current_call_frame
.stack
.push(U256::from(SUCCESS_FOR_RETURN))?;

Ok(OpcodeSuccess::Result(ResultReason::Return))
}
Expand Down
29 changes: 29 additions & 0 deletions crates/vm/levm/tests/edge_case_tests.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use bytes::Bytes;
use ethrex_core::U256;
use ethrex_levm::{
errors::{TxResult, VMError},
operations::Operation,
utils::{new_vm_with_bytecode, new_vm_with_ops},
};
Expand Down Expand Up @@ -114,3 +115,31 @@ fn test_sdiv_zero_dividend_and_negative_divisor() {
vm.execute(&mut current_call_frame);
assert_eq!(current_call_frame.stack.pop().unwrap(), U256::zero());
}

#[test]
fn test_non_compliance_returndatacopy() {
let mut vm =
new_vm_with_bytecode(Bytes::copy_from_slice(&[56, 56, 56, 56, 56, 56, 62, 56])).unwrap();
let mut current_call_frame = vm.call_frames.pop().unwrap();
let txreport = vm.execute(&mut current_call_frame);
assert_eq!(txreport.result, TxResult::Revert(VMError::VeryLargeNumber));
}

#[test]
fn test_non_compliance_extcodecopy() {
let mut vm = new_vm_with_bytecode(Bytes::copy_from_slice(&[88, 88, 88, 89, 60, 89])).unwrap();
let mut current_call_frame = vm.call_frames.pop().unwrap();
vm.execute(&mut current_call_frame);
assert_eq!(current_call_frame.stack.stack.pop().unwrap(), U256::zero());
}

#[test]
fn test_non_compliance_extcodecopy_memory_resize() {
let mut vm = new_vm_with_bytecode(Bytes::copy_from_slice(&[
0x60, 12, 0x5f, 0x5f, 0x5f, 0x3c, 89,
]))
.unwrap();
let mut current_call_frame = vm.call_frames.pop().unwrap();
vm.execute(&mut current_call_frame);
assert_eq!(current_call_frame.stack.pop().unwrap(), U256::from(32));
}

0 comments on commit d12f31f

Please sign in to comment.