Skip to content

Commit

Permalink
Introduce a burn address for cdk_erigon (#463)
Browse files Browse the repository at this point in the history
* Implement burn address for cdk-erigon

* Fix block connections and comments

* Apply some comments

* Revert macro to global change for pay_coinbase_and_refund_sender

* Apply comments

* Cleanup

* Fix Clippy

* Remove feature in trace_decoder, and add it to zero_bin

* Fix RPC entrypoint

* Apply comments

* Link issue to the TODOs

* Apply comment
  • Loading branch information
LindaGuiga authored Aug 30, 2024
1 parent e57fe5a commit 51ecfb4
Show file tree
Hide file tree
Showing 37 changed files with 866 additions and 506 deletions.
794 changes: 328 additions & 466 deletions Cargo.lock

Large diffs are not rendered by default.

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();
let mut active_features = HashSet::new();
if cfg!(feature = "cdk_erigon") {
active_features.insert("cdk_erigon");
}

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

Expand Down
59 changes: 59 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)]
{
// Deduct the extra burn fees from the burn target.
%macro deduct_extra_burn_fees
// stack: leftover_gas'
%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
}

// 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,30 @@ process_message_txn_fail:
// stack: gas_limit - intrinsic_gas
%endmacro

#[cfg(feature = cdk_erigon)]
{
// Refund extra burn fees to the burn target.
%macro add_max_burnt_eth
// 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 @@ -112,10 +112,13 @@ pub(crate) enum GlobalMetadata {

// 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 = 54;
pub(crate) const COUNT: usize = 55;

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

Expand Down Expand Up @@ -244,6 +248,7 @@ impl GlobalMetadata {
Self::InitialStorageLinkedListLen => "GLOBAL_METADATA_INITIAL_STORAGE_LINKED_LIST_LEN",
Self::TransientStorageLen => "GLOBAL_METADATA_TRANSIENT_STORAGE_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
1 change: 1 addition & 0 deletions evm_arithmetization/src/cpu/kernel/tests/init_exc_stop.rs
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

0 comments on commit 51ecfb4

Please sign in to comment.