From 88c10fb5dbad155e305ece7693bfd1713f99f0d9 Mon Sep 17 00:00:00 2001 From: Pierre-Louis Date: Tue, 5 Sep 2023 11:23:31 +0200 Subject: [PATCH] evm/kernel: add simulation tag to differention msg --- src/bin_evm_proxy/lib_dev/simulation.ml | 17 ++++++++++++-- src/kernel_evm/kernel/src/simulation.rs | 31 +++++++++++++++++-------- tezt/tests/evm_rollup.ml | 2 +- 3 files changed, 37 insertions(+), 13 deletions(-) diff --git a/src/bin_evm_proxy/lib_dev/simulation.ml b/src/bin_evm_proxy/lib_dev/simulation.ml index 38bff242fd70..4208c8308bea 100644 --- a/src/bin_evm_proxy/lib_dev/simulation.ml +++ b/src/bin_evm_proxy/lib_dev/simulation.ml @@ -88,9 +88,18 @@ let new_chunked_tag = "\002" (** Tag signaling a simulation message containing a chunk *) let chunk_tag = "\003" +(** Tag indicating simulation is an evaluation *) +let evaluation_tag = "\000" + +(** Tag indicating simulation is a validation *) +let validation_tag = "\001" + (** [hex_str_of_binary_string s] translate a binary string into an hax string *) let hex_str_of_binary_string s = s |> Hex.of_string |> Hex.show +(** [add_tag tag bytes] prefixes bytes by the given tag *) +let add_tag tag bytes = tag ^ Bytes.to_string bytes |> String.to_bytes + let encode_message = function | Start -> hex_str_of_binary_string @@ simulation_tag | Simple s -> hex_str_of_binary_string @@ simulation_tag ^ simple_tag ^ s @@ -103,12 +112,16 @@ let encode_message = function let encode call = let open Result_syntax in - let* messages = call |> rlp_encode |> split_in_messages in + let* messages = + call |> rlp_encode |> add_tag evaluation_tag |> split_in_messages + in return @@ List.map encode_message messages let encode_tx tx = let open Result_syntax in - let* messages = tx |> tx_rlp_encode |> split_in_messages in + let* messages = + tx |> tx_rlp_encode |> add_tag validation_tag |> split_in_messages + in return @@ List.map encode_message messages module Encodings = struct diff --git a/src/kernel_evm/kernel/src/simulation.rs b/src/kernel_evm/kernel/src/simulation.rs index 5b8d74f38810..fadfe3dcaedb 100644 --- a/src/kernel_evm/kernel/src/simulation.rs +++ b/src/kernel_evm/kernel/src/simulation.rs @@ -29,6 +29,10 @@ pub const SIMULATION_CHUNK_TAG: u8 = 3; /// Maximum gas used by the evaluation. Bounded to limit DOS on the rollup node /// Is used as default value if no gas is set. pub const MAX_EVALUATION_GAS: u64 = 1_000_000_000u64; +/// Tag indicating simulation is an evaluation. +pub const EVALUATION_TAG: u8 = 0x00; +/// Tag indicating simulation is a validation. +pub const VALIDATION_TAG: u8 = 0x01; /// Container for eth_call data, used in messages sent by the rollup node /// simulation. @@ -214,15 +218,14 @@ impl TryFrom<&[u8]> for Message { type Error = DecoderError; fn try_from(bytes: &[u8]) -> Result { - if let Ok(simulation) = Evaluation::try_from(bytes) { - return Ok(Message::Evaluation(simulation)); - } + let Some(&tag) = bytes.first() else {return Err(DecoderError::Custom("Empty simulation message"))}; + let Some(bytes) = bytes.get(1..) else {return Err(DecoderError::Custom("Empty simulation message"))}; - if let Ok(tx_validation) = TxValidation::try_from(bytes) { - return Ok(Message::TxValidation(tx_validation)); + match tag { + EVALUATION_TAG => Evaluation::try_from(bytes).map(Message::Evaluation), + VALIDATION_TAG => TxValidation::try_from(bytes).map(Message::TxValidation), + _ => Err(DecoderError::Custom("Unknown message to simulate")), } - - Err(DecoderError::Custom("Unknown message to simulate")) } } @@ -574,7 +577,11 @@ mod tests { let mut encoded = hex::decode("f84894242424242424242424242424242424242424242494353535353535353535353535353535353535353588672b00000000000088ce56000000000000883582000000000000821616").unwrap(); - let mut input = vec![parsing::SIMULATION_TAG, SIMULATION_SIMPLE_TAG]; + let mut input = vec![ + parsing::SIMULATION_TAG, + SIMULATION_SIMPLE_TAG, + EVALUATION_TAG, + ]; input.append(&mut encoded); let parsed = Input::parse(&input); @@ -605,7 +612,7 @@ mod tests { }; let encoded = hex::decode( - "ff01e68094907823e0a92f94355968feb2cbf0fbb594fe321488672b0000000000008080846d4ce63c", + "ff0100e68094907823e0a92f94355968feb2cbf0fbb594fe321488672b0000000000008080846d4ce63c", ) .unwrap(); @@ -700,7 +707,11 @@ mod tests { assert_eq!(tx, expected); let mut encoded = hex::decode(hex).unwrap(); - let mut input = vec![parsing::SIMULATION_TAG, SIMULATION_SIMPLE_TAG]; + let mut input = vec![ + parsing::SIMULATION_TAG, + SIMULATION_SIMPLE_TAG, + VALIDATION_TAG, + ]; input.append(&mut encoded); let parsed = Input::parse(&input); diff --git a/tezt/tests/evm_rollup.ml b/tezt/tests/evm_rollup.ml index 42a2a13d0f54..d371a818d20c 100644 --- a/tezt/tests/evm_rollup.ml +++ b/tezt/tests/evm_rollup.ml @@ -1594,7 +1594,7 @@ let test_eth_call_storage_contract_proxy = Hex.to_string @@ `Hex "ff"; Hex.to_string @@ `Hex - "ff01e68094d77420f73b4612a7a99dba8c2afd30a1886b03448857040000000000008080844e70b1dc"; + "ff0100e68094d77420f73b4612a7a99dba8c2afd30a1886b03448857040000000000008080844e70b1dc"; ] in let expected_insights = -- GitLab