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.

723 lines
23 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
7 years ago
5 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
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
7 years ago
  1. package evidence
  2. import (
  3. "fmt"
  4. "sync"
  5. "time"
  6. "github.com/gogo/protobuf/proto"
  7. gogotypes "github.com/gogo/protobuf/types"
  8. dbm "github.com/tendermint/tm-db"
  9. clist "github.com/tendermint/tendermint/libs/clist"
  10. "github.com/tendermint/tendermint/libs/log"
  11. tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
  12. sm "github.com/tendermint/tendermint/state"
  13. "github.com/tendermint/tendermint/types"
  14. )
  15. const (
  16. baseKeyCommitted = byte(0x00)
  17. baseKeyPending = byte(0x01)
  18. baseKeyPOLC = byte(0x02)
  19. baseKeyAwaitingTrial = byte(0x03)
  20. )
  21. // Pool maintains a pool of valid evidence to be broadcasted and committed
  22. type Pool struct {
  23. logger log.Logger
  24. evidenceStore dbm.DB
  25. evidenceList *clist.CList // concurrent linked-list of evidence
  26. // needed to load validators to verify evidence
  27. stateDB dbm.DB
  28. // needed to load headers to verify evidence
  29. blockStore BlockStore
  30. mtx sync.Mutex
  31. // latest state
  32. state sm.State
  33. // This is the closest height where at one or more of the current trial periods
  34. // will have ended and we will need to then upgrade the evidence to amnesia evidence.
  35. // It is set to -1 when we don't have any evidence on trial.
  36. nextEvidenceTrialEndedHeight int64
  37. }
  38. // Creates a new pool. If using an existing evidence store, it will add all pending evidence
  39. // to the concurrent list.
  40. func NewPool(stateDB, evidenceDB dbm.DB, blockStore BlockStore) (*Pool, error) {
  41. var (
  42. state = sm.LoadState(stateDB)
  43. )
  44. pool := &Pool{
  45. stateDB: stateDB,
  46. blockStore: blockStore,
  47. state: state,
  48. logger: log.NewNopLogger(),
  49. evidenceStore: evidenceDB,
  50. evidenceList: clist.New(),
  51. nextEvidenceTrialEndedHeight: -1,
  52. }
  53. // if pending evidence already in db, in event of prior failure, then load it back to the evidenceList
  54. evList := pool.AllPendingEvidence()
  55. for _, ev := range evList {
  56. pool.evidenceList.PushBack(ev)
  57. }
  58. return pool, nil
  59. }
  60. // PendingEvidence is used primarily as part of block proposal and returns up to maxNum of uncommitted evidence.
  61. // If maxNum is -1, all evidence is returned. Pending evidence is prioritized based on time.
  62. func (evpool *Pool) PendingEvidence(maxNum uint32) []types.Evidence {
  63. evpool.removeExpiredPendingEvidence()
  64. evidence, err := evpool.listEvidence(baseKeyPending, int64(maxNum))
  65. if err != nil {
  66. evpool.logger.Error("Unable to retrieve pending evidence", "err", err)
  67. }
  68. return evidence
  69. }
  70. // AllPendingEvidence returns all evidence ready to be proposed and committed.
  71. func (evpool *Pool) AllPendingEvidence() []types.Evidence {
  72. evpool.removeExpiredPendingEvidence()
  73. evidence, err := evpool.listEvidence(baseKeyPending, -1)
  74. if err != nil {
  75. evpool.logger.Error("Unable to retrieve pending evidence", "err", err)
  76. }
  77. return evidence
  78. }
  79. // Update uses the latest block & state to update any evidence that has been committed, to prune all expired evidence
  80. // and to check if any trial period of potential amnesia evidence has finished.
  81. func (evpool *Pool) Update(block *types.Block, state sm.State) {
  82. // sanity check
  83. if state.LastBlockHeight != block.Height {
  84. panic(fmt.Sprintf("Failed EvidencePool.Update sanity check: got state.Height=%d with block.Height=%d",
  85. state.LastBlockHeight,
  86. block.Height,
  87. ),
  88. )
  89. }
  90. // update the state
  91. evpool.updateState(state)
  92. // remove evidence from pending and mark committed
  93. evpool.MarkEvidenceAsCommitted(block.Height, block.Evidence.Evidence)
  94. // prune pending, committed and potential evidence and polc's periodically
  95. if block.Height%state.ConsensusParams.Evidence.MaxAgeNumBlocks == 0 {
  96. evpool.logger.Debug("Pruning expired evidence")
  97. evpool.pruneExpiredPOLC()
  98. // NOTE: As this is periodic, this implies that there may be some pending evidence in the
  99. // db that have already expired. However, expired evidence will also be removed whenever
  100. // PendingEvidence() is called ensuring that no expired evidence is proposed.
  101. evpool.removeExpiredPendingEvidence()
  102. }
  103. if evpool.nextEvidenceTrialEndedHeight > 0 && block.Height > evpool.nextEvidenceTrialEndedHeight {
  104. evpool.logger.Debug("Upgrading all potential amnesia evidence that have served the trial period")
  105. evpool.nextEvidenceTrialEndedHeight = evpool.upgradePotentialAmnesiaEvidence()
  106. }
  107. }
  108. // AddPOLC adds a proof of lock change to the evidence database
  109. // that may be needed in the future to verify votes
  110. func (evpool *Pool) AddPOLC(polc *types.ProofOfLockChange) error {
  111. key := keyPOLC(polc)
  112. pbplc, err := polc.ToProto()
  113. if err != nil {
  114. return err
  115. }
  116. polcBytes, err := proto.Marshal(pbplc)
  117. if err != nil {
  118. return fmt.Errorf("addPOLC: unable to marshal ProofOfLockChange: %w", err)
  119. }
  120. return evpool.evidenceStore.Set(key, polcBytes)
  121. }
  122. // AddEvidence checks the evidence is valid and adds it to the pool. If
  123. // evidence is composite (ConflictingHeadersEvidence), it will be broken up
  124. // into smaller pieces.
  125. func (evpool *Pool) AddEvidence(evidence types.Evidence) error {
  126. var (
  127. state = evpool.State()
  128. evList = []types.Evidence{evidence}
  129. )
  130. evpool.logger.Debug("Attempting to add evidence", "ev", evidence)
  131. valSet, err := sm.LoadValidators(evpool.stateDB, evidence.Height())
  132. if err != nil {
  133. return fmt.Errorf("can't load validators at height #%d: %w", evidence.Height(), err)
  134. }
  135. // Break composite evidence into smaller pieces.
  136. if ce, ok := evidence.(types.CompositeEvidence); ok {
  137. evpool.logger.Info("Breaking up composite evidence", "ev", evidence)
  138. blockMeta := evpool.blockStore.LoadBlockMeta(evidence.Height())
  139. if blockMeta == nil {
  140. return fmt.Errorf("don't have block meta at height #%d", evidence.Height())
  141. }
  142. if err := ce.VerifyComposite(&blockMeta.Header, valSet); err != nil {
  143. return err
  144. }
  145. evList = ce.Split(&blockMeta.Header, valSet)
  146. }
  147. for _, ev := range evList {
  148. if evpool.Has(ev) {
  149. // if it is an amnesia evidence we have but POLC is not absent then
  150. // we should still process it
  151. if ae, ok := ev.(*types.AmnesiaEvidence); !ok || ae.Polc.IsAbsent() {
  152. continue
  153. }
  154. }
  155. // A header needs to be fetched. For lunatic evidence this is so we can verify
  156. // that some of the fields are different to the ones we have. For all evidence it
  157. // it so we can verify that the time of the evidence is correct
  158. var header *types.Header
  159. // if the evidence is from the current height - this means the evidence is fresh from the consensus
  160. // and we won't have it in the block store. We thus check that the time isn't before the previous block
  161. if ev.Height() == evpool.State().LastBlockHeight+1 {
  162. if ev.Time().Before(evpool.State().LastBlockTime) {
  163. return fmt.Errorf("evidence is from an earlier time than the previous block: %v < %v",
  164. ev.Time(),
  165. evpool.State().LastBlockTime)
  166. }
  167. header = &types.Header{Time: ev.Time()}
  168. } else { // if the evidence is from a prior height
  169. header = evpool.Header(ev.Height())
  170. if header == nil {
  171. return fmt.Errorf("don't have header at height #%d", ev.Height())
  172. }
  173. }
  174. // 1) Verify against state.
  175. if err := sm.VerifyEvidence(evpool.stateDB, state, ev, header); err != nil {
  176. evpool.logger.Debug("Inbound evidence is invalid", "evidence", ev, "err", err)
  177. return types.NewErrEvidenceInvalid(ev, err)
  178. }
  179. // For potential amnesia evidence, if this node is indicted it shall retrieve a polc
  180. // to form AmensiaEvidence else start the trial period for the piece of evidence
  181. if pe, ok := ev.(*types.PotentialAmnesiaEvidence); ok {
  182. if err := evpool.handleInboundPotentialAmnesiaEvidence(pe); err != nil {
  183. return err
  184. }
  185. continue
  186. } else if ae, ok := ev.(*types.AmnesiaEvidence); ok {
  187. // we have received an new amnesia evidence that we have never seen before so we must extract out the
  188. // potential amnesia evidence part and run our own trial
  189. if ae.Polc.IsAbsent() && ae.PotentialAmnesiaEvidence.VoteA.Round <
  190. ae.PotentialAmnesiaEvidence.VoteB.Round {
  191. if err := evpool.handleInboundPotentialAmnesiaEvidence(ae.PotentialAmnesiaEvidence); err != nil {
  192. return fmt.Errorf("failed to handle amnesia evidence, err: %w", err)
  193. }
  194. continue
  195. } else {
  196. // we are going to add this amnesia evidence as it's already punishable.
  197. // We also check if we already have an amnesia evidence or potential
  198. // amnesia evidence that addesses the same case that we will need to remove
  199. aeWithoutPolc := types.NewAmnesiaEvidence(ae.PotentialAmnesiaEvidence, types.NewEmptyPOLC())
  200. if evpool.IsPending(aeWithoutPolc) {
  201. evpool.removePendingEvidence(aeWithoutPolc)
  202. } else if evpool.IsOnTrial(ae.PotentialAmnesiaEvidence) {
  203. key := keyAwaitingTrial(ae.PotentialAmnesiaEvidence)
  204. if err := evpool.evidenceStore.Delete(key); err != nil {
  205. evpool.logger.Error("Failed to remove potential amnesia evidence from database", "err", err)
  206. }
  207. }
  208. }
  209. }
  210. // 2) Save to store.
  211. if err := evpool.addPendingEvidence(ev); err != nil {
  212. return fmt.Errorf("database error when adding evidence: %v", err)
  213. }
  214. // 3) Add evidence to clist.
  215. evpool.evidenceList.PushBack(ev)
  216. evpool.logger.Info("Verified new evidence of byzantine behavior", "evidence", ev)
  217. }
  218. return nil
  219. }
  220. // MarkEvidenceAsCommitted marks all the evidence as committed and removes it
  221. // from the queue.
  222. func (evpool *Pool) MarkEvidenceAsCommitted(height int64, evidence []types.Evidence) {
  223. // make a map of committed evidence to remove from the clist
  224. blockEvidenceMap := make(map[string]struct{})
  225. for _, ev := range evidence {
  226. // As the evidence is stored in the block store we only need to record the height that it was saved at.
  227. key := keyCommitted(ev)
  228. h := gogotypes.Int64Value{Value: height}
  229. evBytes, err := proto.Marshal(&h)
  230. if err != nil {
  231. panic(err)
  232. }
  233. if err := evpool.evidenceStore.Set(key, evBytes); err != nil {
  234. evpool.logger.Error("Unable to add committed evidence", "err", err)
  235. // if we can't move evidence to committed then don't remove the evidence from pending
  236. continue
  237. }
  238. // if pending, remove from that bucket, remember not all evidence has been seen before
  239. if evpool.IsPending(ev) {
  240. evpool.removePendingEvidence(ev)
  241. blockEvidenceMap[evMapKey(ev)] = struct{}{}
  242. }
  243. }
  244. // remove committed evidence from the clist
  245. if len(blockEvidenceMap) != 0 {
  246. evpool.removeEvidenceFromList(blockEvidenceMap)
  247. }
  248. }
  249. // Has checks whether the evidence exists either pending or already committed
  250. func (evpool *Pool) Has(evidence types.Evidence) bool {
  251. return evpool.IsPending(evidence) || evpool.IsCommitted(evidence) || evpool.IsOnTrial(evidence)
  252. }
  253. // IsEvidenceExpired checks whether evidence is past the maximum age where it can be used
  254. func (evpool *Pool) IsEvidenceExpired(evidence types.Evidence) bool {
  255. return evpool.IsExpired(evidence.Height(), evidence.Time())
  256. }
  257. // IsExpired checks whether evidence or a polc is expired by checking whether a height and time is older
  258. // than set by the evidence consensus parameters
  259. func (evpool *Pool) IsExpired(height int64, time time.Time) bool {
  260. var (
  261. params = evpool.State().ConsensusParams.Evidence
  262. ageDuration = evpool.State().LastBlockTime.Sub(time)
  263. ageNumBlocks = evpool.State().LastBlockHeight - height
  264. )
  265. return ageNumBlocks > params.MaxAgeNumBlocks &&
  266. ageDuration > params.MaxAgeDuration
  267. }
  268. // IsCommitted returns true if we have already seen this exact evidence and it is already marked as committed.
  269. func (evpool *Pool) IsCommitted(evidence types.Evidence) bool {
  270. key := keyCommitted(evidence)
  271. ok, err := evpool.evidenceStore.Has(key)
  272. if err != nil {
  273. evpool.logger.Error("Unable to find committed evidence", "err", err)
  274. }
  275. return ok
  276. }
  277. // IsPending checks whether the evidence is already pending. DB errors are passed to the logger.
  278. func (evpool *Pool) IsPending(evidence types.Evidence) bool {
  279. key := keyPending(evidence)
  280. ok, err := evpool.evidenceStore.Has(key)
  281. if err != nil {
  282. evpool.logger.Error("Unable to find pending evidence", "err", err)
  283. }
  284. return ok
  285. }
  286. // IsOnTrial checks whether a piece of evidence is in the awaiting bucket.
  287. // Only Potential Amnesia Evidence is stored here.
  288. func (evpool *Pool) IsOnTrial(evidence types.Evidence) bool {
  289. pe, ok := evidence.(*types.PotentialAmnesiaEvidence)
  290. if !ok {
  291. return false
  292. }
  293. key := keyAwaitingTrial(pe)
  294. ok, err := evpool.evidenceStore.Has(key)
  295. if err != nil {
  296. evpool.logger.Error("Unable to find evidence on trial", "err", err)
  297. }
  298. return ok
  299. }
  300. // RetrievePOLC attempts to find a polc at the given height and round, if not there than exist returns false, all
  301. // database errors are automatically logged
  302. func (evpool *Pool) RetrievePOLC(height int64, round int32) (*types.ProofOfLockChange, error) {
  303. var pbpolc tmproto.ProofOfLockChange
  304. key := keyPOLCFromHeightAndRound(height, round)
  305. polcBytes, err := evpool.evidenceStore.Get(key)
  306. if err != nil {
  307. evpool.logger.Error("Unable to retrieve polc", "err", err)
  308. return nil, err
  309. }
  310. // polc doesn't exist
  311. if polcBytes == nil {
  312. return nil, nil
  313. }
  314. err = proto.Unmarshal(polcBytes, &pbpolc)
  315. if err != nil {
  316. return nil, err
  317. }
  318. polc, err := types.ProofOfLockChangeFromProto(&pbpolc)
  319. if err != nil {
  320. return nil, err
  321. }
  322. return polc, err
  323. }
  324. // EvidenceFront goes to the first evidence in the clist
  325. func (evpool *Pool) EvidenceFront() *clist.CElement {
  326. return evpool.evidenceList.Front()
  327. }
  328. // EvidenceWaitChan is a channel that closes once the first evidence in the list is there. i.e Front is not nil
  329. func (evpool *Pool) EvidenceWaitChan() <-chan struct{} {
  330. return evpool.evidenceList.WaitChan()
  331. }
  332. // SetLogger sets the Logger.
  333. func (evpool *Pool) SetLogger(l log.Logger) {
  334. evpool.logger = l
  335. }
  336. // Header gets the header from the block store at a specified height.
  337. // Is used for validation of LunaticValidatorEvidence
  338. func (evpool *Pool) Header(height int64) *types.Header {
  339. blockMeta := evpool.blockStore.LoadBlockMeta(height)
  340. if blockMeta == nil {
  341. return nil
  342. }
  343. return &blockMeta.Header
  344. }
  345. // State returns the current state of the evpool.
  346. func (evpool *Pool) State() sm.State {
  347. evpool.mtx.Lock()
  348. defer evpool.mtx.Unlock()
  349. return evpool.state
  350. }
  351. func (evpool *Pool) addPendingEvidence(evidence types.Evidence) error {
  352. evi, err := types.EvidenceToProto(evidence)
  353. if err != nil {
  354. return fmt.Errorf("unable to convert to proto, err: %w", err)
  355. }
  356. evBytes, err := proto.Marshal(evi)
  357. if err != nil {
  358. return fmt.Errorf("unable to marshal evidence: %w", err)
  359. }
  360. key := keyPending(evidence)
  361. return evpool.evidenceStore.Set(key, evBytes)
  362. }
  363. func (evpool *Pool) removePendingEvidence(evidence types.Evidence) {
  364. key := keyPending(evidence)
  365. if err := evpool.evidenceStore.Delete(key); err != nil {
  366. evpool.logger.Error("Unable to delete pending evidence", "err", err)
  367. } else {
  368. evpool.logger.Info("Deleted pending evidence", "evidence", evidence)
  369. }
  370. }
  371. // listEvidence lists up to maxNum pieces of evidence for the given prefix key.
  372. // If maxNum is -1, there's no cap on the size of returned evidence.
  373. func (evpool *Pool) listEvidence(prefixKey byte, maxNum int64) ([]types.Evidence, error) {
  374. var count int64
  375. var evidence []types.Evidence
  376. iter, err := dbm.IteratePrefix(evpool.evidenceStore, []byte{prefixKey})
  377. if err != nil {
  378. return nil, fmt.Errorf("database error: %v", err)
  379. }
  380. defer iter.Close()
  381. for ; iter.Valid(); iter.Next() {
  382. if count == maxNum {
  383. return evidence, nil
  384. }
  385. count++
  386. val := iter.Value()
  387. var (
  388. ev types.Evidence
  389. evpb tmproto.Evidence
  390. )
  391. err := proto.Unmarshal(val, &evpb)
  392. if err != nil {
  393. return nil, err
  394. }
  395. ev, err = types.EvidenceFromProto(&evpb)
  396. if err != nil {
  397. return nil, err
  398. }
  399. evidence = append(evidence, ev)
  400. }
  401. return evidence, nil
  402. }
  403. func (evpool *Pool) removeExpiredPendingEvidence() {
  404. iter, err := dbm.IteratePrefix(evpool.evidenceStore, []byte{baseKeyPending})
  405. if err != nil {
  406. evpool.logger.Error("Unable to iterate over pending evidence", "err", err)
  407. return
  408. }
  409. defer iter.Close()
  410. blockEvidenceMap := make(map[string]struct{})
  411. for ; iter.Valid(); iter.Next() {
  412. evBytes := iter.Value()
  413. var (
  414. ev types.Evidence
  415. evpb tmproto.Evidence
  416. )
  417. err := proto.Unmarshal(evBytes, &evpb)
  418. if err != nil {
  419. evpool.logger.Error("Unable to unmarshal Evidence", "err", err)
  420. continue
  421. }
  422. ev, err = types.EvidenceFromProto(&evpb)
  423. if err != nil {
  424. evpool.logger.Error("Error in transition evidence from protobuf", "err", err)
  425. continue
  426. }
  427. if !evpool.IsExpired(ev.Height()-1, ev.Time()) {
  428. if len(blockEvidenceMap) != 0 {
  429. evpool.removeEvidenceFromList(blockEvidenceMap)
  430. }
  431. return
  432. }
  433. evpool.removePendingEvidence(ev)
  434. blockEvidenceMap[evMapKey(ev)] = struct{}{}
  435. }
  436. }
  437. func (evpool *Pool) removeEvidenceFromList(
  438. blockEvidenceMap map[string]struct{}) {
  439. for e := evpool.evidenceList.Front(); e != nil; e = e.Next() {
  440. // Remove from clist
  441. ev := e.Value.(types.Evidence)
  442. if _, ok := blockEvidenceMap[evMapKey(ev)]; ok {
  443. evpool.evidenceList.Remove(e)
  444. e.DetachPrev()
  445. }
  446. }
  447. }
  448. func (evpool *Pool) pruneExpiredPOLC() {
  449. evpool.logger.Debug("Pruning expired POLC's")
  450. iter, err := dbm.IteratePrefix(evpool.evidenceStore, []byte{baseKeyPOLC})
  451. if err != nil {
  452. evpool.logger.Error("Unable to iterate over POLC's", "err", err)
  453. return
  454. }
  455. defer iter.Close()
  456. for ; iter.Valid(); iter.Next() {
  457. proofBytes := iter.Value()
  458. var (
  459. pbproof tmproto.ProofOfLockChange
  460. )
  461. err := proto.Unmarshal(proofBytes, &pbproof)
  462. if err != nil {
  463. evpool.logger.Error("Unable to unmarshal POLC", "err", err)
  464. continue
  465. }
  466. proof, err := types.ProofOfLockChangeFromProto(&pbproof)
  467. if err != nil {
  468. evpool.logger.Error("Unable to transition POLC from protobuf", "err", err)
  469. continue
  470. }
  471. if !evpool.IsExpired(proof.Height()-1, proof.Time()) {
  472. return
  473. }
  474. err = evpool.evidenceStore.Delete(iter.Key())
  475. if err != nil {
  476. evpool.logger.Error("Unable to delete expired POLC", "err", err)
  477. continue
  478. }
  479. evpool.logger.Info("Deleted expired POLC", "polc", proof)
  480. }
  481. }
  482. func (evpool *Pool) updateState(state sm.State) {
  483. evpool.mtx.Lock()
  484. defer evpool.mtx.Unlock()
  485. evpool.state = state
  486. }
  487. // upgrades any potential evidence that has undergone the trial period and is primed to be made into
  488. // amnesia evidence
  489. func (evpool *Pool) upgradePotentialAmnesiaEvidence() int64 {
  490. iter, err := dbm.IteratePrefix(evpool.evidenceStore, []byte{baseKeyAwaitingTrial})
  491. if err != nil {
  492. evpool.logger.Error("Unable to iterate over POLC's", "err", err)
  493. return -1
  494. }
  495. defer iter.Close()
  496. trialPeriod := evpool.State().ConsensusParams.Evidence.ProofTrialPeriod
  497. currentHeight := evpool.State().LastBlockHeight
  498. // 1) Iterate through all potential amnesia evidence in order of height
  499. for ; iter.Valid(); iter.Next() {
  500. paeBytes := iter.Value()
  501. // 2) Retrieve the evidence
  502. var evpb tmproto.Evidence
  503. err := evpb.Unmarshal(paeBytes)
  504. if err != nil {
  505. evpool.logger.Error("Unable to unmarshal potential amnesia evidence", "err", err)
  506. continue
  507. }
  508. ev, err := types.EvidenceFromProto(&evpb)
  509. if err != nil {
  510. evpool.logger.Error("Converting from proto to evidence", "err", err)
  511. continue
  512. }
  513. // 3) Check if the trial period has lapsed and amnesia evidence can be formed
  514. if pe, ok := ev.(*types.PotentialAmnesiaEvidence); ok {
  515. if pe.Primed(trialPeriod, currentHeight) {
  516. ae := types.NewAmnesiaEvidence(pe, types.NewEmptyPOLC())
  517. err := evpool.addPendingEvidence(ae)
  518. if err != nil {
  519. evpool.logger.Error("Unable to add amnesia evidence", "err", err)
  520. continue
  521. }
  522. evpool.logger.Info("Upgraded to amnesia evidence", "amnesiaEvidence", ae)
  523. err = evpool.evidenceStore.Delete(iter.Key())
  524. if err != nil {
  525. evpool.logger.Error("Unable to delete potential amnesia evidence", "err", err)
  526. continue
  527. }
  528. } else {
  529. evpool.logger.Debug("Potential amnesia evidence is not ready to be upgraded. Ready at", "height",
  530. pe.HeightStamp+trialPeriod, "currentHeight", currentHeight)
  531. // once we reach a piece of evidence that isn't ready send back the height with which it will be ready
  532. return pe.HeightStamp + trialPeriod
  533. }
  534. }
  535. }
  536. // if we have no evidence left to process we want to reset nextEvidenceTrialEndedHeight
  537. return -1
  538. }
  539. func (evpool *Pool) handleInboundPotentialAmnesiaEvidence(pe *types.PotentialAmnesiaEvidence) error {
  540. var (
  541. height = pe.Height()
  542. exists = false
  543. polc *types.ProofOfLockChange
  544. err error
  545. )
  546. evpool.logger.Debug("Received Potential Amnesia Evidence", "pe", pe)
  547. // a) first try to find a corresponding polc
  548. for round := pe.VoteB.Round; round > pe.VoteA.Round; round-- {
  549. polc, err = evpool.RetrievePOLC(height, round)
  550. if err != nil {
  551. evpool.logger.Error("Failed to retrieve polc for potential amnesia evidence", "err", err, "pae", pe.String())
  552. continue
  553. }
  554. if polc != nil && !polc.IsAbsent() {
  555. evpool.logger.Debug("Found polc for potential amnesia evidence", "polc", polc)
  556. // we should not need to verify it if both the polc and potential amnesia evidence have already
  557. // been verified. We replace the potential amnesia evidence.
  558. ae := types.NewAmnesiaEvidence(pe, polc)
  559. err := evpool.AddEvidence(ae)
  560. if err != nil {
  561. evpool.logger.Error("Failed to create amnesia evidence from potential amnesia evidence", "err", err)
  562. // revert back to processing potential amnesia evidence
  563. exists = false
  564. } else {
  565. evpool.logger.Info("Formed amnesia evidence from own polc", "amnesiaEvidence", ae)
  566. }
  567. break
  568. }
  569. }
  570. // stamp height that the evidence was received
  571. pe.HeightStamp = evpool.State().LastBlockHeight
  572. // b) check if amnesia evidence can be made now or if we need to enact the trial period
  573. if !exists && pe.Primed(1, pe.HeightStamp) {
  574. evpool.logger.Debug("PotentialAmnesiaEvidence can be instantly upgraded")
  575. err := evpool.AddEvidence(types.NewAmnesiaEvidence(pe, types.NewEmptyPOLC()))
  576. if err != nil {
  577. return err
  578. }
  579. } else if !exists && evpool.State().LastBlockHeight+evpool.State().ConsensusParams.Evidence.ProofTrialPeriod <
  580. pe.Height()+evpool.State().ConsensusParams.Evidence.MaxAgeNumBlocks {
  581. // if we can't find a proof of lock change and we know that the trial period will finish before the
  582. // evidence has expired, then we commence the trial period by saving it in the awaiting bucket
  583. pbe, err := types.EvidenceToProto(pe)
  584. if err != nil {
  585. return err
  586. }
  587. evBytes, err := pbe.Marshal()
  588. if err != nil {
  589. return err
  590. }
  591. key := keyAwaitingTrial(pe)
  592. err = evpool.evidenceStore.Set(key, evBytes)
  593. if err != nil {
  594. return err
  595. }
  596. evpool.logger.Debug("Valid potential amnesia evidence has been added. Starting trial period",
  597. "ev", pe)
  598. // keep track of when the next pe has finished the trial period
  599. if evpool.nextEvidenceTrialEndedHeight == -1 {
  600. evpool.nextEvidenceTrialEndedHeight = pe.Height() + evpool.State().ConsensusParams.Evidence.ProofTrialPeriod
  601. }
  602. // add to the broadcast list so it can continue to be gossiped
  603. evpool.evidenceList.PushBack(pe)
  604. }
  605. return nil
  606. }
  607. func evMapKey(ev types.Evidence) string {
  608. return string(ev.Hash())
  609. }
  610. // big endian padded hex
  611. func bE(h int64) string {
  612. return fmt.Sprintf("%0.16X", h)
  613. }
  614. func keyCommitted(evidence types.Evidence) []byte {
  615. return append([]byte{baseKeyCommitted}, keySuffix(evidence)...)
  616. }
  617. func keyPending(evidence types.Evidence) []byte {
  618. return append([]byte{baseKeyPending}, keySuffix(evidence)...)
  619. }
  620. func keyAwaitingTrial(evidence types.Evidence) []byte {
  621. return append([]byte{baseKeyAwaitingTrial}, keySuffix(evidence)...)
  622. }
  623. func keyPOLC(polc *types.ProofOfLockChange) []byte {
  624. return keyPOLCFromHeightAndRound(polc.Height(), polc.Round())
  625. }
  626. func keyPOLCFromHeightAndRound(height int64, round int32) []byte {
  627. return append([]byte{baseKeyPOLC}, []byte(fmt.Sprintf("%s/%s", bE(height), bE(int64(round))))...)
  628. }
  629. func keySuffix(evidence types.Evidence) []byte {
  630. return []byte(fmt.Sprintf("%s/%X", bE(evidence.Height()), evidence.Hash()))
  631. }