Skip to main content

co_api/types/
reducer.rs

1// SPDX-License-Identifier: AGPL-3.0-only
2// Copyright (C) 2026 1io BRANDGUARDIAN GmbH
3
4use crate::CoreBlockStorage;
5use cid::Cid;
6use co_primitives::{Link, OptionLink, ReducerAction};
7
8/// COre execution context.
9pub trait Context {
10	/// Storage instance.
11	fn storage(&self) -> &CoreBlockStorage;
12
13	/// Get runtime payload.
14	fn payload(&self) -> Vec<u8>;
15
16	/// Get action to apply to the state.
17	fn event(&self) -> Cid;
18
19	/// Get current COre state.
20	/// Returns [`None`] if no prior state.
21	fn state(&self) -> Option<Cid>;
22
23	/// Set next COre state.
24	fn set_state(&mut self, cid: Cid);
25
26	/// Write diagnostic block.
27	fn write_diagnostic(&mut self, cid: Cid);
28}
29
30#[allow(async_fn_in_trait)]
31pub trait Reducer<A>
32where
33	Self: Sized,
34	A: Clone,
35{
36	async fn reduce(
37		state: OptionLink<Self>,
38		event: Link<ReducerAction<A>>,
39		storage: &CoreBlockStorage,
40	) -> Result<Link<Self>, anyhow::Error>;
41}