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.

80 lines
2.1 KiB

  1. package privval
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "github.com/tendermint/tendermint/crypto"
  6. cmn "github.com/tendermint/tendermint/libs/common"
  7. "github.com/tendermint/tendermint/types"
  8. )
  9. // OldFilePV is the old version of the FilePV, pre v0.28.0.
  10. type OldFilePV struct {
  11. Address types.Address `json:"address"`
  12. PubKey crypto.PubKey `json:"pub_key"`
  13. LastHeight int64 `json:"last_height"`
  14. LastRound int `json:"last_round"`
  15. LastStep int8 `json:"last_step"`
  16. LastSignature []byte `json:"last_signature,omitempty"`
  17. LastSignBytes cmn.HexBytes `json:"last_signbytes,omitempty"`
  18. PrivKey crypto.PrivKey `json:"priv_key"`
  19. filePath string
  20. }
  21. // LoadOldFilePV loads an OldFilePV from the filePath.
  22. func LoadOldFilePV(filePath string) (*OldFilePV, error) {
  23. pvJSONBytes, err := ioutil.ReadFile(filePath)
  24. if err != nil {
  25. return nil, err
  26. }
  27. pv := &OldFilePV{}
  28. err = cdc.UnmarshalJSON(pvJSONBytes, &pv)
  29. if err != nil {
  30. return nil, err
  31. }
  32. // overwrite pubkey and address for convenience
  33. pv.PubKey = pv.PrivKey.PubKey()
  34. pv.Address = pv.PubKey.Address()
  35. pv.filePath = filePath
  36. return pv, nil
  37. }
  38. // Upgrade convets the OldFilePV to the new FilePV, separating the immutable and mutable components,
  39. // and persisting them to the keyFilePath and stateFilePath, respectively.
  40. // It renames the original file by adding ".bak".
  41. func (oldFilePV *OldFilePV) Upgrade(keyFilePath, stateFilePath string) *FilePV {
  42. privKey := oldFilePV.PrivKey
  43. pvKey := FilePVKey{
  44. PrivKey: privKey,
  45. PubKey: privKey.PubKey(),
  46. Address: privKey.PubKey().Address(),
  47. filePath: keyFilePath,
  48. }
  49. pvState := FilePVLastSignState{
  50. Height: oldFilePV.LastHeight,
  51. Round: oldFilePV.LastRound,
  52. Step: oldFilePV.LastStep,
  53. Signature: oldFilePV.LastSignature,
  54. SignBytes: oldFilePV.LastSignBytes,
  55. filePath: stateFilePath,
  56. }
  57. // Save the new PV files
  58. pv := &FilePV{
  59. Key: pvKey,
  60. LastSignState: pvState,
  61. }
  62. pv.Save()
  63. // Rename the old PV file
  64. err := os.Rename(oldFilePV.filePath, oldFilePV.filePath+".bak")
  65. if err != nil {
  66. panic(err)
  67. }
  68. return pv
  69. }