Skip to main content

miden_agglayer/
update_ger_note.rs

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