bark/subsystem/
mod.rs

1//! Contains subsystem-related constants and types for Bark.
2//!
3//! In its current form this has little functionality; however, in the future
4//! it will contain interfaces allowing for developers to add their own
5//! subsystems which Bark can use.
6
7use std::fmt;
8
9use bitcoin::consensus::encode::serialize_hex;
10use bitcoin::{Amount, OutPoint, Transaction};
11
12use ark::lightning::PaymentHash;
13use ark::vtxo::VtxoRef;
14
15/// A unique identifier for a subsystem.
16#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, Deserialize, Serialize)]
17pub struct Subsystem(&'static str);
18
19impl Subsystem {
20	pub const fn new(id: &'static str) -> Self {
21		Subsystem(id)
22	}
23
24	pub fn as_name(&self) -> &'static str {
25		self.0
26	}
27
28	/// The built-in arkoor subsystem
29	pub const ARKOOR: Subsystem = Subsystem::new("bark.arkoor");
30
31	/// The built-in board subsystem
32	pub const BOARD: Subsystem = Subsystem::new("bark.board");
33
34	/// The built-in offboard subsystem
35	pub const OFFBOARD: Subsystem = Subsystem::new("bark.offboard");
36
37	/// The built-in exit subsystem
38	pub const EXIT: Subsystem = Subsystem::new("bark.exit");
39
40	/// The built-in Lightning receive subsystem
41	pub const LIGHTNING_RECEIVE: Subsystem = Subsystem::new("bark.lightning_receive");
42
43	/// The built-in Lightning send subsystem
44	pub const LIGHTNING_SEND: Subsystem = Subsystem::new("bark.lightning_send");
45
46	/// The built-in round subsystem
47	pub const ROUND: Subsystem = Subsystem::new("bark.round");
48}
49
50impl fmt::Display for Subsystem {
51	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
52		f.write_str(self.0)
53	}
54}
55
56#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
57pub enum RoundMovement {
58	Refresh,
59	SendOnchain,
60}
61
62impl fmt::Display for RoundMovement {
63	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
64		match self {
65			RoundMovement::Refresh => f.write_str("refresh"),
66			RoundMovement::SendOnchain => f.write_str("send_onchain"),
67		}
68	}
69}
70
71#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
72pub(crate) enum ArkoorMovement {
73	Receive,
74	Send,
75}
76
77impl fmt::Display for ArkoorMovement {
78	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
79		match self {
80			ArkoorMovement::Receive => f.write_str("receive"),
81			ArkoorMovement::Send => f.write_str("send"),
82		}
83	}
84}
85
86#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
87pub(crate) enum BoardMovement {
88	Board,
89}
90
91impl BoardMovement {
92	pub fn metadata(
93		outpoint: OutPoint,
94		onchain_fee: Amount,
95	) -> impl IntoIterator<Item = (String, serde_json::Value)> {
96		[
97			(
98				"chain_anchor".into(),
99				serde_json::to_value(outpoint).expect("outpoint can serde"),
100			),
101			(
102				"onchain_fee_sat".into(),
103				serde_json::to_value(onchain_fee.to_sat()).expect("int can serde"),
104			),
105		]
106	}
107}
108
109impl fmt::Display for BoardMovement {
110	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
111		match self {
112			BoardMovement::Board => f.write_str("board"),
113		}
114	}
115}
116
117#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
118pub(crate) enum OffboardMovement {
119	Offboard,
120}
121
122impl OffboardMovement {
123	pub fn metadata(
124		offboard_tx: &Transaction,
125	) -> impl IntoIterator<Item = (String, serde_json::Value)> {
126		[
127			(
128				"offboard_txid".into(),
129				serde_json::to_value(offboard_tx.compute_txid()).expect("txid can serde"),
130			),
131			(
132				"offboard_tx".into(),
133				serde_json::to_value(serialize_hex(&offboard_tx)).expect("string can serde"),
134			),
135		]
136	}
137}
138
139impl fmt::Display for OffboardMovement {
140	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
141		match self {
142			OffboardMovement::Offboard => f.write_str("offboard"),
143		}
144	}
145}
146
147#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
148pub(crate) enum ExitMovement {
149	Exit,
150}
151
152impl fmt::Display for ExitMovement {
153	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
154		match self {
155			ExitMovement::Exit => f.write_str("start"),
156		}
157	}
158}
159
160/// Provides helper methods for lightning-related movements.
161pub(crate) struct LightningMovement {}
162
163impl LightningMovement {
164	pub fn metadata(
165		payment_hash: PaymentHash,
166		htlcs: impl IntoIterator<Item = impl VtxoRef>,
167	) -> impl IntoIterator<Item = (String, serde_json::Value)> {
168		let htlcs = htlcs.into_iter().map(|v| v.vtxo_id()).collect::<Vec<_>>();
169		[
170			(
171				"payment_hash".into(),
172				serde_json::to_value(payment_hash).expect("payment hash can serde"),
173			),
174			(
175				"htlc_vtxos".into(),
176				serde_json::to_value(&htlcs).expect("vtxo ids can serde"),
177			),
178		]
179	}
180}
181
182#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
183pub(crate) enum LightningReceiveMovement {
184	Receive,
185}
186
187impl fmt::Display for LightningReceiveMovement {
188	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
189		match self {
190			LightningReceiveMovement::Receive => f.write_str("receive"),
191		}
192	}
193}
194
195#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
196pub(crate) enum LightningSendMovement {
197	Send,
198}
199
200impl fmt::Display for LightningSendMovement {
201	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
202		match self {
203			LightningSendMovement::Send => f.write_str("send"),
204		}
205	}
206}