}
func CheckProofOfWork(hash *bc.Hash, bits uint64) bool {
- if HashToBig(hash).Cmp(CompactToBig(bits)) <= 0 {
- return true
- }
- return false
+ return HashToBig(hash).Cmp(CompactToBig(bits)) <= 0
}
-func CalcNextRequiredDifficulty(lastBH, prevBH *legacy.BlockHeader) uint64 {
- return uint64(2161727821138738707)
-
- //TODO: test it and enable it
+func CalcNextRequiredDifficulty(lastBH, compareBH *legacy.BlockHeader) uint64 {
if lastBH == nil {
return powMinBits
- } else if (lastBH.Height+1)%blocksPerRetarget != 0 {
+ } else if (lastBH.Height+1)%BlocksPerRetarget != 0 {
return lastBH.Bits
}
- actualTimespan := int64(lastBH.Time().Sub(prevBH.Time()).Seconds())
+ targetTimeSpan := int64(BlocksPerRetarget * targetSecondsPerBlock)
+ actualTimespan := int64(lastBH.Time().Sub(compareBH.Time()).Seconds())
+
oldTarget := CompactToBig(lastBH.Bits)
newTarget := new(big.Int).Mul(oldTarget, big.NewInt(actualTimespan))
- targetTimeSpan := int64(blocksPerRetarget * targetSecondsPerBlock)
newTarget.Div(newTarget, big.NewInt(targetTimeSpan))
newTargetBits := BigToCompact(newTarget)
--- /dev/null
+package consensus
+
+import (
+ "math/big"
+ "testing"
+
+ "github.com/bytom/protocol/bc/legacy"
+)
+
+func TestCalcNextRequiredDifficulty(t *testing.T) {
+ targetTimeSpan := uint64(BlocksPerRetarget * targetSecondsPerBlock * 1000)
+ cases := []struct {
+ lastBH *legacy.BlockHeader
+ compareBH *legacy.BlockHeader
+ want uint64
+ }{
+ {nil, nil, powMinBits},
+ {&legacy.BlockHeader{Height: BlocksPerRetarget, Bits: 87654321}, nil, 87654321},
+ {
+ &legacy.BlockHeader{Height: BlocksPerRetarget - 1, TimestampMS: targetTimeSpan, Bits: BigToCompact(big.NewInt(1000))},
+ &legacy.BlockHeader{Height: 0, TimestampMS: 0},
+ BigToCompact(big.NewInt(1000)),
+ },
+ {
+ &legacy.BlockHeader{Height: BlocksPerRetarget - 1, TimestampMS: targetTimeSpan * 2, Bits: BigToCompact(big.NewInt(1000))},
+ &legacy.BlockHeader{Height: 0, TimestampMS: 0},
+ BigToCompact(big.NewInt(2000)),
+ },
+ {
+ &legacy.BlockHeader{Height: BlocksPerRetarget - 1, TimestampMS: targetTimeSpan / 2, Bits: BigToCompact(big.NewInt(1000))},
+ &legacy.BlockHeader{Height: 0, TimestampMS: 0},
+ BigToCompact(big.NewInt(500)),
+ },
+ }
+
+ for i, c := range cases {
+ if got := CalcNextRequiredDifficulty(c.lastBH, c.compareBH); got != c.want {
+ t.Errorf("Compile(%d) = %d want %d", i, got, c.want)
+ }
+ }
+}
// config for pow mining
powMinBits = uint64(2161727821138738707)
- blocksPerRetarget = uint64(1024)
+ BlocksPerRetarget = uint64(1024)
targetSecondsPerBlock = uint64(60)
)
package consensus
-import (
- "fmt"
- "math/big"
- "testing"
-)
-
-func TestCalcNextRequiredDifficulty(t *testing.T) {
- //fmt.Println(CalcNextRequiredDifficulty())
- x := big.NewInt(123)
- y, _ := x.SetString("94847123945178081620347972471576132812524935594538618173381454864040345", 10)
- fmt.Println(BigToCompact(y))
-}
-
/*func TestSubsidy(t *testing.T) {
cases := []struct {
bh *BlockHeader
blockWeight := uint64(0)
txFee := uint64(0)
+ var compareDiffBH *legacy.BlockHeader
+ if compareDiffBlock, err := c.GetBlock(nextBlockHeight - consensus.BlocksPerRetarget); err == nil {
+ compareDiffBH = &compareDiffBlock.BlockHeader
+ }
+
b := &legacy.Block{
BlockHeader: legacy.BlockHeader{
Version: 1,
PreviousBlockHash: preBlock.Hash(),
TimestampMS: bc.Millis(time.Now()),
BlockCommitment: legacy.BlockCommitment{},
- Bits: consensus.CalcNextRequiredDifficulty(nil, nil),
+ Bits: consensus.CalcNextRequiredDifficulty(&preBlock.BlockHeader, compareDiffBH),
},
Transactions: make([]*legacy.Tx, 0, len(txDescs)),
}