Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chapter 5, Exercise 2: inputs parsing part of the parse method in Tx and the parse method for TxIn. #230

Open
bn185068 opened this issue Dec 16, 2021 · 2 comments

Comments

@bn185068
Copy link

In the TxIn class, the solution for the parse method is as follows:

# tag::answer2.2[]
@classmethod
def parse(cls, s):
    '''Takes a byte stream and parses the tx_input at the start.
    Returns a TxIn object.
    '''
    prev_tx = s.read(32)[::-1]
    prev_index = little_endian_to_int(s.read(4))
    script_sig = Script.parse(s)
    sequence = little_endian_to_int(s.read(4))
    return cls(prev_tx, prev_index, script_sig, sequence)
# end::answer2.2[]

I've re-read through pages 87-96 a couple times looking for the rationale for this line of code

prev_tx = s.read(32)[::-1]

While I understand that [::-1] reverses the s.read(32), it's not clear to me why this is done. The best conclusion I could draw was: if there are multiple inputs, the bytes would be pushed onto the stack one after another resulting in the first input showing up last and the last input showing up first in the byte stream (i.e. Stack = LIFO).

Can someone help clarify this for me? Thanks!

@mplsgrant
Copy link

mplsgrant commented Jan 21, 2022

While I understand that [::-1] reverses the s.read(32), it's not clear to me why this is done.

I am new to this topic, but I believe that this quote from the learnmeabitcoin site sheds light on this issue:

TXID Byte Order: When you refer to a TXID within transaction data, you have to reverse the byte order to get it in its original format. The byte-order used when searching for a TXID is in reverse (due to a historical mistake in the way the original bitcoin client works).

@Seriousaccident
Copy link

P 93:

"Note that the previous transaction ID is 32 bytes and that the previous transaction index is 4 bytes. Both are in little-endian."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants