Skip to content

Commit 24cdf0a

Browse files
committed
chore(persister): log more errors
1 parent 6aff9f6 commit 24cdf0a

File tree

1 file changed

+98
-35
lines changed

1 file changed

+98
-35
lines changed

crates/duty-tracker/src/contract_persister.rs

Lines changed: 98 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use sqlx::{
1010
use strata_bridge_tx_graph::transactions::prelude::CovenantTx;
1111
use strata_primitives::params::RollupParams;
1212
use thiserror::Error;
13+
use tracing::{debug, error};
1314

1415
use crate::contract_state_machine::{ContractCfg, ContractSM, MachineState};
1516

@@ -57,7 +58,11 @@ impl ContractPersister {
5758
)
5859
.execute(&pool)
5960
.await
60-
.map_err(|e| ContractPersistErr::Unexpected(e.to_string()))?;
61+
.map_err(|e| {
62+
error!(?e, "failed to create contracts table");
63+
64+
ContractPersistErr::Unexpected(e.to_string())
65+
})?;
6166
Ok(ContractPersister { pool })
6267
}
6368

@@ -85,7 +90,10 @@ impl ContractPersister {
8590
.bind(bincode::serialize(&state)?)
8691
.execute(&self.pool)
8792
.await
88-
.map_err(|e| ContractPersistErr::Unexpected(e.to_string()))?;
93+
.map_err(|e| {
94+
error!(?e, "failed to insert contract into database");
95+
ContractPersistErr::Unexpected(e.to_string())
96+
})?;
8997
Ok(())
9098
}
9199

@@ -104,7 +112,10 @@ impl ContractPersister {
104112
.bind(deposit_txid.to_string())
105113
.execute(&self.pool)
106114
.await
107-
.map_err(|e| ContractPersistErr::Unexpected(e.to_string()))?;
115+
.map_err(|e| {
116+
error!(?e, "failed to commit machine state to disk");
117+
ContractPersistErr::Unexpected(e.to_string())
118+
})?;
108119
Ok(())
109120
}
110121

@@ -113,6 +124,8 @@ impl ContractPersister {
113124
&self,
114125
active_contracts: impl Iterator<Item = (&Txid, &ContractSM)>,
115126
) -> Result<(), ContractPersistErr> {
127+
debug!("committing all active contracts");
128+
116129
for (txid, contract_sm) in active_contracts {
117130
let machine_state = contract_sm.state();
118131
// FIXME: (@Rajil1213) wrap all commits into a single db transaction.
@@ -145,22 +158,37 @@ impl ContractPersister {
145158
.bind(deposit_txid.to_string())
146159
.fetch_one(&self.pool)
147160
.await
148-
.map_err(|e| ContractPersistErr::Unexpected(e.to_string()))?;
149-
let deposit_idx = row
150-
.try_get("deposit_idx")
151-
.map_err(|e| ContractPersistErr::Unexpected(e.to_string()))?;
152-
let deposit_tx = bincode::deserialize(
153-
row.try_get("deposit_tx")
154-
.map_err(|e| ContractPersistErr::Unexpected(e.to_string()))?,
155-
)?;
156-
let operator_table = bincode::deserialize(
157-
row.try_get("operator_table")
158-
.map_err(|e| ContractPersistErr::Unexpected(e.to_string()))?,
159-
)?;
160-
let state = bincode::deserialize(
161-
row.try_get("state")
162-
.map_err(|e| ContractPersistErr::Unexpected(e.to_string()))?,
163-
)?;
161+
.map_err(|e| {
162+
error!(?e, %deposit_txid, "could not load contract from disk");
163+
ContractPersistErr::Unexpected(e.to_string())
164+
})?;
165+
166+
let deposit_idx = row.try_get("deposit_idx").map_err(|e| {
167+
error!(?e, %deposit_txid, "could not parse deposit_idx from contract entry in disk");
168+
ContractPersistErr::Unexpected(e.to_string())
169+
})?;
170+
171+
let deposit_tx = bincode::deserialize(row.try_get("deposit_tx").map_err(|e| {
172+
error!(?e, %deposit_txid, "could not parse deposit_tx from contract entry in disk");
173+
ContractPersistErr::Unexpected(e.to_string())
174+
})?).inspect_err(|e| {
175+
error!(?e, %deposit_txid, "could not deserialize deposit_tx from contract entry in disk");
176+
})?;
177+
178+
let operator_table = bincode::deserialize(row.try_get("operator_table").map_err(|e| {
179+
error!(?e, %deposit_txid, "could not parse operator_table from contract entry in disk");
180+
ContractPersistErr::Unexpected(e.to_string())
181+
})?).inspect_err(|e| {
182+
error!(?e, %deposit_txid, "could not deserialize operator_table from contract entry in disk");
183+
})?;
184+
185+
let state = bincode::deserialize(row.try_get("state").map_err(|e| {
186+
error!(?e, %deposit_txid, "could not parse state from contract entry in disk");
187+
ContractPersistErr::Unexpected(e.to_string())
188+
})?)
189+
.inspect_err(|e| {
190+
error!(?e, %deposit_txid, "could not deserialize state from contract entry in disk");
191+
})?;
164192

165193
Ok((
166194
ContractCfg {
@@ -199,24 +227,59 @@ impl ContractPersister {
199227
)
200228
.fetch_all(&self.pool)
201229
.await
202-
.map_err(|e| ContractPersistErr::Unexpected(e.to_string()))?;
230+
.map_err(|e| {
231+
error!(?e, "could not load all contracts from disk");
232+
233+
ContractPersistErr::Unexpected(e.to_string())
234+
})?;
235+
203236
rows.into_iter()
204237
.map(|row| {
205-
let deposit_idx = row
206-
.try_get("deposit_idx")
207-
.map_err(|e| ContractPersistErr::Unexpected(e.to_string()))?;
208-
let deposit_tx = bincode::deserialize(
209-
row.try_get("deposit_tx")
210-
.map_err(|e| ContractPersistErr::Unexpected(e.to_string()))?,
211-
)?;
212-
let operator_table = bincode::deserialize(
213-
row.try_get("operator_table")
214-
.map_err(|e| ContractPersistErr::Unexpected(e.to_string()))?,
215-
)?;
216-
let state = bincode::deserialize(
217-
row.try_get("state")
218-
.map_err(|e| ContractPersistErr::Unexpected(e.to_string()))?,
219-
)?;
238+
let deposit_idx = row.try_get("deposit_idx").map_err(|e| {
239+
error!(
240+
?e,
241+
"could not parse deposit_idx from contract entry in disk"
242+
);
243+
ContractPersistErr::Unexpected(e.to_string())
244+
})?;
245+
246+
let deposit_tx = bincode::deserialize(row.try_get("deposit_tx").map_err(|e| {
247+
error!(?e, "could not parse deposit_tx from contract entry in disk");
248+
ContractPersistErr::Unexpected(e.to_string())
249+
})?)
250+
.inspect_err(|e| {
251+
error!(
252+
?e,
253+
"could not deserialize deposit_tx from contract entry in disk"
254+
);
255+
})?;
256+
257+
let operator_table =
258+
bincode::deserialize(row.try_get("operator_table").map_err(|e| {
259+
error!(
260+
?e,
261+
"could not parse operator_table from contract entry in disk"
262+
);
263+
ContractPersistErr::Unexpected(e.to_string())
264+
})?)
265+
.inspect_err(|e| {
266+
error!(
267+
?e,
268+
"could not deserialize operator_table from contract entry in disk"
269+
);
270+
})?;
271+
272+
let state = bincode::deserialize(row.try_get("state").map_err(|e| {
273+
error!(?e, "could not parse state from contract entry in disk");
274+
ContractPersistErr::Unexpected(e.to_string())
275+
})?)
276+
.inspect_err(|e| {
277+
error!(
278+
?e,
279+
"could not deserialize state from contract entry in disk"
280+
);
281+
})?;
282+
220283
Ok((
221284
ContractCfg {
222285
network,

0 commit comments

Comments
 (0)