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.

286 lines
8.2 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
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
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
8 years ago
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
8 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
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
8 years ago
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
7 years ago
7 years ago
7 years ago
7 years ago
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
7 years ago
7 years ago
  1. package kvstore
  2. import (
  3. "bytes"
  4. "encoding/base64"
  5. "fmt"
  6. "strconv"
  7. "strings"
  8. dbm "github.com/tendermint/tm-db"
  9. "github.com/tendermint/tendermint/abci/example/code"
  10. "github.com/tendermint/tendermint/abci/types"
  11. "github.com/tendermint/tendermint/crypto/encoding"
  12. "github.com/tendermint/tendermint/libs/log"
  13. cryptoproto "github.com/tendermint/tendermint/proto/tendermint/crypto"
  14. )
  15. const (
  16. ValidatorSetChangePrefix string = "val:"
  17. )
  18. //-----------------------------------------
  19. var _ types.Application = (*PersistentKVStoreApplication)(nil)
  20. type PersistentKVStoreApplication struct {
  21. app *Application
  22. // validator set
  23. ValUpdates []types.ValidatorUpdate
  24. valAddrToPubKeyMap map[string]cryptoproto.PublicKey
  25. logger log.Logger
  26. }
  27. func NewPersistentKVStoreApplication(dbDir string) *PersistentKVStoreApplication {
  28. name := "kvstore"
  29. db, err := dbm.NewGoLevelDB(name, dbDir)
  30. if err != nil {
  31. panic(err)
  32. }
  33. state := loadState(db)
  34. return &PersistentKVStoreApplication{
  35. app: &Application{state: state},
  36. valAddrToPubKeyMap: make(map[string]cryptoproto.PublicKey),
  37. logger: log.NewNopLogger(),
  38. }
  39. }
  40. func (app *PersistentKVStoreApplication) Close() error {
  41. return app.app.state.db.Close()
  42. }
  43. func (app *PersistentKVStoreApplication) SetLogger(l log.Logger) {
  44. app.logger = l
  45. }
  46. func (app *PersistentKVStoreApplication) Info(req types.RequestInfo) types.ResponseInfo {
  47. res := app.app.Info(req)
  48. res.LastBlockHeight = app.app.state.Height
  49. res.LastBlockAppHash = app.app.state.AppHash
  50. return res
  51. }
  52. // tx is either "val:pubkey!power" or "key=value" or just arbitrary bytes
  53. func (app *PersistentKVStoreApplication) DeliverTx(req types.RequestDeliverTx) types.ResponseDeliverTx {
  54. // if it starts with "val:", update the validator set
  55. // format is "val:pubkey!power"
  56. if isValidatorTx(req.Tx) {
  57. // update validators in the merkle tree
  58. // and in app.ValUpdates
  59. return app.execValidatorTx(req.Tx)
  60. }
  61. // otherwise, update the key-value store
  62. return app.app.DeliverTx(req)
  63. }
  64. func (app *PersistentKVStoreApplication) CheckTx(req types.RequestCheckTx) types.ResponseCheckTx {
  65. return app.app.CheckTx(req)
  66. }
  67. // Commit will panic if InitChain was not called
  68. func (app *PersistentKVStoreApplication) Commit() types.ResponseCommit {
  69. return app.app.Commit()
  70. }
  71. // When path=/val and data={validator address}, returns the validator update (types.ValidatorUpdate) varint encoded.
  72. // For any other path, returns an associated value or nil if missing.
  73. func (app *PersistentKVStoreApplication) Query(reqQuery types.RequestQuery) (resQuery types.ResponseQuery) {
  74. switch reqQuery.Path {
  75. case "/val":
  76. key := []byte("val:" + string(reqQuery.Data))
  77. value, err := app.app.state.db.Get(key)
  78. if err != nil {
  79. panic(err)
  80. }
  81. resQuery.Key = reqQuery.Data
  82. resQuery.Value = value
  83. return
  84. default:
  85. return app.app.Query(reqQuery)
  86. }
  87. }
  88. // Save the validators in the merkle tree
  89. func (app *PersistentKVStoreApplication) InitChain(req types.RequestInitChain) types.ResponseInitChain {
  90. for _, v := range req.Validators {
  91. r := app.updateValidator(v)
  92. if r.IsErr() {
  93. app.logger.Error("error updating validators", "r", r)
  94. }
  95. }
  96. return types.ResponseInitChain{}
  97. }
  98. // Track the block hash and header information
  99. func (app *PersistentKVStoreApplication) BeginBlock(req types.RequestBeginBlock) types.ResponseBeginBlock {
  100. // reset valset changes
  101. app.ValUpdates = make([]types.ValidatorUpdate, 0)
  102. // Punish validators who committed equivocation.
  103. for _, ev := range req.ByzantineValidators {
  104. if ev.Type == types.EvidenceType_DUPLICATE_VOTE {
  105. addr := string(ev.Validator.Address)
  106. if pubKey, ok := app.valAddrToPubKeyMap[addr]; ok {
  107. app.updateValidator(types.ValidatorUpdate{
  108. PubKey: pubKey,
  109. Power: ev.Validator.Power - 1,
  110. })
  111. app.logger.Info("Decreased val power by 1 because of the equivocation",
  112. "val", addr)
  113. } else {
  114. app.logger.Error("Wanted to punish val, but can't find it",
  115. "val", addr)
  116. }
  117. }
  118. }
  119. return types.ResponseBeginBlock{}
  120. }
  121. // Update the validator set
  122. func (app *PersistentKVStoreApplication) EndBlock(req types.RequestEndBlock) types.ResponseEndBlock {
  123. return types.ResponseEndBlock{ValidatorUpdates: app.ValUpdates}
  124. }
  125. func (app *PersistentKVStoreApplication) ListSnapshots(
  126. req types.RequestListSnapshots) types.ResponseListSnapshots {
  127. return types.ResponseListSnapshots{}
  128. }
  129. func (app *PersistentKVStoreApplication) LoadSnapshotChunk(
  130. req types.RequestLoadSnapshotChunk) types.ResponseLoadSnapshotChunk {
  131. return types.ResponseLoadSnapshotChunk{}
  132. }
  133. func (app *PersistentKVStoreApplication) OfferSnapshot(
  134. req types.RequestOfferSnapshot) types.ResponseOfferSnapshot {
  135. return types.ResponseOfferSnapshot{Result: types.ResponseOfferSnapshot_ABORT}
  136. }
  137. func (app *PersistentKVStoreApplication) ApplySnapshotChunk(
  138. req types.RequestApplySnapshotChunk) types.ResponseApplySnapshotChunk {
  139. return types.ResponseApplySnapshotChunk{Result: types.ResponseApplySnapshotChunk_ABORT}
  140. }
  141. //---------------------------------------------
  142. // update validators
  143. func (app *PersistentKVStoreApplication) Validators() (validators []types.ValidatorUpdate) {
  144. itr, err := app.app.state.db.Iterator(nil, nil)
  145. if err != nil {
  146. panic(err)
  147. }
  148. for ; itr.Valid(); itr.Next() {
  149. if isValidatorTx(itr.Key()) {
  150. validator := new(types.ValidatorUpdate)
  151. err := types.ReadMessage(bytes.NewBuffer(itr.Value()), validator)
  152. if err != nil {
  153. panic(err)
  154. }
  155. validators = append(validators, *validator)
  156. }
  157. }
  158. if err = itr.Error(); err != nil {
  159. panic(err)
  160. }
  161. return
  162. }
  163. func MakeValSetChangeTx(pubkey cryptoproto.PublicKey, power int64) []byte {
  164. pk, err := encoding.PubKeyFromProto(pubkey)
  165. if err != nil {
  166. panic(err)
  167. }
  168. pubStr := base64.StdEncoding.EncodeToString(pk.Bytes())
  169. return []byte(fmt.Sprintf("val:%s!%d", pubStr, power))
  170. }
  171. func isValidatorTx(tx []byte) bool {
  172. return strings.HasPrefix(string(tx), ValidatorSetChangePrefix)
  173. }
  174. // format is "val:pubkey!power"
  175. // pubkey is a base64-encoded 32-byte ed25519 key
  176. func (app *PersistentKVStoreApplication) execValidatorTx(tx []byte) types.ResponseDeliverTx {
  177. tx = tx[len(ValidatorSetChangePrefix):]
  178. // get the pubkey and power
  179. pubKeyAndPower := strings.Split(string(tx), "!")
  180. if len(pubKeyAndPower) != 2 {
  181. return types.ResponseDeliverTx{
  182. Code: code.CodeTypeEncodingError,
  183. Log: fmt.Sprintf("Expected 'pubkey!power'. Got %v", pubKeyAndPower)}
  184. }
  185. pubkeyS, powerS := pubKeyAndPower[0], pubKeyAndPower[1]
  186. // decode the pubkey
  187. pubkey, err := base64.StdEncoding.DecodeString(pubkeyS)
  188. if err != nil {
  189. return types.ResponseDeliverTx{
  190. Code: code.CodeTypeEncodingError,
  191. Log: fmt.Sprintf("Pubkey (%s) is invalid base64", pubkeyS)}
  192. }
  193. // decode the power
  194. power, err := strconv.ParseInt(powerS, 10, 64)
  195. if err != nil {
  196. return types.ResponseDeliverTx{
  197. Code: code.CodeTypeEncodingError,
  198. Log: fmt.Sprintf("Power (%s) is not an int", powerS)}
  199. }
  200. // update
  201. return app.updateValidator(types.UpdateValidator(pubkey, power, ""))
  202. }
  203. // add, update, or remove a validator
  204. func (app *PersistentKVStoreApplication) updateValidator(v types.ValidatorUpdate) types.ResponseDeliverTx {
  205. pubkey, err := encoding.PubKeyFromProto(v.PubKey)
  206. if err != nil {
  207. panic(fmt.Errorf("can't decode public key: %w", err))
  208. }
  209. key := []byte("val:" + string(pubkey.Bytes()))
  210. if v.Power == 0 {
  211. // remove validator
  212. hasKey, err := app.app.state.db.Has(key)
  213. if err != nil {
  214. panic(err)
  215. }
  216. if !hasKey {
  217. pubStr := base64.StdEncoding.EncodeToString(pubkey.Bytes())
  218. return types.ResponseDeliverTx{
  219. Code: code.CodeTypeUnauthorized,
  220. Log: fmt.Sprintf("Cannot remove non-existent validator %s", pubStr)}
  221. }
  222. if err = app.app.state.db.Delete(key); err != nil {
  223. panic(err)
  224. }
  225. delete(app.valAddrToPubKeyMap, string(pubkey.Address()))
  226. } else {
  227. // add or update validator
  228. value := bytes.NewBuffer(make([]byte, 0))
  229. if err := types.WriteMessage(&v, value); err != nil {
  230. return types.ResponseDeliverTx{
  231. Code: code.CodeTypeEncodingError,
  232. Log: fmt.Sprintf("error encoding validator: %v", err)}
  233. }
  234. if err = app.app.state.db.Set(key, value.Bytes()); err != nil {
  235. panic(err)
  236. }
  237. app.valAddrToPubKeyMap[string(pubkey.Address())] = v.PubKey
  238. }
  239. // we only update the changes array if we successfully updated the tree
  240. app.ValUpdates = append(app.ValUpdates, v)
  241. return types.ResponseDeliverTx{Code: code.CodeTypeOK}
  242. }