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.

275 lines
8.0 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/keys"
  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. for _, ev := range req.ByzantineValidators {
  104. if ev.Type == tmtypes.ABCIEvidenceTypeDuplicateVote {
  105. // decrease voting power by 1
  106. if ev.TotalVotingPower == 0 {
  107. continue
  108. }
  109. app.updateValidator(types.ValidatorUpdate{
  110. PubKey: app.valAddrToPubKeyMap[string(ev.Validator.Address)],
  111. Power: ev.TotalVotingPower - 1,
  112. })
  113. }
  114. }
  115. return types.ResponseBeginBlock{}
  116. }
  117. // Update the validator set
  118. func (app *PersistentKVStoreApplication) EndBlock(req types.RequestEndBlock) types.ResponseEndBlock {
  119. return types.ResponseEndBlock{ValidatorUpdates: app.ValUpdates}
  120. }
  121. func (app *PersistentKVStoreApplication) ListSnapshots(
  122. req types.RequestListSnapshots) types.ResponseListSnapshots {
  123. return types.ResponseListSnapshots{}
  124. }
  125. func (app *PersistentKVStoreApplication) LoadSnapshotChunk(
  126. req types.RequestLoadSnapshotChunk) types.ResponseLoadSnapshotChunk {
  127. return types.ResponseLoadSnapshotChunk{}
  128. }
  129. func (app *PersistentKVStoreApplication) OfferSnapshot(
  130. req types.RequestOfferSnapshot) types.ResponseOfferSnapshot {
  131. return types.ResponseOfferSnapshot{Result: types.ResponseOfferSnapshot_ABORT}
  132. }
  133. func (app *PersistentKVStoreApplication) ApplySnapshotChunk(
  134. req types.RequestApplySnapshotChunk) types.ResponseApplySnapshotChunk {
  135. return types.ResponseApplySnapshotChunk{Result: types.ResponseApplySnapshotChunk_ABORT}
  136. }
  137. //---------------------------------------------
  138. // update validators
  139. func (app *PersistentKVStoreApplication) Validators() (validators []types.ValidatorUpdate) {
  140. itr, err := app.app.state.db.Iterator(nil, nil)
  141. if err != nil {
  142. panic(err)
  143. }
  144. for ; itr.Valid(); itr.Next() {
  145. if isValidatorTx(itr.Key()) {
  146. validator := new(types.ValidatorUpdate)
  147. err := types.ReadMessage(bytes.NewBuffer(itr.Value()), validator)
  148. if err != nil {
  149. panic(err)
  150. }
  151. validators = append(validators, *validator)
  152. }
  153. }
  154. return
  155. }
  156. func MakeValSetChangeTx(pubkey pc.PublicKey, power int64) []byte {
  157. pk, err := cryptoenc.PubKeyFromProto(pubkey)
  158. if err != nil {
  159. panic(err)
  160. }
  161. pubStr := base64.StdEncoding.EncodeToString(pk.Bytes())
  162. return []byte(fmt.Sprintf("val:%s!%d", pubStr, power))
  163. }
  164. func isValidatorTx(tx []byte) bool {
  165. return strings.HasPrefix(string(tx), ValidatorSetChangePrefix)
  166. }
  167. // format is "val:pubkey!power"
  168. // pubkey is a base64-encoded 32-byte ed25519 key
  169. func (app *PersistentKVStoreApplication) execValidatorTx(tx []byte) types.ResponseDeliverTx {
  170. tx = tx[len(ValidatorSetChangePrefix):]
  171. //get the pubkey and power
  172. pubKeyAndPower := strings.Split(string(tx), "!")
  173. if len(pubKeyAndPower) != 2 {
  174. return types.ResponseDeliverTx{
  175. Code: code.CodeTypeEncodingError,
  176. Log: fmt.Sprintf("Expected 'pubkey!power'. Got %v", pubKeyAndPower)}
  177. }
  178. pubkeyS, powerS := pubKeyAndPower[0], pubKeyAndPower[1]
  179. // decode the pubkey
  180. pubkey, err := base64.StdEncoding.DecodeString(pubkeyS)
  181. if err != nil {
  182. return types.ResponseDeliverTx{
  183. Code: code.CodeTypeEncodingError,
  184. Log: fmt.Sprintf("Pubkey (%s) is invalid base64", pubkeyS)}
  185. }
  186. // decode the power
  187. power, err := strconv.ParseInt(powerS, 10, 64)
  188. if err != nil {
  189. return types.ResponseDeliverTx{
  190. Code: code.CodeTypeEncodingError,
  191. Log: fmt.Sprintf("Power (%s) is not an int", powerS)}
  192. }
  193. // update
  194. return app.updateValidator(types.Ed25519ValidatorUpdate(pubkey, power))
  195. }
  196. // add, update, or remove a validator
  197. func (app *PersistentKVStoreApplication) updateValidator(v types.ValidatorUpdate) types.ResponseDeliverTx {
  198. key := []byte("val:" + string(v.PubKey.GetEd25519()))
  199. pubkey, err := cryptoenc.PubKeyFromProto(v.PubKey)
  200. if err != nil {
  201. panic(fmt.Errorf("can't decode public key: %w", err))
  202. }
  203. if v.Power == 0 {
  204. // remove validator
  205. hasKey, err := app.app.state.db.Has(key)
  206. if err != nil {
  207. panic(err)
  208. }
  209. if !hasKey {
  210. pubStr := base64.StdEncoding.EncodeToString(pubkey.Bytes())
  211. return types.ResponseDeliverTx{
  212. Code: code.CodeTypeUnauthorized,
  213. Log: fmt.Sprintf("Cannot remove non-existent validator %s", pubStr)}
  214. }
  215. app.app.state.db.Delete(key)
  216. delete(app.valAddrToPubKeyMap, string(pubkey.Address()))
  217. } else {
  218. // add or update validator
  219. value := bytes.NewBuffer(make([]byte, 0))
  220. if err := types.WriteMessage(&v, value); err != nil {
  221. return types.ResponseDeliverTx{
  222. Code: code.CodeTypeEncodingError,
  223. Log: fmt.Sprintf("Error encoding validator: %v", err)}
  224. }
  225. app.app.state.db.Set(key, value.Bytes())
  226. app.valAddrToPubKeyMap[string(pubkey.Address())] = v.PubKey
  227. }
  228. // we only update the changes array if we successfully updated the tree
  229. app.ValUpdates = append(app.ValUpdates, v)
  230. return types.ResponseDeliverTx{Code: code.CodeTypeOK}
  231. }