Skip to content

Commit

Permalink
hotfix 🚑: Slightly more robust solution to #47, and additional enhanc…
Browse files Browse the repository at this point in the history
…ments to the shell scripts

ISSUE: #47
  • Loading branch information
emailnjv committed Nov 29, 2024
1 parent 15a2b96 commit 85d8c26
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 94 deletions.
36 changes: 26 additions & 10 deletions app/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use futures::pin_mut;
use std::str::FromStr;
use std::time::Duration;
use std::{future::Future, sync::Arc};
use bitcoin::Address;
use tracing::log::Level::{Debug, Trace};
use tracing::*;
use tracing_subscriber::{prelude::*, EnvFilter};
Expand Down Expand Up @@ -176,13 +177,13 @@ impl App {
.unwrap()
.unwrap_or(chain_spec.bitcoin_start_height);

let og_threshold = ((3 * 2) + 2) / 3;
let threshold = ((chain_spec.federation_bitcoin_pubkeys.len() * 2) + 2) / 3; // 2rds majority, rounded up
let og_bitcoin_federation = Federation::new(
chain_spec.federation_bitcoin_pubkeys[0..3].to_vec(),
og_threshold,
self.bitcoin_network,
);
let mut bitcoin_addresses = Vec::new();

fn calculate_threshold (federation_bitcoin_pubkeys_len: usize) -> usize {
(((federation_bitcoin_pubkeys_len * 2) + 2) / 3)
};

let threshold = calculate_threshold(chain_spec.federation_bitcoin_pubkeys.len()); // 2rds majority, rounded up
let bitcoin_federation = Federation::new(
chain_spec.federation_bitcoin_pubkeys.clone(),
threshold,
Expand All @@ -193,7 +194,23 @@ impl App {
bitcoin_federation.taproot_address
);

let wallet_path = self
bitcoin_addresses.push(bitcoin_federation.taproot_address.clone());

if chain_spec.federation_bitcoin_pubkeys.len() > 3 {
{
let original_federation_set = chain_spec.federation_bitcoin_pubkeys[0..3].to_vec();
let og_threshold = calculate_threshold(original_federation_set.len());
let og_bitcoin_federation = Federation::new(
original_federation_set,
og_threshold,
self.bitcoin_network,
);

bitcoin_addresses.push(og_bitcoin_federation.taproot_address.clone());
}
}

let wallet_path = self
.wallet_path
.unwrap_or(format!("{DEFAULT_ROOT_DIR}/wallet"));
let bitcoin_wallet = BitcoinWallet::new(&wallet_path, bitcoin_federation.clone())?;
Expand Down Expand Up @@ -246,8 +263,7 @@ impl App {
self.bitcoin_rpc_user.expect("RPC user is configured"),
self.bitcoin_rpc_pass.expect("RPC password is configured"),
),
og_bitcoin_federation.taproot_address.clone(),
bitcoin_federation.taproot_address.clone(),
bitcoin_addresses,
),
bitcoin_wallet,
bitcoin_signature_collector,
Expand Down
27 changes: 13 additions & 14 deletions crates/federation/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,16 @@ pub struct PegInInfo {
}

pub struct Bridge {
og_pegin_address: BitcoinAddress,
pegin_address: BitcoinAddress,
pegin_addresses: Vec<BitcoinAddress>,
bitcoin_core: BitcoinCore,
}

impl Bridge {
const BRIDGE_CONTRACT_ADDRESS: &'static str = "0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB";

pub fn new(bitcoin_core: BitcoinCore, og_pegin_address: BitcoinAddress, pegin_address: BitcoinAddress) -> Self {
pub fn new(bitcoin_core: BitcoinCore, pegin_addresses: Vec<BitcoinAddress>) -> Self {
Self {
og_pegin_address,
pegin_address,
pegin_addresses,
bitcoin_core,
}
}
Expand Down Expand Up @@ -209,9 +207,10 @@ impl Bridge {
.output
.iter()
.find(|output| {
self.og_pegin_address
.matches_script_pubkey(&output.script_pubkey) || self.pegin_address
.matches_script_pubkey(&output.script_pubkey)

self.pegin_addresses.iter().any(|pegin_address| {
pegin_address.matches_script_pubkey(&output.script_pubkey)
})
})
.map(|x| x.value)?;

Expand Down Expand Up @@ -315,10 +314,10 @@ mod tests {
async fn test_stream_e2e() {
let federation = Bridge::new(
BitcoinCore::new("http://localhost:18443", "rpcuser", "rpcpassword"),
"bcrt1pnv0qv2q86ny0my4tycezez7e72jnjns2ays3l4w98v6l383k2h7q0lwmyh"
vec!["bcrt1pnv0qv2q86ny0my4tycezez7e72jnjns2ays3l4w98v6l383k2h7q0lwmyh"
.parse::<BitcoinAddress<NetworkUnchecked>>()
.unwrap()
.assume_checked(),
.assume_checked()],
);

federation
Expand All @@ -333,10 +332,10 @@ mod tests {

let federation = Bridge::new(
BitcoinCore::new("http://localhost:18443", "rpcuser", "rpcpassword"),
"bcrt1pnv0qv2q86ny0my4tycezez7e72jnjns2ays3l4w98v6l383k2h7q0lwmyh"
vec!["bcrt1pnv0qv2q86ny0my4tycezez7e72jnjns2ays3l4w98v6l383k2h7q0lwmyh"
.parse::<BitcoinAddress<NetworkUnchecked>>()
.unwrap()
.assume_checked(),
.assume_checked()],
);
let info = federation
.pegin_info(&tx, BlockHash::all_zeros(), 0)
Expand All @@ -352,10 +351,10 @@ mod tests {

let federation = Bridge::new(
BitcoinCore::new("http://localhost:18443", "rpcuser", "rpcpassword"),
"bcrt1pnv0qv2q86ny0my4tycezez7e72jnjns2ays3l4w98v6l383k2h7q0lwmyh"
vec!["bcrt1pnv0qv2q86ny0my4tycezez7e72jnjns2ays3l4w98v6l383k2h7q0lwmyh"
.parse::<BitcoinAddress<NetworkUnchecked>>()
.unwrap()
.assume_checked(),
.assume_checked()],
);
let info = federation
.pegin_info(&tx, BlockHash::all_zeros(), 0)
Expand Down
1 change: 1 addition & 0 deletions scripts/start_testnet_alys.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ BASE_DIR=$(realpath "$SCRIPT_DIR/../")
BTC_RPC_USER=${BTC_RPC_USER:-"rpcuser"}
BTC_RPC_PASSWORD=${BTC_RPC_PASSWORD:-"rpcpassword"}
BTC_RPC_URL=${BTC_RPC_URL:-"http://localhost:18332"}
GETH_JSON_RPC_URL=${GETH_JSON_RPC_URL:-"http://localhost:8545"}
# Set default number of nodes if not already set
NUM=${NUM:-0}

Expand Down
2 changes: 1 addition & 1 deletion scripts/utils/chain.sh
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ function start_testnet_full_node() {
local NUM=$1
local LOG_FILE=$(get_log_path $NUM)

cargo run --bin app -- --chain "${PWD}/etc/config/chain.json" --geth-url http://localhost:8551/ --db-path "$(get_data_path $NUM)/chain_db" --rpc-port 3000 --wallet-path "$(get_data_path $NUM)" --bitcoin-rpc-url $BTC_RPC_URL --bitcoin-rpc-user $BTC_RPC_USER --bitcoin-rpc-pass $BTC_RPC_PASSWORD --bitcoin-network testnet --p2p-port 55444 --remote-bootnode /ip4/54.161.100.208/tcp/55444 > "$LOG_FILE" 2>&1 &
cargo run --bin app -- --chain "${PWD}/etc/config/chain.json" --geth-execution-url http://localhost:8551 --geth-url http://localhost:8551/ --db-path "$(get_data_path $NUM)/chain_db" --rpc-port 3000 --wallet-path "$(get_data_path $NUM)" --bitcoin-rpc-url $BTC_RPC_URL --bitcoin-rpc-user $BTC_RPC_USER --bitcoin-rpc-pass $BTC_RPC_PASSWORD --bitcoin-network testnet --p2p-port 55444 --remote-bootnode /ip4/54.161.100.208/tcp/55444 > "$LOG_FILE" 2>&1 &
CHAIN_PIDS[$NUM]=$!
}

Expand Down
135 changes: 66 additions & 69 deletions scripts/utils/geth.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ function start_geth() {
local NUM=$1
local LOG_FILE=$(get_log_path $NUM)


# Define the file path
BOOTNODE_FILE_PATH="${BASE_DIR}/etc/data/execution/node_${NUM}/bootnode.key"
NODEKEY_FILE_PATH="${BASE_DIR}/etc/data/execution/node_${NUM}/geth/nodekey"
Expand All @@ -39,53 +38,51 @@ function start_geth() {
# mkdir -p "${BASE_DIR}/etc/data/execution/node_${NUM}" "${BASE_DIR}/etc/data/logs"
# touch "$LOG_FILE"


# Port calculations
local AUTHRPC_PORT=$((8551 + $NUM * 10))
local HTTP_PORT=$((8545 + $NUM * 10))
local WS_PORT=$((8546 + $NUM * 10))
local PORT=$((30303 + $NUM * 10))

# Initialize and start Geth
geth init --state.scheme "hash" --datadir "${BASE_DIR}/etc/data/execution/node_${NUM}" "${BASE_DIR}/etc/config/genesis.json" > "$LOG_FILE" 2>&1
geth init --state.scheme "hash" --datadir "${BASE_DIR}/etc/data/execution/node_${NUM}" "${BASE_DIR}/etc/config/genesis.json" >"$LOG_FILE" 2>&1
geth --datadir "${BASE_DIR}/etc/data/execution/node_${NUM}" \
--state.scheme "hash" \
--networkid 212121 \
--authrpc.vhosts "*" \
--authrpc.addr "0.0.0.0" \
--authrpc.jwtsecret "${BASE_DIR}/etc/config/jwt/jwt" \
--authrpc.port ${AUTHRPC_PORT} \
--http \
--http.addr "0.0.0.0" \
--http.vhosts "*" \
--http.port ${HTTP_PORT} \
--http.api "debug,net,eth,web3,txpool" \
--http.corsdomain=* \
--ws.api "eth,net,web3,debug,txpool" \
--ws \
--ws.addr "0.0.0.0" \
--ws.port ${WS_PORT} \
--ws.origins "*" \
--port ${PORT} \
--gcmode "archive" \
--verbosity 5 \
--log.file $LOG_FILE \
--nodiscover \
--maxpeers 20 \
--gpo.ignoreprice 1 \
--metrics \
--metrics.expensive \
--miner.gasprice 1 \
--history.state 0 \
$ARGUMENT &
--state.scheme "hash" \
--networkid 212121 \
--authrpc.vhosts "*" \
--authrpc.addr "0.0.0.0" \
--authrpc.jwtsecret "${BASE_DIR}/etc/config/jwt/jwt" \
--authrpc.port ${AUTHRPC_PORT} \
--http \
--http.addr "0.0.0.0" \
--http.vhosts "*" \
--http.port ${HTTP_PORT} \
--http.api "debug,net,eth,web3,txpool" \
--http.corsdomain=* \
--ws.api "eth,net,web3,debug,txpool" \
--ws \
--ws.addr "0.0.0.0" \
--ws.port ${WS_PORT} \
--ws.origins "*" \
--port ${PORT} \
--gcmode "archive" \
--verbosity 5 \
--log.file $LOG_FILE \
--nodiscover \
--maxpeers 20 \
--gpo.ignoreprice 1 \
--metrics \
--metrics.expensive \
--miner.gasprice 1 \
--history.state 0 \
$ARGUMENT &
GETH_PIDS[$NUM]=$!
}

function start_testnet_geth() {
local NUM=$1
local LOG_FILE=$(get_log_path $NUM)


mkdir -p "${BASE_DIR}/etc/data/execution/node_${NUM}"

# Port calculations
Expand All @@ -95,57 +92,57 @@ function start_testnet_geth() {
local PORT=$((30303 + $NUM * 10))

# Initialize and start Geth
geth init --state.scheme "hash" --datadir "${BASE_DIR}/etc/data/execution/node_${NUM}" "${BASE_DIR}/etc/config/genesis.json" > "$LOG_FILE" 2>&1
geth init --state.scheme "hash" --datadir "${BASE_DIR}/etc/data/execution/node_${NUM}" "${BASE_DIR}/etc/config/genesis.json" >"$LOG_FILE" 2>&1

BOOTNODE_FILE_PATH="${BASE_DIR}/etc/data/execution/node_${NUM}/bootnode.key"
NODEKEY_FILE_PATH="${BASE_DIR}/etc/data/execution/node_${NUM}/geth/nodekey"

# Check if the file exists
if [ -f "$BOOTNODE_FILE_PATH" ]; then
# File exists, include the argument
ARGUMENT="--nodekey $BOOTNODE_FILE_PATH"
# File exists, include the argument
ARGUMENT="--nodekey $BOOTNODE_FILE_PATH"
elif [ -f "$NODEKEY_FILE_PATH" ]; then
ARGUMENT="--nodekey $NODEKEY_FILE_PATH"
ARGUMENT="--nodekey $NODEKEY_FILE_PATH"
else
bootnode -genkey "$BOOTNODE_FILE_PATH"
ARGUMENT="--nodekey $BOOTNODE_FILE_PATH"
bootnode -genkey "$BOOTNODE_FILE_PATH"
ARGUMENT="--nodekey $BOOTNODE_FILE_PATH"
fi

geth --datadir "${BASE_DIR}/etc/data/execution/node_${NUM}" \
--state.scheme "hash" \
--networkid 212121 \
--authrpc.vhosts "*" \
--authrpc.addr "0.0.0.0" \
--authrpc.jwtsecret "${BASE_DIR}/etc/config/jwt/jwt" \
--authrpc.port ${AUTHRPC_PORT} \
--http \
--http.addr "0.0.0.0" \
--http.vhosts "*" \
--http.port ${HTTP_PORT} \
--http.api "debug,net,eth,web3,txpool" \
--http.corsdomain=* \
--ws.api "eth,net,web3,debug,txpool" \
--ws \
--ws.addr "0.0.0.0" \
--ws.port ${WS_PORT} \
--ws.origins "*" \
--verbosity 4 \
--log.file $LOG_FILE \
--gpo.ignoreprice 1 \
--metrics \
--metrics.expensive \
--miner.gasprice 1 \
--history.state 0 \
--port ${PORT} \
--gcmode "archive" \
--maxpeers 20 \
--bootnodes "enode://6f8c2bfe5b83e79d0dfcf2a619af0a05ca178c5c22c30654db80e8e975133797cf704f0707f6b739731c89cf147fd6835500e632484064b048fdad141ccf542c@54.161.100.208:30303" \
$ARGUMENT &
--state.scheme "hash" \
--networkid 212121 \
--authrpc.vhosts "*" \
--authrpc.addr "0.0.0.0" \
--authrpc.jwtsecret "${BASE_DIR}/etc/config/jwt/jwt" \
--authrpc.port ${AUTHRPC_PORT} \
--http \
--http.addr "0.0.0.0" \
--http.vhosts "*" \
--http.port ${HTTP_PORT} \
--http.api "debug,net,eth,web3,txpool" \
--http.corsdomain=* \
--ws.api "eth,net,web3,debug,txpool" \
--ws \
--ws.addr "0.0.0.0" \
--ws.port ${WS_PORT} \
--ws.origins "*" \
--verbosity 4 \
--log.file $LOG_FILE \
--gpo.ignoreprice 1 \
--metrics \
--metrics.expensive \
--miner.gasprice 1 \
--history.state 0 \
--port ${PORT} \
--gcmode "archive" \
--maxpeers 20 \
--bootnodes "enode://6f8c2bfe5b83e79d0dfcf2a619af0a05ca178c5c22c30654db80e8e975133797cf704f0707f6b739731c89cf147fd6835500e632484064b048fdad141ccf542c@54.161.100.208:30303" \
$ARGUMENT &
GETH_PIDS[$NUM]=$!
}

function start_multiple_geth() {
for (( i=0; i<$1; i++ )); do
for ((i = 0; i < $1; i++)); do
start_geth $i
echo "Started geth ${GETH_PIDS[$i]}"
done
Expand Down

0 comments on commit 85d8c26

Please sign in to comment.