@@ -41,6 +41,7 @@ class StreamEnd(IntFlag):
41
41
TX_NOT_CONFIRMED = 4
42
42
INVALID_PARAMS = 5
43
43
INTERNAL_ERROR = 6
44
+ PER_REQUEST = 7
44
45
45
46
def __str__ (self ):
46
47
if self is StreamEnd .END_HASH_REACHED :
@@ -57,6 +58,8 @@ def __str__(self):
57
58
return 'streamed with invalid parameters'
58
59
elif self is StreamEnd .INTERNAL_ERROR :
59
60
return 'internal error'
61
+ elif self is StreamEnd .PER_REQUEST :
62
+ return 'stopped per request'
60
63
else :
61
64
raise ValueError (f'invalid StreamEnd value: { self .value } ' )
62
65
@@ -99,12 +102,15 @@ def safe_send_next(self) -> None:
99
102
try :
100
103
self .send_next ()
101
104
except Exception :
102
- self .stop ()
103
- self .sync_agent .send_blocks_end (StreamEnd .INTERNAL_ERROR )
105
+ self ._stop_streaming_server (StreamEnd .INTERNAL_ERROR )
104
106
raise
105
107
else :
106
108
self .schedule_if_needed ()
107
109
110
+ def _stop_streaming_server (self , response_code : StreamEnd ) -> None :
111
+ """Stop streaming server."""
112
+ raise NotImplementedError
113
+
108
114
def start (self ) -> None :
109
115
"""Start pushing."""
110
116
self .log .debug ('start streaming' )
@@ -153,6 +159,9 @@ def __init__(self, sync_agent: 'NodeBlockSync', start_block: Block, end_hash: by
153
159
self .end_hash = end_hash
154
160
self .reverse = reverse
155
161
162
+ def _stop_streaming_server (self , response_code : StreamEnd ) -> None :
163
+ self .sync_agent .stop_blk_streaming_server (response_code )
164
+
156
165
def send_next (self ) -> None :
157
166
"""Push next block to peer."""
158
167
assert self .is_running
@@ -165,26 +174,23 @@ def send_next(self) -> None:
165
174
166
175
meta = cur .get_metadata ()
167
176
if meta .voided_by :
168
- self .stop ()
169
- self .sync_agent .send_blocks_end (StreamEnd .STREAM_BECAME_VOIDED )
177
+ self .sync_agent .stop_blk_streaming_server (StreamEnd .STREAM_BECAME_VOIDED )
170
178
return
171
179
172
180
if cur .hash == self .end_hash :
173
181
# only send the last when not reverse
174
182
if not self .reverse :
175
183
self .log .debug ('send next block' , blk_id = cur .hash .hex ())
176
184
self .sync_agent .send_blocks (cur )
177
- self .stop ()
178
- self .sync_agent .send_blocks_end (StreamEnd .END_HASH_REACHED )
185
+ self .sync_agent .stop_blk_streaming_server (StreamEnd .END_HASH_REACHED )
179
186
return
180
187
181
188
if self .counter >= self .limit :
182
189
# only send the last when not reverse
183
190
if not self .reverse :
184
191
self .log .debug ('send next block' , blk_id = cur .hash .hex ())
185
192
self .sync_agent .send_blocks (cur )
186
- self .stop ()
187
- self .sync_agent .send_blocks_end (StreamEnd .LIMIT_EXCEEDED )
193
+ self .sync_agent .stop_blk_streaming_server (StreamEnd .LIMIT_EXCEEDED )
188
194
return
189
195
190
196
self .counter += 1
@@ -199,8 +205,7 @@ def send_next(self) -> None:
199
205
200
206
# XXX: don't send the genesis or the current block
201
207
if self .current_block is None or self .current_block .is_genesis :
202
- self .stop ()
203
- self .sync_agent .send_blocks_end (StreamEnd .NO_MORE_BLOCKS )
208
+ self .sync_agent .stop_blk_streaming_server (StreamEnd .NO_MORE_BLOCKS )
204
209
return
205
210
206
211
@@ -235,6 +240,9 @@ def __init__(self,
235
240
self .bfs = BFSOrderWalk (self .tx_storage , is_dag_verifications = True , is_dag_funds = True , is_left_to_right = False )
236
241
self .iter = self .get_iter ()
237
242
243
+ def _stop_streaming_server (self , response_code : StreamEnd ) -> None :
244
+ self .sync_agent .stop_tx_streaming_server (response_code )
245
+
238
246
def get_iter (self ) -> Iterator [BaseTransaction ]:
239
247
"""Return an iterator that yields all transactions confirmed by each block in sequence."""
240
248
root : Union [BaseTransaction , Iterable [BaseTransaction ]]
@@ -258,8 +266,7 @@ def get_iter(self) -> Iterator[BaseTransaction]:
258
266
259
267
# Check if this block is still in the best blockchain.
260
268
if self .current_block .get_metadata ().voided_by :
261
- self .stop ()
262
- self .sync_agent .send_blocks_end (StreamEnd .STREAM_BECAME_VOIDED )
269
+ self .sync_agent .stop_tx_streaming_server (StreamEnd .STREAM_BECAME_VOIDED )
263
270
return
264
271
265
272
self .current_block = self .current_block .get_next_block_best_chain ()
@@ -275,8 +282,7 @@ def send_next(self) -> None:
275
282
except StopIteration :
276
283
# nothing more to send
277
284
self .log .debug ('no more transactions, stopping streaming' )
278
- self .stop ()
279
- self .sync_agent .send_transactions_end (StreamEnd .END_HASH_REACHED )
285
+ self .sync_agent .stop_tx_streaming_server (StreamEnd .END_HASH_REACHED )
280
286
return
281
287
282
288
# Skip blocks.
@@ -290,8 +296,7 @@ def send_next(self) -> None:
290
296
cur_metadata = cur .get_metadata ()
291
297
if cur_metadata .first_block is None :
292
298
self .log .debug ('reached a tx that is not confirmed, stopping streaming' )
293
- self .stop ()
294
- self .sync_agent .send_transactions_end (StreamEnd .TX_NOT_CONFIRMED )
299
+ self .sync_agent .stop_tx_streaming_server (StreamEnd .TX_NOT_CONFIRMED )
295
300
return
296
301
297
302
# Check if tx is confirmed by the `self.current_block` or any next block.
@@ -308,6 +313,6 @@ def send_next(self) -> None:
308
313
309
314
self .counter += 1
310
315
if self .counter >= self .limit :
311
- self .stop ( )
312
- self .sync_agent .send_transactions_end (StreamEnd .LIMIT_EXCEEDED )
316
+ self .log . debug ( 'limit exceeded, stopping streaming' )
317
+ self .sync_agent .stop_tx_streaming_server (StreamEnd .LIMIT_EXCEEDED )
313
318
return
0 commit comments