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.

190 lines
5.3 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. package evidence
  2. import (
  3. "fmt"
  4. "github.com/tendermint/tendermint/types"
  5. dbm "github.com/tendermint/tmlibs/db"
  6. )
  7. /*
  8. Requirements:
  9. - Valid new evidence must be persisted immediately and never forgotten
  10. - Uncommitted evidence must be continuously broadcast
  11. - Uncommitted evidence has a partial order, the evidence's priority
  12. Impl:
  13. - First commit atomically in outqueue, pending, lookup.
  14. - Once broadcast, remove from outqueue. No need to sync
  15. - Once committed, atomically remove from pending and update lookup.
  16. Schema for indexing evidence (note you need both height and hash to find a piece of evidence):
  17. "evidence-lookup"/<evidence-height>/<evidence-hash> -> EvidenceInfo
  18. "evidence-outqueue"/<priority>/<evidence-height>/<evidence-hash> -> EvidenceInfo
  19. "evidence-pending"/<evidence-height>/<evidence-hash> -> EvidenceInfo
  20. */
  21. type EvidenceInfo struct {
  22. Committed bool
  23. Priority int64
  24. Evidence types.Evidence
  25. }
  26. const (
  27. baseKeyLookup = "evidence-lookup" // all evidence
  28. baseKeyOutqueue = "evidence-outqueue" // not-yet broadcast
  29. baseKeyPending = "evidence-pending" // broadcast but not committed
  30. )
  31. func keyLookup(evidence types.Evidence) []byte {
  32. return keyLookupFromHeightAndHash(evidence.Height(), evidence.Hash())
  33. }
  34. // big endian padded hex
  35. func bE(h int64) string {
  36. return fmt.Sprintf("%0.16X", h)
  37. }
  38. func keyLookupFromHeightAndHash(height int64, hash []byte) []byte {
  39. return _key("%s/%s/%X", baseKeyLookup, bE(height), hash)
  40. }
  41. func keyOutqueue(evidence types.Evidence, priority int64) []byte {
  42. return _key("%s/%s/%s/%X", baseKeyOutqueue, bE(priority), bE(evidence.Height()), evidence.Hash())
  43. }
  44. func keyPending(evidence types.Evidence) []byte {
  45. return _key("%s/%s/%X", baseKeyPending, bE(evidence.Height()), evidence.Hash())
  46. }
  47. func _key(fmt_ string, o ...interface{}) []byte {
  48. return []byte(fmt.Sprintf(fmt_, o...))
  49. }
  50. // EvidenceStore is a store of all the evidence we've seen, including
  51. // evidence that has been committed, evidence that has been verified but not broadcast,
  52. // and evidence that has been broadcast but not yet committed.
  53. type EvidenceStore struct {
  54. db dbm.DB
  55. }
  56. func NewEvidenceStore(db dbm.DB) *EvidenceStore {
  57. return &EvidenceStore{
  58. db: db,
  59. }
  60. }
  61. // PriorityEvidence returns the evidence from the outqueue, sorted by highest priority.
  62. func (store *EvidenceStore) PriorityEvidence() (evidence []types.Evidence) {
  63. // reverse the order so highest priority is first
  64. l := store.ListEvidence(baseKeyOutqueue)
  65. l2 := make([]types.Evidence, len(l))
  66. for i := range l {
  67. l2[i] = l[len(l)-1-i]
  68. }
  69. return l2
  70. }
  71. // PendingEvidence returns all known uncommitted evidence.
  72. func (store *EvidenceStore) PendingEvidence() (evidence []types.Evidence) {
  73. return store.ListEvidence(baseKeyPending)
  74. }
  75. // ListEvidence lists the evidence for the given prefix key.
  76. // It is wrapped by PriorityEvidence and PendingEvidence for convenience.
  77. func (store *EvidenceStore) ListEvidence(prefixKey string) (evidence []types.Evidence) {
  78. iter := dbm.IteratePrefix(store.db, []byte(prefixKey))
  79. for ; iter.Valid(); iter.Next() {
  80. val := iter.Value()
  81. var ei EvidenceInfo
  82. err := cdc.UnmarshalBinaryBare(val, &ei)
  83. if err != nil {
  84. panic(err)
  85. }
  86. evidence = append(evidence, ei.Evidence)
  87. }
  88. return evidence
  89. }
  90. // GetEvidence fetches the evidence with the given height and hash.
  91. func (store *EvidenceStore) GetEvidence(height int64, hash []byte) *EvidenceInfo {
  92. key := keyLookupFromHeightAndHash(height, hash)
  93. val := store.db.Get(key)
  94. if len(val) == 0 {
  95. return nil
  96. }
  97. var ei EvidenceInfo
  98. err := cdc.UnmarshalBinaryBare(val, &ei)
  99. if err != nil {
  100. panic(err)
  101. }
  102. return &ei
  103. }
  104. // AddNewEvidence adds the given evidence to the database.
  105. // It returns false if the evidence is already stored.
  106. func (store *EvidenceStore) AddNewEvidence(evidence types.Evidence, priority int64) bool {
  107. // check if we already have seen it
  108. ei_ := store.GetEvidence(evidence.Height(), evidence.Hash())
  109. if ei_ != nil && ei_.Evidence != nil {
  110. return false
  111. }
  112. ei := EvidenceInfo{
  113. Committed: false,
  114. Priority: priority,
  115. Evidence: evidence,
  116. }
  117. eiBytes := cdc.MustMarshalBinaryBare(ei)
  118. // add it to the store
  119. key := keyOutqueue(evidence, priority)
  120. store.db.Set(key, eiBytes)
  121. key = keyPending(evidence)
  122. store.db.Set(key, eiBytes)
  123. key = keyLookup(evidence)
  124. store.db.SetSync(key, eiBytes)
  125. return true
  126. }
  127. // MarkEvidenceAsBroadcasted removes evidence from Outqueue.
  128. func (store *EvidenceStore) MarkEvidenceAsBroadcasted(evidence types.Evidence) {
  129. ei := store.getEvidenceInfo(evidence)
  130. key := keyOutqueue(evidence, ei.Priority)
  131. store.db.Delete(key)
  132. }
  133. // MarkEvidenceAsCommitted removes evidence from pending and outqueue and sets the state to committed.
  134. func (store *EvidenceStore) MarkEvidenceAsCommitted(evidence types.Evidence) {
  135. // if its committed, its been broadcast
  136. store.MarkEvidenceAsBroadcasted(evidence)
  137. pendingKey := keyPending(evidence)
  138. store.db.Delete(pendingKey)
  139. ei := store.getEvidenceInfo(evidence)
  140. ei.Committed = true
  141. lookupKey := keyLookup(evidence)
  142. store.db.SetSync(lookupKey, cdc.MustMarshalBinaryBare(ei))
  143. }
  144. //---------------------------------------------------
  145. // utils
  146. func (store *EvidenceStore) getEvidenceInfo(evidence types.Evidence) EvidenceInfo {
  147. key := keyLookup(evidence)
  148. var ei EvidenceInfo
  149. b := store.db.Get(key)
  150. err := cdc.UnmarshalBinaryBare(b, &ei)
  151. if err != nil {
  152. panic(err)
  153. }
  154. return ei
  155. }