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

Introduce deploymentstatus #19438

Merged
merged 12 commits into from
Jul 1, 2021
Merged

Conversation

ajtowns
Copy link
Contributor

@ajtowns ajtowns commented Jul 3, 2020

Introduces helper functions to make it easy to bury future deployments, along the lines of the suggestion from 11398 "I would prefer it if a buried deployment wouldn't require all code paths that check the BIP9 status to require changing".

This provides three functions: DeploymentEnabled() which tests if a deployment can ever be active, DeploymentActiveAt() which checks if a deployment should be enforced in the given block, and DeploymentActiveAfter() which checks if a deployment should be enforced in the block following the given block, and overloads all three to work both with buried deployments and versionbits deployments.

This adds a dedicated lock for the versionbits cache, which is acquired internally by the versionbits functions, rather than relying on cs_main. It also moves moves versionbitscache into deploymentstatus to avoid a circular dependency with validation.

@DrahtBot
Copy link
Contributor

DrahtBot commented Jul 3, 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

@JeremyRubin JeremyRubin 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 and some lite code review feedback.

@@ -11,6 +11,15 @@

namespace Consensus {

enum BuriedDeployment
Copy link
Contributor

Choose a reason for hiding this comment

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

prefer enum class to prevent leaking names.

Copy link
Contributor

Choose a reason for hiding this comment

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

Also helps with avoiding overlap with SignalledDeployment.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Leaking the names is the point -- otherwise you'd need to change every use from Consensus::DeploymentPos::SEGWIT to Consensus::BuriedDeployment::SEGWIT. With C++20, you will be able to specify enum class DeploymentPos { .. }; using enum DeploymentPos; but that's a while away.

Comment on lines 17 to 21
DEPLOYMENT_CLTV = -255,
DEPLOYMENT_DERSIG,
DEPLOYMENT_CSV,
DEPLOYMENT_SEGWIT,
Copy link
Contributor

Choose a reason for hiding this comment

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

prefer explicitly numbering these.

-255 is an extremely odd number, can you clarify why you picked it? It doesn't fit in a signed byte.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Explicitly numbering them risks accidentally repeating a number. No particular objection to a different starting point. Could perhaps make it enum DeploymentPos : uint8_t and enum BuriedDeployment : int16_t { DEPLOYMENT_CLTV = 256, // larger than largest possible DeploymentPos value.

(I guess 2**n is an "extremely even" number, so calling +/- 2**n-1 "extremely odd" makes a lot of sense...)

@@ -80,7 +89,19 @@ struct Params {
int64_t DifficultyAdjustmentInterval() const { return nPowTargetTimespan / nPowTargetSpacing; }
uint256 nMinimumChainWork;
uint256 defaultAssumeValid;

inline int DeploymentHeight(BuriedDeployment dep) const
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe tighter to make this a templated function as we're never calling DeploymentHeight with a variable, only literal.

Then we get not just a warning, but a compiler error.

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 had tried that, but thought that it might be useful to be able to do for (dep = 0; dep < MAX_VERSIONBITS_DEPLOYMENTS; ++dep) { ... DeploymentActiveAt(pindex, consParams, dep) .. }. I think in all the cases where that currently comes up you want to know more fine grained details though. (Changing BuriedForkDescPushBack to take a BuriedDeployment instead of a height would make use of that, eg)

I don't think the type checking is any better with the template though, as unfortunately you can invoke DeploymentHeight<(BuriedDeployment)9000>() just as easily as DeploymentHeight((BuriedDeployment)9000) with both g++ and clang++.

src/deploymentstatus.h Outdated Show resolved Hide resolved
extern VersionBitsCache versionbitscache GUARDED_BY(cs_main);

/** Determine if a deployment is active for the next block */
inline bool DeploymentActiveAfter(const CBlockIndex* pindexPrev, const Consensus::Params& params, Consensus::BuriedDeployment dep)
Copy link
Contributor

Choose a reason for hiding this comment

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

This dispatch scares me a bit unless we use enum class -- enums could coerce poorly right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nope; int (and the like) won't automatically coerce to enum, so with int x = Consensus::DEPLOYMENT_SEGWIT; return DeploymentActiveAt(pindex, params, x) you get errors:

validation.cpp:1921:12: error: no matching function for call to 'DeploymentActiveAt'
    return DeploymentActiveAt(pindex, consensusparams, x);
           ^~~~~~~~~~~~~~~~~~
./deploymentstatus.h:32:13: note: candidate function not viable: no known conversion from 'int' to 'Consensus::BuriedDeployment' for 3rd argument
inline bool DeploymentActiveAt(const CBlockIndex* pindex, const Consensus::Params& params, Consensus::BuriedDeployment dep)
            ^
./deploymentstatus.h:37:13: note: candidate function not viable: no known conversion from 'int' to 'Consensus::DeploymentPos' for 3rd argument
inline bool DeploymentActiveAt(const CBlockIndex* pindex, const Consensus::Params& params, Consensus::DeploymentPos dep) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
            ^

(even if it did, which enum it coerced to is ambiguous between the two types, resulting in a failure like error: call to 'DeploymentActiveAt' is ambiguous)

@ajtowns ajtowns force-pushed the 202007-deployment-refactor branch from 5834cc8 to 6b08b76 Compare July 3, 2020 21:48
@ajtowns ajtowns force-pushed the 202007-deployment-refactor branch from e14966e to df308ab Compare June 29, 2021 22:03
@ajtowns
Copy link
Contributor Author

ajtowns commented Jun 29, 2021

Rebased past #21789

Adds support for versionbits deployments to DeploymentEnabled,
DeploymentActiveAfter and DeploymentActiveAt. Also moves versionbitscache
from validation to deploymentstatus.
-BEGIN VERIFY SCRIPT-
sed -i -e 's/versionbitscache/g_versionbitscache/g' $(git grep -l versionbitscache)
-END VERIFY SCRIPT-
Rename BIP9SoftForkPushBack and BuriedSoftForkPushBack to SoftForkPushBack
and have the compiler figure out which one to use based on the deployment
type. Avoids the need to update the file when burying a deployment.
Moves the VersionBits* functions to be methods of the cache class,
and makes the cache and its lock private to the class.
This also changes ComputeBlockVersion to take the versionbits cache
mutex once, rather than once for each versionbits deployment.
Copy link
Member

@maflcko maflcko left a comment

Choose a reason for hiding this comment

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

re-ACK e48826a 🥈

Checked with:
git range-diff bitcoin-core/master e14966e e48826a --word-diff-regex=. -U0

Show signature and timestamp

Signature:

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

re-ACK e48826ad87b4f92261f7433e84f48dac9bd9e5c3 🥈

Checked with:
git range-diff bitcoin-core/master e14966ee31f02e359896dbb72e2627b3ae12a314 e48826ad87b4f92261f7433e84f48dac9bd9e5c3 --word-diff-regex=. -U0
-----BEGIN PGP SIGNATURE-----

iQGzBAEBCgAdFiEE+rVPoUahrI9sLGYTzit1aX5ppUgFAlwqrYAACgkQzit1aX5p
pUh+dAv+NVpZKf22BScmqPb4YLJH2snw87TwSnr6Ez2ocyL441p57JrElilouzMN
gklFgwx0RYY2WlGKtGVub1aa3gVLqmDbdWKi6sGxTJB+5tbQnaK4S5tEDKAWfPDu
q7xawsZ7mOKeyIJXlRrwMiipeziVGtuDW7pQ5OkfCfQevvCMmexdA/vwigKR9UbW
adVkvMNwkA1g8LLbfn3xMUGk3QnidnYyaJtP+55LK91kLMx/5fDgmEoj5kBFYu1s
8n5SE4UW06RfkUfAuPJT5TgM3IjxoHmO9bwQbdh27a/1yY+zTggjy9Ak8cWs3xAi
jr3NjAohqfp1EKKMP8/oGTUn7AHMUu/tUPzqWH0iTJ2NdGt8HHrT54qBSB7ryS/W
+rfMOv0G3quOWCd0jc5zQLHsaMndlItfPgRMWBgMQ255zo0hWBOs6HV9Xe1kuZgF
4jCLR/KvxoK9/e0ore9XqEDpyeABo0oRwzVSg0XrHIMWHSpB2qOZx1ycQwqnoaR4
Y722yBJ3
=PNtg
-----END PGP SIGNATURE-----

Timestamp of file with hash 51b700b2e20a4085313cce0dc833915df20496b87f40dbee5ed2e7651008877d -

@jnewbery
Copy link
Contributor

ACK e48826a

@gruve-p
Copy link
Contributor

gruve-p commented Jul 1, 2021

ACK e48826a

@maflcko maflcko merged commit ddc6979 into bitcoin:master Jul 1, 2021
@bitcoin bitcoin deleted a comment Jul 1, 2021
@bitcoin bitcoin deleted a comment Jul 1, 2021
@bitcoin bitcoin deleted a comment Jul 1, 2021
@bitcoin bitcoin deleted a comment Jul 1, 2021
@bitcoin bitcoin deleted a comment Jul 1, 2021
@bitcoin bitcoin deleted a comment Jul 1, 2021
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Jul 3, 2021
gwillen pushed a commit to ElementsProject/elements that referenced this pull request Jun 1, 2022
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Aug 16, 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.

None yet