OSDN Git Service

add edge test for TestCalcNextRequiredDifficulty (#1639)
authoroysheng <33340252+oysheng@users.noreply.github.com>
Tue, 19 Mar 2019 05:57:08 +0000 (13:57 +0800)
committerPaladz <yzhu101@uottawa.ca>
Tue, 19 Mar 2019 05:57:08 +0000 (13:57 +0800)
* add edge test for TestCalcNextRequiredDifficulty

* optimise

* adjust code style

* optimise

consensus/difficulty/difficulty_test.go

index 0f0f4d2..4ed55c0 100644 (file)
@@ -23,80 +23,304 @@ func TestCalcNextRequiredDifficulty(t *testing.T) {
                        &types.BlockHeader{
                                Height:    consensus.BlocksPerRetarget,
                                Timestamp: targetTimeSpan,
-                               Bits:      BigToCompact(big.NewInt(1000))},
+                               Bits:      BigToCompact(big.NewInt(1000)),
+                       },
                        &types.BlockHeader{
                                Height:    0,
-                               Timestamp: 0},
+                               Timestamp: 0,
+                       },
                        BigToCompact(big.NewInt(1000)),
                },
                {
                        &types.BlockHeader{
                                Height:    consensus.BlocksPerRetarget,
                                Timestamp: targetTimeSpan * 2,
-                               Bits:      BigToCompact(big.NewInt(1000))},
+                               Bits:      BigToCompact(big.NewInt(1000)),
+                       },
                        &types.BlockHeader{
                                Height:    0,
-                               Timestamp: 0},
+                               Timestamp: 0,
+                       },
                        BigToCompact(big.NewInt(2000)),
                },
                {
                        &types.BlockHeader{
                                Height:    consensus.BlocksPerRetarget - 1,
                                Timestamp: targetTimeSpan*2 - consensus.TargetSecondsPerBlock,
-                               Bits:      BigToCompact(big.NewInt(1000))},
+                               Bits:      BigToCompact(big.NewInt(1000)),
+                       },
                        &types.BlockHeader{
                                Height:    0,
-                               Timestamp: 0},
+                               Timestamp: 0,
+                       },
                        BigToCompact(big.NewInt(1000)),
                },
                {
                        &types.BlockHeader{
                                Height:    consensus.BlocksPerRetarget,
                                Timestamp: targetTimeSpan / 2,
-                               Bits:      BigToCompact(big.NewInt(1000))},
+                               Bits:      BigToCompact(big.NewInt(1000)),
+                       },
                        &types.BlockHeader{
                                Height:    0,
-                               Timestamp: 0},
+                               Timestamp: 0,
+                       },
                        BigToCompact(big.NewInt(500)),
                },
                {
                        &types.BlockHeader{
                                Height:    consensus.BlocksPerRetarget * 2,
                                Timestamp: targetTimeSpan + targetTimeSpan*2,
-                               Bits:      BigToCompact(big.NewInt(1000))},
+                               Bits:      BigToCompact(big.NewInt(1000)),
+                       },
                        &types.BlockHeader{
                                Height:    consensus.BlocksPerRetarget,
-                               Timestamp: targetTimeSpan},
+                               Timestamp: targetTimeSpan,
+                       },
                        BigToCompact(big.NewInt(2000)),
                },
                {
                        &types.BlockHeader{
                                Height:    consensus.BlocksPerRetarget * 2,
                                Timestamp: targetTimeSpan + targetTimeSpan/2,
-                               Bits:      BigToCompact(big.NewInt(1000))},
+                               Bits:      BigToCompact(big.NewInt(1000)),
+                       },
                        &types.BlockHeader{
                                Height:    consensus.BlocksPerRetarget,
-                               Timestamp: targetTimeSpan},
+                               Timestamp: targetTimeSpan,
+                       },
                        BigToCompact(big.NewInt(500)),
                },
                {
                        &types.BlockHeader{
                                Height:    consensus.BlocksPerRetarget*2 - 1,
                                Timestamp: targetTimeSpan + targetTimeSpan*2 - consensus.TargetSecondsPerBlock,
-                               Bits:      BigToCompact(big.NewInt(1000))},
+                               Bits:      BigToCompact(big.NewInt(1000)),
+                       },
                        &types.BlockHeader{
                                Height:    consensus.BlocksPerRetarget,
-                               Timestamp: targetTimeSpan},
+                               Timestamp: targetTimeSpan,
+                       },
                        BigToCompact(big.NewInt(1000)),
                },
                {
                        &types.BlockHeader{
                                Height:    consensus.BlocksPerRetarget*2 - 1,
                                Timestamp: targetTimeSpan + targetTimeSpan/2 - consensus.TargetSecondsPerBlock,
-                               Bits:      BigToCompact(big.NewInt(1000))},
+                               Bits:      BigToCompact(big.NewInt(1000)),
+                       },
+                       &types.BlockHeader{
+                               Height:    consensus.BlocksPerRetarget,
+                               Timestamp: targetTimeSpan,
+                       },
+                       BigToCompact(big.NewInt(1000)),
+               },
+               // lastBH.Height: 0, lastBH.Timestamp - compareBH.Timestamp: 0, lastBH.Bits: 0
+               {
+                       &types.BlockHeader{
+                               Height:    0,
+                               Timestamp: 0,
+                               Bits:      0,
+                       },
+                       &types.BlockHeader{
+                               Height:    0,
+                               Timestamp: 0,
+                       },
+                       0,
+               },
+               // lastBH.Height: 0, lastBH.Timestamp - compareBH.Timestamp: 0, lastBH.Bits: 18446744073709551615
+               {
+                       &types.BlockHeader{
+                               Height:    0,
+                               Timestamp: 0,
+                               Bits:      18446744073709551615,
+                       },
+                       &types.BlockHeader{
+                               Height:    0,
+                               Timestamp: 0,
+                       },
+                       18446744073709551615,
+               },
+               // lastBH.Height: 0, lastBH.Timestamp - compareBH.Timestamp: 0, lastBH.Bits: bigInt(1000)
+               {
+                       &types.BlockHeader{
+                               Height:    0,
+                               Timestamp: 0,
+                               Bits:      BigToCompact(big.NewInt(1000)),
+                       },
+                       &types.BlockHeader{
+                               Height:    0,
+                               Timestamp: 0,
+                       },
+                       BigToCompact(big.NewInt(1000)),
+               },
+               // lastBH.Height: consensus.BlocksPerRetarget, lastBH.Timestamp - compareBH.Timestamp: 0, lastBH.Bits: bigInt(1000)
+               {
+                       &types.BlockHeader{
+                               Height:    consensus.BlocksPerRetarget,
+                               Timestamp: targetTimeSpan,
+                               Bits:      BigToCompact(big.NewInt(1000)),
+                       },
+                       &types.BlockHeader{
+                               Height:    consensus.BlocksPerRetarget - 1,
+                               Timestamp: targetTimeSpan,
+                       },
+                       0,
+               },
+               // lastBH.Height: consensus.BlocksPerRetarget, lastBH.Timestamp - compareBH.Timestamp: -9223372036854775808, lastBH.Bits: bigInt(1000)
+               {
+                       &types.BlockHeader{
+                               Height:    consensus.BlocksPerRetarget,
+                               Timestamp: targetTimeSpan,
+                               Bits:      BigToCompact(big.NewInt(1000)),
+                       },
+                       &types.BlockHeader{
+                               Height:    consensus.BlocksPerRetarget - 1,
+                               Timestamp: targetTimeSpan + 9223372036854775808,
+                       },
+                       540431955291560988,
+               },
+               // lastBH.Height: consensus.BlocksPerRetarget, lastBH.Timestamp - compareBH.Timestamp: 9223372036854775807, lastBH.Bits: bigInt(1000)
+               {
+                       &types.BlockHeader{
+                               Height:    consensus.BlocksPerRetarget,
+                               Timestamp: targetTimeSpan + 9223372036854775807,
+                               Bits:      BigToCompact(big.NewInt(1000)),
+                       },
+                       &types.BlockHeader{
+                               Height:    consensus.BlocksPerRetarget - 1,
+                               Timestamp: targetTimeSpan,
+                       },
+                       504403158272597019,
+               },
+               // lastBH.Height: consensus.BlocksPerRetarget, lastBH.Timestamp - compareBH.Timestamp: 18446744073709551615, lastBH.Bits: bigInt(1000)
+               {
+                       &types.BlockHeader{
+                               Height:    consensus.BlocksPerRetarget,
+                               Timestamp: 18446744073709551615,
+                               Bits:      BigToCompact(big.NewInt(1000)),
+                       },
+                       &types.BlockHeader{
+                               Height:    consensus.BlocksPerRetarget - 1,
+                               Timestamp: 0,
+                       },
+                       108086391056957440,
+               },
+               // lastBH.Height: consensus.BlocksPerRetarget, lastBH.Timestamp - compareBH.Timestamp: 302400, lastBH.Bits: bigInt(1000)
+               {
+                       &types.BlockHeader{
+                               Height:    consensus.BlocksPerRetarget,
+                               Timestamp: targetTimeSpan * 2,
+                               Bits:      BigToCompact(big.NewInt(1000)),
+                       },
+                       &types.BlockHeader{
+                               Height:    consensus.BlocksPerRetarget - 1,
+                               Timestamp: targetTimeSpan,
+                       },
+                       BigToCompact(big.NewInt(1000)),
+               },
+               // lastBH.Height: consensus.BlocksPerRetarget, lastBH.Timestamp - compareBH.Timestamp: 604800, lastBH.Bits: bigInt(1000)
+               {
                        &types.BlockHeader{
                                Height:    consensus.BlocksPerRetarget,
-                               Timestamp: targetTimeSpan},
+                               Timestamp: targetTimeSpan * 3,
+                               Bits:      BigToCompact(big.NewInt(1000)),
+                       },
+                       &types.BlockHeader{
+                               Height:    consensus.BlocksPerRetarget - 1,
+                               Timestamp: targetTimeSpan,
+                       },
+                       144115188076367872,
+               },
+               // lastBH.Height: consensus.BlocksPerRetarget, lastBH.Timestamp - compareBH.Timestamp: 151200, lastBH.Bits: bigInt(1000)
+               {
+                       &types.BlockHeader{
+                               Height:    consensus.BlocksPerRetarget,
+                               Timestamp: targetTimeSpan + 9223372036854775807,
+                               Bits:      BigToCompact(big.NewInt(1000)),
+                       },
+                       &types.BlockHeader{
+                               Height:    consensus.BlocksPerRetarget - 1,
+                               Timestamp: targetTimeSpan,
+                       },
+                       504403158272597019,
+               },
+               // lastBH.Height: consensus.BlocksPerRetarget, lastBH.Timestamp - compareBH.Timestamp: 302400, lastBH.Bits: 0
+               {
+                       &types.BlockHeader{
+                               Height:    consensus.BlocksPerRetarget,
+                               Timestamp: targetTimeSpan * 2,
+                               Bits:      0,
+                       },
+                       &types.BlockHeader{
+                               Height:    consensus.BlocksPerRetarget - 1,
+                               Timestamp: targetTimeSpan,
+                       },
+                       0,
+               },
+               // lastBH.Height: consensus.BlocksPerRetarget, lastBH.Timestamp - compareBH.Timestamp: 302400, lastBH.Bits: 18446744073709551615
+               {
+                       &types.BlockHeader{
+                               Height:    consensus.BlocksPerRetarget,
+                               Timestamp: targetTimeSpan * 2,
+                               Bits:      18446744073709551615,
+                       },
+                       &types.BlockHeader{
+                               Height:    consensus.BlocksPerRetarget - 1,
+                               Timestamp: targetTimeSpan,
+                       },
+                       252201579141136384,
+               },
+               // lastBH.Height: consensus.BlocksPerRetarget + 1, lastBH.Timestamp - compareBH.Timestamp: 302400, lastBH.Bits: bigInt(1000)
+               {
+                       &types.BlockHeader{
+                               Height:    consensus.BlocksPerRetarget + 1,
+                               Timestamp: targetTimeSpan * 2,
+                               Bits:      BigToCompact(big.NewInt(1000)),
+                       },
+                       &types.BlockHeader{
+                               Height:    consensus.BlocksPerRetarget,
+                               Timestamp: targetTimeSpan,
+                       },
+                       BigToCompact(big.NewInt(1000)),
+               },
+               // lastBH.Height: consensus.BlocksPerRetarget - 1, lastBH.Timestamp - compareBH.Timestamp: 302400, lastBH.Bits: bigInt(1000)
+               {
+                       &types.BlockHeader{
+                               Height:    consensus.BlocksPerRetarget - 1,
+                               Timestamp: targetTimeSpan * 2,
+                               Bits:      BigToCompact(big.NewInt(1000)),
+                       },
+                       &types.BlockHeader{
+                               Height:    consensus.BlocksPerRetarget - 2,
+                               Timestamp: targetTimeSpan,
+                       },
+                       BigToCompact(big.NewInt(1000)),
+               },
+               // lastBH.Height: consensus.BlocksPerRetarget * 2, lastBH.Timestamp - compareBH.Timestamp: 302400, lastBH.Bits: bigInt(1000)
+               {
+                       &types.BlockHeader{
+                               Height:    consensus.BlocksPerRetarget * 2,
+                               Timestamp: targetTimeSpan * 2,
+                               Bits:      BigToCompact(big.NewInt(1000)),
+                       },
+                       &types.BlockHeader{
+                               Height:    consensus.BlocksPerRetarget*2 - 1,
+                               Timestamp: targetTimeSpan,
+                       },
+                       BigToCompact(big.NewInt(1000)),
+               },
+               // lastBH.Height: consensus.BlocksPerRetarget / 2, lastBH.Timestamp - compareBH.Timestamp: 302400, lastBH.Bits: bigInt(1000)
+               {
+                       &types.BlockHeader{
+                               Height:    consensus.BlocksPerRetarget / 2,
+                               Timestamp: targetTimeSpan * 2,
+                               Bits:      BigToCompact(big.NewInt(1000)),
+                       },
+                       &types.BlockHeader{
+                               Height:    consensus.BlocksPerRetarget/2 - 1,
+                               Timestamp: targetTimeSpan,
+                       },
                        BigToCompact(big.NewInt(1000)),
                },
        }