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.

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