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.

345 lines
10 KiB

  1. package privval
  2. import (
  3. "bytes"
  4. "errors"
  5. "fmt"
  6. "io/ioutil"
  7. "sync"
  8. "time"
  9. "github.com/tendermint/go-crypto"
  10. "github.com/tendermint/tendermint/types"
  11. cmn "github.com/tendermint/tmlibs/common"
  12. )
  13. // TODO: type ?
  14. const (
  15. stepNone int8 = 0 // Used to distinguish the initial state
  16. stepPropose int8 = 1
  17. stepPrevote int8 = 2
  18. stepPrecommit int8 = 3
  19. )
  20. func voteToStep(vote *types.Vote) int8 {
  21. switch vote.Type {
  22. case types.VoteTypePrevote:
  23. return stepPrevote
  24. case types.VoteTypePrecommit:
  25. return stepPrecommit
  26. default:
  27. cmn.PanicSanity("Unknown vote type")
  28. return 0
  29. }
  30. }
  31. // FilePV implements PrivValidator using data persisted to disk
  32. // to prevent double signing.
  33. // NOTE: the directory containing the pv.filePath must already exist.
  34. type FilePV struct {
  35. Address types.Address `json:"address"`
  36. PubKey crypto.PubKey `json:"pub_key"`
  37. LastHeight int64 `json:"last_height"`
  38. LastRound int `json:"last_round"`
  39. LastStep int8 `json:"last_step"`
  40. LastSignature crypto.Signature `json:"last_signature,omitempty"` // so we dont lose signatures XXX Why would we lose signatures?
  41. LastSignBytes cmn.HexBytes `json:"last_signbytes,omitempty"` // so we dont lose signatures XXX Why would we lose signatures?
  42. PrivKey crypto.PrivKey `json:"priv_key"`
  43. // For persistence.
  44. // Overloaded for testing.
  45. filePath string
  46. mtx sync.Mutex
  47. }
  48. // GetAddress returns the address of the validator.
  49. // Implements PrivValidator.
  50. func (pv *FilePV) GetAddress() types.Address {
  51. return pv.Address
  52. }
  53. // GetPubKey returns the public key of the validator.
  54. // Implements PrivValidator.
  55. func (pv *FilePV) GetPubKey() crypto.PubKey {
  56. return pv.PubKey
  57. }
  58. // GenFilePV generates a new validator with randomly generated private key
  59. // and sets the filePath, but does not call Save().
  60. func GenFilePV(filePath string) *FilePV {
  61. privKey := crypto.GenPrivKeyEd25519()
  62. return &FilePV{
  63. Address: privKey.PubKey().Address(),
  64. PubKey: privKey.PubKey(),
  65. PrivKey: privKey,
  66. LastStep: stepNone,
  67. filePath: filePath,
  68. }
  69. }
  70. // LoadFilePV loads a FilePV from the filePath. The FilePV handles double
  71. // signing prevention by persisting data to the filePath. If the filePath does
  72. // not exist, the FilePV must be created manually and saved.
  73. func LoadFilePV(filePath string) *FilePV {
  74. pvJSONBytes, err := ioutil.ReadFile(filePath)
  75. if err != nil {
  76. cmn.Exit(err.Error())
  77. }
  78. pv := &FilePV{}
  79. err = cdc.UnmarshalJSON(pvJSONBytes, &pv)
  80. if err != nil {
  81. cmn.Exit(cmn.Fmt("Error reading PrivValidator from %v: %v\n", filePath, err))
  82. }
  83. pv.filePath = filePath
  84. return pv
  85. }
  86. // LoadOrGenFilePV loads a FilePV from the given filePath
  87. // or else generates a new one and saves it to the filePath.
  88. func LoadOrGenFilePV(filePath string) *FilePV {
  89. var pv *FilePV
  90. if cmn.FileExists(filePath) {
  91. pv = LoadFilePV(filePath)
  92. } else {
  93. pv = GenFilePV(filePath)
  94. pv.Save()
  95. }
  96. return pv
  97. }
  98. // Save persists the FilePV to disk.
  99. func (pv *FilePV) Save() {
  100. pv.mtx.Lock()
  101. defer pv.mtx.Unlock()
  102. pv.save()
  103. }
  104. func (pv *FilePV) save() {
  105. outFile := pv.filePath
  106. if outFile == "" {
  107. panic("Cannot save PrivValidator: filePath not set")
  108. }
  109. jsonBytes, err := cdc.MarshalJSONIndent(pv, "", " ")
  110. if err != nil {
  111. panic(err)
  112. }
  113. err = cmn.WriteFileAtomic(outFile, jsonBytes, 0600)
  114. if err != nil {
  115. panic(err)
  116. }
  117. }
  118. // Reset resets all fields in the FilePV.
  119. // NOTE: Unsafe!
  120. func (pv *FilePV) Reset() {
  121. var sig crypto.Signature
  122. pv.LastHeight = 0
  123. pv.LastRound = 0
  124. pv.LastStep = 0
  125. pv.LastSignature = sig
  126. pv.LastSignBytes = nil
  127. pv.Save()
  128. }
  129. // SignVote signs a canonical representation of the vote, along with the
  130. // chainID. Implements PrivValidator.
  131. func (pv *FilePV) SignVote(chainID string, vote *types.Vote) error {
  132. pv.mtx.Lock()
  133. defer pv.mtx.Unlock()
  134. if err := pv.signVote(chainID, vote); err != nil {
  135. return errors.New(cmn.Fmt("Error signing vote: %v", err))
  136. }
  137. return nil
  138. }
  139. // SignProposal signs a canonical representation of the proposal, along with
  140. // the chainID. Implements PrivValidator.
  141. func (pv *FilePV) SignProposal(chainID string, proposal *types.Proposal) error {
  142. pv.mtx.Lock()
  143. defer pv.mtx.Unlock()
  144. if err := pv.signProposal(chainID, proposal); err != nil {
  145. return fmt.Errorf("Error signing proposal: %v", err)
  146. }
  147. return nil
  148. }
  149. // returns error if HRS regression or no LastSignBytes. returns true if HRS is unchanged
  150. func (pv *FilePV) checkHRS(height int64, round int, step int8) (bool, error) {
  151. if pv.LastHeight > height {
  152. return false, errors.New("Height regression")
  153. }
  154. if pv.LastHeight == height {
  155. if pv.LastRound > round {
  156. return false, errors.New("Round regression")
  157. }
  158. if pv.LastRound == round {
  159. if pv.LastStep > step {
  160. return false, errors.New("Step regression")
  161. } else if pv.LastStep == step {
  162. if pv.LastSignBytes != nil {
  163. if pv.LastSignature == nil {
  164. panic("pv: LastSignature is nil but LastSignBytes is not!")
  165. }
  166. return true, nil
  167. }
  168. return false, errors.New("No LastSignature found")
  169. }
  170. }
  171. }
  172. return false, nil
  173. }
  174. // signVote checks if the vote is good to sign and sets the vote signature.
  175. // It may need to set the timestamp as well if the vote is otherwise the same as
  176. // a previously signed vote (ie. we crashed after signing but before the vote hit the WAL).
  177. func (pv *FilePV) signVote(chainID string, vote *types.Vote) error {
  178. height, round, step := vote.Height, vote.Round, voteToStep(vote)
  179. signBytes := vote.SignBytes(chainID)
  180. sameHRS, err := pv.checkHRS(height, round, step)
  181. if err != nil {
  182. return err
  183. }
  184. // We might crash before writing to the wal,
  185. // causing us to try to re-sign for the same HRS.
  186. // If signbytes are the same, use the last signature.
  187. // If they only differ by timestamp, use last timestamp and signature
  188. // Otherwise, return error
  189. if sameHRS {
  190. if bytes.Equal(signBytes, pv.LastSignBytes) {
  191. vote.Signature = pv.LastSignature
  192. } else if timestamp, ok := checkVotesOnlyDifferByTimestamp(pv.LastSignBytes, signBytes); ok {
  193. vote.Timestamp = timestamp
  194. vote.Signature = pv.LastSignature
  195. } else {
  196. err = fmt.Errorf("Conflicting data")
  197. }
  198. return err
  199. }
  200. // It passed the checks. Sign the vote
  201. sig := pv.PrivKey.Sign(signBytes)
  202. pv.saveSigned(height, round, step, signBytes, sig)
  203. vote.Signature = sig
  204. return nil
  205. }
  206. // signProposal checks if the proposal is good to sign and sets the proposal signature.
  207. // It may need to set the timestamp as well if the proposal is otherwise the same as
  208. // a previously signed proposal ie. we crashed after signing but before the proposal hit the WAL).
  209. func (pv *FilePV) signProposal(chainID string, proposal *types.Proposal) error {
  210. height, round, step := proposal.Height, proposal.Round, stepPropose
  211. signBytes := proposal.SignBytes(chainID)
  212. sameHRS, err := pv.checkHRS(height, round, step)
  213. if err != nil {
  214. return err
  215. }
  216. // We might crash before writing to the wal,
  217. // causing us to try to re-sign for the same HRS.
  218. // If signbytes are the same, use the last signature.
  219. // If they only differ by timestamp, use last timestamp and signature
  220. // Otherwise, return error
  221. if sameHRS {
  222. if bytes.Equal(signBytes, pv.LastSignBytes) {
  223. proposal.Signature = pv.LastSignature
  224. } else if timestamp, ok := checkProposalsOnlyDifferByTimestamp(pv.LastSignBytes, signBytes); ok {
  225. proposal.Timestamp = timestamp
  226. proposal.Signature = pv.LastSignature
  227. } else {
  228. err = fmt.Errorf("Conflicting data")
  229. }
  230. return err
  231. }
  232. // It passed the checks. Sign the proposal
  233. sig := pv.PrivKey.Sign(signBytes)
  234. pv.saveSigned(height, round, step, signBytes, sig)
  235. proposal.Signature = sig
  236. return nil
  237. }
  238. // Persist height/round/step and signature
  239. func (pv *FilePV) saveSigned(height int64, round int, step int8,
  240. signBytes []byte, sig crypto.Signature) {
  241. pv.LastHeight = height
  242. pv.LastRound = round
  243. pv.LastStep = step
  244. pv.LastSignature = sig
  245. pv.LastSignBytes = signBytes
  246. pv.save()
  247. }
  248. // SignHeartbeat signs a canonical representation of the heartbeat, along with the chainID.
  249. // Implements PrivValidator.
  250. func (pv *FilePV) SignHeartbeat(chainID string, heartbeat *types.Heartbeat) error {
  251. pv.mtx.Lock()
  252. defer pv.mtx.Unlock()
  253. heartbeat.Signature = pv.PrivKey.Sign(heartbeat.SignBytes(chainID))
  254. return nil
  255. }
  256. // String returns a string representation of the FilePV.
  257. func (pv *FilePV) String() string {
  258. return fmt.Sprintf("PrivValidator{%v LH:%v, LR:%v, LS:%v}", pv.GetAddress(), pv.LastHeight, pv.LastRound, pv.LastStep)
  259. }
  260. //-------------------------------------
  261. // returns the timestamp from the lastSignBytes.
  262. // returns true if the only difference in the votes is their timestamp.
  263. func checkVotesOnlyDifferByTimestamp(lastSignBytes, newSignBytes []byte) (time.Time, bool) {
  264. var lastVote, newVote types.CanonicalJSONVote
  265. if err := cdc.UnmarshalJSON(lastSignBytes, &lastVote); err != nil {
  266. panic(fmt.Sprintf("LastSignBytes cannot be unmarshalled into vote: %v", err))
  267. }
  268. if err := cdc.UnmarshalJSON(newSignBytes, &newVote); err != nil {
  269. panic(fmt.Sprintf("signBytes cannot be unmarshalled into vote: %v", err))
  270. }
  271. lastTime, err := time.Parse(types.TimeFormat, lastVote.Timestamp)
  272. if err != nil {
  273. panic(err)
  274. }
  275. // set the times to the same value and check equality
  276. now := types.CanonicalTime(time.Now())
  277. lastVote.Timestamp = now
  278. newVote.Timestamp = now
  279. lastVoteBytes, _ := cdc.MarshalJSON(lastVote)
  280. newVoteBytes, _ := cdc.MarshalJSON(newVote)
  281. return lastTime, bytes.Equal(newVoteBytes, lastVoteBytes)
  282. }
  283. // returns the timestamp from the lastSignBytes.
  284. // returns true if the only difference in the proposals is their timestamp
  285. func checkProposalsOnlyDifferByTimestamp(lastSignBytes, newSignBytes []byte) (time.Time, bool) {
  286. var lastProposal, newProposal types.CanonicalJSONProposal
  287. if err := cdc.UnmarshalJSON(lastSignBytes, &lastProposal); err != nil {
  288. panic(fmt.Sprintf("LastSignBytes cannot be unmarshalled into proposal: %v", err))
  289. }
  290. if err := cdc.UnmarshalJSON(newSignBytes, &newProposal); err != nil {
  291. panic(fmt.Sprintf("signBytes cannot be unmarshalled into proposal: %v", err))
  292. }
  293. lastTime, err := time.Parse(types.TimeFormat, lastProposal.Timestamp)
  294. if err != nil {
  295. panic(err)
  296. }
  297. // set the times to the same value and check equality
  298. now := types.CanonicalTime(time.Now())
  299. lastProposal.Timestamp = now
  300. newProposal.Timestamp = now
  301. lastProposalBytes, _ := cdc.MarshalJSON(lastProposal)
  302. newProposalBytes, _ := cdc.MarshalJSON(newProposal)
  303. return lastTime, bytes.Equal(newProposalBytes, lastProposalBytes)
  304. }