Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions src/hooks/evm_hook_call.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
use hedera_proto::services;

use crate::{
FromProtobuf,
ToProtobuf,
};

/// An EVM hook call.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EvmHookCall {
/// The call data for the EVM hook.
pub call_data: Option<Vec<u8>>,
/// The gas limit for the hook call.
pub gas_limit: Option<u64>,
}

impl EvmHookCall {
/// Create a new `EvmHookCall`.
pub fn new(call_data: Option<Vec<u8>>) -> Self {
Self { call_data, gas_limit: None }
}

/// Set the call data for the hook.
pub fn set_call_data(&mut self, call_data: Vec<u8>) -> &mut Self {
self.call_data = Some(call_data);
self
}

/// Set the gas limit for the hook call.
pub fn set_gas_limit(&mut self, gas_limit: u64) -> &mut Self {
self.gas_limit = Some(gas_limit);
self
}
}

impl ToProtobuf for EvmHookCall {
type Protobuf = services::EvmHookCall;

fn to_protobuf(&self) -> Self::Protobuf {
services::EvmHookCall {
data: self.call_data.clone().unwrap_or_default(),
gas_limit: self.gas_limit.unwrap_or(0),
}
}
}

impl FromProtobuf<services::EvmHookCall> for EvmHookCall {
fn from_protobuf(pb: services::EvmHookCall) -> crate::Result<Self> {
Ok(Self {
call_data: if pb.data.is_empty() { None } else { Some(pb.data) },
gas_limit: if pb.gas_limit == 0 { None } else { Some(pb.gas_limit) },
})
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_evm_hook_call_creation() {
let call_data = vec![1, 2, 3, 4, 5];
let hook_call = EvmHookCall::new(Some(call_data.clone()));

assert_eq!(hook_call.call_data, Some(call_data));
}

#[test]
fn test_evm_hook_call_setters() {
let mut hook_call = EvmHookCall::new(None);
let call_data = vec![6, 7, 8, 9, 10];

hook_call.set_call_data(call_data.clone());
assert_eq!(hook_call.call_data, Some(call_data));
}

#[test]
fn test_evm_hook_call_protobuf_roundtrip() {
let call_data = vec![11, 12, 13, 14, 15];
let original = EvmHookCall::new(Some(call_data));

let protobuf = original.to_protobuf();
let reconstructed = EvmHookCall::from_protobuf(protobuf).unwrap();

assert_eq!(original, reconstructed);
}
}
71 changes: 71 additions & 0 deletions src/hooks/evm_hook_spec.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
use hedera_proto::services;

use crate::contract::ContractId;
use crate::{
FromProtobuf,
ToProtobuf,
};

/// Shared specifications for an EVM hook. May be used for any extension point.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EvmHookSpec {
/// The id of a contract that implements the extension point API with EVM bytecode.
pub contract_id: Option<ContractId>,
}

impl EvmHookSpec {
/// Create a new `EvmHookSpec`.
pub fn new(contract_id: Option<ContractId>) -> Self {
Self { contract_id }
}
}

impl ToProtobuf for EvmHookSpec {
type Protobuf = services::EvmHookSpec;

fn to_protobuf(&self) -> Self::Protobuf {
services::EvmHookSpec {
bytecode_source: self
.contract_id
.as_ref()
.map(|id| services::evm_hook_spec::BytecodeSource::ContractId(id.to_protobuf())),
}
}
}

impl FromProtobuf<services::EvmHookSpec> for EvmHookSpec {
fn from_protobuf(pb: services::EvmHookSpec) -> crate::Result<Self> {
let contract_id = match pb.bytecode_source {
Some(services::evm_hook_spec::BytecodeSource::ContractId(id)) => {
Some(ContractId::from_protobuf(id)?)
}
None => None,
};

Ok(Self { contract_id })
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::contract::ContractId;

#[test]
fn test_evm_hook_spec_creation() {
let contract_id = ContractId::new(0, 0, 123);
let spec = EvmHookSpec::new(Some(contract_id.clone()));

assert_eq!(spec.contract_id, Some(contract_id));
}

#[test]
fn test_evm_hook_spec_protobuf_roundtrip() {
let contract_id = ContractId::new(0, 0, 456);
let original = EvmHookSpec::new(Some(contract_id));
let protobuf = original.to_protobuf();
let reconstructed = EvmHookSpec::from_protobuf(protobuf).unwrap();

assert_eq!(original, reconstructed);
}
}
73 changes: 73 additions & 0 deletions src/hooks/fungible_hook_call.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use hedera_proto::services;

use crate::hooks::{
EvmHookCall,
FungibleHookType,
HookCall,
};
use crate::{
FromProtobuf,
ToProtobuf,
};

/// A typed hook call for fungible (HBAR and FT) transfers.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FungibleHookCall {
/// The underlying hook call data.
pub hook_call: HookCall,
/// The type of fungible hook.
pub hook_type: FungibleHookType,
}

impl FungibleHookCall {
/// Create a new `FungibleHookCall`.
pub fn new(hook_call: HookCall, hook_type: FungibleHookType) -> Self {
Self { hook_call, hook_type }
}

/// Internal method to create from protobuf with a known type.
pub(crate) fn from_protobuf_with_type(
pb: services::HookCall,
hook_type: FungibleHookType,
) -> crate::Result<Self> {
Ok(Self { hook_call: HookCall::from_protobuf(pb)?, hook_type })
}
}

impl ToProtobuf for FungibleHookCall {
type Protobuf = services::HookCall;

fn to_protobuf(&self) -> Self::Protobuf {
self.hook_call.to_protobuf()
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_fungible_hook_call_creation() {
let hook_id = 123;
let hook_type = FungibleHookType::PreTxAllowanceHook;
let mut hook_call_obj = HookCall::new(None, None);
hook_call_obj.set_hook_id(hook_id);
let hook_call = FungibleHookCall::new(hook_call_obj, hook_type);

assert_eq!(hook_call.hook_call.hook_id, Some(hook_id));
assert_eq!(hook_call.hook_type, hook_type);
}

#[test]
fn test_fungible_hook_call_with_call() {
let call_data = vec![1, 2, 3, 4, 5];
let evm_call = EvmHookCall::new(Some(call_data));
let hook_type = FungibleHookType::PrePostTxAllowanceHook;
let mut hook_call_obj = HookCall::new(None, None);
hook_call_obj.set_call(evm_call.clone());
let hook_call = FungibleHookCall::new(hook_call_obj, hook_type);

assert_eq!(hook_call.hook_call.call, Some(evm_call));
assert_eq!(hook_call.hook_type, hook_type);
}
}
28 changes: 28 additions & 0 deletions src/hooks/fungible_hook_type.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/// Types of fungible (HBAR and FT) hooks.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum FungibleHookType {
/// A single call made before attempting the transfer.
PreTxAllowanceHook = 0,
/// Two calls - first before attempting the transfer (allowPre), and second after
/// attempting the transfer (allowPost).
PrePostTxAllowanceHook = 1,
}

impl FungibleHookType {
/// Returns the numeric value of the hook type.
pub fn value(&self) -> u8 {
*self as u8
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_fungible_hook_type_values() {
assert_eq!(FungibleHookType::PreTxAllowanceHook.value(), 0);
assert_eq!(FungibleHookType::PrePostTxAllowanceHook.value(), 1);
}
}
Loading
Loading