use super::{
NnsRegistryReplayError, NnsRegistryReplayProgress, NnsRegistryReplaySession,
NnsRegistryReplaySessionLimits,
};
use crate::nns::registry::NnsAuthenticatedRegistryDeltaBatch;
#[derive(Debug, Eq, PartialEq)]
pub struct NnsAuthenticatedRegistryReplayBuilder {
session: NnsRegistryReplaySession,
}
impl NnsAuthenticatedRegistryReplayBuilder {
#[must_use]
pub const fn new(limits: NnsRegistryReplaySessionLimits) -> Self {
Self {
session: NnsRegistryReplaySession::new(limits),
}
}
pub fn apply_batch(
&mut self,
batch: &NnsAuthenticatedRegistryDeltaBatch<'_>,
) -> Result<NnsRegistryReplayProgress, NnsRegistryReplayError> {
self.session.apply_prevalidated_batch(batch.report())
}
#[must_use]
pub const fn replay_session(&self) -> &NnsRegistryReplaySession {
&self.session
}
pub fn into_authenticated_replay_session(
self,
) -> Result<NnsAuthenticatedRegistryReplaySession, NnsRegistryReplayError> {
NnsAuthenticatedRegistryReplaySession::from_verified_complete(self.session)
}
}
#[derive(Debug, Eq, PartialEq)]
pub struct NnsAuthenticatedRegistryReplaySession {
session: NnsRegistryReplaySession,
}
impl NnsAuthenticatedRegistryReplaySession {
pub(super) fn from_verified_complete(
session: NnsRegistryReplaySession,
) -> Result<Self, NnsRegistryReplayError> {
if !session.is_complete()
|| session.root_key_digest().is_none()
|| session.evidence_chain_digest().is_none()
|| session.complete_state_digest().is_none()
{
return Err(
NnsRegistryReplayError::AuthenticationRequiresCompleteSession {
selected_version: session.selected_version(),
through_version: session.state().through_version(),
},
);
}
Ok(Self { session })
}
#[must_use]
pub const fn replay_session(&self) -> &NnsRegistryReplaySession {
&self.session
}
#[must_use]
pub fn into_replay_session(self) -> NnsRegistryReplaySession {
self.session
}
}