unknown_proposal.rs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  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, 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 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. validator.consensus.module = module;
  343. validator.consensus.forks = vec![peer_fork];
  344. // Check if we can confirm anything and broadcast them
  345. let confirmed = match validator.confirmation().await {
  346. Ok(f) => f,
  347. Err(e) => {
  348. error!(target: "darkfid::task::handle_reorg", "Confirmation failed: {e}");
  349. return false
  350. }
  351. };
  352. // Refresh mining registry
  353. if let Err(e) = node.registry.refresh(&validator).await {
  354. error!(target: "darkfid::task::handle_reorg", "Failed refreshing mining block templates: {e}")
  355. }
  356. if !confirmed.is_empty() {
  357. let mut notif_blocks = Vec::with_capacity(confirmed.len());
  358. for block in confirmed {
  359. notif_blocks.push(JsonValue::String(base64::encode(&serialize_async(&block).await)));
  360. }
  361. node.subscribers.get("blocks").unwrap().notify(JsonValue::Array(notif_blocks)).await;
  362. }
  363. // Broadcast proposal to the network
  364. let message = ProposalMessage(proposal.clone());
  365. node.p2p_handler.p2p.broadcast(&message).await;
  366. // Notify proposals subscriber
  367. let enc_prop = JsonValue::String(base64::encode(&serialize_async(proposal).await));
  368. node.subscribers.get("proposals").unwrap().notify(vec![enc_prop].into()).await;
  369. false
  370. }
  371. /// Auxiliary function to retrieve the last common header and height,
  372. /// along with the headers sequence up to provided peer proposal.
  373. async fn retrieve_peer_header_hashes(
  374. // Validator pointer
  375. validator: &ValidatorPtr,
  376. // Peer channel and its communications timeout
  377. channel: &(&ChannelPtr, &u64),
  378. // Peer fork proposal
  379. proposal: &Proposal,
  380. ) -> Result<(u32, HeaderHash, Vec<HeaderHash>)> {
  381. // Communication setup
  382. let response_sub = channel.0.subscribe_msg::<ForkHeaderHashResponse>().await?;
  383. // Keep track of received header hashes sequence
  384. let mut peer_header_hashes = vec![];
  385. // Find last common header, going backwards from the proposal
  386. let mut previous_height = proposal.block.header.height;
  387. let mut previous_hash = proposal.hash;
  388. for height in (0..proposal.block.header.height).rev() {
  389. // Request peer header hash for this height
  390. let request = ForkHeaderHashRequest { height, fork_header: proposal.hash };
  391. channel.0.send(&request).await?;
  392. // Node waits for response
  393. let response = response_sub.receive_with_timeout(*channel.1).await?;
  394. debug!(target: "darkfid::task::handle_reorg", "Peer response: {response:?}");
  395. // Check if peer returned a header
  396. let Some(peer_header) = response.fork_header else {
  397. return Err(Custom(String::from("Peer responded with an empty header")))
  398. };
  399. // Check if we know this header
  400. let headers = match validator.read().await.blockchain.blocks.get_order(&[height], false) {
  401. Ok(h) => h,
  402. Err(e) => return Err(DatabaseError(format!("Retrieving headers failed: {e}"))),
  403. };
  404. match headers[0] {
  405. Some(known_header) => {
  406. if known_header == peer_header {
  407. previous_height = height;
  408. previous_hash = known_header;
  409. break
  410. }
  411. // Since we retrieve in right -> left order we push them in reverse order
  412. peer_header_hashes.insert(0, peer_header);
  413. }
  414. None => peer_header_hashes.insert(0, peer_header),
  415. }
  416. }
  417. Ok((previous_height, previous_hash, peer_header_hashes))
  418. }
  419. /// Auxiliary function to retrieve provided peer headers hashes
  420. /// sequence and its ranking, based on provided last common
  421. /// information.
  422. async fn retrieve_peer_headers_sequence_ranking(
  423. // Last common header, PoW module and difficulty
  424. last_common_info: (&u32, &HeaderHash, &PoWModule, &BlockDifficulty),
  425. // Peer channel and its communications timeout
  426. channel: &(&ChannelPtr, &u64),
  427. // Peer fork trigger proposal
  428. proposal: &Proposal,
  429. // Peer header hashes sequence
  430. header_hashes: &[HeaderHash],
  431. ) -> Result<(BigUint, BigUint)> {
  432. // Communication setup
  433. let response_sub = channel.0.subscribe_msg::<ForkHeadersResponse>().await?;
  434. // Retrieve the headers of the hashes sequence, in batches, keeping track of the sequence ranking
  435. info!(target: "darkfid::task::handle_reorg", "Retrieving {} headers from peer...", header_hashes.len());
  436. let mut previous_height = *last_common_info.0;
  437. let mut previous_hash = *last_common_info.1;
  438. let mut module = last_common_info.2.clone();
  439. let mut targets_rank = last_common_info.3.ranks.targets_rank.clone();
  440. let mut hashes_rank = last_common_info.3.ranks.hashes_rank.clone();
  441. let mut batch = Vec::with_capacity(BATCH);
  442. let mut total_processed = 0;
  443. for (index, hash) in header_hashes.iter().enumerate() {
  444. // Add hash in batch sequence
  445. batch.push(*hash);
  446. // Check if batch is full so we can send it
  447. if batch.len() < BATCH && index != header_hashes.len() - 1 {
  448. continue
  449. }
  450. // Request peer headers
  451. let request = ForkHeadersRequest { headers: batch.clone(), fork_header: proposal.hash };
  452. channel.0.send(&request).await?;
  453. // Node waits for response
  454. let response = response_sub.receive_with_timeout(*channel.1).await?;
  455. debug!(target: "darkfid::task::handle_reorg", "Peer response: {response:?}");
  456. // Response sequence must be the same length as the one requested
  457. if response.headers.len() != batch.len() {
  458. return Err(Custom(String::from(
  459. "Peer responded with a different headers sequence length",
  460. )))
  461. }
  462. // Process retrieved headers
  463. for (peer_header_index, peer_header) in response.headers.iter().enumerate() {
  464. let peer_header_hash = peer_header.hash();
  465. debug!(target: "darkfid::task::handle_reorg", "Processing header: {peer_header_hash} - {}", peer_header.height);
  466. // Validate its the header we requested
  467. if peer_header_hash != batch[peer_header_index] {
  468. return Err(Custom(format!(
  469. "Peer responded with a differend header: {} - {peer_header_hash}",
  470. batch[peer_header_index]
  471. )))
  472. }
  473. // Validate sequence is correct
  474. if peer_header.previous != previous_hash || peer_header.height != previous_height + 1 {
  475. return Err(Custom(String::from("Invalid header sequence detected")))
  476. }
  477. // Verify header hash and calculate its rank
  478. let (next_difficulty, target_distance_sq, hash_distance_sq) =
  479. match header_rank(&module, peer_header) {
  480. Ok(tuple) => tuple,
  481. Err(PoWInvalidOutHash) => return Err(PoWInvalidOutHash),
  482. Err(e) => {
  483. return Err(DatabaseError(format!("Computing header rank failed: {e}")))
  484. }
  485. };
  486. // Update sequence ranking
  487. targets_rank += target_distance_sq.clone();
  488. hashes_rank += hash_distance_sq.clone();
  489. // Update PoW headers module
  490. module.append(peer_header, &next_difficulty)?;
  491. // Set previous header
  492. previous_height = peer_header.height;
  493. previous_hash = peer_header_hash;
  494. }
  495. total_processed += response.headers.len();
  496. info!(target: "darkfid::task::handle_reorg", "Headers received and verified: {total_processed}/{}", header_hashes.len());
  497. // Reset batch
  498. batch = Vec::with_capacity(BATCH);
  499. }
  500. // Validate trigger proposal header sequence is correct
  501. if proposal.block.header.previous != previous_hash ||
  502. proposal.block.header.height != previous_height + 1
  503. {
  504. return Err(Custom(String::from("Invalid header sequence detected")))
  505. }
  506. // Verify trigger proposal header hash and calculate its rank
  507. let (_, target_distance_sq, hash_distance_sq) =
  508. match header_rank(&module, &proposal.block.header) {
  509. Ok(tuple) => tuple,
  510. Err(PoWInvalidOutHash) => return Err(PoWInvalidOutHash),
  511. Err(e) => return Err(DatabaseError(format!("Computing header rank failed: {e}"))),
  512. };
  513. // Update sequence ranking
  514. targets_rank += target_distance_sq.clone();
  515. hashes_rank += hash_distance_sq.clone();
  516. Ok((targets_rank, hashes_rank))
  517. }
  518. /// Auxiliary function to generate provided peer headers hashes fork
  519. /// and its ranking, based on provided last common information.
  520. async fn retrieve_peer_fork(
  521. // Validator pointer
  522. validator: &Validator,
  523. // Last common header height, PoW module and difficulty
  524. last_common_info: (&u32, &PoWModule, &BlockDifficulty),
  525. // Peer channel and its communications timeout
  526. channel: &(&ChannelPtr, &u64),
  527. // Peer fork trigger proposal
  528. proposal: &Proposal,
  529. // Peer header hashes sequence
  530. header_hashes: &[HeaderHash],
  531. ) -> Result<Fork> {
  532. // Communication setup
  533. let response_sub = channel.0.subscribe_msg::<ForkProposalsResponse>().await?;
  534. // Create a fork from last common height
  535. let mut peer_fork =
  536. match Fork::new(validator.consensus.blockchain.clone(), last_common_info.1.clone()).await {
  537. Ok(f) => f,
  538. Err(e) => return Err(DatabaseError(format!("Generating peer fork failed: {e}"))),
  539. };
  540. peer_fork.targets_rank = last_common_info.2.ranks.targets_rank.clone();
  541. peer_fork.hashes_rank = last_common_info.2.ranks.hashes_rank.clone();
  542. // Grab all state inverse diffs after last common height, and add them to the fork
  543. let inverse_diffs =
  544. match validator.blockchain.blocks.get_state_inverse_diffs_after(*last_common_info.0) {
  545. Ok(i) => i,
  546. Err(e) => {
  547. return Err(DatabaseError(format!("Retrieving state inverse diffs failed: {e}")))
  548. }
  549. };
  550. for inverse_diff in inverse_diffs.iter().rev() {
  551. let result =
  552. peer_fork.overlay.lock().unwrap().overlay.lock().unwrap().add_diff(inverse_diff);
  553. if let Err(e) = result {
  554. return Err(DatabaseError(format!("Applying state inverse diff failed: {e}")))
  555. }
  556. }
  557. // Grab current overlay diff and use it as the first diff of the
  558. // peer fork, so all consecutive diffs represent just the proposal
  559. // changes.
  560. let diff = peer_fork.overlay.lock().unwrap().overlay.lock().unwrap().diff(&[]);
  561. let diff = match diff {
  562. Ok(d) => d,
  563. Err(e) => {
  564. return Err(DatabaseError(format!("Generate full state inverse diff failed: {e}")))
  565. }
  566. };
  567. peer_fork.diffs = vec![diff];
  568. // Retrieve the proposals of the hashes sequence, in batches
  569. info!(target: "darkfid::task::handle_reorg", "Peer sequence ranks higher than our current best fork, retrieving {} proposals from peer...", header_hashes.len());
  570. let mut batch = Vec::with_capacity(BATCH);
  571. let mut total_processed = 0;
  572. for (index, hash) in header_hashes.iter().enumerate() {
  573. // Add hash in batch sequence
  574. batch.push(*hash);
  575. // Check if batch is full so we can send it
  576. if batch.len() < BATCH && index != header_hashes.len() - 1 {
  577. continue
  578. }
  579. // Request peer proposals
  580. let request = ForkProposalsRequest { headers: batch.clone(), fork_header: proposal.hash };
  581. channel.0.send(&request).await?;
  582. // Node waits for response
  583. let response = response_sub.receive_with_timeout(*channel.1).await?;
  584. debug!(target: "darkfid::task::handle_reorg", "Peer response: {response:?}");
  585. // Response sequence must be the same length as the one requested
  586. if response.proposals.len() != batch.len() {
  587. return Err(Custom(String::from(
  588. "Peer responded with a different proposals sequence length",
  589. )))
  590. }
  591. // Process retrieved proposal
  592. for (peer_proposal_index, peer_proposal) in response.proposals.iter().enumerate() {
  593. info!(target: "darkfid::task::handle_reorg", "Processing proposal: {} - {}", peer_proposal.hash, peer_proposal.block.header.height);
  594. // Validate its the proposal we requested
  595. if peer_proposal.hash != batch[peer_proposal_index] {
  596. return Err(Custom(format!(
  597. "Peer responded with a differend proposal: {} - {}",
  598. batch[peer_proposal_index], peer_proposal.hash
  599. )))
  600. }
  601. // Verify proposal
  602. verify_fork_proposal(&mut peer_fork, peer_proposal, validator.verify_fees).await?;
  603. // Append proposal
  604. peer_fork.append_proposal(peer_proposal).await?;
  605. }
  606. total_processed += response.proposals.len();
  607. info!(target: "darkfid::task::handle_reorg", "Proposals received and verified: {total_processed}/{}", header_hashes.len());
  608. // Reset batch
  609. batch = Vec::with_capacity(BATCH);
  610. }
  611. // Verify trigger proposal
  612. verify_fork_proposal(&mut peer_fork, proposal, validator.verify_fees).await?;
  613. // Append trigger proposal
  614. peer_fork.append_proposal(proposal).await?;
  615. // Remove the reorg diff from the fork
  616. peer_fork.diffs.remove(0);
  617. Ok(peer_fork)
  618. }