20
20
from hathor .p2p .sync_v2 .exception import (
21
21
InvalidVertexError ,
22
22
StreamingError ,
23
- TooManyRepeatedVerticesError ,
24
23
TooManyVerticesReceivedError ,
25
24
UnexpectedVertex ,
26
25
)
@@ -49,13 +48,9 @@ def __init__(self,
49
48
50
49
self .partial_blocks = partial_blocks
51
50
52
- # Let's keep it at "infinity" until a known issue is fixed.
53
- self .max_repeated_transactions = 1_000_000
54
-
55
51
self ._deferred : Deferred [StreamEnd ] = Deferred ()
56
52
57
53
self ._tx_received : int = 0
58
- self ._tx_repeated : int = 0
59
54
60
55
self ._tx_max_quantity = DEFAULT_STREAMING_LIMIT
61
56
@@ -96,8 +91,16 @@ def handle_transaction(self, tx: BaseTransaction) -> None:
96
91
97
92
self .log .debug ('tx received' , tx_id = tx .hash .hex ())
98
93
99
- # TODO Handle repeated transactions.
94
+ # Run basic verification.
95
+ # if not tx.is_genesis:
96
+ # try:
97
+ # self.manager.verification_service.validate_basic(tx)
98
+ # except TxValidationError as e:
99
+ # self.fails(InvalidVertexError(repr(e)))
100
+ # return
100
101
102
+ # Any repeated transaction will fail this check because they will
103
+ # not belong to the waiting list.
101
104
if tx .hash not in self ._waiting_for :
102
105
self .fails (UnexpectedVertex (tx .hash .hex ()))
103
106
return
@@ -118,21 +121,9 @@ def handle_transaction(self, tx: BaseTransaction) -> None:
118
121
119
122
if self ._tx_received % 100 == 0 :
120
123
self .log .debug ('tx streaming in progress' , txs_received = self ._tx_received )
121
- return
122
-
123
- assert tx .hash is not None
124
- is_duplicated = False
125
- if self .partial_vertex_exists (tx .hash ):
126
- # We reached a tx we already have. Skip it.
127
- self ._tx_repeated += 1
128
- is_duplicated = True
129
- if self ._tx_repeated > self .max_repeated_transactions :
130
- self .log .debug ('too many repeated transactions received' , total_repeated = self ._tx_repeated )
131
- self .fails (TooManyRepeatedVerticesError ())
132
- if is_duplicated :
133
- pass
134
124
135
125
def get_missing_deps (self , tx : BaseTransaction ) -> Iterator [bytes ]:
126
+ """Return missing dependencies."""
136
127
for dep in tx .get_all_dependencies ():
137
128
if self .tx_storage .transaction_exists (dep ):
138
129
continue
@@ -147,18 +138,15 @@ def handle_transactions_end(self, response_code: StreamEnd) -> None:
147
138
self ._deferred .callback (response_code )
148
139
149
140
def _execute_and_prepare_next (self ) -> None :
150
- self ._buffer .sort (key = lambda vertex_id : self ._db [vertex_id ].timestamp )
151
- for vertex_id in self ._buffer :
152
- tx = self ._db [vertex_id ]
153
- try :
154
- self .manager .on_new_tx (tx , propagate_to_peers = False , fails_silently = False )
155
- except HathorError as e :
156
- self .fails (InvalidVertexError (repr (e )))
157
- return
141
+ """Add the block and its vertices to the DAG."""
142
+ assert not self ._waiting_for
158
143
159
144
blk = self .partial_blocks [self ._idx ]
145
+ vertex_list = [self ._db [_id ] for _id in self ._buffer ]
146
+ vertex_list .sort (key = lambda v : v .timestamp )
147
+
160
148
try :
161
- self .manager . on_new_tx (blk , propagate_to_peers = False , fails_silently = False )
149
+ self .sync_agent . on_block_complete (blk , vertex_list )
162
150
except HathorError as e :
163
151
self .fails (InvalidVertexError (repr (e )))
164
152
return
@@ -168,15 +156,16 @@ def _execute_and_prepare_next(self) -> None:
168
156
self ._prepare_block (self .partial_blocks [self ._idx ])
169
157
170
158
def _prepare_block (self , blk : 'Block' ) -> None :
171
- # Reset everything for the next block
172
- self ._buffer = []
173
- self ._waiting_for = set ()
174
- self ._db = {}
159
+ """ Reset everything for the next block. It also adds blocks that have no dependencies."""
160
+ self ._buffer . clear ()
161
+ self ._waiting_for . clear ()
162
+ self ._db . clear ()
175
163
176
164
# Add pending dependencies from block.
177
165
for dep in blk .get_all_dependencies ():
178
166
if not self .tx_storage .transaction_exists (dep ):
179
167
self ._waiting_for .add (dep )
180
168
169
+ # If block is ready to be added then do it.
181
170
if not self ._waiting_for :
182
171
self ._execute_and_prepare_next ()
0 commit comments