You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docker/bitcoin/miner.sh
+81-65Lines changed: 81 additions & 65 deletions
Original file line number
Diff line number
Diff line change
@@ -1,46 +1,46 @@
1
+
#!/bin/env bash
2
+
1
3
set -e
2
4
trap"exit" INT TERM
3
5
trap"kill 0" EXIT
4
6
5
-
6
-
DEFAULT_TIMEOUT=$(($(date +%s) +30))## set the default mining timeout to the current epoch +30s
7
-
DEFINED_ADDRESSES=$(compgen -A variable | grep "STACKS.*.BTC_ADDR")## retrieve env vars matching STACKS*BTC_ADDRESS
8
-
DEFINED_WALLETS=$(compgen -A variable | grep "STACKS.*.BTC_WALLET")## retrieve env vars matching STACKS*BTC_WALLET
9
-
mapfile -t ADDRESSES <<(printf '%s\n'"$DEFINED_ADDRESSES"| tr '''\n')## convert the compgen output to an array
10
-
mapfile -t WALLETS <<(printf '%s\n'"$DEFINED_WALLETS"| tr '''\n')## convert the compgen output to an array
11
-
NUM_MINERS=${#ADDRESSES[@]}## use the same value for total miners throughout script
12
-
RESERVED_BLOCKS=100 ## during initial block mining, reserve 100 blocks to mine at the end so the earlier blocks have received rewards by the epoch 2.0 block
7
+
DEFAULT_TIMEOUT=$(($(date +%s) +30))# set the default mining timeout to the current epoch +30s
8
+
DEFINED_ADDRESSES=$(compgen -A variable | grep "STACKS.*.BTC_ADDR")# retrieve env vars matching STACKS*BTC_ADDRESS
9
+
DEFINED_WALLETS=$(compgen -A variable | grep "STACKS.*.BTC_WALLET")# retrieve env vars matching STACKS*BTC_WALLET
10
+
mapfile -t ADDRESSES <<(printf '%s\n'"$DEFINED_ADDRESSES"| tr '''\n')# convert the compgen output to an array
11
+
mapfile -t WALLETS <<(printf '%s\n'"$DEFINED_WALLETS"| tr '''\n')# convert the compgen output to an array
12
+
NUM_MINERS=${#ADDRESSES[@]}# use the same value for total miners throughout script
13
+
RESERVED_BLOCKS=100 # during initial block mining, reserve 100 blocks to mine at the end so the earlier blocks have received rewards by the epoch 2.0 block
13
14
14
15
15
16
functionget_height(){
16
-
## return the current block height, or -1 in case of error
## if wallet db exists, but wallet is not loaded: this will lead to failure of the script since the wallet cannot be created since it is on disk but not loaded in memory
36
-
## note: the health check in the docker compose file should avoid this scenario, since stacks-miner services will wait until the epoch 2.05 block before starting
35
+
# returns if a wallet exists
36
+
# if wallet db exists, but wallet is not loaded: this will lead to failure of the script since the wallet cannot be created since it is on disk but not loaded in memory
37
+
# note: the health check in the docker compose file should avoid this scenario, since stacks-miner services will wait until the epoch 2.05 block before starting
echo"In Epoch2.5, sleeping for ${MINE_INTERVAL_EPOCH25} ... "
163
166
sleep_duration=${MINE_INTERVAL_EPOCH25}
164
167
fi
165
168
echo"Current btc height: ${block_height}"
166
169
echo"total mined blocks: ${mined_block_counter}"
167
170
echo"sleeping for ${sleep_duration}s"
168
-
sleep ${sleep_duration}&
171
+
sleep "${sleep_duration}"&
169
172
wait||exit 0
170
173
done
171
174
true
@@ -178,11 +181,9 @@ function init(){
178
181
echo"Waiting indefinitely for a return from 'bitcoin-cli getmininginfo'"
179
182
sleep 1
180
183
done
181
-
182
184
local mineable_blocks=$(( (STACKS_2_05_HEIGHT -1) - RESERVED_BLOCKS ))# calculate the total number of blocks to allocate to the defined stacks-miner wallets
183
185
local remainder_blocks=0 # set the initial remainder as zero before calculating if there is a modulus
184
186
local mined_counter=0 # keep track of initial mined blocks
185
-
local address_count=0 # keep track of how many addresses have an initial balance
186
187
blocks_per_miner=$(( mineable_blocks / NUM_MINERS ))# how many blocks per miner address
187
188
remainder_blocks=$(( (STACKS_2_05_HEIGHT -1) - (blocks_per_miner * NUM_MINERS + RESERVED_BLOCKS) ))# if there is a modulus, we need to mine them to the last miner address
188
189
foriin$(seq 0 $((NUM_MINERS -1)));do
@@ -195,40 +196,54 @@ function init(){
195
196
if [ "$i"-eq$((NUM_MINERS -1)) ];then
196
197
blocks_per_miner=$((blocks_per_miner + remainder_blocks))# this is the last miner address. add the modulus to the defined number of blocks for all miner
197
198
fi
198
-
if! get_wallet_info ${btc_wallet};then## Check if a wallet is loaded in memory
199
-
if! create_wallet ${btc_wallet};then## Create a wallet if one is not loaded in memory (if not in memory, but on disk...this will break the script)
199
+
# Check if a wallet is loaded in memory
200
+
if! get_wallet_info "${btc_wallet}";then
201
+
# Create a wallet if one is not loaded in memory (if not in memory, but on disk...this will break the script)
202
+
if! create_wallet "${btc_wallet}";then
200
203
echo"ERROR creating wallet (${btc_wallet})"
201
-
exit 1 ## Exit if wallet creation returns a failure
204
+
# Exit if wallet creation returns a failure
205
+
exit 1
202
206
fi
203
207
fi
204
-
if! get_address_info ${btc_wallet}${btc_address};then## Check if a specified address is loaded in a specific wallet
205
-
if! import_address ${btc_wallet}${btc_address};then## Import an address into a wallet
208
+
# Check if a specified address is loaded in a specific wallet
echo"ERROR importing address (${btc_address}) into wallet (${btc_wallet})"
207
-
exit 1 ## Exit if address import returns a failure
213
+
# Exit if address import returns a failure
214
+
exit 1
208
215
fi
209
216
fi
210
-
mine_blocks ${btc_wallet}${btc_address}${blocks_per_miner}## mined the initial balance per address (102 blocks / number of miners) per address
211
-
mined_counter=$((mined_counter + blocks_per_miner))## keep track of how many blocks were mined in this stage
217
+
mine_blocks "${btc_wallet}""${btc_address}""${blocks_per_miner}"# mined the initial balance per address (102 blocks / number of miners) per address
218
+
mined_counter=$((mined_counter + blocks_per_miner))# keep track of how many blocks were mined in this stage
212
219
done
213
220
echo""
214
221
echo"******************************************"
215
-
local depositor_blocks=$(((STACKS_2_05_HEIGHT -1) - mined_counter))## this should be equal to reserved_blocks (100). this is needed for the stacks-miner wallet address to have mature rewards
222
+
local depositor_blocks=$(((STACKS_2_05_HEIGHT -1) - mined_counter))# this should be equal to reserved_blocks (100). this is needed for the stacks-miner wallet address to have mature rewards
216
223
echo"btc_wallet: depositor"
217
224
echo"btc_address: tbd"
218
-
if! get_wallet_info depositor;then## Check if depositor wallet is loaded in memory
219
-
if! create_wallet depositor;then## Create depositor wallet if one is not loaded in memory (if not in memory, but on disk...this will break the script)
225
+
local depositor_addr
226
+
# Check if depositor wallet is loaded in memory
227
+
if! get_wallet_info depositor;then
228
+
# Create depositor wallet if one is not loaded in memory (if not in memory, but on disk...this will break the script)
229
+
if! create_wallet depositor;then
220
230
echo"Error creating depositor wallet"
221
-
exit 1 ## Exit if depositor wallet creation returns a failure
231
+
# Exit if depositor wallet creation returns a failure
echo"ERROR importing address (depositor) into wallet (${depositor_addr})"
228
-
exit 1 ## Exit if depositor address import returns a failure
241
+
# Exit if depositor address import returns a failure
242
+
exit 1
229
243
fi
230
244
fi
231
-
mine_blocks "depositor"${depositor_addr}${depositor_blocks}## mine blocks to the depositor address (should be 100 blocks so stacks-miner blocks are mature for epoch 2.0)
245
+
# mine blocks to the depositor address (should be 100 blocks so stacks-miner blocks are mature for epoch 2.0)
0 commit comments