Add difficulty adjustment calculation#2740
Conversation
Pull Request Test Coverage Report for Build 9089264878Details
💛 - Coveralls |
|
cc #1820 which added some different-but-related missing functionality |
|
In 206bc621d34f0ef25b5d91a512d2e67e635f77e4: There is trailing whitespace on several lines that I'd like you to remove. You can find them with And could you add a couple unit tests, maybe by taking headers from the actual blockchain, that demonstrate that the adjustment does the right thing under normal circumstances, when it hits the "minimum 1 difficulty" rule, and when it hits one of the factor-of-4 rails? (The latter has never happened on Bitcoin but it should be easy to gin up a test vector by editing block timestamps, since we're not validating the PoW here.) |
|
But concept ACK -- this functionality is missing AFAICT. Thanks for your contribution! |
206bc62 to
ed5574f
Compare
|
I approach NACKed myself over the weekend. After thinking about it I decided that the Bitcoin Core approach is less susceptible to user misinterpretation. Also when considering the difficulty adjustment it is more appropriate to be dealing with |
tcharding
left a comment
There was a problem hiding this comment.
Thanks for working on this man! I was no where near getting back to #2180 and I really wanted to deliver this functionality in the next release, so thank you.
My review as mostly styleistic but includes a question about the function argument choice - that is the most important thing in the review I believe.
Thanks again
|
I couldn't manage to get a link to a line number for some reason so I did this instead: This is what I meant by my question about the |
|
@tcharding I don't understand exactly what you're suggesting. Whether the user passes a header or It seems like our choices are either (a) take the whole blockchain, or at least the most recent few thousand blocks, and compute the block intervals ourselves, protecting the user, or (b) take just the two relevant blocks, trusting the user to choose the right ones. This PR currently has both (though maybe they could be named more clearly to distinguish them), and I think we need both, since we don't want to require the user to have tons of blockheaders available when they're not even relevant. |
Mine is just a question on API design and whether we want/need the additional functionality In #2180 the user passes current header to FTR #2180 suffers the same issue I'm describing because both |
|
Ok, yes, you can encapsulate this by requiring the user provide an accessor for every single block in the chain. (Though you can cheat by knowing exactly which blocks it is going to access, of course.) This PR does this, but also provides a method in which you only need to have the two relevant headers. So I think it's strictly better. |
|
ACK ed5574f5b511ca803a9fb041d820631e7ae57158 except that we should restore the "just pass the whole chain" functionality that was present in the original PR. |
Okay, back to two |
|
I think you want something like |
Yeah, I think we should have both. |
ed5574f to
4114394
Compare
|
In 0aa279aa33791dcc462f90e1574bd8d443871fbb:
|
|
With the compute from all the headers approach, I've only come up with two solutions, both concerned with chain height. The only height-implicit approach I know of is to pass a |
|
I'm leaning towards thinking this is mergable as is and we do the additional functionality in a separate PR. This PR is already a big improvement. |
You can take a closure which takes a usize and returns the header at that index. We have an existing PoW-related function that does this, I forget which one.
Sure, I'm happy to do this -- but we're a little ways from release and we're close to having this PR having the API we want. It's up to you @rustaceanrob. If you want me to just merge this I'll do it. |
6536b5e to
54b5456
Compare
Updated to included the specific commit
I think this is an appropriate stopping point for now. Users are able to perform the adjustment and any additions would be additive. I am unsure how much time I will have to work on this in the next couple days. I am envisioning something similar to #2180 where there is a nice |
apoelstra
left a comment
There was a problem hiding this comment.
ACK 54b5456785568f29bc71971bf3cbd7fee7663edb
tcharding
left a comment
There was a problem hiding this comment.
Excluding the docs typo
ACK 54b5456785568f29bc71971bf3cbd7fee7663edb
54b5456 to
47dc4a3
Compare
|
|
Bollocks :) |
|
@apoelstra @tcharding how hard would it be to backport this? I want to get rid of my out-of-tree version of bitcoin in floresta and this would be very useful to have on 0.32 |
|
I think it'd be super straightforward. @tcharding can you open a PR? I suspect you can just cherry-pick this commit. |
|
Thanks @apoelstra . If @tcharding can't do it I can give it a shot. |
|
@Davidson-Souza that would be great actually, since then both of us can ACK it and merge it quickly. |
|
Hey @apoelstra I see you added a new label, we have three backport labels already that are version specific. When I added those (eg |
|
@tcharding oh, good to know, thanks. I will rename them so they have "backport" in the name and drop mine. |
6624b94 feat(pow): add difficulty adjustment calculation (Rob N) Pull request description: As discussed in #2740 (comment) this is a backport of #2740 to 0.32.x. I've simply cherry-picked the commit and all tests are passing locally. ACKs for top commit: tcharding: ACK 6624b94 apoelstra: ACK 6624b94; successfully ran local tests Tree-SHA512: 27c122a051b590cdae66557044fcd72c417cf5d4ffc2bbf2d334b06f8f773038990168c116de3a026b08de6eb833d049eb510b58bb179dfd5add2245b37450a3
|
Backported in #3494 |
Hi, I hit a roadblock with the current
powAPI. As far as I can tell, the only workaround to calculate the next work required similar tobitcoin/src/pow.cppis to use a general big integer library, convert theTargetto bytes, do the math, and convert back toTargetfrom bytes. I have also been working with Floresta and their solution was to fork off and exposed theU256struct publicly on their branch. I think these home brewed difficulty adjustment solutions will continually pop up, so I created afrom_next_work_requiredmethod to return aTarget. My work veers significantly from #2180, as I only provided a single method to do so, without further guidance on when exactly this retarget occurs.I am happy to add tests once I get further direction from maintainers if this as a likelihood of being accepted or not. Thanks.