Skip to content

Commit

Permalink
Deleted dual network
Browse files Browse the repository at this point in the history
  • Loading branch information
polespinasa committed Apr 21, 2024
1 parent a9b906c commit efca378
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 182 deletions.
4 changes: 2 additions & 2 deletions server/grouphug-server/Config.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[electrum]
mainnet_server_endpoint = "ssl://electrum.blockstream.info:50002"
testnet_server_endpoint = "ssl://electrum.blockstream.info:60002"
# mainnet_server_endpoint = "ssl://electrum.blockstream.info:50002"
endpoint = "ssl://electrum.blockstream.info:60002"

[group]
max_time = 300
Expand Down
41 changes: 2 additions & 39 deletions server/grouphug-server/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,41 +1,5 @@
/*
use once_cell::sync::OnceCell;
// Electrum Server endpoints
/// Mainnet
pub static MAINNET_ELECTRUM_SERVER_ENDPOINT: OnceCell<&'static str> = OnceCell::new();
//pub const MAINNET_ELECTRUM_SERVER_ENDPOINT: &str = "ssl://electrum.blockstream.info:50002";
/// Testnet
pub static TESTNET_ELECTRUM_SERVER_ENDPOINT: OnceCell<&'static str> = OnceCell::new();
//pub const TESTNET_ELECTRUM_SERVER_ENDPOINT: &str = "ssl://electrum.blockstream.info:60002";
pub static ELECTRUM_ENDPOINT: OnceCell<&'static str> = OnceCell::new();
/// Time to wait until closing a group if it is not fulfilled (in seconds).
//5min -> 300 | 12h -> 43200
//pub const MAX_TIME: usize = 300;
/// Maximum number of participants of each group.
pub const MAX_SIZE: usize = 3;
/// Dust limit in sats
pub const DUST_LIMIT: u64 = 1000;
/// Fee range
pub const FEE_RANGE: f32 = 2.0;
/// IP & PORT FOR BINDING THE SERVER
pub const SERVER_IP: &str = "127.0.0.1";
pub const SERVER_PORT: &str = "8787";
/// Network
pub static NETWORK: OnceCell<&'static str> = OnceCell::new();
*/

use serde::Deserialize;

#[derive(Deserialize)]
pub struct Config {
pub electrum: Electrum,
Expand All @@ -48,8 +12,7 @@ pub struct Config {

#[derive(Deserialize)]
pub struct Electrum {
pub mainnet_server_endpoint: String,
pub testnet_server_endpoint: String,
pub endpoint: String,
}

#[derive(Deserialize)]
Expand Down
15 changes: 1 addition & 14 deletions server/grouphug-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,6 @@ pub static CONFIG: Lazy<Config> = Lazy::new(|| {
config
});


/*
use crate::config::{
FEE_RANGE,
SERVER_IP,
SERVER_PORT,
ELECTRUM_ENDPOINT,
TESTNET_ELECTRUM_SERVER_ENDPOINT,
MAINNET_ELECTRUM_SERVER_ENDPOINT,
NETWORK};
*/



use crate::server::group::Group;

// External libraries
Expand Down Expand Up @@ -151,6 +137,7 @@ fn handle_client(mut stream: TcpStream) {
println!("New user connected: {}\n", stream.peer_addr().unwrap());

// send the network configuration
// TODO -> Find a way to ask the electrum server what network is running
if &crate::CONFIG.network.name == "testnet" {
stream.write(b"TESTNET\n").unwrap();
}
Expand Down
21 changes: 3 additions & 18 deletions server/grouphug-server/src/server/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,6 @@ use bdk::bitcoin::{
use bdk::electrum_client::{Client, ElectrumApi};
use bdk::blockchain::{ElectrumBlockchain, GetTx};

/*
use crate::config::{
ELECTRUM_ENDPOINT,
MAX_SIZE,
//MAX_TIME
};
*/

pub struct Group {
pub fee_rate: f32,
Expand Down Expand Up @@ -88,11 +81,8 @@ impl Group {
// Finalize the transaction and send it to the network

// Connect to Electrum node
let client = if &crate::CONFIG.network.name == "testnet" {
Client::new(&crate::CONFIG.electrum.testnet_server_endpoint).unwrap()
} else {
Client::new(&crate::CONFIG.electrum.mainnet_server_endpoint).unwrap()
};
let client = Client::new(&crate::CONFIG.electrum.endpoint).unwrap();


let blockchain = ElectrumBlockchain::from(client);

Expand Down Expand Up @@ -148,12 +138,7 @@ impl Group {

// broadcast the transaction
// There's a issue with client 1 here... TODO FIX
let client2 = if &crate::CONFIG.network.name == "testnet" {
Client::new(&crate::CONFIG.electrum.testnet_server_endpoint).unwrap()
} else {
Client::new(&crate::CONFIG.electrum.mainnet_server_endpoint).unwrap()
};

let client2 = Client::new(&crate::CONFIG.electrum.endpoint).unwrap();
let txid = client2.transaction_broadcast_raw(&tx_bytes);

match txid {
Expand Down
118 changes: 9 additions & 109 deletions server/grouphug-server/src/utils/transactions.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//! Functions related to the transactions validation and manipulation.


use bdk::bitcoin::OutPoint;
use bdk::bitcoin::{
Transaction,
Expand All @@ -11,63 +10,34 @@ use bdk::blockchain::{ElectrumBlockchain, GetTx};
use bdk::electrum_client::{Client, ElectrumApi};
use hex::decode as hex_decode;

/*
use crate::config::{TESTNET_ELECTRUM_SERVER_ENDPOINT,
MAINNET_ELECTRUM_SERVER_ENDPOINT,
ELECTRUM_ENDPOINT,
DUST_LIMIT,
NETWORK
};
*/


pub fn which_network(tx: &Transaction) -> &str {
pub fn which_network(tx: &Transaction) -> bool {

// Take previous UTXO
let tx_id = tx.input[0].previous_output.txid;

// Test mainnet
//let client_mainnet = Client::new(MAINNET_ELECTRUM_SERVER_ENDPOINT).unwrap();
let client_mainnet = Client::new(&crate::CONFIG.electrum.mainnet_server_endpoint).unwrap();
let client_mainnet = Client::new(&crate::CONFIG.electrum.endpoint).unwrap();
let blockchain_mainnet = ElectrumBlockchain::from(client_mainnet);


let tx_result_mainnet = blockchain_mainnet.get_tx(&tx_id);
match tx_result_mainnet {
Ok(Some(_tx)) => {
return "mainnet"
},
Ok(None) => (),
Err(_) => (),
}

// Test testnet
//let client_testnet = Client::new(TESTNET_ELECTRUM_SERVER_ENDPOINT).unwrap();
let client_testnet = Client::new(&crate::CONFIG.electrum.testnet_server_endpoint).unwrap();
let blockchain_testnet = ElectrumBlockchain::from(client_testnet);
let tx_result_testnet = blockchain_testnet.get_tx(&tx_id);

match tx_result_testnet {
Ok(Some(_tx)) => {
return "testnet";
return true
},
Ok(None) => (),
Err(_) => (),
}

return "UNKNOWN";
return false;
}

pub fn get_previous_utxo_value(utxo: OutPoint) -> f32 {
// Given an input from a certain transaction returns the value of the pointed UTXO.
// If no UTXO is recieved back, the value returned is 0.

// Connect to Electrum node
let client = if &crate::CONFIG.network.name == "testnet" {
Client::new(&crate::CONFIG.electrum.testnet_server_endpoint).unwrap()
} else {
Client::new(&crate::CONFIG.electrum.mainnet_server_endpoint).unwrap()
};
let client = Client::new(&crate::CONFIG.electrum.endpoint).unwrap();

let blockchain = ElectrumBlockchain::from(client);

Expand All @@ -94,11 +64,7 @@ pub fn previous_utxo_spent(tx: &Transaction) -> bool {
// Validates that the utxo pointed to by the transaction input has not been spent.

// Connect to Electrum node
let client = if &crate::CONFIG.network.name == "testnet" {
Client::new(&crate::CONFIG.electrum.testnet_server_endpoint).unwrap()
} else {
Client::new(&crate::CONFIG.electrum.mainnet_server_endpoint).unwrap()
};
let client = Client::new(&crate::CONFIG.electrum.endpoint).unwrap();

let blockchain = ElectrumBlockchain::from(client);

Expand Down Expand Up @@ -212,9 +178,9 @@ pub fn validate_tx_query_one_to_one_single_anyone_can_pay(tx_hex: &str ) -> (boo
};

// Check that the transaction belongs to the specified network
let network: &str = which_network(&tx);
if network != &crate::CONFIG.network.name {
let msg = format!("The grouphug network is {} and you sent a transaction from the {} network", &crate::CONFIG.network.name, network);
let network: bool = which_network(&tx);
if !network {
let msg = format!("The tx you provided is not from {} network", &crate::CONFIG.network.name);
return (false, msg, real_fee_rate);
}

Expand Down Expand Up @@ -279,70 +245,4 @@ pub fn validate_tx_query_one_to_one_single_anyone_can_pay(tx_hex: &str ) -> (boo

return (true, String::from("Ok"), real_fee_rate);

}



// TESTS MAY NOT WORK AS THEY ARE NOT UPDATED
#[cfg(test)]

mod tests {

use crate::utils::transactions;

#[test]
fn test_validate_tx_query_utxo_wrong_sighash() {
let fee_rate: f32 = 1.0;

//tx should be rejected because of wrong sighash type
let tx_hex = "0200000000010120855900d4b1009d46b257be2a1b773b154364d21f4e358b8b3d2d617f5d44a00200000000fdffffff0192e0f5050000000016001474ea10df0c2455406b686cd7060bad71feb08b740247304402206262868854e24b4d99a9da929e0955555218ea120b52a35bc8435820b7d7c14c0220641129e5f855a1eb8f659cb487d0e509b1cc664a774fb2322d6e598809f5430b012103bbf8c79c3158a1cb32ab3dcffcb2c0e0a677fc600d7ce8e0fb7ff5294ce2574e80552700";
assert_eq!(transactions::validate_tx_query_one_to_one_single_anyone_can_pay(fee_rate, tx_hex), false);
}

#[test]
fn validate_tx_query_2_outputs() {
let fee_rate: f32 = 1.0;

//tx should be rejected because has 2 outputs
let tx_hex = "0200000000010120855900d4b1009d46b257be2a1b773b154364d21f4e358b8b3d2d617f5d44a00200000000fdffffff02404b4c000000000016001474ea10df0c2455406b686cd7060bad71feb08b743395a905000000001600142a9e8c87018f003bddc8de007109eaef3295384d0247304402207abd64a3c565f070ebbb560c8134e0e57530f7461e3ff70b7a378dbc59cec07102207f30dbbe33bf374e35afb16cd29e91b5dc855341f384bee4fba5f35fe89d348f832103bbf8c79c3158a1cb32ab3dcffcb2c0e0a677fc600d7ce8e0fb7ff5294ce2574e80552700";
assert_eq!(transactions::validate_tx_query_one_to_one_single_anyone_can_pay(fee_rate, tx_hex),false);
}

#[test]
fn validate_tx_query_2_inputs() {
let fee_rate: f32 = 1.0;

//tx should be rejected because has 2 inputs
let tx_hex = "0200000000010220855900d4b1009d46b257be2a1b773b154364d21f4e358b8b3d2d617f5d44a00000000000fdffffff20855900d4b1009d46b257be2a1b773b154364d21f4e358b8b3d2d617f5d44a00100000000fdffffff014ec1eb0b0000000016001474ea10df0c2455406b686cd7060bad71feb08b74024730440220573432bfdbeaf51478a9792aed4865312458c583eceacadbd26e02b97f04889102204c0b9037e1085dd4d16c1cb3898cc9e34f68e9bc0c2ad826213585d632091b98832103bbf8c79c3158a1cb32ab3dcffcb2c0e0a677fc600d7ce8e0fb7ff5294ce2574e0247304402207dfe8f0686eefbd2d8619a426f6715ac9dbb606f4525e9bb4d02b78338461596022074ef896493baddbfbe8980348124f696e23e66b678e366742fc8f1e04bcfccef832103bbf8c79c3158a1cb32ab3dcffcb2c0e0a677fc600d7ce8e0fb7ff5294ce2574e80552700";
assert_eq!(transactions::validate_tx_query_one_to_one_single_anyone_can_pay(fee_rate, tx_hex), false);
}

#[test]
fn validate_tx_query_valid_tx() {
let fee_rate: f32 = 1.5;

//tx for this tust must satisfy all requirements
let tx_hex = "0200000000010136740240418792cf35e8dea54f9ec215170594ecef73740bd001392a7b464d110100000000fdffffff0129260000000000001600149664e4a54e7f04f09799d2e61268057a033876780247304402204bb811853c6e0f8e49b0bab3ced01da988afc3a08bf127962ab24129be555f1c0220026f413918a7f9fdc02968271e7c730cadb5a3a5476505bc106a87ff1e1f23c3832102fc889ef1d04e7c489f225a718b3742893d88e1e3f6662c1e5e84c7489252968c80552700";
assert_eq!(transactions::validate_tx_query_one_to_one_single_anyone_can_pay(fee_rate, tx_hex), true);
}


#[test]
fn test_validate_tx_query_fee_to_low() {
let fee_rate: f32 = 7.0;

//tx should be rejected as real fee is below the declarated one.
let tx_hex = "0200000000010120855900d4b1009d46b257be2a1b773b154364d21f4e358b8b3d2d617f5d44a00000000000fdffffff0161dff5050000000016001474ea10df0c2455406b686cd7060bad71feb08b7402473044022072f5f0603a6229efc0bffb4922c44d01772325577f73b3a76935c23b6947c8e4022067ffc97c49605ae77e0c320a598ab254dee35f87aff16d05858eeda4ee325a64012103bbf8c79c3158a1cb32ab3dcffcb2c0e0a677fc600d7ce8e0fb7ff5294ce2574e80552700";
assert_eq!(transactions::validate_tx_query_one_to_one_single_anyone_can_pay(fee_rate, tx_hex), false);
}

#[test]
fn test_validate_tx_query_double_spending() {
let fee_rate: f32 = 1.0;

//tx should be rejected as real fee is below the declarated one.
let tx_hex = "0200000000010120855900d4b1009d46b257be2a1b773b154364d21f4e358b8b3d2d617f5d44a00300000000fdffffff01c8b4c6290000000016001474ea10df0c2455406b686cd7060bad71feb08b7402473044022001ff2702495ff5ed0b73a178b8e9f84eccc8475e0c5c8a4306abbd56cbcf91e30220465844718edb42df0354cca7bbe91e9798eaedbc8c9dc18322a84cfb77dbd6bc8321028f1b8a4db265de2e99e3ba9575d8b572c8cb85bb12697f7e26a7288b9a4b13ac80552700";
assert_eq!(transactions::validate_tx_query_one_to_one_single_anyone_can_pay(fee_rate, tx_hex), false);
}

}

0 comments on commit efca378

Please sign in to comment.