Skip to content

Commit

Permalink
require block.Time of the fist block to be genesis time
Browse files Browse the repository at this point in the history
Refs #2587:

```
We only start validating block.Time when Height > 1, because there is no
commit to compute the median timestamp from for the first block. This
means a faulty proposer could make the first block with whatever time
they want.

Instead, we should require the timestamp of block 1 to match the genesis
time.

I discovered this while refactoring the ValidateBlock tests to be
table-driven while working on tests for #2560.
```
  • Loading branch information
melekes committed Oct 10, 2018
1 parent 6ec52a9 commit 2d05d51
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
5 changes: 1 addition & 4 deletions state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,7 @@ func (state State) MakeBlock(

// Set time
if height == 1 {
block.Time = tmtime.Now()
if block.Time.Before(state.LastBlockTime) {
block.Time = state.LastBlockTime // state.LastBlockTime for height == 1 is genesis time
}
block.Time = state.LastBlockTime // genesis time
} else {
block.Time = MedianTime(commit, state.LastValidators)
}
Expand Down
9 changes: 9 additions & 0 deletions state/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,15 @@ func validateBlock(stateDB dbm.DB, state State, block *types.Block) error {
block.Time,
)
}
} else if block.Height == 1 {
genesisTime := state.LastBlockTime
if !block.Time.Equal(genesisTime) {
return fmt.Errorf(
"Block time %v is not equal to genesis time %v",
block.Time,
genesisTime,
)
}
}

// Limit the amount of evidence
Expand Down
11 changes: 6 additions & 5 deletions state/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package state

import (
"testing"
"time"

"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/crypto/ed25519"
Expand Down Expand Up @@ -32,11 +33,11 @@ func TestValidateBlockHeader(t *testing.T) {
name string
malleateBlock func(block *types.Block)
}{
{"ChainID wrong", func(block *types.Block) { block.ChainID = "not-the-real-one" }}, // wrong chain id
{"Height wrong", func(block *types.Block) { block.Height += 10 }}, // wrong height
// TODO(#2589) (#2587) : {"Time", func(block *types.Block) { block.Time.Add(-time.Second * 3600 * 24) }}, // wrong time
{"NumTxs wrong", func(block *types.Block) { block.NumTxs += 10 }}, // wrong num txs
{"TotalTxs wrong", func(block *types.Block) { block.TotalTxs += 10 }}, // wrong total txs
{"ChainID wrong", func(block *types.Block) { block.ChainID = "not-the-real-one" }},
{"Height wrong", func(block *types.Block) { block.Height += 10 }},
{"Time wrong", func(block *types.Block) { block.Time = block.Time.Add(-time.Second * 3600 * 24) }},
{"NumTxs wrong", func(block *types.Block) { block.NumTxs += 10 }},
{"TotalTxs wrong", func(block *types.Block) { block.TotalTxs += 10 }},

{"LastBlockID wrong", func(block *types.Block) { block.LastBlockID.PartsHeader.Total += 10 }},
{"LastCommitHash wrong", func(block *types.Block) { block.LastCommitHash = wrongHash }},
Expand Down

0 comments on commit 2d05d51

Please sign in to comment.