|
1 | 1 | use std::{collections::HashMap, sync::Arc}; |
2 | 2 |
|
| 3 | +use fallible_iterator::FallibleIterator as _; |
3 | 4 | use futures::{StreamExt, TryFutureExt}; |
4 | 5 | use parking_lot::RwLock; |
5 | 6 | use rustreexo::accumulator::proof::Proof; |
@@ -36,6 +37,8 @@ pub enum Error { |
36 | 37 | Node(#[source] Box<node::Error>), |
37 | 38 | #[error("No CUSF mainchain wallet client")] |
38 | 39 | NoCusfMainchainWalletClient, |
| 40 | + #[error("Failed to request mainchain ancestor info for {block_hash}")] |
| 41 | + RequestMainchainAncestorInfos { block_hash: bitcoin::BlockHash }, |
39 | 42 | #[error("Utreexo error: {0}")] |
40 | 43 | Utreexo(String), |
41 | 44 | #[error("Unable to verify existence of CUSF mainchain service(s) at {url}")] |
@@ -319,49 +322,156 @@ impl App { |
319 | 322 | let Some(miner) = self.miner.as_ref() else { |
320 | 323 | return Err(Error::NoCusfMainchainWalletClient); |
321 | 324 | }; |
322 | | - const NUM_TRANSACTIONS: usize = 1000; |
323 | | - let (txs, tx_fees) = self.node.get_transactions(NUM_TRANSACTIONS)?; |
324 | | - let coinbase = match tx_fees { |
325 | | - bitcoin::Amount::ZERO => vec![], |
326 | | - _ => vec![types::Output { |
327 | | - address: self.wallet.get_new_address()?, |
328 | | - content: types::OutputContent::Value(tx_fees), |
329 | | - }], |
330 | | - }; |
331 | | - let body = types::Body::new(txs, coinbase); |
332 | | - let prev_side_hash = self.node.try_get_best_hash()?; |
333 | 325 | let prev_main_hash = { |
334 | 326 | let mut miner_write = miner.write().await; |
335 | 327 | let prev_main_hash = |
336 | 328 | miner_write.cusf_mainchain.get_chain_tip().await?.block_hash; |
337 | 329 | drop(miner_write); |
338 | 330 | prev_main_hash |
339 | 331 | }; |
340 | | - let roots = { |
341 | | - let mut accumulator = self.node.get_tip_accumulator()?; |
342 | | - body.modify_memforest(&mut accumulator.0) |
343 | | - .map_err(Error::Utreexo)?; |
344 | | - accumulator |
345 | | - .0 |
346 | | - .get_roots() |
347 | | - .iter() |
348 | | - .map(|root| root.get_data()) |
349 | | - .collect() |
350 | | - }; |
351 | | - let header = types::Header { |
352 | | - merkle_root: body.compute_merkle_root(), |
353 | | - roots, |
354 | | - prev_side_hash, |
355 | | - prev_main_hash, |
356 | | - }; |
357 | | - let bribe = fee.unwrap_or_else(|| { |
358 | | - if tx_fees > bitcoin::Amount::ZERO { |
359 | | - tx_fees |
| 332 | + let tip_hash = self.node.try_get_best_hash()?; |
| 333 | + // If `prev_side_hash` is not the best tip to mine on, then mine an |
| 334 | + // empty block. |
| 335 | + // This is a temporary fix, ideally we always choose the best tip to |
| 336 | + // mine on |
| 337 | + let prev_side_hash = if let Some(tip_hash) = tip_hash { |
| 338 | + let tip_header = self.node.get_header(tip_hash)?; |
| 339 | + let archive = self.node.archive(); |
| 340 | + let prev_main_hash_header_in_archive = { |
| 341 | + let rotxn = |
| 342 | + self.node.env().read_txn().map_err(node::Error::from)?; |
| 343 | + archive |
| 344 | + .try_get_main_header_info(&rotxn, &prev_main_hash) |
| 345 | + .map_err(node::Error::from)? |
| 346 | + .is_some() |
| 347 | + }; |
| 348 | + if !prev_main_hash_header_in_archive { |
| 349 | + // Request mainchain header info |
| 350 | + if !self |
| 351 | + .node |
| 352 | + .request_mainchain_ancestor_infos(prev_main_hash) |
| 353 | + .await? |
| 354 | + { |
| 355 | + return Err(Error::RequestMainchainAncestorInfos { |
| 356 | + block_hash: prev_main_hash, |
| 357 | + }); |
| 358 | + } |
| 359 | + } |
| 360 | + let rotxn = |
| 361 | + self.node.env().read_txn().map_err(node::Error::from)?; |
| 362 | + let last_common_main_ancestor = archive |
| 363 | + .last_common_main_ancestor( |
| 364 | + &rotxn, |
| 365 | + prev_main_hash, |
| 366 | + tip_header.prev_main_hash, |
| 367 | + ) |
| 368 | + .map_err(node::Error::from)?; |
| 369 | + if last_common_main_ancestor == tip_header.prev_main_hash { |
| 370 | + Some(tip_hash) |
360 | 371 | } else { |
361 | | - Self::EMPTY_BLOCK_BMM_BRIBE |
| 372 | + // Find a tip to mine on |
| 373 | + archive |
| 374 | + .ancestor_headers(&rotxn, tip_hash) |
| 375 | + .find_map(|(block_hash, header)| { |
| 376 | + if header.prev_main_hash == last_common_main_ancestor { |
| 377 | + Ok(None) |
| 378 | + } else if archive.is_main_descendant( |
| 379 | + &rotxn, |
| 380 | + header.prev_main_hash, |
| 381 | + last_common_main_ancestor, |
| 382 | + )? { |
| 383 | + Ok(Some(block_hash)) |
| 384 | + } else { |
| 385 | + Ok(None) |
| 386 | + } |
| 387 | + }) |
| 388 | + .map_err(node::Error::from)? |
362 | 389 | } |
363 | | - }); |
364 | | - |
| 390 | + } else { |
| 391 | + None |
| 392 | + }; |
| 393 | + let (bribe, header, body) = if prev_side_hash == tip_hash { |
| 394 | + const NUM_TRANSACTIONS: usize = 1000; |
| 395 | + let (txs, tx_fees) = |
| 396 | + self.node.get_transactions(NUM_TRANSACTIONS)?; |
| 397 | + let coinbase = match tx_fees { |
| 398 | + bitcoin::Amount::ZERO => Vec::new(), |
| 399 | + _ => vec![types::Output { |
| 400 | + address: self.wallet.get_new_address()?, |
| 401 | + content: types::OutputContent::Value(tx_fees), |
| 402 | + }], |
| 403 | + }; |
| 404 | + let body = types::Body::new(txs, coinbase); |
| 405 | + let roots = { |
| 406 | + let mut accumulator = if let Some(tip_hash) = tip_hash { |
| 407 | + let rotxn = self |
| 408 | + .node |
| 409 | + .env() |
| 410 | + .read_txn() |
| 411 | + .map_err(node::Error::from)?; |
| 412 | + self.node |
| 413 | + .archive() |
| 414 | + .get_accumulator(&rotxn, tip_hash) |
| 415 | + .map_err(node::Error::from)? |
| 416 | + } else { |
| 417 | + types::Accumulator::default() |
| 418 | + }; |
| 419 | + body.modify_memforest(&mut accumulator.0) |
| 420 | + .map_err(Error::Utreexo)?; |
| 421 | + accumulator |
| 422 | + .0 |
| 423 | + .get_roots() |
| 424 | + .iter() |
| 425 | + .map(|root| root.get_data()) |
| 426 | + .collect() |
| 427 | + }; |
| 428 | + let header = types::Header { |
| 429 | + merkle_root: body.compute_merkle_root(), |
| 430 | + roots, |
| 431 | + prev_side_hash, |
| 432 | + prev_main_hash, |
| 433 | + }; |
| 434 | + let bribe = fee.unwrap_or_else(|| { |
| 435 | + if tx_fees > bitcoin::Amount::ZERO { |
| 436 | + tx_fees |
| 437 | + } else { |
| 438 | + Self::EMPTY_BLOCK_BMM_BRIBE |
| 439 | + } |
| 440 | + }); |
| 441 | + (bribe, header, body) |
| 442 | + } else { |
| 443 | + let coinbase = Vec::new(); |
| 444 | + let body = types::Body::new(Vec::new(), coinbase); |
| 445 | + let roots = { |
| 446 | + let accumulator = if let Some(prev_side_hash) = prev_side_hash { |
| 447 | + let rotxn = self |
| 448 | + .node |
| 449 | + .env() |
| 450 | + .read_txn() |
| 451 | + .map_err(node::Error::from)?; |
| 452 | + self.node |
| 453 | + .archive() |
| 454 | + .get_accumulator(&rotxn, prev_side_hash) |
| 455 | + .map_err(node::Error::from)? |
| 456 | + } else { |
| 457 | + types::Accumulator::default() |
| 458 | + }; |
| 459 | + accumulator |
| 460 | + .0 |
| 461 | + .get_roots() |
| 462 | + .iter() |
| 463 | + .map(|root| root.get_data()) |
| 464 | + .collect() |
| 465 | + }; |
| 466 | + let header = types::Header { |
| 467 | + merkle_root: body.compute_merkle_root(), |
| 468 | + roots, |
| 469 | + prev_side_hash, |
| 470 | + prev_main_hash, |
| 471 | + }; |
| 472 | + let bribe = Self::EMPTY_BLOCK_BMM_BRIBE; |
| 473 | + (bribe, header, body) |
| 474 | + }; |
365 | 475 | let mut miner_write = miner.write().await; |
366 | 476 | let bmm_txid = miner_write |
367 | 477 | .attempt_bmm(bribe.to_sat(), 0, header, body) |
|
0 commit comments