From 0f90b9d59e1450d28054a5eb00a42fd9412cac89 Mon Sep 17 00:00:00 2001 From: Chengcheng Zhang <943420582@qq.com> Date: Wed, 5 Jun 2019 21:36:31 +0800 Subject: [PATCH] add mac function, modify nextBlockTime (#122) * add mac function, modify nextBlockTime * delete max function * update * modify block timestamp * update * update * update * update * update * update * update * update --- consensus/general.go | 2 +- proposal/blockproposer/blockproposer.go | 13 ++++++++++--- protocol/validation/block.go | 7 ++++--- protocol/validation/block_test.go | 8 ++++---- 4 files changed, 19 insertions(+), 11 deletions(-) diff --git a/consensus/general.go b/consensus/general.go index 47650f65..e048217e 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(BlockTimeInterval * BlockNumEachNode / 3) MedianTimeBlocks = 11 PayToWitnessPubKeyHashDataSize = 20 diff --git a/proposal/blockproposer/blockproposer.go b/proposal/blockproposer/blockproposer.go index 13ebf174..32105db3 100644 --- a/proposal/blockproposer/blockproposer.go +++ b/proposal/blockproposer/blockproposer.go @@ -52,9 +52,16 @@ 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 := 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) diff --git a/protocol/validation/block.go b/protocol/validation/block.go index 8b63df0e..630a87d4 100644 --- a/protocol/validation/block.go +++ b/protocol/validation/block.go @@ -26,13 +26,14 @@ var ( ) 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 } diff --git a/protocol/validation/block_test.go b/protocol/validation/block_test.go index 99e152ef..6f126c0d 100644 --- a/protocol/validation/block_test.go +++ b/protocol/validation/block_test.go @@ -21,15 +21,15 @@ func TestCheckBlockTime(t *testing.T) { 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)", -- 2.11.0