Skip to content

Conversation

@jnewbery
Copy link
Contributor

In 7c4bf77 , @jl2012 updated getrawtransaction() to return the witness data for transaction inputs spending P2WPK and P2WSH.

bitcoin-tx doesn't call into this function and instead calls TxToUniv() in core_write.cpp, so bitcoin-tx calls don't return witness data.

This PR adds code to TxToUniv() to return witness data.

The full fix is to have getrawtransaction() call into TxToUniv() so there isn't duplicate code. I'll open a separate PR for that change.

@sipa
Copy link
Member

sipa commented Sep 28, 2016

utACK 76992bf

Side note: it seems we have no tests for bitcoin-tx JSON functionality, despite there being some other tests for it. Perhaps it can be added (see src/test/data/bitcoin-util-test.py).

@jonasschnelli
Copy link
Contributor

utACK 76992bf
Agree with @sipa about the tests.

@jnewbery
Copy link
Contributor Author

I've added JSON test cases in #8829

@maflcko maflcko added this to the 0.13.1 milestone Sep 29, 2016
@maflcko
Copy link
Member

maflcko commented Sep 29, 2016

I assume this needs backport?

@btcdrak
Copy link
Contributor

btcdrak commented Sep 29, 2016

utACK 76992bf

@fivepiece
Copy link
Contributor

fivepiece commented Sep 29, 2016

I'll reference this here as well since it all seems (to me) to be setting some kind of precedent wrt blank transactions.
Would love a bit of insight into this.
#8837 (comment)

Cleared.

@jtimon
Copy link
Contributor

jtimon commented Sep 30, 2016

Concept ACK. I believe it would be simpler and not harder for me to review if you go with your "full solution that doesn't duplicate code" in the same PR, but that may just be me...

@sipa
Copy link
Member

sipa commented Oct 3, 2016

utACK 76992bf

@gmaxwell
Copy link
Contributor

gmaxwell commented Oct 3, 2016

ACK

if (!tx.wit.IsNull()) {
if (!tx.wit.vtxinwit[i].IsNull()) {
UniValue txinwitness(UniValue::VARR);
for (unsigned int j = 0; j < tx.wit.vtxinwit[i].scriptWitness.stack.size(); j++) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Might be good to use BOOST_FOREACH here (for readability and to avoid copying the stack items).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

In that case, for (const auto& item : tx.wit.vtxinwit[i].scriptWitness.stack) would be great.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep. Much better. Thanks!

o.pushKV("hex", HexStr(txin.scriptSig.begin(), txin.scriptSig.end()));
in.pushKV("scriptSig", o);
if (!tx.wit.IsNull()) {
if (!tx.wit.vtxinwit[i].IsNull()) {
Copy link
Contributor

@ryanofsky ryanofsky Oct 3, 2016

Choose a reason for hiding this comment

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

This check is redundant with the check above, and might be worth removing.

It would probably also be good to add an (i < tx.wit.vtxinwit.size()) check or assertion.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've added your (i < tx.wit.vtxinwit.size()) check and moved all checks into a single if statement.

o.pushKV("asm", ScriptToAsmStr(txin.scriptSig, true));
o.pushKV("hex", HexStr(txin.scriptSig.begin(), txin.scriptSig.end()));
in.pushKV("scriptSig", o);
if (!tx.wit.IsNull()) {
Copy link
Contributor

@ryanofsky ryanofsky Oct 3, 2016

Choose a reason for hiding this comment

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

For clarity and efficiency would suggest moving this outside the for loop, since internally CTxWitness::IsNull loops over all the i's.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think we need to be too concerned about efficiency here. ScriptPubKeyToUniv() isn't heavily used, so looping through the CTxIn isn't going to cause any performance issues.

Copy link
Contributor

Choose a reason for hiding this comment

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

Sounds good. I was suggesting this more for clarity than efficiency, though. Right now I would be surprised looking at this code to learn that if that any /single/ vtxinwit is null, then no output will be produced for all other vtxinwit. Adding a well named bool variable outside the for loop might make it a little clearer. Anyway, thanks for considering this, and for making the other changes.

Copy link
Contributor

Choose a reason for hiding this comment

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

Actually, I'm wrong about this. CTxWitness::IsNull returns true if all vtxinwits are null, not if any are null, so having this check isn't actually misleading w.r.t the individual entries, just redundant.

@jnewbery jnewbery force-pushed the bitcoin-tx-witness branch 3 times, most recently from eb930c9 to 9f85db7 Compare October 3, 2016 22:23
@jnewbery
Copy link
Contributor Author

jnewbery commented Oct 3, 2016

I've applied @ryanofsky's changes and rebased.

@maflcko
Copy link
Member

maflcko commented Oct 4, 2016

Fails with Output data mismatch for blanktx.json

Copy link
Member

Choose a reason for hiding this comment

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

Why? Please keep it at BOOST or make it cpp11

Copy link
Member

Choose a reason for hiding this comment

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

I think it's because he needs index i below. I've wondered this before: does c++11 have a canonical way to do enumerated iteration (like enumerate() in python or for i, a := range ... { in golang)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's correct. I don't think there's a direct equivalent for python's enumerate in c++11. The most common suggestions I can find on stackoverflow are zipping with an index or tracking the index outside the for loop. I think it's clearer just to iterate through the indexes in the for loop.

@laanwj
Copy link
Member

laanwj commented Oct 4, 2016

Failing test:


make  check-TESTS check-local
make[3]: Entering directory `/home/travis/build/bitcoin/bitcoin/build/src'
Running test/bitcoin-util-test.py...
make[4]: Entering directory `/home/travis/build/bitcoin/bitcoin/build/src'
Output data mismatch for blanktx.json
make[3]: *** [check-local] Error 1
make[3]: *** Waiting for unfinished jobs....

@fanquake
Copy link
Member

fanquake commented Oct 5, 2016

Looks like all tests are now passing.

Making check in src
/Applications/Xcode.app/Contents/Developer/usr/bin/make  check-TESTS check-local
PASS: test/test_bitcoin
PASS: qt/test/test_bitcoin-qt
============================================================================
Testsuite summary for Bitcoin Core 0.13.99
============================================================================
# TOTAL: 2
# PASS:  2
# SKIP:  0
# XFAIL: 0
# FAIL:  0
# XPASS: 0
# ERROR: 0
============================================================================
Running test/bitcoin-util-test.py...
/Applications/Xcode.app/Contents/Developer/usr/bin/make  check-TESTS
PASS: tests
``

@jnewbery jnewbery force-pushed the bitcoin-tx-witness branch from 3a32aa8 to 4408558 Compare October 5, 2016 13:00
@jnewbery
Copy link
Contributor Author

jnewbery commented Oct 5, 2016

Commits squashed and commit comment updated.

@laanwj laanwj merged commit 4408558 into bitcoin:master Oct 13, 2016
laanwj added a commit that referenced this pull request Oct 13, 2016
4408558 Update bitcoin-tx to output witness data. (jonnynewbs)
laanwj pushed a commit to laanwj/bitcoin that referenced this pull request Oct 13, 2016
@jnewbery jnewbery deleted the bitcoin-tx-witness branch October 14, 2016 09:23
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Sep 8, 2021
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.