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.

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