Skip to main content

Algorithm

Trait Algorithm 

Source
pub trait Algorithm: Send + Sync {
    type GraphType: Send + Sync;
    type GraphManager: GraphManager<Self::GraphType> + Default;

    // Required methods
    fn name(&self) -> &str;
    async fn find_best_route(
        &self,
        request: SolveRequest<'_, Self::GraphType>,
    ) -> Result<RouteResult, AlgorithmError>;
    fn computation_requirements(&self) -> ComputationRequirements;
    fn timeout(&self) -> Duration;
}
Expand description

Trait for route-finding algorithms.

Algorithms are generic over their preferred graph type G, allowing them to:

  • Use different graph crates (petgraph, custom, etc.)
  • Leverage built-in algorithms from graph libraries
  • Optimize their graph representation for their specific needs

§Implementation Notes

  • Algorithms should respect the timeout from timeout()
  • They should use graph for path finding (BFS/etc)
  • They should use market to read component states for simulation
  • They should NOT modify the graph or market data

Required Associated Types§

Source

type GraphType: Send + Sync

The graph type this algorithm uses.

Source

type GraphManager: GraphManager<Self::GraphType> + Default

The graph manager type for this algorithm. This allows the solver to automatically create the appropriate graph manager.

Required Methods§

Source

fn name(&self) -> &str

Returns the algorithm’s name.

Source

async fn find_best_route( &self, request: SolveRequest<'_, Self::GraphType>, ) -> Result<RouteResult, AlgorithmError>

Finds the best route for the order the request carries.

SolveRequest holds the graph, the market, the order, the overlay to read state through, the derived data, and what the caller excludes from a route. Read these through the getters or move them out with SolveRequest::into_parts.

Honour SolveRequest::exclusions during search and simulation. The worker rejects returned routes that violate the request filter.

§Returns

The best route and its gas-adjusted net output amount, or an error if no route could be found.

Source

fn computation_requirements(&self) -> ComputationRequirements

Returns the derived data computation requirements for this algorithm.

Algorithms declare freshness requirements for derived data:

  • require_fresh: Data must be from the current block (same as MarketState)
  • allow_stale: Data can be from any past block, as long as it exists

Workers use this to determine when they can safely solve.

Default implementation returns no requirements - algorithm works without any derived data.

Source

fn timeout(&self) -> Duration

Returns the timeout for solving.

Workers use this to set the maximum time to wait for derived data before failing a solve request.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§