use miden_protocol::block::BlockNumber;
use miden_protocol::note::{Note, Nullifier};
use miden_standards::note::AccountTargetNetworkNote;
use crate::actor::has_backoff_passed;
#[derive(Debug, Clone)]
pub struct InflightNetworkNote {
note: AccountTargetNetworkNote,
attempt_count: usize,
last_attempt: Option<BlockNumber>,
}
impl InflightNetworkNote {
pub fn new(note: AccountTargetNetworkNote) -> Self {
Self {
note,
attempt_count: 0,
last_attempt: None,
}
}
pub fn from_parts(
note: AccountTargetNetworkNote,
attempt_count: usize,
last_attempt: Option<BlockNumber>,
) -> Self {
Self { note, attempt_count, last_attempt }
}
pub fn into_inner(self) -> AccountTargetNetworkNote {
self.note
}
pub fn to_inner(&self) -> &AccountTargetNetworkNote {
&self.note
}
pub fn attempt_count(&self) -> usize {
self.attempt_count
}
pub fn is_available(&self, block_num: BlockNumber) -> bool {
self.note.execution_hint().can_be_consumed(block_num).unwrap_or(true)
&& has_backoff_passed(block_num, self.last_attempt, self.attempt_count)
}
pub fn fail(&mut self, block_num: BlockNumber) {
self.last_attempt = Some(block_num);
self.attempt_count += 1;
}
pub fn nullifier(&self) -> Nullifier {
self.note.as_note().nullifier()
}
}
impl From<InflightNetworkNote> for Note {
fn from(value: InflightNetworkNote) -> Self {
value.into_inner().into_note()
}
}