OSDN Git Service

skip compute nonce in tests
authorlbqds <lbqds@outlook.com>
Sat, 14 Apr 2018 06:01:00 +0000 (14:01 +0800)
committerlbqds <lbqds@outlook.com>
Sat, 14 Apr 2018 06:01:00 +0000 (14:01 +0800)
test/block_test.go
test/block_test_util.go
test/util.go

index 6d1d31c..3953c96 100644 (file)
@@ -38,7 +38,7 @@ func TestBlockHeader(t *testing.T) {
                        desc:       "block version is 0",
                        version:    func() uint64 { return 0 },
                        prevHeight: chain.BestBlockHeight,
-                       timestamp:  func() uint64 { return uint64(time.Now().Unix()) },
+                       timestamp:  func() uint64 { return chain.BestBlockHeader().Timestamp + 1 },
                        prevHash:   chain.BestBlockHash,
                        bits:       func() uint64 { return chain.BestBlockHeader().Bits },
                        solve:      true,
@@ -48,7 +48,7 @@ func TestBlockHeader(t *testing.T) {
                        desc:       "block version grater than prevBlock.Version",
                        version:    func() uint64 { return chain.BestBlockHeader().Version + 10 },
                        prevHeight: chain.BestBlockHeight,
-                       timestamp:  func() uint64 { return uint64(time.Now().Unix()) },
+                       timestamp:  func() uint64 { return chain.BestBlockHeader().Timestamp + 1 },
                        prevHash:   chain.BestBlockHash,
                        bits:       func() uint64 { return chain.BestBlockHeader().Bits },
                        solve:      true,
@@ -58,7 +58,7 @@ func TestBlockHeader(t *testing.T) {
                        desc:       "invalid block, misorder block height",
                        version:    func() uint64 { return chain.BestBlockHeader().Version },
                        prevHeight: func() uint64 { return chain.BestBlockHeight() + 1 },
-                       timestamp:  func() uint64 { return uint64(time.Now().Unix()) },
+                       timestamp:  func() uint64 { return chain.BestBlockHeader().Timestamp + 1 },
                        prevHash:   chain.BestBlockHash,
                        bits:       func() uint64 { return chain.BestBlockHeader().Bits },
                        solve:      true,
@@ -68,7 +68,7 @@ func TestBlockHeader(t *testing.T) {
                        desc:       "invalid prev hash, prev hash dismatch",
                        version:    func() uint64 { return chain.BestBlockHeader().Version },
                        prevHeight: chain.BestBlockHeight,
-                       timestamp:  func() uint64 { return uint64(time.Now().Unix()) },
+                       timestamp:  func() uint64 { return chain.BestBlockHeader().Timestamp + 1 },
                        prevHash:   func() *bc.Hash { hash := genesisHeader.Hash(); return &hash },
                        bits:       func() uint64 { return chain.BestBlockHeader().Bits },
                        solve:      true,
@@ -78,7 +78,7 @@ func TestBlockHeader(t *testing.T) {
                        desc:       "invalid bits",
                        version:    func() uint64 { return chain.BestBlockHeader().Version },
                        prevHeight: chain.BestBlockHeight,
-                       timestamp:  func() uint64 { return uint64(time.Now().Unix()) },
+                       timestamp:  func() uint64 { return chain.BestBlockHeader().Timestamp + 1 },
                        prevHash:   chain.BestBlockHash,
                        bits:       func() uint64 { return chain.BestBlockHeader().Bits + 100 },
                        solve:      true,
index 0c839a8..acd8d26 100644 (file)
@@ -1,10 +1,9 @@
 package test
 
 import (
-       "time"
-
        "github.com/bytom/consensus"
        "github.com/bytom/consensus/difficulty"
+       "github.com/bytom/mining/tensority"
        "github.com/bytom/protocol"
        "github.com/bytom/protocol/bc"
        "github.com/bytom/protocol/bc/types"
@@ -31,7 +30,7 @@ func NewBlock(chain *protocol.Chain, txs []*types.Tx, controlProgram []byte) (*t
                        Version:           1,
                        Height:            preBlockHeader.Height + 1,
                        PreviousBlockHash: preBlockHeader.Hash(),
-                       Timestamp:         uint64(time.Now().Unix()),
+                       Timestamp:         preBlockHeader.Timestamp + 1,
                        BlockCommitment:   types.BlockCommitment{},
                        Bits:              difficulty.CalcNextRequiredDifficulty(preBlockHeader, compareDiffBH),
                },
@@ -109,15 +108,8 @@ func SolveAndUpdate(chain *protocol.Chain, block *types.Block) error {
        return err
 }
 
-// Solve solve difficulty
-func Solve(seed *bc.Hash, block *types.Block) error {
-       header := &block.BlockHeader
-       for i := uint64(0); i < maxNonce; i++ {
-               header.Nonce = i
-               headerHash := header.Hash()
-               if difficulty.CheckProofOfWork(&headerHash, seed, header.Bits) {
-                       return nil
-               }
-       }
-       return nil
+// Solve simulate solve difficulty by add result to cache
+func Solve(seed *bc.Hash, block *types.Block) {
+       hash := block.BlockHeader.Hash()
+       tensority.AIHash.AddCache(&hash, seed, &bc.Hash{})
 }
index d2036ba..94daf03 100644 (file)
@@ -20,9 +20,7 @@ import (
 
 const (
        vmVersion    = 1
-       blockVersion = 1
        assetVersion = 1
-       maxNonce     = ^uint64(0)
 )
 
 // MockTxPool mock transaction pool