Skip to content

Commit

Permalink
explorerd: automate starting explorer nodes and associated darkfi net…
Browse files Browse the repository at this point in the history
…works

Enhanced Makefile with capabilities to automate the startup and shutdown of explorerd and its dependencies using a single `make` command. These additions create a streamlined user experience for running explorer nodes across the DarkFi networks, orchestrating the process by automatically building necessary dependencies prior to starting an explorer node environment. Ordered startup sequencing has been implemented to ensure darkfid starts before launching explorerd, with a mechanism to wait for explorerd to complete initialization to avoid startup race conditions.

The implementation maintains PID tracking for graceful process shutdown. Additionally, it organizes logs by network type (localnet, testnet, mainnet) in dedicated directories (`~/.local/share/darkfi/logs/NETWORK`) that are monitored to determine when nodes are initialized.

To view a list of available make commands (targets), run: `make help`.

Key Features:
- Single Command Launch: Provides targets to start explorer node environments with a single command (`make start-localnet/testnet/mainnet`)
- Automatic Dependency Checks and Builds: Automatic dependency resolution that builds missing `darkfid`, `explorerd`, and `minerd` binaries before starting nodes
- Graceful Shutdown: Added graceful network shutdown capability with PID tracking (`make stop`)
- Ordered Initialization: Implemented ordered startup sequencing for network nodes, ensuring `darkfid` starts before launching `explorerd`, and waiting for explorerd to complete initialization before it is marked as started
- Organized Logging: Logs for each started network (localnet, testnet, mainnet) are saved in their respective directories (~/.local/share/darkfi/logs/NETWORK)
- Log Monitoring: Implemented log monitoring to ensure that nodes are not started before their dependencies are ready
- Error Handling: Ensures invalid or unsupported network arguments are reported with an error message
  • Loading branch information
kalm committed Mar 9, 2025
1 parent 27d8618 commit f16ed54
Showing 1 changed file with 111 additions and 1 deletion.
112 changes: 111 additions & 1 deletion bin/explorer/explorerd/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
.POSIX:

# Suppress all directory-related messages for cleaner output
MAKEFLAGS += --no-print-directory

# Install prefix
PREFIX = $(HOME)/.cargo

Expand All @@ -11,6 +14,12 @@ RUST_TARGET = $(shell rustc -Vv | grep '^host: ' | cut -d' ' -f2)
# Uncomment when doing musl static builds
#RUSTFLAGS = -C target-feature=+crt-static -C link-self-contained=yes

# Root directory of the project
PROJECT_ROOT=../../..

# Directory where node logs are stored
LOG_HOME := $(shell echo ~/.local/share/darkfi/logs)

SRC = \
Cargo.toml \
../../../Cargo.toml \
Expand All @@ -21,6 +30,27 @@ BIN = $(shell grep '^name = ' Cargo.toml | sed 1q | cut -d' ' -f3 | tr -d '"')

all: $(BIN)

help:
@echo "Explorerd Makefile Commands:"
@echo ""
@echo "Build targets:"
@echo " make - Build the $(BIN) binary"
@echo " make clean - Remove build artifacts"
@echo " make install - Install $(BIN) to $(PREFIX)/bin"
@echo " make uninstall - Remove $(BIN) from $(PREFIX)/bin"
@echo ""
@echo "Network management:"
@echo " make start-localnet - Start the explorer node environment on localnet"
@echo " make start-testnet - Start the explorer node environment on testnet"
@echo " make start-mainnet - Start the explorer node environment on mainnet"
@echo " make stop - Stop all nodes running within the explorer node environment"
@echo ""
@echo "Utility targets:"
@echo " make bundle_contracts_src - Bundle contract sources and ZK proofs into a tar archives in native_contracts_src directory"
@echo " make await-startup-NETWORK - Wait until nodes are ready (used in scripting, replace NETWORK with localnet/testnet/mainnet)"
@echo ""
@echo "Log files are stored in: $(LOG_HOME)/{localnet|testnet|mainnet}/"

$(BIN): $(SRC) bundle_contracts_src
RUSTFLAGS="$(RUSTFLAGS)" $(CARGO) build --target=$(RUST_TARGET) --release --package $@
cp -f ../../../target/$(RUST_TARGET)/release/$@ $@
Expand All @@ -39,6 +69,7 @@ install: all
uninstall:
rm -f $(DESTDIR)$(PREFIX)/bin/$(BIN)

# Bundle native contract sources and their ZK proofs
bundle_contracts_src:
@mkdir -p $(CURDIR)/native_contracts_src
@(cd ../../../src && \
Expand All @@ -49,4 +80,83 @@ bundle_contracts_src:
find contract/money/proof -name '*.zk' -exec tar -rf $(CURDIR)/native_contracts_src/money_contract_src.tar --transform 's,^.*proof/,proof/,' {} + \
)

.PHONY: all clean install uninstall bundle_contracts_src
# Start explorer on darkfid localnet
start-localnet: check-minerd

# Start localnet/testnet/mainnet networks
start-%: check-darkfid check-explorerd
@if [ "$*" != "localnet" ] && [ "$*" != "testnet" ] && [ "$*" != "mainnet" ]; then \
echo "Error: Unsupported network '$*'. Use 'localnet', 'testnet', or 'mainnet'."; \
exit 1; \
fi
@$(MAKE) stop suppress_not_running=1
@echo "Starting explorer node environment $*..."
@sh -c ' \
LOG_DIR=$(LOG_HOME)/$*; \
mkdir -p "$$LOG_DIR"; \
$(if $(filter localnet,$*),$(PROJECT_ROOT)/minerd -c $(PROJECT_ROOT)/bin/minerd/minerd_config.toml & echo $$! >> PIDs.txt; sleep 2;) \
$(PROJECT_ROOT)/darkfid --log "$$LOG_DIR/darkfid.log" -c $(PROJECT_ROOT)/bin/darkfid/darkfid_config.toml --network $* & echo $$! >> PIDs.txt; sleep 2; \
$(call wait_for_darkfid_startup, $$LOG_DIR) \
./explorerd --log "$$LOG_DIR/explorerd.log" -c ./explorerd_config.toml --network $* & echo $$! >> PIDs.txt; \
$(call wait_for_explorerd_startup, $$LOG_DIR) \
'

# Check and build darkfid if it does not exist
check-darkfid:
@if [ ! -f $(PROJECT_ROOT)/darkfid ]; then \
echo "Building darkfid..."; \
$(MAKE) -C $(PROJECT_ROOT) darkfid; \
fi

# Check and build explorerd if it does not exist
check-explorerd:
@if [ ! -f ./explorerd ]; then \
echo "Building explorerd..."; \
$(MAKE) -C . ; \
fi

# Check and build minerd if it does not exist
check-minerd:
@if [ ! -f $(PROJECT_ROOT)/minerd ]; then \
echo "Building minerd..."; \
$(MAKE) -C $(PROJECT_ROOT) minerd; \
fi

# Stop the running network
# Usage: make stop [suppress_not_running=1]
stop:
@if [ -f PIDs.txt ]; then \
while read PID; do \
kill $$PID 2>/dev/null || echo "Unable to kill PID $$PID"; \
done < PIDs.txt; \
rm -f PIDs.txt; \
sleep 5; \
echo "Stopped explorer node environment"; \
else \
if [ "$(suppress_not_running)" != "1" ]; then \
echo "Explorer node environment not running, nothing to stop."; \
fi; \
fi

# Waits for Darkfid to start
define wait_for_darkfid_startup
log_dir=$(strip $(1)); \
while ! grep -q "Darkfid P2P handler started successfully!" "$$log_dir/darkfid.log" 2>/dev/null; do \
sleep 1; \
done;
endef

# Waits for Explorerd to start
define wait_for_explorerd_startup
log_dir=$(strip $(1)); \
while ! grep -q "Started DarkFi Explorer Node" "$$log_dir/explorerd.log" 2>/dev/null; do \
sleep 1; \
done;
endef

# Waits for network to start
await-startup-%:
@$(call wait_for_darkfid_startup,$(LOG_HOME)/$*)
@$(call wait_for_explorerd_startup,$(LOG_HOME)/$*)

.PHONY: help all clean install uninstall bundle_contracts_src check-minerd check-darkfid check-explorerd stop start-%

0 comments on commit f16ed54

Please sign in to comment.