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.

153 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 up to maxNum uncommitted evidence.
  48. // If maxNum is -1, all evidence is returned.
  49. func (evpool *EvidencePool) PendingEvidence(maxNum int64) []types.Evidence {
  50. return evpool.evidenceStore.PendingEvidence(maxNum)
  51. }
  52. // State returns the current state of the evpool.
  53. func (evpool *EvidencePool) State() sm.State {
  54. evpool.mtx.Lock()
  55. defer evpool.mtx.Unlock()
  56. return evpool.state
  57. }
  58. // Update loads the latest
  59. func (evpool *EvidencePool) Update(block *types.Block, state sm.State) {
  60. // sanity check
  61. if state.LastBlockHeight != block.Height {
  62. panic(fmt.Sprintf("Failed EvidencePool.Update sanity check: got state.Height=%d with block.Height=%d", state.LastBlockHeight, block.Height))
  63. }
  64. // update the state
  65. evpool.mtx.Lock()
  66. evpool.state = state
  67. evpool.mtx.Unlock()
  68. // remove evidence from pending and mark committed
  69. evpool.MarkEvidenceAsCommitted(block.Height, block.Evidence.Evidence)
  70. }
  71. // AddEvidence checks the evidence is valid and adds it to the pool.
  72. func (evpool *EvidencePool) AddEvidence(evidence types.Evidence) (err error) {
  73. // TODO: check if we already have evidence for this
  74. // validator at this height so we dont get spammed
  75. if err := sm.VerifyEvidence(evpool.stateDB, evpool.State(), evidence); err != nil {
  76. return err
  77. }
  78. // fetch the validator and return its voting power as its priority
  79. // TODO: something better ?
  80. valset, _ := sm.LoadValidators(evpool.stateDB, evidence.Height())
  81. _, val := valset.GetByAddress(evidence.Address())
  82. priority := val.VotingPower
  83. added := evpool.evidenceStore.AddNewEvidence(evidence, priority)
  84. if !added {
  85. // evidence already known, just ignore
  86. return
  87. }
  88. evpool.logger.Info("Verified new evidence of byzantine behaviour", "evidence", evidence)
  89. // add evidence to clist
  90. evpool.evidenceList.PushBack(evidence)
  91. return nil
  92. }
  93. // MarkEvidenceAsCommitted marks all the evidence as committed and removes it from the queue.
  94. func (evpool *EvidencePool) MarkEvidenceAsCommitted(height int64, evidence []types.Evidence) {
  95. // make a map of committed evidence to remove from the clist
  96. blockEvidenceMap := make(map[string]struct{})
  97. for _, ev := range evidence {
  98. evpool.evidenceStore.MarkEvidenceAsCommitted(ev)
  99. blockEvidenceMap[evMapKey(ev)] = struct{}{}
  100. }
  101. // remove committed evidence from the clist
  102. maxAge := evpool.State().ConsensusParams.Evidence.MaxAge
  103. evpool.removeEvidence(height, maxAge, blockEvidenceMap)
  104. }
  105. func (evpool *EvidencePool) removeEvidence(height, maxAge int64, blockEvidenceMap map[string]struct{}) {
  106. for e := evpool.evidenceList.Front(); e != nil; e = e.Next() {
  107. ev := e.Value.(types.Evidence)
  108. // Remove the evidence if it's already in a block
  109. // or if it's now too old.
  110. if _, ok := blockEvidenceMap[evMapKey(ev)]; ok ||
  111. ev.Height() < height-maxAge {
  112. // remove from clist
  113. evpool.evidenceList.Remove(e)
  114. e.DetachPrev()
  115. }
  116. }
  117. }
  118. func evMapKey(ev types.Evidence) string {
  119. return string(ev.Hash())
  120. }