Merge rust-bitcoin/rust-bitcoin#3286: Add additional docs to Witness
333c8ab297
Add additional docs to Witness (Tobin C. Harding) Pull request description: The `Witness` struct is non-trivial, in particular it is not immediately obvious where and when the compact size encode value for each witness element is stored. Make an effort to improve the docs on `Witness` in relation to the compact size encoded length of each witness element. ACKs for top commit: apoelstra: ACK333c8ab297
successfully ran local tests Kixunil: ACK333c8ab297
Tree-SHA512: 1c61a9ad071c035d5ad2e54446120d29ebf8cc4a779c96f04eda825890687dcbd53accc17522f57ef4ffb226eb1d85c6a3a115f27bebcfc7ad3c677033a8a414
This commit is contained in:
commit
c63695ac1e
|
@ -32,10 +32,12 @@ use crate::{Script, VarInt};
|
||||||
pub struct Witness {
|
pub struct Witness {
|
||||||
/// Contains the witness `Vec<Vec<u8>>` serialization.
|
/// Contains the witness `Vec<Vec<u8>>` serialization.
|
||||||
///
|
///
|
||||||
/// Does not include the initial varint indicating the number of elements, instead this is
|
/// Does not include the initial varint indicating the number of elements. Each element however,
|
||||||
/// stored stored in `witness_elements`. Concatenated onto the end of `content` is the index
|
/// does include a varint indicating the element length. The number of elements is stored in
|
||||||
/// area, this is a `4 * witness_elements` bytes area which stores the index of the start of
|
/// `witness_elements`.
|
||||||
/// each witness item.
|
///
|
||||||
|
/// Concatenated onto the end of `content` is the index area. This is a `4 * witness_elements`
|
||||||
|
/// bytes area which stores the index of the start of each witness item.
|
||||||
content: Vec<u8>,
|
content: Vec<u8>,
|
||||||
|
|
||||||
/// The number of elements in the witness.
|
/// The number of elements in the witness.
|
||||||
|
@ -227,6 +229,7 @@ fn resize_if_needed(vec: &mut Vec<u8>, required_len: usize) {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Encodable for Witness {
|
impl Encodable for Witness {
|
||||||
|
// `self.content` includes the varints so encoding here includes them, as expected.
|
||||||
fn consensus_encode<W: Write + ?Sized>(&self, w: &mut W) -> Result<usize, io::Error> {
|
fn consensus_encode<W: Write + ?Sized>(&self, w: &mut W) -> Result<usize, io::Error> {
|
||||||
let len = VarInt::from(self.witness_elements);
|
let len = VarInt::from(self.witness_elements);
|
||||||
len.consensus_encode(w)?;
|
len.consensus_encode(w)?;
|
||||||
|
@ -517,6 +520,7 @@ impl serde::Serialize for Witness {
|
||||||
let human_readable = serializer.is_human_readable();
|
let human_readable = serializer.is_human_readable();
|
||||||
let mut seq = serializer.serialize_seq(Some(self.witness_elements))?;
|
let mut seq = serializer.serialize_seq(Some(self.witness_elements))?;
|
||||||
|
|
||||||
|
// Note that the `Iter` strips the varints out when iterating.
|
||||||
for elem in self.iter() {
|
for elem in self.iter() {
|
||||||
if human_readable {
|
if human_readable {
|
||||||
seq.serialize_element(&crate::serde_utils::SerializeBytesAsHex(elem))?;
|
seq.serialize_element(&crate::serde_utils::SerializeBytesAsHex(elem))?;
|
||||||
|
|
Loading…
Reference in New Issue