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.

152 lines
4.2 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
7 years ago
7 years ago
7 years ago
  1. package evidence
  2. import (
  3. "fmt"
  4. "sync"
  5. clist "github.com/tendermint/tendermint/libs/clist"
  6. dbm "github.com/tendermint/tendermint/libs/db"
  7. "github.com/tendermint/tendermint/libs/log"
  8. sm "github.com/tendermint/tendermint/state"
  9. "github.com/tendermint/tendermint/types"
  10. )
  11. // EvidencePool maintains a pool of valid evidence
  12. // in an EvidenceStore.
  13. type EvidencePool struct {
  14. logger log.Logger
  15. evidenceStore *EvidenceStore
  16. evidenceList *clist.CList // concurrent linked-list of evidence
  17. // needed to load validators to verify evidence
  18. stateDB dbm.DB
  19. // latest state
  20. mtx sync.Mutex
  21. state sm.State
  22. }
  23. func NewEvidencePool(stateDB dbm.DB, evidenceStore *EvidenceStore) *EvidencePool {
  24. evpool := &EvidencePool{
  25. stateDB: stateDB,
  26. state: sm.LoadState(stateDB),
  27. logger: log.NewNopLogger(),
  28. evidenceStore: evidenceStore,
  29. evidenceList: clist.New(),
  30. }
  31. return evpool
  32. }
  33. func (evpool *EvidencePool) EvidenceFront() *clist.CElement {
  34. return evpool.evidenceList.Front()
  35. }
  36. func (evpool *EvidencePool) EvidenceWaitChan() <-chan struct{} {
  37. return evpool.evidenceList.WaitChan()
  38. }
  39. // SetLogger sets the Logger.
  40. func (evpool *EvidencePool) SetLogger(l log.Logger) {
  41. evpool.logger = l
  42. }
  43. // PriorityEvidence returns the priority evidence.
  44. func (evpool *EvidencePool) PriorityEvidence() []types.Evidence {
  45. return evpool.evidenceStore.PriorityEvidence()
  46. }
  47. // PendingEvidence returns all uncommitted evidence.
  48. func (evpool *EvidencePool) PendingEvidence() []types.Evidence {
  49. return evpool.evidenceStore.PendingEvidence()
  50. }
  51. // State returns the current state of the evpool.
  52. func (evpool *EvidencePool) State() sm.State {
  53. evpool.mtx.Lock()
  54. defer evpool.mtx.Unlock()
  55. return evpool.state
  56. }
  57. // Update loads the latest
  58. func (evpool *EvidencePool) Update(block *types.Block, state sm.State) {
  59. // sanity check
  60. if state.LastBlockHeight != block.Height {
  61. panic(fmt.Sprintf("Failed EvidencePool.Update sanity check: got state.Height=%d with block.Height=%d", state.LastBlockHeight, block.Height))
  62. }
  63. // update the state
  64. evpool.mtx.Lock()
  65. evpool.state = state
  66. evpool.mtx.Unlock()
  67. // remove evidence from pending and mark committed
  68. evpool.MarkEvidenceAsCommitted(block.Height, block.Evidence.Evidence)
  69. }
  70. // AddEvidence checks the evidence is valid and adds it to the pool.
  71. func (evpool *EvidencePool) AddEvidence(evidence types.Evidence) (err error) {
  72. // TODO: check if we already have evidence for this
  73. // validator at this height so we dont get spammed
  74. if err := sm.VerifyEvidence(evpool.stateDB, evpool.State(), evidence); err != nil {
  75. return err
  76. }
  77. // fetch the validator and return its voting power as its priority
  78. // TODO: something better ?
  79. valset, _ := sm.LoadValidators(evpool.stateDB, evidence.Height())
  80. _, val := valset.GetByAddress(evidence.Address())
  81. priority := val.VotingPower
  82. added := evpool.evidenceStore.AddNewEvidence(evidence, priority)
  83. if !added {
  84. // evidence already known, just ignore
  85. return
  86. }
  87. evpool.logger.Info("Verified new evidence of byzantine behaviour", "evidence", evidence)
  88. // add evidence to clist
  89. evpool.evidenceList.PushBack(evidence)
  90. return nil
  91. }
  92. // MarkEvidenceAsCommitted marks all the evidence as committed and removes it from the queue.
  93. func (evpool *EvidencePool) MarkEvidenceAsCommitted(height int64, evidence []types.Evidence) {
  94. // make a map of committed evidence to remove from the clist
  95. blockEvidenceMap := make(map[string]struct{})
  96. for _, ev := range evidence {
  97. evpool.evidenceStore.MarkEvidenceAsCommitted(ev)
  98. blockEvidenceMap[evMapKey(ev)] = struct{}{}
  99. }
  100. // remove committed evidence from the clist
  101. maxAge := evpool.State().ConsensusParams.EvidenceParams.MaxAge
  102. evpool.removeEvidence(height, maxAge, blockEvidenceMap)
  103. }
  104. func (evpool *EvidencePool) removeEvidence(height, maxAge int64, blockEvidenceMap map[string]struct{}) {
  105. for e := evpool.evidenceList.Front(); e != nil; e = e.Next() {
  106. ev := e.Value.(types.Evidence)
  107. // Remove the evidence if it's already in a block
  108. // or if it's now too old.
  109. if _, ok := blockEvidenceMap[evMapKey(ev)]; ok ||
  110. ev.Height() < height-maxAge {
  111. // remove from clist
  112. evpool.evidenceList.Remove(e)
  113. e.DetachPrev()
  114. }
  115. }
  116. }
  117. func evMapKey(ev types.Evidence) string {
  118. return string(ev.Hash())
  119. }