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.

82 lines
2.1 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
7 years ago
  1. package evpool
  2. import (
  3. "github.com/tendermint/tmlibs/log"
  4. cfg "github.com/tendermint/tendermint/config"
  5. "github.com/tendermint/tendermint/types"
  6. )
  7. // EvidencePool maintains a pool of valid evidence
  8. // in an EvidenceStore.
  9. type EvidencePool struct {
  10. config *cfg.EvidenceConfig
  11. logger log.Logger
  12. state types.State
  13. evidenceStore *EvidenceStore
  14. evidenceChan chan types.Evidence
  15. }
  16. func NewEvidencePool(config *cfg.EvidenceConfig, evidenceStore *EvidenceStore, state types.State) *EvidencePool {
  17. evpool := &EvidencePool{
  18. config: config,
  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. // XXX: is this thread safe ?
  46. priority, err := evpool.state.VerifyEvidence(evidence)
  47. if err != nil {
  48. return err
  49. }
  50. added, err := evpool.evidenceStore.AddNewEvidence(evidence, priority)
  51. if err != nil {
  52. return err
  53. } else if !added {
  54. // evidence already known, just ignore
  55. return
  56. }
  57. evpool.logger.Info("Verified new evidence of byzantine behaviour", "evidence", evidence)
  58. evpool.evidenceChan <- evidence
  59. return nil
  60. }
  61. // MarkEvidenceAsCommitted marks all the evidence as committed.
  62. func (evpool *EvidencePool) MarkEvidenceAsCommitted(evidence []types.Evidence) {
  63. for _, ev := range evidence {
  64. evpool.evidenceStore.MarkEvidenceAsCommitted(ev)
  65. }
  66. }