OSDN Git Service

init version for enable mining diff calculate func
authorpaladz <453256728@qq.com>
Tue, 10 Oct 2017 02:04:05 +0000 (10:04 +0800)
committerpaladz <453256728@qq.com>
Tue, 10 Oct 2017 02:04:05 +0000 (10:04 +0800)
consensus/difficulty.go
consensus/difficulty_test.go [new file with mode: 0644]
consensus/general.go
consensus/general_test.go
mining/mining.go

index f1e2b5f..86e21fd 100644 (file)
@@ -82,26 +82,21 @@ func BigToCompact(n *big.Int) uint64 {
 }
 
 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)
 
diff --git a/consensus/difficulty_test.go b/consensus/difficulty_test.go
new file mode 100644 (file)
index 0000000..ea5dbd6
--- /dev/null
@@ -0,0 +1,41 @@
+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)
+               }
+       }
+}
index 5362e60..c694fb0 100644 (file)
@@ -16,7 +16,7 @@ const (
 
        // config for pow mining
        powMinBits            = uint64(2161727821138738707)
-       blocksPerRetarget     = uint64(1024)
+       BlocksPerRetarget     = uint64(1024)
        targetSecondsPerBlock = uint64(60)
 )
 
index 9408951..dc861c8 100644 (file)
@@ -1,18 +1,5 @@
 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
index 610b3f3..ecf0a3f 100644 (file)
@@ -64,6 +64,11 @@ func NewBlockTemplate(c *protocol.Chain, txPool *protocol.TxPool, addr []byte) (
        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,
@@ -71,7 +76,7 @@ func NewBlockTemplate(c *protocol.Chain, txPool *protocol.TxPool, addr []byte) (
                        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)),
        }