Struct HashChain

Source
pub struct HashChain { /* private fields */ }
Expand description

A live hash chain: holds the root key and current head hash.

Cloning is cheap (Arc’d). Thread-safe via parking_lot::Mutex on the head and chain id (the latter so that Self::load_head_from_file can adopt the persisted id from a previous process invocation).

Implementations§

Source§

impl HashChain

Source

pub fn new(root_key: &[u8]) -> Self

Construct a new chain with the given root key and all-zero head. Call HashChain::load_head_from_file after construction to resume from an existing chain head persisted in the audit log.

Source

pub fn resume(root_key: &[u8], head: [u8; 32], id: ChainId) -> Self

Resume from a known head hash. Use this when you have the head computed by HashChain::head from a previous session.

Source

pub fn load_head_from_file(&self, path: &Path) -> Result<(), ChainLoadError>

Read the last record_hash (and the matching chain_id) from the audit log and use them as the current state. This lets a fresh process pick up where the last left off without breaking the chain’s identity (both head and id are restored). Tolerates: missing file, empty file, all-blank file. Corrupt JSON or a wrong-length hash in the last line is reported as Err so the caller can refuse to start (a silent reset would let the chain appear to verify while being disconnected from prior records).

Source

pub fn id(&self) -> ChainId

Returns the chain id. If Self::load_head_from_file has been called and the file contained a chain_id, that id is returned; otherwise a fresh id is generated lazily on first call and cached.

Source

pub fn adopt_id(&self, id: ChainId)

Adopt the chain id from an external source (e.g. a sidecar file persisted by crate::decision::DecisionLog). Idempotent: if an id has already been observed (loaded from the audit log or set explicitly), this is a no-op.

Source

pub fn head(&self) -> [u8; 32]

Source

pub fn set_head(&self, head: [u8; 32])

Set the current head. Use this to resume a chain from a previously serialized head hash (e.g. when starting a new process that wants to continue appending to an existing log).

Source

pub fn append(&self, canonical_record: &[u8]) -> ([u8; 32], [u8; 32])

Append a record: returns (prev_hash, new_hash).

The canonical record bytes should be the canonical JSON serialization of the record (see crate::decision::canonical).

Thread-safe: the head read, HMAC computation, and head write happen inside a single critical section. Concurrent appenders serialize.

Note: this advances the in-memory head before the caller has persisted anything. If the caller’s persistence step fails, the chain is left one step ahead of the on-disk file. Callers that need an atomic “compute hash + persist” should use Self::try_append_with_io instead.

Source

pub fn try_append_with_io<F, T>( &self, canonical_record: &[u8], persist: F, ) -> Result<T>
where F: FnOnce([u8; 32], [u8; 32]) -> Result<T>,

Atomic “compute hash + persist” for callers that hold an external lock (typically a file lock). The chain head is only advanced if the closure succeeds — so a failed write leaves the chain unchanged and a retry will produce the same hash.

Lock order: caller must hold the outer (file) lock before entering this method; we then take the chain head lock. The file lock is released by the caller, the chain head lock by this method’s return.

Source

pub fn verify( &self, canonical_record: &[u8], claimed_prev: &[u8; 32], claimed_hash: &[u8; 32], ) -> Result<()>

Verify a single record’s hash matches what the chain would produce.

Source

pub fn verify_chain( &self, records: &[(Vec<u8>, [u8; 32], [u8; 32])], ) -> Result<()>

Verify a full chain given the records. The first record’s prev_hash must equal [0; HASH_LEN]. The last record’s hash must equal the current head.

Trait Implementations§

Source§

impl Clone for HashChain

Source§

fn clone(&self) -> HashChain

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for HashChain

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,