Browse Source

check if we already have evidence

pull/592/head
Ethan Buchman 7 years ago
parent
commit
7d086e9524
3 changed files with 31 additions and 5 deletions
  1. +11
    -4
      consensus/state.go
  2. +0
    -1
      types/block.go
  3. +20
    -0
      types/evidence.go

+ 11
- 4
consensus/state.go View File

@ -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) 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 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 return err
} else { } else {
// Probably an invalid signature / Bad peer. // Probably an invalid signature / Bad peer.
@ -1353,6 +1350,16 @@ func (cs *ConsensusState) tryAddVote(vote *types.Vote, peerKey string) error {
return nil 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) { func (cs *ConsensusState) addVote(vote *types.Vote, peerKey string) (added bool, err error) {


+ 0
- 1
types/block.go View File

@ -86,7 +86,6 @@ func (b *Block) FillHeader() {
// Hash computes and returns the block hash. // Hash computes and returns the block hash.
// If the block is incomplete, block hash is nil for safety. // If the block is incomplete, block hash is nil for safety.
func (b *Block) Hash() data.Bytes { func (b *Block) Hash() data.Bytes {
// fmt.Println(">>", b.Data)
if b == nil || b.Header == nil || b.Data == nil || b.LastCommit == nil { if b == nil || b.Header == nil || b.Data == nil || b.LastCommit == nil {
return nil return nil
} }


+ 20
- 0
types/evidence.go View File

@ -30,6 +30,7 @@ type Evidence interface {
Address() []byte Address() []byte
Hash() []byte Hash() []byte
Verify(chainID string) error Verify(chainID string) error
Equal(Evidence) bool
String() string String() string
} }
@ -61,6 +62,15 @@ func (evs Evidences) String() string {
return s 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. // DuplicateVoteEvidence contains evidence a validator signed two conflicting votes.
@ -120,3 +130,13 @@ func (dve *DuplicateVoteEvidence) Verify(chainID string) error {
return nil 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))
}

Loading…
Cancel
Save