Skip to content

Feat/reward multiwallet #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
PRIVKEY=cUdVo9US5MwknUaHjSUu64iJD2DNtPv11Z8chgZHtLfydyaJqUsS
MINERENABLED=1
SIGNETCHALLENGE=51210232cc77c0e1ac931c691d2bd5e413523e9a284e96b7056cc202f119634465e9c551ae
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.env
16 changes: 12 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM debian:buster-slim as builder

ARG BITCOIN_VERSION=${BITCOIN_VERSION:-27.0}
ARG BITCOIN_VERSION=${BITCOIN_VERSION:-d8434da3c14e}

ARG TARGETPLATFORM

Expand All @@ -14,7 +14,7 @@ WORKDIR /tmp
linux/arm64) \
echo "arm64" && export TRIPLET="aarch64-linux-gnu";; \
esac && \
BITCOIN_URL="https://bitcoincore.org/bin/bitcoin-core-${BITCOIN_VERSION}/bitcoin-${BITCOIN_VERSION}-${TRIPLET}.tar.gz" && \
BITCOIN_URL="https://github.com/benthecarman/bitcoin/releases/download/custom-signet-blocktime/bitcoin-${BITCOIN_VERSION}-${TRIPLET}.tar.gz" && \
BITCOIN_FILE="bitcoin-${BITCOIN_VERSION}-${TRIPLET}.tar.gz" && \
wget -qO "${BITCOIN_FILE}" "${BITCOIN_URL}" && \
mkdir -p bin && \
Expand Down Expand Up @@ -45,22 +45,30 @@ ENV ZMQPUBRAWBLOCK=${ZMQPUBRAWBLOCK:-"tcp://0.0.0.0:28332"}
ENV ZMQPUBRAWTX=${ZMQPUBRAWTX:-"tcp://0.0.0.0:28333"}
ENV ZMQPUBHASHBLOCK=${ZMQPUBHASHBLOCK:-"tcp://0.0.0.0:28334"}

ENV RPCTHREADS=${RPCTHREADS:-"16"}
ENV RPCSERVERTIMEOUT=${RPCSERVERTIMEOUT:-"600"}
ENV RPCWORKQUEUE=${RPCWORKQUEUE:-"50"}


ENV RPCBIND=${RPCBIND:-"0.0.0.0:38332"}
ENV RPCALLOWIP=${RPCALLOWIP:-"0.0.0.0/0"}
ENV WHITELIST=${WHITELIST:-"0.0.0.0/0"}
ENV ADDNODE=${ADDNODE:-""}
ENV BLOCKPRODUCTIONDELAY=${BLOCKPRODUCTIONDELAY:-""}
ENV BLOCKPRODUCTIONDELAY=${BLOCKPRODUCTIONDELAY:-"30"}
ENV MINERENABLED=${MINERENABLED:-"1"}
ENV MINETO=${MINETO:-""}
ENV EXTERNAL_IP=${EXTERNAL_IP:-""}

# Variable used to generate wallets used by bridge operators
ENV NUM_WALLETS=${NUM_WALLETS:-5}
VOLUME $BITCOIN_DIR
EXPOSE 28332 28333 28334 38332 38333 38334
RUN apt-get update && \
apt-get install -qq --no-install-recommends procps python3 python3-pip jq && \
apt-get clean
COPY --from=builder "/tmp/bin" /usr/local/bin
COPY docker-entrypoint.sh /usr/local/bin/entrypoint.sh
COPY setup-mineto-address.sh /usr/local/bin/setup-mineto-address.sh
COPY miner_imports /usr/local/bin
COPY miner /usr/local/bin/miner
COPY *.sh /usr/local/bin/
Expand All @@ -69,4 +77,4 @@ RUN pip3 install setuptools

ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]

CMD ["run.sh"]
CMD ["run.sh"]
43 changes: 43 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
services:
bitcoind-miner:
build:
context: .
dockerfile: Dockerfile

container_name: bitcoin-miner
image: public.ecr.aws/s6b4k6i9/strata/signet:mw0.0.2
env_file:
.env
environment:
BLOCKPRODUCTIONDELAY: 10
RPCUSER: user
RPCPASSWORD: password
PRIVKEY: $PRIVKEY
SIGNETCHALLENGE: $SIGNETCHALLENGE
MINERENABLED: $MINERENABLED
ports:
- "28332:28332"
- "28333:28333"
- "28334:28334"
- "38332:38332"
- "38333:38333"
- "38334:38334"

bitcoind-fullnode:
build:
context: .
dockerfile: Dockerfile
container_name: bitcoin-fullnode
image: public.ecr.aws/s6b4k6i9/strata/signet:mw0.0.2

environment:
BLOCKPRODUCTIONDELAY: 10
RPCPASSWORD: password
SIGNETCHALLENGE: 512102a772f06f3860f4c1b803734db603f1c0cd86601a27670aadf5ff364a1d34f92551ae
MINERENABLED: 0
ADDNODE: bitcoin-miner:38333

ports:
- "38335:38332"
- "38336:38333"
- "38337:38334"
10 changes: 9 additions & 1 deletion gen-bitcoind-conf.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,19 @@ if [[ "$EXTERNAL_IP" != "" ]]; then
done
fi

echo "rpcthreads=$RPCTHREADS
rpcservertimeout=$RPCSERVERTIMEOUT
rpcworkqueue=$RPCWORKQUEUE"

echo "[signet]
rest=1
daemon=1
listen=1
server=1
acceptnonstdtxn=1
v2transport=1
discover=1
signetblocktime=$BLOCKPRODUCTIONDELAY
signetchallenge=$SIGNETCHALLENGE
zmqpubrawblock=$ZMQPUBRAWBLOCK
zmqpubrawtx=$ZMQPUBRAWTX
Expand Down Expand Up @@ -60,4 +68,4 @@ fi

if [[ "$TORCONTROL" != "" ]]; then
echo "torcontrol=$TORCONTROL"
fi
fi
2 changes: 1 addition & 1 deletion gen-signet-keys.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ if [[ "$MINERENABLED" == "1" && ("$SIGNETCHALLENGE" == "" || "$PRIVKEY" == "") ]
rpcpassword=bitcoin
" >$DATADIR/bitcoin.conf
#start daemon
$BITCOIND -wallet="temp"
$BITCOIND -wallet="temp" -deprecatedrpc=create_bdb
#wait a bit for startup
sleep 5s
#create wallet
Expand Down
3 changes: 2 additions & 1 deletion install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ echo "Generate bitcoind configuration"
gen-bitcoind-conf.sh >~/.bitcoin/bitcoin.conf
echo "Setup Signet"
setup-signet.sh

echo "Setup Mineto address(es)"
setup-mineto-address.sh
if [[ "$MINE_GENESIS" == "1" ]]; then
echo "Mine Genesis Block"
mine-genesis.sh
Expand Down
26 changes: 23 additions & 3 deletions mine.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@
#!/bin/bash
source setup-mineto-address.sh

NBITS=${NBITS:-"1e0377ae"} #minimum difficulty in signet
ADDR=$(get_first_address) # It will get the first address from the wallet ie. custom_signet

echo "Starting mining with address $ADDR"

# Initial mining of 100 blocks if blocks count is less than 100
BLOCKS_COUNT=$(bitcoin-cli -rpcwallet=custom_signet getblockcount)
if [[ $BLOCKS_COUNT -lt 100 ]]; then
echo "Mining initial 100 blocks"
for ((i = BLOCKS_COUNT; i <= 100; i++)); do
echo "Minining initial block $i"
miner --cli="bitcoin-cli -rpcwallet=custom_signet" generate --grind-cmd="bitcoin-util grind" --address=$ADDR --nbits=$NBITS --set-block-time=$(date +%s)
done
else
echo "Starting bitcoind mining from block $BLOCKS_COUNT"
fi


while true; do
ADDR=${MINETO:-$(bitcoin-cli getnewaddress)}
# Get address to receive miner reward.
ADDR=$(get_next_address)
if [[ -f "${BITCOIN_DIR}/BLOCKPRODUCTIONDELAY.txt" ]]; then
BLOCKPRODUCTIONDELAY_OVERRIDE=$(cat ~/.bitcoin/BLOCKPRODUCTIONDELAY.txt)
echo "Delay OVERRIDE before next block" $BLOCKPRODUCTIONDELAY_OVERRIDE "seconds."
Expand All @@ -15,5 +34,6 @@ while true; do
fi
fi
echo "Mine To:" $ADDR
miner --cli="bitcoin-cli" generate --grind-cmd="bitcoin-util grind" --address=$ADDR --nbits=$NBITS --set-block-time=$(date +%s)
done
# We must specify rpcwallet when multiple wallets are loaded
miner --cli="bitcoin-cli -rpcwallet=custom_signet" generate --grind-cmd="bitcoin-util grind" --address=$ADDR --nbits=$NBITS --set-block-time=$(date +%s)
done
2 changes: 1 addition & 1 deletion run.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash

# run bitcoind
bitcoind --daemonwait
bitcoind --daemonwait -deprecatedrpc=create_bdb
sleep 5
echo "get magic"
magic=$(cat /root/.bitcoin/signet/debug.log | grep -m1 magic)
Expand Down
108 changes: 108 additions & 0 deletions setup-mineto-address.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#!/bin/bash
# This script creates n different wallets and addresses to receive miner reward.
# For generated n wallets, each will receive rewards in the order they are
# specified in a `ADDRESS.txt` file.
# Index of reward receiving wallet for current round will be maintained in a `MINETO_IDX`
# text file.


DATADIR=${DATADIR:-~/.bitcoin}
# Number of wallets to create (you can change this)
NUM_WALLETS=${NUM_WALLETS:-5}

# CSV file to store wallet names and addresses
OUTPUT_FILE="$DATADIR/wallets.csv"
INDEX_FILE="$DATADIR/MINETO_IDX"

# Check if a wallet already exists
wallet_exists() {
local wallet_name=$1
if bitcoin-cli -datadir="$DATADIR" listwalletdir | grep -q "\"$wallet_name\""; then
return 0 # Wallet exists
else
return 1 # Wallet does not exist
fi
}

if [ ! -f "$OUTPUT_FILE" ]; then

# Generate an address for custom_signet wallet which is used by sequencer
# to add btc txs.
ADDRESS=$(bitcoin-cli -datadir="$DATADIR" -rpcwallet="custom_signet" getnewaddress)

# Add the custom_signet as our first address
# This is needed for backward compatibility
echo "custom_signet,$ADDRESS" > "$OUTPUT_FILE"

# Create wallets, generate addresses, and store in CSV
for ((i=1; i<=NUM_WALLETS; i++)); do
WALLET_NAME="wallet_$i"
if wallet_exists "$WALLET_NAME"; then
echo "Wallet $WALLET_NAME already exists. Skipping..."
else
bitcoin-cli -datadir="$DATADIR" -named createwallet wallet_name="$WALLET_NAME" load_on_startup=true descriptors=false
fi
# Generate new address since we do not already have the wallets.csv
ADDRESS=$(bitcoin-cli -datadir="$DATADIR" -rpcwallet="$WALLET_NAME" getnewaddress)

echo "$WALLET_NAME,$ADDRESS" >> "$OUTPUT_FILE"
echo "Created wallet: $WALLET_NAME with address: $ADDRESS"
done

echo "All wallets created and addresses saved in $OUTPUT_FILE."
else
echo "wallet list already exists"
fi



# Function to load addresses from the CSV file into an array
load_addresses() {
addresses=()
while IFS=',' read -r wallet address; do
# Skip the header line
if [[ "$wallet" != "wallet_name" ]]; then
addresses+=("$address")
fi
done < "$OUTPUT_FILE"
}


# Initialize first address as the Mineto address
# Function to get the next address to receive mining rewards

get_next_address() {
load_addresses

# Check if the addresses array is empty
if [ ${#addresses[@]} -eq 0 ]; then
echo "Error: No addresses found in $OUTPUT_FILE"
exit 1
fi

# Read the current index from the file, or initialize to 0 if not found
if [ -f "$INDEX_FILE" ]; then
current_index=$(cat "$INDEX_FILE")
else
current_index=0
fi

# Get the address at the current index
next_address=${addresses[$current_index]}

# Increment the index, wrapping around if necessary
new_index=$(( (current_index + 1) % ${#addresses[@]} ))

# Save the updated index back to the file
echo "$new_index" > "$INDEX_FILE"

# Return the next address
echo "$next_address"
}
# echo "Next Address" $(get_next_address)

# get first address
get_first_address() {
load_addresses
echo ${addresses[0]}
}
2 changes: 1 addition & 1 deletion setup-signet.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
PRIVKEY=${PRIVKEY:-$(cat ~/.bitcoin/PRIVKEY.txt)}
DATADIR=${DATADIR:-~/.bitcoin/}
bitcoind -datadir=$DATADIR --daemonwait -persistmempool
bitcoind -datadir=$DATADIR --daemonwait -persistmempool -deprecatedrpc=create_bdb
bitcoin-cli -datadir=$DATADIR -named createwallet wallet_name="custom_signet" load_on_startup=true descriptors=false

#only used in case of mining node
Expand Down