-
Notifications
You must be signed in to change notification settings - Fork 37.7k
Chainparams: Refactor: Decouple IsSuperMajority from Params() #5968
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
Conversation
I like the concept, although this has a lot of 'collateral' code impact. Of course this is a necessary step in the process toward explicitly passing in a Consensus or ChainParams structure. The part that generates most of the diff here is that which removes the getters and replaces them with And then do e.g. the GetBlockValue/GetBlockSubsidy change and other more significant changes on top of that, so that it's easier to see what happens. |
I left the last 3 commits for #5970, so the redundant getters aren't removed yet and the GetBlockValue/GetBlockSubsidy and IsSupermajority changes are also delayed. Is this what you had in mind? |
Last commit updated to avoid conflicts with #5975 after its latest chainparams fix. |
ac59d37
to
6d04c74
Compare
Rebased |
6d04c74
to
cb96bb9
Compare
cb96bb9
to
d64931f
Compare
Updated only with the changes to IsSuperMajority. Other changes are left for other PRs like #5996 |
*/ | ||
static bool IsSuperMajority(int minVersion, const CBlockIndex* pstart, unsigned int nRequired); | ||
static bool IsSuperMajority(int minVersion, const CBlockIndex* pstart, unsigned nRequired, unsigned nToCheck); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just pass Consensus::Params and use the parts here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would only serve for nToCheck. nRequired is sometimes enforceUpgrade and some times rejectOoutdated. I thought that instead of passing the params to take the majority window, I could just pass a number directly.
40bc452
to
98fee57
Compare
Added a squashme commit with @theuni 's suggestion of passing Consensus::Params instead of nToCheck. At the very least it produces shorter lines. I'm happy to squash it. |
utACK |
98fee57
to
edba47f
Compare
Squashed the last 2 commits. |
@@ -2571,19 +2570,11 @@ bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& sta | |||
if (pcheckpoint && nHeight < pcheckpoint->nHeight) | |||
return state.DoS(100, error("%s: forked chain older than last checkpoint (height %d)", __func__, nHeight)); | |||
|
|||
// Reject block.nVersion=1 blocks when 95% (75% on testnet) of the network has upgraded: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why change this? Seems less clear and less conducive to documentation. If anything, I'd prefer to see comments added for when block versions stopped being accepted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, if we have 10 voted softfork at the same time I prefer to have a loop over 10 practically identical unrolled ifs...
I would also like to have a "time proof" solution for forks that are enforced by height instead of voting as suggested in #5966 . This is the only place where you can have a generic solution for block versions.
Anyway, I don't think
// Reject block.nVersion=n blocks when 95% (75% on testnet) of the network has upgraded (last version=3)
is less clear than
// Reject block.nVersion=1 blocks when 95% (75% on testnet) of the network has upgraded:
// Reject block.nVersion=2 blocks when 95% (75% on testnet) of the network has upgraded:
just less repetitive. But if you have a better idea I'm happy to change the comments.
Or are you directly against the loop?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not completely against, but if we had 10 voted softforks, it'd probably be a good place to have 10 short explanations.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mhmm, I think putting those in ContextualCheckBlock and Consensus::GetFlags (not existing yet, see jtimon@9ba7d06) would be more informative. But anyway, it seems I squashed the loop change too fast, I can separate that again if it's going to delay this.
ut ACK other than the nit above |
bd7d718
to
6ef2e51
Compare
Updated without introducing the loop |
c7ed619
to
55d732f
Compare
@@ -2701,11 +2697,10 @@ bool AcceptBlock(CBlock& block, CValidationState& state, CBlockIndex** ppindex, | |||
return true; | |||
} | |||
|
|||
static bool IsSuperMajority(int minVersion, const CBlockIndex* pstart, unsigned int nRequired) | |||
static bool IsSuperMajority(int minVersion, const CBlockIndex* pstart, unsigned nRequired, const Consensus::Params& consensusParams) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would just pass both nMajorityWindow and nRequired as argument, and leave this as a utility function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the initial version took both ints instead of taking Consensus::Params as the second. But I changed it solving a nit because this produces shorter lines. If you mean maintaining a utility function still coupled to Params(), no, only consensus functions call this function (well, plus the consensus part of connectBlock) and they need it decoupled.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As said, at the beginning it was getting nRequired and nToCheck, but @theuni suggested the change. I'm happy to change it back but I don't have strong feelings either way.
You could also have a version with nRequired and nToCheck and two other inlined functions:
bool IsSuperMajorityUpgrade(int minVersion, const CBlockIndex* pstart, const Consensus::Params& consensusParams);
bool IsSuperMajorityRejectOld(int minVersion, const CBlockIndex* pstart, const Consensus::Params& consensusParams);
that call the Consensus::Params-independent IsSuperMajority.
That should also make things easier for replacing the softfork feature voting since one would only need to touch inside those methods and Consensus::Params.
I think those are all options but I'm happy just decoupling IsSuperMajority from the Params() factory, so if anyone has stronger opinions between the 3 options I don't really care which one to use.
Needed rebase |
unsigned int nFound = 0; | ||
for (unsigned int i = 0; i < nToCheck && nFound < nRequired && pstart != NULL; i++) | ||
for (int i = 0; i < consensusParams.nMajorityWindow && nFound < nRequired && pstart != NULL; i++) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're changing the counter data type here, is that intentional?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, to avoid introducing a new warning.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another option would be to change consensusParams.nMajorityWindow to unsigned.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm happy with an int counter, just asked to be sure.
51aa249 Chainparams: Refactor: Decouple IsSuperMajority from Params() (Jorge Timón)
After the struct Consensus::Params was created in #5812, there are some redundant getters in CChainParams.
This is not the minimal way to remove it, but the fastest way would involve putting Params().GetConsensus() instead of params or params.GetConsensus().
With a few more extra changes we're going forward functions depending explicitly on CChainParams or Consensus::Params whenever it's possible.
@sipa suggested removing the redundancies in #5812, this is what I meant by doing it later, better.
@laanwj celebrated passing Consensus::Params as a parameter in that same PR, so I hope he likes this one too.
@theuni will probably be interested in this one as well since he is working on similar things.