-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
feat: Add testnet4 chain support #2274
base: master
Are you sure you want to change the base?
feat: Add testnet4 chain support #2274
Conversation
Looks like we have another here: #2275 Neither that one, nor this one, have any of the new difficulty adjustment algorithm changes. |
I know that in #2275 they were working on the difficultly bits. @marcopeereboom I thought you had started on that part, is it in your PR? |
@Roasbeef @jcvernaleo thanks for your inputs. |
@bullet-tooth see the BIP: https://bips.dev/94/ |
Pull Request Test Coverage Report for Build 12318119030Warning: This coverage report may be inaccurate.This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.
Details
💛 - Coveralls |
Hey @Roasbeef, do you have any estimates about reviewing/merging this PR? |
If you clean up the PR into a review friendly commit structure, I'll take a look. |
Adds support of the Bitcoin testnet version 4 chain. Reference: https://bips.dev/94/
c71544f
to
730a170
Compare
Hello, @guggero. Do you have any estimates when you will be able to review the PR. |
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.
Thanks for the update.
Still requires quite a bit of formal cleanup before more review time should be put in.
With "review friendly commit structure" I didn't mean squashing everything into a single commit.
Suggestion for commit order:
- Any comment/typo fixes.
- Any changes to non-Go files (e.g.
.gitignore
) - Add new parameter to
chaincfg
including unit tests. - Add
EnforceBip94
flag to parameters. - Add changes to
blockchain
package. - Add changes to
cmd
package.
@@ -191,12 +191,22 @@ func calcNextRequiredDifficulty(lastNode HeaderCtx, newBlockTime time.Time, | |||
adjustedTimespan = c.MaxRetargetTimespan() | |||
} | |||
|
|||
var oldTarget *big.Int | |||
// Special difficulty rule for Testnet4 |
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.
Please use full sentences, including punctuation, for comments. Also, if a comment isn't at the beginning of a block, it's nice to have an empty line before it to have some vertical breathing room.
Finally, else
is kind of discouraged in Go if it can be written in a different way, as it often leads to less readable code.
So I'd write this the following way:
// Special difficulty rule for Testnet4
oldTarget := CompactToBig(lastNode.Bits())
if c.ChainParams().EnforceBIP94 {
// Here we use the first block of the difficulty period. This way
// the real difficulty is always preserved in the first block as
// it is not allowed to use the min-difficulty exception.
oldTarget = CompactToBig(firstNode.Bits())
}
Of course the assumption here is that CompactToBig(lastNode.Bits())
isn't computationally expensive to wanting to avoid calling it twice in the worst case.
@@ -220,6 +220,8 @@ const ( | |||
// current chain tip. This is not a block validation rule, but is required | |||
// for block proposals submitted via getblocktemplate RPC. | |||
ErrPrevBlockNotBest | |||
|
|||
ErrTimewarpAttack |
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.
nit: missing Godoc comment.
@@ -85,7 +90,7 @@ func ShouldHaveSerializedBlockHeight(header *wire.BlockHeader) bool { | |||
|
|||
// IsCoinBaseTx determines whether or not a transaction is a coinbase. A coinbase | |||
// is a special transaction created by miners that has no inputs. This is | |||
// represented in the block chain by a transaction with a single input that has | |||
// represented in the blockchain by a transaction with a single input that has |
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.
Please revert these. This specifically refer to the chain of blocks, not the package name. IMO "Blockchain" is a marketing term used by people trying to sell you something that isn't Bitcoin.
if c.ChainParams().EnforceBIP94 { | ||
// Check timestamp for the first block of each difficulty adjustment | ||
// interval, except the genesis block. | ||
if blockHeight%c.BlocksPerRetarget() == 0 { | ||
prevBlockTimestamp := time.Unix(prevNode.Timestamp(), 0) | ||
if header.Timestamp.Before(prevBlockTimestamp.Add(-maxTimeWarp)) { | ||
str := "block's timestamp %v is too early on diff adjustment block %v" | ||
str = fmt.Sprintf(str, header.Timestamp, prevBlockTimestamp) | ||
return ruleError(ErrTimewarpAttack, str) | ||
} | ||
} | ||
} | ||
} |
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.
nit: line length. Also, sounds like this whole block could be extracted into a standalone function. Something like checkBip94()
.
@@ -64,6 +64,11 @@ func btcdMain(serverChan chan<- *server) error { | |||
// Show version at startup. | |||
btcdLog.Infof("Version %s", version()) | |||
|
|||
if cfg.TestNet3 { |
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 sure I agree with that whole block.
@@ -79,6 +80,7 @@ func netName(chainParams *chaincfg.Params) string { | |||
switch chainParams.Net { | |||
case wire.TestNet3: | |||
return "testnet" | |||
// TODO do we need to use "testnet" dir for testnet4 as well? |
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.
We should use a distinct directory, otherwise you couldn't run both on the same computer.
@@ -106,7 +106,8 @@ type config struct { | |||
RPCUser string `short:"u" long:"rpcuser" description:"RPC username"` | |||
SimNet bool `long:"simnet" description:"Connect to the simulation test network"` | |||
TLSSkipVerify bool `long:"skipverify" description:"Do not verify tls certificates (not recommended!)"` | |||
TestNet3 bool `long:"testnet" description:"Connect to testnet"` | |||
TestNet3 bool `long:"testnet" description:"Connect to testnet (version 3). Support for testnet3 is deprecated. Consider moving to testnet4 now by using -testnet4"` |
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.
Remove deprecation comment.
@@ -41,7 +41,8 @@ type config struct { | |||
NumCandidates int `short:"n" long:"numcandidates" description:"Max num of checkpoint candidates to show {1-20}"` | |||
RegressionTest bool `long:"regtest" description:"Use the regression test network"` | |||
SimNet bool `long:"simnet" description:"Use the simulation test network"` | |||
TestNet3 bool `long:"testnet" description:"Use the test network"` | |||
TestNet3 bool `long:"testnet" description:"Use the test network (version 3). Support for testnet3 is deprecated. Consider moving to testnet4 now by using -testnet4"` |
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.
Same here re deprecation.
@@ -169,7 +169,8 @@ type config struct { | |||
SigNet bool `long:"signet" description:"Use the signet test network"` | |||
SigNetChallenge string `long:"signetchallenge" description:"Connect to a custom signet network defined by this challenge instead of using the global default signet test network -- Can be specified multiple times"` | |||
SigNetSeedNode []string `long:"signetseednode" description:"Specify a seed node for the signet network instead of using the global default signet network seed nodes"` | |||
TestNet3 bool `long:"testnet" description:"Use the test network"` | |||
TestNet3 bool `long:"testnet" description:"Use the test network (version 3). Support for testnet3 is deprecated. Consider moving to testnet4 now by using -testnet4"` |
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.
And here.
@@ -2377,6 +2377,7 @@ func newPeerBase(origCfg *Config, inbound bool) *Peer { | |||
|
|||
// Set the chain parameters to testnet if the caller did not specify any. | |||
if cfg.ChainParams == nil { | |||
// TODO: change to testnet4 because testnet3 will be deprecated soon |
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.
Disagree.
This PR adds support of the Bitcoin testnet version 4 chain (testnet3 is in a way of deprecation).
Reference: https://bips.dev/94/
Testnet4 genesis block: https://mempool.space/testnet4/block/00000000da84f2bafbbc53dee25a72ae507ff4914b867c565be350b0da8bf043
Also, I have intention to add testnet4 support to btcwallet after this PR