unknown_proposal.rs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2026 Dyne.org foundation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. use std::{
  19. collections::{HashMap, HashSet},
  20. sync::Arc,
  21. };
  22. use num_bigint::BigUint;
  23. use smol::{channel::Receiver, lock::RwLock};
  24. use tinyjson::JsonValue;
  25. use tracing::{debug, error, info};
  26. use darkfi::{
  27. blockchain::{BlockDifficulty, BlockchainOverlay, HeaderHash},
  28. net::ChannelPtr,
  29. util::{encoding::base64, time::Timestamp},
  30. validator::{
  31. consensus::{Fork, Proposal},
  32. pow::PoWModule,
  33. utils::{best_fork_index, header_rank},
  34. verification::verify_fork_proposal,
  35. Validator, ValidatorPtr,
  36. },
  37. Error::{Custom, DatabaseError, PoWInvalidOutHash, ProposalAlreadyExists},
  38. Result,
  39. };
  40. use darkfi_serial::serialize_async;
  41. use crate::{
  42. proto::{
  43. ForkHeaderHashRequest, ForkHeaderHashResponse, ForkHeadersRequest, ForkHeadersResponse,
  44. ForkProposalsRequest, ForkProposalsResponse, ForkSyncRequest, ForkSyncResponse,
  45. ProposalMessage, BATCH,
  46. },
  47. DarkfiNodePtr,
  48. };
  49. /// Background task to handle unknown proposals.
  50. pub async fn handle_unknown_proposals(
  51. receiver: Receiver<(Proposal, u32)>,
  52. unknown_proposals: Arc<RwLock<HashSet<[u8; 32]>>>,
  53. unknown_proposals_channels: Arc<RwLock<HashMap<u32, (u8, u64)>>>,
  54. node: DarkfiNodePtr,
  55. ) -> Result<()> {
  56. debug!(target: "darkfid::task::handle_unknown_proposal", "START");
  57. loop {
  58. // Wait for a new unknown proposal trigger
  59. let (proposal, channel) = match receiver.recv().await {
  60. Ok(m) => m,
  61. Err(e) => {
  62. debug!(
  63. target: "darkfid::task::handle_unknown_proposal",
  64. "recv fail: {e}"
  65. );
  66. continue
  67. }
  68. };
  69. // Check if proposal exists in our queue
  70. let lock = unknown_proposals.read().await;
  71. let contains_proposal = lock.contains(proposal.hash.inner());
  72. drop(lock);
  73. if !contains_proposal {
  74. debug!(
  75. target: "darkfid::task::handle_unknown_proposal",
  76. "Proposal {} is not in our unknown proposals queue.",
  77. proposal.hash,
  78. );
  79. continue
  80. };
  81. // Increase channel counter
  82. let mut lock = unknown_proposals_channels.write().await;
  83. let channel_counter = if let Some((counter, timestamp)) = lock.get_mut(&channel) {
  84. *counter += 1;
  85. *timestamp = Timestamp::current_time().inner();
  86. *counter
  87. } else {
  88. lock.insert(channel, (1, Timestamp::current_time().inner()));
  89. 1
  90. };
  91. drop(lock);
  92. // Handle the unknown proposal
  93. if handle_unknown_proposal(&node, channel, &proposal).await {
  94. // Ban channel if it exceeds 5 consecutive unknown proposals
  95. if channel_counter > 5 {
  96. if let Some(channel) = node.p2p_handler.p2p.get_channel(channel) {
  97. channel.ban().await;
  98. }
  99. unknown_proposals_channels.write().await.remove(&channel);
  100. }
  101. };
  102. // Remove proposal from the queue
  103. let mut lock = unknown_proposals.write().await;
  104. lock.remove(proposal.hash.inner());
  105. drop(lock);
  106. }
  107. }
  108. /// Background task to handle an unknown proposal.
  109. /// Returns a boolean flag indicate if we should ban the channel.
  110. async fn handle_unknown_proposal(node: &DarkfiNodePtr, channel: u32, proposal: &Proposal) -> bool {
  111. // If proposal fork chain was not found, we ask our peer for its sequence
  112. debug!(target: "darkfid::task::handle_unknown_proposal", "Asking peer for fork sequence");
  113. let Some(channel) = node.p2p_handler.p2p.get_channel(channel) else {
  114. debug!(target: "darkfid::task::handle_unknown_proposal", "Channel {channel} wasn't found.");
  115. return false
  116. };
  117. // Communication setup
  118. let Ok(response_sub) = channel.subscribe_msg::<ForkSyncResponse>().await else {
  119. debug!(target: "darkfid::task::handle_unknown_proposal", "Failure during `ForkSyncResponse` communication setup with peer: {channel:?}");
  120. return true
  121. };
  122. // Grab last known block to create the request and execute it
  123. let last = match node.validator.read().await.blockchain.last() {
  124. Ok(l) => l,
  125. Err(e) => {
  126. error!(target: "darkfid::task::handle_unknown_proposal", "Blockchain last retriaval failed: {e}");
  127. return false
  128. }
  129. };
  130. let request = ForkSyncRequest { tip: last.1, fork_tip: Some(proposal.hash) };
  131. if let Err(e) = channel.send(&request).await {
  132. debug!(target: "darkfid::task::handle_unknown_proposal", "Channel send failed: {e}");
  133. return true
  134. };
  135. let comms_timeout = node
  136. .p2p_handler
  137. .p2p
  138. .settings()
  139. .read_arc()
  140. .await
  141. .outbound_connect_timeout(channel.address().scheme());
  142. // Node waits for response
  143. let response = match response_sub.receive_with_timeout(comms_timeout).await {
  144. Ok(r) => r,
  145. Err(e) => {
  146. debug!(target: "darkfid::task::handle_unknown_proposal", "Asking peer for fork sequence failed: {e}");
  147. return true
  148. }
  149. };
  150. debug!(target: "darkfid::task::handle_unknown_proposal", "Peer response: {response:?}");
  151. // Verify and store retrieved proposals
  152. debug!(target: "darkfid::task::handle_unknown_proposal", "Processing received proposals");
  153. // Response should not be empty
  154. if response.proposals.is_empty() {
  155. debug!(target: "darkfid::task::handle_unknown_proposal", "Peer responded with empty sequence, node might be out of sync!");
  156. return handle_reorg(node, &(&channel, &comms_timeout), proposal).await
  157. }
  158. // Sequence length must correspond to requested height
  159. if response.proposals.len() as u32 != proposal.block.header.height - last.0 {
  160. debug!(target: "darkfid::task::handle_unknown_proposal", "Response sequence length is erroneous");
  161. return handle_reorg(node, &(&channel, &comms_timeout), proposal).await
  162. }
  163. // First proposal must extend canonical
  164. if response.proposals[0].block.header.previous != last.1 {
  165. debug!(target: "darkfid::task::handle_unknown_proposal", "Response sequence doesn't extend canonical");
  166. return handle_reorg(node, &(&channel, &comms_timeout), proposal).await
  167. }
  168. // Last proposal must be the same as the one requested
  169. if response.proposals.last().unwrap().hash != proposal.hash {
  170. debug!(target: "darkfid::task::handle_unknown_proposal", "Response sequence doesn't correspond to requested tip");
  171. return handle_reorg(node, &(&channel, &comms_timeout), proposal).await
  172. }
  173. // Process response proposals
  174. for proposal in &response.proposals {
  175. // Append proposal
  176. match node.validator.write().await.append_proposal(proposal).await {
  177. Ok(()) => { /* Do nothing */ }
  178. // Skip already existing proposals
  179. Err(ProposalAlreadyExists) => continue,
  180. Err(e) => {
  181. debug!(
  182. target: "darkfid::task::handle_unknown_proposal",
  183. "Error while appending response proposal: {e}"
  184. );
  185. break;
  186. }
  187. };
  188. // Broadcast proposal to rest nodes
  189. let message = ProposalMessage(proposal.clone());
  190. node.p2p_handler.p2p.broadcast_with_exclude(&message, &[channel.address().clone()]).await;
  191. // Notify proposals subscriber
  192. let enc_prop = JsonValue::String(base64::encode(&serialize_async(proposal).await));
  193. node.subscribers.get("proposals").unwrap().notify(vec![enc_prop].into()).await;
  194. }
  195. false
  196. }
  197. /// Auxiliary function to handle a potential reorg. We first find our
  198. /// last common block with the peer, then grab the header sequence from
  199. /// that block until the proposal and check if it ranks higher than our
  200. /// current best ranking fork, to perform a reorg.
  201. ///
  202. /// Returns a boolean flag indicate if we should ban the channel.
  203. ///
  204. /// Note: Always remember to purge new trees from the database if not
  205. /// needed.
  206. // TODO: We keep everything in memory which can result in OOM for a
  207. // valid long fork. We could use some disk space to store stuff.
  208. async fn handle_reorg(
  209. // Node pointer
  210. node: &DarkfiNodePtr,
  211. // Peer channel and its communications timeout
  212. channel: &(&ChannelPtr, &u64),
  213. // Peer fork proposal
  214. proposal: &Proposal,
  215. ) -> bool {
  216. info!(target: "darkfid::task::handle_reorg", "Checking for potential reorg from proposal {} - {} by peer: {:?}", proposal.hash, proposal.block.header.height, channel.0);
  217. // Check if genesis proposal was provided
  218. if proposal.block.header.height == 0 {
  219. debug!(target: "darkfid::task::handle_reorg", "Peer send a genesis proposal, skipping...");
  220. return true
  221. }
  222. // Find last common header and its sequence, going backwards from
  223. // the proposal.
  224. let (last_common_height, last_common_hash, peer_header_hashes) =
  225. match retrieve_peer_header_hashes(&node.validator, channel, proposal).await {
  226. Ok(t) => t,
  227. Err(DatabaseError(e)) => {
  228. error!(target: "darkfid::task::handle_reorg", "Internal error while retrieving peer headers hashes: {e}");
  229. return false
  230. }
  231. Err(e) => {
  232. error!(target: "darkfid::task::handle_reorg", "Retrieving peer headers hashes failed: {e}");
  233. return true
  234. }
  235. };
  236. // Create a new PoW module from last common height
  237. let validator = node.validator.read().await;
  238. let module = match PoWModule::new(
  239. validator.consensus.blockchain.clone(),
  240. validator.consensus.module.target,
  241. validator.consensus.module.fixed_difficulty.clone(),
  242. Some(last_common_height + 1),
  243. ) {
  244. Ok(m) => m,
  245. Err(e) => {
  246. error!(target: "darkfid::task::handle_reorg", "PoWModule generation failed: {e}");
  247. return false
  248. }
  249. };
  250. // Grab last common height ranks
  251. let last_difficulty = match last_common_height {
  252. 0 => {
  253. let genesis_timestamp = match validator.blockchain.genesis_block() {
  254. Ok(b) => b.header.timestamp,
  255. Err(e) => {
  256. error!(target: "darkfid::task::handle_reorg", "Retrieving genesis block failed: {e}");
  257. return false
  258. }
  259. };
  260. BlockDifficulty::genesis(genesis_timestamp)
  261. }
  262. _ => match validator.blockchain.blocks.get_difficulty(&[last_common_height], true) {
  263. Ok(d) => d[0].clone().unwrap(),
  264. Err(e) => {
  265. error!(target: "darkfid::task::handle_reorg", "Retrieving block difficulty failed: {e}");
  266. return false
  267. }
  268. },
  269. };
  270. drop(validator);
  271. // Retrieve the headers of the hashes sequence and its ranking
  272. let (targets_rank, hashes_rank) = match retrieve_peer_headers_sequence_ranking(
  273. (&last_common_height, &last_common_hash, &module, &last_difficulty),
  274. channel,
  275. proposal,
  276. &peer_header_hashes,
  277. )
  278. .await
  279. {
  280. Ok(p) => p,
  281. Err(DatabaseError(e)) => {
  282. error!(target: "darkfid::task::handle_reorg", "Internal error while retrieving peer headers: {e}");
  283. return false
  284. }
  285. Err(e) => {
  286. error!(target: "darkfid::task::handle_reorg", "Retrieving peer headers failed: {e}");
  287. return true
  288. }
  289. };
  290. // Grab the validator lock so no other proposal gets processed
  291. // while we are verifying the sequence.
  292. let mut validator = node.validator.write().await;
  293. // Check if the sequence ranks higher than our current best fork
  294. let index = match best_fork_index(&validator.consensus.forks) {
  295. Ok(i) => i,
  296. Err(e) => {
  297. debug!(target: "darkfid::task::handle_reorg", "Retrieving best fork index failed: {e}");
  298. return false
  299. }
  300. };
  301. let best_fork = &validator.consensus.forks[index];
  302. if targets_rank < best_fork.targets_rank ||
  303. (targets_rank == best_fork.targets_rank && hashes_rank <= best_fork.hashes_rank)
  304. {
  305. info!(target: "darkfid::task::handle_reorg", "Peer sequence ranks lower than our current best fork, skipping...");
  306. return true
  307. }
  308. // Generate the peer fork and retrieve its ranking
  309. let mut peer_fork = match retrieve_peer_fork(
  310. &validator,
  311. (&last_common_height, &module, &last_difficulty),
  312. channel,
  313. proposal,
  314. &peer_header_hashes,
  315. )
  316. .await
  317. {
  318. Ok(p) => p,
  319. Err(DatabaseError(e)) => {
  320. error!(target: "darkfid::task::handle_reorg", "Internal error while retrieving peer fork: {e}");
  321. return false
  322. }
  323. Err(e) => {
  324. error!(target: "darkfid::task::handle_reorg", "Retrieving peer fork failed: {e}");
  325. return true
  326. }
  327. };
  328. // Check if the peer fork ranks higher than our current best fork
  329. if peer_fork.targets_rank < best_fork.targets_rank ||
  330. (peer_fork.targets_rank == best_fork.targets_rank &&
  331. peer_fork.hashes_rank <= best_fork.hashes_rank)
  332. {
  333. info!(target: "darkfid::task::handle_reorg", "Peer fork ranks lower than our current best fork, skipping...");
  334. return true
  335. }
  336. // Execute the reorg
  337. info!(target: "darkfid::task::handle_reorg", "Peer fork ranks higher than our current best fork, executing reorg...");
  338. if let Err(e) = validator.blockchain.reset_to_height(last_common_height) {
  339. error!(target: "darkfid::task::handle_reorg", "Applying full inverse diff failed: {e}");
  340. return false
  341. };
  342. // Update fork diffs to forward-only ones
  343. let overlay = match BlockchainOverlay::new(&validator.blockchain) {
  344. Ok(o) => o,
  345. Err(e) => {
  346. error!(target: "darkfid::task::handle_reorg", "Generating a new blockchain overlay failed: {e}");
  347. return false
  348. }
  349. };
  350. let mut diffs = Vec::with_capacity(peer_fork.diffs.len());
  351. for diff in peer_fork.diffs {
  352. let overlay = overlay.lock().unwrap();
  353. let mut overlay = overlay.overlay.lock().unwrap();
  354. if let Err(e) = overlay.add_diff(&diff) {
  355. error!(target: "darkfid::task::handle_reorg", "Applying peer fork diff failed: {e}");
  356. return false
  357. }
  358. match overlay.diff(&diffs) {
  359. Ok(diff) => diffs.push(diff),
  360. Err(e) => {
  361. error!(target: "darkfid::task::handle_reorg", "Generate clean state inverse diff failed: {e}");
  362. return false
  363. }
  364. }
  365. }
  366. peer_fork.overlay = overlay;
  367. peer_fork.diffs = diffs;
  368. // Update validator consensus state
  369. validator.consensus.module = module;
  370. validator.consensus.forks = vec![peer_fork];
  371. // Check if we can confirm anything and broadcast them
  372. let confirmed = match validator.confirmation().await {
  373. Ok(f) => f,
  374. Err(e) => {
  375. error!(target: "darkfid::task::handle_reorg", "Confirmation failed: {e}");
  376. return false
  377. }
  378. };
  379. // Refresh mining registry
  380. if let Err(e) = node.registry.state.write().await.refresh(&validator).await {
  381. error!(target: "darkfid::task::handle_reorg", "Failed refreshing mining block templates: {e}")
  382. }
  383. if !confirmed.is_empty() {
  384. let mut notif_blocks = Vec::with_capacity(confirmed.len());
  385. for block in confirmed {
  386. notif_blocks.push(JsonValue::String(base64::encode(&serialize_async(&block).await)));
  387. }
  388. node.subscribers.get("blocks").unwrap().notify(JsonValue::Array(notif_blocks)).await;
  389. }
  390. // Broadcast proposal to the network
  391. let message = ProposalMessage(proposal.clone());
  392. node.p2p_handler.p2p.broadcast(&message).await;
  393. // Notify proposals subscriber
  394. let enc_prop = JsonValue::String(base64::encode(&serialize_async(proposal).await));
  395. node.subscribers.get("proposals").unwrap().notify(vec![enc_prop].into()).await;
  396. false
  397. }
  398. /// Auxiliary function to retrieve the last common header and height,
  399. /// along with the headers sequence up to provided peer proposal.
  400. async fn retrieve_peer_header_hashes(
  401. // Validator pointer
  402. validator: &ValidatorPtr,
  403. // Peer channel and its communications timeout
  404. channel: &(&ChannelPtr, &u64),
  405. // Peer fork proposal
  406. proposal: &Proposal,
  407. ) -> Result<(u32, HeaderHash, Vec<HeaderHash>)> {
  408. // Communication setup
  409. let response_sub = channel.0.subscribe_msg::<ForkHeaderHashResponse>().await?;
  410. // Keep track of received header hashes sequence
  411. let mut peer_header_hashes = vec![];
  412. // Find last common header, going backwards from the proposal
  413. let mut previous_height = proposal.block.header.height;
  414. let mut previous_hash = proposal.hash;
  415. for height in (0..proposal.block.header.height).rev() {
  416. // Request peer header hash for this height
  417. let request = ForkHeaderHashRequest { height, fork_header: proposal.hash };
  418. channel.0.send(&request).await?;
  419. // Node waits for response
  420. let response = response_sub.receive_with_timeout(*channel.1).await?;
  421. debug!(target: "darkfid::task::handle_reorg", "Peer response: {response:?}");
  422. // Check if peer returned a header
  423. let Some(peer_header) = response.fork_header else {
  424. return Err(Custom(String::from("Peer responded with an empty header")))
  425. };
  426. // Check if we know this header
  427. let headers = match validator.read().await.blockchain.blocks.get_order(&[height], false) {
  428. Ok(h) => h,
  429. Err(e) => return Err(DatabaseError(format!("Retrieving headers failed: {e}"))),
  430. };
  431. match headers[0] {
  432. Some(known_header) => {
  433. if known_header == peer_header {
  434. previous_height = height;
  435. previous_hash = known_header;
  436. break
  437. }
  438. // Since we retrieve in right -> left order we push them in reverse order
  439. peer_header_hashes.insert(0, peer_header);
  440. }
  441. None => peer_header_hashes.insert(0, peer_header),
  442. }
  443. }
  444. Ok((previous_height, previous_hash, peer_header_hashes))
  445. }
  446. /// Auxiliary function to retrieve provided peer headers hashes
  447. /// sequence and its ranking, based on provided last common
  448. /// information.
  449. async fn retrieve_peer_headers_sequence_ranking(
  450. // Last common header, PoW module and difficulty
  451. last_common_info: (&u32, &HeaderHash, &PoWModule, &BlockDifficulty),
  452. // Peer channel and its communications timeout
  453. channel: &(&ChannelPtr, &u64),
  454. // Peer fork trigger proposal
  455. proposal: &Proposal,
  456. // Peer header hashes sequence
  457. header_hashes: &[HeaderHash],
  458. ) -> Result<(BigUint, BigUint)> {
  459. // Communication setup
  460. let response_sub = channel.0.subscribe_msg::<ForkHeadersResponse>().await?;
  461. // Retrieve the headers of the hashes sequence, in batches, keeping track of the sequence ranking
  462. info!(target: "darkfid::task::handle_reorg", "Retrieving {} headers from peer...", header_hashes.len());
  463. let mut previous_height = *last_common_info.0;
  464. let mut previous_hash = *last_common_info.1;
  465. let mut module = last_common_info.2.clone();
  466. let mut targets_rank = last_common_info.3.ranks.targets_rank.clone();
  467. let mut hashes_rank = last_common_info.3.ranks.hashes_rank.clone();
  468. let mut batch = Vec::with_capacity(BATCH);
  469. let mut total_processed = 0;
  470. for (index, hash) in header_hashes.iter().enumerate() {
  471. // Add hash in batch sequence
  472. batch.push(*hash);
  473. // Check if batch is full so we can send it
  474. if batch.len() < BATCH && index != header_hashes.len() - 1 {
  475. continue
  476. }
  477. // Request peer headers
  478. let request = ForkHeadersRequest { headers: batch.clone(), fork_header: proposal.hash };
  479. channel.0.send(&request).await?;
  480. // Node waits for response
  481. let response = response_sub.receive_with_timeout(*channel.1).await?;
  482. debug!(target: "darkfid::task::handle_reorg", "Peer response: {response:?}");
  483. // Response sequence must be the same length as the one requested
  484. if response.headers.len() != batch.len() {
  485. return Err(Custom(String::from(
  486. "Peer responded with a different headers sequence length",
  487. )))
  488. }
  489. // Process retrieved headers
  490. for (peer_header_index, peer_header) in response.headers.iter().enumerate() {
  491. let peer_header_hash = peer_header.hash();
  492. debug!(target: "darkfid::task::handle_reorg", "Processing header: {peer_header_hash} - {}", peer_header.height);
  493. // Validate its the header we requested
  494. if peer_header_hash != batch[peer_header_index] {
  495. return Err(Custom(format!(
  496. "Peer responded with a differend header: {} - {peer_header_hash}",
  497. batch[peer_header_index]
  498. )))
  499. }
  500. // Validate sequence is correct
  501. if peer_header.previous != previous_hash || peer_header.height != previous_height + 1 {
  502. return Err(Custom(String::from("Invalid header sequence detected")))
  503. }
  504. // Verify header hash and calculate its rank
  505. let (next_difficulty, target_distance_sq, hash_distance_sq) =
  506. match header_rank(&mut module, peer_header) {
  507. Ok(tuple) => tuple,
  508. Err(PoWInvalidOutHash) => return Err(PoWInvalidOutHash),
  509. Err(e) => {
  510. return Err(DatabaseError(format!("Computing header rank failed: {e}")))
  511. }
  512. };
  513. // Update sequence ranking
  514. targets_rank += target_distance_sq.clone();
  515. hashes_rank += hash_distance_sq.clone();
  516. // Update PoW headers module
  517. module.append(peer_header, &next_difficulty)?;
  518. // Set previous header
  519. previous_height = peer_header.height;
  520. previous_hash = peer_header_hash;
  521. }
  522. total_processed += response.headers.len();
  523. info!(target: "darkfid::task::handle_reorg", "Headers received and verified: {total_processed}/{}", header_hashes.len());
  524. // Reset batch
  525. batch = Vec::with_capacity(BATCH);
  526. }
  527. // Validate trigger proposal header sequence is correct
  528. if proposal.block.header.previous != previous_hash ||
  529. proposal.block.header.height != previous_height + 1
  530. {
  531. return Err(Custom(String::from("Invalid header sequence detected")))
  532. }
  533. // Verify trigger proposal header hash and calculate its rank
  534. let (_, target_distance_sq, hash_distance_sq) =
  535. match header_rank(&mut module, &proposal.block.header) {
  536. Ok(tuple) => tuple,
  537. Err(PoWInvalidOutHash) => return Err(PoWInvalidOutHash),
  538. Err(e) => return Err(DatabaseError(format!("Computing header rank failed: {e}"))),
  539. };
  540. // Update sequence ranking
  541. targets_rank += target_distance_sq.clone();
  542. hashes_rank += hash_distance_sq.clone();
  543. Ok((targets_rank, hashes_rank))
  544. }
  545. /// Auxiliary function to generate provided peer headers hashes fork
  546. /// and its ranking, based on provided last common information.
  547. async fn retrieve_peer_fork(
  548. // Validator pointer
  549. validator: &Validator,
  550. // Last common header height, PoW module and difficulty
  551. last_common_info: (&u32, &PoWModule, &BlockDifficulty),
  552. // Peer channel and its communications timeout
  553. channel: &(&ChannelPtr, &u64),
  554. // Peer fork trigger proposal
  555. proposal: &Proposal,
  556. // Peer header hashes sequence
  557. header_hashes: &[HeaderHash],
  558. ) -> Result<Fork> {
  559. // Communication setup
  560. let response_sub = channel.0.subscribe_msg::<ForkProposalsResponse>().await?;
  561. // Create a fork from last common height
  562. let mut peer_fork =
  563. match Fork::new(validator.consensus.blockchain.clone(), last_common_info.1.clone()).await {
  564. Ok(f) => f,
  565. Err(e) => return Err(DatabaseError(format!("Generating peer fork failed: {e}"))),
  566. };
  567. peer_fork.targets_rank = last_common_info.2.ranks.targets_rank.clone();
  568. peer_fork.hashes_rank = last_common_info.2.ranks.hashes_rank.clone();
  569. // Grab all state inverse diffs after last common height, and add them to the fork
  570. let inverse_diffs =
  571. match validator.blockchain.blocks.get_state_inverse_diffs_after(*last_common_info.0) {
  572. Ok(i) => i,
  573. Err(e) => {
  574. return Err(DatabaseError(format!("Retrieving state inverse diffs failed: {e}")))
  575. }
  576. };
  577. for inverse_diff in inverse_diffs.iter().rev() {
  578. let result =
  579. peer_fork.overlay.lock().unwrap().overlay.lock().unwrap().add_diff(inverse_diff);
  580. if let Err(e) = result {
  581. return Err(DatabaseError(format!("Applying state inverse diff failed: {e}")))
  582. }
  583. }
  584. // Grab current overlay diff and use it as the first diff of the
  585. // peer fork, so all consecutive diffs represent just the proposal
  586. // changes.
  587. let diff = peer_fork.overlay.lock().unwrap().overlay.lock().unwrap().diff(&[]);
  588. let diff = match diff {
  589. Ok(d) => d,
  590. Err(e) => {
  591. return Err(DatabaseError(format!("Generate full state inverse diff failed: {e}")))
  592. }
  593. };
  594. peer_fork.diffs = vec![diff];
  595. // Retrieve the proposals of the hashes sequence, in batches
  596. info!(target: "darkfid::task::handle_reorg", "Peer sequence ranks higher than our current best fork, retrieving {} proposals from peer...", header_hashes.len());
  597. let mut batch = Vec::with_capacity(BATCH);
  598. let mut total_processed = 0;
  599. for (index, hash) in header_hashes.iter().enumerate() {
  600. // Add hash in batch sequence
  601. batch.push(*hash);
  602. // Check if batch is full so we can send it
  603. if batch.len() < BATCH && index != header_hashes.len() - 1 {
  604. continue
  605. }
  606. // Request peer proposals
  607. let request = ForkProposalsRequest { headers: batch.clone(), fork_header: proposal.hash };
  608. channel.0.send(&request).await?;
  609. // Node waits for response
  610. let response = response_sub.receive_with_timeout(*channel.1).await?;
  611. debug!(target: "darkfid::task::handle_reorg", "Peer response: {response:?}");
  612. // Response sequence must be the same length as the one requested
  613. if response.proposals.len() != batch.len() {
  614. return Err(Custom(String::from(
  615. "Peer responded with a different proposals sequence length",
  616. )))
  617. }
  618. // Process retrieved proposal
  619. for (peer_proposal_index, peer_proposal) in response.proposals.iter().enumerate() {
  620. info!(target: "darkfid::task::handle_reorg", "Processing proposal: {} - {}", peer_proposal.hash, peer_proposal.block.header.height);
  621. // Validate its the proposal we requested
  622. if peer_proposal.hash != batch[peer_proposal_index] {
  623. return Err(Custom(format!(
  624. "Peer responded with a differend proposal: {} - {}",
  625. batch[peer_proposal_index], peer_proposal.hash
  626. )))
  627. }
  628. // Verify proposal
  629. verify_fork_proposal(&mut peer_fork, peer_proposal, validator.verify_fees).await?;
  630. // Append proposal
  631. peer_fork.append_proposal(peer_proposal).await?;
  632. }
  633. total_processed += response.proposals.len();
  634. info!(target: "darkfid::task::handle_reorg", "Proposals received and verified: {total_processed}/{}", header_hashes.len());
  635. // Reset batch
  636. batch = Vec::with_capacity(BATCH);
  637. }
  638. // Verify trigger proposal
  639. verify_fork_proposal(&mut peer_fork, proposal, validator.verify_fees).await?;
  640. // Append trigger proposal
  641. peer_fork.append_proposal(proposal).await?;
  642. // Remove the reorg diff from the fork
  643. peer_fork.diffs.remove(0);
  644. Ok(peer_fork)
  645. }