Skip to content

Commit 2670e9c

Browse files
committed
Break long paragraph lines
1 parent 8f0c2c8 commit 2670e9c

File tree

1 file changed

+62
-16
lines changed

1 file changed

+62
-16
lines changed

Diff for: 12_reading_inputs_and_type_coercion.md

+62-16
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,16 @@ Each input in a transaction contains the following information:
66
* ScriptSig (variable length preceded by compact size integer)
77
* sequence (4 bytes length, represented as a u32 integer)
88

9-
The ScriptSig can be a variable length and so is preceded by a compact size integer which indicates the length of the field in bytes. Prior to Segwit, the ScriptSig was where the signature would be provided for unlocking the funds of the referenced output (as indicated by the previous output txid and previous output index). Now, for Segwit transactions, this field is empty with a compact size length of 0x00 as the signature is no longer contained in the input data, but is instead "*segregated*" from the rest of the transaction in a separate witness field. For more information on SegWit, see [this section](https://github.com/bitcoinbook/bitcoinbook/blob/6d1c26e1640ae32b28389d5ae4caf1214c2be7db/ch06_transactions.adoc#segregated-witness) from Mastering Bitcoin, Chapter 6.
9+
The ScriptSig can be a variable length and so is preceded by a compact size integer which indicates the length of the field in bytes.
10+
Prior to Segwit, the ScriptSig was where the signature would be provided for unlocking the funds of the referenced output (as indicated by the previous output txid and previous output index).
11+
Now, for Segwit transactions, this field is empty with a compact size length of 0x00 as the signature is no longer contained in the input data, but is instead "*segregated*" from the rest of the transaction in a separate witness field.
12+
For more information on SegWit, see [this section](https://github.com/bitcoinbook/bitcoinbook/blob/6d1c26e1640ae32b28389d5ae4caf1214c2be7db/ch06_transactions.adoc#segregated-witness) from Mastering Bitcoin, Chapter 6.
1013

11-
So let's update our code. We have the input length now so we know how many times to read the input information. We'll start by using a for loop and iterate over a range. Since we don't need the range index number, we can just replace the unused variable with an underscore, `_`. More details on loops in Rust can be [found here](https://doc.rust-lang.org/book/ch03-05-control-flow.html#looping-through-a-collection-with-for).
14+
So let's update our code.
15+
We have the input length now so we know how many times to read the input information.
16+
We'll start by using a for loop and iterate over a range.
17+
Since we don't need the range index number, we can just replace the unused variable with an underscore, `_`.
18+
More details on loops in Rust can be [found here](https://doc.rust-lang.org/book/ch03-05-control-flow.html#looping-through-a-collection-with-for).
1219

1320
```rust
1421
fn main() {
@@ -24,12 +31,23 @@ fn main() {
2431
}
2532
```
2633

27-
Let's implement the `read_txid` function. We know we're looking to read the next 32 bytes, which displayed in hex format will be the transaction id we show to a user. There's one catch, however. Whenever we display transaction ids to users, we display them in *big-endian* format. However, those ids are stored internally in blocks as *little-endian*. This means we have to reverse the id before showing it to the user. A description about why this is can be [found here](https://github.com/bitcoinbook/bitcoinbook/blob/6d1c26e1640ae32b28389d5ae4caf1214c2be7db/ch06_transactions.adoc#internal_and_display_order) in Mastering Bitcoin, Chapter 6.
34+
Let's implement the `read_txid` function.
35+
We know we're looking to read the next 32 bytes, which displayed in hex format will be the transaction id we show to a user.
36+
There's one catch, however.
37+
Whenever we display transaction ids to users, we display them in *big-endian* format.
38+
However, those ids are stored internally in blocks as *little-endian*.
39+
This means we have to reverse the id before showing it to the user.
40+
A description about why this is can be [found here](https://github.com/bitcoinbook/bitcoinbook/blob/6d1c26e1640ae32b28389d5ae4caf1214c2be7db/ch06_transactions.adoc#internal_and_display_order) in Mastering Bitcoin, Chapter 6.
2841

29-
Before we show the implementation below, why don't you take a stab at the function signature? If you're feeling confident, write out the whole function and then compare your answer here.
42+
Before we show the implementation below, why don't you take a stab at the function signature?
43+
If you're feeling confident, write out the whole function and then compare your answer here.
3044
Some hints:
31-
1. Let's not worry about the hex display just yet. Let's just return the appropriate bytes in *big endian*. Remember, by default it is *little-endian*.
32-
2. *We know the exact size of the amount of bytes we want to return, so what kind of data structure is appropriate for a fixed-size amount of bytes? Do we need to store anything on the heap? Or can we just store this data on the stack?*
45+
1. Let's not worry about the hex display just yet.
46+
Let's just return the appropriate bytes in *big endian*.
47+
Remember, by default it is *little-endian*.
48+
2. *We know the exact size of the amount of bytes we want to return, so what kind of data structure is appropriate for a fixed-size amount of bytes?
49+
Do we need to store anything on the heap?
50+
Or can we just store this data on the stack?*
3351

3452
<hr/>
3553

@@ -42,9 +60,14 @@ fn read_txid(transaction_bytes: &mut &[u8]) -> [u8; 32] {
4260
}
4361
```
4462

45-
So all we have to do here is read 32 bytes and store that into an array, which is a fixed size amount of bytes. Of course, we need to reverse the bytes so that they are in big-endian. Pretty simple right? So what's next? The next 4 bytes will give us the index of that transaction that we're spending.
63+
So all we have to do here is read 32 bytes and store that into an array, which is a fixed size amount of bytes.
64+
Of course, we need to reverse the bytes so that they are in big-endian.
65+
Pretty simple right? So what's next? The next 4 bytes will give us the index of that transaction that we're spending.
4666

47-
If you think about it, this is identical to what we did to get the version. We read 4 bytes and returned the u32 integer which represented the version. The index is the same. So perhaps instead of calling that function `read_version`, we can rename it to `read_u32` to make it more generic and just call that here:
67+
If you think about it, this is identical to what we did to get the version.
68+
We read 4 bytes and returned the u32 integer which represented the version.
69+
The index is the same.
70+
So perhaps instead of calling that function `read_version`, we can rename it to `read_u32` to make it more generic and just call that here:
4871

4972
```rust
5073
...
@@ -78,21 +101,41 @@ Next let's get the size of our ScriptSig by reading the compactSize.
78101

79102
### Type Coercions
80103

81-
Now that we have the `script_size`, we know how many bytes to read, but this gets us into an interesting problem. When we create a buffer to read bytes into, we always have to provide a fixed size array. However, the `script_size` is dynamic and cannot be known at compile time. It can only be determined at runtime. You're not able to do something like this as the compiler will complain:
104+
Now that we have the `script_size`, we know how many bytes to read, but this gets us into an interesting problem.
105+
When we create a buffer to read bytes into, we always have to provide a fixed size array.
106+
However, the `script_size` is dynamic and cannot be known at compile time.
107+
It can only be determined at runtime.
108+
You're not able to do something like this as the compiler will complain:
82109

83110
```rust
84111
let mut buffer = [0; script_size];
85112
```
86113

87-
Let's look closer at the `read` method and what type of argument it accepts. If we look at the documentation, it accepts the argument of type `&mut [u8]`. https://doc.rust-lang.org/std/io/trait.Read.html#tymethod.read
114+
Let's look closer at the `read` method and what type of argument it accepts.
115+
If we look at the documentation, it accepts the argument of type `&mut [u8]`.
116+
https://doc.rust-lang.org/std/io/trait.Read.html#tymethod.read
88117

89-
This is interesting. Technically, it only accepts a mutable reference to a slice. But we've actually been passing in a mutable reference to an array! Remember an array is a fixed size of type `[u8; n]` and not a slice of type `[u8]`. So how has this been working at all? I thought we had to be explicit with types in Rust?
118+
This is interesting.
119+
Technically, it only accepts a mutable reference to a slice.
120+
But we've actually been passing in a mutable reference to an array! Remember an array is a fixed size of type `[u8; n]` and not a slice of type `[u8]`.
121+
So how has this been working at all?
122+
I thought we had to be explicit with types in Rust?
90123

91-
Well, under the hood, Rust is making an implicit conversion. It does this in a few different cases. In the case of an array, there is something known as an **Unsized Coercion**, in which it will automatically convert a sized type (such as an array, `[T; n]`) into an unsized type (a slice, `[T]`).
124+
Well, under the hood, Rust is making an implicit conversion.
125+
It does this in a few different cases.
126+
In the case of an array, there is something known as an **Unsized Coercion**, in which it will automatically convert a sized type (such as an array, `[T; n]`) into an unsized type (a slice, `[T]`).
92127

93-
There is also something known as a **Deref Coercion**, which we can take advantage of here and which is something we alluded to in chapter 9. Basically, if a type implements the `Deref` trait, Rust will implicitly call the `deref` method on it until it gets the type that matches the argument's required type.
128+
There is also something known as a **Deref Coercion**, which we can take advantage of here and which is something we alluded to in chapter 9.
129+
Basically, if a type implements the `Deref` trait, Rust will implicitly call the `deref` method on it until it gets the type that matches the argument's required type.
94130

95-
So going back to reading our script, what we want is a dynamically-sized buffer to read into. A vector would work just fine. But can we use it? Can we pass it into the `read` method as an argument? It turns out we can! In Rust, a Vec [implements the `DerefMut`](https://doc.rust-lang.org/src/alloc/vec/mod.rs.html#2769) trait which dereferences to a slice. So we can initialize a `Vec` filled with 0s of the size of the script and then pass that into the `read` method as a mutable reference (`&mut Vec<u8>`). It will then be dereferenced to a slice and match the correct argument type, which is `&mut [u8]`.
131+
So going back to reading our script, what we want is a dynamically-sized buffer to read into.
132+
A vector would work just fine.
133+
But can we use it?
134+
Can we pass it into the `read` method as an argument?
135+
It turns out we can!
136+
In Rust, a Vec [implements the `DerefMut`](https://doc.rust-lang.org/src/alloc/vec/mod.rs.html#2769) trait which dereferences to a slice.
137+
So we can initialize a `Vec` filled with 0s of the size of the script and then pass that into the `read` method as a mutable reference (`&mut Vec<u8>`).
138+
It will then be dereferenced to a slice and match the correct argument type, which is `&mut [u8]`.
96139

97140
We'll create a new function called `read_script` which will return a `Vec<u8>`:
98141

@@ -105,7 +148,8 @@ fn read_script(transaction_bytes: &mut &[u8]) -> Vec<u8> {
105148
}
106149
```
107150

108-
Lastly, we need to read the sequence, which are the last 4 bytes. A description of what the sequence number represents can be found in Mastering Bitcoin, Chapter 6.
151+
Lastly, we need to read the sequence, which are the last 4 bytes.
152+
A description of what the sequence number represents can be found in Mastering Bitcoin, Chapter 6.
109153

110154
```rust
111155
...
@@ -118,7 +162,9 @@ Lastly, we need to read the sequence, which are the last 4 bytes. A description
118162
...
119163
```
120164

121-
Alright, now that we have each of the components of an input, what should we do with it? It makes sense to collect all this data together into one unified structure rather than just separate variables. The right type for this is Rust's `Struct` type, which we'll explore in the next lesson. Onwards!
165+
Alright, now that we have each of the components of an input, what should we do with it? It makes sense to collect all this data together into one unified structure rather than just separate variables.
166+
The right type for this is Rust's `Struct` type, which we'll explore in the next lesson.
167+
Onwards!
122168

123169
### Additional Reading
124170
* Implicit Deref Coercions: https://doc.rust-lang.org/book/ch15-02-deref.html#implicit-deref-coercions-with-functions-and-methods

0 commit comments

Comments
 (0)