1
+ from hathor .transaction import Block , Transaction
1
2
from hathor .transaction .token_creation_tx import TokenCreationTransaction
2
3
from tests import unittest
3
4
4
5
5
- class DAGCreatorTestCase (unittest .TestCase ):
6
+ class DAGBuilderTestCase (unittest .TestCase ):
6
7
def setUp (self ):
7
8
super ().setUp ()
8
9
@@ -26,14 +27,12 @@ def test_one_tx(self) -> None:
26
27
b40 --> tx1
27
28
""" )
28
29
29
- for node , vertex in artifacts .list :
30
- self .manager .on_new_tx (vertex , fails_silently = False )
30
+ artifacts .propagate_with (self .manager )
31
31
32
32
v_order = [node .name for node , _ in artifacts .list ]
33
33
34
- tx1 = artifacts .by_name ['tx1' ].vertex
35
- b1 = artifacts .by_name ['b1' ].vertex
36
- b40 = artifacts .by_name ['b40' ].vertex
34
+ b1 , b40 = artifacts .get_typed_vertices (['b1' , 'b40' ], Block )
35
+ tx1 = artifacts .get_typed_vertex ('tx1' , Transaction )
37
36
38
37
# blockchain genesis b[1..50]
39
38
self .assertEqual (b1 .parents [0 ], self ._settings .GENESIS_BLOCK_HASH )
@@ -65,13 +64,11 @@ def test_weight(self) -> None:
65
64
c1.weight = 80.6
66
65
""" )
67
66
68
- for node , vertex in artifacts .list :
69
- self .manager .on_new_tx (vertex , fails_silently = False )
67
+ artifacts .propagate_with (self .manager )
70
68
71
- tx1 = artifacts .by_name ['tx1' ].vertex
72
- tka = artifacts .by_name ['TKA' ].vertex
73
- c1 = artifacts .by_name ['c1' ].vertex
74
- b38 = artifacts .by_name ['b38' ].vertex
69
+ c1 , b38 = artifacts .get_typed_vertices (['c1' , 'b38' ], Block )
70
+ tx1 = artifacts .get_typed_vertex ('tx1' , Transaction )
71
+ tka = artifacts .get_typed_vertex ('TKA' , TokenCreationTransaction )
75
72
76
73
self .assertAlmostEqual (tka .weight , 31.8 )
77
74
self .assertAlmostEqual (tx1 .weight , 25.2 )
@@ -85,10 +82,9 @@ def test_spend_unspecified_utxo(self) -> None:
85
82
tx1.out[0] <<< tx2
86
83
""" )
87
84
88
- for node , vertex in artifacts .list :
89
- self .manager .on_new_tx (vertex , fails_silently = False )
85
+ artifacts .propagate_with (self .manager )
90
86
91
- tx1 = artifacts .by_name [ 'tx1' ]. vertex
87
+ tx1 = artifacts .get_typed_vertex ( 'tx1' , Transaction )
92
88
self .assertEqual (len (tx1 .outputs ), 1 )
93
89
# the default filler fills unspecified utxos with 1 HTR
94
90
self .assertEqual (tx1 .outputs [0 ].value , 1 )
@@ -107,22 +103,11 @@ def test_block_parents(self) -> None:
107
103
b36 --> tx4
108
104
""" )
109
105
110
- for node , vertex in artifacts .list :
111
- self .manager .on_new_tx (vertex , fails_silently = False )
106
+ artifacts .propagate_with (self .manager )
112
107
113
- b0 = artifacts .by_name ['b30' ].vertex
114
- b1 = artifacts .by_name ['b31' ].vertex
115
- b2 = artifacts .by_name ['b32' ].vertex
116
- b3 = artifacts .by_name ['b33' ].vertex
117
- b4 = artifacts .by_name ['b34' ].vertex
118
- b5 = artifacts .by_name ['b35' ].vertex
119
- b6 = artifacts .by_name ['b36' ].vertex
120
- b7 = artifacts .by_name ['b37' ].vertex
121
-
122
- tx1 = artifacts .by_name ['tx1' ].vertex
123
- tx2 = artifacts .by_name ['tx2' ].vertex
124
- tx3 = artifacts .by_name ['tx3' ].vertex
125
- tx4 = artifacts .by_name ['tx4' ].vertex
108
+ blocks = ['b30' , 'b31' , 'b32' , 'b33' , 'b34' , 'b35' , 'b36' , 'b37' ]
109
+ b0 , b1 , b2 , b3 , b4 , b5 , b6 , b7 = artifacts .get_typed_vertices (blocks , Block )
110
+ tx1 , tx2 , tx3 , tx4 = artifacts .get_typed_vertices (['tx1' , 'tx2' , 'tx3' , 'tx4' ], Transaction )
126
111
127
112
self .assertEqual (b2 .parents [0 ], b1 .hash )
128
113
self .assertEqual (b3 .parents [0 ], b2 .hash )
@@ -149,14 +134,12 @@ def test_custom_token(self) -> None:
149
134
b40 --> tx1
150
135
""" )
151
136
152
- for node , vertex in artifacts .list :
153
- self .manager .on_new_tx (vertex , fails_silently = False )
137
+ artifacts .propagate_with (self .manager )
154
138
155
- tka = artifacts .by_name [ 'TKA' ]. vertex
156
- tx1 = artifacts .by_name [ 'tx1' ]. vertex
139
+ tx1 = artifacts .get_typed_vertex ( 'tx1' , Transaction )
140
+ tka = artifacts .get_typed_vertex ( 'TKA' , TokenCreationTransaction )
157
141
158
142
# TKA token creation transaction
159
- self .assertIsInstance (tka , TokenCreationTransaction )
160
143
self .assertEqual (tka .token_name , 'TKA' )
161
144
self .assertEqual (tka .token_symbol , 'TKA' )
162
145
@@ -201,8 +184,7 @@ def test_big_dag(self) -> None:
201
184
b16 < tx4
202
185
""" )
203
186
204
- for node , vertex in artifacts .list :
205
- self .manager .on_new_tx (vertex , fails_silently = False )
187
+ artifacts .propagate_with (self .manager )
206
188
207
189
def test_no_hash_conflict (self ) -> None :
208
190
artifacts = self .dag_builder .build_from_str ("""
@@ -212,9 +194,24 @@ def test_no_hash_conflict(self) -> None:
212
194
213
195
tx10.out[0] <<< tx20 tx30 tx40
214
196
""" )
197
+ artifacts .propagate_with (self .manager )
198
+
199
+ def test_propagate_with (self ) -> None :
200
+ tx_storage = self .manager .tx_storage
201
+ artifacts = self .dag_builder .build_from_str ('''
202
+ blockchain genesis b[1..10]
203
+ b10 < dummy
204
+ tx1 <-- tx2
205
+ ''' )
206
+
207
+ artifacts .propagate_with (self .manager , up_to = 'b5' )
208
+ assert len (list (tx_storage .get_all_transactions ())) == 8 # 3 genesis + 5 blocks
209
+
210
+ artifacts .propagate_with (self .manager , up_to = 'b10' )
211
+ assert len (list (tx_storage .get_all_transactions ())) == 13 # 3 genesis + 10 blocks
212
+
213
+ artifacts .propagate_with (self .manager , up_to = 'tx1' )
214
+ assert len (list (tx_storage .get_all_transactions ())) == 15 # 3 genesis + 10 blocks + dummy + tx1
215
215
216
- for node , vertex in artifacts .list :
217
- print ()
218
- print (node .name )
219
- print ()
220
- self .manager .on_new_tx (vertex , fails_silently = False )
216
+ artifacts .propagate_with (self .manager )
217
+ assert len (list (tx_storage .get_all_transactions ())) == 16 # 3 genesis + 10 blocks + dummy + tx1 + tx2
0 commit comments