use alloc::vec::Vec;
use miden_protocol::account::AccountId;
use miden_protocol::assembly::Path;
use miden_protocol::crypto::rand::FeltRng;
use miden_protocol::errors::NoteError;
use miden_protocol::note::{
Note,
NoteAssets,
NoteAttachment,
NoteAttachments,
NoteRecipient,
NoteScript,
NoteScriptRoot,
NoteStorage,
NoteTag,
NoteType,
PartialNoteMetadata,
};
use miden_protocol::utils::sync::LazyLock;
use miden_protocol::{Felt, Word};
use crate::StandardsLib;
const PAUSE_ACTION_SCRIPT_PATH: &str = "::miden::standards::notes::pause_action::main";
static PAUSE_ACTION_SCRIPT: LazyLock<NoteScript> = LazyLock::new(|| {
let standards_lib = StandardsLib::default();
let path = Path::new(PAUSE_ACTION_SCRIPT_PATH);
NoteScript::from_library_reference(standards_lib.as_ref(), path)
.expect("Standards library contains PAUSE_ACTION note script procedure")
});
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PauseAction {
Pause,
Unpause,
}
impl PauseAction {
const SELECTOR_PAUSE: u8 = 0;
const SELECTOR_UNPAUSE: u8 = 1;
fn to_storage_values(self) -> Vec<Felt> {
match self {
PauseAction::Pause => vec![Felt::from(Self::SELECTOR_PAUSE)],
PauseAction::Unpause => vec![Felt::from(Self::SELECTOR_UNPAUSE)],
}
}
}
impl From<PauseAction> for NoteStorage {
fn from(action: PauseAction) -> Self {
NoteStorage::new(action.to_storage_values())
.expect("number of storage items should not exceed max storage items")
}
}
#[derive(Debug, Clone)]
pub struct PauseActionNote {
sender: AccountId,
account: AccountId,
action: PauseAction,
serial_number: Word,
attachments: NoteAttachments,
}
#[bon::bon]
impl PauseActionNote {
#[builder]
pub fn new(
#[builder(field)] attachments: Vec<NoteAttachment>,
sender: AccountId,
account: AccountId,
action: PauseAction,
serial_number: Word,
) -> Result<Self, NoteError> {
let attachments = NoteAttachments::new(attachments)?;
Ok(Self {
sender,
account,
action,
serial_number,
attachments,
})
}
}
impl PauseActionNote {
pub const NUM_STORAGE_ITEMS: usize = 1;
pub fn script() -> NoteScript {
PAUSE_ACTION_SCRIPT.clone()
}
pub fn script_root() -> NoteScriptRoot {
PAUSE_ACTION_SCRIPT.root()
}
pub fn sender(&self) -> AccountId {
self.sender
}
pub fn account(&self) -> AccountId {
self.account
}
pub fn action(&self) -> PauseAction {
self.action
}
pub fn serial_number(&self) -> Word {
self.serial_number
}
pub fn attachments(&self) -> &NoteAttachments {
&self.attachments
}
}
impl<S: pause_action_note_builder::State> PauseActionNoteBuilder<S> {
pub fn attachment(mut self, attachment: impl Into<NoteAttachment>) -> Self {
self.attachments.push(attachment.into());
self
}
pub fn attachments(
mut self,
attachments: impl IntoIterator<Item = impl Into<NoteAttachment>>,
) -> Self {
self.attachments.extend(attachments.into_iter().map(Into::into));
self
}
}
impl<S: pause_action_note_builder::State> PauseActionNoteBuilder<S>
where
S::SerialNumber: pause_action_note_builder::IsUnset,
{
pub fn generate_serial_number(
self,
rng: &mut impl FeltRng,
) -> PauseActionNoteBuilder<pause_action_note_builder::SetSerialNumber<S>> {
self.serial_number(rng.draw_word())
}
}
impl From<PauseActionNote> for Note {
fn from(note: PauseActionNote) -> Self {
let metadata = PartialNoteMetadata::new(note.sender, NoteType::Public)
.with_tag(NoteTag::with_account_target(note.account));
let recipient = NoteRecipient::new(
note.serial_number,
PauseActionNote::script(),
NoteStorage::from(note.action),
);
Note::with_attachments(NoteAssets::default(), metadata, recipient, note.attachments)
}
}
#[cfg(test)]
mod tests {
use miden_protocol::account::AccountType;
use miden_protocol::crypto::rand::RandomCoin;
use super::*;
fn account_id(seed: u8) -> AccountId {
AccountId::builder()
.account_type(AccountType::Public)
.build_with_seed([seed; 32])
}
#[test]
fn builder_builds_pause_action_note() {
let mut rng = RandomCoin::new(Word::empty());
let managed = account_id(1);
let sender = account_id(2);
let note = PauseActionNote::builder()
.sender(sender)
.account(managed)
.action(PauseAction::Pause)
.generate_serial_number(&mut rng)
.build()
.unwrap();
assert_eq!(note.sender(), sender);
assert_eq!(note.account(), managed);
let note = Note::from(note);
assert_eq!(note.metadata().note_type(), NoteType::Public);
assert_eq!(note.metadata().tag(), NoteTag::with_account_target(managed));
assert_eq!(note.assets().num_assets(), 0);
}
#[test]
fn action_storage_layout() {
let pause = NoteStorage::from(PauseAction::Pause);
assert_eq!(pause.items(), &[Felt::from(PauseAction::SELECTOR_PAUSE)]);
let unpause = NoteStorage::from(PauseAction::Unpause);
assert_eq!(unpause.items(), &[Felt::from(PauseAction::SELECTOR_UNPAUSE)]);
}
}