Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce a burn address for cdk_erigon #463

Merged
merged 18 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 evm_arithmetization/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ parallel = [
"starky/parallel",
]
polygon_pos = []
cdk_erigon = []

[[bin]]
name = "assemble"
Expand Down
1 change: 1 addition & 0 deletions evm_arithmetization/benches/fibonacci_25m_gas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ fn prepare_setup() -> anyhow::Result<GenerationInputs> {

Ok(GenerationInputs {
signed_txns: vec![txn.to_vec()],
burn_addr: None,
withdrawals: vec![],
tries: tries_before,
trie_roots_after,
Expand Down
10 changes: 9 additions & 1 deletion evm_arithmetization/src/cpu/kernel/aggregator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,15 @@ pub static KERNEL_FILES: [&str; NUMBER_KERNEL_FILES] = [
pub static KERNEL: Lazy<Kernel> = Lazy::new(combined_kernel);

pub(crate) fn combined_kernel_from_files<const N: usize>(files: [&str; N]) -> Kernel {
let parsed_files = files.iter().map(|f| parse(f, HashSet::new())).collect_vec();
#[cfg(feature = "cdk_erigon")]
let active_features = HashSet::from(["cdk_erigon"]);
#[cfg(not(feature = "cdk_erigon"))]
let active_features = HashSet::new();
LindaGuiga marked this conversation as resolved.
Show resolved Hide resolved

let parsed_files = files
.iter()
.map(|f| parse(f, &active_features))
.collect_vec();
assemble(parsed_files, evm_constants(), true)
}

Expand Down
58 changes: 58 additions & 0 deletions evm_arithmetization/src/cpu/kernel/asm/core/process_txn.asm
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ global buy_gas:
// stack: deduct_eth_status, retdest
%jumpi(panic)
// stack: retdest
#[cfg(feature = cdk_erigon)]
{
%add_max_burnt_eth
// stack: retdest
}

global increment_sender_nonce:
%mload_txn_field(@TXN_FIELD_ORIGIN)
Expand Down Expand Up @@ -362,8 +367,38 @@ process_message_txn_fail:
// stack: origin, leftover_gas_cost, leftover_gas'
%add_eth
// stack: leftover_gas'

#[cfg(feature = cdk_erigon)]
{
%deduct_extra_burn_fees
}
%endmacro

#[cfg(feature = cdk_erigon)]
{
%macro deduct_extra_burn_fees
// stack: leftover_gas'
// Deduct the extra burn fees from the burn target.
%mload_global_metadata(@GLOBAL_METADATA_BURN_ADDR) DUP1
%eq_const(@U256_MAX) %jumpi(%%deduct_extra_burn_fees_pop)
// stack: burn_target, leftover_gas'
DUP2
%mload_global_metadata(@GLOBAL_METADATA_BLOCK_BASE_FEE)
MUL
// stack: refund_base_cost, burn_target, leftover_gas'
SWAP1
%deduct_eth
// stack: deduct_status, leftover_gas'
%jumpi(panic)
%jump(%%deduct_extra_burn_fees_end)
%%deduct_extra_burn_fees_pop:
// stack: burn_target, leftover_gas'
POP
%%deduct_extra_burn_fees_end:
// stack: leftover_gas'
%endmacro
}
LindaGuiga marked this conversation as resolved.
Show resolved Hide resolved

// Sets @TXN_FIELD_MAX_FEE_PER_GAS and @TXN_FIELD_MAX_PRIORITY_FEE_PER_GAS.
%macro compute_fees
// stack: (empty)
Expand Down Expand Up @@ -395,6 +430,29 @@ process_message_txn_fail:
// stack: gas_limit - intrinsic_gas
%endmacro

#[cfg(feature = cdk_erigon)]
{
%macro add_max_burnt_eth
LindaGuiga marked this conversation as resolved.
Show resolved Hide resolved
// stack: (empty)
%mload_global_metadata(@GLOBAL_METADATA_BURN_ADDR)
// If there is no burn target, we skip the transfer.
DUP1 %eq_const(@U256_MAX) %jumpi(%%add_max_burnt_eth_pop)
// stack: burn_target
%mload_global_metadata(@GLOBAL_METADATA_BLOCK_BASE_FEE)
%mload_txn_field(@TXN_FIELD_GAS_LIMIT)
MUL
// stack: max_burnt_cost, burn_target
SWAP1
%add_eth
%jump(%%add_max_burnt_eth_end)
%%add_max_burnt_eth_pop:
// stack: burn_target
POP
%%add_max_burnt_eth_end:
// stack: (empty)
%endmacro
}

create_contract_account_fault:
%revert_checkpoint
// stack: address, retdest
Expand Down
5 changes: 4 additions & 1 deletion evm_arithmetization/src/cpu/kernel/assembler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,10 @@ mod tests {
constants: HashMap<String, U256>,
optimize: bool,
) -> Kernel {
let parsed_files = files.iter().map(|f| parse(f, HashSet::new())).collect_vec();
let parsed_files = files
.iter()
.map(|f| parse(f, &HashSet::new()))
.collect_vec();
assemble(parsed_files, constants, optimize)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,13 @@ pub(crate) enum GlobalMetadata {
BlobVersionedHashesRlpLen,
// Number of blob versioned hashes contained in the current type-3 transaction.
BlobVersionedHashesLen,

/// Address where the base fee to be burnt is sent.
BurnAddr,
}

impl GlobalMetadata {
pub(crate) const COUNT: usize = 59;
pub(crate) const COUNT: usize = 60;

/// Unscales this virtual offset by their respective `Segment` value.
pub(crate) const fn unscale(&self) -> usize {
Expand Down Expand Up @@ -195,6 +198,7 @@ impl GlobalMetadata {
Self::BlobVersionedHashesRlpStart,
Self::BlobVersionedHashesRlpLen,
Self::BlobVersionedHashesLen,
Self::BurnAddr,
]
}

Expand Down Expand Up @@ -266,6 +270,7 @@ impl GlobalMetadata {
Self::BlobVersionedHashesRlpStart => "GLOBAL_METADATA_BLOB_VERSIONED_HASHES_RLP_START",
Self::BlobVersionedHashesRlpLen => "GLOBAL_METADATA_BLOB_VERSIONED_HASHES_RLP_LEN",
Self::BlobVersionedHashesLen => "GLOBAL_METADATA_BLOB_VERSIONED_HASHES_LEN",
Self::BurnAddr => "GLOBAL_METADATA_BURN_ADDR",
}
}
}
6 changes: 6 additions & 0 deletions evm_arithmetization/src/cpu/kernel/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,10 @@ impl<F: Field> Interpreter<F> {

// Set `GlobalMetadata` values.
let metadata = &inputs.block_metadata;
#[cfg(feature = "cdk_erigon")]
let burn_addr = inputs
.burn_addr
.map_or_else(U256::max_value, |addr| U256::from_big_endian(&addr.0));
let global_metadata_to_set = [
(
GlobalMetadata::BlockBeneficiary,
Expand Down Expand Up @@ -328,6 +332,8 @@ impl<F: Field> Interpreter<F> {
),
(GlobalMetadata::KernelHash, h2u(KERNEL.code_hash)),
(GlobalMetadata::KernelLen, KERNEL.code.len().into()),
#[cfg(feature = "cdk_erigon")]
(GlobalMetadata::BurnAddr, burn_addr),
];

self.set_global_metadata_multi_fields(&global_metadata_to_set);
Expand Down
2 changes: 1 addition & 1 deletion evm_arithmetization/src/cpu/kernel/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use crate::cpu::kernel::constants::evm_constants;
/// Assemble files, outputting bytes.
/// This is for debugging the kernel only.
pub fn assemble_to_bytes(files: &[String]) -> Vec<u8> {
let parsed_files: Vec<_> = files.iter().map(|f| parse(f, HashSet::new())).collect();
let parsed_files: Vec<_> = files.iter().map(|f| parse(f, &HashSet::new())).collect();
let kernel = assemble(parsed_files, evm_constants(), true);
kernel.code
}
20 changes: 10 additions & 10 deletions evm_arithmetization/src/cpu/kernel/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ use crate::cpu::kernel::ast::{File, Item, PushTarget, StackReplacement};
#[grammar = "cpu/kernel/evm_asm.pest"]
struct AsmParser;

pub(crate) fn parse(s: &str, active_features: HashSet<&str>) -> File {
pub(crate) fn parse(s: &str, active_features: &HashSet<&str>) -> File {
let file = AsmParser::parse(Rule::file, s)
.expect("Parsing failed")
.next()
.unwrap();
let body = file
.into_inner()
.map(|i| parse_item(i, &active_features))
.map(|i| parse_item(i, active_features))
.collect();
File { body }
}
Expand Down Expand Up @@ -292,7 +292,7 @@ mod tests {
// Test `feature_1`.
let active_features = HashSet::from(["feature_1"]);

let parsed_code = parse(code, active_features);
let parsed_code = parse(code, &active_features);
let final_code = assemble(vec![parsed_code], HashMap::new(), false);

let expected_code = r#"
Expand All @@ -318,15 +318,15 @@ mod tests {
DIV
"#;

let parsed_expected = parse(expected_code, HashSet::new());
let parsed_expected = parse(expected_code, &HashSet::new());
let final_expected = assemble(vec![parsed_expected], HashMap::new(), false);

assert_eq!(final_code.code, final_expected.code);

// Test `feature_2`.
let active_features = HashSet::from(["feature_2"]);

let parsed_code = parse(code, active_features);
let parsed_code = parse(code, &active_features);
let final_code = assemble(vec![parsed_code], HashMap::new(), false);

let expected_code = r#"
Expand All @@ -348,15 +348,15 @@ mod tests {
MOD
"#;

let parsed_expected = parse(expected_code, HashSet::new());
let parsed_expected = parse(expected_code, &HashSet::new());
let final_expected = assemble(vec![parsed_expected], HashMap::new(), false);

assert_eq!(final_code.code, final_expected.code);

// Test with both features enabled.
let active_features = HashSet::from(["feature_1", "feature_2"]);

let parsed_code = parse(code, active_features);
let parsed_code = parse(code, &active_features);
let final_code = assemble(vec![parsed_code], HashMap::new(), false);

let expected_code = r#"
Expand Down Expand Up @@ -387,15 +387,15 @@ mod tests {
MOD
"#;

let parsed_expected = parse(expected_code, HashSet::new());
let parsed_expected = parse(expected_code, &HashSet::new());
let final_expected = assemble(vec![parsed_expected], HashMap::new(), false);

assert_eq!(final_code.code, final_expected.code);

// Test with all features disabled.
let active_features = HashSet::new();

let parsed_code = parse(code, active_features);
let parsed_code = parse(code, &active_features);
let final_code = assemble(vec![parsed_code], HashMap::new(), false);

let expected_code = r#"
Expand All @@ -412,7 +412,7 @@ mod tests {
DIV
"#;

let parsed_expected = parse(expected_code, HashSet::new());
let parsed_expected = parse(expected_code, &HashSet::new());
let final_expected = assemble(vec![parsed_expected], HashMap::new(), false);

assert_eq!(final_code.code, final_expected.code);
Expand Down
2 changes: 2 additions & 0 deletions evm_arithmetization/src/cpu/kernel/tests/add11.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ fn test_add11_yml() {

let inputs = GenerationInputs {
signed_txns: vec![txn.to_vec()],
burn_addr: None,
withdrawals: vec![],
global_exit_roots: vec![],
tries: tries_before,
Expand Down Expand Up @@ -362,6 +363,7 @@ fn test_add11_yml_with_exception() {

let inputs = GenerationInputs {
signed_txns: vec![txn.to_vec()],
burn_addr: None,
withdrawals: vec![],
global_exit_roots: vec![],
tries: tries_before,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ fn test_init_exc_stop() {

let inputs = GenerationInputs {
signed_txns: vec![],
burn_addr: None,
withdrawals: vec![],
tries: TrieInputs {
state_trie: state_trie_before,
Expand Down
Loading
Loading