Skip to content

Commit

Permalink
Merge pull request #65 from AnduroProject/64-pegout-min-verification
Browse files Browse the repository at this point in the history
64 pegout min verification
  • Loading branch information
emailnjv authored Dec 11, 2024
2 parents b3a9043 + ac67ffd commit a8de643
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 46 deletions.
24 changes: 11 additions & 13 deletions app/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::chain::{BitcoinWallet, Chain};
use crate::engine::*;
use crate::spec::{genesis_value_parser, ChainSpec, DEV_BITCOIN_SECRET_KEY, DEV_SECRET_KEY};
use crate::store::{Storage, DEFAULT_ROOT_DIR};
use bitcoin::Address;
use bls::{Keypair, SecretKey};
use bridge::{
bitcoin::Network, BitcoinCore, BitcoinSecretKey, BitcoinSignatureCollector, BitcoinSigner,
Expand All @@ -15,7 +16,6 @@ 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 @@ -151,9 +151,7 @@ impl App {
vec![main_layer.with_filter(filter).boxed()]
};

tracing_subscriber::registry()
.with(layers)
.init();
tracing_subscriber::registry().with(layers).init();
}

async fn execute(self) -> eyre::Result<()> {
Expand All @@ -179,7 +177,7 @@ impl App {

let mut bitcoin_addresses = Vec::new();

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

Expand All @@ -200,17 +198,14 @@ impl App {
{
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,
);
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
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 @@ -296,9 +291,12 @@ impl App {
chain.clone().listen_for_rpc_requests().await;

if chain_spec.is_validator {
chain.clone().monitor_bitcoin_blocks(bitcoin_start_height).await;
chain
.clone()
.monitor_bitcoin_blocks(bitcoin_start_height)
.await;
}

AuraSlotWorker::new(
Duration::from_millis(slot_duration),
authorities,
Expand Down
6 changes: 5 additions & 1 deletion app/src/network/rpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,11 @@ impl<Id: ReqId, TSpec: EthSpec> RPC<Id, TSpec> {
}
};

tracing::debug!("Pushing RPC request to swarm request_id: {:?}, request_id: {:?}", request_id, peer_id);
tracing::debug!(
"Pushing RPC request to swarm request_id: {:?}, request_id: {:?}",
request_id,
peer_id
);
self.events.push(event);
}

Expand Down
2 changes: 1 addition & 1 deletion app/src/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub static DEV: Lazy<ChainSpec> = Lazy::new(|| {
"2e80ab37dfb510a64526296fd1f295c42ef19c29".parse().unwrap(),
],
federation_bitcoin_pubkeys: vec![
BitcoinPublicKey::from_str("02767b3ebfdee7190772742cbeacf678e21f1aa043b66e8bfd6d07ac9e50b0049a").unwrap()
BitcoinPublicKey::from_str("0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798").unwrap()
],
bits: 505794034,
chain_id: 212121,
Expand Down
65 changes: 36 additions & 29 deletions crates/federation/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use bitcoincore_rpc::{Error as RpcError, RpcApi};
use ethers::prelude::*;
use futures::prelude::*;
use std::str::FromStr;
use tracing::{debug, instrument, warn};
use tracing::{debug, info, instrument, warn};

pub use bitcoin_signing::{
BitcoinSignatureCollector, BitcoinSigner, Federation, FeeRate,
Expand Down Expand Up @@ -138,12 +138,8 @@ impl Bridge {
let pegin_info = self.pegin_info(&tx, *block_hash, block_info.height as u32);

match pegin_info {
None => {
Err(Error::NotAPegin)
}
Some(info) => {
Ok(info)
}
None => Err(Error::NotAPegin),
Some(info) => Ok(info),
}
}

Expand Down Expand Up @@ -207,10 +203,9 @@ impl Bridge {
.output
.iter()
.find(|output| {

self.pegin_addresses.iter().any(|pegin_address| {
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 @@ -252,15 +247,21 @@ impl Bridge {

for log in receipt.logs {
if let Ok(event) = parse_log::<RequestPegOut>(log) {
if let Some(address) = parse_bitcoin_address(event.bitcoin_address) {
let txout = TxOut {
script_pubkey: address.script_pubkey(),
value: wei_to_sats(event.value),
};
let event_amount_in_sats = wei_to_sats(event.value);
if event_amount_in_sats >= 1000000 {
if let Some(address) = parse_bitcoin_address(event.bitcoin_address) {
let txout = TxOut {
script_pubkey: address.script_pubkey(),
value: event_amount_in_sats,
};

if txout.value >= 100000000000000 {
pegouts.push(txout);
}
} else {
info!(
"Ignoring pegout with for {} sats from {}:{}",
event_amount_in_sats, event.evm_address, event.bitcoin_address
);
}
}
}
Expand Down Expand Up @@ -314,10 +315,12 @@ mod tests {
async fn test_stream_e2e() {
let federation = Bridge::new(
BitcoinCore::new("http://localhost:18443", "rpcuser", "rpcpassword"),
vec!["bcrt1pnv0qv2q86ny0my4tycezez7e72jnjns2ays3l4w98v6l383k2h7q0lwmyh"
.parse::<BitcoinAddress<NetworkUnchecked>>()
.unwrap()
.assume_checked()],
vec![
"bcrt1pnv0qv2q86ny0my4tycezez7e72jnjns2ays3l4w98v6l383k2h7q0lwmyh"
.parse::<BitcoinAddress<NetworkUnchecked>>()
.unwrap()
.assume_checked(),
],
);

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

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

let federation = Bridge::new(
BitcoinCore::new("http://localhost:18443", "rpcuser", "rpcpassword"),
vec!["bcrt1pnv0qv2q86ny0my4tycezez7e72jnjns2ays3l4w98v6l383k2h7q0lwmyh"
.parse::<BitcoinAddress<NetworkUnchecked>>()
.unwrap()
.assume_checked()],
vec![
"bcrt1pnv0qv2q86ny0my4tycezez7e72jnjns2ays3l4w98v6l383k2h7q0lwmyh"
.parse::<BitcoinAddress<NetworkUnchecked>>()
.unwrap()
.assume_checked(),
],
);
let info = federation
.pegin_info(&tx, BlockHash::all_zeros(), 0)
Expand Down
3 changes: 2 additions & 1 deletion scripts/regtest_pegout.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
. $SCRIPT_DIR/utils/shared.sh

PRIVATE_KEY=${PRIVATE_KEY:-$1}
SATOSHIS=${SATOSHIS:-1000001}
if [ -z $PRIVATE_KEY ]; then
PRIVATE_KEY=$DEV_PRIVATE_KEY
else
Expand All @@ -12,4 +13,4 @@ fi

BTC_ADDRESS=${BTC_ADDRESS:-$2}

pegout $PRIVATE_KEY $BTC_ADDRESS
pegout $PRIVATE_KEY $BTC_ADDRESS $SATOSHIS
2 changes: 1 addition & 1 deletion scripts/utils/shared.sh
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ function pegout() {
cd $PWD/contracts

# 0.00100000 btc, or 100000 satoshi
export SATOSHIS=100000
export SATOSHIS="$3"
export PRIVATE_KEY="$1"
forge script script/PegOut.s.sol --rpc-url http://localhost:8545 --broadcast --silent
)
Expand Down

0 comments on commit a8de643

Please sign in to comment.