Skip to content

Conversation

@sipa
Copy link
Member

@sipa sipa commented Aug 4, 2016

As explained on zcash/zcash#967, using const member fields in CTransaction (or any deserializable class) is not well defined in C++, as such fields should only ever be assigned at construction time.

I believe we can get rid of them. Their purpose was making sure that CTransaction was not used in places where mutable fields were required, but now that CMutableTransaction is well-established that requirement is not so strong anymore.

Longer term, I'd prefer to see better encapsulation of CTransaction (which could also introduce better internal representation of the data, without a gazillion allocations) and improvements to the serialization framework so that we can construct objects by deserializing... but I think that's further out than a fix for this.

So at a higher level, this changes CTransaction from "immutable transaction data" to "transaction data with cached hashes". I've added a small comment about this as well.

Ping @daira @ebfull.

@ebfull
Copy link

ebfull commented Aug 4, 2016

utACK

@dcousens
Copy link
Contributor

dcousens commented Aug 4, 2016

utACK 232529f

@laanwj
Copy link
Member

laanwj commented Aug 4, 2016

From a design point I really liked CTransaction as immutable transaction data, but yes it's better to be without the C++ hacks.

@dcousens
Copy link
Contributor

dcousens commented Aug 4, 2016

Wouldn't this also negate the need for CMutableTransaction? (which is only used in bitcoin-tx?)

@sipa
Copy link
Member Author

sipa commented Aug 4, 2016

@dcousens It's used in all signing code. And CMutableTransaction has the advantage of not needing to call UpdateHash all the time, so it may be useful to keep.

@sipa
Copy link
Member Author

sipa commented Aug 4, 2016

@laanwj I agree, it gave a pretty clean and safe abstraction... but I know no way to do it correctly with the current serialization framework. We should think about a construct-while-deserializing mechanism if we want that.

@daira
Copy link
Contributor

daira commented Aug 8, 2016

NACK due to sipa@232529f#commitcomment-18556288 (easily fixed).

@sipa
Copy link
Member Author

sipa commented Aug 8, 2016

@daira In my understanding (but I may be wrong), a reference to a const value only means that that value cannot be modified through that reference but does not mean that it cannot change overall. This is different from declaring a variable or field as const, which means it cannot change at all.

@sipa
Copy link
Member Author

sipa commented Aug 16, 2016

@daira I addressed all your comments apart from the returning of const uint256&.

This issue made me investigate the concept of const correctness in C++ much more deeply, but all explanations I can find state that a const reference merely means that the value aliased by that reference cannot be modifed through that const reference. It is not a guarantee that the value cannot be modified at all.

Consider the example at the bottom of http://en.cppreference.com/w/cpp/language/const_cast; a const reference (cref_i) is taken of a non-const value (i); then it is modified through the non-const reference const_cast<int&>(cref_i), and accessed through the const reference cref_i. If your reasoning was correct, this example would be undefined. The issue that this PR does fix is lower down in the same example: j is a const value, and as such no non-const aliases exist, and const casting it to non-const is therefore illegal.

@daira
Copy link
Contributor

daira commented Aug 24, 2016

@sipa
Copy link
Member Author

sipa commented Sep 19, 2016

See #8580 for an alternative solution that is a better step towards making CTransaction having its own more efficient in-memory representation.

@laanwj
Copy link
Member

laanwj commented Sep 26, 2016

Closing in favor of #8580

@laanwj laanwj closed this Sep 26, 2016
zkbot added a commit to zcash/zcash that referenced this pull request Jul 14, 2017
Remove the incorrect const declarations in CTransaction.

Based on upstream bitcoin/bitcoin#8451, which is a much, much safer and easier-to-review fix than bitcoin/bitcoin#8580. fixes #967

Signed-off-by: Daira Hopwood <[email protected]>
@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.

6 participants