Skip to content

Commit

Permalink
Merge branch 'main' into assertoor-allaround-tx-check
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigo-o authored Nov 28, 2024
2 parents 574135b + 3162649 commit 5a7f713
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 33 deletions.
14 changes: 9 additions & 5 deletions .github/workflows/ci_skipped.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ name: CI Skipped
on:
pull_request:
branches: ["**"]
paths:
- 'README.md'
- 'LICENSE'
- "**/README.md"
- "**/docs/**"
paths-ignore:
- '**/*.rs'
- '**/*.toml'
- '**/*.yaml'
- '**/*.sh'
- '**/*.json'
- '**/*.rlp'
- 'Dockefile*'

jobs:
lint:
# "Lint" is a required check, don't change the name
Expand Down
22 changes: 21 additions & 1 deletion .github/workflows/loc.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Weekly LoC
name: Daily Lines of Code Report

on:
schedule:
Expand All @@ -25,10 +25,30 @@ jobs:
- name: Add Rust Cache
uses: Swatinem/rust-cache@v2

- name: Restore cache
id: cache-loc-report
uses: actions/cache@v3
with:
path: loc_report.json
key: loc-report-${{ github.ref_name }}
restore-keys: |
loc-report-
- name: Rename cached loc_report.json to loc_report.json.old
if: steps.cache-loc-report.outputs.cache-hit == 'true'
run: mv loc_report.json loc_report.json.old

- name: Generate the loc report
run: |
make loc
- name: Save new loc_report.json to cache
if: success()
uses: actions/cache@v3
with:
path: loc_report.json
key: loc-report-${{ github.ref_name }}

- name: Post results in summary
run: |
echo "# `ethrex` lines of code report" >> $GITHUB_STEP_SUMMARY
Expand Down
3 changes: 2 additions & 1 deletion cmd/loc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ edition.workspace = true

[dependencies]
tokei = "12.1.2"
colored = "2.1.0"
serde = "1.0.215"
serde_json = "1.0.133"
110 changes: 93 additions & 17 deletions cmd/loc/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
use tokei::{Config, LanguageType, Languages};

const CARGO_MANIFEST_DIR: &str = std::env!("CARGO_MANIFEST_DIR");

#[derive(Default, Serialize, Deserialize, Clone, Copy)]
pub struct LinesOfCodeReport {
ethrex: usize,
ethrex_l1: usize,
ethrex_l2: usize,
levm: usize,
}

fn main() {
let ethrex = PathBuf::from(CARGO_MANIFEST_DIR).join("../../");
let levm = PathBuf::from(CARGO_MANIFEST_DIR).join("../../crates/vm");
Expand All @@ -22,41 +31,108 @@ fn main() {
languages.get_statistics(&[ethrex_l2], &["tests"], &config);
let ethrex_l2_loc = &languages.get(&LanguageType::Rust).unwrap();

let new_report = LinesOfCodeReport {
ethrex: ethrex_loc.code,
ethrex_l1: ethrex_loc.code - ethrex_l2_loc.code - levm_loc.code,
ethrex_l2: ethrex_l2_loc.code,
levm: levm_loc.code,
};

std::fs::write(
"loc_report.json",
serde_json::to_string(&new_report).unwrap(),
)
.expect("loc_report.json could not be written");

let old_report: LinesOfCodeReport = std::fs::read_to_string("loc_report.json.old")
.map(|s| serde_json::from_str(&s).unwrap())
.unwrap_or_default();

std::fs::write(
"loc_report_slack.txt",
slack_message(ethrex_loc.code, ethrex_l2_loc.code, levm_loc.code),
slack_message(old_report, new_report),
)
.unwrap();
std::fs::write(
"loc_report_github.txt",
github_step_summary(ethrex_loc.code, ethrex_l2_loc.code, levm_loc.code),
github_step_summary(old_report, new_report),
)
.unwrap();
}

fn slack_message(ethrex_loc: usize, ethrex_l2_loc: usize, levm_loc: usize) -> String {
fn slack_message(old_report: LinesOfCodeReport, new_report: LinesOfCodeReport) -> String {
let ethrex_l1_diff = new_report.ethrex_l1.abs_diff(old_report.ethrex_l1);
let ethrex_l2_diff = new_report.ethrex_l2.abs_diff(old_report.ethrex_l2);
let levm_diff = new_report.levm.abs_diff(old_report.levm);
let ethrex_diff_total = ethrex_l1_diff + ethrex_l2_diff + levm_diff;

format!(
r#"*ethrex L1:* {}\n*ethrex L2:* {}\n*levm:* {}\n*ethrex (total):* {}"#,
ethrex_loc - ethrex_l2_loc - levm_loc,
ethrex_l2_loc,
levm_loc,
ethrex_loc,
r#"*ethrex L1:* {} {}\n*ethrex L2:* {} {}\n*levm:* {} {}\n*ethrex (total):* {} {}"#,
new_report.ethrex_l1,
if new_report.ethrex > old_report.ethrex {
format!("(+{ethrex_l1_diff})")
} else {
format!("(-{ethrex_l1_diff})")
},
new_report.ethrex_l2,
if new_report.ethrex_l2 > old_report.ethrex_l2 {
format!("(+{ethrex_l2_diff})")
} else {
format!("(-{ethrex_l2_diff})")
},
new_report.levm,
if new_report.levm > old_report.levm {
format!("(+{levm_diff})")
} else {
format!("(-{levm_diff})")
},
new_report.ethrex,
if new_report.ethrex > old_report.ethrex {
format!("(+{ethrex_diff_total})")
} else {
format!("(-{ethrex_diff_total})")
},
)
}

fn github_step_summary(ethrex_loc: usize, ethrex_l2_loc: usize, levm_loc: usize) -> String {
fn github_step_summary(old_report: LinesOfCodeReport, new_report: LinesOfCodeReport) -> String {
let ethrex_l1_diff = new_report.ethrex_l1.abs_diff(old_report.ethrex_l1);
let ethrex_l2_diff = new_report.ethrex_l2.abs_diff(old_report.ethrex_l2);
let levm_diff = new_report.levm.abs_diff(old_report.levm);
let ethrex_diff_total = ethrex_l1_diff + ethrex_l2_diff + levm_diff;

format!(
r#"```
ethrex loc summary
====================
ethrex L1: {}
ethrex L2: {}
levm: {}
ethrex (total): {}
ethrex L1: {} {}
ethrex L2: {} {}
levm: {} ({})
ethrex (total): {} {}
```"#,
ethrex_loc - ethrex_l2_loc - levm_loc,
ethrex_l2_loc,
levm_loc,
ethrex_loc,
new_report.ethrex_l1,
if new_report.ethrex > old_report.ethrex {
format!("(+{ethrex_l1_diff})")
} else {
format!("(-{ethrex_l1_diff})")
},
new_report.ethrex_l2,
if new_report.ethrex_l2 > old_report.ethrex_l2 {
format!("(+{ethrex_l2_diff})")
} else {
format!("(-{ethrex_l2_diff})")
},
new_report.levm,
if new_report.levm > old_report.levm {
format!("(+{levm_diff})")
} else {
format!("(-{levm_diff})")
},
new_report.ethrex,
if new_report.ethrex > old_report.ethrex {
format!("(+{ethrex_diff_total})")
} else {
format!("(-{ethrex_diff_total})")
},
)
}
2 changes: 2 additions & 0 deletions crates/l2/proposer/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ pub enum CommitterError {
FailedToParseLastCommittedBlock(#[from] FromStrRadixErr),
#[error("Committer failed retrieve block from storage: {0}")]
FailedToRetrieveBlockFromStorage(#[from] StoreError),
#[error("Committer failed retrieve data from storage")]
FailedToRetrieveDataFromStorage,
#[error("Committer failed to generate blobs bundle: {0}")]
FailedToGenerateBlobsBundle(#[from] BlobsBundleError),
#[error("Committer failed to get information from storage")]
Expand Down
30 changes: 24 additions & 6 deletions crates/l2/proposer/l1_committer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use ethrex_core::{
},
Address, H256, U256,
};
use ethrex_storage::Store;
use ethrex_storage::{error::StoreError, Store};
use ethrex_vm::{evm_state, execute_block, get_state_transitions};
use keccak_hash::keccak;
use secp256k1::SecretKey;
Expand Down Expand Up @@ -132,7 +132,7 @@ impl Committer {
deposits,
)?;

let blobs_bundle = self.generate_blobs_bundle(state_diff.clone())?;
let blobs_bundle = self.generate_blobs_bundle(&state_diff)?;

let head_block_hash = block_to_commit.hash();
match self
Expand Down Expand Up @@ -242,18 +242,36 @@ impl Committer {
let account_updates = get_state_transitions(&mut state);

let mut modified_accounts = HashMap::new();
account_updates.iter().for_each(|account_update| {
for account_update in &account_updates {
let prev_nonce = match state
.database()
.ok_or(CommitterError::FailedToRetrieveDataFromStorage)?
// If we want the state_diff of a batch, we will have to change the -1 with the `batch_size`
// and we may have to keep track of the latestCommittedBlock (last block of the batch),
// the batch_size and the latestCommittedBatch in the contract.
.get_account_info(block.header.number - 1, account_update.address)
.map_err(StoreError::from)?
{
Some(acc) => acc.nonce,
None => 0,
};

modified_accounts.insert(
account_update.address,
AccountStateDiff {
new_balance: account_update.info.clone().map(|info| info.balance),
nonce_diff: account_update.info.clone().map(|info| info.nonce as u16),
nonce_diff: (account_update
.info
.clone()
.ok_or(CommitterError::FailedToRetrieveDataFromStorage)?
.nonce
- prev_nonce) as u16,
storage: account_update.added_storage.clone().into_iter().collect(),
bytecode: account_update.code.clone(),
bytecode_hash: None,
},
);
});
}

let state_diff = StateDiff {
modified_accounts,
Expand Down Expand Up @@ -287,7 +305,7 @@ impl Committer {
/// Generate the blob bundle necessary for the EIP-4844 transaction.
pub fn generate_blobs_bundle(
&self,
state_diff: StateDiff,
state_diff: &StateDiff,
) -> Result<BlobsBundle, CommitterError> {
let blob_data = state_diff.encode().map_err(CommitterError::from)?;

Expand Down
6 changes: 3 additions & 3 deletions crates/l2/proposer/state_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use super::errors::StateDiffError;
#[derive(Clone)]
pub struct AccountStateDiff {
pub new_balance: Option<U256>,
pub nonce_diff: Option<u16>,
pub nonce_diff: u16,
pub storage: Vec<(H256, U256)>,
pub bytecode: Option<Bytes>,
pub bytecode_hash: Option<H256>,
Expand Down Expand Up @@ -125,9 +125,9 @@ impl AccountStateDiff {
encoded.extend_from_slice(buf);
}

if let Some(nonce_diff) = self.nonce_diff {
if self.nonce_diff != 0 {
r#type += AccountStateDiffType::NonceDiff as u8;
encoded.extend(nonce_diff.to_be_bytes());
encoded.extend(self.nonce_diff.to_be_bytes());
}

if !self.storage.is_empty() {
Expand Down

0 comments on commit 5a7f713

Please sign in to comment.