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.
refactor: Improve GetWitnessCommitmentIndex #19640
pull promag wants to merge 1 commits into bitcoin:master from promag:2020-08-getwitnesscommitmentindex changing 1 files +4 −5-
promag commented at 11:16 PM on August 1, 2020: member
- promag force-pushed on Aug 1, 2020
-
refactor: Improve GetWitnessCommitmentIndex f4d5c7ce15
-
in src/validation.cpp:3409 in 3a25f18ce6 outdated
3408 | vout.scriptPubKey[2] == 0xaa && 3409 | vout.scriptPubKey[3] == 0x21 && 3410 | vout.scriptPubKey[4] == 0xa9 && 3411 | vout.scriptPubKey[5] == 0xed) { 3412 | - commitpos = o; 3413 | + return o;
ajtowns commented at 12:14 AM on August 2, 2020:return o-1;?
promag commented at 12:28 AM on August 2, 2020:Yup yup, forgot to fix this. Initially was bad because
ois unsigned. Will fix.promag force-pushed on Aug 2, 2020DrahtBot added the label Refactoring on Aug 2, 2020DrahtBot added the label Validation on Aug 2, 2020DrahtBot commented at 3:08 AM on August 2, 2020: member<!--e57a25ab6845829454e8d69fc972939a-->
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.
<!--174a7506f384e20aa4161008e828411d-->
Conflicts
Reviewers, this pull request conflicts with the following ones:
- #18267 (BIP-325: Signet [consensus] by kallewoof)
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.
in src/validation.cpp:3401 in f4d5c7ce15
3395 | @@ -3396,22 +3396,21 @@ bool IsWitnessEnabled(const CBlockIndex* pindexPrev, const Consensus::Params& pa 3396 | 3397 | int GetWitnessCommitmentIndex(const CBlock& block) 3398 | { 3399 | - int commitpos = -1; 3400 | if (!block.vtx.empty()) { 3401 | - for (size_t o = 0; o < block.vtx[0]->vout.size(); o++) { 3402 | - const CTxOut& vout = block.vtx[0]->vout[o]; 3403 | + for (size_t o = block.vtx[0]->vout.size(); o > 0;) { 3404 | + const CTxOut& vout = block.vtx[0]->vout[--o];
theStack commented at 2:46 PM on August 2, 2020: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) tointsigned, but I don't see why that would be a problem.
promag commented at 3:30 PM on August 2, 2020:Yes, need to change to signed. I'm fine with both TBH. Let's see what others say.
theStack commented at 2:54 PM on August 2, 2020: memberConcept ACK
sdaftuar commented at 3:44 PM on August 5, 2020: memberConcept 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 closed this on Aug 5, 2020promag deleted the branch on Aug 5, 2020MarcoFalke commented at 6:27 AM on August 17, 2020: memberThe bip says that the last element has to be picked
DrahtBot locked this on Feb 15, 2022Labels
This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-04-21 21:14 UTC
More mirrored repositories can be found on mirror.b10c.me