Skip to content

Commit

Permalink
Remove logic for multiple txns at once (#1341)
Browse files Browse the repository at this point in the history
* Have prover take only a single txn at most

* Update comment

* Apply review
  • Loading branch information
Nashtare authored Nov 9, 2023
1 parent 5d5628b commit 954d1a7
Show file tree
Hide file tree
Showing 16 changed files with 36 additions and 472 deletions.
8 changes: 4 additions & 4 deletions evm/src/cpu/kernel/asm/core/process_txn.asm
Original file line number Diff line number Diff line change
Expand Up @@ -452,22 +452,22 @@ global invalid_txn:
POP
%mload_txn_field(@TXN_FIELD_GAS_LIMIT)
PUSH 0
%jump(txn_loop_after)
%jump(txn_after)

global invalid_txn_1:
%pop2
%mload_txn_field(@TXN_FIELD_GAS_LIMIT)
PUSH 0
%jump(txn_loop_after)
%jump(txn_after)

global invalid_txn_2:
%pop3
%mload_txn_field(@TXN_FIELD_GAS_LIMIT)
PUSH 0
%jump(txn_loop_after)
%jump(txn_after)

global invalid_txn_3:
%pop4
%mload_txn_field(@TXN_FIELD_GAS_LIMIT)
PUSH 0
%jump(txn_loop_after)
%jump(txn_after)
14 changes: 6 additions & 8 deletions evm/src/cpu/kernel/asm/main.asm
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ global hash_initial_tries:
%mpt_hash_txn_trie %mload_global_metadata(@GLOBAL_METADATA_TXN_TRIE_DIGEST_BEFORE) %assert_eq
%mpt_hash_receipt_trie %mload_global_metadata(@GLOBAL_METADATA_RECEIPT_TRIE_DIGEST_BEFORE) %assert_eq

global start_txns:
global start_txn:
// stack: (empty)
// The special case of an empty trie (i.e. for the first transaction)
// is handled outside of the kernel.
Expand All @@ -29,24 +29,22 @@ global start_txns:
SWAP2
// stack: init_gas_used, txn_counter, num_nibbles, txn_nb

txn_loop:
// If the prover has no more txns for us to process, halt.
PROVER_INPUT(end_of_txns)
// If the prover has no txn for us to process, halt.
PROVER_INPUT(no_txn)
%jumpi(execute_withdrawals)

// Call route_txn. When we return, continue the txn loop.
PUSH txn_loop_after
// Call route_txn. When we return, we will process the txn receipt.
PUSH txn_after
// stack: retdest, prev_gas_used, txn_counter, num_nibbles, txn_nb
DUP4 DUP4 %increment_bounded_rlp
%stack (next_txn_counter, next_num_nibbles, retdest, prev_gas_used, txn_counter, num_nibbles) -> (txn_counter, num_nibbles, retdest, prev_gas_used, txn_counter, num_nibbles, next_txn_counter, next_num_nibbles)
%jump(route_txn)

global txn_loop_after:
global txn_after:
// stack: success, leftover_gas, cur_cum_gas, prev_txn_counter, prev_num_nibbles, txn_counter, num_nibbles, txn_nb
%process_receipt
// stack: new_cum_gas, txn_counter, num_nibbles, txn_nb
SWAP3 %increment SWAP3
%jump(txn_loop)

global execute_withdrawals:
// stack: cum_gas, txn_counter, num_nibbles, txn_nb
Expand Down
5 changes: 3 additions & 2 deletions evm/src/generation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ pub struct GenerationInputs {
pub gas_used_after: U256,
pub block_bloom_after: [U256; 8],

pub signed_txns: Vec<Vec<u8>>,
// A None would yield an empty proof, otherwise this contains the encoding of a transaction.
pub signed_txn: Option<Vec<u8>>,
// Withdrawal pairs `(addr, amount)`. At the end of the txs, `amount` is added to `addr`'s balance. See EIP-4895.
pub withdrawals: Vec<(Address, U256)>,
pub tries: TrieInputs,
Expand Down Expand Up @@ -122,7 +123,7 @@ fn apply_metadata_and_tries_memops<F: RichField + Extendable<D>, const D: usize>
(GlobalMetadata::TxnNumberBefore, inputs.txn_number_before),
(
GlobalMetadata::TxnNumberAfter,
inputs.txn_number_before + inputs.signed_txns.len(),
inputs.txn_number_before + if inputs.signed_txn.is_some() { 1 } else { 0 },
),
(
GlobalMetadata::StateTrieRootDigestBefore,
Expand Down
12 changes: 3 additions & 9 deletions evm/src/generation/prover_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl From<Vec<String>> for ProverInputFn {
impl<F: Field> GenerationState<F> {
pub(crate) fn prover_input(&mut self, input_fn: &ProverInputFn) -> Result<U256, ProgramError> {
match input_fn.0[0].as_str() {
"end_of_txns" => self.run_end_of_txns(),
"no_txn" => self.no_txn(),
"ff" => self.run_ff(input_fn),
"sf" => self.run_sf(input_fn),
"ffe" => self.run_ffe(input_fn),
Expand All @@ -49,14 +49,8 @@ impl<F: Field> GenerationState<F> {
}
}

fn run_end_of_txns(&mut self) -> Result<U256, ProgramError> {
let end = self.next_txn_index == self.inputs.signed_txns.len();
if end {
Ok(U256::one())
} else {
self.next_txn_index += 1;
Ok(U256::zero())
}
fn no_txn(&mut self) -> Result<U256, ProgramError> {
Ok(U256::from(self.inputs.signed_txn.is_none() as u8))
}

/// Finite field operations.
Expand Down
14 changes: 6 additions & 8 deletions evm/src/generation/rlp.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
use ethereum_types::U256;

pub(crate) fn all_rlp_prover_inputs_reversed(signed_txns: &[Vec<u8>]) -> Vec<U256> {
let mut inputs = all_rlp_prover_inputs(signed_txns);
pub(crate) fn all_rlp_prover_inputs_reversed(signed_txn: &[u8]) -> Vec<U256> {
let mut inputs = all_rlp_prover_inputs(signed_txn);
inputs.reverse();
inputs
}

fn all_rlp_prover_inputs(signed_txns: &[Vec<u8>]) -> Vec<U256> {
fn all_rlp_prover_inputs(signed_txn: &[u8]) -> Vec<U256> {
let mut prover_inputs = vec![];
for txn in signed_txns {
prover_inputs.push(txn.len().into());
for &byte in txn {
prover_inputs.push(byte.into());
}
prover_inputs.push(signed_txn.len().into());
for &byte in signed_txn {
prover_inputs.push(byte.into());
}
prover_inputs
}
8 changes: 3 additions & 5 deletions evm/src/generation/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ pub(crate) struct GenerationState<F: Field> {
pub(crate) memory: MemoryState,
pub(crate) traces: Traces<F>,

pub(crate) next_txn_index: usize,

/// Prover inputs containing MPT data, in reverse order so that the next input can be obtained
/// via `pop()`.
pub(crate) mpt_prover_inputs: Vec<U256>,
Expand All @@ -54,7 +52,7 @@ pub(crate) struct GenerationState<F: Field> {

impl<F: Field> GenerationState<F> {
pub(crate) fn new(inputs: GenerationInputs, kernel_code: &[u8]) -> Result<Self, ProgramError> {
log::debug!("Input signed_txns: {:?}", &inputs.signed_txns);
log::debug!("Input signed_txn: {:?}", &inputs.signed_txn);
log::debug!("Input state_trie: {:?}", &inputs.tries.state_trie);
log::debug!(
"Input transactions_trie: {:?}",
Expand All @@ -64,7 +62,8 @@ impl<F: Field> GenerationState<F> {
log::debug!("Input storage_tries: {:?}", &inputs.tries.storage_tries);
log::debug!("Input contract_code: {:?}", &inputs.contract_code);
let mpt_prover_inputs = all_mpt_prover_inputs_reversed(&inputs.tries)?;
let rlp_prover_inputs = all_rlp_prover_inputs_reversed(&inputs.signed_txns);
let rlp_prover_inputs =
all_rlp_prover_inputs_reversed(inputs.signed_txn.as_ref().unwrap_or(&vec![]));
let withdrawal_prover_inputs = all_withdrawals_prover_inputs_reversed(&inputs.withdrawals);
let bignum_modmul_result_limbs = Vec::new();

Expand All @@ -73,7 +72,6 @@ impl<F: Field> GenerationState<F> {
registers: Default::default(),
memory: MemoryState::new(kernel_code),
traces: Traces::default(),
next_txn_index: 0,
mpt_prover_inputs,
rlp_prover_inputs,
withdrawal_prover_inputs,
Expand Down
2 changes: 1 addition & 1 deletion evm/tests/add11_yml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ fn add11_yml() -> anyhow::Result<()> {
receipts_root: receipts_trie.hash(),
};
let inputs = GenerationInputs {
signed_txns: vec![txn.to_vec()],
signed_txn: Some(txn.to_vec()),
withdrawals: vec![],
tries: tries_before,
trie_roots_after,
Expand Down
2 changes: 1 addition & 1 deletion evm/tests/basic_smart_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ fn test_basic_smart_contract() -> anyhow::Result<()> {
receipts_root: receipts_trie.hash(),
};
let inputs = GenerationInputs {
signed_txns: vec![txn.to_vec()],
signed_txn: Some(txn.to_vec()),
withdrawals: vec![],
tries: tries_before,
trie_roots_after,
Expand Down
2 changes: 1 addition & 1 deletion evm/tests/empty_txn_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ fn test_empty_txn_list() -> anyhow::Result<()> {
receipts_root: receipts_trie.hash(),
};
let inputs = GenerationInputs {
signed_txns: vec![],
signed_txn: None,
withdrawals: vec![],
tries: TrieInputs {
state_trie,
Expand Down
2 changes: 1 addition & 1 deletion evm/tests/erc20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ fn test_erc20() -> anyhow::Result<()> {
receipts_root: receipts_trie.hash(),
};
let inputs = GenerationInputs {
signed_txns: vec![txn.to_vec()],
signed_txn: Some(txn.to_vec()),
withdrawals: vec![],
tries: tries_before,
trie_roots_after,
Expand Down
Loading

0 comments on commit 954d1a7

Please sign in to comment.