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::{BLOCKLIST_CONFIG_CONSUMPTION_CYCLES, NoteConsumptionCost};
const BLOCKLIST_CONFIG_SCRIPT_PATH: &str = "::miden::standards::notes::blocklist_config::main";
static BLOCKLIST_CONFIG_SCRIPT: LazyLock<NoteScript> = LazyLock::new(|| {
let standards_lib = StandardsLib::default();
let path = Path::new(BLOCKLIST_CONFIG_SCRIPT_PATH);
NoteScript::from_package_reference(standards_lib.as_ref(), path)
.expect("Standards library contains BLOCKLIST_CONFIG note script procedure")
});
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BlocklistConfig {
BlockAccount { account: AccountId },
UnblockAccount { account: AccountId },
}
impl BlocklistConfig {
const SELECTOR_BLOCK_ACCOUNT: u8 = 0;
const SELECTOR_UNBLOCK_ACCOUNT: u8 = 1;
const fn selector(self) -> u8 {
match self {
BlocklistConfig::BlockAccount { .. } => Self::SELECTOR_BLOCK_ACCOUNT,
BlocklistConfig::UnblockAccount { .. } => Self::SELECTOR_UNBLOCK_ACCOUNT,
}
}
const fn target(self) -> AccountId {
match self {
BlocklistConfig::BlockAccount { account }
| BlocklistConfig::UnblockAccount { account } => account,
}
}
fn to_storage_values(self) -> Vec<Felt> {
let account = self.target();
vec![Felt::from(self.selector()), account.suffix(), account.prefix().as_felt()]
}
}
impl From<BlocklistConfig> for NoteStorage {
fn from(config: BlocklistConfig) -> Self {
NoteStorage::new(config.to_storage_values())
.expect("number of storage items should not exceed max storage items")
}
}
#[derive(Debug, Clone)]
pub struct BlocklistConfigNote {
sender: AccountId,
target: AccountId,
config: BlocklistConfig,
serial_number: Word,
attachments: NoteAttachments,
}
#[bon::bon]
impl BlocklistConfigNote {
#[builder]
pub fn new(
#[builder(field)] mut attachments: Vec<NoteAttachment>,
sender: AccountId,
target: AccountId,
config: BlocklistConfig,
serial_number: Word,
) -> Result<Self, NoteError> {
NetworkAccountTarget::ensure_presence(&mut attachments, target).map_err(|err| {
NoteError::other_with_source(
"failed to bind the BlocklistConfig note to its target account",
err,
)
})?;
let attachments = NoteAttachments::new(attachments)?;
Ok(Self {
sender,
target,
config,
serial_number,
attachments,
})
}
}
impl BlocklistConfigNote {
pub const NUM_STORAGE_ITEMS: usize = 3;
pub fn script() -> NoteScript {
BLOCKLIST_CONFIG_SCRIPT.clone()
}
pub fn script_root() -> NoteScriptRoot {
BLOCKLIST_CONFIG_SCRIPT.root()
}
pub fn sender(&self) -> AccountId {
self.sender
}
pub fn target(&self) -> AccountId {
self.target
}
pub fn config(&self) -> BlocklistConfig {
self.config
}
pub fn serial_number(&self) -> Word {
self.serial_number
}
pub fn attachments(&self) -> &NoteAttachments {
&self.attachments
}
}
impl<S: blocklist_config_note_builder::State> BlocklistConfigNoteBuilder<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: blocklist_config_note_builder::State> BlocklistConfigNoteBuilder<S>
where
S::SerialNumber: blocklist_config_note_builder::IsUnset,
{
pub fn generate_serial_number(
self,
rng: &mut impl FeltRng,
) -> BlocklistConfigNoteBuilder<blocklist_config_note_builder::SetSerialNumber<S>> {
self.serial_number(rng.draw_word())
}
}
impl From<BlocklistConfigNote> for Note {
fn from(note: BlocklistConfigNote) -> Self {
let metadata = PartialNoteMetadata::new(note.sender, NoteType::Public)
.with_tag(NoteTag::with_account_target(note.target));
let recipient = NoteRecipient::new(
note.serial_number,
BlocklistConfigNote::script(),
NoteStorage::from(note.config),
);
Note::with_attachments(NoteAssets::default(), metadata, recipient, note.attachments)
}
}
impl NoteConsumptionCost for BlocklistConfigNote {
fn consumption_cycles() -> u32 {
BLOCKLIST_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_blocklist_config_note() {
let mut rng = RandomCoin::new(Word::empty());
let managed = account_id(1);
let owner = account_id(2);
let blocked = account_id(3);
let note = BlocklistConfigNote::builder()
.sender(owner)
.target(managed)
.config(BlocklistConfig::BlockAccount { account: blocked })
.generate_serial_number(&mut rng)
.build()
.unwrap();
assert_eq!(note.sender(), owner);
assert_eq!(note.target(), 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 block_account_storage_layout() {
let blocked = account_id(3);
let storage = NoteStorage::from(BlocklistConfig::BlockAccount { account: blocked });
assert_eq!(
storage.items(),
&[
Felt::from(BlocklistConfig::SELECTOR_BLOCK_ACCOUNT),
blocked.suffix(),
blocked.prefix().as_felt(),
]
);
}
#[test]
fn unblock_account_storage_layout() {
let blocked = account_id(3);
let storage = NoteStorage::from(BlocklistConfig::UnblockAccount { account: blocked });
assert_eq!(
storage.items(),
&[
Felt::from(BlocklistConfig::SELECTOR_UNBLOCK_ACCOUNT),
blocked.suffix(),
blocked.prefix().as_felt(),
]
);
}
}