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.
This commit is contained in:
LLFourn 2024-01-19 11:23:46 +11:00
parent 0bee46e75b
commit e6433fb2c1
No known key found for this signature in database
GPG Key ID: A27093B54DA11F65
5 changed files with 21 additions and 19 deletions

View File

@ -55,6 +55,18 @@ where
// if written successfully, take and return `self.stage` // if written successfully, take and return `self.stage`
.map(|_| Some(core::mem::take(&mut self.stage))) .map(|_| Some(core::mem::take(&mut self.stage)))
} }
/// Stages a new changeset and commits it (along with any other previously staged changes) to
/// the persistence backend
///
/// Convience method for calling [`stage`] and then [`commit`].
///
/// [`stage`]: Self::stage
/// [`commit`]: Self::commit
pub fn stage_and_commit(&mut self, changeset: C) -> Result<Option<C>, B::WriteError> {
self.stage(changeset);
self.commit()
}
} }
/// A persistence backend for [`Persist`]. /// A persistence backend for [`Persist`].

View File

@ -147,7 +147,7 @@ fn main() -> anyhow::Result<()> {
let rpc_cmd = match args.command { let rpc_cmd = match args.command {
example_cli::Commands::ChainSpecific(rpc_cmd) => rpc_cmd, example_cli::Commands::ChainSpecific(rpc_cmd) => rpc_cmd,
general_cmd => { general_cmd => {
let res = example_cli::handle_commands( return example_cli::handle_commands(
&graph, &graph,
&db, &db,
&chain, &chain,
@ -160,8 +160,6 @@ fn main() -> anyhow::Result<()> {
}, },
general_cmd, general_cmd,
); );
db.lock().unwrap().commit()?;
return res;
} }
}; };

View File

@ -457,11 +457,10 @@ where
let ((spk_i, spk), index_changeset) = spk_chooser(index, &Keychain::External); let ((spk_i, spk), index_changeset) = spk_chooser(index, &Keychain::External);
let db = &mut *db.lock().unwrap(); let db = &mut *db.lock().unwrap();
db.stage(C::from(( db.stage_and_commit(C::from((
local_chain::ChangeSet::default(), local_chain::ChangeSet::default(),
indexed_tx_graph::ChangeSet::from(index_changeset), indexed_tx_graph::ChangeSet::from(index_changeset),
))); )))?;
db.commit()?;
let addr = let addr =
Address::from_script(spk, network).context("failed to derive address")?; Address::from_script(spk, network).context("failed to derive address")?;
println!("[address @ {}] {}", spk_i, addr); println!("[address @ {}] {}", spk_i, addr);
@ -601,11 +600,10 @@ where
// If we're unable to persist this, then we don't want to broadcast. // If we're unable to persist this, then we don't want to broadcast.
{ {
let db = &mut *db.lock().unwrap(); let db = &mut *db.lock().unwrap();
db.stage(C::from(( db.stage_and_commit(C::from((
local_chain::ChangeSet::default(), local_chain::ChangeSet::default(),
indexed_tx_graph::ChangeSet::from(index_changeset), indexed_tx_graph::ChangeSet::from(index_changeset),
))); )))?;
db.commit()?;
} }
// We don't want other callers/threads to use this address while we're using it // We don't want other callers/threads to use this address while we're using it
@ -627,10 +625,10 @@ where
// We know the tx is at least unconfirmed now. Note if persisting here fails, // We know the tx is at least unconfirmed now. Note if persisting here fails,
// it's not a big deal since we can always find it again form // it's not a big deal since we can always find it again form
// blockchain. // blockchain.
db.lock().unwrap().stage(C::from(( db.lock().unwrap().stage_and_commit(C::from((
local_chain::ChangeSet::default(), local_chain::ChangeSet::default(),
keychain_changeset, keychain_changeset,
))); )))?;
Ok(()) Ok(())
} }
Err(e) => { Err(e) => {

View File

@ -122,7 +122,7 @@ fn main() -> anyhow::Result<()> {
let electrum_cmd = match &args.command { let electrum_cmd = match &args.command {
example_cli::Commands::ChainSpecific(electrum_cmd) => electrum_cmd, example_cli::Commands::ChainSpecific(electrum_cmd) => electrum_cmd,
general_cmd => { general_cmd => {
let res = example_cli::handle_commands( return example_cli::handle_commands(
&graph, &graph,
&db, &db,
&chain, &chain,
@ -135,9 +135,6 @@ fn main() -> anyhow::Result<()> {
}, },
general_cmd.clone(), general_cmd.clone(),
); );
db.lock().unwrap().commit()?;
return res;
} }
}; };

View File

@ -125,7 +125,7 @@ fn main() -> anyhow::Result<()> {
example_cli::Commands::ChainSpecific(esplora_cmd) => esplora_cmd, example_cli::Commands::ChainSpecific(esplora_cmd) => esplora_cmd,
// These are general commands handled by example_cli. Execute the cmd and return. // These are general commands handled by example_cli. Execute the cmd and return.
general_cmd => { general_cmd => {
let res = example_cli::handle_commands( return example_cli::handle_commands(
&graph, &graph,
&db, &db,
&chain, &chain,
@ -140,9 +140,6 @@ fn main() -> anyhow::Result<()> {
}, },
general_cmd.clone(), general_cmd.clone(),
); );
db.lock().unwrap().commit()?;
return res;
} }
}; };