Skip to main content

miden_agglayer/
remove_ger_note.rs

1//! REMOVE_GER note creation utilities.
2//!
3//! This module provides helpers for creating REMOVE_GER notes,
4//! which are used to remove a Global Exit Root from the bridge account and fold it into the
5//! running removed-GER keccak256 hash chain.
6
7use miden_protocol::account::AccountId;
8use miden_protocol::crypto::rand::FeltRng;
9use miden_protocol::errors::NoteError;
10use miden_protocol::note::{Note, NoteScript, NoteScriptRoot};
11use miden_standards::note::costs::NoteConsumptionCost;
12use miden_utils_sync::LazyLock;
13
14use crate::costs::REMOVE_GER_CONSUMPTION_CYCLES;
15use crate::ger_note::create_ger_note;
16use crate::{ExitRoot, note_script};
17
18// NOTE SCRIPT
19// ================================================================================================
20
21/// Path to the REMOVE_GER note script procedure in the agglayer package.
22const REMOVE_GER_SCRIPT_PATH: &str = "::agglayer::notes::remove_ger::main";
23
24// Initialize the REMOVE_GER note script only once
25static REMOVE_GER_SCRIPT: LazyLock<NoteScript> =
26    LazyLock::new(|| note_script(REMOVE_GER_SCRIPT_PATH));
27
28// REMOVE_GER NOTE
29// ================================================================================================
30
31/// REMOVE_GER note.
32///
33/// This note is used to remove a Global Exit Root (GER) from the bridge account and fold it into
34/// the running removed-GER keccak256 hash chain. It carries the GER data and is always public.
35pub struct RemoveGerNote;
36
37impl RemoveGerNote {
38    // CONSTANTS
39    // --------------------------------------------------------------------------------------------
40
41    /// Expected number of storage items for a REMOVE_GER note.
42    pub const NUM_STORAGE_ITEMS: usize = 8;
43
44    // PUBLIC ACCESSORS
45    // --------------------------------------------------------------------------------------------
46
47    /// Returns the REMOVE_GER note script.
48    pub fn script() -> NoteScript {
49        REMOVE_GER_SCRIPT.clone()
50    }
51
52    /// Returns the REMOVE_GER note script root.
53    pub fn script_root() -> NoteScriptRoot {
54        REMOVE_GER_SCRIPT.root()
55    }
56
57    // BUILDERS
58    // --------------------------------------------------------------------------------------------
59
60    /// Creates a REMOVE_GER note with the given GER (Global Exit Root) data.
61    ///
62    /// The note storage contains 8 felts: GER[0..7]
63    ///
64    /// # Parameters
65    /// - `ger`: The Global Exit Root data to remove
66    /// - `sender_account_id`: The account ID of the note creator (must be the GER remover)
67    /// - `target_account_id`: The account ID that will consume this note (bridge account)
68    /// - `rng`: Random number generator for creating the note serial number
69    ///
70    /// # Errors
71    /// Returns an error if note creation fails.
72    pub fn create<R: FeltRng>(
73        ger: ExitRoot,
74        sender_account_id: AccountId,
75        target_account_id: AccountId,
76        rng: &mut R,
77    ) -> Result<Note, NoteError> {
78        create_ger_note(ger, sender_account_id, target_account_id, Self::script(), rng)
79    }
80}
81
82// NOTE CONSUMPTION COST
83// ================================================================================================
84
85impl NoteConsumptionCost for RemoveGerNote {
86    fn consumption_cycles() -> u32 {
87        REMOVE_GER_CONSUMPTION_CYCLES
88    }
89}