Skip to content

Commit

Permalink
add reindex_chainstate action (#147)
Browse files Browse the repository at this point in the history
* add reindex_chainstate action

* fix typo

* fix action id build error

* Copy reindex_chainstate.sh into container

* Commas, phrasing

* typo correction

---------

Co-authored-by: gStart9 <[email protected]>
  • Loading branch information
Dominion5254 and gStart9 authored May 29, 2024
1 parent b869f08 commit 509274a
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ COPY --from=bitcoin-core /opt /opt
COPY ./manager/target/${ARCH}-unknown-linux-musl/release/bitcoind-manager \
./docker_entrypoint.sh \
./actions/reindex.sh \
./actions/reindex_chainstate.sh \
./check-rpc.sh \
./check-synced.sh \
/usr/local/bin/
Expand Down
20 changes: 20 additions & 0 deletions actions/reindex_chainstate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/sh

set -e

touch /root/.bitcoin/requires.reindex_chainstate
action_result_running=" {
\"version\": \"0\",
\"message\": \"Bitcoin Core restarting in reindex chainstate mode\",
\"value\": null,
\"copyable\": false,
\"qr\": false
}"
action_result_stopped=" {
\"version\": \"0\",
\"message\": \"Bitcoin Core will reindex the chainstate the next time the service is started\",
\"value\": null,
\"copyable\": false,
\"qr\": false
}"
bitcoin-cli -rpcconnect=bitcoind.embassy stop >/dev/null 2>/dev/null && echo $action_result_running || echo $action_result_stopped
7 changes: 5 additions & 2 deletions manager/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ fn sidecar(config: &Mapping, addr: &str) -> Result<(), Box<dyn Error>> {
Ok(())
}

fn inner_main(reindex: bool) -> Result<(), Box<dyn Error>> {
fn inner_main(reindex: bool, reindex_chainstate: bool) -> Result<(), Box<dyn Error>> {
while !Path::new("/root/.bitcoin/start9/config.yaml").exists() {
std::thread::sleep(std::time::Duration::from_secs(1));
}
Expand Down Expand Up @@ -455,6 +455,8 @@ fn inner_main(reindex: bool) -> Result<(), Box<dyn Error>> {
}
if reindex {
btc_args.push("-reindex".to_owned());
} else if reindex_chainstate {
btc_args.push("-reindex-chainstate".to_owned());
}

std::io::copy(
Expand Down Expand Up @@ -533,6 +535,7 @@ fn inner_main(reindex: bool) -> Result<(), Box<dyn Error>> {
fn main() -> Result<(), Box<dyn Error>> {
env_logger::Builder::from_env(Env::default().default_filter_or("warn")).init();
let reindex = Path::new("/root/.bitcoin/requires.reindex").exists();
let reindex_chainstate = Path::new("/root/.bitcoin/requires.reindex_chainstate").exists();
ctrlc::set_handler(move || {
if let Some(raw_child) = *CHILD_PID.lock().unwrap() {
use nix::{
Expand All @@ -544,7 +547,7 @@ fn main() -> Result<(), Box<dyn Error>> {
std::process::exit(143)
}
})?;
inner_main(reindex)
inner_main(reindex, reindex_chainstate)
}

fn human_readable_timestamp(unix_time: u64) -> String {
Expand Down
18 changes: 17 additions & 1 deletion manifest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ actions:
reindex:
name: "Reindex Blockchain"
description: "Rebuilds the block and chainstate databases starting from genesis. If blocks already exist on disk, these are used rather than being redownloaded. For pruned nodes, this means downloading the entire blockchain over again."
warning: Blocks not stored on disk will be redownloaded in order to rebuild the database. If your node is pruned, this action is equivalent to syncing the node from scratch, so this process could take a couple of weeks.
warning: Blocks not stored on disk will be redownloaded in order to rebuild the database. If your node is pruned, this action is equivalent to syncing the node from scratch, so this process could take weeks on low-end hardware.
allowed-statuses:
- running
- stopped
Expand All @@ -150,6 +150,22 @@ actions:
mounts:
main: /root/.bitcoin
io-format: json
reindex-chainstate:
name: "Reindex Chainstate"
description: "Rebuilds the chainstate database using existing block index data; as the block index is not rebuilt, 'reindex_chainstate' should be strictly faster than 'reindex'. This action should only be used in the case of chainstate corruption; if the blocks stored on disk are corrupted, the 'reindex' action will need to be run instead."
warning: Blocks not stored on disk will be redownloaded in order to rebuild the database. If your node is pruned, this action is equivalent to syncing the node from scratch, so this process could take weeks on low-end hardware.
allowed-statuses:
- running
- stopped
implementation:
type: docker
image: main
system: false
entrypoint: reindex_chainstate.sh
args: []
mounts:
main: /root/.bitcoin
io-format: json
delete-txindex:
name: "Delete Transaction Index"
description: "Deletes the Transaction Index (txindex) in case it gets corrupted."
Expand Down

0 comments on commit 509274a

Please sign in to comment.