Skip to main content

bark_json/exit/
error.rs

1use bitcoin::{Amount, Txid};
2use thiserror::Error;
3
4use ark::VtxoId;
5use bitcoin_ext::BlockHeight;
6#[cfg(feature = "utoipa")]
7use utoipa::ToSchema;
8
9use crate::exit::ExitStateKind;
10use crate::exit::states::ExitTxStatus;
11
12#[derive(Clone, Debug, Error, PartialEq, Eq, Deserialize, Serialize)]
13#[cfg_attr(feature = "utoipa", derive(ToSchema))]
14#[serde(tag = "type", rename_all = "kebab-case")]
15pub enum ExitError {
16	#[error("Transaction Retrieval Failure: Unable to retrieve ancestral data for TX {txid}: {error}")]
17	AncestorRetrievalFailure {
18		#[cfg_attr(feature = "utoipa", schema(value_type = String))]
19		txid: Txid,
20		error: String
21	},
22
23	#[error("Block Retrieval Failure: Unable to retrieve a block at height {height}: {error}")]
24	BlockRetrievalFailure { height: BlockHeight, error: String },
25
26	#[error("Cannot Cancel Exit: The exit for VTXO {vtxo} can no longer be canceled (state: {state})")]
27	CannotCancelExit {
28		#[cfg_attr(feature = "utoipa", schema(value_type = String))]
29		vtxo: VtxoId,
30		state: ExitStateKind,
31	},
32
33	#[error("Claim Missing Inputs: No inputs given to claim")]
34	ClaimMissingInputs,
35
36	#[error("Claim Fee Exceeds Output: Cost to claim exits was {needed}, but the total output was {output}")]
37	ClaimFeeExceedsOutput {
38		#[cfg_attr(feature = "utoipa", schema(value_type = u64))]
39		needed: Amount,
40		#[cfg_attr(feature = "utoipa", schema(value_type = u64))]
41		output: Amount,
42	},
43
44	#[error("Claim Missing Signable Clause: Couldn't find a signable clause for VTXO {vtxo}")]
45	ClaimMissingSignableClause {
46		#[cfg_attr(feature = "utoipa", schema(value_type = String))]
47		vtxo: VtxoId,
48	},
49
50	#[error("Claim Signing Error: Unable to sign claim: {error}")]
51	ClaimSigningError { error: String },
52
53	#[error("Cyclic Exit Transactions Error: The exit transactions for VTXO {vtxo} are cyclic")]
54	CyclicExitTransactions {
55		#[cfg_attr(feature = "utoipa", schema(value_type = String))]
56		vtxo: VtxoId
57	},
58
59	#[error("Database Store Failure: Unable to update exit VTXO {vtxo_id} in the database: {error}")]
60	DatabaseVtxoStoreFailure {
61		#[cfg_attr(feature = "utoipa", schema(value_type = String))]
62		vtxo_id: VtxoId,
63		error: String
64	},
65
66	#[error("Database Retrieval Failure: Unable to get child tx: {error}")]
67	DatabaseChildRetrievalFailure { error: String },
68
69	#[error("Database Store Failure: Unable to store child tx: {error}")]
70	DatabaseChildStoreFailure { error: String },
71
72	#[error("Dust Limit Error: The dust limit for a VTXO is {dust} but the balance is only {vtxo}")]
73	DustLimit {
74		#[cfg_attr(feature = "utoipa", schema(value_type = u64))]
75		vtxo: Amount,
76		#[cfg_attr(feature = "utoipa", schema(value_type = u64))]
77		dust: Amount
78	},
79
80	#[error("Exit Package Broadcast Failure: Unable to broadcast exit transaction package {txid}: {error}")]
81	ExitPackageBroadcastFailure {
82		#[cfg_attr(feature = "utoipa", schema(value_type = String))]
83		txid: Txid,
84		error: String
85	},
86
87	#[error("Exit Package Finalize Failure: Unable to create exit transaction package: {error}")]
88	ExitPackageFinalizeFailure { error: String },
89
90	#[error("Exit Package Store Failure: Unable to store exit transaction package {txid}: {error}")]
91	ExitPackageStoreFailure {
92		#[cfg_attr(feature = "utoipa", schema(value_type = String))]
93		txid: Txid,
94		error: String
95	},
96
97	#[error("Exit Tx Already Broadcast: Cannot cancel the exit for VTXO {vtxo}, its final exit tx {txid} has already been broadcast")]
98	ExitTxAlreadyBroadcast {
99		#[cfg_attr(feature = "utoipa", schema(value_type = String))]
100		vtxo: VtxoId,
101		#[cfg_attr(feature = "utoipa", schema(value_type = String))]
102		txid: Txid,
103	},
104
105	#[error("Insufficient Confirmed Funds: {needed} is needed but only {available} is available")]
106	InsufficientConfirmedFunds {
107		#[cfg_attr(feature = "utoipa", schema(value_type = u64))]
108		needed: Amount,
109		#[cfg_attr(feature = "utoipa", schema(value_type = u64))]
110		available: Amount
111	},
112
113	#[error("Internal Error: An unexpected problem occurred, {error}")]
114	InternalError { error: String },
115
116	#[error("Invalid Exit Transaction Status: Exit tx {txid} has an invalid status ({status}): {error}")]
117	InvalidExitTransactionStatus {
118		#[cfg_attr(feature = "utoipa", schema(value_type = String))]
119		txid: Txid,
120		status: ExitTxStatus,
121		error: String
122	},
123
124	#[error("Invalid LockTime ({tip}): {error}")]
125	InvalidLocktime { tip: BlockHeight, error: String },
126
127	#[error("Invalid Wallet State: {error}")]
128	InvalidWalletState { error: String },
129
130	#[error("Missing Anchor Output: Malformed exit tx {txid}")]
131	MissingAnchorOutput { #[cfg_attr(feature = "utoipa", schema(value_type = String))] txid: Txid },
132
133	#[error("Missing VTXO Transaction: Couldn't find exit tx {txid}")]
134	MissingExitTransaction { #[cfg_attr(feature = "utoipa", schema(value_type = String))] txid: Txid },
135
136	#[error("Non-Standard VTXO {vtxo}: exit chain is not relayable: {error}")]
137	NonStandardVtxo {
138		#[cfg_attr(feature = "utoipa", schema(value_type = String))]
139		vtxo: VtxoId,
140		error: String,
141	},
142
143	#[error("Movement Registration Failure: {error}")]
144	MovementRegistrationFailure { error: String },
145
146	#[error("Not Exiting: VTXO {vtxo} has no unilateral exit")]
147	NotExiting { #[cfg_attr(feature = "utoipa", schema(value_type = String))] vtxo: VtxoId },
148
149	#[error("Tip Retrieval Failure: Unable to retrieve the blockchain tip height: {error}")]
150	TipRetrievalFailure { error: String },
151
152	#[error("Transaction Retrieval Failure: Unable to check the status of TX {txid}: {error}")]
153	TransactionRetrievalFailure { #[cfg_attr(feature = "utoipa", schema(value_type = String))] txid: Txid, error: String },
154
155	#[error("VTXO Not Spendable Error: Attempted to claim a VTXO which is not in a spendable state: {vtxo}")]
156	VtxoNotClaimable { #[cfg_attr(feature = "utoipa", schema(value_type = String))] vtxo: VtxoId },
157
158	#[error("VTXO ScriptPubKey Invalid: {error}")]
159	VtxoScriptPubKeyInvalid { error: String },
160}
161
162impl From<bark::exit::ExitError> for ExitError {
163	fn from(v: bark::exit::ExitError) -> Self {
164		match v {
165			bark::exit::ExitError::AncestorRetrievalFailure { txid, error } => {
166				ExitError::AncestorRetrievalFailure { txid, error }
167			},
168			bark::exit::ExitError::BlockRetrievalFailure { height, error } => {
169				ExitError::BlockRetrievalFailure { height, error }
170			},
171			bark::exit::ExitError::ClaimMissingInputs => {
172				ExitError::ClaimMissingInputs
173			},
174			bark::exit::ExitError::ClaimFeeExceedsOutput { needed, output } => {
175				ExitError::ClaimFeeExceedsOutput { needed, output }
176			},
177			bark::exit::ExitError::ClaimMissingSignableClause { vtxo } => {
178				ExitError::ClaimMissingSignableClause { vtxo }
179			},
180			bark::exit::ExitError::ClaimSigningError { error } => {
181				ExitError::ClaimSigningError { error }
182			},
183			bark::exit::ExitError::CyclicExitTransactions { vtxo } => {
184				ExitError::CyclicExitTransactions { vtxo }
185			},
186			bark::exit::ExitError::DatabaseVtxoStoreFailure { vtxo_id, error } => {
187				ExitError::DatabaseVtxoStoreFailure { vtxo_id, error }
188			},
189			bark::exit::ExitError::DatabaseChildRetrievalFailure { error } => {
190				ExitError::DatabaseChildRetrievalFailure { error }
191			},
192			bark::exit::ExitError::DatabaseChildStoreFailure { error } => {
193				ExitError::DatabaseChildStoreFailure { error }
194			},
195			bark::exit::ExitError::DustLimit { vtxo, dust } => {
196				ExitError::DustLimit { vtxo, dust }
197			},
198			bark::exit::ExitError::ExitPackageBroadcastFailure { txid, error } => {
199				ExitError::ExitPackageBroadcastFailure { txid, error: error.to_string() }
200			},
201			bark::exit::ExitError::ExitPackageFinalizeFailure { error } => {
202				ExitError::ExitPackageFinalizeFailure { error }
203			},
204			bark::exit::ExitError::ExitPackageStoreFailure { txid, error } => {
205				ExitError::ExitPackageStoreFailure { txid, error }
206			},
207			bark::exit::ExitError::ExitTxAlreadyBroadcast { vtxo, txid } => {
208				ExitError::ExitTxAlreadyBroadcast { vtxo, txid }
209			},
210			bark::exit::ExitError::InsufficientConfirmedFunds { needed, available } => {
211				ExitError::InsufficientConfirmedFunds { needed, available }
212			},
213			bark::exit::ExitError::InternalError { error } => {
214				ExitError::InternalError { error }
215			},
216			bark::exit::ExitError::InvalidExitTransactionStatus { txid, status, error } => {
217				ExitError::InvalidExitTransactionStatus { txid, status: status.into(), error }
218			},
219			bark::exit::ExitError::InvalidLocktime { tip, error } => {
220				ExitError::InvalidLocktime { tip, error }
221			},
222			bark::exit::ExitError::InvalidWalletState { error } => {
223				ExitError::InvalidWalletState { error }
224			},
225			bark::exit::ExitError::MissingAnchorOutput { txid } => {
226				ExitError::MissingAnchorOutput { txid }
227			},
228			bark::exit::ExitError::MissingExitTransaction { txid } => {
229				ExitError::MissingExitTransaction { txid }
230			},
231			bark::exit::ExitError::NonStandardVtxo { vtxo, error } => {
232				ExitError::NonStandardVtxo { vtxo, error: error.to_string() }
233			},
234			bark::exit::ExitError::MovementRegistrationFailure { error } => {
235				ExitError::MovementRegistrationFailure { error }
236			},
237			bark::exit::ExitError::CannotCancelExit { vtxo, state } => {
238				ExitError::CannotCancelExit { vtxo, state: state.into() }
239			},
240			bark::exit::ExitError::NotExiting { vtxo } => {
241				ExitError::NotExiting { vtxo }
242			},
243			bark::exit::ExitError::TipRetrievalFailure { error } => {
244				ExitError::TipRetrievalFailure { error }
245			},
246			bark::exit::ExitError::TransactionRetrievalFailure { txid, error } => {
247				ExitError::TransactionRetrievalFailure { txid, error }
248			},
249			bark::exit::ExitError::VtxoNotClaimable { vtxo } => {
250				ExitError::VtxoNotClaimable { vtxo }
251			},
252			bark::exit::ExitError::VtxoScriptPubKeyInvalid { error } => {
253				ExitError::VtxoScriptPubKeyInvalid { error }
254			},
255		}
256	}
257}
258
259#[cfg(test)]
260mod test {
261	use super::*;
262	#[test]
263	fn json_roundtrip() {
264		let err = ExitError::InvalidWalletState { error: "none shall pass".into() };
265		let json = serde_json::to_string(&err).unwrap();
266		let err2 = serde_json::from_str::<ExitError>(&json).unwrap();
267		assert_eq!(err, err2);
268	}
269}