From 14f4db7c19c16e5499fb277c3f90971550bc90b8 Mon Sep 17 00:00:00 2001 From: Anca Zamfir Date: Thu, 9 Dec 2021 05:01:27 +0100 Subject: [PATCH] Remove MedianTime, set block time to Now() (#7382) * Remove MedianTime, set block time to Now() * Fix goimports * Fix import ordering --- internal/state/state.go | 25 +------------------------ internal/state/validation.go | 7 ------- internal/state/validation_test.go | 1 - 3 files changed, 1 insertion(+), 32 deletions(-) diff --git a/internal/state/state.go b/internal/state/state.go index 8b69e6f0c..876c87fe8 100644 --- a/internal/state/state.go +++ b/internal/state/state.go @@ -268,7 +268,7 @@ func (state State) MakeBlock( if height == state.InitialHeight { timestamp = state.LastBlockTime // genesis time } else { - timestamp = MedianTime(commit, state.LastValidators) + timestamp = time.Now() } // Fill rest of header with state data. @@ -288,29 +288,6 @@ func (state State) MakeBlock( return block, bps, nil } -// MedianTime computes a median time for a given Commit (based on Timestamp field of votes messages) and the -// corresponding validator set. The computed time is always between timestamps of -// the votes sent by honest processes, i.e., a faulty processes can not arbitrarily increase or decrease the -// computed value. -func MedianTime(commit *types.Commit, validators *types.ValidatorSet) time.Time { - weightedTimes := make([]*weightedTime, len(commit.Signatures)) - totalVotingPower := int64(0) - - for i, commitSig := range commit.Signatures { - if commitSig.Absent() { - continue - } - _, validator := validators.GetByAddress(commitSig.ValidatorAddress) - // If there's no condition, TestValidateBlockCommit panics; not needed normally. - if validator != nil { - totalVotingPower += validator.VotingPower - weightedTimes[i] = newWeightedTime(commitSig.Timestamp, validator.VotingPower) - } - } - - return weightedMedian(weightedTimes, totalVotingPower) -} - //------------------------------------------------------------------------ // Genesis diff --git a/internal/state/validation.go b/internal/state/validation.go index fbd285f8a..8bec8f119 100644 --- a/internal/state/validation.go +++ b/internal/state/validation.go @@ -114,13 +114,6 @@ func validateBlock(state State, block *types.Block) error { state.LastBlockTime, ) } - medianTime := MedianTime(block.LastCommit, state.LastValidators) - if !block.Time.Equal(medianTime) { - return fmt.Errorf("invalid block time. Expected %v, got %v", - medianTime, - block.Time, - ) - } case block.Height == state.InitialHeight: genesisTime := state.LastBlockTime diff --git a/internal/state/validation_test.go b/internal/state/validation_test.go index 411740c37..3fb723a27 100644 --- a/internal/state/validation_test.go +++ b/internal/state/validation_test.go @@ -64,7 +64,6 @@ func TestValidateBlockHeader(t *testing.T) { {"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 * 1) }}, - {"Time wrong 2", func(block *types.Block) { block.Time = block.Time.Add(time.Second * 1) }}, {"LastBlockID wrong", func(block *types.Block) { block.LastBlockID.PartSetHeader.Total += 10 }}, {"LastCommitHash wrong", func(block *types.Block) { block.LastCommitHash = wrongHash }},