From: paladz <453256728@qq.com> Date: Tue, 10 Oct 2017 02:04:05 +0000 (+0800) Subject: init version for enable mining diff calculate func X-Git-Tag: v1.0.5~466^2~3^2~1 X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=7b2cd807874480986d2fd0ff3f9ab3ece990a576;p=bytom%2Fbytom.git init version for enable mining diff calculate func --- diff --git a/consensus/difficulty.go b/consensus/difficulty.go index f1e2b5fb..86e21fda 100644 --- a/consensus/difficulty.go +++ b/consensus/difficulty.go @@ -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 index 00000000..ea5dbd6a --- /dev/null +++ b/consensus/difficulty_test.go @@ -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) + } + } +} diff --git a/consensus/general.go b/consensus/general.go index 5362e607..c694fb05 100644 --- a/consensus/general.go +++ b/consensus/general.go @@ -16,7 +16,7 @@ const ( // config for pow mining powMinBits = uint64(2161727821138738707) - blocksPerRetarget = uint64(1024) + BlocksPerRetarget = uint64(1024) targetSecondsPerBlock = uint64(60) ) diff --git a/consensus/general_test.go b/consensus/general_test.go index 94089514..dc861c8a 100644 --- a/consensus/general_test.go +++ b/consensus/general_test.go @@ -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 diff --git a/mining/mining.go b/mining/mining.go index 610b3f3c..ecf0a3fa 100644 --- a/mining/mining.go +++ b/mining/mining.go @@ -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)), }