@@ -136,4 +136,76 @@ impl AbridgedBlock {
136
136
uncles : uncles,
137
137
} )
138
138
}
139
+ }
140
+
141
+ #[ cfg( test) ]
142
+ mod tests {
143
+ use views:: BlockView ;
144
+ use block:: Block ;
145
+ use super :: AbridgedBlock ;
146
+ use types:: transaction:: { Action , Transaction } ;
147
+
148
+ use util:: numbers:: U256 ;
149
+ use util:: hash:: { Address , H256 , FixedHash } ;
150
+ use util:: { Bytes , RlpStream , Stream } ;
151
+
152
+ fn encode_block ( b : & Block ) -> Bytes {
153
+ let mut s = RlpStream :: new_list ( 3 ) ;
154
+
155
+ b. header . stream_rlp ( & mut s, :: basic_types:: Seal :: With ) ;
156
+ s. append ( & b. transactions ) ;
157
+ s. append ( & b. uncles ) ;
158
+
159
+ s. out ( )
160
+ }
161
+
162
+ #[ test]
163
+ fn empty_block_abridging ( ) {
164
+ let b = Block :: default ( ) ;
165
+ let encoded = encode_block ( & b) ;
166
+
167
+ let abridged = AbridgedBlock :: from_block_view ( & BlockView :: new ( & encoded) ) ;
168
+ assert_eq ! ( abridged. to_block( H256 :: new( ) , 0 ) . unwrap( ) , b) ;
169
+ }
170
+
171
+ #[ test]
172
+ #[ should_panic]
173
+ fn wrong_number ( ) {
174
+ let b = Block :: default ( ) ;
175
+ let encoded = encode_block ( & b) ;
176
+
177
+ let abridged = AbridgedBlock :: from_block_view ( & BlockView :: new ( & encoded) ) ;
178
+ assert_eq ! ( abridged. to_block( H256 :: new( ) , 2 ) . unwrap( ) , b) ;
179
+ }
180
+
181
+ #[ test]
182
+ fn with_transactions ( ) {
183
+ let mut b = Block :: default ( ) ;
184
+
185
+ let t1 = Transaction {
186
+ action : Action :: Create ,
187
+ nonce : U256 :: from ( 42 ) ,
188
+ gas_price : U256 :: from ( 3000 ) ,
189
+ gas : U256 :: from ( 50_000 ) ,
190
+ value : U256 :: from ( 1 ) ,
191
+ data : b"Hello!" . to_vec ( )
192
+ } . fake_sign ( Address :: from ( 0x69 ) ) ;
193
+
194
+ let t2 = Transaction {
195
+ action : Action :: Create ,
196
+ nonce : U256 :: from ( 88 ) ,
197
+ gas_price : U256 :: from ( 12345 ) ,
198
+ gas : U256 :: from ( 300000 ) ,
199
+ value : U256 :: from ( 1000000000 ) ,
200
+ data : "Eep!" . into ( ) ,
201
+ } . fake_sign ( Address :: from ( 0x55 ) ) ;
202
+
203
+ b. transactions . push ( t1) ;
204
+ b. transactions . push ( t2) ;
205
+
206
+ let encoded = encode_block ( & b) ;
207
+
208
+ let abridged = AbridgedBlock :: from_block_view ( & BlockView :: new ( & encoded[ ..] ) ) ;
209
+ assert_eq ! ( abridged. to_block( H256 :: new( ) , 0 ) . unwrap( ) , b) ;
210
+ }
139
211
}
0 commit comments