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;
use crate::note::NetworkAccountTarget;
use crate::note::costs::{NoteConsumptionCost, PAUSE_CONFIG_CONSUMPTION_CYCLES};
const PAUSE_CONFIG_SCRIPT_PATH: &str = "::miden::standards::notes::pause_config::main";
static PAUSE_CONFIG_SCRIPT: LazyLock<NoteScript> = LazyLock::new(|| {
let standards_lib = StandardsLib::default();
let path = Path::new(PAUSE_CONFIG_SCRIPT_PATH);
NoteScript::from_package_reference(standards_lib.as_ref(), path)
.expect("Standards library contains PAUSE_CONFIG note script procedure")
});
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PauseConfig {
Pause,
Unpause,
}
impl PauseConfig {
const SELECTOR_PAUSE: u8 = 0;
const SELECTOR_UNPAUSE: u8 = 1;
fn to_storage_values(self) -> Vec<Felt> {
match self {
PauseConfig::Pause => vec![Felt::from(Self::SELECTOR_PAUSE)],
PauseConfig::Unpause => vec![Felt::from(Self::SELECTOR_UNPAUSE)],
}
}
}
impl From<PauseConfig> for NoteStorage {
fn from(config: PauseConfig) -> Self {
NoteStorage::new(config.to_storage_values())
.expect("number of storage items should not exceed max storage items")
}
}
#[derive(Debug, Clone)]
pub struct PauseConfigNote {
sender: AccountId,
target: AccountId,
config: PauseConfig,
serial_number: Word,
attachments: NoteAttachments,
}
#[bon::bon]
impl PauseConfigNote {
#[builder]
pub fn new(
#[builder(field)] mut attachments: Vec<NoteAttachment>,
sender: AccountId,
target: AccountId,
config: PauseConfig,
serial_number: Word,
) -> Result<Self, NoteError> {
NetworkAccountTarget::ensure_presence(&mut attachments, target).map_err(|err| {
NoteError::other_with_source(
"failed to bind the PauseConfig note to its target account",
err,
)
})?;
let attachments = NoteAttachments::new(attachments)?;
Ok(Self {
sender,
target,
config,
serial_number,
attachments,
})
}
}
impl PauseConfigNote {
pub const NUM_STORAGE_ITEMS: usize = 1;
pub fn script() -> NoteScript {
PAUSE_CONFIG_SCRIPT.clone()
}
pub fn script_root() -> NoteScriptRoot {
PAUSE_CONFIG_SCRIPT.root()
}
pub fn sender(&self) -> AccountId {
self.sender
}
pub fn account(&self) -> AccountId {
self.target
}
pub fn config(&self) -> PauseConfig {
self.config
}
pub fn serial_number(&self) -> Word {
self.serial_number
}
pub fn attachments(&self) -> &NoteAttachments {
&self.attachments
}
}
impl<S: pause_config_note_builder::State> PauseConfigNoteBuilder<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_config_note_builder::State> PauseConfigNoteBuilder<S>
where
S::SerialNumber: pause_config_note_builder::IsUnset,
{
pub fn generate_serial_number(
self,
rng: &mut impl FeltRng,
) -> PauseConfigNoteBuilder<pause_config_note_builder::SetSerialNumber<S>> {
self.serial_number(rng.draw_word())
}
}
impl From<PauseConfigNote> for Note {
fn from(note: PauseConfigNote) -> Self {
let metadata = PartialNoteMetadata::new(note.sender, NoteType::Public)
.with_tag(NoteTag::with_account_target(note.target));
let recipient = NoteRecipient::new(
note.serial_number,
PauseConfigNote::script(),
NoteStorage::from(note.config),
);
Note::with_attachments(NoteAssets::default(), metadata, recipient, note.attachments)
}
}
impl NoteConsumptionCost for PauseConfigNote {
fn consumption_cycles() -> u32 {
PAUSE_CONFIG_CONSUMPTION_CYCLES
}
}
#[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_config_note() {
let mut rng = RandomCoin::new(Word::empty());
let managed = account_id(1);
let sender = account_id(2);
let note = PauseConfigNote::builder()
.sender(sender)
.target(managed)
.config(PauseConfig::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(PauseConfig::Pause);
assert_eq!(pause.items(), &[Felt::from(PauseConfig::SELECTOR_PAUSE)]);
let unpause = NoteStorage::from(PauseConfig::Unpause);
assert_eq!(unpause.items(), &[Felt::from(PauseConfig::SELECTOR_UNPAUSE)]);
}
}