Skip to content

Conversation

@jtimon
Copy link
Contributor

@jtimon jtimon commented Apr 10, 2015

Decouple main::GetBlockValue() from Params() and rename it GetBlockSubsidy.
Then remove redundant the getter CChainParams::SubsidyHalvingInterval().

@jgarzik
Copy link
Contributor

jgarzik commented Apr 10, 2015

Looks OK from quick glance. Test plan?

@sipa
Copy link
Member

sipa commented Apr 11, 2015

utACK.

One way to test this would be creating a unit test with the old GetBlockValue() implementation, and calling it for some arbitrary heights/fees, and comparing with the new. It looks trivially correct to me, though.

@jtimon jtimon force-pushed the params_subsidy branch 2 times, most recently from f3a0734 to af6f6bf Compare April 13, 2015 16:19
@jtimon
Copy link
Contributor Author

jtimon commented Apr 13, 2015

Needed rebase.
I also think it is trivial enough: passing Consensus::Params, adding the fees outside of the function and not calculating the initial subsidy if it's going to return 0.

In any case, the current main_tests.cpp (which just tests this function) could be improved, it only checks that the subsidy is always <= 50 * COIN. Probably a dataset with pairs height -> subsidy would be better.

We could also test for other subsidy halving intervals (now that Consensus::Params it's passed, it's trivial to set another Consensus::Params::nSubsidyHalvingInterval in the tests, even without calling Params()).
Does that sound good enough? Suggestions for improving main_tests (sat some point they should be renamed subsidy_tests and moved to src/consensus/tests but not yet)?

@jtimon
Copy link
Contributor Author

jtimon commented Apr 15, 2015

Needed rebase again.
I also added some more tests.
Note that if the constants...

    int maxHalvings = 64;
    CAmount nInitialSubsidy = 50 * COIN;

...were part of Consensus::Params, we could easily adapt my tests to test different values for them.
But I'm not sure it's worth it.
And even less sure if that belongs to this PR, I mean to get rid of CChainParams::SubsidyHalvingInterval() I could have simply added the Consensus::Params parameter to GetBlockValue without removing the nFees parameter...

…renamed GetBlockSubsidy]

Remove redundant getter CChainParams::SubsidyHalvingInterval()
@jtimon
Copy link
Contributor Author

jtimon commented May 15, 2015

Rebased

@laanwj
Copy link
Member

laanwj commented May 16, 2015

I mean to get rid of CChainParams::SubsidyHalvingInterval() I could have simply added the Consensus::Params parameter to GetBlockValue without removing the nFees parameter...

If you had done that, this would have already been merged. I'm a bit wary as this changes the fee computation code and is not simple substitution like the other "Remove redundant getter" pulls.

@jtimon
Copy link
Contributor Author

jtimon commented May 16, 2015

That's fine.
Libconsensus-wise, this is only required for VerifyBlock, which can only come after both VerifyTx and VerifyHeader, so this isn't really blocking anything in the short term. Therefore I prefer to do it in a way that I think is right rather than in the fastest way possible.

This doesn't "change the way fees are computed": fees are computed outside of this function. The only difference is that the subsidy and the fees were added together inside the function and now they will be added outside it. If the previous function had a more descriptive declaration like Camount CalculateSubsidyAndAddPreviouslyCalculatedFeesToIt(nHeight, previouslyCalucaletedFees) I doubt anybody would have complained about replacing CalculateSubsidyAndAddPreviouslyCalculatedFeesToIt(nHeight, nFees) with GetSubsidy(nHeight) + nFees, which is far more readable IMO (and more aligned with general principles about reusability, but let's not go there).

Calculating the initial subsidy to then ignore when halvings >= 64 it's stupid, so I would have also felt bad if I had touched this function without fixing that. I can always remove that tiny optimization if people insist on keep doing the wrong thing.

Maybe I should have done this in 3 commits and only squash them after people have seen this as the obviously equivalent-but-clearer thing I see. If someone else wants to do a version without the optimization and keeping the addition inside the function (because somehow they thing , nFees) is clearer than + nFees), I'm totally fine with it. But it will change exactly 2 lines less (the 2 lines required for the micro-optimization, that is, moving CAmount nSubsidy = 50 * COIN; after ````if halvings >= 64`).
If the creator of the competitor PR likes the micro-optimization but still wants to do the addition inside the function, the new PR will change exactly the same lines.
But I'm sorry, I'm not proposing that alternative myself because I don't like it, my change is free diff-wise and I consider rejecting that change another form of "DoS against development" that IMO makes less sense than asking people to order includes alphabetically (which at least has some minimal advantages rather than minimal disadvantages).

@laanwj
Copy link
Member

laanwj commented May 18, 2015

@jjtimon I didn't intend to argue, or demand that you have to change this code now after all that time, just tried to explain why this one requires more thorough scrutiny than those other mentioned pulls.

That said, the change looks good to me. utACK

@jtimon
Copy link
Contributor Author

jtimon commented May 19, 2015

I understood that and that's why this one got tests.
Re-reading my comment it sounds overly defensive and almost rude, my apologies.

@laanwj laanwj merged commit 935bd0a into bitcoin:master May 19, 2015
laanwj added a commit that referenced this pull request May 19, 2015
935bd0a Chainparams: Refactor: Decouple main::GetBlockValue() from Params() [renamed GetBlockSubsidy] (Jorge Timón)
Copy link

Choose a reason for hiding this comment

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

Nit: You could have added a comment explaining the function AFAIK all main.h functions should include a comment.

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 can still be done as an independent PR. Documentation is good, but there's no reason why it has to be added when a function is touched. For example, should I complete the documentation of all functions touched in #6163 ? Seems orthogonal to me...
Also I don't see how functions declared main.h should be special in that regard.

@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.

5 participants