forked from botanix-labs/Macbeth
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup-local-env.sh
More file actions
executable file
·179 lines (149 loc) · 5.51 KB
/
setup-local-env.sh
File metadata and controls
executable file
·179 lines (149 loc) · 5.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/bin/bash
set -e
# Load environment variables from .env file if it exists
if [ -f .env ]; then
echo "Loading configuration from .env file..."
set -a
source .env
set +a
fi
NUM_NODES=${NUM_NODES:-3}
MIN_SIGNERS=${MIN_SIGNERS:-2}
MAX_SIGNERS=${MAX_SIGNERS:-3}
OUTPUT_PATH=${OUTPUT_PATH:-"docker-local"}
CONFIG_PATH=${CONFIG_PATH:-"docker-local/configs"}
DOCKER_SUBNET=${DOCKER_SUBNET:-"172.22.0.0/16"}
PROJECT_PREFIX=${PROJECT_PREFIX:-"botanix"}
while [[ $# -gt 0 ]]; do
case $1 in
--num-nodes=*)
NUM_NODES="${1#*=}"
shift
;;
--output-path=*)
OUTPUT_PATH="${1#*=}"
shift
;;
--min-signers=*)
MIN_SIGNERS="${1#*=}"
shift
;;
--max-signers=*)
MAX_SIGNERS="${1#*=}"
shift
;;
--subnet=*)
DOCKER_SUBNET="${1#*=}"
shift
;;
--prefix=*)
PROJECT_PREFIX="${1#*=}"
shift
;;
*)
echo "Unknown option: $1"
echo "Usage: $0 [--num-nodes=N] [--output-path=PATH] [--min-signers=N] [--max-signers=N] [--subnet=CIDR] [--prefix=NAME]"
exit 1
;;
esac
done
echo "Setting up local docker environment..."
echo " Nodes: $NUM_NODES"
echo " Output: $OUTPUT_PATH"
echo " Config: $CONFIG_PATH"
echo " Min Signers: $MIN_SIGNERS"
echo " Max Signers: $MAX_SIGNERS"
echo " Subnet: $DOCKER_SUBNET"
echo ""
mkdir -p "$OUTPUT_PATH"
if [ -d "$CONFIG_PATH" ]; then
echo "Cleaning existing config directory..."
rm -rf "$CONFIG_PATH"
fi
echo "Generating node configurations..."
cargo run -p botanix-up -- \
--num-nodes=$NUM_NODES \
--output-path=$CONFIG_PATH \
--multisig-min-signers=$MIN_SIGNERS \
--multisig-max-signers=$MAX_SIGNERS \
--docker-subnet=$DOCKER_SUBNET \
--project-name-prefix=$PROJECT_PREFIX
echo ""
echo "Configuration generated successfully in $CONFIG_PATH"
echo "Fixing genesis.json files..."
MASTER_GENESIS="$CONFIG_PATH/node-1/cometbft/config/genesis.json"
if [ -f "$MASTER_GENESIS" ]; then
echo " Using node-1 genesis as master"
for NODE_DIR in "$CONFIG_PATH"/node-*/cometbft/config; do
if [ "$NODE_DIR" != "$CONFIG_PATH/node-1/cometbft/config" ]; then
cp "$MASTER_GENESIS" "$NODE_DIR/genesis.json"
echo "Copied genesis to $(basename $(dirname $(dirname $NODE_DIR)))"
fi
done
else
echo " Warning: Master genesis file not found"
fi
echo "Fixing persistent_peers in config.toml files..."
for NODE_DIR in "$CONFIG_PATH"/node-*/cometbft/config; do
if [ -f "$NODE_DIR/config.toml" ]; then
sed -i.bak 's/^persistent_peers = .*/persistent_peers = ""/' "$NODE_DIR/config.toml"
echo " Cleared persistent_peers in $(basename $(dirname $(dirname $NODE_DIR)))"
fi
done
NETWORK_NAME="${PROJECT_PREFIX}-local"
if ! docker network inspect $NETWORK_NAME > /dev/null 2>&1; then
echo "Creating docker network: $NETWORK_NAME"
docker network create --subnet=$DOCKER_SUBNET $NETWORK_NAME
else
echo "Docker network $NETWORK_NAME already exists"
fi
echo "Generating docker-compose.yml..."
bash generate-compose.sh $NUM_NODES $OUTPUT_PATH $CONFIG_PATH $PROJECT_PREFIX
echo "Generating bitcoin initialization script..."
cat > "$OUTPUT_PATH/init-bitcoin.sh" << 'INIT_SCRIPT'
#!/bin/bash
set -e
WALLET_NAME="testwallet"
BLOCKS_TO_GENERATE=1000
echo "Waiting for bitcoin-core to be ready..."
until docker exec bitcoin-core bitcoin-cli -regtest -rpcuser=foo -rpcpassword=bar getblockchaininfo &> /dev/null; do
echo "Bitcoin node not ready yet, waiting..."
sleep 2
done
echo "Bitcoin node is ready!"
# Check if wallet already exists
if docker exec bitcoin-core bitcoin-cli -regtest -rpcuser=foo -rpcpassword=bar listwallets | grep -q "$WALLET_NAME"; then
echo "Wallet '$WALLET_NAME' already exists"
else
echo "Creating wallet '$WALLET_NAME'..."
docker exec bitcoin-core bitcoin-cli -regtest -rpcuser=foo -rpcpassword=bar createwallet "$WALLET_NAME"
fi
# Load wallet if not loaded
echo "Loading wallet '$WALLET_NAME'..."
docker exec bitcoin-core bitcoin-cli -regtest -rpcuser=foo -rpcpassword=bar loadwallet "$WALLET_NAME" 2>/dev/null || echo "Wallet already loaded"
echo "Generating address..."
ADDRESS=$(docker exec bitcoin-core bitcoin-cli -regtest -rpcuser=foo -rpcpassword=bar -rpcwallet="$WALLET_NAME" getnewaddress)
echo "Generated address: $ADDRESS"
echo "Generating $BLOCKS_TO_GENERATE blocks to $ADDRESS..."
docker exec bitcoin-core bitcoin-cli -regtest -rpcuser=foo -rpcpassword=bar -rpcwallet="$WALLET_NAME" generatetoaddress $BLOCKS_TO_GENERATE "$ADDRESS"
BLOCK_COUNT=$(docker exec bitcoin-core bitcoin-cli -regtest -rpcuser=foo -rpcpassword=bar getblockcount)
echo "Current block height: $BLOCK_COUNT"
BALANCE=$(docker exec bitcoin-core bitcoin-cli -regtest -rpcuser=foo -rpcpassword=bar -rpcwallet="$WALLET_NAME" getbalance)
echo "Wallet balance: $BALANCE BTC"
echo ""
echo "Bitcoin initialization complete!"
echo "Wallet: $WALLET_NAME"
echo "Address: $ADDRESS"
echo "Blocks: $BLOCK_COUNT"
echo "Balance: $BALANCE BTC"
INIT_SCRIPT
chmod +x "$OUTPUT_PATH/init-bitcoin.sh"
echo ""
echo "Setup complete!"
echo ""
echo "To start the environment:"
echo " 1. Start services: docker compose -f $OUTPUT_PATH/docker-compose-generated.yml up -d"
echo " 2. Initialize bitcoin: $OUTPUT_PATH/init-bitcoin.sh"
echo ""
echo "Or run both in one command:"
echo " docker compose -f $OUTPUT_PATH/docker-compose-generated.yml up -d && sleep 5 && $OUTPUT_PATH/init-bitcoin.sh"