Skip to main content

Module transaction

Module transaction 

Source
Expand description

Enhanced transaction API with preflight, state updates, and event logging

Provides a unified interface for the three-phase transaction pattern:

  1. Preflight: Validate constraints (key exists, value >= X, etc.)
  2. Update: Modify state (KV operations)
  3. Log: Append events

§Example

use azoth::prelude::*;
use azoth::{Transaction, TypedValue};

let db = AzothDb::open("./data")?;

// Build and execute a transaction with lambda-based validation
Transaction::new(&db)
    // Phase 1: Preflight validation with arbitrary logic
    .require(b"balance".to_vec(), |value| {
        let typed_value = value.ok_or(AzothError::PreflightFailed("Balance must exist".into()))?;
        let balance = typed_value.as_i64()?;
        if balance < 50 {
            return Err(AzothError::PreflightFailed("Insufficient balance".into()));
        }
        Ok(())
    })
    // Phase 2 & 3: State updates and event logging
    .execute(|ctx| {
        let balance = ctx.get(b"balance")?.as_i64()?;
        ctx.set(b"balance", &TypedValue::I64(balance - 50))?;
        ctx.log_bytes(b"withdraw:50")?;
        ctx.log_bytes(b"fee:1")?;
        Ok(())
    })?;

Structs§

PreflightContext
Preflight context for validation
Transaction
Transaction builder
TransactionContext
Transaction context for state updates and event logging