Skip to content

Commit 9e2c1c7

Browse files
feat(wip): hook 1195
Signed-off-by: Ivaylo Nikolov <[email protected]>
1 parent f45e3cd commit 9e2c1c7

19 files changed

+1471
-13
lines changed

src/account/account_create_transaction.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ pub struct AccountCreateTransactionData {
7676

7777
/// If true, the account declines receiving a staking reward. The default value is false.
7878
decline_staking_reward: bool,
79+
80+
/// Hooks to add immediately after creating this account.
81+
hooks: Vec<LambdaEvmHook>,
7982
}
8083

8184
impl Default for AccountCreateTransactionData {
@@ -91,6 +94,7 @@ impl Default for AccountCreateTransactionData {
9194
alias: None,
9295
staked_id: None,
9396
decline_staking_reward: false,
97+
hooks: Vec::new(),
9498
}
9599
}
96100
}
@@ -293,6 +297,20 @@ impl AccountCreateTransaction {
293297
self.data_mut().decline_staking_reward = decline;
294298
self
295299
}
300+
301+
pub fn add_hook(&mut self, hook: LambdaEvmHook) -> &mut Self {
302+
self.data_mut().hooks.push(hook);
303+
self
304+
}
305+
306+
pub fn set_hooks(&mut self, hooks: Vec<LambdaEvmHook>) -> &mut Self {
307+
self.data_mut().hooks = hooks;
308+
self
309+
}
310+
311+
pub fn get_hooks(&self) -> &[LambdaEvmHook] {
312+
&self.data().hooks
313+
}
296314
}
297315

298316
impl TransactionData for AccountCreateTransactionData {}
@@ -353,6 +371,11 @@ impl FromProtobuf<services::CryptoCreateTransactionBody> for AccountCreateTransa
353371
alias,
354372
staked_id: Option::from_protobuf(pb.staked_id)?,
355373
decline_staking_reward: pb.decline_reward,
374+
hooks: pb
375+
.hook_creation_details
376+
.into_iter()
377+
.map(LambdaEvmHook::from_protobuf)
378+
.collect::<Result<Vec<_>, _>>()?,
356379
})
357380
}
358381
}
@@ -391,6 +414,7 @@ impl ToProtobuf for AccountCreateTransactionData {
391414
alias: self.alias.map_or(vec![], |it| it.to_bytes().to_vec()),
392415
decline_reward: self.decline_staking_reward,
393416
staked_id,
417+
hooks: self.hooks.iter().map(|hook| hook.to_protobuf()).collect(),
394418
}
395419
}
396420
}

src/account/account_update_transaction.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use time::{
88
};
99
use tonic::transport::Channel;
1010

11+
use crate::hooks::LambdaEvmHook;
1112
use crate::ledger_id::RefLedgerId;
1213
use crate::protobuf::{
1314
FromProtobuf,
@@ -89,6 +90,10 @@ pub struct AccountUpdateTransactionData {
8990

9091
/// If true, the account declines receiving a staking reward. The default value is false.
9192
decline_staking_reward: Option<bool>,
93+
94+
/// Hooks to add immediately after updating this account.
95+
hooks: Vec<LambdaEvmHook>,
96+
hook_ids_to_delete: Vec<i64>,
9297
}
9398

9499
impl AccountUpdateTransaction {
@@ -271,6 +276,42 @@ impl AccountUpdateTransaction {
271276
self.data_mut().decline_staking_reward = Some(decline);
272277
self
273278
}
279+
280+
/// Returns the hooks to be created.
281+
#[must_use]
282+
pub fn get_hooks_to_create(&self) -> &[LambdaEvmHook] {
283+
&self.data().hooks
284+
}
285+
286+
/// Adds a hook to be created.
287+
pub fn add_hook(&mut self, hook: LambdaEvmHook) -> &mut Self {
288+
self.data_mut().hooks.push(hook);
289+
self
290+
}
291+
292+
/// Sets the hooks to be created.
293+
pub fn set_hooks(&mut self, hooks: Vec<LambdaEvmHook>) -> &mut Self {
294+
self.data_mut().hooks = hooks;
295+
self
296+
}
297+
298+
/// Returns the hook IDs to be deleted.
299+
#[must_use]
300+
pub fn get_hooks_to_delete(&self) -> &[i64] {
301+
&self.data().hook_ids_to_delete
302+
}
303+
304+
/// Adds a hook ID to be deleted.
305+
pub fn delete_hook(&mut self, hook_id: i64) -> &mut Self {
306+
self.data_mut().hook_ids_to_delete.push(hook_id);
307+
self
308+
}
309+
310+
/// Sets the hook IDs to be deleted.
311+
pub fn delete_hooks(&mut self, hook_ids: Vec<i64>) -> &mut Self {
312+
self.data_mut().hook_ids_to_delete = hook_ids;
313+
self
314+
}
274315
}
275316

276317
impl TransactionData for AccountUpdateTransactionData {}
@@ -339,6 +380,12 @@ impl FromProtobuf<services::CryptoUpdateTransactionBody> for AccountUpdateTransa
339380
max_automatic_token_associations: pb.max_automatic_token_associations,
340381
staked_id: Option::from_protobuf(pb.staked_id)?,
341382
decline_staking_reward: pb.decline_reward,
383+
hooks: pb
384+
.hook_creation_details
385+
.into_iter()
386+
.map(LambdaEvmHook::from_protobuf)
387+
.collect::<Result<Vec<_>, _>>()?,
388+
hook_ids_to_delete: pb.hook_ids_to_delete,
342389
})
343390
}
344391
}
@@ -382,6 +429,8 @@ impl ToProtobuf for AccountUpdateTransactionData {
382429
receive_record_threshold_field: None,
383430
receiver_sig_required_field: receiver_signature_required,
384431
staked_id,
432+
hooks: self.hooks.iter().map(|hook| hook.to_protobuf()).collect(),
433+
hook_ids_to_delete: self.hook_ids_to_delete,
385434
}
386435
}
387436
}

src/contract/contract_create_transaction.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ pub struct ContractCreateTransactionData {
5757
staked_id: Option<StakedId>,
5858

5959
decline_staking_reward: bool,
60+
61+
/// Hooks to add immediately after creating this contract.
62+
hooks: Vec<LambdaEvmHook>,
6063
}
6164

6265
impl Default for ContractCreateTransactionData {
@@ -74,6 +77,7 @@ impl Default for ContractCreateTransactionData {
7477
auto_renew_account_id: None,
7578
staked_id: None,
7679
decline_staking_reward: false,
80+
hooks: Vec::new(),
7781
}
7882
}
7983
}
@@ -313,6 +317,11 @@ impl FromProtobuf<services::ContractCreateTransactionBody> for ContractCreateTra
313317
auto_renew_account_id: Option::from_protobuf(pb.auto_renew_account_id)?,
314318
staked_id: Option::from_protobuf(pb.staked_id)?,
315319
decline_staking_reward: pb.decline_reward,
320+
hooks: pb
321+
.hook_creation_details
322+
.into_iter()
323+
.map(LambdaEvmHook::from_protobuf)
324+
.collect::<Result<Vec<_>, _>>()?,
316325
})
317326
}
318327
}
@@ -372,6 +381,7 @@ impl ToProtobuf for ContractCreateTransactionData {
372381
decline_reward: self.decline_staking_reward,
373382
initcode_source,
374383
staked_id,
384+
hooks: self.hooks.iter().map(|hook| hook.to_protobuf()).collect(),
375385
}
376386
}
377387
}

src/contract/contract_update_transaction.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use time::{
88
};
99
use tonic::transport::Channel;
1010

11+
use crate::hooks::LambdaEvmHook;
1112
use crate::ledger_id::RefLedgerId;
1213
use crate::protobuf::FromProtobuf;
1314
use crate::staked_id::StakedId;
@@ -55,6 +56,10 @@ pub struct ContractUpdateTransactionData {
5556
staked_id: Option<StakedId>,
5657

5758
decline_staking_reward: Option<bool>,
59+
60+
/// Hooks to add immediately after updating this contract.
61+
hooks: Vec<LambdaEvmHook>,
62+
hook_ids_to_delete: Vec<i64>,
5863
}
5964

6065
impl ContractUpdateTransaction {
@@ -195,6 +200,42 @@ impl ContractUpdateTransaction {
195200
self.data_mut().decline_staking_reward = Some(decline);
196201
self
197202
}
203+
204+
/// Returns the hooks to be created.
205+
#[must_use]
206+
pub fn get_hooks_to_create(&self) -> &[LambdaEvmHook] {
207+
&self.data().hooks
208+
}
209+
210+
/// Adds a hook to be created.
211+
pub fn add_hook(&mut self, hook: LambdaEvmHook) -> &mut Self {
212+
self.data_mut().hooks.push(hook);
213+
self
214+
}
215+
216+
/// Sets the hooks to be created.
217+
pub fn set_hooks(&mut self, hooks: Vec<LambdaEvmHook>) -> &mut Self {
218+
self.data_mut().hooks = hooks;
219+
self
220+
}
221+
222+
/// Returns the hook IDs to be deleted.
223+
#[must_use]
224+
pub fn get_hooks_to_delete(&self) -> &[i64] {
225+
&self.data().hook_ids_to_delete
226+
}
227+
228+
/// Adds a hook ID to be deleted.
229+
pub fn delete_hook(&mut self, hook_id: i64) -> &mut Self {
230+
self.data_mut().hook_ids_to_delete.push(hook_id);
231+
self
232+
}
233+
234+
/// Sets the hook IDs to be deleted.
235+
pub fn delete_hooks(&mut self, hook_ids: Vec<i64>) -> &mut Self {
236+
self.data_mut().hook_ids_to_delete = hook_ids;
237+
self
238+
}
198239
}
199240

200241
impl TransactionData for ContractUpdateTransactionData {}
@@ -255,6 +296,12 @@ impl FromProtobuf<services::ContractUpdateTransactionBody> for ContractUpdateTra
255296
proxy_account_id: Option::from_protobuf(pb.proxy_account_id)?,
256297
staked_id: Option::from_protobuf(pb.staked_id)?,
257298
decline_staking_reward: pb.decline_reward,
299+
hooks: pb
300+
.hook_creation_details
301+
.into_iter()
302+
.map(LambdaEvmHook::from_protobuf)
303+
.collect::<Result<Vec<_>, _>>()?,
304+
hook_ids_to_delete: pb.hook_ids_to_delete,
258305
})
259306
}
260307
}
@@ -301,6 +348,8 @@ impl ToProtobuf for ContractUpdateTransactionData {
301348
staked_id,
302349
file_id: None,
303350
memo_field,
351+
hook_creation_details: self.hooks.iter().map(|hook| hook.to_protobuf()).collect(),
352+
hook_ids_to_delete: self.hook_ids_to_delete.clone(),
304353
}
305354
}
306355
}

src/hooks/evm_hook_call.rs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
use crate::protobuf::services;
2+
use crate::{
3+
FromProtobuf,
4+
ToProtobuf,
5+
};
6+
7+
/// An EVM hook call.
8+
#[derive(Debug, Clone, PartialEq, Eq)]
9+
pub struct EvmHookCall {
10+
/// The call data for the EVM hook.
11+
pub call_data: Option<Vec<u8>>,
12+
}
13+
14+
impl EvmHookCall {
15+
/// Create a new `EvmHookCall`.
16+
pub fn new(call_data: Option<Vec<u8>>) -> Self {
17+
Self { call_data }
18+
}
19+
20+
pub fn set_call_data(&mut self, call_data: Vec<u8>) -> &mut Self {
21+
self.call_data = Some(call_data);
22+
self
23+
}
24+
}
25+
26+
impl ToProtobuf for EvmHookCall {
27+
type Protobuf = services::EvmHookCall;
28+
29+
fn to_protobuf(&self) -> Self::Protobuf {
30+
services::EvmHookCall { call_data: self.call_data.clone().unwrap_or_default() }
31+
}
32+
}
33+
34+
impl FromProtobuf<services::EvmHookCall> for EvmHookCall {
35+
fn from_protobuf(pb: services::EvmHookCall) -> crate::Result<Self> {
36+
Ok(Self { call_data: if pb.call_data.is_empty() { None } else { Some(pb.call_data) } })
37+
}
38+
}
39+
40+
#[cfg(test)]
41+
mod tests {
42+
use super::*;
43+
44+
#[test]
45+
fn test_evm_hook_call_creation() {
46+
let call_data = vec![1, 2, 3, 4, 5];
47+
let hook_call = EvmHookCall::with_call_data(call_data.clone());
48+
49+
assert_eq!(hook_call.call_data, Some(call_data));
50+
}
51+
52+
#[test]
53+
fn test_evm_hook_call_setters() {
54+
let mut hook_call = EvmHookCall::new(None);
55+
let call_data = vec![6, 7, 8, 9, 10];
56+
57+
hook_call.set_call_data(call_data.clone());
58+
assert_eq!(hook_call.call_data, Some(call_data));
59+
}
60+
61+
#[test]
62+
fn test_evm_hook_call_protobuf_roundtrip() {
63+
let call_data = vec![11, 12, 13, 14, 15];
64+
let original = EvmHookCall::with_call_data(call_data);
65+
66+
let protobuf = original.to_protobuf();
67+
let reconstructed = EvmHookCall::from_protobuf(protobuf).unwrap();
68+
69+
assert_eq!(original, reconstructed);
70+
}
71+
}

0 commit comments

Comments
 (0)