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.

326 lines
9.3 KiB

7 years ago
7 years ago
7 years ago
7 years ago
lint: Enable Golint (#4212) * Fix many golint errors * Fix golint errors in the 'lite' package * Don't export Pool.store * Fix typo * Revert unwanted changes * Fix errors in counter package * Fix linter errors in kvstore package * Fix linter error in example package * Fix error in tests package * Fix linter errors in v2 package * Fix linter errors in consensus package * Fix linter errors in evidence package * Fix linter error in fail package * Fix linter errors in query package * Fix linter errors in core package * Fix linter errors in node package * Fix linter errors in mempool package * Fix linter error in conn package * Fix linter errors in pex package * Rename PEXReactor export to Reactor * Fix linter errors in trust package * Fix linter errors in upnp package * Fix linter errors in p2p package * Fix linter errors in proxy package * Fix linter errors in mock_test package * Fix linter error in client_test package * Fix linter errors in coretypes package * Fix linter errors in coregrpc package * Fix linter errors in rpcserver package * Fix linter errors in rpctypes package * Fix linter errors in rpctest package * Fix linter error in json2wal script * Fix linter error in wal2json script * Fix linter errors in kv package * Fix linter error in state package * Fix linter error in grpc_client * Fix linter errors in types package * Fix linter error in version package * Fix remaining errors * Address review comments * Fix broken tests * Reconcile package coregrpc * Fix golangci bot error * Fix new golint errors * Fix broken reference * Enable golint linter * minor changes to bring golint into line * fix failing test * fix pex reactor naming * address PR comments
5 years ago
7 years ago
lint: Enable Golint (#4212) * Fix many golint errors * Fix golint errors in the 'lite' package * Don't export Pool.store * Fix typo * Revert unwanted changes * Fix errors in counter package * Fix linter errors in kvstore package * Fix linter error in example package * Fix error in tests package * Fix linter errors in v2 package * Fix linter errors in consensus package * Fix linter errors in evidence package * Fix linter error in fail package * Fix linter errors in query package * Fix linter errors in core package * Fix linter errors in node package * Fix linter errors in mempool package * Fix linter error in conn package * Fix linter errors in pex package * Rename PEXReactor export to Reactor * Fix linter errors in trust package * Fix linter errors in upnp package * Fix linter errors in p2p package * Fix linter errors in proxy package * Fix linter errors in mock_test package * Fix linter error in client_test package * Fix linter errors in coretypes package * Fix linter errors in coregrpc package * Fix linter errors in rpcserver package * Fix linter errors in rpctypes package * Fix linter errors in rpctest package * Fix linter error in json2wal script * Fix linter error in wal2json script * Fix linter errors in kv package * Fix linter error in state package * Fix linter error in grpc_client * Fix linter errors in types package * Fix linter error in version package * Fix remaining errors * Address review comments * Fix broken tests * Reconcile package coregrpc * Fix golangci bot error * Fix new golint errors * Fix broken reference * Enable golint linter * minor changes to bring golint into line * fix failing test * fix pex reactor naming * address PR comments
5 years ago
7 years ago
7 years ago
7 years ago
7 years ago
lint: Enable Golint (#4212) * Fix many golint errors * Fix golint errors in the 'lite' package * Don't export Pool.store * Fix typo * Revert unwanted changes * Fix errors in counter package * Fix linter errors in kvstore package * Fix linter error in example package * Fix error in tests package * Fix linter errors in v2 package * Fix linter errors in consensus package * Fix linter errors in evidence package * Fix linter error in fail package * Fix linter errors in query package * Fix linter errors in core package * Fix linter errors in node package * Fix linter errors in mempool package * Fix linter error in conn package * Fix linter errors in pex package * Rename PEXReactor export to Reactor * Fix linter errors in trust package * Fix linter errors in upnp package * Fix linter errors in p2p package * Fix linter errors in proxy package * Fix linter errors in mock_test package * Fix linter error in client_test package * Fix linter errors in coretypes package * Fix linter errors in coregrpc package * Fix linter errors in rpcserver package * Fix linter errors in rpctypes package * Fix linter errors in rpctest package * Fix linter error in json2wal script * Fix linter error in wal2json script * Fix linter errors in kv package * Fix linter error in state package * Fix linter error in grpc_client * Fix linter errors in types package * Fix linter error in version package * Fix remaining errors * Address review comments * Fix broken tests * Reconcile package coregrpc * Fix golangci bot error * Fix new golint errors * Fix broken reference * Enable golint linter * minor changes to bring golint into line * fix failing test * fix pex reactor naming * address PR comments
5 years ago
lint: Enable Golint (#4212) * Fix many golint errors * Fix golint errors in the 'lite' package * Don't export Pool.store * Fix typo * Revert unwanted changes * Fix errors in counter package * Fix linter errors in kvstore package * Fix linter error in example package * Fix error in tests package * Fix linter errors in v2 package * Fix linter errors in consensus package * Fix linter errors in evidence package * Fix linter error in fail package * Fix linter errors in query package * Fix linter errors in core package * Fix linter errors in node package * Fix linter errors in mempool package * Fix linter error in conn package * Fix linter errors in pex package * Rename PEXReactor export to Reactor * Fix linter errors in trust package * Fix linter errors in upnp package * Fix linter errors in p2p package * Fix linter errors in proxy package * Fix linter errors in mock_test package * Fix linter error in client_test package * Fix linter errors in coretypes package * Fix linter errors in coregrpc package * Fix linter errors in rpcserver package * Fix linter errors in rpctypes package * Fix linter errors in rpctest package * Fix linter error in json2wal script * Fix linter error in wal2json script * Fix linter errors in kv package * Fix linter error in state package * Fix linter error in grpc_client * Fix linter errors in types package * Fix linter error in version package * Fix remaining errors * Address review comments * Fix broken tests * Reconcile package coregrpc * Fix golangci bot error * Fix new golint errors * Fix broken reference * Enable golint linter * minor changes to bring golint into line * fix failing test * fix pex reactor naming * address PR comments
5 years ago
7 years ago
lint: Enable Golint (#4212) * Fix many golint errors * Fix golint errors in the 'lite' package * Don't export Pool.store * Fix typo * Revert unwanted changes * Fix errors in counter package * Fix linter errors in kvstore package * Fix linter error in example package * Fix error in tests package * Fix linter errors in v2 package * Fix linter errors in consensus package * Fix linter errors in evidence package * Fix linter error in fail package * Fix linter errors in query package * Fix linter errors in core package * Fix linter errors in node package * Fix linter errors in mempool package * Fix linter error in conn package * Fix linter errors in pex package * Rename PEXReactor export to Reactor * Fix linter errors in trust package * Fix linter errors in upnp package * Fix linter errors in p2p package * Fix linter errors in proxy package * Fix linter errors in mock_test package * Fix linter error in client_test package * Fix linter errors in coretypes package * Fix linter errors in coregrpc package * Fix linter errors in rpcserver package * Fix linter errors in rpctypes package * Fix linter errors in rpctest package * Fix linter error in json2wal script * Fix linter error in wal2json script * Fix linter errors in kv package * Fix linter error in state package * Fix linter error in grpc_client * Fix linter errors in types package * Fix linter error in version package * Fix remaining errors * Address review comments * Fix broken tests * Reconcile package coregrpc * Fix golangci bot error * Fix new golint errors * Fix broken reference * Enable golint linter * minor changes to bring golint into line * fix failing test * fix pex reactor naming * address PR comments
5 years ago
7 years ago
lint: Enable Golint (#4212) * Fix many golint errors * Fix golint errors in the 'lite' package * Don't export Pool.store * Fix typo * Revert unwanted changes * Fix errors in counter package * Fix linter errors in kvstore package * Fix linter error in example package * Fix error in tests package * Fix linter errors in v2 package * Fix linter errors in consensus package * Fix linter errors in evidence package * Fix linter error in fail package * Fix linter errors in query package * Fix linter errors in core package * Fix linter errors in node package * Fix linter errors in mempool package * Fix linter error in conn package * Fix linter errors in pex package * Rename PEXReactor export to Reactor * Fix linter errors in trust package * Fix linter errors in upnp package * Fix linter errors in p2p package * Fix linter errors in proxy package * Fix linter errors in mock_test package * Fix linter error in client_test package * Fix linter errors in coretypes package * Fix linter errors in coregrpc package * Fix linter errors in rpcserver package * Fix linter errors in rpctypes package * Fix linter errors in rpctest package * Fix linter error in json2wal script * Fix linter error in wal2json script * Fix linter errors in kv package * Fix linter error in state package * Fix linter error in grpc_client * Fix linter errors in types package * Fix linter error in version package * Fix remaining errors * Address review comments * Fix broken tests * Reconcile package coregrpc * Fix golangci bot error * Fix new golint errors * Fix broken reference * Enable golint linter * minor changes to bring golint into line * fix failing test * fix pex reactor naming * address PR comments
5 years ago
7 years ago
lint: Enable Golint (#4212) * Fix many golint errors * Fix golint errors in the 'lite' package * Don't export Pool.store * Fix typo * Revert unwanted changes * Fix errors in counter package * Fix linter errors in kvstore package * Fix linter error in example package * Fix error in tests package * Fix linter errors in v2 package * Fix linter errors in consensus package * Fix linter errors in evidence package * Fix linter error in fail package * Fix linter errors in query package * Fix linter errors in core package * Fix linter errors in node package * Fix linter errors in mempool package * Fix linter error in conn package * Fix linter errors in pex package * Rename PEXReactor export to Reactor * Fix linter errors in trust package * Fix linter errors in upnp package * Fix linter errors in p2p package * Fix linter errors in proxy package * Fix linter errors in mock_test package * Fix linter error in client_test package * Fix linter errors in coretypes package * Fix linter errors in coregrpc package * Fix linter errors in rpcserver package * Fix linter errors in rpctypes package * Fix linter errors in rpctest package * Fix linter error in json2wal script * Fix linter error in wal2json script * Fix linter errors in kv package * Fix linter error in state package * Fix linter error in grpc_client * Fix linter errors in types package * Fix linter error in version package * Fix remaining errors * Address review comments * Fix broken tests * Reconcile package coregrpc * Fix golangci bot error * Fix new golint errors * Fix broken reference * Enable golint linter * minor changes to bring golint into line * fix failing test * fix pex reactor naming * address PR comments
5 years ago
7 years ago
lint: Enable Golint (#4212) * Fix many golint errors * Fix golint errors in the 'lite' package * Don't export Pool.store * Fix typo * Revert unwanted changes * Fix errors in counter package * Fix linter errors in kvstore package * Fix linter error in example package * Fix error in tests package * Fix linter errors in v2 package * Fix linter errors in consensus package * Fix linter errors in evidence package * Fix linter error in fail package * Fix linter errors in query package * Fix linter errors in core package * Fix linter errors in node package * Fix linter errors in mempool package * Fix linter error in conn package * Fix linter errors in pex package * Rename PEXReactor export to Reactor * Fix linter errors in trust package * Fix linter errors in upnp package * Fix linter errors in p2p package * Fix linter errors in proxy package * Fix linter errors in mock_test package * Fix linter error in client_test package * Fix linter errors in coretypes package * Fix linter errors in coregrpc package * Fix linter errors in rpcserver package * Fix linter errors in rpctypes package * Fix linter errors in rpctest package * Fix linter error in json2wal script * Fix linter error in wal2json script * Fix linter errors in kv package * Fix linter error in state package * Fix linter error in grpc_client * Fix linter errors in types package * Fix linter error in version package * Fix remaining errors * Address review comments * Fix broken tests * Reconcile package coregrpc * Fix golangci bot error * Fix new golint errors * Fix broken reference * Enable golint linter * minor changes to bring golint into line * fix failing test * fix pex reactor naming * address PR comments
5 years ago
lint: Enable Golint (#4212) * Fix many golint errors * Fix golint errors in the 'lite' package * Don't export Pool.store * Fix typo * Revert unwanted changes * Fix errors in counter package * Fix linter errors in kvstore package * Fix linter error in example package * Fix error in tests package * Fix linter errors in v2 package * Fix linter errors in consensus package * Fix linter errors in evidence package * Fix linter error in fail package * Fix linter errors in query package * Fix linter errors in core package * Fix linter errors in node package * Fix linter errors in mempool package * Fix linter error in conn package * Fix linter errors in pex package * Rename PEXReactor export to Reactor * Fix linter errors in trust package * Fix linter errors in upnp package * Fix linter errors in p2p package * Fix linter errors in proxy package * Fix linter errors in mock_test package * Fix linter error in client_test package * Fix linter errors in coretypes package * Fix linter errors in coregrpc package * Fix linter errors in rpcserver package * Fix linter errors in rpctypes package * Fix linter errors in rpctest package * Fix linter error in json2wal script * Fix linter error in wal2json script * Fix linter errors in kv package * Fix linter error in state package * Fix linter error in grpc_client * Fix linter errors in types package * Fix linter error in version package * Fix remaining errors * Address review comments * Fix broken tests * Reconcile package coregrpc * Fix golangci bot error * Fix new golint errors * Fix broken reference * Enable golint linter * minor changes to bring golint into line * fix failing test * fix pex reactor naming * address PR comments
5 years ago
add support for block pruning via ABCI Commit response (#4588) * Added BlockStore.DeleteBlock() * Added initial block pruner prototype * wip * Added BlockStore.PruneBlocks() * Added consensus setting for block pruning * Added BlockStore base * Error on replay if base does not have blocks * Handle missing blocks when sending VoteSetMaj23Message * Error message tweak * Properly update blockstore state * Error message fix again * blockchain: ignore peer missing blocks * Added FIXME * Added test for block replay with truncated history * Handle peer base in blockchain reactor * Improved replay error handling * Added tests for Store.PruneBlocks() * Fix non-RPC handling of truncated block history * Panic on missing block meta in needProofBlock() * Updated changelog * Handle truncated block history in RPC layer * Added info about earliest block in /status RPC * Reorder height and base in blockchain reactor messages * Updated changelog * Fix tests * Appease linter * Minor review fixes * Non-empty BlockStores should always have base > 0 * Update code to assume base > 0 invariant * Added blockstore tests for pruning to 0 * Make sure we don't prune below the current base * Added BlockStore.Size() * config: added retain_blocks recommendations * Update v1 blockchain reactor to handle blockstore base * Added state database pruning * Propagate errors on missing validator sets * Comment tweaks * Improved error message Co-Authored-By: Anton Kaliaev <anton.kalyaev@gmail.com> * use ABCI field ResponseCommit.retain_height instead of retain-blocks config option * remove State.RetainHeight, return value instead * fix minor issues * rename pruneHeights() to pruneBlocks() * noop to fix GitHub borkage Co-authored-by: Anton Kaliaev <anton.kalyaev@gmail.com>
5 years ago
7 years ago
7 years ago
7 years ago
7 years ago
lint: Enable Golint (#4212) * Fix many golint errors * Fix golint errors in the 'lite' package * Don't export Pool.store * Fix typo * Revert unwanted changes * Fix errors in counter package * Fix linter errors in kvstore package * Fix linter error in example package * Fix error in tests package * Fix linter errors in v2 package * Fix linter errors in consensus package * Fix linter errors in evidence package * Fix linter error in fail package * Fix linter errors in query package * Fix linter errors in core package * Fix linter errors in node package * Fix linter errors in mempool package * Fix linter error in conn package * Fix linter errors in pex package * Rename PEXReactor export to Reactor * Fix linter errors in trust package * Fix linter errors in upnp package * Fix linter errors in p2p package * Fix linter errors in proxy package * Fix linter errors in mock_test package * Fix linter error in client_test package * Fix linter errors in coretypes package * Fix linter errors in coregrpc package * Fix linter errors in rpcserver package * Fix linter errors in rpctypes package * Fix linter errors in rpctest package * Fix linter error in json2wal script * Fix linter error in wal2json script * Fix linter errors in kv package * Fix linter error in state package * Fix linter error in grpc_client * Fix linter errors in types package * Fix linter error in version package * Fix remaining errors * Address review comments * Fix broken tests * Reconcile package coregrpc * Fix golangci bot error * Fix new golint errors * Fix broken reference * Enable golint linter * minor changes to bring golint into line * fix failing test * fix pex reactor naming * address PR comments
5 years ago
lint: Enable Golint (#4212) * Fix many golint errors * Fix golint errors in the 'lite' package * Don't export Pool.store * Fix typo * Revert unwanted changes * Fix errors in counter package * Fix linter errors in kvstore package * Fix linter error in example package * Fix error in tests package * Fix linter errors in v2 package * Fix linter errors in consensus package * Fix linter errors in evidence package * Fix linter error in fail package * Fix linter errors in query package * Fix linter errors in core package * Fix linter errors in node package * Fix linter errors in mempool package * Fix linter error in conn package * Fix linter errors in pex package * Rename PEXReactor export to Reactor * Fix linter errors in trust package * Fix linter errors in upnp package * Fix linter errors in p2p package * Fix linter errors in proxy package * Fix linter errors in mock_test package * Fix linter error in client_test package * Fix linter errors in coretypes package * Fix linter errors in coregrpc package * Fix linter errors in rpcserver package * Fix linter errors in rpctypes package * Fix linter errors in rpctest package * Fix linter error in json2wal script * Fix linter error in wal2json script * Fix linter errors in kv package * Fix linter error in state package * Fix linter error in grpc_client * Fix linter errors in types package * Fix linter error in version package * Fix remaining errors * Address review comments * Fix broken tests * Reconcile package coregrpc * Fix golangci bot error * Fix new golint errors * Fix broken reference * Enable golint linter * minor changes to bring golint into line * fix failing test * fix pex reactor naming * address PR comments
5 years ago
7 years ago
  1. package evidence
  2. import (
  3. "fmt"
  4. "sync"
  5. "time"
  6. dbm "github.com/tendermint/tm-db"
  7. clist "github.com/tendermint/tendermint/libs/clist"
  8. "github.com/tendermint/tendermint/libs/log"
  9. sm "github.com/tendermint/tendermint/state"
  10. "github.com/tendermint/tendermint/store"
  11. "github.com/tendermint/tendermint/types"
  12. )
  13. // Pool maintains a pool of valid evidence in an Store.
  14. type Pool struct {
  15. logger log.Logger
  16. store *Store
  17. evidenceList *clist.CList // concurrent linked-list of evidence
  18. // needed to load validators to verify evidence
  19. stateDB dbm.DB
  20. blockStore *store.BlockStore
  21. // a map of active validators and respective last heights validator is active
  22. // if it was in validator set after EvidenceParams.MaxAgeNumBlocks or
  23. // currently is (ie. [MaxAgeNumBlocks, CurrentHeight])
  24. // In simple words, it means it's still bonded -> therefore slashable.
  25. valToLastHeight valToLastHeightMap
  26. // latest state
  27. mtx sync.Mutex
  28. state sm.State
  29. }
  30. // Validator.Address -> Last height it was in validator set
  31. type valToLastHeightMap map[string]int64
  32. func NewPool(stateDB, evidenceDB dbm.DB, blockStore *store.BlockStore) (*Pool, error) {
  33. var (
  34. store = NewStore(evidenceDB)
  35. state = sm.LoadState(stateDB)
  36. )
  37. valToLastHeight, err := buildValToLastHeightMap(state, stateDB, blockStore)
  38. if err != nil {
  39. return nil, err
  40. }
  41. return &Pool{
  42. stateDB: stateDB,
  43. blockStore: blockStore,
  44. state: state,
  45. logger: log.NewNopLogger(),
  46. store: store,
  47. evidenceList: clist.New(),
  48. valToLastHeight: valToLastHeight,
  49. }, nil
  50. }
  51. func (evpool *Pool) EvidenceFront() *clist.CElement {
  52. return evpool.evidenceList.Front()
  53. }
  54. func (evpool *Pool) EvidenceWaitChan() <-chan struct{} {
  55. return evpool.evidenceList.WaitChan()
  56. }
  57. // SetLogger sets the Logger.
  58. func (evpool *Pool) SetLogger(l log.Logger) {
  59. evpool.logger = l
  60. }
  61. // PriorityEvidence returns the priority evidence.
  62. func (evpool *Pool) PriorityEvidence() []types.Evidence {
  63. return evpool.store.PriorityEvidence()
  64. }
  65. // PendingEvidence returns up to maxNum uncommitted evidence.
  66. // If maxNum is -1, all evidence is returned.
  67. func (evpool *Pool) PendingEvidence(maxNum int64) []types.Evidence {
  68. return evpool.store.PendingEvidence(maxNum)
  69. }
  70. // State returns the current state of the evpool.
  71. func (evpool *Pool) State() sm.State {
  72. evpool.mtx.Lock()
  73. defer evpool.mtx.Unlock()
  74. return evpool.state
  75. }
  76. // Update loads the latest
  77. func (evpool *Pool) Update(block *types.Block, state sm.State) {
  78. // sanity check
  79. if state.LastBlockHeight != block.Height {
  80. panic(
  81. fmt.Sprintf("Failed EvidencePool.Update sanity check: got state.Height=%d with block.Height=%d",
  82. state.LastBlockHeight,
  83. block.Height,
  84. ),
  85. )
  86. }
  87. // update the state
  88. evpool.mtx.Lock()
  89. evpool.state = state
  90. evpool.mtx.Unlock()
  91. // remove evidence from pending and mark committed
  92. evpool.MarkEvidenceAsCommitted(block.Height, block.Time, block.Evidence.Evidence)
  93. evpool.updateValToLastHeight(block.Height, state)
  94. }
  95. // AddEvidence checks the evidence is valid and adds it to the pool. If
  96. // evidence is composite (ConflictingHeadersEvidence), it will be broken up
  97. // into smaller pieces.
  98. func (evpool *Pool) AddEvidence(evidence types.Evidence) error {
  99. var (
  100. state = evpool.State()
  101. evList = []types.Evidence{evidence}
  102. )
  103. valSet, err := sm.LoadValidators(evpool.stateDB, evidence.Height())
  104. if err != nil {
  105. return fmt.Errorf("can't load validators at height #%d: %w", evidence.Height(), err)
  106. }
  107. // Break composite evidence into smaller pieces.
  108. if ce, ok := evidence.(types.CompositeEvidence); ok {
  109. evpool.logger.Info("Breaking up composite evidence", "ev", evidence)
  110. blockMeta := evpool.blockStore.LoadBlockMeta(evidence.Height())
  111. if blockMeta == nil {
  112. return fmt.Errorf("don't have block meta at height #%d", evidence.Height())
  113. }
  114. if err := ce.VerifyComposite(&blockMeta.Header, valSet); err != nil {
  115. return err
  116. }
  117. evList = ce.Split(&blockMeta.Header, valSet, evpool.valToLastHeight)
  118. }
  119. for _, ev := range evList {
  120. if evpool.store.Has(evidence) {
  121. return ErrEvidenceAlreadyStored{}
  122. }
  123. // For lunatic validator evidence, a header needs to be fetched.
  124. var header *types.Header
  125. if _, ok := ev.(*types.LunaticValidatorEvidence); ok {
  126. blockMeta := evpool.blockStore.LoadBlockMeta(ev.Height())
  127. if blockMeta == nil {
  128. return fmt.Errorf("don't have block meta at height #%d", ev.Height())
  129. }
  130. header = &blockMeta.Header
  131. }
  132. // 1) Verify against state.
  133. if err := sm.VerifyEvidence(evpool.stateDB, state, ev, header); err != nil {
  134. return fmt.Errorf("failed to verify %v: %w", ev, err)
  135. }
  136. // 2) Compute priority.
  137. _, val := valSet.GetByAddress(ev.Address())
  138. priority := val.VotingPower
  139. // 3) Save to store.
  140. _, err := evpool.store.AddNewEvidence(ev, priority)
  141. if err != nil {
  142. return fmt.Errorf("failed to add new evidence %v: %w", ev, err)
  143. }
  144. // 4) Add evidence to clist.
  145. evpool.evidenceList.PushBack(ev)
  146. evpool.logger.Info("Verified new evidence of byzantine behaviour", "evidence", ev)
  147. }
  148. return nil
  149. }
  150. // MarkEvidenceAsCommitted marks all the evidence as committed and removes it
  151. // from the queue.
  152. func (evpool *Pool) MarkEvidenceAsCommitted(height int64, lastBlockTime time.Time, evidence []types.Evidence) {
  153. // make a map of committed evidence to remove from the clist
  154. blockEvidenceMap := make(map[string]struct{})
  155. for _, ev := range evidence {
  156. evpool.store.MarkEvidenceAsCommitted(ev)
  157. blockEvidenceMap[evMapKey(ev)] = struct{}{}
  158. }
  159. // remove committed evidence from the clist
  160. evidenceParams := evpool.State().ConsensusParams.Evidence
  161. evpool.removeEvidence(height, lastBlockTime, evidenceParams, blockEvidenceMap)
  162. }
  163. // IsCommitted returns true if we have already seen this exact evidence and it
  164. // is already marked as committed.
  165. func (evpool *Pool) IsCommitted(evidence types.Evidence) bool {
  166. ei := evpool.store.getInfo(evidence)
  167. return ei.Evidence != nil && ei.Committed
  168. }
  169. // ValidatorLastHeight returns the last height of the validator w/ the
  170. // given address. 0 - if address never was a validator or was such a
  171. // long time ago (> ConsensusParams.Evidence.MaxAgeDuration && >
  172. // ConsensusParams.Evidence.MaxAgeNumBlocks).
  173. func (evpool *Pool) ValidatorLastHeight(address []byte) int64 {
  174. h, ok := evpool.valToLastHeight[string(address)]
  175. if !ok {
  176. return 0
  177. }
  178. return h
  179. }
  180. func (evpool *Pool) removeEvidence(
  181. height int64,
  182. lastBlockTime time.Time,
  183. params types.EvidenceParams,
  184. blockEvidenceMap map[string]struct{}) {
  185. for e := evpool.evidenceList.Front(); e != nil; e = e.Next() {
  186. var (
  187. ev = e.Value.(types.Evidence)
  188. ageDuration = lastBlockTime.Sub(ev.Time())
  189. ageNumBlocks = height - ev.Height()
  190. )
  191. // Remove the evidence if it's already in a block or if it's now too old.
  192. if _, ok := blockEvidenceMap[evMapKey(ev)]; ok ||
  193. (ageDuration > params.MaxAgeDuration && ageNumBlocks > params.MaxAgeNumBlocks) {
  194. // remove from clist
  195. evpool.evidenceList.Remove(e)
  196. e.DetachPrev()
  197. }
  198. }
  199. }
  200. func evMapKey(ev types.Evidence) string {
  201. return string(ev.Hash())
  202. }
  203. func (evpool *Pool) updateValToLastHeight(blockHeight int64, state sm.State) {
  204. // Update current validators & add new ones.
  205. for _, val := range state.Validators.Validators {
  206. evpool.valToLastHeight[string(val.Address)] = blockHeight
  207. }
  208. // Remove validators outside of MaxAgeNumBlocks & MaxAgeDuration.
  209. removeHeight := blockHeight - evpool.State().ConsensusParams.Evidence.MaxAgeNumBlocks
  210. if removeHeight >= 1 {
  211. valSet, err := sm.LoadValidators(evpool.stateDB, removeHeight)
  212. if err != nil {
  213. for _, val := range valSet.Validators {
  214. h, ok := evpool.valToLastHeight[string(val.Address)]
  215. if ok && h == removeHeight {
  216. delete(evpool.valToLastHeight, string(val.Address))
  217. }
  218. }
  219. }
  220. }
  221. }
  222. func buildValToLastHeightMap(state sm.State, stateDB dbm.DB, blockStore *store.BlockStore) (valToLastHeightMap, error) {
  223. var (
  224. valToLastHeight = make(map[string]int64)
  225. params = state.ConsensusParams.Evidence
  226. numBlocks = int64(0)
  227. minAgeTime = time.Now().Add(-params.MaxAgeDuration)
  228. height = state.LastBlockHeight
  229. )
  230. if height == 0 {
  231. return valToLastHeight, nil
  232. }
  233. meta := blockStore.LoadBlockMeta(height)
  234. if meta == nil {
  235. return nil, fmt.Errorf("block meta for height %d not found", height)
  236. }
  237. blockTime := meta.Header.Time
  238. // From state.LastBlockHeight, build a map of "active" validators until
  239. // MaxAgeNumBlocks is passed and block time is less than now() -
  240. // MaxAgeDuration.
  241. for height >= 1 && (numBlocks <= params.MaxAgeNumBlocks || !blockTime.Before(minAgeTime)) {
  242. valSet, err := sm.LoadValidators(stateDB, height)
  243. if err != nil {
  244. // last stored height -> return
  245. if _, ok := err.(sm.ErrNoValSetForHeight); ok {
  246. return valToLastHeight, nil
  247. }
  248. return nil, fmt.Errorf("validator set for height %d not found", height)
  249. }
  250. for _, val := range valSet.Validators {
  251. key := string(val.Address)
  252. if _, ok := valToLastHeight[key]; !ok {
  253. valToLastHeight[key] = height
  254. }
  255. }
  256. height--
  257. if height > 0 {
  258. // NOTE: we assume here blockStore and state.Validators are in sync. I.e if
  259. // block N is stored, then validators for height N are also stored in
  260. // state.
  261. meta := blockStore.LoadBlockMeta(height)
  262. if meta == nil {
  263. return nil, fmt.Errorf("block meta for height %d not found", height)
  264. }
  265. blockTime = meta.Header.Time
  266. }
  267. numBlocks++
  268. }
  269. return valToLastHeight, nil
  270. }