Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 28 additions & 33 deletions src/chain/block_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,49 +94,32 @@ impl BlockQueue {
#[derive(Debug)]
pub(crate) struct Request {
hash: BlockHash,
recipient: BlockRecipient,
recipient: oneshot::Sender<Result<IndexedBlock, FetchBlockError>>,
}

impl Request {
fn new(hash: BlockHash) -> Self {
Self {
hash,
recipient: BlockRecipient::Event,
}
}

fn from_block_request(
block_request: ClientRequest<BlockHash, Result<IndexedBlock, FetchBlockError>>,
) -> Self {
let (hash, oneshot) = block_request.into_values();
Self {
hash,
recipient: BlockRecipient::Client(oneshot),
recipient: oneshot,
}
}
}

impl From<BlockHash> for Request {
fn from(value: BlockHash) -> Self {
Request::new(value)
}
}

impl From<ClientRequest<BlockHash, Result<IndexedBlock, FetchBlockError>>> for Request {
fn from(value: ClientRequest<BlockHash, Result<IndexedBlock, FetchBlockError>>) -> Self {
Request::from_block_request(value)
}
}

#[derive(Debug)]
pub(crate) enum BlockRecipient {
Client(oneshot::Sender<Result<IndexedBlock, FetchBlockError>>),
Event,
}

#[derive(Debug)]
pub(crate) enum ProcessBlockResponse {
Accepted { block_recipient: BlockRecipient },
Accepted {
block_recipient: oneshot::Sender<Result<IndexedBlock, FetchBlockError>>,
},
LateResponse,
UnknownHash,
}
Expand All @@ -161,14 +144,26 @@ mod test {
[hash_1, hash_2, hash_3]
}

trait DummyRequestExt {
fn dummy_request(&self) -> Request;
}

impl DummyRequestExt for BlockHash {
fn dummy_request(&self) -> Request {
let (tx, _rx) = oneshot::channel();
let client_request = ClientRequest::new(*self, tx);
Request::from_block_request(client_request)
}
}

#[test]
fn test_block_queue() {
let [hash_1, hash_2, hash_3] = three_block_hashes();
let mut queue = BlockQueue::new();
queue.add(hash_1);
queue.add(hash_2);
queue.add(hash_3);
queue.add(hash_1);
queue.add(hash_1.dummy_request());
queue.add(hash_2.dummy_request());
queue.add(hash_3.dummy_request());
queue.add(hash_1.dummy_request());
assert_eq!(queue.queue.len(), 4);
assert_eq!(queue.pop(), Some(hash_1));
assert_eq!(queue.pop(), None);
Expand Down Expand Up @@ -201,9 +196,9 @@ mod test {
async fn test_laggy_peer() {
let [hash_1, hash_2, hash_3] = three_block_hashes();
let mut queue = BlockQueue::new();
queue.add(hash_1);
queue.add(hash_2);
queue.add(hash_3);
queue.add(hash_1.dummy_request());
queue.add(hash_2.dummy_request());
queue.add(hash_3.dummy_request());
assert_eq!(queue.queue.len(), 3);
assert_eq!(queue.pop(), Some(hash_1));
tokio::time::sleep(Duration::from_secs(6)).await;
Expand Down Expand Up @@ -241,10 +236,10 @@ mod test {
fn test_blocks_removed() {
let [hash_1, hash_2, hash_3] = three_block_hashes();
let mut queue = BlockQueue::new();
queue.add(hash_1);
queue.add(hash_2);
queue.add(hash_3);
queue.add(hash_1);
queue.add(hash_1.dummy_request());
queue.add(hash_2.dummy_request());
queue.add(hash_3.dummy_request());
queue.add(hash_1.dummy_request());
assert_eq!(queue.queue.len(), 4);
assert_eq!(queue.pop(), Some(hash_1));
assert_eq!(
Expand Down
2 changes: 0 additions & 2 deletions src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ impl core::fmt::Display for Info {
/// Data and structures useful for a consumer, such as a wallet.
#[derive(Debug, Clone)]
pub enum Event {
/// A [`Block`](crate) that was requested.
Block(IndexedBlock),
/// The chain of block headers has been altered in some way.
ChainUpdate(BlockHeaderChanges),
/// The node is fully synced, having scanned the requested range.
Expand Down
20 changes: 7 additions & 13 deletions src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use tokio::{

use crate::{
chain::{
block_queue::{BlockQueue, BlockRecipient, ProcessBlockResponse},
block_queue::{BlockQueue, ProcessBlockResponse},
chain::Chain,
checkpoints::HeaderCheckpoint,
error::HeaderSyncError,
Expand Down Expand Up @@ -552,18 +552,12 @@ impl Node {
self.dialog
.send_info(Info::BlockReceived(block.block_hash()))
.await;
match block_recipient {
BlockRecipient::Client(sender) => {
let send_err = sender.send(Ok(IndexedBlock::new(height, block))).is_err();
if send_err {
self.dialog.send_warning(Warning::ChannelDropped);
};
}
BlockRecipient::Event => {
self.dialog
.send_event(Event::Block(IndexedBlock::new(height, block)));
}
}
let send_err = block_recipient
.send(Ok(IndexedBlock::new(height, block)))
.is_err();
if send_err {
self.dialog.send_warning(Warning::ChannelDropped);
};
}
ProcessBlockResponse::LateResponse => {
crate::debug!(format!(
Expand Down
Loading