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(BlockTimeInterval * BlockNumEachNode / 3)
MedianTimeBlocks = 11
PayToWitnessPubKeyHashDataSize = 20
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 := now
+ if now < bestBlockHeader.Timestamp {
+ base = bestBlockHeader.Timestamp
+ }
+ minTimeToNextBlock := consensus.BlockTimeInterval - base%consensus.BlockTimeInterval
+ nextBlockTime := base + minTimeToNextBlock
+ if (nextBlockTime - now) < consensus.BlockTimeInterval/10 {
+ nextBlockTime += consensus.BlockTimeInterval
}
isBlocker, err := b.chain.IsBlocker(&bestBlockHash, xpubStr, nextBlockTime)
)
func checkBlockTime(b *bc.Block, parent *state.BlockNode) error {
- if b.Timestamp > uint64(time.Now().UnixNano()/int64(time.Millisecond))+consensus.MaxTimeOffsetMs {
+ now := uint64(time.Now().UnixNano() / 1e6)
+ if b.Timestamp < (parent.Timestamp + consensus.BlockTimeInterval) {
return errBadTimestamp
}
-
- if b.Timestamp <= parent.CalcPastMedianTime() {
+ if b.Timestamp > (now + consensus.MaxTimeOffsetMs) {
return errBadTimestamp
}
+
return nil
}
err error
}{
{
- blockTime: 1520000001,
+ blockTime: 1520000500,
parentTime: []uint64{1520000000},
err: nil,
},
{
desc: "timestamp less than past median time (blocktest#1005)",
- blockTime: 1510000094,
- parentTime: []uint64{1520000000, 1510000099, 1510000098, 1510000097, 1510000096, 1510000095, 1510000094, 1510000093, 1510000092, 1510000091, 1510000090},
- err: errBadTimestamp,
+ blockTime: 1520005500,
+ parentTime: []uint64{1520000000, 1520000500, 1520001000, 1520001500, 1520002000, 1520002500, 1520003000, 1520003500, 1520004000, 1520004500, 1520005000},
+ err: nil,
},
{
desc: "timestamp greater than max limit (blocktest#1006)",