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.

282 lines
8.1 KiB

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