From 6b8cf3151051e8b515f29b9e36ae53ce1273467f Mon Sep 17 00:00:00 2001 From: Anca Zamfir Date: Tue, 21 Dec 2021 20:28:53 +0100 Subject: [PATCH] Mark proposal invalid in SetProposal, fix in the future test --- internal/consensus/pbts_test.go | 1 + internal/consensus/state.go | 12 +++++------- types/proposal.go | 5 +++-- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/internal/consensus/pbts_test.go b/internal/consensus/pbts_test.go index 761cbf18d..f97aecf3b 100644 --- a/internal/consensus/pbts_test.go +++ b/internal/consensus/pbts_test.go @@ -486,6 +486,7 @@ func TestTooFarInTheFutureProposal(t *testing.T) { genesisTime: initialTime, height2ProposedBlockTime: initialTime.Add(100 * time.Millisecond), height2ProposalDeliverTime: initialTime.Add(10 * time.Millisecond), + height4ProposedBlockTime: initialTime.Add(150 * time.Millisecond), } pbtsTest := newPBTSTestHarness(ctx, t, cfg) diff --git a/internal/consensus/state.go b/internal/consensus/state.go index 929b27328..5b9f4c017 100644 --- a/internal/consensus/state.go +++ b/internal/consensus/state.go @@ -36,7 +36,6 @@ import ( var ( ErrInvalidProposalSignature = errors.New("error invalid proposal signature") ErrInvalidProposalPOLRound = errors.New("error invalid proposal POL round") - ErrInvalidProposalNotTimely = errors.New("error invalid proposal un-timely block timestamp ") ErrAddingVote = errors.New("error adding vote") ErrSignatureFoundInPastBlocks = errors.New("found signature from the same key") @@ -1370,7 +1369,7 @@ func (cs *State) defaultDoPrevote(height int64, round int32) { */ if cs.Proposal.POLRound == -1 { if cs.LockedRound == -1 { - if !cs.proposalIsTimely(cs.Proposal) { + if !cs.Proposal.Valid { logger.Debug("prevote step: ProposalBlock is not timely; prevoting nil") cs.signAddVote(tmproto.PrevoteType, nil, types.PartSetHeader{}) return @@ -1926,11 +1925,6 @@ func (cs *State) defaultSetProposal(proposal *types.Proposal) error { return ErrInvalidProposalPOLRound } - // Verify that the proposal is not too far in the past or future - //if !cs.proposalIsTimely(proposal) { - // return ErrInvalidProposalNotTimely - //} - p := proposal.ToProto() // Verify signature if !cs.Validators.GetProposer().PubKey.VerifySignature( @@ -1948,6 +1942,10 @@ func (cs *State) defaultSetProposal(proposal *types.Proposal) error { cs.ProposalBlockParts = types.NewPartSetFromHeader(proposal.BlockID.PartSetHeader) } + // Mark the proposal as invalid if too far in the past or future + if proposal.POLRound > -1 || cs.proposalIsTimely(proposal) { + cs.Proposal.Valid = true + } cs.Logger.Info("received proposal", "proposal", proposal) return nil } diff --git a/types/proposal.go b/types/proposal.go index 90b289c97..323bfdf37 100644 --- a/types/proposal.go +++ b/types/proposal.go @@ -30,6 +30,7 @@ type Proposal struct { BlockID BlockID `json:"block_id"` Timestamp time.Time `json:"timestamp"` Signature []byte `json:"signature"` + Valid bool } // NewProposal returns a new Proposal. @@ -89,7 +90,7 @@ func (p *Proposal) ValidateBasic() error { // // For more information on the meaning of 'timely', see the proposer-based timestamp specification: // https://github.com/tendermint/spec/tree/master/spec/consensus/proposer-based-timestamp -func (p *Proposal) IsTimely(clock tmtime.Source, tp TimingParams, genesisHeight int64) bool { +func (p *Proposal) IsTimely(clock tmtime.Source, tp TimingParams, initialHeight int64) bool { localTime := clock.Now() // lhs is `proposedBlockTime - Precision` in the first inequality lhs := p.Timestamp.Add(-tp.Precision) @@ -98,7 +99,7 @@ func (p *Proposal) IsTimely(clock tmtime.Source, tp TimingParams, genesisHeight localTimeAfterOrEqLHS := localTime.After(lhs) || localTime.Equal(lhs) localTimeBeforeOrEqRHS := localTime.Before(rhs) || localTime.Equal(rhs) - if localTimeAfterOrEqLHS && (p.Height == genesisHeight || localTimeBeforeOrEqRHS) { + if localTimeAfterOrEqLHS && (p.Height == initialHeight || localTimeBeforeOrEqRHS) { return true } return false