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.

81 lines
2.1 KiB

privval: improve Remote Signer implementation (#3351) This issue is related to #3107 This is a first renaming/refactoring step before reworking and removing heartbeats. As discussed with @Liamsi , we preferred to go for a couple of independent and separate PRs to simplify review work. The changes: Help to clarify the relation between the validator and remote signer endpoints Differentiate between timeouts and deadlines Prepare to encapsulate networking related code behind RemoteSigner in the next PR My intention is to separate and encapsulate the "network related" code from the actual signer. SignerRemote ---(uses/contains)--> SignerValidatorEndpoint <--(connects to)--> SignerServiceEndpoint ---> SignerService (future.. not here yet but would like to decouple too) All reconnection/heartbeat/whatever code goes in the endpoints. Signer[Remote/Service] do not need to know about that. I agree Endpoint may not be the perfect name. I tried to find something "Go-ish" enough. It is a common name in go-kit, kubernetes, etc. Right now: SignerValidatorEndpoint: handles the listener contains SignerRemote Implements the PrivValidator interface connects and sets a connection object in a contained SignerRemote delegates PrivValidator some calls to SignerRemote which in turn uses the conn object that was set externally SignerRemote: Implements the PrivValidator interface read/writes from a connection object directly handles heartbeats SignerServiceEndpoint: Does most things in a single place delegates to a PrivValidator IIRC. * cleanup * Refactoring step 1 * Refactoring step 2 * move messages to another file * mark for future work / next steps * mark deprecated classes in docs * Fix linter problems * additional linter fixes
5 years ago
  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. // Deprecated: Use FilePV instead.
  11. type OldFilePV struct {
  12. Address types.Address `json:"address"`
  13. PubKey crypto.PubKey `json:"pub_key"`
  14. LastHeight int64 `json:"last_height"`
  15. LastRound int `json:"last_round"`
  16. LastStep int8 `json:"last_step"`
  17. LastSignature []byte `json:"last_signature,omitempty"`
  18. LastSignBytes cmn.HexBytes `json:"last_signbytes,omitempty"`
  19. PrivKey crypto.PrivKey `json:"priv_key"`
  20. filePath string
  21. }
  22. // LoadOldFilePV loads an OldFilePV from the filePath.
  23. func LoadOldFilePV(filePath string) (*OldFilePV, error) {
  24. pvJSONBytes, err := ioutil.ReadFile(filePath)
  25. if err != nil {
  26. return nil, err
  27. }
  28. pv := &OldFilePV{}
  29. err = cdc.UnmarshalJSON(pvJSONBytes, &pv)
  30. if err != nil {
  31. return nil, err
  32. }
  33. // overwrite pubkey and address for convenience
  34. pv.PubKey = pv.PrivKey.PubKey()
  35. pv.Address = pv.PubKey.Address()
  36. pv.filePath = filePath
  37. return pv, nil
  38. }
  39. // Upgrade convets the OldFilePV to the new FilePV, separating the immutable and mutable components,
  40. // and persisting them to the keyFilePath and stateFilePath, respectively.
  41. // It renames the original file by adding ".bak".
  42. func (oldFilePV *OldFilePV) Upgrade(keyFilePath, stateFilePath string) *FilePV {
  43. privKey := oldFilePV.PrivKey
  44. pvKey := FilePVKey{
  45. PrivKey: privKey,
  46. PubKey: privKey.PubKey(),
  47. Address: privKey.PubKey().Address(),
  48. filePath: keyFilePath,
  49. }
  50. pvState := FilePVLastSignState{
  51. Height: oldFilePV.LastHeight,
  52. Round: oldFilePV.LastRound,
  53. Step: oldFilePV.LastStep,
  54. Signature: oldFilePV.LastSignature,
  55. SignBytes: oldFilePV.LastSignBytes,
  56. filePath: stateFilePath,
  57. }
  58. // Save the new PV files
  59. pv := &FilePV{
  60. Key: pvKey,
  61. LastSignState: pvState,
  62. }
  63. pv.Save()
  64. // Rename the old PV file
  65. err := os.Rename(oldFilePV.filePath, oldFilePV.filePath+".bak")
  66. if err != nil {
  67. panic(err)
  68. }
  69. return pv
  70. }