fud/pow: add setting to toggle BTC block hash in challenge

This commit is contained in:
epiphany
2025-11-11 11:53:19 +00:00
parent 7c67eb9375
commit 4c19e2a04a
3 changed files with 23 additions and 3 deletions

View File

@@ -17,6 +17,9 @@ base_dir = "~/.local/share/darkfi/fud"
## Equi-X effort value
#equix_value = 10000
## Toggle BTC block hash in PoW challenge (useful for localnet/debug)
#btc_enabled = true
## Number of latest BTC block hashes that are valid for fud's PoW
#btc_hash_count = 144

View File

@@ -42,6 +42,8 @@ use crate::{
pub struct PowSettings {
/// Equi-X effort value
pub equix_effort: u32,
/// Toggle BTC block hash in PoW challenge (useful for localnet/debug)
pub btc_enabled: bool,
/// Number of latest BTC block hashes that are valid for fud's PoW
pub btc_hash_count: usize,
/// Electrum nodes timeout in seconds
@@ -54,6 +56,7 @@ impl Default for PowSettings {
fn default() -> Self {
Self {
equix_effort: 10000,
btc_enabled: true,
btc_hash_count: 144,
btc_timeout: 15,
btc_electrum_nodes: vec![],
@@ -69,6 +72,10 @@ pub struct PowSettingsOpt {
#[structopt(long)]
pub equix_effort: Option<u32>,
/// Toggle BTC block hash in PoW challenge (useful for localnet/debug)
#[structopt(long)]
pub btc_enabled: Option<bool>,
/// Number of latest BTC block hashes that are valid for fud's PoW
#[structopt(long)]
pub btc_hash_count: Option<usize>,
@@ -88,6 +95,7 @@ impl From<PowSettingsOpt> for PowSettings {
Self {
equix_effort: opt.equix_effort.unwrap_or(def.equix_effort),
btc_enabled: opt.btc_enabled.unwrap_or(def.btc_enabled),
btc_hash_count: opt.btc_hash_count.unwrap_or(def.btc_hash_count),
btc_timeout: opt.btc_timeout.unwrap_or(def.btc_timeout),
btc_electrum_nodes: opt.btc_electrum_nodes,
@@ -127,7 +135,9 @@ impl FudPow {
// Get a recent Bitcoin block hash
let n = 3;
let btc_block_hash = {
let btc_block_hash = if !self.settings.read().await.btc_enabled {
[0; 32]
} else {
let block_hashes = &self.bitcoin_hash_cache.block_hashes;
if block_hashes.is_empty() {
return Err(Error::Custom(
@@ -176,11 +186,14 @@ impl FudPow {
/// Check if the Equi-X solution in a [`VerifiableNodeData`] is valid and has enough effort.
pub async fn verify_node(&mut self, node_data: &VerifiableNodeData) -> Result<()> {
let settings = self.settings.read().await;
// Update the effort using the value from `self.settings`
self.equix_pow.effort = self.settings.read().await.equix_effort;
self.equix_pow.effort = settings.equix_effort;
// Verify if the Bitcoin block hash is known
if !self.bitcoin_hash_cache.block_hashes.contains(&node_data.btc_block_hash) {
if settings.btc_enabled &&
!self.bitcoin_hash_cache.block_hashes.contains(&node_data.btc_block_hash)
{
return Err(Error::Custom(
"Error verifying node data: the BTC block hash is unknown".into(),
))

View File

@@ -225,6 +225,10 @@ pub async fn node_id_task(fud: Arc<Fud>) -> Result<()> {
sleep(interval).await;
let mut pow = fud.pow.write().await;
if !pow.settings.read().await.btc_enabled {
continue
}
let btc = &mut pow.bitcoin_hash_cache;
if btc.update().await.is_err() {