Makefile 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. .POSIX:
  2. # Suppress all directory-related messages for cleaner output
  3. MAKEFLAGS += --no-print-directory
  4. # Install prefix
  5. PREFIX = $(HOME)/.cargo
  6. # Cargo binary
  7. CARGO = cargo
  8. # Compile target
  9. RUST_TARGET = $(shell rustc -Vv | grep '^host: ' | cut -d' ' -f2)
  10. # Uncomment when doing musl static builds
  11. #RUSTFLAGS = -C target-feature=+crt-static -C link-self-contained=yes
  12. # Root directory of the project
  13. PROJECT_ROOT = ../../..
  14. # Directory where logs are stored
  15. LOG_HOME := $(shell echo ~/.local/share/darkfi/logs)
  16. # This is the main binary built by the Makefile.
  17. # Its corresponding configuration file is used to start the node.
  18. EXPLORERD_BIN := $(shell grep '^name = ' Cargo.toml | sed 1q | cut -d' ' -f3 | tr -d '"')
  19. EXPLORERD_CONFIG := $(EXPLORERD_BIN)_config.toml
  20. # If these binaries are missing when launching the explorer node environment,
  21. # the Makefile (via a sub-make call) will build them if needed. Their configurations
  22. # are used to start the nodes.
  23. MINERD_BIN := $(PROJECT_ROOT)/minerd
  24. MINERD_CONFIG := $(PROJECT_ROOT)/bin/minerd/minerd_config.toml
  25. DARKFID_BIN := $(PROJECT_ROOT)/darkfid
  26. DARKFID_CONFIG := $(PROJECT_ROOT)/bin/darkfid/darkfid_config.toml
  27. # Source to build
  28. SRC = \
  29. Cargo.toml \
  30. $(PROJECT_ROOT)/Cargo.toml \
  31. $(shell find src -type f -name '*.rs') \
  32. $(shell find $(PROJECT_ROOT)/src -type f -name '*.rs') \
  33. all: $(EXPLORERD_BIN)
  34. help:
  35. @echo "Explorerd Makefile Commands:"
  36. @echo ""
  37. @echo "Build targets:"
  38. @echo " make - Build the $(EXPLORERD_BIN) binary"
  39. @echo " make clean - Remove build artifacts"
  40. @echo " make install - Install $(EXPLORERD_BIN) to $(PREFIX)/bin"
  41. @echo " make uninstall - Remove $(EXPLORERD_BIN) from $(PREFIX)/bin"
  42. @echo ""
  43. @echo "Network management:"
  44. @echo " make start-localnet - Start the explorer node environment on localnet"
  45. @echo " make start-testnet - Start the explorer node environment on testnet"
  46. @echo " make start-mainnet - Start the explorer node environment on mainnet"
  47. @echo " ** Use VERBOSE=-vv or -vvv for debugging (applies to start-% commands only)"
  48. @echo ""
  49. @echo " make stop - Stop all nodes running within the explorer node environment"
  50. @echo ""
  51. @echo "Utility targets:"
  52. @echo " make bundle_contracts_src - Bundle contract sources and ZK proofs into tar archives in native_contracts_src directory"
  53. @echo " make await-startup-{network} - Wait until nodes are ready (used in scripting, replace {network} with localnet/testnet/mainnet)"
  54. @echo ""
  55. @echo "Log files are stored in: $(LOG_HOME)/{localnet|testnet|mainnet}/"
  56. $(EXPLORERD_BIN): $(SRC) bundle_contracts_src
  57. RUSTFLAGS="$(RUSTFLAGS)" $(CARGO) build --target=$(RUST_TARGET) --release --package $@
  58. cp -f $(PROJECT_ROOT)/target/$(RUST_TARGET)/release/$@ $@
  59. cp -f $(PROJECT_ROOT)/target/$(RUST_TARGET)/release/$@ $(PROJECT_ROOT)/$@
  60. clean:
  61. RUSTFLAGS="$(RUSTFLAGS)" $(CARGO) clean --target=$(RUST_TARGET) --release --package $(EXPLORERD_BIN)
  62. rm -f $(EXPLORERD_BIN) $(PROJECT_ROOT)/$(EXPLORERD_BIN)
  63. rm -rf native_contracts_src
  64. install: all
  65. mkdir -p $(DESTDIR)$(PREFIX)/bin
  66. cp -f $(EXPLORERD_BIN) $(DESTDIR)$(PREFIX)/bin
  67. chmod 755 $(DESTDIR)$(PREFIX)/bin/$(EXPLORERD_BIN)
  68. uninstall:
  69. rm -f $(DESTDIR)$(PREFIX)/bin/$(EXPLORERD_BIN)
  70. # Bundles contract sources and ZK proofs into tar archives, supporting both GNU and BSD tar
  71. bundle_contracts_src:
  72. @TMP_DIR="native_contracts_src/tmp"; \
  73. CONTRACT_SRC_DIR="$(PROJECT_ROOT)/src/contract"; \
  74. CONTRACTS="deployooor money dao"; \
  75. set -e; \
  76. # Create a temporary directory and clean up on exit \
  77. mkdir -p "$$TMP_DIR"; \
  78. trap "rm -rf $$TMP_DIR" 0 1 2 15; \
  79. # Bundle each native contract \
  80. for contract in $$CONTRACTS; do \
  81. PROOF_DIR="$$TMP_DIR/$$contract/proof"; \
  82. mkdir -p "$$TMP_DIR/$$contract"; \
  83. cp -R "$$CONTRACT_SRC_DIR/$$contract/src/"* "$$TMP_DIR/$$contract"; \
  84. # Include zk proofs if they exist \
  85. if [ -d "$$CONTRACT_SRC_DIR/$$contract/proof" ]; then \
  86. mkdir -p "$$PROOF_DIR"; \
  87. find "$$CONTRACT_SRC_DIR/$$contract/proof" -type f ! -name '*.bin' -exec cp {} "$$PROOF_DIR/" \; ; \
  88. # Move witness files to their own directory if present \
  89. if find "$$PROOF_DIR" -name '*.json' | grep -q .; then \
  90. mkdir -p "$$PROOF_DIR/witness"; \
  91. mv "$$PROOF_DIR"/*.json "$$PROOF_DIR/witness"; \
  92. fi; \
  93. fi; \
  94. (cd "$$TMP_DIR/$$contract" && tar --format=pax -cf ../../$${contract}_contract_src.tar *); \
  95. done;
  96. # Start explorer on localnet (requires minerd)
  97. start-localnet: check-minerd
  98. # Starts an explorer node environment for `localnet`, `testnet`, or `mainnet` networks.
  99. # It validates the input network, stops any currently running nodes, initializes the log directory,
  100. # optionally starts `minerd` for `localnet`, and starts the remaining nodes while waiting for them
  101. # to properly initialize.
  102. start-%: check-darkfid check-explorerd
  103. @if [ "$*" != "localnet" ] && [ "$*" != "testnet" ] && [ "$*" != "mainnet" ]; then \
  104. echo "Error: Unsupported network '$*'. Use 'localnet', 'testnet', or 'mainnet'."; \
  105. exit 1; \
  106. fi
  107. @$(MAKE) stop suppress_not_running=1
  108. @echo "Starting explorer node environment $*..."
  109. @sh -c ' \
  110. LOG_DIR=$(LOG_HOME)/$*; \
  111. mkdir -p "$$LOG_DIR"; \
  112. $(if $(filter localnet,$*), $(MINERD_BIN) -c $(MINERD_CONFIG) & echo $$! >> PIDs.txt; sleep 2;) \
  113. $(DARKFID_BIN) $(VERBOSE) --log "$$LOG_DIR/darkfid.log" -c $(DARKFID_CONFIG) --network $* & echo $$! >> PIDs.txt; sleep 2; \
  114. $(call wait_for_darkfid_startup, $$LOG_DIR) \
  115. ./$(EXPLORERD_BIN) $(VERBOSE) --log "$$LOG_DIR/explorerd.log" -c $(EXPLORERD_CONFIG) --network $* & echo $$! >> PIDs.txt; \
  116. $(call wait_for_explorerd_startup, $$LOG_DIR) \
  117. '
  118. # Starts an explorer node in no-sync mode for `localnet`, `testnet`, or `mainnet` networks.
  119. # In no-sync mode, no connections are made with the Darkfi blockchain network and the explorer
  120. # relies solely on a local database without attempting synchronization. As with the `start-%`
  121. # target, inputs are verified, any running nodes are stopped, the log directory is initialized,
  122. # and the node is started.
  123. start-no-sync-%: check-contracts check-explorerd
  124. @if [ "$*" != "localnet" ] && [ "$*" != "testnet" ] && [ "$*" != "mainnet" ]; then \
  125. echo "Error: Unsupported network '$*'. Use 'localnet', 'testnet', or 'mainnet'."; \
  126. exit 1; \
  127. fi
  128. @$(MAKE) stop suppress_not_running=1
  129. @echo "Starting explorer node environment (no sync) $*..."
  130. @sh -c ' \
  131. LOG_DIR=$(LOG_HOME)/$*; \
  132. mkdir -p "$$LOG_DIR"; \
  133. ./$(EXPLORERD_BIN) --log "$$LOG_DIR/explorerd.log" -c $(EXPLORERD_CONFIG) --network $* --no-sync & echo $$! >> PIDs.txt; \
  134. $(call wait_for_explorerd_startup, $$LOG_DIR) \
  135. '
  136. # Check and build darkfid if it does not exist
  137. check-darkfid:
  138. @if [ ! -f "$(DARKFID_BIN)" ]; then \
  139. echo "Building darkfid..."; \
  140. $(MAKE) -C "$(PROJECT_ROOT)" darkfid; \
  141. fi
  142. # Check and build explorerd if it does not exist
  143. check-explorerd:
  144. @if [ ! -f "$(EXPLORERD_BIN)" ]; then \
  145. echo "Building explorerd..."; \
  146. $(MAKE) -C .; \
  147. fi
  148. # Check and build minerd if it does not exist
  149. check-minerd:
  150. @if [ ! -f "$(MINERD_BIN)" ]; then \
  151. echo "Building minerd..."; \
  152. $(MAKE) -C "$(PROJECT_ROOT)" minerd; \
  153. fi
  154. # Check and build contracts if they do not exist
  155. check-contracts:
  156. @if [ ! -f "$(PROJECT_ROOT)/src/contract/money/darkfi_money_contract.wasm" ] \
  157. || [ ! -f "$(PROJECT_ROOT)/contract/dao/darkfi_dao_contract.wasm" ] \
  158. || [ ! -f "$(PROJECT_ROOT)/contract/deployooor/darkfi_deployooor_contract.wasm" ]; then \
  159. echo "Building contracts..."; \
  160. $(MAKE) -C "$(PROJECT_ROOT)" contracts; \
  161. fi
  162. # Stop the running network
  163. # Usage: make stop [suppress_not_running=1]
  164. stop:
  165. @if [ -f PIDs.txt ]; then \
  166. while read PID; do \
  167. if ps -p $$PID > /dev/null 2>&1; then \
  168. kill -15 $$PID 2>/dev/null; \
  169. sleep 5; \
  170. ps -p $$PID > /dev/null 2>&1 && kill -9 $$PID 2>/dev/null; \
  171. fi; \
  172. done < PIDs.txt; \
  173. rm -f PIDs.txt; \
  174. echo "Stopped explorer node environment"; \
  175. else \
  176. if [ "$(suppress_not_running)" != "1" ]; then \
  177. echo "Explorer node environment not running, nothing to stop."; \
  178. fi; \
  179. fi
  180. # Wait for the network to start
  181. await-startup-%:
  182. @$(call wait_for_darkfid_startup,$(LOG_HOME)/$*)
  183. @$(call wait_for_explorerd_startup,$(LOG_HOME)/$*)
  184. # Wait for the explorer node environment to start in no-sync mode (skip waiting for darkfid startup)
  185. await-startup-no-sync-%:
  186. @$(call wait_for_explorerd_startup,$(LOG_HOME)/$*)
  187. # Waits for Darkfid to start
  188. define wait_for_darkfid_startup
  189. log_dir=$(strip $(1)); \
  190. while ! grep -q "Blockchain synced!" "$$log_dir/darkfid.log" 2>/dev/null; do \
  191. sleep 1; \
  192. done;
  193. endef
  194. # Waits for Explorerd to start
  195. define wait_for_explorerd_startup
  196. log_dir=$(strip $(1)); \
  197. while ! grep -q "Started DarkFi Explorer Node" "$$log_dir/explorerd.log" 2>/dev/null; do \
  198. sleep 1; \
  199. done;
  200. endef
  201. .PHONY: help all clean install uninstall bundle_contracts_src check-minerd check-darkfid check-explorerd stop start-%