network: remove glob import in message-handling

This clarifies which parts are the handler state and which parts are the
incoming message.
This commit is contained in:
Henry de Valence 2020-09-19 10:33:02 -07:00
parent 9c021025a7
commit 64905563d1
1 changed files with 19 additions and 20 deletions

View File

@ -74,21 +74,20 @@ impl Handler {
// into responses to messages in the internal request/response protocol. // into responses to messages in the internal request/response protocol.
// This conversion is done by a sequence of (request, message) match arms, // This conversion is done by a sequence of (request, message) match arms,
// each of which contains the conversion logic for that pair. // each of which contains the conversion logic for that pair.
use Handler::*;
let mut ignored_msg = None; let mut ignored_msg = None;
// XXX can this be avoided? // XXX can this be avoided?
let tmp_state = std::mem::replace(self, Finished(Ok(Response::Nil))); let tmp_state = std::mem::replace(self, Handler::Finished(Ok(Response::Nil)));
*self = match (tmp_state, msg) { *self = match (tmp_state, msg) {
(Ping(req_nonce), Message::Pong(rsp_nonce)) => { (Handler::Ping(req_nonce), Message::Pong(rsp_nonce)) => {
if req_nonce == rsp_nonce { if req_nonce == rsp_nonce {
Finished(Ok(Response::Nil)) Handler::Finished(Ok(Response::Nil))
} else { } else {
Ping(req_nonce) Handler::Ping(req_nonce)
} }
} }
(Peers, Message::Addr(addrs)) => Finished(Ok(Response::Peers(addrs))), (Handler::Peers, Message::Addr(addrs)) => Handler::Finished(Ok(Response::Peers(addrs))),
( (
TransactionsByHash { Handler::TransactionsByHash {
mut hashes, mut hashes,
mut transactions, mut transactions,
}, },
@ -97,9 +96,9 @@ impl Handler {
if hashes.remove(&transaction.hash()) { if hashes.remove(&transaction.hash()) {
transactions.push(transaction); transactions.push(transaction);
if hashes.is_empty() { if hashes.is_empty() {
Finished(Ok(Response::Transactions(transactions))) Handler::Finished(Ok(Response::Transactions(transactions)))
} else { } else {
TransactionsByHash { Handler::TransactionsByHash {
hashes, hashes,
transactions, transactions,
} }
@ -109,14 +108,14 @@ impl Handler {
// but unsolicited transactions are OK, so leave // but unsolicited transactions are OK, so leave
// for future handling. // for future handling.
ignored_msg = Some(Message::Tx(transaction)); ignored_msg = Some(Message::Tx(transaction));
TransactionsByHash { Handler::TransactionsByHash {
hashes, hashes,
transactions, transactions,
} }
} }
} }
( (
BlocksByHash { Handler::BlocksByHash {
mut hashes, mut hashes,
mut blocks, mut blocks,
}, },
@ -125,36 +124,36 @@ impl Handler {
if hashes.remove(&block.hash()) { if hashes.remove(&block.hash()) {
blocks.push(block); blocks.push(block);
if hashes.is_empty() { if hashes.is_empty() {
Finished(Ok(Response::Blocks(blocks))) Handler::Finished(Ok(Response::Blocks(blocks)))
} else { } else {
BlocksByHash { hashes, blocks } Handler::BlocksByHash { hashes, blocks }
} }
} else { } else {
// Blocks shouldn't be sent unsolicited, // Blocks shouldn't be sent unsolicited,
// so fail the request if we got the wrong one. // so fail the request if we got the wrong one.
Finished(Err(PeerError::WrongBlock.into())) Handler::Finished(Err(PeerError::WrongBlock.into()))
} }
} }
(FindBlocks, Message::Inv(items)) (Handler::FindBlocks, Message::Inv(items))
if items if items
.iter() .iter()
.all(|item| matches!(item, InventoryHash::Block(_))) => .all(|item| matches!(item, InventoryHash::Block(_))) =>
{ {
Finished(Ok(Response::BlockHashes( Handler::Finished(Ok(Response::BlockHashes(
block_hashes(&items[..]).collect(), block_hashes(&items[..]).collect(),
))) )))
} }
(MempoolTransactions, Message::Inv(items)) (Handler::MempoolTransactions, Message::Inv(items))
if items if items
.iter() .iter()
.all(|item| matches!(item, InventoryHash::Tx(_))) => .all(|item| matches!(item, InventoryHash::Tx(_))) =>
{ {
Finished(Ok(Response::TransactionHashes( Handler::Finished(Ok(Response::TransactionHashes(
transaction_hashes(&items[..]).collect(), transaction_hashes(&items[..]).collect(),
))) )))
} }
(FindHeaders, Message::Headers(headers)) => { (Handler::FindHeaders, Message::Headers(headers)) => {
Finished(Ok(Response::BlockHeaders(headers))) Handler::Finished(Ok(Response::BlockHeaders(headers)))
} }
// By default, messages are not responses. // By default, messages are not responses.
(state, msg) => { (state, msg) => {