Makefile 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. DARKFID_BIN := $(PROJECT_ROOT)/darkfid
  24. DARKFID_CONFIG := $(PROJECT_ROOT)/bin/darkfid/darkfid_config.toml
  25. # Source to build
  26. SRC = \
  27. Cargo.toml \
  28. $(PROJECT_ROOT)/Cargo.toml \
  29. $(shell find src -type f -name '*.rs') \
  30. $(shell find $(PROJECT_ROOT)/src -type f -name '*.rs') \
  31. all: $(EXPLORERD_BIN)
  32. help:
  33. @echo "Explorerd Makefile Commands:"
  34. @echo ""
  35. @echo "Build targets:"
  36. @echo " make - Build the $(EXPLORERD_BIN) binary"
  37. @echo " make clean - Remove build artifacts"
  38. @echo " make install - Install $(EXPLORERD_BIN) to $(PREFIX)/bin"
  39. @echo " make uninstall - Remove $(EXPLORERD_BIN) from $(PREFIX)/bin"
  40. @echo ""
  41. @echo "Network management:"
  42. @echo " make start-localnet - Start the explorer node environment on localnet"
  43. @echo " make start-testnet - Start the explorer node environment on testnet"
  44. @echo " make start-mainnet - Start the explorer node environment on mainnet"
  45. @echo " ** Use VERBOSE=-vv or -vvv for debugging (applies to start-% commands only)"
  46. @echo ""
  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. # Starts an explorer node environment for `localnet`, `testnet`, or `mainnet` networks.
  95. # It validates the input network, stops any currently running nodes, initializes the log directory,
  96. # and starts the remaining nodes while waiting for them to properly initialize.
  97. start-%: check-darkfid check-explorerd
  98. @if [ "$*" != "localnet" ] && [ "$*" != "testnet" ] && [ "$*" != "mainnet" ]; then \
  99. echo "Error: Unsupported network '$*'. Use 'localnet', 'testnet', or 'mainnet'."; \
  100. exit 1; \
  101. fi
  102. @$(MAKE) stop suppress_not_running=1
  103. @echo "Starting explorer node environment $*..."
  104. @sh -c ' \
  105. LOG_DIR=$(LOG_HOME)/$*; \
  106. mkdir -p "$$LOG_DIR"; \
  107. $(DARKFID_BIN) $(VERBOSE) --log "$$LOG_DIR/darkfid.log" -c $(DARKFID_CONFIG) --network $* & echo $$! >> PIDs.txt; sleep 2; \
  108. $(call wait_for_darkfid_startup, $$LOG_DIR) \
  109. ./$(EXPLORERD_BIN) $(VERBOSE) --log "$$LOG_DIR/explorerd.log" -c $(EXPLORERD_CONFIG) --network $* & echo $$! >> PIDs.txt; \
  110. $(call wait_for_explorerd_startup, $$LOG_DIR) \
  111. '
  112. # Starts an explorer node in no-sync mode for `localnet`, `testnet`, or `mainnet` networks.
  113. # In no-sync mode, no connections are made with the Darkfi blockchain network and the explorer
  114. # relies solely on a local database without attempting synchronization. As with the `start-%`
  115. # target, inputs are verified, any running nodes are stopped, the log directory is initialized,
  116. # and the node is started.
  117. start-no-sync-%: check-contracts check-explorerd
  118. @if [ "$*" != "localnet" ] && [ "$*" != "testnet" ] && [ "$*" != "mainnet" ]; then \
  119. echo "Error: Unsupported network '$*'. Use 'localnet', 'testnet', or 'mainnet'."; \
  120. exit 1; \
  121. fi
  122. @$(MAKE) stop suppress_not_running=1
  123. @echo "Starting explorer node environment (no sync) $*..."
  124. @sh -c ' \
  125. LOG_DIR=$(LOG_HOME)/$*; \
  126. mkdir -p "$$LOG_DIR"; \
  127. ./$(EXPLORERD_BIN) --log "$$LOG_DIR/explorerd.log" -c $(EXPLORERD_CONFIG) --network $* --no-sync & echo $$! >> PIDs.txt; \
  128. $(call wait_for_explorerd_startup, $$LOG_DIR) \
  129. '
  130. # Check and build darkfid if it does not exist
  131. check-darkfid:
  132. @if [ ! -f "$(DARKFID_BIN)" ]; then \
  133. echo "Building darkfid..."; \
  134. $(MAKE) -C "$(PROJECT_ROOT)" darkfid; \
  135. fi
  136. # Check and build explorerd if it does not exist
  137. check-explorerd:
  138. @if [ ! -f "$(EXPLORERD_BIN)" ]; then \
  139. echo "Building explorerd..."; \
  140. $(MAKE) -C .; \
  141. fi
  142. # Check and build contracts if they do not exist
  143. check-contracts:
  144. @if [ ! -f "$(PROJECT_ROOT)/src/contract/money/darkfi_money_contract.wasm" ] \
  145. || [ ! -f "$(PROJECT_ROOT)/contract/dao/darkfi_dao_contract.wasm" ] \
  146. || [ ! -f "$(PROJECT_ROOT)/contract/deployooor/darkfi_deployooor_contract.wasm" ]; then \
  147. echo "Building contracts..."; \
  148. $(MAKE) -C "$(PROJECT_ROOT)" contracts; \
  149. fi
  150. # Stop the running network
  151. # Usage: make stop [suppress_not_running=1]
  152. stop:
  153. @if [ -f PIDs.txt ]; then \
  154. while read PID; do \
  155. if ps -p $$PID > /dev/null 2>&1; then \
  156. kill -15 $$PID 2>/dev/null; \
  157. sleep 5; \
  158. ps -p $$PID > /dev/null 2>&1 && kill -9 $$PID 2>/dev/null; \
  159. fi; \
  160. done < PIDs.txt; \
  161. rm -f PIDs.txt; \
  162. echo "Stopped explorer node environment"; \
  163. else \
  164. if [ "$(suppress_not_running)" != "1" ]; then \
  165. echo "Explorer node environment not running, nothing to stop."; \
  166. fi; \
  167. fi
  168. # Wait for the network to start
  169. await-startup-%:
  170. @$(call wait_for_darkfid_startup,$(LOG_HOME)/$*)
  171. @$(call wait_for_explorerd_startup,$(LOG_HOME)/$*)
  172. # Wait for the explorer node environment to start in no-sync mode (skip waiting for darkfid startup)
  173. await-startup-no-sync-%:
  174. @$(call wait_for_explorerd_startup,$(LOG_HOME)/$*)
  175. # Waits for Darkfid to start
  176. define wait_for_darkfid_startup
  177. log_dir=$(strip $(1)); \
  178. while ! grep -q "Blockchain synced!" "$$log_dir/darkfid.log" 2>/dev/null; do \
  179. sleep 1; \
  180. done;
  181. endef
  182. # Waits for Explorerd to start
  183. define wait_for_explorerd_startup
  184. log_dir=$(strip $(1)); \
  185. while ! grep -q "Started DarkFi Explorer Node" "$$log_dir/explorerd.log" 2>/dev/null; do \
  186. sleep 1; \
  187. done;
  188. endef
  189. .PHONY: help all clean install uninstall bundle_contracts_src check-darkfid check-explorerd stop start-%