@@ -27,9 +27,6 @@ use crate::transaction::{Transaction, TxIn, TxOut};
27
27
28
28
pub use bitcoin:: { self , consensus:: encode:: MAX_VEC_SIZE } ;
29
29
30
- // Use the ReadExt/WriteExt traits as is from upstream
31
- pub use bitcoin:: consensus:: encode:: { ReadExt , WriteExt } ;
32
-
33
30
use crate :: taproot:: TapLeafHash ;
34
31
35
32
/// Encoding error
@@ -207,7 +204,7 @@ impl Decodable for sha256::Midstate {
207
204
}
208
205
}
209
206
210
- pub ( crate ) fn consensus_encode_with_size < S : io :: Write > (
207
+ pub ( crate ) fn consensus_encode_with_size < S : crate :: WriteExt > (
211
208
data : & [ u8 ] ,
212
209
mut s : S ,
213
210
) -> Result < usize , Error > {
@@ -245,64 +242,16 @@ impl Decodable for crate::locktime::Time {
245
242
}
246
243
}
247
244
248
- // TODO reuse bitcoin's `WriteExt::emit_varint`, `ReadExt::read_varint` when available
249
-
250
245
/// A variable sized integer.
251
246
pub struct VarInt ( pub u64 ) ;
252
247
impl Encodable for VarInt {
253
- fn consensus_encode < W : io:: Write > ( & self , mut e : W ) -> Result < usize , Error > {
254
- match self . 0 {
255
- i @ 0 ..=0xFC => {
256
- e. emit_u8 ( i as u8 ) ?;
257
- Ok ( 1 )
258
- }
259
- i @ 0xFD ..=0xFFFF => {
260
- e. emit_u8 ( 0xFD ) ?;
261
- e. emit_u16 ( i as u16 ) ?;
262
- Ok ( 3 )
263
- }
264
- i @ 0x10000 ..=0xFFFFFFFF => {
265
- e. emit_u8 ( 0xFE ) ?;
266
- e. emit_u32 ( i as u32 ) ?;
267
- Ok ( 5 )
268
- }
269
- i => {
270
- e. emit_u8 ( 0xFF ) ?;
271
- e. emit_u64 ( i) ?;
272
- Ok ( 9 )
273
- }
274
- }
248
+ fn consensus_encode < W : crate :: WriteExt > ( & self , mut e : W ) -> Result < usize , Error > {
249
+ Ok ( e. emit_varint ( self . 0 ) ?)
275
250
}
276
251
}
277
252
impl Decodable for VarInt {
278
- fn consensus_decode < D : io:: Read > ( mut d : D ) -> Result < Self , Error > {
279
- match d. read_u8 ( ) ? {
280
- 0xFF => {
281
- let x = d. read_u64 ( ) ?;
282
- if x < 0x100000000 {
283
- Err ( Error :: NonMinimalVarInt )
284
- } else {
285
- Ok ( VarInt ( x) )
286
- }
287
- }
288
- 0xFE => {
289
- let x = d. read_u32 ( ) ?;
290
- if x < 0x10000 {
291
- Err ( Error :: NonMinimalVarInt )
292
- } else {
293
- Ok ( VarInt ( x as u64 ) )
294
- }
295
- }
296
- 0xFD => {
297
- let x = d. read_u16 ( ) ?;
298
- if x < 0xFD {
299
- Err ( Error :: NonMinimalVarInt )
300
- } else {
301
- Ok ( VarInt ( x as u64 ) )
302
- }
303
- }
304
- n => Ok ( VarInt ( n as u64 ) ) ,
305
- }
253
+ fn consensus_decode < D : crate :: ReadExt > ( mut d : D ) -> Result < Self , Error > {
254
+ Ok ( VarInt ( d. read_varint ( ) ?) )
306
255
}
307
256
}
308
257
impl VarInt {
@@ -321,14 +270,14 @@ impl VarInt {
321
270
macro_rules! impl_int {
322
271
( $ty: ident, $meth_dec: ident, $meth_enc: ident) => {
323
272
impl Encodable for $ty {
324
- fn consensus_encode<W : io :: Write >( & self , mut w: W ) -> Result <usize , Error > {
273
+ fn consensus_encode<W : crate :: WriteExt >( & self , mut w: W ) -> Result <usize , Error > {
325
274
w. $meth_enc( * self ) ?;
326
275
Ok ( mem:: size_of:: <$ty>( ) )
327
276
}
328
277
}
329
278
impl Decodable for $ty {
330
- fn consensus_decode<R : io :: Read >( mut r: R ) -> Result <Self , Error > {
331
- Ok ( ReadExt :: $meth_dec( & mut r) ?)
279
+ fn consensus_decode<R : crate :: ReadExt >( mut r: R ) -> Result <Self , Error > {
280
+ Ok ( crate :: ReadExt :: $meth_dec( & mut r) ?)
332
281
}
333
282
}
334
283
} ;
@@ -411,7 +360,7 @@ macro_rules! impl_array {
411
360
( $size: literal ) => {
412
361
impl Encodable for [ u8 ; $size] {
413
362
#[ inline]
414
- fn consensus_encode<W : WriteExt >(
363
+ fn consensus_encode<W : crate :: WriteExt >(
415
364
& self ,
416
365
mut w: W ,
417
366
) -> core:: result:: Result <usize , Error > {
@@ -422,7 +371,7 @@ macro_rules! impl_array {
422
371
423
372
impl Decodable for [ u8 ; $size] {
424
373
#[ inline]
425
- fn consensus_decode<R : ReadExt >( mut r: R ) -> core:: result:: Result <Self , Error > {
374
+ fn consensus_decode<R : crate :: ReadExt >( mut r: R ) -> core:: result:: Result <Self , Error > {
426
375
let mut ret = [ 0 ; $size] ;
427
376
r. read_slice( & mut ret) ?;
428
377
Ok ( ret)
@@ -452,7 +401,7 @@ impl Encodable for Vec<u8> {
452
401
}
453
402
}
454
403
impl Decodable for Vec < u8 > {
455
- fn consensus_decode < D : io :: Read > ( mut d : D ) -> Result < Self , Error > {
404
+ fn consensus_decode < D : crate :: ReadExt > ( mut d : D ) -> Result < Self , Error > {
456
405
let s = VarInt :: consensus_decode ( & mut d) ?. 0 as usize ;
457
406
let mut v = vec ! [ 0 ; s] ;
458
407
d. read_slice ( & mut v) ?;
0 commit comments