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.

206 lines
5.7 KiB

7 years ago
8 years ago
8 years ago
8 years ago
8 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
7 years ago
7 years ago
  1. package dummy
  2. import (
  3. "bytes"
  4. "encoding/hex"
  5. "fmt"
  6. "strconv"
  7. "strings"
  8. "github.com/tendermint/abci/example/code"
  9. "github.com/tendermint/abci/types"
  10. crypto "github.com/tendermint/go-crypto"
  11. cmn "github.com/tendermint/tmlibs/common"
  12. dbm "github.com/tendermint/tmlibs/db"
  13. "github.com/tendermint/tmlibs/log"
  14. )
  15. const (
  16. ValidatorSetChangePrefix string = "val:"
  17. )
  18. //-----------------------------------------
  19. var _ types.Application = (*PersistentDummyApplication)(nil)
  20. type PersistentDummyApplication struct {
  21. app *DummyApplication
  22. // validator set
  23. ValUpdates []types.Validator
  24. logger log.Logger
  25. }
  26. func NewPersistentDummyApplication(dbDir string) *PersistentDummyApplication {
  27. name := "dummy"
  28. db, err := dbm.NewGoLevelDB(name, dbDir)
  29. if err != nil {
  30. panic(err)
  31. }
  32. state := loadState(db)
  33. return &PersistentDummyApplication{
  34. app: &DummyApplication{state: state},
  35. logger: log.NewNopLogger(),
  36. }
  37. }
  38. func (app *PersistentDummyApplication) SetLogger(l log.Logger) {
  39. app.logger = l
  40. }
  41. func (app *PersistentDummyApplication) Info(req types.RequestInfo) types.ResponseInfo {
  42. res := app.app.Info(req)
  43. res.LastBlockHeight = app.app.state.Height
  44. res.LastBlockAppHash = app.app.state.AppHash
  45. return res
  46. }
  47. func (app *PersistentDummyApplication) SetOption(req types.RequestSetOption) types.ResponseSetOption {
  48. return app.app.SetOption(req)
  49. }
  50. // tx is either "val:pubkey/power" or "key=value" or just arbitrary bytes
  51. func (app *PersistentDummyApplication) DeliverTx(tx []byte) types.ResponseDeliverTx {
  52. // if it starts with "val:", update the validator set
  53. // format is "val:pubkey/power"
  54. if isValidatorTx(tx) {
  55. // update validators in the merkle tree
  56. // and in app.ValUpdates
  57. return app.execValidatorTx(tx)
  58. }
  59. // otherwise, update the key-value store
  60. return app.app.DeliverTx(tx)
  61. }
  62. func (app *PersistentDummyApplication) CheckTx(tx []byte) types.ResponseCheckTx {
  63. return app.app.CheckTx(tx)
  64. }
  65. // Commit will panic if InitChain was not called
  66. func (app *PersistentDummyApplication) Commit() types.ResponseCommit {
  67. return app.app.Commit()
  68. }
  69. func (app *PersistentDummyApplication) Query(reqQuery types.RequestQuery) types.ResponseQuery {
  70. return app.app.Query(reqQuery)
  71. }
  72. // Save the validators in the merkle tree
  73. func (app *PersistentDummyApplication) InitChain(req types.RequestInitChain) types.ResponseInitChain {
  74. for _, v := range req.Validators {
  75. r := app.updateValidator(v)
  76. if r.IsErr() {
  77. app.logger.Error("Error updating validators", "r", r)
  78. }
  79. }
  80. return types.ResponseInitChain{}
  81. }
  82. // Track the block hash and header information
  83. func (app *PersistentDummyApplication) BeginBlock(req types.RequestBeginBlock) types.ResponseBeginBlock {
  84. // reset valset changes
  85. app.ValUpdates = make([]types.Validator, 0)
  86. return types.ResponseBeginBlock{}
  87. }
  88. // Update the validator set
  89. func (app *PersistentDummyApplication) EndBlock(req types.RequestEndBlock) types.ResponseEndBlock {
  90. return types.ResponseEndBlock{ValidatorUpdates: app.ValUpdates}
  91. }
  92. //---------------------------------------------
  93. // update validators
  94. func (app *PersistentDummyApplication) Validators() (validators []types.Validator) {
  95. itr := app.app.state.db.Iterator(nil, nil)
  96. for ; itr.Valid(); itr.Next() {
  97. if isValidatorTx(itr.Key()) {
  98. validator := new(types.Validator)
  99. err := types.ReadMessage(bytes.NewBuffer(itr.Value()), validator)
  100. if err != nil {
  101. panic(err)
  102. }
  103. validators = append(validators, *validator)
  104. }
  105. }
  106. return
  107. }
  108. func MakeValSetChangeTx(pubkey []byte, power int64) []byte {
  109. return []byte(cmn.Fmt("val:%X/%d", pubkey, power))
  110. }
  111. func isValidatorTx(tx []byte) bool {
  112. return strings.HasPrefix(string(tx), ValidatorSetChangePrefix)
  113. }
  114. // format is "val:pubkey1/power1,addr2/power2,addr3/power3"tx
  115. func (app *PersistentDummyApplication) execValidatorTx(tx []byte) types.ResponseDeliverTx {
  116. tx = tx[len(ValidatorSetChangePrefix):]
  117. //get the pubkey and power
  118. pubKeyAndPower := strings.Split(string(tx), "/")
  119. if len(pubKeyAndPower) != 2 {
  120. return types.ResponseDeliverTx{
  121. Code: code.CodeTypeEncodingError,
  122. Log: fmt.Sprintf("Expected 'pubkey/power'. Got %v", pubKeyAndPower)}
  123. }
  124. pubkeyS, powerS := pubKeyAndPower[0], pubKeyAndPower[1]
  125. // decode the pubkey, ensuring its go-crypto encoded
  126. pubkey, err := hex.DecodeString(pubkeyS)
  127. if err != nil {
  128. return types.ResponseDeliverTx{
  129. Code: code.CodeTypeEncodingError,
  130. Log: fmt.Sprintf("Pubkey (%s) is invalid hex", pubkeyS)}
  131. }
  132. _, err = crypto.PubKeyFromBytes(pubkey)
  133. if err != nil {
  134. return types.ResponseDeliverTx{
  135. Code: code.CodeTypeEncodingError,
  136. Log: fmt.Sprintf("Pubkey (%X) is invalid go-crypto encoded", pubkey)}
  137. }
  138. // decode the power
  139. power, err := strconv.ParseInt(powerS, 10, 64)
  140. if err != nil {
  141. return types.ResponseDeliverTx{
  142. Code: code.CodeTypeEncodingError,
  143. Log: fmt.Sprintf("Power (%s) is not an int", powerS)}
  144. }
  145. // update
  146. return app.updateValidator(types.Validator{pubkey, power})
  147. }
  148. // add, update, or remove a validator
  149. func (app *PersistentDummyApplication) updateValidator(v types.Validator) types.ResponseDeliverTx {
  150. key := []byte("val:" + string(v.PubKey))
  151. if v.Power == 0 {
  152. // remove validator
  153. if !app.app.state.db.Has(key) {
  154. return types.ResponseDeliverTx{
  155. Code: code.CodeTypeUnauthorized,
  156. Log: fmt.Sprintf("Cannot remove non-existent validator %X", key)}
  157. }
  158. app.app.state.db.Delete(key)
  159. } else {
  160. // add or update validator
  161. value := bytes.NewBuffer(make([]byte, 0))
  162. if err := types.WriteMessage(&v, value); err != nil {
  163. return types.ResponseDeliverTx{
  164. Code: code.CodeTypeEncodingError,
  165. Log: fmt.Sprintf("Error encoding validator: %v", err)}
  166. }
  167. app.app.state.db.Set(key, value.Bytes())
  168. }
  169. // we only update the changes array if we successfully updated the tree
  170. app.ValUpdates = append(app.ValUpdates, v)
  171. return types.ResponseDeliverTx{Code: code.CodeTypeOK}
  172. }