diff --git a/consensus/state.go b/consensus/state.go index 2f9f07ec3..4785b3621 100644 --- a/consensus/state.go +++ b/consensus/state.go @@ -1338,10 +1338,7 @@ func (cs *ConsensusState) tryAddVote(vote *types.Vote, peerKey string) error { cs.Logger.Error("Found conflicting vote from ourselves. Did you unsafe_reset a validator?", "height", vote.Height, "round", vote.Round, "type", vote.Type) return err } - cs.Logger.Error("Found conflicting vote. Recording evidence in the RoundState", "height", vote.Height, "round", vote.Round, "type", vote.Type, "valAddr", vote.ValidatorAddress, "valIndex", vote.ValidatorIndex) - - // TODO: ensure we haven't seen this evidence already ! - cs.Evidence = append(cs.Evidence, voteErr.DuplicateVoteEvidence) + cs.addEvidence(voteErr.DuplicateVoteEvidence) return err } else { // Probably an invalid signature / Bad peer. @@ -1353,6 +1350,16 @@ func (cs *ConsensusState) tryAddVote(vote *types.Vote, peerKey string) error { return nil } +func (cs *ConsensusState) addEvidence(ev types.Evidence) { + if cs.Evidence.Has(ev) { + return + } + + cs.Logger.Error("Found conflicting vote. Recording evidence in the RoundState", "evidence", ev) + + cs.Evidence = append(cs.Evidence, ev) +} + //----------------------------------------------------------------------------- func (cs *ConsensusState) addVote(vote *types.Vote, peerKey string) (added bool, err error) { diff --git a/types/block.go b/types/block.go index fa59c8420..b3e754427 100644 --- a/types/block.go +++ b/types/block.go @@ -86,7 +86,6 @@ func (b *Block) FillHeader() { // Hash computes and returns the block hash. // If the block is incomplete, block hash is nil for safety. func (b *Block) Hash() data.Bytes { - // fmt.Println(">>", b.Data) if b == nil || b.Header == nil || b.Data == nil || b.LastCommit == nil { return nil } diff --git a/types/evidence.go b/types/evidence.go index 2756faf3b..d40c63ea2 100644 --- a/types/evidence.go +++ b/types/evidence.go @@ -30,6 +30,7 @@ type Evidence interface { Address() []byte Hash() []byte Verify(chainID string) error + Equal(Evidence) bool String() string } @@ -61,6 +62,15 @@ func (evs Evidences) String() string { return s } +func (evs Evidences) Has(evidence Evidence) bool { + for _, ev := range evs { + if ev.Equal(evidence) { + return true + } + } + return false +} + //------------------------------------------- // DuplicateVoteEvidence contains evidence a validator signed two conflicting votes. @@ -120,3 +130,13 @@ func (dve *DuplicateVoteEvidence) Verify(chainID string) error { return nil } + +// Equal checks if two pieces of evidence are equal. +func (dve *DuplicateVoteEvidence) Equal(ev Evidence) bool { + if _, ok := ev.(*DuplicateVoteEvidence); !ok { + return false + } + + // just check their hashes + return bytes.Equal(merkle.SimpleHashFromBinary(dve), merkle.SimpleHashFromBinary(ev)) +}