OSDN Git Service

add mac function, modify nextBlockTime
authorChengcheng Zhang <943420582@qq.com>
Mon, 3 Jun 2019 13:24:33 +0000 (21:24 +0800)
committerChengcheng Zhang <943420582@qq.com>
Mon, 3 Jun 2019 13:24:33 +0000 (21:24 +0800)
consensus/general.go
proposal/blockproposer/blockproposer.go

index 910bce4..34a93d3 100644 (file)
@@ -32,7 +32,7 @@ const (
        BlockTimeInterval = 500
 
        // MaxTimeOffsetMs is the maximum number of seconds a block time is allowed to be ahead of the current time
-       MaxTimeOffsetMs  = uint64(60 * 60 * 1000)
+       MaxTimeOffsetMs  = uint64(7 * 1000)
        MedianTimeBlocks = 11
 
        PayToWitnessPubKeyHashDataSize = 20
index cd84e70..06eac2d 100644 (file)
@@ -52,10 +52,17 @@ func (b *BlockProposer) generateBlocks() {
 
                bestBlockHeader := b.chain.BestBlockHeader()
                bestBlockHash := bestBlockHeader.Hash()
-               nextBlockTime := uint64(time.Now().UnixNano() / 1e6)
-               if minNextBlockTime := bestBlockHeader.Timestamp + consensus.BlockTimeInterval; nextBlockTime < minNextBlockTime {
-                       nextBlockTime = minNextBlockTime
+               now := uint64(time.Now().UnixNano() / 1e6)
+               base := max(now, bestBlockHeader.Timestamp)
+               minTimeToNextBlock := consensus.BlockTimeInterval - now%consensus.BlockTimeInterval
+               nextBlockTime := base + minTimeToNextBlock
+               if nextBlockTime-now < consensus.BlockTimeInterval/10 {
+                       nextBlockTime += consensus.BlockTimeInterval
                }
+               // nextBlockTime := uint64(time.Now().UnixNano() / 1e6)
+               // if minNextBlockTime := bestBlockHeader.Timestamp + consensus.BlockTimeInterval; nextBlockTime < minNextBlockTime {
+               //      nextBlockTime = minNextBlockTime
+               // }
 
                if isBlocker, err := b.chain.IsBlocker(&bestBlockHash, xpubStr, nextBlockTime); !isBlocker {
                        log.WithFields(log.Fields{"module": logModule, "error": err, "pubKey": xpubStr}).Debug("fail on check is next blocker")
@@ -82,6 +89,13 @@ func (b *BlockProposer) generateBlocks() {
        }
 }
 
+func max(x, y uint64) uint64 {
+       if x > y {
+               return x
+       }
+       return y
+}
+
 // Start begins the block propose process as well as the speed monitor used to
 // track hashing metrics.  Calling this function when the block proposer has
 // already been started will have no effect.