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.

78 lines
2.4 KiB

cleanup: Reduce and normalize import path aliasing. (#6975) The code in the Tendermint repository makes heavy use of import aliasing. This is made necessary by our extensive reuse of common base package names, and by repetition of similar names across different subdirectories. Unfortunately we have not been very consistent about which packages we alias in various circumstances, and the aliases we use vary. In the spirit of the advice in the style guide and https://github.com/golang/go/wiki/CodeReviewComments#imports, his change makes an effort to clean up and normalize import aliasing. This change makes no API or behavioral changes. It is a pure cleanup intended o help make the code more readable to developers (including myself) trying to understand what is being imported where. Only unexported names have been modified, and the changes were generated and applied mechanically with gofmt -r and comby, respecting the lexical and syntactic rules of Go. Even so, I did not fix every inconsistency. Where the changes would be too disruptive, I left it alone. The principles I followed in this cleanup are: - Remove aliases that restate the package name. - Remove aliases where the base package name is unambiguous. - Move overly-terse abbreviations from the import to the usage site. - Fix lexical issues (remove underscores, remove capitalization). - Fix import groupings to more closely match the style guide. - Group blank (side-effecting) imports and ensure they are commented. - Add aliases to multiple imports with the same base package name.
3 years ago
  1. package kvstore
  2. import (
  3. "bytes"
  4. dbm "github.com/tendermint/tm-db"
  5. "github.com/tendermint/tendermint/abci/types"
  6. "github.com/tendermint/tendermint/libs/log"
  7. cryptoproto "github.com/tendermint/tendermint/proto/tendermint/crypto"
  8. ptypes "github.com/tendermint/tendermint/proto/tendermint/types"
  9. )
  10. const (
  11. ValidatorSetChangePrefix string = "val:"
  12. )
  13. //-----------------------------------------
  14. var _ types.Application = (*PersistentKVStoreApplication)(nil)
  15. type PersistentKVStoreApplication struct {
  16. *Application
  17. }
  18. func NewPersistentKVStoreApplication(logger log.Logger, dbDir string) *PersistentKVStoreApplication {
  19. db, err := dbm.NewGoLevelDB("kvstore", dbDir)
  20. if err != nil {
  21. panic(err)
  22. }
  23. return &PersistentKVStoreApplication{
  24. Application: &Application{
  25. valAddrToPubKeyMap: make(map[string]cryptoproto.PublicKey),
  26. state: loadState(db),
  27. logger: logger,
  28. },
  29. }
  30. }
  31. func (app *PersistentKVStoreApplication) OfferSnapshot(req types.RequestOfferSnapshot) types.ResponseOfferSnapshot {
  32. return types.ResponseOfferSnapshot{Result: types.ResponseOfferSnapshot_ABORT}
  33. }
  34. func (app *PersistentKVStoreApplication) ApplySnapshotChunk(req types.RequestApplySnapshotChunk) types.ResponseApplySnapshotChunk {
  35. return types.ResponseApplySnapshotChunk{Result: types.ResponseApplySnapshotChunk_ABORT}
  36. }
  37. func (app *PersistentKVStoreApplication) ExtendVote(req types.RequestExtendVote) types.ResponseExtendVote {
  38. return types.ResponseExtendVote{VoteExtension: ConstructVoteExtension(req.Vote.ValidatorAddress)}
  39. }
  40. func (app *PersistentKVStoreApplication) VerifyVoteExtension(req types.RequestVerifyVoteExtension) types.ResponseVerifyVoteExtension {
  41. return types.RespondVerifyVoteExtension(app.verifyExtension(req.Vote.ValidatorAddress, req.Vote.VoteExtension))
  42. }
  43. // -----------------------------
  44. func ConstructVoteExtension(valAddr []byte) *ptypes.VoteExtension {
  45. return &ptypes.VoteExtension{
  46. AppDataToSign: valAddr,
  47. AppDataSelfAuthenticating: valAddr,
  48. }
  49. }
  50. func (app *PersistentKVStoreApplication) verifyExtension(valAddr []byte, ext *ptypes.VoteExtension) bool {
  51. if ext == nil {
  52. return false
  53. }
  54. canonical := ConstructVoteExtension(valAddr)
  55. if !bytes.Equal(canonical.AppDataToSign, ext.AppDataToSign) {
  56. return false
  57. }
  58. if !bytes.Equal(canonical.AppDataSelfAuthenticating, ext.AppDataSelfAuthenticating) {
  59. return false
  60. }
  61. return true
  62. }