bitcoin 0.32.102

General purpose library for using and interoperating with Bitcoin.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
// SPDX-License-Identifier: CC0-1.0

//! Bitcoin Client Side Block Filtering network messages.
//!
//! This module describes BIP157 Client Side Block Filtering network messages.
//!

#[cfg(feature = "encoding")]
use core::convert::Infallible;
#[cfg(feature = "encoding")]
use core::fmt;

use crate::bip158::{FilterHash, FilterHeader};
#[cfg(feature = "encoding")]
use crate::bip158::{FilterHeaderDecoder, FilterHeaderEncoder};
use crate::blockdata::block::BlockHash;
use crate::internal_macros::impl_consensus_encoding;
#[cfg(feature = "encoding")]
use crate::internal_macros::write_err;

/// getcfilters message
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct GetCFilters {
    /// Filter type for which headers are requested
    pub filter_type: u8,
    /// The height of the first block in the requested range
    pub start_height: u32,
    /// The hash of the last block in the requested range
    pub stop_hash: BlockHash,
}
impl_consensus_encoding!(GetCFilters, filter_type, start_height, stop_hash);

#[cfg(feature = "encoding")]
encoding::encoder_newtype_exact! {
    /// Encoder type for the [`GetCFilters`] message.
    #[derive(Debug, Clone)]
    pub struct GetCFiltersEncoder<'e>(
        encoding::Encoder3<encoding::ArrayEncoder<1>, encoding::ArrayEncoder<4>, crate::blockdata::block::BlockHashEncoder<'e>>
    );
}

#[cfg(feature = "encoding")]
impl encoding::Encode for GetCFilters {
    type Encoder<'e> = GetCFiltersEncoder<'e>;

    fn encoder(&self) -> Self::Encoder<'_> {
        GetCFiltersEncoder::new(encoding::Encoder3::new(
            encoding::ArrayEncoder::without_length_prefix(self.filter_type.to_le_bytes()),
            encoding::ArrayEncoder::without_length_prefix(self.start_height.to_le_bytes()),
            self.stop_hash.encoder(),
        ))
    }
}

#[cfg(feature = "encoding")]
type GetCFiltersInnerDecoder = encoding::Decoder3<
    encoding::ArrayDecoder<1>,
    encoding::ArrayDecoder<4>,
    crate::blockdata::block::BlockHashDecoder,
>;

#[cfg(feature = "encoding")]
crate::decoder_newtype! {
    /// Decoder type for the [`GetCFilters`] message.
    #[derive(Debug, Default, Clone)]
    pub struct GetCFiltersDecoder(GetCFiltersInnerDecoder);

    fn end(
        result: Result<<GetCFiltersInnerDecoder as encoding::Decoder>::Output, <GetCFiltersInnerDecoder as encoding::Decoder>::Error>
    ) -> Result<GetCFilters, GetCFiltersDecoderError> {
        let (ty, start_height, stop_hash) = result.map_err(GetCFiltersDecoderError)?;
        Ok(GetCFilters {
            filter_type: u8::from_le_bytes(ty),
            start_height: u32::from_le_bytes(start_height),
            stop_hash,
        })
    }
}

#[cfg(feature = "encoding")]
impl encoding::Decode for GetCFilters {
    type Decoder = GetCFiltersDecoder;
}

/// Errors occurring when decoding a [`GetCFilters`] message.
#[cfg(feature = "encoding")]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GetCFiltersDecoderError(
    pub(crate) <GetCFiltersInnerDecoder as encoding::Decoder>::Error,
);

#[cfg(feature = "encoding")]
impl From<Infallible> for GetCFiltersDecoderError {
    fn from(never: Infallible) -> Self { match never {} }
}

#[cfg(feature = "encoding")]
impl fmt::Display for GetCFiltersDecoderError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write_err!(f, "getcfilters error"; self.0)
    }
}

#[cfg(all(feature = "encoding", feature = "std"))]
impl std::error::Error for GetCFiltersDecoderError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { Some(&self.0) }
}

/// cfilter message
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct CFilter {
    /// Byte identifying the type of filter being returned
    pub filter_type: u8,
    /// Block hash of the Bitcoin block for which the filter is being returned
    pub block_hash: BlockHash,
    /// The serialized compact filter for this block
    pub filter: Vec<u8>,
}
impl_consensus_encoding!(CFilter, filter_type, block_hash, filter);

#[cfg(feature = "encoding")]
encoding::encoder_newtype! {
    /// Encoder type for a [`CFilter`] message.
    #[derive(Debug, Clone)]
    pub struct CFilterEncoder<'e>(
        encoding::Encoder3<
            encoding::ArrayEncoder<1>,
            crate::blockdata::block::BlockHashEncoder<'e>,
            encoding::Encoder2<encoding::CompactSizeEncoder, encoding::BytesEncoder<'e>>,
        >
    );
}

#[cfg(feature = "encoding")]
impl encoding::Encode for CFilter {
    type Encoder<'e> = CFilterEncoder<'e>;

    fn encoder(&self) -> Self::Encoder<'_> {
        CFilterEncoder::new(encoding::Encoder3::new(
            encoding::ArrayEncoder::without_length_prefix(self.filter_type.to_le_bytes()),
            self.block_hash.encoder(),
            encoding::Encoder2::new(
                encoding::CompactSizeEncoder::new(self.filter.len()),
                encoding::BytesEncoder::without_length_prefix(&self.filter),
            ),
        ))
    }
}

#[cfg(feature = "encoding")]
type CFilterInnerDecoder = encoding::Decoder3<
    encoding::ArrayDecoder<1>,
    crate::blockdata::block::BlockHashDecoder,
    encoding::ByteVecDecoder,
>;

#[cfg(feature = "encoding")]
crate::decoder_newtype! {
    /// Decoder type for a [`CFilter`] message.
    #[derive(Debug, Default, Clone)]
    pub struct CFilterDecoder(CFilterInnerDecoder);

    fn end(
        result: Result<<CFilterInnerDecoder as encoding::Decoder>::Output, <CFilterInnerDecoder as encoding::Decoder>::Error>
    ) -> Result<CFilter, CFilterDecoderError> {
        let (ty, block_hash, filter) = result.map_err(CFilterDecoderError)?;
        Ok(CFilter { filter_type: u8::from_le_bytes(ty), block_hash, filter })
    }
}

#[cfg(feature = "encoding")]
impl encoding::Decode for CFilter {
    type Decoder = CFilterDecoder;
}

/// Errors occurring when decoding a [`CFilter`] message.
#[cfg(feature = "encoding")]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CFilterDecoderError(pub(crate) <CFilterInnerDecoder as encoding::Decoder>::Error);

#[cfg(feature = "encoding")]
impl From<Infallible> for CFilterDecoderError {
    fn from(never: Infallible) -> Self { match never {} }
}

#[cfg(feature = "encoding")]
impl fmt::Display for CFilterDecoderError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write_err!(f, "cfilter error"; self.0)
    }
}

#[cfg(all(feature = "encoding", feature = "std"))]
impl std::error::Error for CFilterDecoderError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { Some(&self.0) }
}

/// getcfheaders message
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct GetCFHeaders {
    /// Byte identifying the type of filter being returned
    pub filter_type: u8,
    /// The height of the first block in the requested range
    pub start_height: u32,
    /// The hash of the last block in the requested range
    pub stop_hash: BlockHash,
}
impl_consensus_encoding!(GetCFHeaders, filter_type, start_height, stop_hash);

#[cfg(feature = "encoding")]
encoding::encoder_newtype_exact! {
    /// Encoder type for the [`GetCFHeaders`] message.
    #[derive(Debug, Clone)]
    pub struct GetCFHeadersEncoder<'e>(
        encoding::Encoder3<encoding::ArrayEncoder<1>, encoding::ArrayEncoder<4>, crate::blockdata::block::BlockHashEncoder<'e>>
    );
}

#[cfg(feature = "encoding")]
impl encoding::Encode for GetCFHeaders {
    type Encoder<'e> = GetCFHeadersEncoder<'e>;

    fn encoder(&self) -> Self::Encoder<'_> {
        GetCFHeadersEncoder::new(encoding::Encoder3::new(
            encoding::ArrayEncoder::without_length_prefix(self.filter_type.to_le_bytes()),
            encoding::ArrayEncoder::without_length_prefix(self.start_height.to_le_bytes()),
            self.stop_hash.encoder(),
        ))
    }
}

#[cfg(feature = "encoding")]
type GetCFHeadersInnerDecoder = encoding::Decoder3<
    encoding::ArrayDecoder<1>,
    encoding::ArrayDecoder<4>,
    crate::blockdata::block::BlockHashDecoder,
>;

#[cfg(feature = "encoding")]
crate::decoder_newtype! {
    /// Decoder type for the [`GetCFHeaders`] message.
    #[derive(Debug, Default, Clone)]
    pub struct GetCFHeadersDecoder(GetCFHeadersInnerDecoder);

    fn end(
        result: Result<<GetCFHeadersInnerDecoder as encoding::Decoder>::Output, <GetCFHeadersInnerDecoder as encoding::Decoder>::Error>
    ) -> Result<GetCFHeaders, GetCFHeadersDecoderError> {
        let (ty, start_height, stop_hash) = result.map_err(GetCFHeadersDecoderError)?;
        Ok(GetCFHeaders {
            filter_type: u8::from_le_bytes(ty),
            start_height: u32::from_le_bytes(start_height),
            stop_hash,
        })
    }
}

#[cfg(feature = "encoding")]
impl encoding::Decode for GetCFHeaders {
    type Decoder = GetCFHeadersDecoder;
}

/// Errors occurring when decoding a [`GetCFHeaders`] message.
#[cfg(feature = "encoding")]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GetCFHeadersDecoderError(
    pub(crate) <GetCFHeadersInnerDecoder as encoding::Decoder>::Error,
);

#[cfg(feature = "encoding")]
impl From<Infallible> for GetCFHeadersDecoderError {
    fn from(never: Infallible) -> Self { match never {} }
}

#[cfg(feature = "encoding")]
impl fmt::Display for GetCFHeadersDecoderError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write_err!(f, "getcfheaders error"; self.0)
    }
}

#[cfg(all(feature = "encoding", feature = "std"))]
impl std::error::Error for GetCFHeadersDecoderError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { Some(&self.0) }
}

/// cfheaders message
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct CFHeaders {
    /// Filter type for which headers are requested
    pub filter_type: u8,
    /// The hash of the last block in the requested range
    pub stop_hash: BlockHash,
    /// The filter header preceding the first block in the requested range
    pub previous_filter_header: FilterHeader,
    /// The filter hashes for each block in the requested range
    pub filter_hashes: Vec<FilterHash>,
}
impl_consensus_encoding!(CFHeaders, filter_type, stop_hash, previous_filter_header, filter_hashes);

#[cfg(feature = "encoding")]
encoding::encoder_newtype! {
    /// Encoder type for a [`CFHeaders`] message.
    #[derive(Debug, Clone)]
    pub struct CFHeadersEncoder<'e>(
        encoding::Encoder4<
            encoding::ArrayEncoder<1>,
            crate::blockdata::block::BlockHashEncoder<'e>,
            FilterHeaderEncoder<'e>,
            encoding::Encoder2<encoding::CompactSizeEncoder, encoding::SliceEncoder<'e, FilterHash>>,
        >
    );
}

#[cfg(feature = "encoding")]
impl encoding::Encode for CFHeaders {
    type Encoder<'e> = CFHeadersEncoder<'e>;

    fn encoder(&self) -> Self::Encoder<'_> {
        CFHeadersEncoder::new(encoding::Encoder4::new(
            encoding::ArrayEncoder::without_length_prefix(self.filter_type.to_le_bytes()),
            self.stop_hash.encoder(),
            self.previous_filter_header.encoder(),
            encoding::Encoder2::new(
                encoding::CompactSizeEncoder::new(self.filter_hashes.len()),
                encoding::SliceEncoder::without_length_prefix(&self.filter_hashes),
            ),
        ))
    }
}

#[cfg(feature = "encoding")]
type CFHeadersInnerDecoder = encoding::Decoder4<
    encoding::ArrayDecoder<1>,
    crate::blockdata::block::BlockHashDecoder,
    FilterHeaderDecoder,
    encoding::VecDecoder<FilterHash>,
>;

#[cfg(feature = "encoding")]
crate::decoder_newtype! {
    /// Decoder type for a [`CFHeaders`] message.
    #[derive(Debug, Default, Clone)]
    pub struct CFHeadersDecoder(CFHeadersInnerDecoder);

    fn end(
        result: Result<<CFHeadersInnerDecoder as encoding::Decoder>::Output, <CFHeadersInnerDecoder as encoding::Decoder>::Error>
    ) -> Result<CFHeaders, CFHeadersDecoderError> {
        let (ty, stop_hash, previous_filter_header, filter_hashes) = result.map_err(CFHeadersDecoderError)?;
        Ok(CFHeaders {
            filter_type: u8::from_le_bytes(ty),
            stop_hash,
            previous_filter_header,
            filter_hashes,
        })
    }
}

#[cfg(feature = "encoding")]
impl encoding::Decode for CFHeaders {
    type Decoder = CFHeadersDecoder;
}

/// Errors occurring when decoding a [`CFHeaders`] message.
#[cfg(feature = "encoding")]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CFHeadersDecoderError(pub(crate) <CFHeadersInnerDecoder as encoding::Decoder>::Error);

#[cfg(feature = "encoding")]
impl From<Infallible> for CFHeadersDecoderError {
    fn from(never: Infallible) -> Self { match never {} }
}

#[cfg(feature = "encoding")]
impl fmt::Display for CFHeadersDecoderError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write_err!(f, "cfheaders error"; self.0)
    }
}

#[cfg(all(feature = "encoding", feature = "std"))]
impl std::error::Error for CFHeadersDecoderError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { Some(&self.0) }
}

/// getcfcheckpt message
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct GetCFCheckpt {
    /// Filter type for which headers are requested
    pub filter_type: u8,
    /// The hash of the last block in the requested range
    pub stop_hash: BlockHash,
}
impl_consensus_encoding!(GetCFCheckpt, filter_type, stop_hash);

#[cfg(feature = "encoding")]
encoding::encoder_newtype_exact! {
    /// Encoder type for the [`GetCFCheckpt`] message.
    #[derive(Debug, Clone)]
    pub struct GetCFCheckptEncoder<'e>(
        encoding::Encoder2<encoding::ArrayEncoder<1>, crate::blockdata::block::BlockHashEncoder<'e>>
    );
}

#[cfg(feature = "encoding")]
impl encoding::Encode for GetCFCheckpt {
    type Encoder<'e> = GetCFCheckptEncoder<'e>;

    fn encoder(&self) -> Self::Encoder<'_> {
        GetCFCheckptEncoder::new(encoding::Encoder2::new(
            encoding::ArrayEncoder::without_length_prefix(self.filter_type.to_le_bytes()),
            self.stop_hash.encoder(),
        ))
    }
}

#[cfg(feature = "encoding")]
type GetCFCheckptInnerDecoder =
    encoding::Decoder2<encoding::ArrayDecoder<1>, crate::blockdata::block::BlockHashDecoder>;

#[cfg(feature = "encoding")]
crate::decoder_newtype! {
    /// Decoder type for a [`GetCFCheckpt`] message.
    #[derive(Debug, Default, Clone)]
    pub struct GetCFCheckptDecoder(GetCFCheckptInnerDecoder);

    fn end(
        result: Result<<GetCFCheckptInnerDecoder as encoding::Decoder>::Output, <GetCFCheckptInnerDecoder as encoding::Decoder>::Error>
    ) -> Result<GetCFCheckpt, GetCFCheckptDecoderError> {
        let (ty, stop_hash) = result.map_err(GetCFCheckptDecoderError)?;
        Ok(GetCFCheckpt { filter_type: u8::from_le_bytes(ty), stop_hash })
    }
}

#[cfg(feature = "encoding")]
impl encoding::Decode for GetCFCheckpt {
    type Decoder = GetCFCheckptDecoder;
}

/// Errors occurring when decoding a [`GetCFCheckpt`] message.
#[cfg(feature = "encoding")]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GetCFCheckptDecoderError(
    pub(crate) <GetCFCheckptInnerDecoder as encoding::Decoder>::Error,
);

#[cfg(feature = "encoding")]
impl From<Infallible> for GetCFCheckptDecoderError {
    fn from(never: Infallible) -> Self { match never {} }
}

#[cfg(feature = "encoding")]
impl fmt::Display for GetCFCheckptDecoderError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write_err!(f, "getcfcheckpt error"; self.0)
    }
}

#[cfg(all(feature = "encoding", feature = "std"))]
impl std::error::Error for GetCFCheckptDecoderError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { Some(&self.0) }
}

/// cfcheckpt message
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct CFCheckpt {
    /// Filter type for which headers are requested
    pub filter_type: u8,
    /// The hash of the last block in the requested range
    pub stop_hash: BlockHash,
    /// The filter headers at intervals of 1,000
    pub filter_headers: Vec<FilterHeader>,
}
impl_consensus_encoding!(CFCheckpt, filter_type, stop_hash, filter_headers);

#[cfg(feature = "encoding")]
encoding::encoder_newtype! {
    /// Encoder type for a [`CFCheckpt`] message.
    #[derive(Debug, Clone)]
    pub struct CFCheckptEncoder<'e>(
        encoding::Encoder3<
            encoding::ArrayEncoder<1>,
            crate::blockdata::block::BlockHashEncoder<'e>,
            encoding::Encoder2<encoding::CompactSizeEncoder, encoding::SliceEncoder<'e, FilterHeader>>,
        >
    );
}

#[cfg(feature = "encoding")]
impl encoding::Encode for CFCheckpt {
    type Encoder<'e> = CFCheckptEncoder<'e>;

    fn encoder(&self) -> Self::Encoder<'_> {
        CFCheckptEncoder::new(encoding::Encoder3::new(
            encoding::ArrayEncoder::without_length_prefix(self.filter_type.to_le_bytes()),
            self.stop_hash.encoder(),
            encoding::Encoder2::new(
                encoding::CompactSizeEncoder::new(self.filter_headers.len()),
                encoding::SliceEncoder::without_length_prefix(&self.filter_headers),
            ),
        ))
    }
}

#[cfg(feature = "encoding")]
type CFCheckptInnerDecoder = encoding::Decoder3<
    encoding::ArrayDecoder<1>,
    crate::blockdata::block::BlockHashDecoder,
    encoding::VecDecoder<FilterHeader>,
>;

#[cfg(feature = "encoding")]
crate::decoder_newtype! {
    /// Decoder type for a [`CFCheckpt`] message.
    #[derive(Debug, Default, Clone)]
    pub struct CFCheckptDecoder(CFCheckptInnerDecoder);

    fn end(
        result: Result<<CFCheckptInnerDecoder as encoding::Decoder>::Output, <CFCheckptInnerDecoder as encoding::Decoder>::Error>
    ) -> Result<CFCheckpt, CFCheckptDecoderError> {
        let (ty, stop_hash, filter_headers) = result.map_err(CFCheckptDecoderError)?;
        Ok(CFCheckpt { filter_type: u8::from_le_bytes(ty), stop_hash, filter_headers })
    }
}

#[cfg(feature = "encoding")]
impl encoding::Decode for CFCheckpt {
    type Decoder = CFCheckptDecoder;
}

/// Errors occurring when decoding a [`CFCheckpt`] message.
#[cfg(feature = "encoding")]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CFCheckptDecoderError(pub(crate) <CFCheckptInnerDecoder as encoding::Decoder>::Error);

#[cfg(feature = "encoding")]
impl From<Infallible> for CFCheckptDecoderError {
    fn from(never: Infallible) -> Self { match never {} }
}

#[cfg(feature = "encoding")]
impl fmt::Display for CFCheckptDecoderError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write_err!(f, "cfcheckpt error"; self.0)
    }
}

#[cfg(all(feature = "encoding", feature = "std"))]
impl std::error::Error for CFCheckptDecoderError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { Some(&self.0) }
}