From: Chengcheng Zhang <943420582@qq.com> Date: Mon, 3 Jun 2019 13:24:33 +0000 (+0800) Subject: add mac function, modify nextBlockTime X-Git-Url: http://git.osdn.net/view?p=bytom%2Fvapor.git;a=commitdiff_plain;h=0594c99d83fb20257b1be257790195cdcbf27d37 add mac function, modify nextBlockTime --- diff --git a/consensus/general.go b/consensus/general.go index 910bce40..34a93d31 100644 --- a/consensus/general.go +++ b/consensus/general.go @@ -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 diff --git a/proposal/blockproposer/blockproposer.go b/proposal/blockproposer/blockproposer.go index cd84e708..06eac2d8 100644 --- a/proposal/blockproposer/blockproposer.go +++ b/proposal/blockproposer/blockproposer.go @@ -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.