Makefile 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 " make stop - Stop all nodes running within the explorer node environment"
  48. @echo ""
  49. @echo "Utility targets:"
  50. @echo " make bundle_contracts_src - Bundle contract sources and ZK proofs into tar archives in native_contracts_src directory"
  51. @echo " make await-startup-{network} - Wait until nodes are ready (used in scripting, replace {network} with localnet/testnet/mainnet)"
  52. @echo ""
  53. @echo "Log files are stored in: $(LOG_HOME)/{localnet|testnet|mainnet}/"
  54. $(EXPLORERD_BIN): $(SRC) bundle_contracts_src
  55. RUSTFLAGS="$(RUSTFLAGS)" $(CARGO) build --target=$(RUST_TARGET) --release --package $@
  56. cp -f $(PROJECT_ROOT)/target/$(RUST_TARGET)/release/$@ $@
  57. cp -f $(PROJECT_ROOT)/target/$(RUST_TARGET)/release/$@ $(PROJECT_ROOT)/$@
  58. clean:
  59. RUSTFLAGS="$(RUSTFLAGS)" $(CARGO) clean --target=$(RUST_TARGET) --release --package $(EXPLORERD_BIN)
  60. rm -f $(EXPLORERD_BIN) $(PROJECT_ROOT)/$(EXPLORERD_BIN)
  61. rm -rf native_contracts_src
  62. install: all
  63. mkdir -p $(DESTDIR)$(PREFIX)/bin
  64. cp -f $(EXPLORERD_BIN) $(DESTDIR)$(PREFIX)/bin
  65. chmod 755 $(DESTDIR)$(PREFIX)/bin/$(EXPLORERD_BIN)
  66. uninstall:
  67. rm -f $(DESTDIR)$(PREFIX)/bin/$(EXPLORERD_BIN)
  68. # Bundles contract sources and ZK proofs into tar archives, supporting both GNU and BSD tar
  69. bundle_contracts_src:
  70. @TMP_DIR="native_contracts_src/tmp"; \
  71. CONTRACT_SRC_DIR="$(PROJECT_ROOT)/src/contract"; \
  72. CONTRACTS="deployooor money dao"; \
  73. set -e; \
  74. # Create a temporary directory and clean up on exit \
  75. mkdir -p "$$TMP_DIR"; \
  76. trap "rm -rf $$TMP_DIR" 0 1 2 15; \
  77. # Bundle each native contract \
  78. for contract in $$CONTRACTS; do \
  79. PROOF_DIR="$$TMP_DIR/$$contract/proof"; \
  80. mkdir -p "$$TMP_DIR/$$contract"; \
  81. cp -R "$$CONTRACT_SRC_DIR/$$contract/src/"* "$$TMP_DIR/$$contract"; \
  82. # Include zk proofs if they exist \
  83. if [ -d "$$CONTRACT_SRC_DIR/$$contract/proof" ]; then \
  84. mkdir -p "$$PROOF_DIR"; \
  85. find "$$CONTRACT_SRC_DIR/$$contract/proof" -type f ! -name '*.bin' -exec cp {} "$$PROOF_DIR/" \; ; \
  86. # Move witness files to their own directory if present \
  87. if find "$$PROOF_DIR" -name '*.json' | grep -q .; then \
  88. mkdir -p "$$PROOF_DIR/witness"; \
  89. mv "$$PROOF_DIR"/*.json "$$PROOF_DIR/witness"; \
  90. fi; \
  91. fi; \
  92. (cd "$$TMP_DIR/$$contract" && tar --format=pax -cf ../../$${contract}_contract_src.tar *); \
  93. done;
  94. # Start explorer on localnet (requires minerd)
  95. start-localnet: check-minerd
  96. # Starts an explorer node environment for `localnet`, `testnet`, or `mainnet` networks.
  97. # It validates the input network, stops any currently running nodes, initializes the log directory,
  98. # optionally starts `minerd` for `localnet`, and starts the remaining nodes while waiting for them
  99. # to properly initialize.
  100. start-%: check-darkfid check-explorerd
  101. @if [ "$*" != "localnet" ] && [ "$*" != "testnet" ] && [ "$*" != "mainnet" ]; then \
  102. echo "Error: Unsupported network '$*'. Use 'localnet', 'testnet', or 'mainnet'."; \
  103. exit 1; \
  104. fi
  105. @$(MAKE) stop suppress_not_running=1
  106. @echo "Starting explorer node environment $*..."
  107. @sh -c ' \
  108. LOG_DIR=$(LOG_HOME)/$*; \
  109. mkdir -p "$$LOG_DIR"; \
  110. $(if $(filter localnet,$*), $(MINERD_BIN) -c $(MINERD_CONFIG) & echo $$! >> PIDs.txt; sleep 2;) \
  111. $(DARKFID_BIN) --log "$$LOG_DIR/darkfid.log" -c $(DARKFID_CONFIG) --network $* & echo $$! >> PIDs.txt; sleep 2; \
  112. $(call wait_for_darkfid_startup, $$LOG_DIR) \
  113. ./$(EXPLORERD_BIN) --log "$$LOG_DIR/explorerd.log" -c $(EXPLORERD_CONFIG) --network $* & echo $$! >> PIDs.txt; \
  114. $(call wait_for_explorerd_startup, $$LOG_DIR) \
  115. '
  116. # Starts an explorer node in no-sync mode for `localnet`, `testnet`, or `mainnet` networks.
  117. # In no-sync mode, no connections are made with the Darkfi blockchain network and the explorer
  118. # relies solely on a local database without attempting synchronization. As with the `start-%`
  119. # target, inputs are verified, any running nodes are stopped, the log directory is initialized,
  120. # and the node is started.
  121. start-no-sync-%: check-contracts check-explorerd
  122. @if [ "$*" != "localnet" ] && [ "$*" != "testnet" ] && [ "$*" != "mainnet" ]; then \
  123. echo "Error: Unsupported network '$*'. Use 'localnet', 'testnet', or 'mainnet'."; \
  124. exit 1; \
  125. fi
  126. @$(MAKE) stop suppress_not_running=1
  127. @echo "Starting explorer node environment (no sync) $*..."
  128. @sh -c ' \
  129. LOG_DIR=$(LOG_HOME)/$*; \
  130. mkdir -p "$$LOG_DIR"; \
  131. ./$(EXPLORERD_BIN) --log "$$LOG_DIR/explorerd.log" -c $(EXPLORERD_CONFIG) --network $* --no-sync & echo $$! >> PIDs.txt; \
  132. $(call wait_for_explorerd_startup, $$LOG_DIR) \
  133. '
  134. # Check and build darkfid if it does not exist
  135. check-darkfid:
  136. @if [ ! -f "$(DARKFID_BIN)" ]; then \
  137. echo "Building darkfid..."; \
  138. $(MAKE) -C "$(PROJECT_ROOT)" darkfid; \
  139. fi
  140. # Check and build explorerd if it does not exist
  141. check-explorerd:
  142. @if [ ! -f "$(EXPLORERD_BIN)" ]; then \
  143. echo "Building explorerd..."; \
  144. $(MAKE) -C .; \
  145. fi
  146. # Check and build minerd if it does not exist
  147. check-minerd:
  148. @if [ ! -f "$(MINERD_BIN)" ]; then \
  149. echo "Building minerd..."; \
  150. $(MAKE) -C "$(PROJECT_ROOT)" minerd; \
  151. fi
  152. # Check and build contracts if they do not exist
  153. check-contracts:
  154. @if [ ! -f "$(PROJECT_ROOT)/src/contract/money/darkfi_money_contract.wasm" ] \
  155. || [ ! -f "$(PROJECT_ROOT)/contract/dao/darkfi_dao_contract.wasm" ] \
  156. || [ ! -f "$(PROJECT_ROOT)/contract/deployooor/darkfi_deployooor_contract.wasm" ]; then \
  157. echo "Building contracts..."; \
  158. $(MAKE) -C "$(PROJECT_ROOT)" contracts; \
  159. fi
  160. # Stop the running network
  161. # Usage: make stop [suppress_not_running=1]
  162. stop:
  163. @if [ -f PIDs.txt ]; then \
  164. while read PID; do \
  165. if ps -p $$PID > /dev/null 2>&1; then \
  166. kill -15 $$PID 2>/dev/null; \
  167. sleep 5; \
  168. ps -p $$PID > /dev/null 2>&1 && kill -9 $$PID 2>/dev/null; \
  169. fi; \
  170. done < PIDs.txt; \
  171. rm -f PIDs.txt; \
  172. echo "Stopped explorer node environment"; \
  173. else \
  174. if [ "$(suppress_not_running)" != "1" ]; then \
  175. echo "Explorer node environment not running, nothing to stop."; \
  176. fi; \
  177. fi
  178. # Wait for the network to start
  179. await-startup-%:
  180. @$(call wait_for_darkfid_startup,$(LOG_HOME)/$*)
  181. @$(call wait_for_explorerd_startup,$(LOG_HOME)/$*)
  182. # Wait for the explorer node environment to start in no-sync mode (skip waiting for darkfid startup)
  183. await-startup-no-sync-%:
  184. @$(call wait_for_explorerd_startup,$(LOG_HOME)/$*)
  185. # Waits for Darkfid to start
  186. define wait_for_darkfid_startup
  187. log_dir=$(strip $(1)); \
  188. while ! grep -q "Darkfid P2P handler started successfully!" "$$log_dir/darkfid.log" 2>/dev/null; do \
  189. sleep 1; \
  190. done;
  191. endef
  192. # Waits for Explorerd to start
  193. define wait_for_explorerd_startup
  194. log_dir=$(strip $(1)); \
  195. while ! grep -q "Started DarkFi Explorer Node" "$$log_dir/explorerd.log" 2>/dev/null; do \
  196. sleep 1; \
  197. done;
  198. endef
  199. .PHONY: help all clean install uninstall bundle_contracts_src check-minerd check-darkfid check-explorerd stop start-%