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.

199 lines
5.6 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. package evidence
  2. import (
  3. "fmt"
  4. dbm "github.com/tendermint/tendermint/libs/db"
  5. "github.com/tendermint/tendermint/types"
  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, -1)
  65. for i, j := 0, len(l)-1; i < j; i, j = i+1, j-1 {
  66. l[i], l[j] = l[j], l[i]
  67. }
  68. return l
  69. }
  70. // PendingEvidence returns known uncommitted evidence up to maxBytes.
  71. // If maxBytes is -1, all evidence is returned.
  72. func (store *EvidenceStore) PendingEvidence(maxBytes int64) (evidence []types.Evidence) {
  73. return store.listEvidence(baseKeyPending, maxBytes)
  74. }
  75. // listEvidence lists the evidence for the given prefix key up to maxBytes.
  76. // It is wrapped by PriorityEvidence and PendingEvidence for convenience.
  77. // If maxBytes is -1, there's no cap on the size of returned evidence.
  78. func (store *EvidenceStore) listEvidence(prefixKey string, maxBytes int64) (evidence []types.Evidence) {
  79. var bytes int64
  80. iter := dbm.IteratePrefix(store.db, []byte(prefixKey))
  81. defer iter.Close()
  82. for ; iter.Valid(); iter.Next() {
  83. val := iter.Value()
  84. if maxBytes > 0 && bytes+int64(len(val)) > maxBytes {
  85. return evidence
  86. }
  87. bytes += int64(len(val))
  88. var ei EvidenceInfo
  89. err := cdc.UnmarshalBinaryBare(val, &ei)
  90. if err != nil {
  91. panic(err)
  92. }
  93. evidence = append(evidence, ei.Evidence)
  94. }
  95. return evidence
  96. }
  97. // GetEvidence fetches the evidence with the given height and hash.
  98. func (store *EvidenceStore) GetEvidence(height int64, hash []byte) *EvidenceInfo {
  99. key := keyLookupFromHeightAndHash(height, hash)
  100. val := store.db.Get(key)
  101. if len(val) == 0 {
  102. return nil
  103. }
  104. var ei EvidenceInfo
  105. err := cdc.UnmarshalBinaryBare(val, &ei)
  106. if err != nil {
  107. panic(err)
  108. }
  109. return &ei
  110. }
  111. // AddNewEvidence adds the given evidence to the database.
  112. // It returns false if the evidence is already stored.
  113. func (store *EvidenceStore) AddNewEvidence(evidence types.Evidence, priority int64) bool {
  114. // check if we already have seen it
  115. ei_ := store.GetEvidence(evidence.Height(), evidence.Hash())
  116. if ei_ != nil && ei_.Evidence != nil {
  117. return false
  118. }
  119. ei := EvidenceInfo{
  120. Committed: false,
  121. Priority: priority,
  122. Evidence: evidence,
  123. }
  124. eiBytes := cdc.MustMarshalBinaryBare(ei)
  125. // add it to the store
  126. key := keyOutqueue(evidence, priority)
  127. store.db.Set(key, eiBytes)
  128. key = keyPending(evidence)
  129. store.db.Set(key, eiBytes)
  130. key = keyLookup(evidence)
  131. store.db.SetSync(key, eiBytes)
  132. return true
  133. }
  134. // MarkEvidenceAsBroadcasted removes evidence from Outqueue.
  135. func (store *EvidenceStore) MarkEvidenceAsBroadcasted(evidence types.Evidence) {
  136. ei := store.getEvidenceInfo(evidence)
  137. key := keyOutqueue(evidence, ei.Priority)
  138. store.db.Delete(key)
  139. }
  140. // MarkEvidenceAsCommitted removes evidence from pending and outqueue and sets the state to committed.
  141. func (store *EvidenceStore) MarkEvidenceAsCommitted(evidence types.Evidence) {
  142. // if its committed, its been broadcast
  143. store.MarkEvidenceAsBroadcasted(evidence)
  144. pendingKey := keyPending(evidence)
  145. store.db.Delete(pendingKey)
  146. ei := store.getEvidenceInfo(evidence)
  147. ei.Committed = true
  148. lookupKey := keyLookup(evidence)
  149. store.db.SetSync(lookupKey, cdc.MustMarshalBinaryBare(ei))
  150. }
  151. //---------------------------------------------------
  152. // utils
  153. func (store *EvidenceStore) getEvidenceInfo(evidence types.Evidence) EvidenceInfo {
  154. key := keyLookup(evidence)
  155. var ei EvidenceInfo
  156. b := store.db.Get(key)
  157. err := cdc.UnmarshalBinaryBare(b, &ei)
  158. if err != nil {
  159. panic(err)
  160. }
  161. return ei
  162. }