Skip to content

Commit

Permalink
add: allow configuring the pool-id network
Browse files Browse the repository at this point in the history
  • Loading branch information
0xB10C committed Dec 25, 2023
1 parent b4f4344 commit d82aa2c
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 19 deletions.
13 changes: 9 additions & 4 deletions daemon-config.toml.example
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,15 @@ retag_transactions = false
# https://raw.githubusercontent.com/0xB10C/ofac-sanctioned-digital-currency-addresses/lists/sanctioned_addresses_XBT.txt
sanctioned_addresses_url = "https://raw.githubusercontent.com/0xB10C/ofac-sanctioned-digital-currency-addresses/lists/sanctioned_addresses_XBT.txt"

# URL where to query the pool identification dataset from. This is optional
# and defaults to:
# https://raw.githubusercontent.com/bitcoin-data/mining-pools/generated/pool-list.json
pool_identification_dataset_url = "https://raw.githubusercontent.com/bitcoin-data/mining-pools/generated/pool-list.json"
[pool_identification]
# URL where to query the pool identification dataset from. This is optional
# and defaults to:
# https://raw.githubusercontent.com/bitcoin-data/mining-pools/generated/pool-list.
dataset_url = "https://raw.githubusercontent.com/bitcoin-data/mining-pools/generated/pool-list.json"
# Valid networks for the pool identification are: "bitcoin" (i.e. mainnet),
# "testnet", "signet", and "regtest". Make sure to use the pool-list for the
# right network too.
network = "bitcoin"

# Prometheus Metric Server
# Don't expose this publicly.
Expand Down
33 changes: 25 additions & 8 deletions daemon/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ use bitcoin_pool_identification::{
use miningpool_observer_shared::bitcoincore_rpc::bitcoin;
use miningpool_observer_shared::bitcoincore_rpc::bitcoin::{
address::NetworkUnchecked, address::ParseError, hash_types::Txid, hashes::Hash, Address,
Amount, Block, Network,
Amount, Block,
};
use miningpool_observer_shared::bitcoincore_rpc::jsonrpc::serde_json;
use miningpool_observer_shared::config::PoolIdentificationConfig;
use simple_logger::SimpleLogger;

use miningpool_observer_shared::bitcoincore_rpc::json::{
Expand Down Expand Up @@ -163,7 +164,7 @@ fn main() {

start_pool_identification_update_thread(
miningpool_identification_data.clone(),
config.pool_identification_dataset_url,
config.pool_identification.clone().dataset_url,
);

// Build a custom transport here to be able to configure the timeout.
Expand Down Expand Up @@ -198,6 +199,7 @@ fn main() {
reid_rpc_client,
reid_conn_pool,
miningpool_identification_data.clone(),
config.pool_identification.clone(),
);

match rpc_client.get_network_info() {
Expand Down Expand Up @@ -253,7 +255,12 @@ fn main() {
retag_transactions(retag_rpc_client, retag_conn_pool);
}

main_loop(&rpc_client, &conn_pool, miningpool_identification_data);
main_loop(
&rpc_client,
&conn_pool,
config.pool_identification.clone(),
miningpool_identification_data,
);
}

fn startup_db_mirgation(conn_pool: &db_pool::PgPool) {
Expand Down Expand Up @@ -285,7 +292,12 @@ fn startup_db_mirgation(conn_pool: &db_pool::PgPool) {
}
}

fn main_loop(rpc: &Client, db_pool: &db_pool::PgPool, pools: model::SharedPoolIDData) {
fn main_loop(
rpc: &Client,
db_pool: &db_pool::PgPool,
pool_identification_config: config::PoolIdentificationConfig,
pools: model::SharedPoolIDData,
) {
// stores up to the last MAX_OLD_TEMPLATES GetBlockTemplateResults to lookup older templates
// based on miner block timestamps.
let mut last_templates: VecDeque<GetBlockTemplateResult> =
Expand Down Expand Up @@ -451,7 +463,7 @@ fn main_loop(rpc: &Client, db_pool: &db_pool::PgPool, pools: model::SharedPoolID
target: LOG_TARGET_STATS,
"Processing missed block {} mined by {}",
bitcoin_block.block_hash(),
match bitcoin_block.identify_pool(Network::Bitcoin, &pools.lock().unwrap()) {
match bitcoin_block.identify_pool(pool_identification_config.clone().network, &pools.lock().unwrap()) {
Some(result) => result.pool.name,
None => "UNKNOWN".to_string(),
}
Expand All @@ -464,6 +476,7 @@ fn main_loop(rpc: &Client, db_pool: &db_pool::PgPool, pools: model::SharedPoolID
&block_tx_fees,
&mut last_templates,
pools.clone(),
pool_identification_config.clone(),
);

last_templates.push_back(current_template);
Expand Down Expand Up @@ -492,7 +505,7 @@ fn main_loop(rpc: &Client, db_pool: &db_pool::PgPool, pools: model::SharedPoolID
target: LOG_TARGET_STATS,
"New block detected {} mined by {}",
bitcoin_block.block_hash(),
match bitcoin_block.identify_pool(Network::Bitcoin, &pools.lock().unwrap()) {
match bitcoin_block.identify_pool(pool_identification_config.network.clone(), &pools.lock().unwrap()) {
Some(result) => result.pool.name,
None => "UNKNOWN".to_string(),
}
Expand All @@ -505,6 +518,7 @@ fn main_loop(rpc: &Client, db_pool: &db_pool::PgPool, pools: model::SharedPoolID
&block_tx_fees,
&mut last_templates,
pools.clone(),
pool_identification_config.clone(),
);
}
}
Expand All @@ -516,6 +530,7 @@ fn process(
block_tx_fees: &GetBlockTxFeesResult,
last_templates: &mut VecDeque<GetBlockTemplateResult>,
pools: model::SharedPoolIDData,
pool_identification_config: PoolIdentificationConfig,
) {
let block_tx_data = processing::build_block_tx_data(bitcoin_block, block_tx_fees);

Expand Down Expand Up @@ -641,6 +656,7 @@ fn process(
&outpoint_to_sanctioned_utxo_map,
&sanctioned_addresses,
pools,
pool_identification_config.network,
);

let block_id = match db::insert_block(&block, &mut connection) {
Expand Down Expand Up @@ -1270,6 +1286,7 @@ fn start_retry_unknown_pool_identification_thread(
rpc_client: Client,
db_pool: db_pool::PgPool,
pools: model::SharedPoolIDData,
pool_identification_config: config::PoolIdentificationConfig,
) {
thread::spawn(move || {
let mut conn = match db_pool.get() {
Expand Down Expand Up @@ -1334,8 +1351,8 @@ fn start_retry_unknown_pool_identification_thread(
}
};

if let Some(result) =
bitcoin_block.identify_pool(Network::Bitcoin, &pools.lock().unwrap())
if let Some(result) = bitcoin_block
.identify_pool(pool_identification_config.network, &pools.lock().unwrap())
{
match db::update_pool_name_with_block_id(&mut conn, block.id, &result.pool.name) {
Ok(_) => {
Expand Down
3 changes: 2 additions & 1 deletion daemon/src/processing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -699,9 +699,10 @@ pub fn build_block(
outpoint_to_sanctioned_utxo_map: &HashMap<(Vec<u8>, u32), &shared_model::SanctionedUtxo>,
sanctioned_addresses: &HashSet<String>,
pools: SharedPoolIDData,
network: Network,
) -> shared_model::NewBlock {
let (pool_name, pool_link, pool_id_method) =
get_pool_info_or_default(block.identify_pool(Network::Bitcoin, &pools.lock().unwrap()));
get_pool_info_or_default(block.identify_pool(network, &pools.lock().unwrap()));
let mut block_hash = block.block_hash().to_byte_array().to_vec();
block_hash.reverse();
let mut prev_block_hash = block.header.prev_blockhash.to_byte_array().to_vec();
Expand Down
50 changes: 44 additions & 6 deletions shared/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::path::PathBuf;
use std::str::FromStr;
use std::{env, error, fmt, fs, io};

use bitcoincore_rpc::bitcoin::Network;
use bitcoincore_rpc::Auth;
use log::LevelFilter;
use serde::{Deserialize, Serialize};
Expand All @@ -10,8 +11,9 @@ const ENVVAR_CONFIG_FILE: &str = "CONFIG_FILE";
const DEFAULT_DAEMON_CONFIG: &str = "daemon-config.toml";
const DEFAULT_WEB_CONFIG: &str = "web-config.toml";
const DEFAULT_SANCTIONED_ADDRESSES_URL: &str = "https://raw.githubusercontent.com/0xB10C/ofac-sanctioned-digital-currency-addresses/lists/sanctioned_addresses_XBT.txt";
const DEFAULT_POOL_IDENTIFICATION_DATASET_URL: &str =
const DEFAULT_POOL_IDENTIFICATOIN_DATASET_URL: &str =
"https://raw.githubusercontent.com/bitcoin-data/mining-pools/generated/pool-list.json";
const DEFAULT_POOL_IDENTIFICATOIN_NETWORK: Network = Network::Bitcoin;

#[derive(Deserialize)]
struct DaemonTomlConfig {
Expand All @@ -25,7 +27,7 @@ struct DaemonTomlConfig {
retag_transactions: bool,
prometheus: PrometheusConfig,
sanctioned_addresses_url: Option<String>,
pool_identification_dataset_url: Option<String>,
pool_identificatoin: Option<PoolIdentificationTomlConfig>,
}

#[derive(Serialize, Deserialize)]
Expand All @@ -34,6 +36,44 @@ pub struct PrometheusConfig {
pub address: String,
}

#[derive(Serialize, Deserialize)]
pub struct PoolIdentificationTomlConfig {
pub dataset_url: Option<String>,
pub network: Option<String>,
}

impl Default for PoolIdentificationTomlConfig {
fn default() -> Self {
PoolIdentificationTomlConfig {
dataset_url: Some(DEFAULT_POOL_IDENTIFICATOIN_DATASET_URL.to_string()),
network: Some(DEFAULT_POOL_IDENTIFICATOIN_NETWORK.to_string()),
}
}
}

#[derive(Clone)]
pub struct PoolIdentificationConfig {
pub dataset_url: String,
pub network: Network,
}

impl From<PoolIdentificationTomlConfig> for PoolIdentificationConfig {
fn from(toml: PoolIdentificationTomlConfig) -> Self {
PoolIdentificationConfig {
dataset_url: toml
.dataset_url
.unwrap_or(DEFAULT_POOL_IDENTIFICATOIN_DATASET_URL.to_string()),
network: Network::from_str(
&toml
.network
.unwrap_or(DEFAULT_POOL_IDENTIFICATOIN_NETWORK.to_string())
.to_lowercase(),
)
.expect("invalid pool identification network"),
}
}
}

pub struct DaemonConfig {
pub rpc_url: String,
pub rpc_auth: Auth,
Expand All @@ -42,7 +82,7 @@ pub struct DaemonConfig {
pub retag_transactions: bool,
pub prometheus: PrometheusConfig,
pub sanctioned_addresses_url: String,
pub pool_identification_dataset_url: String,
pub pool_identification: PoolIdentificationConfig,
}

pub fn load_daemon_config() -> Result<DaemonConfig, ConfigError> {
Expand Down Expand Up @@ -79,9 +119,7 @@ pub fn load_daemon_config() -> Result<DaemonConfig, ConfigError> {
sanctioned_addresses_url: config
.sanctioned_addresses_url
.unwrap_or(DEFAULT_SANCTIONED_ADDRESSES_URL.to_string()),
pool_identification_dataset_url: config
.pool_identification_dataset_url
.unwrap_or(DEFAULT_POOL_IDENTIFICATION_DATASET_URL.to_string()),
pool_identification: config.pool_identificatoin.unwrap_or_default().into(),
});
}

Expand Down

0 comments on commit d82aa2c

Please sign in to comment.