From 25924890375fb371a7debd656e4d8cd9ac18a735 Mon Sep 17 00:00:00 2001 From: Anca Zamfir Date: Tue, 30 Nov 2021 16:57:35 -0500 Subject: [PATCH] Remove MedianTime, set block time to Now() --- internal/state/state.go | 25 +------------------------ internal/state/validation.go | 7 ------- internal/state/validation_test.go | 5 ++--- 3 files changed, 3 insertions(+), 34 deletions(-) diff --git a/internal/state/state.go b/internal/state/state.go index 6fd632ff9..fcbacdfcc 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. @@ -283,29 +283,6 @@ func (state State) MakeBlock( return block, block.MakePartSet(types.BlockPartSizeBytes) } -// 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 65c0648d4..64f09f71b 100644 --- a/internal/state/validation_test.go +++ b/internal/state/validation_test.go @@ -2,6 +2,8 @@ package state_test import ( "context" + "github.com/tendermint/tendermint/crypto/ed25519" + "github.com/tendermint/tendermint/crypto/tmhash" "testing" "time" @@ -11,8 +13,6 @@ import ( dbm "github.com/tendermint/tm-db" abci "github.com/tendermint/tendermint/abci/types" - "github.com/tendermint/tendermint/crypto/ed25519" - "github.com/tendermint/tendermint/crypto/tmhash" memmock "github.com/tendermint/tendermint/internal/mempool/mock" sm "github.com/tendermint/tendermint/internal/state" "github.com/tendermint/tendermint/internal/state/mocks" @@ -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 }},