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.

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