This removes boilerplate code in the subclasses which otherwise only differ by the result type.
The subclassing was introduced in a27a295.
This removes boilerplate code in the subclasses which otherwise only differ by the result type.
The subclassing was introduced in a27a295.
Code review ACK aa67e6d736fab1cf125687f3393a3eb259fb8608.
Medium concept ACK. Nice that this eliminates duplicate code. Added #includes and template function might slow down compilation a bit, though.
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.
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.
87 std::string m_reject_reason;
88 std::string m_debug_message;
89-protected:
90- void Invalid(const std::string &reject_reason="",
91+public:
92+ ValidationState() : m_mode(MODE_VALID) {}
} m_mode{MODE_VALID};
and drop constructor?
Invalid
to the public
section and not modifying the constructor. Is the convention to put this in a separate a commit? Or would it be considered outside the scope of the PR?
4@@ -5,6 +5,8 @@
5 #ifndef BITCOIN_CONSENSUS_TX_CHECK_H
6 #define BITCOIN_CONSENSUS_TX_CHECK_H
7
8+#include <consensus/validation.h>
TxValidationState
and BlockValidationState
where needed and avoid the issues pointed by @ryanofsky?
This causes a compilation error:
0In file included from consensus/tx_check.cpp:8:
1./consensus/validation.h:115:7: error: typedef redefinition with different types ('ValidationState<TxValidationResult>' vs 'TxValidationState')
2using TxValidationState = ValidationState<TxValidationResult>;
3 ^
4./consensus/tx_check.h:16:7: note: previous definition is here
5class TxValidationState;
I’d have to forward declare the full template type and enum then use that in the function declaration.
0enum class TxValidationResult;
1template <typename Result>
2class ValidationState;
3
4bool CheckTransaction(const CTransaction& tx, ValidationState<TxValidationResult>& state);
Seems a bit verbose and would mismatch the function definition. FWIW, here are some pros and cons of forward declaring that I’m aware of.
Concept ACK.
Added #includes and template function might slow down compilation a bit, though.
Yes, although this is a quite straightforward use of templates, no boost nested template horrors, so I hope it’s not too bad.
13+#include <string>
14
15 /** Convert ValidationState to a human-readable message for logging */
16-std::string FormatStateMessage(const ValidationState &state);
17+template <typename Result>
18+std::string FormatStateMessage(const ValidationState<Result>& state)
Could move this into ValidationState directly, and make it
0std::string FormatStateMessage() const { return m_reject_reason + (m_debug_message.empty() ? "" : ", " + m_debug_message); }
140- ValidationState::Invalid(reject_reason, debug_message);
141- return false;
142- }
143- BlockValidationResult GetResult() const { return m_result; }
144-};
145+using TxValidationState = ValidationState<TxValidationResult>;
class TxValidationState : ValidationState<TxValidationResult> { };
and still be able to use forward declarations class TxValidationState;
fwiw.
14- return strprintf("%s%s",
15- state.GetRejectReason(),
16- state.GetDebugMessage().empty() ? "" : ", "+state.GetDebugMessage());
17-}
18-
19 const std::string strMessageMagic = "Bitcoin Signed Message:\n";
util/validation.h
to util/strings.h
since it isn’t anything to do with validation any more, and I can’t find anywhere that would be a better home for this const.
std::string
can be constexpr. Also, moving it to the header file might inline it in every translation unit. Probably doesn’t matter for a short string like this, but no need to change existing code to make it worse.
std::string
can’t be constexpr.
Looking at the references to strMessageMagic
, they are always of the form:
0CHashWriter ss(SER_GETHASH, 0);
1ss << strMessageMagic;
2ss << strMessage;
And then the result of its GetHash
is passed to either SignCompact
or RecoverCompact
, and the variable ss
is never used again.
So the above code could be refactored into a function where strMessageMagic
is just an implementation detail. Given that, is there a more appropriate place for it? What does ss
stand for here?
What does ss stand for here?
Not sure. Serialization stream?
CDataStream ss
variables in the codebase since Satoshi era)
Perhaps I could move that boilerplate code to util/hash.h
as HashMessage
?
0uint256 HashMessage(const std::string& message);
Open to more suitable naming for the file and function. Note that util/string.h
already exists for string joining, so the plural version may be confusing.
Would likely leave this to a follow-up PR given I’d be refactoring call sites.
Open to more suitable naming for the file and function
Perhaps /util/message.h
?
Would likely leave this to a follow-up PR given I’d be refactoring call sites.
Yeah, anything other than a simple rename should be left as a follow-up PR.
Tested ACK 0468628e8191c574d2568f5e9e7fa15dd43d80f2. Nice tidy up!
One suggestion for getting rid of a now almost-empty file. Can be done in this PR or a follow-up.
Code review ACK 0468628e8191c574d2568f5e9e7fa15dd43d80f2.
Restarted failed job on travis.
Code review ACK 1452949.
About the increase compilation time concern, on my system compare to master it’s even a bit slightly slightly faster.
ACK 14529499743e071ddc091e5508e1eea4532e754d 🎏
Signature:
0-----BEGIN PGP SIGNED MESSAGE-----
1Hash: SHA512
2
3ACK 14529499743e071ddc091e5508e1eea4532e754d 🎏
4-----BEGIN PGP SIGNATURE-----
5
6iQGzBAEBCgAdFiEE+rVPoUahrI9sLGYTzit1aX5ppUgFAlwqrYAACgkQzit1aX5p
7pUgCpgv/XrM9w3dqV6IqX53LVCU81lx6hex2Gjbb8X0wb2rfaBxS1H/y+DHq9cPs
8iBh6bp5HGABL2dRPcS3mvyBzA9lbWu4MZVzqu728m9m2tUdMjnezxzz0tZOhmcWL
9vW8+UpYVnr4UahR9k5X2N06v7YoqkpNmQ06aSOkRCNd7XPkjOAh3SN5zOSUjs1n8
102wUOPPNAbBS5UgFt6vTPTzm6AMNWp+fXtqnSIe84JFkefEoFo0EW1f72Nyb1agrG
1170Cxd+Ph4de0yZq+lvcrZz62JPOGwF8AWW8ls+82hOFMmmxI1eduQPo9xuEvv14b
12euvSNwjmUenZBhldWXAR0ct13O3NNgSzdbWZjBomqtDFDa5MpcOGHeCppYmxykD7
137FX/+Ye7Q181D7vXBFj/YBEGqPyIuLIm0NV1N6EPl8gYiqaK0c7UF0TJPqwWE6k1
14HjhDdT4cD9rZrcl9u6mt2zdjcavNuyfMO13seNINZVXuNkgleta/sCh2edQU41XL
15I09hdKhh
16=2AEE
17-----END PGP SIGNATURE-----
Timestamp of file with hash 70e15c1a24082f6b136c95c7920ed1bc271fccb39fdfee1f98976554082edd59 -
utACK 14529499743e071ddc091e5508e1eea4532e754d
Since the initialization relies on [TX|BLOCK]_RESULT_UNSET
being 0, what do you think of:
0--- a/src/consensus/validation.h
1+++ b/src/consensus/validation.h
2@@ -16,7 +16,7 @@
3 * provider of the transaction should be banned/ignored/disconnected/etc.
4 */
5 enum class TxValidationResult {
6- TX_RESULT_UNSET, //!< initial value. Tx has not yet been rejected
7+ TX_RESULT_UNSET = 0, //!< initial value. Tx has not yet been rejected. Must be zero/first defined enumerator for default initialization.
8 TX_CONSENSUS, //!< invalid by consensus rules
9 /**
10 * Invalid by a change to consensus rules more recent than SegWit.
11@@ -50,7 +50,7 @@ enum class TxValidationResult {
12 * useful for some other use-cases.
13 */
14 enum class BlockValidationResult {
15- BLOCK_RESULT_UNSET, //!< initial value. Block has not yet been rejected
16+ BLOCK_RESULT_UNSET = 0, //!< initial value. Block has not yet been rejected. Must be zero/first defined enumerator for default initialization.
17 BLOCK_CONSENSUS, //!< invalid by consensus rules (excluding any below reasons)
18 /**
19 * Invalid by a change to consensus rules more recent than SegWit.
utACK 1452949
Since the initialization relies on
[TX|BLOCK]_RESULT_UNSET
being 0, what do you think of:0--- a/src/consensus/validation.h 1+++ b/src/consensus/validation.h 2@@ -16,7 +16,7 @@ 3 * provider of the transaction should be banned/ignored/disconnected/etc. 4 */ 5 enum class TxValidationResult { 6- TX_RESULT_UNSET, //!< initial value. Tx has not yet been rejected 7+ TX_RESULT_UNSET = 0, //!< initial value. Tx has not yet been rejected. Must be zero/first defined enumerator for default initialization. 8 TX_CONSENSUS, //!< invalid by consensus rules 9 /** 10 * Invalid by a change to consensus rules more recent than SegWit. 11@@ -50,7 +50,7 @@ enum class TxValidationResult { 12 * useful for some other use-cases. 13 */ 14 enum class BlockValidationResult { 15- BLOCK_RESULT_UNSET, //!< initial value. Block has not yet been rejected 16+ BLOCK_RESULT_UNSET = 0, //!< initial value. Block has not yet been rejected. Must be zero/first defined enumerator for default initialization. 17 BLOCK_CONSENSUS, //!< invalid by consensus rules (excluding any below reasons) 18 /** 19 * Invalid by a change to consensus rules more recent than SegWit.
Good call. Pushed a975974.
@promag @ariard @MarcoFalke You can verify the change with git range-diff abb30de63 145294997 a975974f4
.
TxValidationResult
/BlockValidationResult
enums to 0.
Rebased to account for changes in #16688. Specifically, pushed 7bb194ba252191b531803ad2952c984c70d192da to:
FormatStateMessage
made in 72f3227c83810936e7a334304e5fd7c6dab8e91b and 6edebacb2191373e76d79a4972d6192300976096FormatStateMessage
in f9abf4ab6d3d3e4d4b7e90723020b5422a141a6fCan be verified with:
0git range-diff 17e14ac..2afff87 e7f8450..bf5bc26
This removes boilerplate code in the subclasses which otherwise only
differ by the result type.
ACK 10efc0487c442bccb0e4a9ac29452af1592a3cf2 🐱
Signature:
0-----BEGIN PGP SIGNED MESSAGE-----
1Hash: SHA512
2
3ACK 10efc0487c442bccb0e4a9ac29452af1592a3cf2 🐱
4-----BEGIN PGP SIGNATURE-----
5
6iQGzBAEBCgAdFiEE+rVPoUahrI9sLGYTzit1aX5ppUgFAlwqrYAACgkQzit1aX5p
7pUiSBQv9GTJ4FVCrg3wq2XM0X75nYvO3+OK32UmnzgXDJjk7Pghohc7v7mUhqpnc
8hmwCVwvvs9r5PdRx2BYx/GgPi6UEQ0KSjZfDxKUYcA7lMYCcHlHUxoohNj0mssN+
9hpuGhzhBEYyX1jHNFazpSMSlKVPvesXQRHgo4Kw4EyyI1EU1Mpdi/nPWpsY6Of0y
10Gg49gHJVvMp08RmkTVLRBWF0KqUATRfUTgqVUr5bit3tKZZPacxaiXSE8W8WXRa5
11Nb6e1WHAa8+jWqlahlCFJnw4UoyXSIHtijhBgVliCo7wX5Lj588VjouQeaXEIufZ
12q823LmNj8QdOwnNnE9wMSKYX/kQaGB+R3KRDpH+s6zHub86fA0/oz3dkER4XWaIM
13DZNcYPnJYNgWtZ9f99uIC87pHeQ3kFA+5/xroVzjPvFEGLv7euwlgZn0v2rb2dB5
14vgmIwvEH/B8/pH3GvL65yGCVFrkVA5rAnNGViav0KJujkWXukc0fkC9to5urHnwB
15oz7K+tzI
16=g9Fg
17-----END PGP SIGNATURE-----
Timestamp of file with hash 02d3cb1c7845b1a605977e2f801af0118c57df49376b61c77970f1807528dacf -