@@ -622,42 +622,42 @@ def handle_get_next_blocks(self, payload: str) -> None:
622
622
self .protocol .send_error_and_close_connection ('GET-NEXT-BLOCKS received before previous one finished' )
623
623
return
624
624
data = GetNextBlocksPayload .parse_raw (payload )
625
+ start_block = self ._validate_block (data .start_hash )
626
+ if start_block is None :
627
+ return
628
+ end_block = self ._validate_block (data .end_hash )
629
+ if end_block is None :
630
+ return
625
631
self .send_next_blocks (
626
- start_hash = data . start_hash ,
632
+ start_block = start_block ,
627
633
end_hash = data .end_hash ,
628
634
quantity = data .quantity ,
629
635
)
630
636
631
- def send_next_blocks (self , start_hash : bytes , end_hash : bytes , quantity : int ) -> None :
632
- """ Send a NEXT-BLOCKS message.
633
- """
634
- self .log .debug ('start NEXT-BLOCKS stream' )
637
+ def _validate_block (self , _hash : VertexId ) -> Optional [Block ]:
638
+ """Validate block given in the GET-NEXT-BLOCKS and GET-TRANSACTIONS-BFS messages."""
635
639
try :
636
- blk = self .tx_storage .get_transaction (start_hash )
640
+ blk = self .tx_storage .get_transaction (_hash )
637
641
except TransactionDoesNotExist :
638
- # In case the tx does not exist we send a NOT-FOUND message
639
- self .log . debug ( 'requested start_hash not found' , start_hash = start_hash .hex ())
640
- self . send_message ( ProtocolMessages . NOT_FOUND , start_hash . hex ())
641
- return
642
+ self . log . debug ( 'requested block not found' , blk_id = _hash . hex ())
643
+ self .send_message ( ProtocolMessages . NOT_FOUND , _hash .hex ())
644
+ return None
645
+
642
646
if not isinstance (blk , Block ):
643
- self .log .debug ('request start_hash is not a block' , start_hash = start_hash .hex ())
644
- self .send_message (ProtocolMessages .NOT_FOUND , start_hash .hex ())
645
- return
646
- assert blk .hash is not None
647
- # XXX: it is not an error for the other peer to request a voided block, we'll pretend it doesn't exist, butf
648
- blk_meta = blk .get_metadata ()
649
- if blk_meta .voided_by :
650
- # In case the tx does not exist we send a NOT-FOUND message
651
- self .log .debug ('requested start_hash is voided, continue anyway' , start_hash = start_hash .hex (),
652
- voided_by = [i .hex () for i in blk_meta .voided_by ])
653
- # XXX: we want to be able to not send this, but we do because the remote node could get stuck otherwise
654
- # (tracked by issue #711)
655
- # self.send_message(ProtocolMessages.NOT_FOUND, start_hash.hex())
656
- # return
647
+ self .log .debug ('request block is not a block' , blk_id = _hash .hex ())
648
+ self .send_message (ProtocolMessages .NOT_FOUND , _hash .hex ())
649
+ return None
650
+
651
+ return blk
652
+
653
+ def send_next_blocks (self , start_block : Block , end_hash : bytes , quantity : int ) -> None :
654
+ """ Send a NEXT-BLOCKS message.
655
+ """
656
+ self .log .debug ('start NEXT-BLOCKS stream' )
657
657
if self ._blk_streaming_server is not None and self ._blk_streaming_server .is_running :
658
658
self ._blk_streaming_server .stop ()
659
659
limit = min (quantity , self .DEFAULT_STREAMING_LIMIT )
660
- self ._blk_streaming_server = BlockchainStreamingServer (self , blk , end_hash , limit = limit )
660
+ self ._blk_streaming_server = BlockchainStreamingServer (self , start_block , end_hash , limit = limit )
661
661
self ._blk_streaming_server .start ()
662
662
663
663
def send_blocks (self , blk : Block ) -> None :
@@ -867,44 +867,44 @@ def handle_get_transactions_bfs(self, payload: str) -> None:
867
867
self .log .warn ('ignore GET-TRANSACTIONS-BFS, already streaming' )
868
868
return
869
869
data = GetTransactionsBFSPayload .parse_raw (payload )
870
- # XXX: todo verify this limit while parsing the payload.
870
+
871
871
if len (data .start_from ) > MAX_GET_TRANSACTIONS_BFS_LEN :
872
872
self .log .error ('too many transactions in GET-TRANSACTIONS-BFS' , state = self .state )
873
873
self .protocol .send_error_and_close_connection ('Too many transactions in GET-TRANSACTIONS-BFS' )
874
874
return
875
- self .send_transactions_bfs (data .start_from , data .first_block_hash , data .last_block_hash )
876
875
877
- def send_transactions_bfs (self ,
878
- start_from : list [bytes ],
879
- first_block_hash : bytes ,
880
- last_block_hash : bytes ) -> None :
881
- """ Start a transactions BFS stream.
882
- """
876
+ first_block = self ._validate_block (data .first_block_hash )
877
+ if first_block is None :
878
+ return
879
+
880
+ last_block = self ._validate_block (data .last_block_hash )
881
+ if last_block is None :
882
+ return
883
+
883
884
start_from_txs = []
884
- for start_from_hash in start_from :
885
+ for start_from_hash in data . start_from :
885
886
try :
886
887
start_from_txs .append (self .tx_storage .get_transaction (start_from_hash ))
887
888
except TransactionDoesNotExist :
888
889
# In case the tx does not exist we send a NOT-FOUND message
889
890
self .log .debug ('requested start_from_hash not found' , start_from_hash = start_from_hash .hex ())
890
891
self .send_message (ProtocolMessages .NOT_FOUND , start_from_hash .hex ())
891
892
return
892
- if not self .tx_storage .transaction_exists (first_block_hash ):
893
- # In case the tx does not exist we send a NOT-FOUND message
894
- self .log .debug ('requested first_block_hash not found' , first_block_hash = first_block_hash .hex ())
895
- self .send_message (ProtocolMessages .NOT_FOUND , first_block_hash .hex ())
896
- return
897
- if not self .tx_storage .transaction_exists (last_block_hash ):
898
- # In case the tx does not exist we send a NOT-FOUND message
899
- self .log .debug ('requested last_block_hash not found' , last_block_hash = last_block_hash .hex ())
900
- self .send_message (ProtocolMessages .NOT_FOUND , last_block_hash .hex ())
901
- return
893
+
894
+ self .send_transactions_bfs (start_from_txs , first_block , last_block )
895
+
896
+ def send_transactions_bfs (self ,
897
+ start_from : list [BaseTransaction ],
898
+ first_block : Block ,
899
+ last_block : Block ) -> None :
900
+ """ Start a transactions BFS stream.
901
+ """
902
902
if self ._tx_streaming_server is not None and self ._tx_streaming_server .is_running :
903
903
self ._tx_streaming_server .stop ()
904
904
self ._tx_streaming_server = TransactionsStreamingServer (self ,
905
- start_from_txs ,
906
- first_block_hash ,
907
- last_block_hash ,
905
+ start_from ,
906
+ first_block ,
907
+ last_block ,
908
908
limit = self .DEFAULT_STREAMING_LIMIT )
909
909
self ._tx_streaming_server .start ()
910
910
0 commit comments