-
Notifications
You must be signed in to change notification settings - Fork 27
feat: hooks necessary files #1153
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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: 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); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| 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 { | ||
| #[allow(unreachable_patterns)] | ||
| 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)?) | ||
| } | ||
| // For future unsupported bytecode sources. | ||
| Some(_) => { | ||
| return Err(crate::Error::from_protobuf("unsupported EvmHookSpec.bytecode_source")); | ||
| } | ||
| None => None, | ||
| }; | ||
| Ok(Self { contract_id }) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use crate::contract::ContractId; | ||
|
|
||
| #[test] | ||
| fn new_with_contract_id_sets_field() { | ||
| let cid = ContractId::new(0, 0, 123); | ||
| let spec = EvmHookSpec::new(Some(cid)); | ||
| assert_eq!(spec.contract_id, Some(cid)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn new_without_contract_id_sets_none() { | ||
| let spec = EvmHookSpec::new(None); | ||
| assert!(spec.contract_id.is_none()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn to_protobuf_with_contract_id_sets_bytecode_source() { | ||
| let cid = ContractId::new(0, 0, 321); | ||
| let spec = EvmHookSpec::new(Some(cid)); | ||
| let pb = spec.to_protobuf(); | ||
|
|
||
| let got = match pb.bytecode_source { | ||
| Some(hedera_proto::services::evm_hook_spec::BytecodeSource::ContractId(id)) => { | ||
| Some(ContractId::from_protobuf(id).unwrap()) | ||
| } | ||
| None => None, | ||
| }; | ||
|
|
||
| assert_eq!(got, Some(cid)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn to_protobuf_without_contract_id_sets_none() { | ||
| let spec = EvmHookSpec::new(None); | ||
| let pb = spec.to_protobuf(); | ||
| assert!(pb.bytecode_source.is_none()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn from_protobuf_with_contract_id_parses() { | ||
| let cid = ContractId::new(0, 0, 555); | ||
| let pb = hedera_proto::services::EvmHookSpec { | ||
| bytecode_source: Some( | ||
| hedera_proto::services::evm_hook_spec::BytecodeSource::ContractId( | ||
| cid.to_protobuf(), | ||
| ), | ||
| ), | ||
| }; | ||
|
|
||
| let spec = EvmHookSpec::from_protobuf(pb).unwrap(); | ||
| assert_eq!(spec.contract_id, Some(cid)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn from_protobuf_without_contract_id_parses_none() { | ||
| let pb = hedera_proto::services::EvmHookSpec { bytecode_source: None }; | ||
| let spec = EvmHookSpec::from_protobuf(pb).unwrap(); | ||
| assert!(spec.contract_id.is_none()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn protobuf_roundtrip() { | ||
| let cid = ContractId::new(0, 0, 999); | ||
| let original = EvmHookSpec::new(Some(cid)); | ||
| let pb = original.to_protobuf(); | ||
| let reconstructed = EvmHookSpec::from_protobuf(pb).unwrap(); | ||
| assert_eq!(original, reconstructed); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.