You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
2.3 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. package evidence
  2. import (
  3. "github.com/tendermint/tmlibs/log"
  4. "github.com/tendermint/tendermint/types"
  5. )
  6. // EvidencePool maintains a pool of valid evidence
  7. // in an EvidenceStore.
  8. type EvidencePool struct {
  9. logger log.Logger
  10. evidenceStore *EvidenceStore
  11. chainID string
  12. lastBlockHeight int64
  13. params types.EvidenceParams
  14. // never close
  15. evidenceChan chan types.Evidence
  16. }
  17. func NewEvidencePool(params types.EvidenceParams, evidenceStore *EvidenceStore, state types.State) *EvidencePool {
  18. evpool := &EvidencePool{
  19. params: params,
  20. logger: log.NewNopLogger(),
  21. evidenceStore: evidenceStore,
  22. state: *state,
  23. evidenceChan: make(chan types.Evidence),
  24. }
  25. return evpool
  26. }
  27. // SetLogger sets the Logger.
  28. func (evpool *EvidencePool) SetLogger(l log.Logger) {
  29. evpool.logger = l
  30. }
  31. // EvidenceChan returns an unbuffered channel on which new evidence can be received.
  32. func (evpool *EvidencePool) EvidenceChan() <-chan types.Evidence {
  33. return evpool.evidenceChan
  34. }
  35. // PriorityEvidence returns the priority evidence.
  36. func (evpool *EvidencePool) PriorityEvidence() []types.Evidence {
  37. return evpool.evidenceStore.PriorityEvidence()
  38. }
  39. // PendingEvidence returns all uncommitted evidence.
  40. func (evpool *EvidencePool) PendingEvidence() []types.Evidence {
  41. return evpool.evidenceStore.PendingEvidence()
  42. }
  43. // AddEvidence checks the evidence is valid and adds it to the pool.
  44. // Blocks on the EvidenceChan.
  45. func (evpool *EvidencePool) AddEvidence(evidence types.Evidence) (err error) {
  46. // TODO: check if we already have evidence for this
  47. // validator at this height so we dont get spammed
  48. priority, err := sm.VerifyEvidence(evpool.state, evidence)
  49. if err != nil {
  50. // TODO: if err is just that we cant find it cuz we pruned, ignore.
  51. // TODO: if its actually bad evidence, punish peer
  52. return err
  53. }
  54. added := evpool.evidenceStore.AddNewEvidence(evidence, priority)
  55. if !added {
  56. // evidence already known, just ignore
  57. return
  58. }
  59. evpool.logger.Info("Verified new evidence of byzantine behaviour", "evidence", evidence)
  60. // never closes. always safe to send on
  61. evpool.evidenceChan <- evidence
  62. return nil
  63. }
  64. // MarkEvidenceAsCommitted marks all the evidence as committed.
  65. func (evpool *EvidencePool) MarkEvidenceAsCommitted(evidence []types.Evidence) {
  66. for _, ev := range evidence {
  67. evpool.evidenceStore.MarkEvidenceAsCommitted(ev)
  68. }
  69. }