From 41342e87068c316a37cdd52a31626cab17527732 Mon Sep 17 00:00:00 2001 From: Michael Zaikin Date: Fri, 5 Jul 2024 16:28:09 +0100 Subject: [PATCH] Can etherlink call itself? --- etherlink/kernel_evm/kernel/src/apply.rs | 64 +++++++++++++++++++++- etherlink/kernel_evm/kernel/src/parsing.rs | 6 +- etherlink/tezt/lib/helpers.ml | 2 +- src/kernel_sdk/encoding/src/contract.rs | 14 ++++- 4 files changed, 80 insertions(+), 6 deletions(-) diff --git a/etherlink/kernel_evm/kernel/src/apply.rs b/etherlink/kernel_evm/kernel/src/apply.rs index ef21d758127b..5fc00961b599 100644 --- a/etherlink/kernel_evm/kernel/src/apply.rs +++ b/etherlink/kernel_evm/kernel/src/apply.rs @@ -17,14 +17,21 @@ use evm_execution::handler::{ExecutionOutcome, ExtendedExitReason, RouterInterfa use evm_execution::precompiles::PrecompileBTreeMap; use evm_execution::run_transaction; use evm_execution::trace::{get_tracer_config, TracerConfig, TracerInput}; +use num_traits::ToBytes; use primitive_types::{H160, U256}; +use tezos_crypto_rs::hash::SmartRollupHash; +use tezos_data_encoding::enc::BinWriter; +use tezos_data_encoding::encoding::HasEncoding; +use tezos_data_encoding::nom::NomReader; use tezos_ethereum::block::BlockConstants; use tezos_ethereum::transaction::{TransactionHash, TransactionType}; use tezos_ethereum::tx_common::EthereumTransactionCommon; use tezos_ethereum::tx_signature::TxSignature; use tezos_evm_logging::{log, Level::*}; use tezos_indexable_storage::IndexableStorage; -use tezos_smart_rollup::outbox::{OutboxMessage, OutboxQueue}; +use tezos_smart_rollup::michelson::{MichelsonBytes, MichelsonOr}; +use tezos_smart_rollup::outbox::{AtomicBatch, AtomicBatch2, OutboxMessage, OutboxMessageFull, OutboxMessageTransaction, OutboxQueue}; +use tezos_smart_rollup::types::{Contract, Entrypoint, SmartRollupAddress}; use tezos_smart_rollup_host::path::{Path, RefPath}; use tezos_smart_rollup_host::runtime::Runtime; @@ -32,6 +39,7 @@ use crate::configuration::Limits; use crate::error::Error; use crate::fees::{tx_execution_gas_limit, FeeUpdates}; use crate::inbox::{Deposit, Transaction, TransactionContent}; +use crate::parsing::RollupType; use crate::storage::index_account; use crate::{tick_model, CONFIG}; @@ -514,6 +522,45 @@ impl From> for ExecutionResult { } } +struct TypedBatch { + pub id: U256, + pub smart_rollup_address: [u8; 20], +} + +impl AtomicBatch for TypedBatch {} + +impl HasEncoding for TypedBatch { + fn encoding() -> tezos_data_encoding::encoding::Encoding { + tezos_data_encoding::encoding::Encoding::Custom + } +} + +impl BinWriter for TypedBatch { + fn bin_write(&self, payload: &mut Vec) -> tezos_data_encoding::enc::BinResult { + payload.extend_from_slice(&[0, 0, 0, 1]); + + let mut bytes = Vec::new(); + self.id.to_big_endian(&mut bytes); + + let data = MichelsonBytes(bytes); + data.bin_write(payload)?; + + payload.push(0x03); + payload.extend_from_slice(&self.smart_rollup_address); + payload.push(0x00); + + let entry = Entrypoint::default(); + entry.bin_write(payload)?; + Ok(()) + } +} + +impl NomReader for TypedBatch { + fn nom_read(input: &[u8]) -> tezos_data_encoding::nom::NomResult { + todo!() + } +} + #[allow(clippy::too_many_arguments)] pub fn handle_transaction_result( host: &mut Host, @@ -546,8 +593,19 @@ pub fn handle_transaction_result( log!(host, Benchmarking, "reason: {:?}", outcome.reason); fee_updates.modify_outcome(outcome); for message in outcome.withdrawals.drain(..) { - let outbox_message: OutboxMessage = message; - let len = outbox_queue.queue_message(host, outbox_message)?; + //let outbox_message: OutboxMessage = message; + //let len = outbox_queue.queue_message(host, outbox_message)?; + + let meta = host.reveal_metadata(); + + let tracking_tx = OutboxMessageTransaction { + parameters: RollupType::Right(MichelsonBytes(vec![1u8; 32])), + destination: Contract::Rollup(SmartRollupHash(meta.raw_rollup_address.to_vec())), + entrypoint: Entrypoint::default() + }; + + let len = outbox_queue.queue_message(host, tracking_tx)?; + log!(host, Debug, "Length of the outbox queue: {}", len); } } diff --git a/etherlink/kernel_evm/kernel/src/parsing.rs b/etherlink/kernel_evm/kernel/src/parsing.rs index 246fd1b102b3..46f040bdebe5 100644 --- a/etherlink/kernel_evm/kernel/src/parsing.rs +++ b/etherlink/kernel_evm/kernel/src/parsing.rs @@ -563,6 +563,7 @@ impl InputResult { } else if tezos_contracts.is_sequencer_governance(&source) { Self::parse_sequencer_update(&bytes) } else { + log!(host, Info, "UNPARSABLE BYTES: {:?}", bytes); Self::Unparsable } } @@ -644,7 +645,10 @@ impl InputResult { enable_fa_deposits, ), }, - Err(_) => InputResult::Unparsable, + Err(err) => { + log!(host, Info, "UNPARSABLE MESSAGE: {:?}", err); + InputResult::Unparsable + }, } } } diff --git a/etherlink/tezt/lib/helpers.ml b/etherlink/tezt/lib/helpers.ml index 8a4aac8b7eca..5a29110c199e 100644 --- a/etherlink/tezt/lib/helpers.ml +++ b/etherlink/tezt/lib/helpers.ml @@ -313,7 +313,7 @@ let find_and_execute_withdrawal ~withdrawal_level ~commitment_period (* Construct and execute the outbox proof. *) let find_outbox level = let rec aux level' = - if level' > level + 10 then + if level' > level + 20 then Test.fail "Looked for an outbox for 10 levels, stopping the loop" else let* outbox = diff --git a/src/kernel_sdk/encoding/src/contract.rs b/src/kernel_sdk/encoding/src/contract.rs index d207c64b1dc1..a8fc03fe926a 100644 --- a/src/kernel_sdk/encoding/src/contract.rs +++ b/src/kernel_sdk/encoding/src/contract.rs @@ -5,7 +5,7 @@ //! Definitions relating to Layer-1 accounts, which the kernel may interact with. use crypto::base58::{FromBase58Check, FromBase58CheckError}; -use crypto::hash::{ContractKt1Hash, Hash, HashTrait, HashType}; +use crypto::hash::{ContractKt1Hash, Hash, HashTrait, HashType, SmartRollupHash}; use nom::branch::alt; use nom::bytes::complete::tag; use nom::combinator::map; @@ -28,6 +28,8 @@ pub enum Contract { Implicit(PublicKeyHash), /// Smart contract account Originated(ContractKt1Hash), + /// Smart rollup + Rollup(SmartRollupHash), } impl Contract { @@ -47,6 +49,7 @@ impl Contract { match self { Self::Implicit(pkh) => pkh.to_b58check(), Self::Originated(kt1) => kt1.to_b58check(), + Self::Rollup(sr1) => sr1.to_base58_check(), } } } @@ -56,6 +59,7 @@ impl From for Hash { match c { Contract::Implicit(pkh) => pkh.into(), Contract::Originated(ckt1) => ckt1.into(), + Contract::Rollup(sr1) => sr1.into(), } } } @@ -107,6 +111,14 @@ impl BinWriter for Contract { enc::bytes(&mut bytes, output)?; Ok(()) } + Self::Rollup(rollup) => { + enc::put_byte(&3, output); + let mut bytes: Hash = rollup.as_ref().to_vec(); + // Originated is padded + bytes.push(0); + enc::bytes(&mut bytes, output)?; + Ok(()) + } } } } -- GitLab