diff --git a/etherlink/kernel_latest/tezos/src/lib.rs b/etherlink/kernel_latest/tezos/src/lib.rs index 4539626c9124312df90937cf63f965c4f651e18c..c8c241be54370f0d330531171fb02219e8cceff5 100644 --- a/etherlink/kernel_latest/tezos/src/lib.rs +++ b/etherlink/kernel_latest/tezos/src/lib.rs @@ -4,3 +4,4 @@ pub mod block; pub mod operation; +pub mod operation_result; diff --git a/etherlink/kernel_latest/tezos/src/operation.rs b/etherlink/kernel_latest/tezos/src/operation.rs index 45bbb5007a428a79fbf7ecee2c21b0bf0b132330..8da026ad6fbdd5c01d9d802b27395f9e34316b84 100644 --- a/etherlink/kernel_latest/tezos/src/operation.rs +++ b/etherlink/kernel_latest/tezos/src/operation.rs @@ -10,9 +10,10 @@ use nom::error::{ErrorKind, ParseError}; use nom::{bytes::complete::take, Finish}; use primitive_types::H256; use tezos_crypto_rs::hash::{HashType, UnknownSignature}; +use tezos_data_encoding::types::Narith; use tezos_data_encoding::{ - enc::{self as tezos_enc, BinError, BinResult, BinWriter}, - nom::{self as tezos_nom, error::DecodeError, NomError, NomReader, NomResult}, + enc::{BinError, BinResult, BinWriter}, + nom::{error::DecodeError, NomError, NomReader, NomResult}, }; use tezos_smart_rollup::types::PublicKey; use tezos_smart_rollup::types::PublicKeyHash; @@ -37,7 +38,7 @@ impl OperationContent { let (array, pk) = PublicKey::nom_read(bytes)?; NomResult::Ok((array, Self::Reveal { pk })) } - _ => Err(nom::Err::Error(tezos_nom::NomError::invalid_tag( + _ => Err(nom::Err::Error(NomError::invalid_tag( bytes, tag.to_string(), ))), @@ -62,10 +63,10 @@ impl BinWriter for OperationContent { #[derive(PartialEq, Debug)] pub struct ManagerOperation { pub source: PublicKeyHash, - pub fee: num_bigint::BigUint, - pub counter: num_bigint::BigUint, - pub gas_limit: num_bigint::BigUint, - pub storage_limit: num_bigint::BigUint, + pub fee: Narith, + pub counter: Narith, + pub gas_limit: Narith, + pub storage_limit: Narith, pub operation: OperationContent, } @@ -87,13 +88,13 @@ impl BinWriter for ManagerOperation { // Push data related to the operation source.bin_write(data)?; - tezos_enc::n_bignum(fee, data)?; + fee.bin_write(data)?; - tezos_enc::n_bignum(counter, data)?; + counter.bin_write(data)?; - tezos_enc::n_bignum(gas_limit, data)?; + gas_limit.bin_write(data)?; - tezos_enc::n_bignum(storage_limit, data)?; + storage_limit.bin_write(data)?; // Append the operation operation.bin_write(data)?; @@ -110,13 +111,13 @@ impl NomReader<'_> for ManagerOperation { let (bytes, source) = PublicKeyHash::nom_read(bytes)?; - let (bytes, fee) = tezos_nom::n_bignum(bytes)?; + let (bytes, fee) = Narith::nom_read(bytes)?; - let (bytes, counter) = tezos_nom::n_bignum(bytes)?; + let (bytes, counter) = Narith::nom_read(bytes)?; - let (bytes, gas_limit) = tezos_nom::n_bignum(bytes)?; + let (bytes, gas_limit) = Narith::nom_read(bytes)?; - let (bytes, storage_limit) = tezos_nom::n_bignum(bytes)?; + let (bytes, storage_limit) = Narith::nom_read(bytes)?; let (bytes, operation) = OperationContent::from_bytes(tag, bytes)?; @@ -198,10 +199,7 @@ impl Operation { pub fn try_from_bytes(data: &[u8]) -> Result> { let (remaining, operation) = Self::nom_read(data).finish()?; if !remaining.is_empty() { - return Err(tezos_nom::NomError::from_error_kind( - remaining, - ErrorKind::NonEmpty, - )); + return Err(NomError::from_error_kind(remaining, ErrorKind::NonEmpty)); } Ok(operation) } diff --git a/etherlink/kernel_latest/tezos/src/operation_result.rs b/etherlink/kernel_latest/tezos/src/operation_result.rs new file mode 100644 index 0000000000000000000000000000000000000000..5675497b701449afde84b0a6df9b99937a47a87f --- /dev/null +++ b/etherlink/kernel_latest/tezos/src/operation_result.rs @@ -0,0 +1,111 @@ +// SPDX-FileCopyrightText: 2025 Functori +// +// SPDX-License-Identifier: MIT + +//! Tezos operations + +/// The whole module is inspired of `src/proto_alpha/lib_protocol/apply_result.ml` to represent the result of an operation +use std::fmt::Debug; +use tezos_data_encoding::enc as tezos_enc; +use tezos_data_encoding::nom as tezos_nom; +use tezos_data_encoding::types::Narith; +use tezos_data_encoding::types::Zarith; +use tezos_enc::BinWriter; +use tezos_nom::NomReader; +use tezos_smart_rollup::types::Contract; +use tezos_smart_rollup::types::{PublicKey, PublicKeyHash}; + +#[derive(Debug, PartialEq, Eq, NomReader, BinWriter)] +pub enum ValidityError { + InvalidCounter(Narith), + CantPayFees(Narith), + EmptyImplicitContract, +} + +#[derive(Debug, PartialEq, Eq, NomReader, BinWriter)] +pub enum ManagerError { + PreviouslyRevealedKey(PublicKey), + InconsistentHash(PublicKeyHash), + InconsistentPublicKey(PublicKeyHash), +} + +#[derive(Debug, PartialEq, Eq, NomReader, BinWriter)] +pub enum OperationError { + Validation(ValidityError), + Manager(ManagerError), +} + +impl From for OperationError { + fn from(value: ValidityError) -> Self { + Self::Validation(value) + } +} + +impl From for OperationError { + fn from(value: ManagerError) -> Self { + Self::Manager(value) + } +} + +pub trait ManagerKind { + type Success: PartialEq + Debug + BinWriter + for<'a> NomReader<'a>; + + fn kind() -> Self; +} + +/// Empty struct to implement [ManagerKind] trait for Reveal +#[derive(PartialEq, Debug)] +pub struct Reveal; + +#[derive(PartialEq, Debug, NomReader, BinWriter)] +pub struct RevealSuccess { + pub consumed_gas: Narith, +} + +#[derive(PartialEq, Debug, NomReader, BinWriter)] +pub struct RevealContent { + pk: PublicKey, +} + +impl ManagerKind for Reveal { + type Success = RevealSuccess; + + fn kind() -> Self { + Self + } +} + +// Inspired from `src/proto_alpha/lib_protocol/apply_operation_result.ml` +// Still need to implement Backtracked and Skipped +#[derive(PartialEq, Debug, BinWriter, NomReader)] +pub enum OperationStatus { + Applied(M::Success), + Failed(Vec), +} + +/// A [Balance] updates can be triggered on different target +#[derive(PartialEq, Debug, NomReader, BinWriter)] +pub enum Balance { + Account(Contract), + BlockFees, +} + +/// Depending of the sign of [credited], the account is credited or debited +#[derive(PartialEq, Debug, NomReader, BinWriter)] +pub struct BalanceUpdate { + pub balance: Balance, + pub credited: Zarith, +} + +// Inspired from `src/proto_alpha/lib_protocol/apply_results.ml` +// Still need to implement internal_results +#[derive(PartialEq, Debug, NomReader, BinWriter)] +pub struct ManagerReceipt { + pub balance_updates: Vec, + pub result: OperationStatus, +} + +#[derive(PartialEq, Debug, NomReader, BinWriter)] +pub enum OperationReceipt { + RevealReceipt(ManagerReceipt), +}