Skip to content

Commit

Permalink
blockchain+chaincfg: disable retargeting for regtest
Browse files Browse the repository at this point in the history
This commit emulates the behavior of Bitcoin Core introduced in
bitcoin/bitcoin#6853 that disables retargeting
of the required proof of work for regtest.
  • Loading branch information
guggero committed May 23, 2023
1 parent 98e3c49 commit cf802a0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
31 changes: 21 additions & 10 deletions blockchain/difficulty.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,21 @@ func HashToBig(hash *chainhash.Hash) *big.Int {
// Like IEEE754 floating point, there are three basic components: the sign,
// the exponent, and the mantissa. They are broken out as follows:
//
// * the most significant 8 bits represent the unsigned base 256 exponent
// * bit 23 (the 24th bit) represents the sign bit
// * the least significant 23 bits represent the mantissa
// - the most significant 8 bits represent the unsigned base 256 exponent
//
// -------------------------------------------------
// | Exponent | Sign | Mantissa |
// -------------------------------------------------
// | 8 bits [31-24] | 1 bit [23] | 23 bits [22-00] |
// -------------------------------------------------
// - bit 23 (the 24th bit) represents the sign bit
//
// - the least significant 23 bits represent the mantissa
//
// -------------------------------------------------
// | Exponent | Sign | Mantissa |
// -------------------------------------------------
// | 8 bits [31-24] | 1 bit [23] | 23 bits [22-00] |
// -------------------------------------------------
//
// The formula to calculate N is:
// N = (-1^sign) * mantissa * 256^(exponent-3)
//
// N = (-1^sign) * mantissa * 256^(exponent-3)
//
// This compact form is only used in bitcoin to encode unsigned 256-bit numbers
// which represent difficulty targets, thus there really is not a need for a
Expand Down Expand Up @@ -218,7 +221,15 @@ func (b *BlockChain) findPrevTestNetDifficulty(startNode *blockNode) uint32 {
// This function differs from the exported CalcNextRequiredDifficulty in that
// the exported version uses the current best chain as the previous block node
// while this function accepts any block node.
func (b *BlockChain) calcNextRequiredDifficulty(lastNode *blockNode, newBlockTime time.Time) (uint32, error) {
func (b *BlockChain) calcNextRequiredDifficulty(lastNode *blockNode,
newBlockTime time.Time) (uint32, error) {

// Emulate the same behavior as Bitcoin Core that for regtest there is
// no difficulty retargeting.
if b.chainParams.PoWNoRetargeting {
return b.chainParams.PowLimitBits, nil
}

// Genesis block.
if lastNode == nil {
return b.chainParams.PowLimitBits, nil
Expand Down
11 changes: 9 additions & 2 deletions chaincfg/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,11 @@ type Params struct {
// block in compact form.
PowLimitBits uint32

// PoWNoRetargeting defines whether the network has difficulty
// retargeting enabled or not. This should only be set to true for
// regtest like networks.
PoWNoRetargeting bool

// These fields define the block heights at which the specified softfork
// BIP became active.
BIP0034Height int32
Expand Down Expand Up @@ -432,6 +437,7 @@ var RegressionNetParams = Params{
GenesisHash: &regTestGenesisHash,
PowLimit: regressionPowLimit,
PowLimitBits: 0x207fffff,
PoWNoRetargeting: true,
CoinbaseMaturity: 100,
BIP0034Height: 100000000, // Not active - Permit ver 1 blocks
BIP0065Height: 1351, // Used by regression tests
Expand Down Expand Up @@ -1007,8 +1013,9 @@ func IsBech32SegwitPrefix(prefix string) bool {
// ErrInvalidHDKeyID error will be returned.
//
// Reference:
// SLIP-0132 : Registered HD version bytes for BIP-0032
// https://github.com/satoshilabs/slips/blob/master/slip-0132.md
//
// SLIP-0132 : Registered HD version bytes for BIP-0032
// https://github.com/satoshilabs/slips/blob/master/slip-0132.md
func RegisterHDKeyID(hdPublicKeyID []byte, hdPrivateKeyID []byte) error {
if len(hdPublicKeyID) != 4 || len(hdPrivateKeyID) != 4 {
return ErrInvalidHDKeyID
Expand Down

0 comments on commit cf802a0

Please sign in to comment.