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.

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