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.

625 lines
19 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
7 years ago
7 years ago
7 years ago
  1. package evidence
  2. import (
  3. "bytes"
  4. "errors"
  5. "fmt"
  6. "reflect"
  7. "sync"
  8. "sync/atomic"
  9. "time"
  10. "github.com/gogo/protobuf/proto"
  11. gogotypes "github.com/gogo/protobuf/types"
  12. dbm "github.com/tendermint/tm-db"
  13. abci "github.com/tendermint/tendermint/abci/types"
  14. clist "github.com/tendermint/tendermint/libs/clist"
  15. "github.com/tendermint/tendermint/libs/log"
  16. evproto "github.com/tendermint/tendermint/proto/tendermint/evidence"
  17. tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
  18. sm "github.com/tendermint/tendermint/state"
  19. "github.com/tendermint/tendermint/types"
  20. )
  21. const (
  22. baseKeyCommitted = byte(0x00)
  23. baseKeyPending = byte(0x01)
  24. )
  25. // Pool maintains a pool of valid evidence to be broadcasted and committed
  26. type Pool struct {
  27. logger log.Logger
  28. evidenceStore dbm.DB
  29. evidenceList *clist.CList // concurrent linked-list of evidence
  30. evidenceSize uint32 // amount of pending evidence
  31. // needed to load validators to verify evidence
  32. stateDB sm.Store
  33. // needed to load headers and commits to verify evidence
  34. blockStore BlockStore
  35. mtx sync.Mutex
  36. // latest state
  37. state sm.State
  38. pruningHeight int64
  39. pruningTime time.Time
  40. }
  41. // NewPool creates an evidence pool. If using an existing evidence store,
  42. // it will add all pending evidence to the concurrent list.
  43. func NewPool(evidenceDB dbm.DB, stateDB sm.Store, blockStore BlockStore) (*Pool, error) {
  44. state, err := stateDB.Load()
  45. if err != nil {
  46. return nil, fmt.Errorf("cannot load state: %w", err)
  47. }
  48. pool := &Pool{
  49. stateDB: stateDB,
  50. blockStore: blockStore,
  51. state: state,
  52. logger: log.NewNopLogger(),
  53. evidenceStore: evidenceDB,
  54. evidenceList: clist.New(),
  55. }
  56. // if pending evidence already in db, in event of prior failure, then check for expiration,
  57. // update the size and load it back to the evidenceList
  58. pool.pruningHeight, pool.pruningTime = pool.removeExpiredPendingEvidence()
  59. evList, _, err := pool.listEvidence(baseKeyPending, -1)
  60. if err != nil {
  61. return nil, err
  62. }
  63. atomic.StoreUint32(&pool.evidenceSize, uint32(len(evList)))
  64. for _, ev := range evList {
  65. pool.evidenceList.PushBack(ev)
  66. }
  67. return pool, nil
  68. }
  69. // PendingEvidence is used primarily as part of block proposal and returns up to maxNum of uncommitted evidence.
  70. func (evpool *Pool) PendingEvidence(maxBytes int64) ([]types.Evidence, int64) {
  71. if evpool.Size() == 0 {
  72. return []types.Evidence{}, 0
  73. }
  74. evidence, size, err := evpool.listEvidence(baseKeyPending, maxBytes)
  75. if err != nil {
  76. evpool.logger.Error("Unable to retrieve pending evidence", "err", err)
  77. }
  78. return evidence, size
  79. }
  80. // Update pulls the latest state to be used for expiration and evidence params and then prunes all expired evidence
  81. func (evpool *Pool) Update(state sm.State) {
  82. // sanity check
  83. if state.LastBlockHeight <= evpool.state.LastBlockHeight {
  84. panic(fmt.Sprintf(
  85. "Failed EvidencePool.Update new state height is less than or equal to previous state height: %d <= %d",
  86. state.LastBlockHeight,
  87. evpool.state.LastBlockHeight,
  88. ))
  89. }
  90. evpool.logger.Info("Updating evidence pool", "last_block_height", state.LastBlockHeight,
  91. "last_block_time", state.LastBlockTime)
  92. // update the state
  93. evpool.updateState(state)
  94. // prune pending evidence when it has expired. This also updates when the next evidence will expire
  95. if evpool.Size() > 0 && state.LastBlockHeight > evpool.pruningHeight &&
  96. state.LastBlockTime.After(evpool.pruningTime) {
  97. evpool.pruningHeight, evpool.pruningTime = evpool.removeExpiredPendingEvidence()
  98. }
  99. }
  100. // AddEvidence checks the evidence is valid and adds it to the pool.
  101. func (evpool *Pool) AddEvidence(ev types.Evidence) error {
  102. evpool.logger.Debug("Attempting to add evidence", "ev", ev)
  103. // We have already verified this piece of evidence - no need to do it again
  104. if evpool.isPending(ev) {
  105. evpool.logger.Info("Evidence already pending, ignoring this one", "ev", ev)
  106. return nil
  107. }
  108. // check that the evidence isn't already committed
  109. if evpool.isCommitted(ev) {
  110. // this can happen if the peer that sent us the evidence is behind so we shouldn't
  111. // punish the peer.
  112. evpool.logger.Debug("Evidence was already committed, ignoring this one", "ev", ev)
  113. return nil
  114. }
  115. // 1) Verify against state.
  116. evInfo, err := evpool.verify(ev)
  117. if err != nil {
  118. return types.NewErrInvalidEvidence(ev, err)
  119. }
  120. // 2) Save to store.
  121. if err := evpool.addPendingEvidence(evInfo); err != nil {
  122. return fmt.Errorf("can't add evidence to pending list: %w", err)
  123. }
  124. // 3) Add evidence to clist.
  125. evpool.evidenceList.PushBack(ev)
  126. evpool.logger.Info("Verified new evidence of byzantine behavior", "evidence", ev)
  127. return nil
  128. }
  129. // AddEvidenceFromConsensus should be exposed only to the consensus so it can add evidence to the pool
  130. // directly without the need for verification.
  131. func (evpool *Pool) AddEvidenceFromConsensus(ev types.Evidence, time time.Time, valSet *types.ValidatorSet) error {
  132. var (
  133. vals []*types.Validator
  134. totalPower int64
  135. )
  136. // we already have this evidence, log this but don't return an error.
  137. if evpool.isPending(ev) {
  138. evpool.logger.Info("Evidence already pending, ignoring this one", "ev", ev)
  139. return nil
  140. }
  141. switch ev := ev.(type) {
  142. case *types.DuplicateVoteEvidence:
  143. _, val := valSet.GetByAddress(ev.VoteA.ValidatorAddress)
  144. vals = append(vals, val)
  145. totalPower = valSet.TotalVotingPower()
  146. default:
  147. return fmt.Errorf("unrecognized evidence type: %T", ev)
  148. }
  149. evInfo := &info{
  150. Evidence: ev,
  151. Time: time,
  152. Validators: vals,
  153. TotalVotingPower: totalPower,
  154. }
  155. if err := evpool.addPendingEvidence(evInfo); err != nil {
  156. return fmt.Errorf("can't add evidence to pending list: %w", err)
  157. }
  158. // add evidence to be gossiped with peers
  159. evpool.evidenceList.PushBack(ev)
  160. evpool.logger.Info("Verified new evidence of byzantine behavior", "evidence", ev)
  161. return nil
  162. }
  163. // CheckEvidence takes an array of evidence from a block and verifies all the evidence there.
  164. // If it has already verified the evidence then it jumps to the next one. It ensures that no
  165. // evidence has already been committed or is being proposed twice. It also adds any
  166. // evidence that it doesn't currently have so that it can quickly form ABCI Evidence later.
  167. func (evpool *Pool) CheckEvidence(evList types.EvidenceList) error {
  168. hashes := make([][]byte, len(evList))
  169. for idx, ev := range evList {
  170. ok := evpool.fastCheck(ev)
  171. if !ok {
  172. // check that the evidence isn't already committed
  173. if evpool.isCommitted(ev) {
  174. return &types.ErrInvalidEvidence{Evidence: ev, Reason: errors.New("evidence was already committed")}
  175. }
  176. evInfo, err := evpool.verify(ev)
  177. if err != nil {
  178. return &types.ErrInvalidEvidence{Evidence: ev, Reason: err}
  179. }
  180. if err := evpool.addPendingEvidence(evInfo); err != nil {
  181. // Something went wrong with adding the evidence but we already know it is valid
  182. // hence we log an error and continue
  183. evpool.logger.Error("Can't add evidence to pending list", "err", err, "evInfo", evInfo)
  184. }
  185. evpool.logger.Info("Verified new evidence of byzantine behavior", "evidence", ev)
  186. }
  187. // check for duplicate evidence. We cache hashes so we don't have to work them out again.
  188. hashes[idx] = ev.Hash()
  189. for i := idx - 1; i >= 0; i-- {
  190. if bytes.Equal(hashes[i], hashes[idx]) {
  191. return &types.ErrInvalidEvidence{Evidence: ev, Reason: errors.New("duplicate evidence")}
  192. }
  193. }
  194. }
  195. return nil
  196. }
  197. // ABCIEvidence processes all the evidence in the block, marking it as committed and removing it
  198. // from the pending database. It then forms the individual abci evidence that will be passed back to
  199. // the application.
  200. func (evpool *Pool) ABCIEvidence(height int64, evidence []types.Evidence) []abci.Evidence {
  201. // make a map of committed evidence to remove from the clist
  202. blockEvidenceMap := make(map[string]struct{}, len(evidence))
  203. abciEvidence := make([]abci.Evidence, 0)
  204. for _, ev := range evidence {
  205. // get entire evidence info from pending list
  206. infoBytes, err := evpool.evidenceStore.Get(keyPending(ev))
  207. if err != nil {
  208. evpool.logger.Error("Unable to retrieve evidence to pass to ABCI. "+
  209. "Evidence pool should have seen this evidence before",
  210. "evidence", ev, "err", err)
  211. continue
  212. }
  213. var infoProto evproto.Info
  214. err = infoProto.Unmarshal(infoBytes)
  215. if err != nil {
  216. evpool.logger.Error("Decoding evidence info failed", "err", err, "height", ev.Height(), "hash", ev.Hash())
  217. continue
  218. }
  219. evInfo, err := infoFromProto(&infoProto)
  220. if err != nil {
  221. evpool.logger.Error("Converting evidence info from proto failed", "err", err, "height", ev.Height(),
  222. "hash", ev.Hash())
  223. continue
  224. }
  225. var evType abci.EvidenceType
  226. switch ev.(type) {
  227. case *types.DuplicateVoteEvidence:
  228. evType = abci.EvidenceType_DUPLICATE_VOTE
  229. case *types.LightClientAttackEvidence:
  230. evType = abci.EvidenceType_LIGHT_CLIENT_ATTACK
  231. default:
  232. evpool.logger.Error("Unknown evidence type", "T", reflect.TypeOf(ev))
  233. continue
  234. }
  235. for _, val := range evInfo.Validators {
  236. abciEv := abci.Evidence{
  237. Type: evType,
  238. Validator: types.TM2PB.Validator(val),
  239. Height: ev.Height(),
  240. Time: evInfo.Time,
  241. TotalVotingPower: evInfo.TotalVotingPower,
  242. }
  243. abciEvidence = append(abciEvidence, abciEv)
  244. evpool.logger.Info("Created ABCI evidence", "ev", abciEv)
  245. }
  246. // we can now remove the evidence from the pending list and the clist that we use for gossiping
  247. evpool.removePendingEvidence(ev)
  248. blockEvidenceMap[evMapKey(ev)] = struct{}{}
  249. // Add evidence to the committed list
  250. // As the evidence is stored in the block store we only need to record the height that it was saved at.
  251. key := keyCommitted(ev)
  252. h := gogotypes.Int64Value{Value: height}
  253. evBytes, err := proto.Marshal(&h)
  254. if err != nil {
  255. panic(err)
  256. }
  257. if err := evpool.evidenceStore.Set(key, evBytes); err != nil {
  258. evpool.logger.Error("Unable to add committed evidence", "err", err)
  259. }
  260. }
  261. // remove committed evidence from the clist
  262. if len(blockEvidenceMap) != 0 {
  263. evpool.removeEvidenceFromList(blockEvidenceMap)
  264. }
  265. return abciEvidence
  266. }
  267. // EvidenceFront goes to the first evidence in the clist
  268. func (evpool *Pool) EvidenceFront() *clist.CElement {
  269. return evpool.evidenceList.Front()
  270. }
  271. // EvidenceWaitChan is a channel that closes once the first evidence in the list is there. i.e Front is not nil
  272. func (evpool *Pool) EvidenceWaitChan() <-chan struct{} {
  273. return evpool.evidenceList.WaitChan()
  274. }
  275. // SetLogger sets the Logger.
  276. func (evpool *Pool) SetLogger(l log.Logger) {
  277. evpool.logger = l
  278. }
  279. func (evpool *Pool) Size() uint32 {
  280. return atomic.LoadUint32(&evpool.evidenceSize)
  281. }
  282. // State returns the current state of the evpool.
  283. func (evpool *Pool) State() sm.State {
  284. evpool.mtx.Lock()
  285. defer evpool.mtx.Unlock()
  286. return evpool.state
  287. }
  288. //--------------------------------------------------------------------------
  289. // Info is a wrapper around the evidence that the evidence pool receives with extensive
  290. // information of what validators were malicious, the time of the attack and the total voting power
  291. // This is saved as a form of cache so that the evidence pool can easily produce the ABCI Evidence
  292. // needed to be sent to the application.
  293. type info struct {
  294. Evidence types.Evidence
  295. Time time.Time
  296. Validators []*types.Validator
  297. TotalVotingPower int64
  298. ByteSize int64
  299. }
  300. // ToProto encodes into protobuf
  301. func (ei info) ToProto() (*evproto.Info, error) {
  302. evpb, err := types.EvidenceToProto(ei.Evidence)
  303. if err != nil {
  304. return nil, err
  305. }
  306. valsProto := make([]*tmproto.Validator, len(ei.Validators))
  307. for i := 0; i < len(ei.Validators); i++ {
  308. valp, err := ei.Validators[i].ToProto()
  309. if err != nil {
  310. return nil, err
  311. }
  312. valsProto[i] = valp
  313. }
  314. return &evproto.Info{
  315. Evidence: *evpb,
  316. Time: ei.Time,
  317. Validators: valsProto,
  318. TotalVotingPower: ei.TotalVotingPower,
  319. }, nil
  320. }
  321. // InfoFromProto decodes from protobuf into Info
  322. func infoFromProto(proto *evproto.Info) (info, error) {
  323. if proto == nil {
  324. return info{}, errors.New("nil evidence info")
  325. }
  326. ev, err := types.EvidenceFromProto(&proto.Evidence)
  327. if err != nil {
  328. return info{}, err
  329. }
  330. vals := make([]*types.Validator, len(proto.Validators))
  331. for i := 0; i < len(proto.Validators); i++ {
  332. val, err := types.ValidatorFromProto(proto.Validators[i])
  333. if err != nil {
  334. return info{}, err
  335. }
  336. vals[i] = val
  337. }
  338. return info{
  339. Evidence: ev,
  340. Time: proto.Time,
  341. Validators: vals,
  342. TotalVotingPower: proto.TotalVotingPower,
  343. ByteSize: int64(proto.Evidence.Size()),
  344. }, nil
  345. }
  346. //--------------------------------------------------------------------------
  347. // fastCheck leverages the fact that the evidence pool may have already verified the evidence to see if it can
  348. // quickly conclude that the evidence is already valid.
  349. func (evpool *Pool) fastCheck(ev types.Evidence) bool {
  350. key := keyPending(ev)
  351. if lcae, ok := ev.(*types.LightClientAttackEvidence); ok {
  352. evBytes, err := evpool.evidenceStore.Get(key)
  353. if evBytes == nil { // the evidence is not in the nodes pending list
  354. return false
  355. }
  356. if err != nil {
  357. evpool.logger.Error("Failed to load evidence", "err", err, "evidence", lcae)
  358. return false
  359. }
  360. evInfo, err := bytesToInfo(evBytes)
  361. if err != nil {
  362. evpool.logger.Error("Failed to convert evidence from proto", "err", err, "evidence", lcae)
  363. return false
  364. }
  365. // ensure that all the validators that the evidence pool have found to be malicious
  366. // are present in the list of commit signatures in the conflicting block
  367. OUTER:
  368. for _, sig := range lcae.ConflictingBlock.Commit.Signatures {
  369. for _, val := range evInfo.Validators {
  370. if bytes.Equal(val.Address, sig.ValidatorAddress) {
  371. continue OUTER
  372. }
  373. }
  374. // a validator we know is malicious is not included in the commit
  375. evpool.logger.Info("Fast check failed: a validator we know is malicious is not " +
  376. "in the commit sigs. Reverting to full verification")
  377. return false
  378. }
  379. return true
  380. }
  381. // for all other evidence the evidence pool just checks if it is already in the pending db
  382. return evpool.isPending(ev)
  383. }
  384. // IsExpired checks whether evidence or a polc is expired by checking whether a height and time is older
  385. // than set by the evidence consensus parameters
  386. func (evpool *Pool) isExpired(height int64, time time.Time) bool {
  387. var (
  388. params = evpool.State().ConsensusParams.Evidence
  389. ageDuration = evpool.State().LastBlockTime.Sub(time)
  390. ageNumBlocks = evpool.State().LastBlockHeight - height
  391. )
  392. return ageNumBlocks > params.MaxAgeNumBlocks &&
  393. ageDuration > params.MaxAgeDuration
  394. }
  395. // IsCommitted returns true if we have already seen this exact evidence and it is already marked as committed.
  396. func (evpool *Pool) isCommitted(evidence types.Evidence) bool {
  397. key := keyCommitted(evidence)
  398. ok, err := evpool.evidenceStore.Has(key)
  399. if err != nil {
  400. evpool.logger.Error("Unable to find committed evidence", "err", err)
  401. }
  402. return ok
  403. }
  404. // IsPending checks whether the evidence is already pending. DB errors are passed to the logger.
  405. func (evpool *Pool) isPending(evidence types.Evidence) bool {
  406. key := keyPending(evidence)
  407. ok, err := evpool.evidenceStore.Has(key)
  408. if err != nil {
  409. evpool.logger.Error("Unable to find pending evidence", "err", err)
  410. }
  411. return ok
  412. }
  413. func (evpool *Pool) addPendingEvidence(evInfo *info) error {
  414. evpb, err := evInfo.ToProto()
  415. if err != nil {
  416. return fmt.Errorf("unable to convert to proto, err: %w", err)
  417. }
  418. evBytes, err := evpb.Marshal()
  419. if err != nil {
  420. return fmt.Errorf("unable to marshal evidence: %w", err)
  421. }
  422. key := keyPending(evInfo.Evidence)
  423. err = evpool.evidenceStore.Set(key, evBytes)
  424. if err != nil {
  425. return fmt.Errorf("can't persist evidence: %w", err)
  426. }
  427. atomic.AddUint32(&evpool.evidenceSize, 1)
  428. return nil
  429. }
  430. func (evpool *Pool) removePendingEvidence(evidence types.Evidence) {
  431. key := keyPending(evidence)
  432. if err := evpool.evidenceStore.Delete(key); err != nil {
  433. evpool.logger.Error("Unable to delete pending evidence", "err", err)
  434. } else {
  435. atomic.AddUint32(&evpool.evidenceSize, ^uint32(0))
  436. evpool.logger.Info("Deleted pending evidence", "evidence", evidence)
  437. }
  438. }
  439. // listEvidence retrieves lists evidence from oldest to newest within maxBytes.
  440. // If maxBytes is -1, there's no cap on the size of returned evidence.
  441. func (evpool *Pool) listEvidence(prefixKey byte, maxBytes int64) ([]types.Evidence, int64, error) {
  442. var totalSize int64
  443. var evidence []types.Evidence
  444. iter, err := dbm.IteratePrefix(evpool.evidenceStore, []byte{prefixKey})
  445. if err != nil {
  446. return nil, totalSize, fmt.Errorf("database error: %v", err)
  447. }
  448. defer iter.Close()
  449. for ; iter.Valid(); iter.Next() {
  450. evInfo, err := bytesToInfo(iter.Value())
  451. if err != nil {
  452. return nil, totalSize, err
  453. }
  454. totalSize += evInfo.ByteSize
  455. if maxBytes != -1 && totalSize > maxBytes {
  456. return evidence, totalSize - evInfo.ByteSize, nil
  457. }
  458. evidence = append(evidence, evInfo.Evidence)
  459. }
  460. return evidence, totalSize, nil
  461. }
  462. func (evpool *Pool) removeExpiredPendingEvidence() (int64, time.Time) {
  463. iter, err := dbm.IteratePrefix(evpool.evidenceStore, []byte{baseKeyPending})
  464. if err != nil {
  465. evpool.logger.Error("Unable to iterate over pending evidence", "err", err)
  466. return evpool.State().LastBlockHeight, evpool.State().LastBlockTime
  467. }
  468. defer iter.Close()
  469. blockEvidenceMap := make(map[string]struct{})
  470. for ; iter.Valid(); iter.Next() {
  471. evInfo, err := bytesToInfo(iter.Value())
  472. if err != nil {
  473. evpool.logger.Error("Error in transition evidence from protobuf", "err", err)
  474. continue
  475. }
  476. if !evpool.isExpired(evInfo.Evidence.Height(), evInfo.Time) {
  477. if len(blockEvidenceMap) != 0 {
  478. evpool.removeEvidenceFromList(blockEvidenceMap)
  479. }
  480. // return the height and time with which this evidence will have expired so we know when to prune next
  481. return evInfo.Evidence.Height() + evpool.State().ConsensusParams.Evidence.MaxAgeNumBlocks + 1,
  482. evInfo.Time.Add(evpool.State().ConsensusParams.Evidence.MaxAgeDuration).Add(time.Second)
  483. }
  484. evpool.removePendingEvidence(evInfo.Evidence)
  485. blockEvidenceMap[evMapKey(evInfo.Evidence)] = struct{}{}
  486. }
  487. // We either have no pending evidence or all evidence has expired
  488. if len(blockEvidenceMap) != 0 {
  489. evpool.removeEvidenceFromList(blockEvidenceMap)
  490. }
  491. return evpool.State().LastBlockHeight, evpool.State().LastBlockTime
  492. }
  493. func (evpool *Pool) removeEvidenceFromList(
  494. blockEvidenceMap map[string]struct{}) {
  495. for e := evpool.evidenceList.Front(); e != nil; e = e.Next() {
  496. // Remove from clist
  497. ev := e.Value.(types.Evidence)
  498. if _, ok := blockEvidenceMap[evMapKey(ev)]; ok {
  499. evpool.evidenceList.Remove(e)
  500. e.DetachPrev()
  501. }
  502. }
  503. }
  504. func (evpool *Pool) updateState(state sm.State) {
  505. evpool.mtx.Lock()
  506. defer evpool.mtx.Unlock()
  507. evpool.state = state
  508. }
  509. func bytesToInfo(evBytes []byte) (info, error) {
  510. var evpb evproto.Info
  511. err := evpb.Unmarshal(evBytes)
  512. if err != nil {
  513. return info{}, err
  514. }
  515. return infoFromProto(&evpb)
  516. }
  517. func evMapKey(ev types.Evidence) string {
  518. return string(ev.Hash())
  519. }
  520. // big endian padded hex
  521. func bE(h int64) string {
  522. return fmt.Sprintf("%0.16X", h)
  523. }
  524. func keyCommitted(evidence types.Evidence) []byte {
  525. return append([]byte{baseKeyCommitted}, keySuffix(evidence)...)
  526. }
  527. func keyPending(evidence types.Evidence) []byte {
  528. return append([]byte{baseKeyPending}, keySuffix(evidence)...)
  529. }
  530. func keySuffix(evidence types.Evidence) []byte {
  531. return []byte(fmt.Sprintf("%s/%X", bE(evidence.Height()), evidence.Hash()))
  532. }