17
17
from structlog import get_logger
18
18
from twisted .internet .defer import Deferred
19
19
20
- from hathor .p2p .sync_v2 .exception import (
21
- InvalidVertexError ,
22
- StreamingError ,
23
- TooManyVerticesReceivedError ,
24
- UnexpectedVertex ,
25
- )
26
- from hathor .p2p .sync_v2 .streamers import DEFAULT_STREAMING_LIMIT , StreamEnd
20
+ from hathor .p2p .sync_v2 .exception import InvalidVertexError , StreamingError , TooManyVerticesReceivedError
21
+ from hathor .p2p .sync_v2 .streamers import StreamEnd
27
22
from hathor .transaction import BaseTransaction
28
23
from hathor .transaction .exceptions import HathorError , TxValidationError
29
24
from hathor .types import VertexId
38
33
class TransactionStreamingClient :
39
34
def __init__ (self ,
40
35
sync_agent : 'NodeBlockSync' ,
41
- partial_blocks : list ['Block' ]) -> None :
36
+ partial_blocks : list ['Block' ],
37
+ * ,
38
+ limit : int ) -> None :
42
39
self .sync_agent = sync_agent
43
40
self .protocol = self .sync_agent .protocol
44
41
self .tx_storage = self .sync_agent .tx_storage
@@ -52,7 +49,7 @@ def __init__(self,
52
49
53
50
self ._tx_received : int = 0
54
51
55
- self ._tx_max_quantity = DEFAULT_STREAMING_LIMIT
52
+ self ._tx_max_quantity = limit
56
53
57
54
self ._idx : int = 0
58
55
self ._buffer : list [VertexId ] = []
@@ -68,6 +65,7 @@ def wait(self) -> Deferred[StreamEnd]:
68
65
def resume (self ) -> Deferred [StreamEnd ]:
69
66
"""Resume receiving vertices."""
70
67
assert self ._deferred .called
68
+ self ._tx_received = 0
71
69
self ._deferred = Deferred ()
72
70
return self ._deferred
73
71
@@ -95,15 +93,21 @@ def handle_transaction(self, tx: BaseTransaction) -> None:
95
93
# Run basic verification.
96
94
if not tx .is_genesis :
97
95
try :
98
- self .manager .verification_service .validate_basic (tx )
96
+ self .manager .verification_service .verify_basic (tx )
99
97
except TxValidationError as e :
100
98
self .fails (InvalidVertexError (repr (e )))
101
99
return
102
100
103
101
# Any repeated transaction will fail this check because they will
104
102
# not belong to the waiting list.
105
103
if tx .hash not in self ._waiting_for :
106
- self .fails (UnexpectedVertex (tx .hash .hex ()))
104
+ if tx .hash in self ._db :
105
+ # This case might happen during a resume, so we just log and keep syncing.
106
+ self .log .info ('duplicated vertex received' , tx_id = tx .hash .hex ())
107
+ else :
108
+ # TODO Uncomment the following code to fail on receiving unexpected vertices.
109
+ # self.fails(UnexpectedVertex(tx.hash.hex()))
110
+ self .log .info ('unexpected vertex received' , tx_id = tx .hash .hex ())
107
111
return
108
112
self ._waiting_for .remove (tx .hash )
109
113
0 commit comments