explorerd: clean up and further document makefile

This commit cleans up the explorerd Makefile by:

- Adding named variables for all binary and configuration paths
- Enhancing the `bundle_contracts_src` target's documentation
- Cleaning up path handling to use variable references
- Improving comments in the `start-%` target to explain the workflow
- Updating all project root path references to use the `$(PROJECT_ROOT)` variable
- Reorganizing the `await-startup-%` target to group it with other targets
This commit is contained in:
kalm
2025-03-13 22:19:57 -07:00
parent 2d02bbae50
commit 8ef29926a1

View File

@@ -15,29 +15,41 @@ RUST_TARGET = $(shell rustc -Vv | grep '^host: ' | cut -d' ' -f2)
#RUSTFLAGS = -C target-feature=+crt-static -C link-self-contained=yes
# Root directory of the project
PROJECT_ROOT=../../..
PROJECT_ROOT = ../../..
# Directory where node logs are stored
# Directory where logs are stored
LOG_HOME := $(shell echo ~/.local/share/darkfi/logs)
# This is the main binary built by the Makefile.
# Its corresponding configuration file is used to start the node.
EXPLORERD_BIN := $(shell grep '^name = ' Cargo.toml | sed 1q | cut -d' ' -f3 | tr -d '"')
EXPLORERD_CONFIG := $(EXPLORERD_BIN)_config.toml
# If these binaries are missing when launching the explorer node environment,
# the Makefile (via a sub-make call) will build them if needed. Their configurations
# are used to start the nodes.
MINERD_BIN := $(PROJECT_ROOT)/minerd
MINERD_CONFIG := $(PROJECT_ROOT)/bin/minerd/minerd_config.toml
DARKFID_BIN := $(PROJECT_ROOT)/darkfid
DARKFID_CONFIG := $(PROJECT_ROOT)/bin/darkfid/darkfid_config.toml
# Source to build
SRC = \
Cargo.toml \
../../../Cargo.toml \
$(PROJECT_ROOT)/Cargo.toml \
$(shell find src -type f -name '*.rs') \
$(shell find ../../../src -type f -name '*.rs') \
$(shell find $(PROJECT_ROOT)/src -type f -name '*.rs') \
BIN = $(shell grep '^name = ' Cargo.toml | sed 1q | cut -d' ' -f3 | tr -d '"')
all: $(BIN)
all: $(EXPLORERD_BIN)
help:
@echo "Explorerd Makefile Commands:"
@echo ""
@echo "Build targets:"
@echo " make - Build the $(BIN) binary"
@echo " make - Build the $(EXPLORERD_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 " make install - Install $(EXPLORERD_BIN) to $(PREFIX)/bin"
@echo " make uninstall - Remove $(EXPLORERD_BIN) from $(PREFIX)/bin"
@echo ""
@echo "Network management:"
@echo " make start-localnet - Start the explorer node environment on localnet"
@@ -46,55 +58,63 @@ help:
@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 " make bundle_contracts_src - Bundle contract sources and ZK proofs into 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
$(EXPLORERD_BIN): $(SRC) bundle_contracts_src
RUSTFLAGS="$(RUSTFLAGS)" $(CARGO) build --target=$(RUST_TARGET) --release --package $@
cp -f ../../../target/$(RUST_TARGET)/release/$@ $@
cp -f ../../../target/$(RUST_TARGET)/release/$@ ../../../$@
cp -f $(PROJECT_ROOT)/target/$(RUST_TARGET)/release/$@ $@
cp -f $(PROJECT_ROOT)/target/$(RUST_TARGET)/release/$@ $(PROJECT_ROOT)/$@
clean:
RUSTFLAGS="$(RUSTFLAGS)" $(CARGO) clean --target=$(RUST_TARGET) --release --package $(BIN)
rm -f $(BIN) ../../../$(BIN)
RUSTFLAGS="$(RUSTFLAGS)" $(CARGO) clean --target=$(RUST_TARGET) --release --package $(EXPLORERD_BIN)
rm -f $(EXPLORERD_BIN) $(PROJECT_ROOT)/$(EXPLORERD_BIN)
rm -rf native_contracts_src
install: all
mkdir -p $(DESTDIR)$(PREFIX)/bin
cp -f $(BIN) $(DESTDIR)$(PREFIX)/bin
chmod 755 $(DESTDIR)$(PREFIX)/bin/$(BIN)
cp -f $(EXPLORERD_BIN) $(DESTDIR)$(PREFIX)/bin
chmod 755 $(DESTDIR)$(PREFIX)/bin/$(EXPLORERD_BIN)
uninstall:
rm -f $(DESTDIR)$(PREFIX)/bin/$(BIN)
rm -f $(DESTDIR)$(PREFIX)/bin/$(EXPLORERD_BIN)
# Bundle native contract sources and their ZK proofs
# Bundles contract sources and ZK proofs into tar archives, supporting both GNU and BSD tar
bundle_contracts_src:
@PROJECT_ROOT=$$(pwd)/$(PROJECT_ROOT); \
CONTRACT_SRC_DIR="$$PROJECT_ROOT/src/contract"; \
@TMP_DIR="native_contracts_src/tmp"; \
CONTRACT_SRC_DIR="$(PROJECT_ROOT)/src/contract"; \
CONTRACTS="deployooor money dao"; \
set -e; \
mkdir -p native_contracts_src/tmp; \
trap "rm -rf native_contracts_src/tmp" 0 1 2 15; \
# Create a temporary directory and clean up on exit \
mkdir -p "$$TMP_DIR"; \
trap "rm -rf $$TMP_DIR" 0 1 2 15; \
# Bundle each native contract \
for contract in $$CONTRACTS; do \
mkdir -p native_contracts_src/tmp/$$contract; \
cp -R "$$CONTRACT_SRC_DIR/$$contract/src/"* "native_contracts_src/tmp/$$contract"; \
PROOF_DIR="$$TMP_DIR/$$contract/proof"; \
mkdir -p "$$TMP_DIR/$$contract"; \
cp -R "$$CONTRACT_SRC_DIR/$$contract/src/"* "$$TMP_DIR/$$contract"; \
# Include zk proofs if they exist \
if [ -d "$$CONTRACT_SRC_DIR/$$contract/proof" ]; then \
mkdir -p native_contracts_src/tmp/$$contract/proof; \
find "$$CONTRACT_SRC_DIR/$$contract/proof" -type f ! -name '*.bin' -exec cp \{\} native_contracts_src/tmp/$$contract/proof/ \; ; \
if find "native_contracts_src/tmp/$$contract/proof" -name '*.json' | grep -q .; then \
mkdir -p "native_contracts_src/tmp/$$contract/proof/witness"; \
mv native_contracts_src/tmp/$$contract/proof/*.json "native_contracts_src/tmp/$$contract/proof/witness"; \
fi; \
mkdir -p "$$PROOF_DIR"; \
find "$$CONTRACT_SRC_DIR/$$contract/proof" -type f ! -name '*.bin' -exec cp {} "$$PROOF_DIR/" \; ; \
# Move witness files to their own directory if present \
if find "$$PROOF_DIR" -name '*.json' | grep -q .; then \
mkdir -p "$$PROOF_DIR/witness"; \
mv "$$PROOF_DIR"/*.json "$$PROOF_DIR/witness"; \
fi; \
fi; \
(cd native_contracts_src/tmp/$$contract && tar --format=pax -cf ../../$${contract}_contract_src.tar *); \
(cd "$$TMP_DIR/$$contract" && tar --format=pax -cf ../../$${contract}_contract_src.tar *); \
done;
# Start explorer on darkfid localnet
# Start explorer on localnet (requires minerd)
start-localnet: check-minerd
# Start localnet/testnet/mainnet networks
# Starts an explorer node environment for `localnet`, `testnet`, or `mainnet` networks.
# It validates the input network, stops any currently running nodes, initializes the log directory,
# optionally starts `minerd` for `localnet`, and starts the remaining nodes while waiting for them
# to properly initialize.
start-%: check-darkfid check-explorerd
@if [ "$*" != "localnet" ] && [ "$*" != "testnet" ] && [ "$*" != "mainnet" ]; then \
echo "Error: Unsupported network '$*'. Use 'localnet', 'testnet', or 'mainnet'."; \
@@ -105,32 +125,32 @@ start-%: check-darkfid check-explorerd
@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; \
$(if $(filter localnet,$*), $(MINERD_BIN) -c $(MINERD_CONFIG) & echo $$! >> PIDs.txt; sleep 2;) \
$(DARKFID_BIN) --log "$$LOG_DIR/darkfid.log" -c $(DARKFID_CONFIG) --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; \
./$(EXPLORERD_BIN) --log "$$LOG_DIR/explorerd.log" -c $(EXPLORERD_CONFIG) --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 \
@if [ ! -f "$(DARKFID_BIN)" ]; then \
echo "Building darkfid..."; \
$(MAKE) -C $(PROJECT_ROOT) darkfid; \
$(MAKE) -C "$(PROJECT_ROOT)" darkfid; \
fi
# Check and build explorerd if it does not exist
check-explorerd:
@if [ ! -f ./explorerd ]; then \
@if [ ! -f "$(EXPLORERD_BIN)" ]; then \
echo "Building explorerd..."; \
$(MAKE) -C . ; \
$(MAKE) -C .; \
fi
# Check and build minerd if it does not exist
check-minerd:
@if [ ! -f $(PROJECT_ROOT)/minerd ]; then \
@if [ ! -f "$(MINERD_BIN)" ]; then \
echo "Building minerd..."; \
$(MAKE) -C $(PROJECT_ROOT) minerd; \
$(MAKE) -C "$(PROJECT_ROOT)" minerd; \
fi
# Stop the running network
@@ -139,7 +159,9 @@ stop:
@if [ -f PIDs.txt ]; then \
while read PID; do \
if ps -p $$PID > /dev/null 2>&1; then \
kill -15 $$PID 2>/dev/null; sleep 5; ps -p $$PID > /dev/null 2>&1 && kill -9 $$PID 2>/dev/null; \
kill -15 $$PID 2>/dev/null; \
sleep 5; \
ps -p $$PID > /dev/null 2>&1 && kill -9 $$PID 2>/dev/null; \
fi; \
done < PIDs.txt; \
rm -f PIDs.txt; \
@@ -150,6 +172,11 @@ stop:
fi; \
fi
# Wait for the network to start from another Makefile
await-startup-%:
@$(call wait_for_darkfid_startup,$(LOG_HOME)/$*)
@$(call wait_for_explorerd_startup,$(LOG_HOME)/$*)
# Waits for Darkfid to start
define wait_for_darkfid_startup
log_dir=$(strip $(1)); \
@@ -166,9 +193,4 @@ define wait_for_explorerd_startup
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-%
.PHONY: help all clean install uninstall bundle_contracts_src check-minerd check-darkfid check-explorerd stop start-%