Optimize proof of work limit bits handling.
Rather than converting the proof of work limit to its compact representation multiple times during operation, do it once at package initialization time and export it via the chain parameters.
This commit is contained in:
parent
cef901ebad
commit
1e891b4e0b
2 changed files with 29 additions and 16 deletions
|
@ -189,6 +189,7 @@ func (b *BlockChain) calcEasiestDifficulty(bits uint32, duration time.Duration)
|
|||
|
||||
// Choose the correct proof of work limit for the active network.
|
||||
powLimit := b.chainParams().PowLimit
|
||||
powLimitBits := b.chainParams().PowLimitBits
|
||||
|
||||
// The test network rules allow minimum difficulty blocks after more
|
||||
// than twice the desired amount of time needed to generate a block has
|
||||
|
@ -198,7 +199,7 @@ func (b *BlockChain) calcEasiestDifficulty(bits uint32, duration time.Duration)
|
|||
fallthrough
|
||||
case btcwire.TestNet3:
|
||||
if durationVal > int64(targetSpacing)*2 {
|
||||
return BigToCompact(powLimit)
|
||||
return powLimitBits
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -225,7 +226,7 @@ func (b *BlockChain) calcEasiestDifficulty(bits uint32, duration time.Duration)
|
|||
func (b *BlockChain) findPrevTestNetDifficulty(startNode *blockNode) (uint32, error) {
|
||||
// Search backwards through the chain for the last block without
|
||||
// the special rule applied.
|
||||
powLimitBits := BigToCompact(b.chainParams().PowLimit)
|
||||
powLimitBits := b.chainParams().PowLimitBits
|
||||
iterNode := startNode
|
||||
for iterNode != nil && iterNode.height%blocksPerRetarget != 0 && iterNode.bits == powLimitBits {
|
||||
// Get the previous block node. This function is used over
|
||||
|
@ -255,10 +256,11 @@ func (b *BlockChain) findPrevTestNetDifficulty(startNode *blockNode) (uint32, er
|
|||
func (b *BlockChain) calcNextRequiredDifficulty(lastNode *blockNode, block *btcutil.Block) (uint32, error) {
|
||||
// Choose the correct proof of work limit for the active network.
|
||||
powLimit := b.chainParams().PowLimit
|
||||
powLimitBits := b.chainParams().PowLimitBits
|
||||
|
||||
// Genesis block.
|
||||
if lastNode == nil {
|
||||
return BigToCompact(powLimit), nil
|
||||
return powLimitBits, nil
|
||||
}
|
||||
|
||||
// Return the previous block's difficulty requirements if this block
|
||||
|
@ -278,7 +280,7 @@ func (b *BlockChain) calcNextRequiredDifficulty(lastNode *blockNode, block *btcu
|
|||
newBlockTime := block.MsgBlock().Header.Timestamp
|
||||
allowMinTime := lastNode.timestamp.Add(targetSpacing * 2)
|
||||
if newBlockTime.After(allowMinTime) {
|
||||
return BigToCompact(powLimit), nil
|
||||
return powLimitBits, nil
|
||||
}
|
||||
|
||||
// The block was mined within the desired timeframe, so
|
||||
|
|
35
params.go
35
params.go
|
@ -21,39 +21,50 @@ type Params struct {
|
|||
// PowLimit is the highest proof of work value a bitcoin block can have
|
||||
// for the specific network.
|
||||
PowLimit *big.Int
|
||||
|
||||
// PowLimitBits is the highest proof of work value a bitcoin block can
|
||||
// have represented in compact form. See CompactToBig for more details
|
||||
// on compact form.
|
||||
PowLimitBits uint32
|
||||
}
|
||||
|
||||
// mainPowLimit is the highest proof of work value a bitcoin block can have for
|
||||
// the main network. It is the value 2^224 - 1.
|
||||
var mainPowLimit = new(big.Int).Sub(new(big.Int).Lsh(bigOne, 224), bigOne)
|
||||
|
||||
// mainNetParams contains parameters specific to the main network
|
||||
// (btcwire.MainNet).
|
||||
var mainNetParams = Params{
|
||||
GenesisBlock: &btcwire.GenesisBlock,
|
||||
GenesisHash: &btcwire.GenesisHash,
|
||||
|
||||
// PowLimit is the highest proof of work value a bitcoin block can have.
|
||||
// It is the value 2^224 - 1 for the main network.
|
||||
PowLimit: new(big.Int).Sub(new(big.Int).Lsh(bigOne, 224), bigOne),
|
||||
PowLimit: mainPowLimit,
|
||||
PowLimitBits: BigToCompact(mainPowLimit),
|
||||
}
|
||||
|
||||
// regressionPowLimit is the highest proof of work value a bitcoin block can
|
||||
// have. It is the value 2^256 - 1.
|
||||
var regressionPowLimit = new(big.Int).Sub(new(big.Int).Lsh(bigOne, 255), bigOne)
|
||||
|
||||
// regressionParams contains parameters specific to the regression test network
|
||||
// (btcwire.TestNet).
|
||||
var regressionParams = Params{
|
||||
GenesisBlock: &btcwire.TestNetGenesisBlock,
|
||||
GenesisHash: &btcwire.TestNetGenesisHash,
|
||||
|
||||
// PowLimit is the highest proof of work value a bitcoin block can have.
|
||||
// It is the value 2^256 - 1 for the regression test network.
|
||||
PowLimit: new(big.Int).Sub(new(big.Int).Lsh(bigOne, 255), bigOne),
|
||||
PowLimit: regressionPowLimit,
|
||||
PowLimitBits: BigToCompact(regressionPowLimit),
|
||||
}
|
||||
|
||||
// testNetPowLimit is the highest proof of work value a bitcoin block can have
|
||||
// for the test network (version 3). It is the value 2^224 - 1.
|
||||
var testNetPowLimit = new(big.Int).Sub(new(big.Int).Lsh(bigOne, 224), bigOne)
|
||||
|
||||
// testNet3Params contains parameters specific to the test network (version 3)
|
||||
// (btcwire.TestNet3).
|
||||
var testNet3Params = Params{
|
||||
GenesisBlock: &btcwire.TestNet3GenesisBlock,
|
||||
GenesisHash: &btcwire.TestNet3GenesisHash,
|
||||
|
||||
// PowLimit is the highest proof of work value a bitcoin block can have.
|
||||
// It is the value 2^224 - 1 for the test network (version 3).
|
||||
PowLimit: new(big.Int).Sub(new(big.Int).Lsh(bigOne, 224), bigOne),
|
||||
PowLimit: testNetPowLimit,
|
||||
PowLimitBits: BigToCompact(testNetPowLimit),
|
||||
}
|
||||
|
||||
// chainParams returns chain parameters specific to the bitcoin network
|
||||
|
|
Loading…
Add table
Reference in a new issue