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.

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