Skip to main content

PolicyEngine

Trait PolicyEngine 

Source
pub trait PolicyEngine: Send + Sync {
    // Required methods
    fn check(&self, tx: &ParsedTx) -> Result<PolicyResult, PolicyError>;
    fn record(&self, tx: &ParsedTx) -> Result<(), PolicyError>;
}
Expand description

Trait for policy engines that enforce transaction rules.

Implementors of this trait can check transactions against policy rules and record signed transactions for tracking purposes (e.g., daily limits).

§Thread Safety

All implementations must be Send + Sync to allow concurrent access from multiple request handlers.

§Example

use txgate_policy::engine::PolicyEngine;
use txgate_core::types::{ParsedTx, PolicyResult};
use txgate_core::error::PolicyError;

fn process_transaction(engine: &dyn PolicyEngine, tx: &ParsedTx) -> Result<(), PolicyError> {
    let result = engine.check(tx)?;
    if result.is_allowed() {
        // Transaction approved, record it
        engine.record(tx)?;
    }
    Ok(())
}

Required Methods§

Source

fn check(&self, tx: &ParsedTx) -> Result<PolicyResult, PolicyError>

Check if a transaction is allowed by policy rules.

Evaluates the transaction against all configured policy rules in order of priority (blacklist > whitelist > tx_limit > daily_limit).

§Arguments
  • tx - The parsed transaction to check
§Returns
  • Ok(PolicyResult::Allowed) - Transaction passes all policy checks
  • Ok(PolicyResult::Denied { rule, reason }) - Transaction denied by a rule
  • Err(PolicyError) - Policy evaluation failed (e.g., database error)
§Errors

Returns PolicyError if policy evaluation fails due to:

  • Database errors when checking daily limits
  • Invalid policy configuration
Source

fn record(&self, tx: &ParsedTx) -> Result<(), PolicyError>

Record a transaction that was signed (for limit tracking).

This should be called after a transaction is successfully signed to update the daily spending totals.

§Arguments
  • tx - The signed transaction to record
§Errors

Returns PolicyError if recording fails due to:

  • Database errors
  • Internal errors

Implementors§