use alloc::collections::BTreeSet;
use miden_protocol::account::component::{
AccountComponentCode,
AccountComponentMetadata,
StorageSchema,
};
use miden_protocol::account::{AccountComponent, AccountId, AccountProcedureRoot};
use crate::account::account_component_code;
use crate::account::policies::transfer::blocklist::BlocklistStorage;
use crate::procedure_root;
account_component_code!(
BASIC_BLOCKLIST_TRANSFER_POLICY_CODE,
"faucets/policies/transfer/basic_blocklist.masl"
);
procedure_root!(
BASIC_BLOCKLIST_TRANSFER_POLICY_ROOT,
BasicBlocklist::NAME,
BasicBlocklist::PROC_NAME,
BasicBlocklist::code()
);
#[derive(Debug, Clone, Default)]
pub struct BasicBlocklist(BTreeSet<AccountId>);
impl BasicBlocklist {
pub const NAME: &'static str =
"miden::standards::components::faucets::policies::transfer::basic_blocklist";
pub(crate) const PROC_NAME: &str = "check_policy";
pub fn with_blocked_accounts<I>(blocked_accounts: I) -> Self
where
I: IntoIterator<Item = AccountId>,
{
Self(blocked_accounts.into_iter().collect())
}
pub fn blocked_accounts(&self) -> &BTreeSet<AccountId> {
&self.0
}
pub fn code() -> &'static AccountComponentCode {
&BASIC_BLOCKLIST_TRANSFER_POLICY_CODE
}
pub fn root() -> AccountProcedureRoot {
*BASIC_BLOCKLIST_TRANSFER_POLICY_ROOT
}
}
impl From<BasicBlocklist> for AccountComponent {
fn from(blocklist: BasicBlocklist) -> Self {
let storage = BlocklistStorage::with_blocked_accounts(blocklist.0);
let storage_schema = StorageSchema::new([BlocklistStorage::blocked_accounts_slot_schema()])
.expect("storage schema should be valid");
let metadata = AccountComponentMetadata::new(BasicBlocklist::NAME)
.with_description(
"Basic blocklist transfer policy: predicate procedure plus the `blocked_accounts` \
storage map it reads",
)
.with_storage_schema(storage_schema);
AccountComponent::new(BasicBlocklist::code().clone(), vec![storage.into_slot()], metadata)
.expect(
"basic blocklist transfer policy component should satisfy the requirements of a valid account component",
)
}
}