Skip to content

Commit

Permalink
Refactor: Makefile (#177)
Browse files Browse the repository at this point in the history
* refactor: makefile updated as per latest bootstraper commit

* refactor: deleted extra files

* changelog and linting

---------

Co-authored-by: mohiiit <[email protected]>
  • Loading branch information
Mohiiit and mohiiit authored Nov 6, 2024
1 parent 4d61e3a commit ac131a5
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 29 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).

## Changed

- refactor: update json made generic to update any json file
- refactor: makefile updated as per bootstraper changes
- removed error return in case of JobAlreadyExists in `create_job` function
- update_job returns the updated job item
- made create_job atomic to avoid race conditions
Expand Down
40 changes: 22 additions & 18 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ ORCHESTRATOR_PATH := $(shell pwd)
# Bootstrapper
OPERATOR_ADDRESS := 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266
BOOTSTRAP_OUTPUT_PATH := $(shell pwd)/build/bootstrap.json
BOOTSTRAPPER_COMMIT := b0b647500c2ae3e3b0d99e345fa652989bca4726
BOOTSTRAP_INPUT_PATH := $(shell pwd)/bootstrap_input.json
BOOTSTRAPPER_COMMIT := f717bf179581da53d68fee03b50ef78e0628ee20
BOOTSTRAPPER_PATH := $(shell pwd)/madara-bootstrapper
VERIFIER_ADDRESS := 0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512

Expand All @@ -25,6 +26,7 @@ ETHEREUM_WSS_RPC_URL := wss://eth-sepolia.g.alchemy.com/v2/${ETHEREUM_API_KEY}
# Environment file
ENV_FILE := $(shell pwd)/.makefile.json
PID_FILE := $(shell pwd)/.pids.json
JSON_UPDATER := $(shell pwd)/scripts/save_json.sh

.PHONY: all anvil madara core-contract update-madara eth-bridge pathfinder orchestrator cleanup

Expand All @@ -42,9 +44,10 @@ define kill_pid
./scripts/kill_pid.sh $1
endef

define save_json
echo "saving json\n"
sh ./scripts/save_json.sh $1 $2
# function to update JSON
# Usage: $(call update-json-safe,<json-file>,<key>,<value>)
define update-json-safe
$(JSON_UPDATER) $(1) $(2) $(3)
endef

setup:
Expand All @@ -68,7 +71,7 @@ setup:
@make eth-bridge

# we need to sleep for a little as it's possible the block hasn't been sealed yet
@sleep 10
@sleep 20

@echo "Terminating previous Madara instance..."
$(call kill_pid,madara)
Expand Down Expand Up @@ -108,7 +111,7 @@ anvil:
define update_core_contract_address
echo "Updating core contract address in YAML..."
if [ -f "$(ENV_FILE)" ]; then \
export CORE_CONTRACT_ADDRESS=$$(jq -r '.CORE_CONTRACT_ADDRESS' $(ENV_FILE)) && \
export CORE_CONTRACT_ADDRESS=$$(jq -r '.core_contract_address' $(BOOTSTRAP_INPUT_PATH)) && \
if [ -n "$$CORE_CONTRACT_ADDRESS" ]; then \
yq e '.eth_core_contract_address = strenv(CORE_CONTRACT_ADDRESS)' -i $(MADARA_PATH)/configs/presets/devnet.yaml; \
echo "Core contract address updated in YAML."; \
Expand All @@ -129,13 +132,16 @@ madara-bootstrap-mode:
cargo run --release -- --name madara --base-path $(MADARA_DATA_PATH) --rpc-port 9944 --rpc-cors "*" --rpc-external --sequencer --chain-config-path configs/presets/devnet.yaml --feeder-gateway-enable --gateway-enable --gateway-external --gas-price 0 --blob-gas-price 0 --rpc-methods unsafe --no-l1-sync

core-contract:
$(call update-json-safe,$(BOOTSTRAP_INPUT_PATH),operator_address,$(OPERATOR_ADDRESS)) && \
$(call update-json-safe,$(BOOTSTRAP_INPUT_PATH),verifier_address,$(VERIFIER_ADDRESS)) && \
echo "values with which we are updating the bootstrapper input file" && \
cd $(BOOTSTRAPPER_PATH) && \
rm -f $(BOOTSTRAP_OUTPUT_PATH) && \
git checkout $(BOOTSTRAPPER_COMMIT) && \
RUST_LOG=debug cargo run --release -- --mode core --operator-address $(OPERATOR_ADDRESS) --output-file $(BOOTSTRAP_OUTPUT_PATH) --verifier-address $(VERIFIER_ADDRESS) --config-hash-version "StarknetOsConfig2" && \
cat $(BOOTSTRAP_OUTPUT_PATH) && \
$(call save_json,"CORE_CONTRACT_ADDRESS",$$(jq -r .starknet_contract_address $(BOOTSTRAP_OUTPUT_PATH))) && \
$(call save_json,"CORE_CONTRACT_IMPLEMENTATION_ADDRESS",$$(jq -r .starknet_contract_implementation_address $(BOOTSTRAP_OUTPUT_PATH)))
RUST_LOG=debug cargo run --release -- --mode setup-l1 --config $(BOOTSTRAP_INPUT_PATH) --output-file $(BOOTSTRAP_OUTPUT_PATH) && \
$(call update-json-safe,$(BOOTSTRAP_INPUT_PATH),core_contract_address,$$(jq -r .starknet_contract_address $(BOOTSTRAP_OUTPUT_PATH))) && \
$(call update-json-safe,$(BOOTSTRAP_INPUT_PATH),core_contract_implementation_address,$$(jq -r .starknet_contract_implementation_address $(BOOTSTRAP_OUTPUT_PATH)))

madara:
$(call update_core_contract_address) && \
Expand All @@ -146,17 +152,15 @@ madara:
eth-bridge:
cd $(BOOTSTRAPPER_PATH) && \
git checkout $(BOOTSTRAPPER_COMMIT) && \
export CORE_CONTRACT_ADDRESS=$$(jq -r '.CORE_CONTRACT_ADDRESS' $(ENV_FILE)) && \
export CORE_CONTRACT_IMPLEMENTATION_ADDRESS=$$(jq -r '.CORE_CONTRACT_IMPLEMENTATION_ADDRESS' $(ENV_FILE)) && \
RUST_LOG=debug cargo run --release -- --mode eth-bridge --core-contract-address $$CORE_CONTRACT_ADDRESS --core-contract-implementation-address $$CORE_CONTRACT_IMPLEMENTATION_ADDRESS --output-file $(BOOTSTRAP_OUTPUT_PATH) && \
$(call save_json,"L1_BRIDGE_ADDRESS","$$(jq -r .eth_bridge_setup_outputs.l1_bridge_address $(BOOTSTRAP_OUTPUT_PATH))") && \
$(call save_json,"L2_ETH_TOKEN_ADDRESS","$$(jq -r .eth_bridge_setup_outputs.l2_eth_proxy_address $(BOOTSTRAP_OUTPUT_PATH))") && \
$(call save_json,"L2_ETH_BRIDGE_ADDRESS","$$(jq -r .eth_bridge_setup_outputs.l2_eth_bridge_proxy_address $(BOOTSTRAP_OUTPUT_PATH))")
RUST_LOG=debug cargo run --release -- --mode eth-bridge --config $(BOOTSTRAP_INPUT_PATH) --output-file $(BOOTSTRAP_OUTPUT_PATH) && \
$(call update-json-safe,$(ENV_FILE),L1_BRIDGE_ADDRESS,$$(jq -r .eth_bridge_setup_outputs.l1_bridge_address $(BOOTSTRAP_OUTPUT_PATH))) && \
$(call update-json-safe,$(ENV_FILE),L2_ETH_TOKEN_ADDRESS,$$(jq -r .eth_bridge_setup_outputs.l2_eth_proxy_address $(BOOTSTRAP_OUTPUT_PATH))) && \
$(call update-json-safe,$(ENV_FILE),L2_ETH_BRIDGE_ADDRESS,$$(jq -r .eth_bridge_setup_outputs.l2_eth_bridge_proxy_address $(BOOTSTRAP_OUTPUT_PATH)))

udc:
cd $(BOOTSTRAPPER_PATH) && \
git checkout $(BOOTSTRAPPER_COMMIT) && \
RUST_LOG=debug cargo run --release -- --mode udc
RUST_LOG=debug cargo run --release -- --mode udc --config $(BOOTSTRAP_INPUT_PATH)

pathfinder:
cd $(PATHFINDER_PATH) && \
Expand All @@ -173,7 +177,7 @@ orchestrator-setup:
cd $(ORCHESTRATOR_PATH) && \
npm i && \
export L1_BRIDGE_ADDRESS=$$(jq -r '.L1_BRIDGE_ADDRESS' $(ENV_FILE)) && \
export CORE_CONTRACT_ADDRESS=$$(jq -r '.CORE_CONTRACT_ADDRESS' $(ENV_FILE)) && \
export CORE_CONTRACT_ADDRESS=$$(jq -r '.core_contract_address' $(BOOTSTRAP_INPUT_PATH)) && \
export L2_ETH_TOKEN_ADDRESS=$$(jq -r '.L2_ETH_TOKEN_ADDRESS' $(ENV_FILE)) && \
export L2_ETH_BRIDGE_ADDRESS=$$(jq -r '.L2_ETH_BRIDGE_ADDRESS' $(ENV_FILE)) && \
node scripts/init_state.js $$L1_BRIDGE_ADDRESS $$CORE_CONTRACT_ADDRESS $$L2_ETH_TOKEN_ADDRESS $$L2_ETH_BRIDGE_ADDRESS
Expand Down
22 changes: 22 additions & 0 deletions bootstrap_input.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"eth_rpc": "http://127.0.0.1:8545",
"eth_priv_key": "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80",
"rollup_seq_url": "http://127.0.0.1:9944",
"rollup_priv_key": "0xabcd",
"eth_chain_id": 31337,
"l1_deployer_address": "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
"l1_wait_time": "15",
"sn_os_program_hash": "0x41fc2a467ef8649580631912517edcab7674173f1dbfa2e9b64fbcd82bc4d79",
"config_hash_version": "StarknetOsConfig2",
"app_chain_id": "MADARA_DEVNET",
"fee_token_address": "0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7",
"native_fee_token_address": "0x04718f5a0fc34cc1af16a1cdee98ffb20c31f5cd61d6ab07201858f4287c938d",
"cross_chain_wait_time": 20,
"l1_multisig_address": "0x70997970C51812dc3A010C7d01b50e0d17dc79C8",
"l2_multisig_address": "0x556455b8ac8bc00e0ad061d7df5458fa3c372304877663fa21d492a8d5e9435",
"verifier_address": "0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512",
"operator_address": "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
"dev": false,
"core_contract_address": "0x99bba657f2bbc93c02d617f8ba121cb8fc104acf",
"core_contract_implementation_address": "0x4826533b4897376654bb4d4ad88b7fafd0c98528"
}
48 changes: 38 additions & 10 deletions scripts/save_json.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,43 @@
#!/bin/bash

ENV_FILE="$(pwd)/.makefile.json"
key="$1"
value="$2"
# Check if required arguments are provided
if [ "$#" -ne 3 ]; then
echo "Usage: $0 <json_file_path> <key> <value>"
exit 1
fi

json_file="$1"
key="$2"
value="$3"

# Create directory structure if it doesn't exist
directory=$(dirname "$json_file")
mkdir -p "$directory"

# Create the environment file if it doesn't exist
if [ ! -f "$ENV_FILE" ]; then
echo '{}' > "$ENV_FILE"
# If file doesn't exist, create an empty JSON object
if [ ! -f "$json_file" ]; then
echo "{}" > "$json_file"
fi

# Use jq to update the JSON file with the new key-value pair
jq --arg key "$key" --arg value "$value" \
'.[$key] = $value' "$ENV_FILE" > "$ENV_FILE.tmp" && \
mv "$ENV_FILE.tmp" "$ENV_FILE"
# Check if the file is valid JSON
if ! jq empty "$json_file" 2>/dev/null; then
echo "Error: File exists but is not valid JSON"
exit 1
fi

# Update or add the key-value pair
# The value is treated as a string. If you need to preserve type,
# remove the quotes around "$value" in the jq command
jq --arg k "$key" --arg v "$value" '.[$k] = $v' "$json_file" > "$json_file.tmp"

# Check if jq command was successful
if [ $? -eq 0 ]; then
mv "$json_file.tmp" "$json_file"
echo "Successfully updated $json_file"
echo "Current content:"
cat "$json_file"
else
rm -f "$json_file.tmp"
echo "Error: Failed to update JSON file"
exit 1
fi

0 comments on commit ac131a5

Please sign in to comment.