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

refactor: Improve GetWitnessCommitmentIndex #19640

Closed

Conversation

promag
Copy link
Contributor

@promag promag commented Aug 1, 2020

GetWitnessCommitmentIndex finds the last output index of coinbase transaction. Currently iterates all outputs. This change reverses iteration and so it only iterates all outputs at worst case.

@promag
Copy link
Contributor Author

promag commented Aug 1, 2020

Originally added in #8149 by @sipa. Got my attention while reviewing 3339d01 from #18267.

@promag promag force-pushed the 2020-08-getwitnesscommitmentindex branch from b4d3f99 to 3a25f18 Compare August 1, 2020 23:48
@promag promag force-pushed the 2020-08-getwitnesscommitmentindex branch from 3a25f18 to f4d5c7c Compare August 2, 2020 00:41
@DrahtBot
Copy link
Contributor

DrahtBot commented Aug 2, 2020

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Conflicts

Reviewers, this pull request conflicts with the following ones:

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

Copy link
Contributor

@theStack theStack left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Concept ACK

Comment on lines +3400 to +3401
for (size_t o = block.vtx[0]->vout.size(); o > 0;) {
const CTxOut& vout = block.vtx[0]->vout[--o];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This 1-based to 0-based array indexing conversion within the body seems rather odd, why not simply 0-base it right from the start:

        for (int o = block.vtx[0]->vout.size()-1; o >= 0; o--) {
            const TxOut& vout = block.vtx[0]->vout[o];

Drawback is that the loop counter type has to be changed from size_t (unsigned) to int signed, but I don't see why that would be a problem.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, need to change to signed. I'm fine with both TBH. Let's see what others say.

@sdaftuar
Copy link
Member

sdaftuar commented Aug 5, 2020

Concept NACK. I say this in every PR of this type: I don't think it makes sense to change consensus code unless we have some good reason to do so. See also my recent comment here.

@promag promag closed this Aug 5, 2020
@promag promag deleted the 2020-08-getwitnesscommitmentindex branch August 5, 2020 16:17
@promag
Copy link
Contributor Author

promag commented Aug 5, 2020

@sdaftuar it's fine. Just curious if it was intended to pick last element.

@promag
Copy link
Contributor Author

promag commented Aug 16, 2020

@sipa ☝️ can you explain?

@maflcko
Copy link
Member

maflcko commented Aug 17, 2020

The bip says that the last element has to be picked

@bitcoin bitcoin locked as resolved and limited conversation to collaborators Feb 15, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants