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.

165 lines
4.6 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(
  64. fmt.Sprintf("Failed EvidencePool.Update sanity check: got state.Height=%d with block.Height=%d",
  65. state.LastBlockHeight,
  66. block.Height,
  67. ),
  68. )
  69. }
  70. // update the state
  71. evpool.mtx.Lock()
  72. evpool.state = state
  73. evpool.mtx.Unlock()
  74. // remove evidence from pending and mark committed
  75. evpool.MarkEvidenceAsCommitted(block.Height, block.Evidence.Evidence)
  76. }
  77. // AddEvidence checks the evidence is valid and adds it to the pool.
  78. func (evpool *EvidencePool) AddEvidence(evidence types.Evidence) (err error) {
  79. // TODO: check if we already have evidence for this
  80. // validator at this height so we dont get spammed
  81. if err := sm.VerifyEvidence(evpool.stateDB, evpool.State(), evidence); err != nil {
  82. return err
  83. }
  84. // fetch the validator and return its voting power as its priority
  85. // TODO: something better ?
  86. valset, _ := sm.LoadValidators(evpool.stateDB, evidence.Height())
  87. _, val := valset.GetByAddress(evidence.Address())
  88. priority := val.VotingPower
  89. added := evpool.evidenceStore.AddNewEvidence(evidence, priority)
  90. if !added {
  91. // evidence already known, just ignore
  92. return
  93. }
  94. evpool.logger.Info("Verified new evidence of byzantine behaviour", "evidence", evidence)
  95. // add evidence to clist
  96. evpool.evidenceList.PushBack(evidence)
  97. return nil
  98. }
  99. // MarkEvidenceAsCommitted marks all the evidence as committed and removes it from the queue.
  100. func (evpool *EvidencePool) MarkEvidenceAsCommitted(height int64, evidence []types.Evidence) {
  101. // make a map of committed evidence to remove from the clist
  102. blockEvidenceMap := make(map[string]struct{})
  103. for _, ev := range evidence {
  104. evpool.evidenceStore.MarkEvidenceAsCommitted(ev)
  105. blockEvidenceMap[evMapKey(ev)] = struct{}{}
  106. }
  107. // remove committed evidence from the clist
  108. maxAge := evpool.State().ConsensusParams.Evidence.MaxAge
  109. evpool.removeEvidence(height, maxAge, blockEvidenceMap)
  110. }
  111. // IsCommitted returns true if we have already seen this exact evidence and it is already marked as committed.
  112. func (evpool *EvidencePool) IsCommitted(evidence types.Evidence) bool {
  113. ei := evpool.evidenceStore.getEvidenceInfo(evidence)
  114. return ei.Evidence != nil && ei.Committed
  115. }
  116. func (evpool *EvidencePool) removeEvidence(height, maxAge int64, blockEvidenceMap map[string]struct{}) {
  117. for e := evpool.evidenceList.Front(); e != nil; e = e.Next() {
  118. ev := e.Value.(types.Evidence)
  119. // Remove the evidence if it's already in a block
  120. // or if it's now too old.
  121. if _, ok := blockEvidenceMap[evMapKey(ev)]; ok ||
  122. ev.Height() < height-maxAge {
  123. // remove from clist
  124. evpool.evidenceList.Remove(e)
  125. e.DetachPrev()
  126. }
  127. }
  128. }
  129. func evMapKey(ev types.Evidence) string {
  130. return string(ev.Hash())
  131. }