Skip to content

Commit e6433fb

Browse files
committed
feat(persist): Add stage_and_commit to Persist
In the example_cli we were not always committing (seemingly by mistake). This then caused all the examples to have to compensate by manually committing.
1 parent 0bee46e commit e6433fb

File tree

5 files changed

+21
-19
lines changed

5 files changed

+21
-19
lines changed

crates/chain/src/persist.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,18 @@ where
5555
// if written successfully, take and return `self.stage`
5656
.map(|_| Some(core::mem::take(&mut self.stage)))
5757
}
58+
59+
/// Stages a new changeset and commits it (along with any other previously staged changes) to
60+
/// the persistence backend
61+
///
62+
/// Convience method for calling [`stage`] and then [`commit`].
63+
///
64+
/// [`stage`]: Self::stage
65+
/// [`commit`]: Self::commit
66+
pub fn stage_and_commit(&mut self, changeset: C) -> Result<Option<C>, B::WriteError> {
67+
self.stage(changeset);
68+
self.commit()
69+
}
5870
}
5971

6072
/// A persistence backend for [`Persist`].

example-crates/example_bitcoind_rpc_polling/src/main.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ fn main() -> anyhow::Result<()> {
147147
let rpc_cmd = match args.command {
148148
example_cli::Commands::ChainSpecific(rpc_cmd) => rpc_cmd,
149149
general_cmd => {
150-
let res = example_cli::handle_commands(
150+
return example_cli::handle_commands(
151151
&graph,
152152
&db,
153153
&chain,
@@ -160,8 +160,6 @@ fn main() -> anyhow::Result<()> {
160160
},
161161
general_cmd,
162162
);
163-
db.lock().unwrap().commit()?;
164-
return res;
165163
}
166164
};
167165

example-crates/example_cli/src/lib.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -457,11 +457,10 @@ where
457457

458458
let ((spk_i, spk), index_changeset) = spk_chooser(index, &Keychain::External);
459459
let db = &mut *db.lock().unwrap();
460-
db.stage(C::from((
460+
db.stage_and_commit(C::from((
461461
local_chain::ChangeSet::default(),
462462
indexed_tx_graph::ChangeSet::from(index_changeset),
463-
)));
464-
db.commit()?;
463+
)))?;
465464
let addr =
466465
Address::from_script(spk, network).context("failed to derive address")?;
467466
println!("[address @ {}] {}", spk_i, addr);
@@ -601,11 +600,10 @@ where
601600
// If we're unable to persist this, then we don't want to broadcast.
602601
{
603602
let db = &mut *db.lock().unwrap();
604-
db.stage(C::from((
603+
db.stage_and_commit(C::from((
605604
local_chain::ChangeSet::default(),
606605
indexed_tx_graph::ChangeSet::from(index_changeset),
607-
)));
608-
db.commit()?;
606+
)))?;
609607
}
610608

611609
// We don't want other callers/threads to use this address while we're using it
@@ -627,10 +625,10 @@ where
627625
// We know the tx is at least unconfirmed now. Note if persisting here fails,
628626
// it's not a big deal since we can always find it again form
629627
// blockchain.
630-
db.lock().unwrap().stage(C::from((
628+
db.lock().unwrap().stage_and_commit(C::from((
631629
local_chain::ChangeSet::default(),
632630
keychain_changeset,
633-
)));
631+
)))?;
634632
Ok(())
635633
}
636634
Err(e) => {

example-crates/example_electrum/src/main.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ fn main() -> anyhow::Result<()> {
122122
let electrum_cmd = match &args.command {
123123
example_cli::Commands::ChainSpecific(electrum_cmd) => electrum_cmd,
124124
general_cmd => {
125-
let res = example_cli::handle_commands(
125+
return example_cli::handle_commands(
126126
&graph,
127127
&db,
128128
&chain,
@@ -135,9 +135,6 @@ fn main() -> anyhow::Result<()> {
135135
},
136136
general_cmd.clone(),
137137
);
138-
139-
db.lock().unwrap().commit()?;
140-
return res;
141138
}
142139
};
143140

example-crates/example_esplora/src/main.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ fn main() -> anyhow::Result<()> {
125125
example_cli::Commands::ChainSpecific(esplora_cmd) => esplora_cmd,
126126
// These are general commands handled by example_cli. Execute the cmd and return.
127127
general_cmd => {
128-
let res = example_cli::handle_commands(
128+
return example_cli::handle_commands(
129129
&graph,
130130
&db,
131131
&chain,
@@ -140,9 +140,6 @@ fn main() -> anyhow::Result<()> {
140140
},
141141
general_cmd.clone(),
142142
);
143-
144-
db.lock().unwrap().commit()?;
145-
return res;
146143
}
147144
};
148145

0 commit comments

Comments
 (0)