Skip to content

Commit 6348e93

Browse files
authored
Merge pull request #557 from rustaceanrob/26-3-18-block-ev
Remove `Event::Block`
2 parents a9419f5 + 18c1824 commit 6348e93

3 files changed

Lines changed: 35 additions & 48 deletions

File tree

src/chain/block_queue.rs

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -94,49 +94,32 @@ impl BlockQueue {
9494
#[derive(Debug)]
9595
pub(crate) struct Request {
9696
hash: BlockHash,
97-
recipient: BlockRecipient,
97+
recipient: oneshot::Sender<Result<IndexedBlock, FetchBlockError>>,
9898
}
9999

100100
impl Request {
101-
fn new(hash: BlockHash) -> Self {
102-
Self {
103-
hash,
104-
recipient: BlockRecipient::Event,
105-
}
106-
}
107-
108101
fn from_block_request(
109102
block_request: ClientRequest<BlockHash, Result<IndexedBlock, FetchBlockError>>,
110103
) -> Self {
111104
let (hash, oneshot) = block_request.into_values();
112105
Self {
113106
hash,
114-
recipient: BlockRecipient::Client(oneshot),
107+
recipient: oneshot,
115108
}
116109
}
117110
}
118111

119-
impl From<BlockHash> for Request {
120-
fn from(value: BlockHash) -> Self {
121-
Request::new(value)
122-
}
123-
}
124-
125112
impl From<ClientRequest<BlockHash, Result<IndexedBlock, FetchBlockError>>> for Request {
126113
fn from(value: ClientRequest<BlockHash, Result<IndexedBlock, FetchBlockError>>) -> Self {
127114
Request::from_block_request(value)
128115
}
129116
}
130117

131-
#[derive(Debug)]
132-
pub(crate) enum BlockRecipient {
133-
Client(oneshot::Sender<Result<IndexedBlock, FetchBlockError>>),
134-
Event,
135-
}
136-
137118
#[derive(Debug)]
138119
pub(crate) enum ProcessBlockResponse {
139-
Accepted { block_recipient: BlockRecipient },
120+
Accepted {
121+
block_recipient: oneshot::Sender<Result<IndexedBlock, FetchBlockError>>,
122+
},
140123
LateResponse,
141124
UnknownHash,
142125
}
@@ -161,14 +144,26 @@ mod test {
161144
[hash_1, hash_2, hash_3]
162145
}
163146

147+
trait DummyRequestExt {
148+
fn dummy_request(&self) -> Request;
149+
}
150+
151+
impl DummyRequestExt for BlockHash {
152+
fn dummy_request(&self) -> Request {
153+
let (tx, _rx) = oneshot::channel();
154+
let client_request = ClientRequest::new(*self, tx);
155+
Request::from_block_request(client_request)
156+
}
157+
}
158+
164159
#[test]
165160
fn test_block_queue() {
166161
let [hash_1, hash_2, hash_3] = three_block_hashes();
167162
let mut queue = BlockQueue::new();
168-
queue.add(hash_1);
169-
queue.add(hash_2);
170-
queue.add(hash_3);
171-
queue.add(hash_1);
163+
queue.add(hash_1.dummy_request());
164+
queue.add(hash_2.dummy_request());
165+
queue.add(hash_3.dummy_request());
166+
queue.add(hash_1.dummy_request());
172167
assert_eq!(queue.queue.len(), 4);
173168
assert_eq!(queue.pop(), Some(hash_1));
174169
assert_eq!(queue.pop(), None);
@@ -201,9 +196,9 @@ mod test {
201196
async fn test_laggy_peer() {
202197
let [hash_1, hash_2, hash_3] = three_block_hashes();
203198
let mut queue = BlockQueue::new();
204-
queue.add(hash_1);
205-
queue.add(hash_2);
206-
queue.add(hash_3);
199+
queue.add(hash_1.dummy_request());
200+
queue.add(hash_2.dummy_request());
201+
queue.add(hash_3.dummy_request());
207202
assert_eq!(queue.queue.len(), 3);
208203
assert_eq!(queue.pop(), Some(hash_1));
209204
tokio::time::sleep(Duration::from_secs(6)).await;
@@ -241,10 +236,10 @@ mod test {
241236
fn test_blocks_removed() {
242237
let [hash_1, hash_2, hash_3] = three_block_hashes();
243238
let mut queue = BlockQueue::new();
244-
queue.add(hash_1);
245-
queue.add(hash_2);
246-
queue.add(hash_3);
247-
queue.add(hash_1);
239+
queue.add(hash_1.dummy_request());
240+
queue.add(hash_2.dummy_request());
241+
queue.add(hash_3.dummy_request());
242+
queue.add(hash_1.dummy_request());
248243
assert_eq!(queue.queue.len(), 4);
249244
assert_eq!(queue.pop(), Some(hash_1));
250245
assert_eq!(

src/messages.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ impl core::fmt::Display for Info {
4343
/// Data and structures useful for a consumer, such as a wallet.
4444
#[derive(Debug, Clone)]
4545
pub enum Event {
46-
/// A [`Block`](crate) that was requested.
47-
Block(IndexedBlock),
4846
/// The chain of block headers has been altered in some way.
4947
ChainUpdate(BlockHeaderChanges),
5048
/// The node is fully synced, having scanned the requested range.

src/node.rs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use tokio::{
2525

2626
use crate::{
2727
chain::{
28-
block_queue::{BlockQueue, BlockRecipient, ProcessBlockResponse},
28+
block_queue::{BlockQueue, ProcessBlockResponse},
2929
chain::Chain,
3030
checkpoints::HeaderCheckpoint,
3131
error::HeaderSyncError,
@@ -552,18 +552,12 @@ impl Node {
552552
self.dialog
553553
.send_info(Info::BlockReceived(block.block_hash()))
554554
.await;
555-
match block_recipient {
556-
BlockRecipient::Client(sender) => {
557-
let send_err = sender.send(Ok(IndexedBlock::new(height, block))).is_err();
558-
if send_err {
559-
self.dialog.send_warning(Warning::ChannelDropped);
560-
};
561-
}
562-
BlockRecipient::Event => {
563-
self.dialog
564-
.send_event(Event::Block(IndexedBlock::new(height, block)));
565-
}
566-
}
555+
let send_err = block_recipient
556+
.send(Ok(IndexedBlock::new(height, block)))
557+
.is_err();
558+
if send_err {
559+
self.dialog.send_warning(Warning::ChannelDropped);
560+
};
567561
}
568562
ProcessBlockResponse::LateResponse => {
569563
crate::debug!(format!(

0 commit comments

Comments
 (0)