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::assembly::Library;
8use miden_protocol::crypto::rand::FeltRng;
9use miden_protocol::errors::NoteError;
10use miden_protocol::note::{Note, NoteScript, NoteScriptRoot};
11use miden_protocol::utils::serde::Deserializable;
12use miden_utils_sync::LazyLock;
13
14use crate::ExitRoot;
15use crate::ger_note::create_ger_note;
16
17// NOTE SCRIPT
18// ================================================================================================
19
20// Initialize the UPDATE_GER note script only once
21static UPDATE_GER_SCRIPT: LazyLock<NoteScript> = LazyLock::new(|| {
22    let bytes = include_bytes!(concat!(env!("OUT_DIR"), "/assets/note_scripts/update_ger.masp"));
23    let library =
24        Library::read_from_bytes(bytes).expect("shipped UPDATE_GER script library is well-formed");
25    NoteScript::from_library(&library).expect("shipped UPDATE_GER script is well-formed")
26});
27
28// UPDATE_GER NOTE
29// ================================================================================================
30
31/// UPDATE_GER note.
32///
33/// This note is used to update the Global Exit Root (GER) in the bridge account.
34/// It carries the new GER data and is always public.
35pub struct UpdateGerNote;
36
37impl UpdateGerNote {
38    // CONSTANTS
39    // --------------------------------------------------------------------------------------------
40
41    /// Expected number of storage items for an UPDATE_GER note.
42    pub const NUM_STORAGE_ITEMS: usize = 8;
43
44    // PUBLIC ACCESSORS
45    // --------------------------------------------------------------------------------------------
46
47    /// Returns the UPDATE_GER note script.
48    pub fn script() -> NoteScript {
49        UPDATE_GER_SCRIPT.clone()
50    }
51
52    /// Returns the UPDATE_GER note script root.
53    pub fn script_root() -> NoteScriptRoot {
54        UPDATE_GER_SCRIPT.root()
55    }
56
57    // BUILDERS
58    // --------------------------------------------------------------------------------------------
59
60    /// Creates an UPDATE_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
66    /// - `sender_account_id`: The account ID of the note creator
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}