OSDN Git Service

add mac function, modify nextBlockTime (#122)
authorChengcheng Zhang <943420582@qq.com>
Wed, 5 Jun 2019 13:36:31 +0000 (21:36 +0800)
committerPaladz <yzhu101@uottawa.ca>
Wed, 5 Jun 2019 13:36:31 +0000 (21:36 +0800)
* add mac function, modify nextBlockTime

* delete max function

* update

* modify block timestamp

* update

* update

* update

* update

* update

* update

* update

* update

consensus/general.go
proposal/blockproposer/blockproposer.go
protocol/validation/block.go
protocol/validation/block_test.go

index 47650f6..e048217 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(BlockTimeInterval * BlockNumEachNode / 3)
        MedianTimeBlocks = 11
 
        PayToWitnessPubKeyHashDataSize = 20
index 13ebf17..32105db 100644 (file)
@@ -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)
index 8b63df0..630a87d 100644 (file)
@@ -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
 }
 
index 99e152e..6f126c0 100644 (file)
@@ -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)",