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.

471 lines
14 KiB

Close and retry a RemoteSigner on err (#2923) * Close and recreate a RemoteSigner on err * Update changelog * Address Anton's comments / suggestions: - update changelog - restart TCPVal - shut down on `ErrUnexpectedResponse` * re-init remote signer client with fresh connection if Ping fails - add/update TODOs in secret connection - rename tcp.go -> tcp_client.go, same with ipc to clarify their purpose * account for `conn returned by waitConnection can be `nil` - also add TODO about RemoteSigner conn field * Tests for retrying: IPC / TCP - shorter info log on success - set conn and use it in tests to close conn * Tests for retrying: IPC / TCP - shorter info log on success - set conn and use it in tests to close conn - add rwmutex for conn field in IPC * comments and doc.go * fix ipc tests. fixes #2677 * use constants for tests * cleanup some error statements * fixes #2784, race in tests * remove print statement * minor fixes from review * update comment on sts spec * cosmetics * p2p/conn: add failing tests * p2p/conn: make SecretConnection thread safe * changelog * IPCVal signer refactor - use a .reset() method - don't use embedded RemoteSignerClient - guard RemoteSignerClient with mutex - drop the .conn - expose Close() on RemoteSignerClient * apply IPCVal refactor to TCPVal * remove mtx from RemoteSignerClient * consolidate IPCVal and TCPVal, fixes #3104 - done in tcp_client.go - now called SocketVal - takes a listener in the constructor - make tcpListener and unixListener contain all the differences * delete ipc files * introduce unix and tcp dialer for RemoteSigner * rename files - drop tcp_ prefix - rename priv_validator.go to file.go * bring back listener options * fix node * fix priv_val_server * fix node test * minor cleanup and comments
6 years ago
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
6 years ago
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
6 years ago
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
6 years ago
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
6 years ago
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
6 years ago
  1. package privval
  2. import (
  3. "bytes"
  4. "context"
  5. "errors"
  6. "fmt"
  7. "os"
  8. "time"
  9. "github.com/gogo/protobuf/proto"
  10. "github.com/tendermint/tendermint/crypto"
  11. "github.com/tendermint/tendermint/crypto/ed25519"
  12. "github.com/tendermint/tendermint/crypto/secp256k1"
  13. "github.com/tendermint/tendermint/internal/libs/protoio"
  14. "github.com/tendermint/tendermint/internal/libs/tempfile"
  15. tmbytes "github.com/tendermint/tendermint/libs/bytes"
  16. tmjson "github.com/tendermint/tendermint/libs/json"
  17. tmos "github.com/tendermint/tendermint/libs/os"
  18. tmtime "github.com/tendermint/tendermint/libs/time"
  19. tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
  20. "github.com/tendermint/tendermint/types"
  21. )
  22. // TODO: type ?
  23. const (
  24. stepNone int8 = 0 // Used to distinguish the initial state
  25. stepPropose int8 = 1
  26. stepPrevote int8 = 2
  27. stepPrecommit int8 = 3
  28. )
  29. // A vote is either stepPrevote or stepPrecommit.
  30. func voteToStep(vote *tmproto.Vote) (int8, error) {
  31. switch vote.Type {
  32. case tmproto.PrevoteType:
  33. return stepPrevote, nil
  34. case tmproto.PrecommitType:
  35. return stepPrecommit, nil
  36. default:
  37. return 0, fmt.Errorf("unknown vote type: %v", vote.Type)
  38. }
  39. }
  40. //-------------------------------------------------------------------------------
  41. // FilePVKey stores the immutable part of PrivValidator.
  42. type FilePVKey struct {
  43. Address types.Address `json:"address"`
  44. PubKey crypto.PubKey `json:"pub_key"`
  45. PrivKey crypto.PrivKey `json:"priv_key"`
  46. filePath string
  47. }
  48. // Save persists the FilePVKey to its filePath.
  49. func (pvKey FilePVKey) Save() error {
  50. outFile := pvKey.filePath
  51. if outFile == "" {
  52. return errors.New("cannot save PrivValidator key: filePath not set")
  53. }
  54. jsonBytes, err := tmjson.MarshalIndent(pvKey, "", " ")
  55. if err != nil {
  56. return err
  57. }
  58. return tempfile.WriteFileAtomic(outFile, jsonBytes, 0600)
  59. }
  60. //-------------------------------------------------------------------------------
  61. // FilePVLastSignState stores the mutable part of PrivValidator.
  62. type FilePVLastSignState struct {
  63. Height int64 `json:"height"`
  64. Round int32 `json:"round"`
  65. Step int8 `json:"step"`
  66. Signature []byte `json:"signature,omitempty"`
  67. SignBytes tmbytes.HexBytes `json:"signbytes,omitempty"`
  68. filePath string
  69. }
  70. // CheckHRS checks the given height, round, step (HRS) against that of the
  71. // FilePVLastSignState. It returns an error if the arguments constitute a regression,
  72. // or if they match but the SignBytes are empty.
  73. // The returned boolean indicates whether the last Signature should be reused -
  74. // it returns true if the HRS matches the arguments and the SignBytes are not empty (indicating
  75. // we have already signed for this HRS, and can reuse the existing signature).
  76. // It panics if the HRS matches the arguments, there's a SignBytes, but no Signature.
  77. func (lss *FilePVLastSignState) CheckHRS(height int64, round int32, step int8) (bool, error) {
  78. if lss.Height > height {
  79. return false, fmt.Errorf("height regression. Got %v, last height %v", height, lss.Height)
  80. }
  81. if lss.Height == height {
  82. if lss.Round > round {
  83. return false, fmt.Errorf("round regression at height %v. Got %v, last round %v", height, round, lss.Round)
  84. }
  85. if lss.Round == round {
  86. if lss.Step > step {
  87. return false, fmt.Errorf(
  88. "step regression at height %v round %v. Got %v, last step %v",
  89. height,
  90. round,
  91. step,
  92. lss.Step,
  93. )
  94. } else if lss.Step == step {
  95. if lss.SignBytes != nil {
  96. if lss.Signature == nil {
  97. panic("pv: Signature is nil but SignBytes is not!")
  98. }
  99. return true, nil
  100. }
  101. return false, errors.New("no SignBytes found")
  102. }
  103. }
  104. }
  105. return false, nil
  106. }
  107. // Save persists the FilePvLastSignState to its filePath.
  108. func (lss *FilePVLastSignState) Save() error {
  109. outFile := lss.filePath
  110. if outFile == "" {
  111. return errors.New("cannot save FilePVLastSignState: filePath not set")
  112. }
  113. jsonBytes, err := tmjson.MarshalIndent(lss, "", " ")
  114. if err != nil {
  115. return err
  116. }
  117. return tempfile.WriteFileAtomic(outFile, jsonBytes, 0600)
  118. }
  119. //-------------------------------------------------------------------------------
  120. // FilePV implements PrivValidator using data persisted to disk
  121. // to prevent double signing.
  122. // NOTE: the directories containing pv.Key.filePath and pv.LastSignState.filePath must already exist.
  123. // It includes the LastSignature and LastSignBytes so we don't lose the signature
  124. // if the process crashes after signing but before the resulting consensus message is processed.
  125. type FilePV struct {
  126. Key FilePVKey
  127. LastSignState FilePVLastSignState
  128. }
  129. var _ types.PrivValidator = (*FilePV)(nil)
  130. // NewFilePV generates a new validator from the given key and paths.
  131. func NewFilePV(privKey crypto.PrivKey, keyFilePath, stateFilePath string) *FilePV {
  132. return &FilePV{
  133. Key: FilePVKey{
  134. Address: privKey.PubKey().Address(),
  135. PubKey: privKey.PubKey(),
  136. PrivKey: privKey,
  137. filePath: keyFilePath,
  138. },
  139. LastSignState: FilePVLastSignState{
  140. Step: stepNone,
  141. filePath: stateFilePath,
  142. },
  143. }
  144. }
  145. // GenFilePV generates a new validator with randomly generated private key
  146. // and sets the filePaths, but does not call Save().
  147. func GenFilePV(keyFilePath, stateFilePath, keyType string) (*FilePV, error) {
  148. switch keyType {
  149. case types.ABCIPubKeyTypeSecp256k1:
  150. return NewFilePV(secp256k1.GenPrivKey(), keyFilePath, stateFilePath), nil
  151. case "", types.ABCIPubKeyTypeEd25519:
  152. return NewFilePV(ed25519.GenPrivKey(), keyFilePath, stateFilePath), nil
  153. default:
  154. return nil, fmt.Errorf("key type: %s is not supported", keyType)
  155. }
  156. }
  157. // LoadFilePV loads a FilePV from the filePaths. The FilePV handles double
  158. // signing prevention by persisting data to the stateFilePath. If either file path
  159. // does not exist, the program will exit.
  160. func LoadFilePV(keyFilePath, stateFilePath string) (*FilePV, error) {
  161. return loadFilePV(keyFilePath, stateFilePath, true)
  162. }
  163. // LoadFilePVEmptyState loads a FilePV from the given keyFilePath, with an empty LastSignState.
  164. // If the keyFilePath does not exist, the program will exit.
  165. func LoadFilePVEmptyState(keyFilePath, stateFilePath string) (*FilePV, error) {
  166. return loadFilePV(keyFilePath, stateFilePath, false)
  167. }
  168. // If loadState is true, we load from the stateFilePath. Otherwise, we use an empty LastSignState.
  169. func loadFilePV(keyFilePath, stateFilePath string, loadState bool) (*FilePV, error) {
  170. keyJSONBytes, err := os.ReadFile(keyFilePath)
  171. if err != nil {
  172. return nil, err
  173. }
  174. pvKey := FilePVKey{}
  175. err = tmjson.Unmarshal(keyJSONBytes, &pvKey)
  176. if err != nil {
  177. return nil, fmt.Errorf("error reading PrivValidator key from %v: %w", keyFilePath, err)
  178. }
  179. // overwrite pubkey and address for convenience
  180. pvKey.PubKey = pvKey.PrivKey.PubKey()
  181. pvKey.Address = pvKey.PubKey.Address()
  182. pvKey.filePath = keyFilePath
  183. pvState := FilePVLastSignState{}
  184. if loadState {
  185. stateJSONBytes, err := os.ReadFile(stateFilePath)
  186. if err != nil {
  187. return nil, err
  188. }
  189. err = tmjson.Unmarshal(stateJSONBytes, &pvState)
  190. if err != nil {
  191. return nil, fmt.Errorf("error reading PrivValidator state from %v: %w", stateFilePath, err)
  192. }
  193. }
  194. pvState.filePath = stateFilePath
  195. return &FilePV{
  196. Key: pvKey,
  197. LastSignState: pvState,
  198. }, nil
  199. }
  200. // LoadOrGenFilePV loads a FilePV from the given filePaths
  201. // or else generates a new one and saves it to the filePaths.
  202. func LoadOrGenFilePV(keyFilePath, stateFilePath string) (*FilePV, error) {
  203. if tmos.FileExists(keyFilePath) {
  204. pv, err := LoadFilePV(keyFilePath, stateFilePath)
  205. if err != nil {
  206. return nil, err
  207. }
  208. return pv, nil
  209. }
  210. pv, err := GenFilePV(keyFilePath, stateFilePath, "")
  211. if err != nil {
  212. return nil, err
  213. }
  214. if err := pv.Save(); err != nil {
  215. return nil, err
  216. }
  217. return pv, nil
  218. }
  219. // GetAddress returns the address of the validator.
  220. // Implements PrivValidator.
  221. func (pv *FilePV) GetAddress() types.Address {
  222. return pv.Key.Address
  223. }
  224. // GetPubKey returns the public key of the validator.
  225. // Implements PrivValidator.
  226. func (pv *FilePV) GetPubKey(ctx context.Context) (crypto.PubKey, error) {
  227. return pv.Key.PubKey, nil
  228. }
  229. // SignVote signs a canonical representation of the vote, along with the
  230. // chainID. Implements PrivValidator.
  231. func (pv *FilePV) SignVote(ctx context.Context, chainID string, vote *tmproto.Vote) error {
  232. if err := pv.signVote(chainID, vote); err != nil {
  233. return fmt.Errorf("error signing vote: %v", err)
  234. }
  235. return nil
  236. }
  237. // SignProposal signs a canonical representation of the proposal, along with
  238. // the chainID. Implements PrivValidator.
  239. func (pv *FilePV) SignProposal(ctx context.Context, chainID string, proposal *tmproto.Proposal) error {
  240. if err := pv.signProposal(chainID, proposal); err != nil {
  241. return fmt.Errorf("error signing proposal: %v", err)
  242. }
  243. return nil
  244. }
  245. // Save persists the FilePV to disk.
  246. func (pv *FilePV) Save() error {
  247. if err := pv.Key.Save(); err != nil {
  248. return err
  249. }
  250. return pv.LastSignState.Save()
  251. }
  252. // Reset resets all fields in the FilePV.
  253. // NOTE: Unsafe!
  254. func (pv *FilePV) Reset() error {
  255. var sig []byte
  256. pv.LastSignState.Height = 0
  257. pv.LastSignState.Round = 0
  258. pv.LastSignState.Step = 0
  259. pv.LastSignState.Signature = sig
  260. pv.LastSignState.SignBytes = nil
  261. return pv.Save()
  262. }
  263. // String returns a string representation of the FilePV.
  264. func (pv *FilePV) String() string {
  265. return fmt.Sprintf(
  266. "PrivValidator{%v LH:%v, LR:%v, LS:%v}",
  267. pv.GetAddress(),
  268. pv.LastSignState.Height,
  269. pv.LastSignState.Round,
  270. pv.LastSignState.Step,
  271. )
  272. }
  273. //------------------------------------------------------------------------------------
  274. // signVote checks if the vote is good to sign and sets the vote signature.
  275. // It may need to set the timestamp as well if the vote is otherwise the same as
  276. // a previously signed vote (ie. we crashed after signing but before the vote hit the WAL).
  277. func (pv *FilePV) signVote(chainID string, vote *tmproto.Vote) error {
  278. step, err := voteToStep(vote)
  279. if err != nil {
  280. return err
  281. }
  282. height := vote.Height
  283. round := vote.Round
  284. lss := pv.LastSignState
  285. sameHRS, err := lss.CheckHRS(height, round, step)
  286. if err != nil {
  287. return err
  288. }
  289. signBytes := types.VoteSignBytes(chainID, vote)
  290. // We might crash before writing to the wal,
  291. // causing us to try to re-sign for the same HRS.
  292. // If signbytes are the same, use the last signature.
  293. // If they only differ by timestamp, use last timestamp and signature
  294. // Otherwise, return error
  295. if sameHRS {
  296. if bytes.Equal(signBytes, lss.SignBytes) {
  297. vote.Signature = lss.Signature
  298. } else {
  299. timestamp, ok, err := checkVotesOnlyDifferByTimestamp(lss.SignBytes, signBytes)
  300. if err != nil {
  301. return err
  302. }
  303. if !ok {
  304. return errors.New("conflicting data")
  305. }
  306. vote.Timestamp = timestamp
  307. vote.Signature = lss.Signature
  308. return nil
  309. }
  310. }
  311. // It passed the checks. Sign the vote
  312. sig, err := pv.Key.PrivKey.Sign(signBytes)
  313. if err != nil {
  314. return err
  315. }
  316. if err := pv.saveSigned(height, round, step, signBytes, sig); err != nil {
  317. return err
  318. }
  319. vote.Signature = sig
  320. return nil
  321. }
  322. // signProposal checks if the proposal is good to sign and sets the proposal signature.
  323. // It may need to set the timestamp as well if the proposal is otherwise the same as
  324. // a previously signed proposal ie. we crashed after signing but before the proposal hit the WAL).
  325. func (pv *FilePV) signProposal(chainID string, proposal *tmproto.Proposal) error {
  326. height, round, step := proposal.Height, proposal.Round, stepPropose
  327. lss := pv.LastSignState
  328. sameHRS, err := lss.CheckHRS(height, round, step)
  329. if err != nil {
  330. return err
  331. }
  332. signBytes := types.ProposalSignBytes(chainID, proposal)
  333. // We might crash before writing to the wal,
  334. // causing us to try to re-sign for the same HRS.
  335. // If signbytes are the same, use the last signature.
  336. // If they only differ by timestamp, use last timestamp and signature
  337. // Otherwise, return error
  338. if sameHRS {
  339. if bytes.Equal(signBytes, lss.SignBytes) {
  340. proposal.Signature = lss.Signature
  341. } else {
  342. timestamp, ok, err := checkProposalsOnlyDifferByTimestamp(lss.SignBytes, signBytes)
  343. if err != nil {
  344. return err
  345. }
  346. if !ok {
  347. return errors.New("conflicting data")
  348. }
  349. proposal.Timestamp = timestamp
  350. proposal.Signature = lss.Signature
  351. return nil
  352. }
  353. }
  354. // It passed the checks. Sign the proposal
  355. sig, err := pv.Key.PrivKey.Sign(signBytes)
  356. if err != nil {
  357. return err
  358. }
  359. if err := pv.saveSigned(height, round, step, signBytes, sig); err != nil {
  360. return err
  361. }
  362. proposal.Signature = sig
  363. return nil
  364. }
  365. // Persist height/round/step and signature
  366. func (pv *FilePV) saveSigned(height int64, round int32, step int8, signBytes []byte, sig []byte) error {
  367. pv.LastSignState.Height = height
  368. pv.LastSignState.Round = round
  369. pv.LastSignState.Step = step
  370. pv.LastSignState.Signature = sig
  371. pv.LastSignState.SignBytes = signBytes
  372. return pv.LastSignState.Save()
  373. }
  374. //-----------------------------------------------------------------------------------------
  375. // returns the timestamp from the lastSignBytes.
  376. // returns true if the only difference in the votes is their timestamp.
  377. func checkVotesOnlyDifferByTimestamp(lastSignBytes, newSignBytes []byte) (time.Time, bool, error) {
  378. var lastVote, newVote tmproto.CanonicalVote
  379. if err := protoio.UnmarshalDelimited(lastSignBytes, &lastVote); err != nil {
  380. return time.Time{}, false, fmt.Errorf("LastSignBytes cannot be unmarshalled into vote: %v", err)
  381. }
  382. if err := protoio.UnmarshalDelimited(newSignBytes, &newVote); err != nil {
  383. return time.Time{}, false, fmt.Errorf("signBytes cannot be unmarshalled into vote: %v", err)
  384. }
  385. lastTime := lastVote.Timestamp
  386. // set the times to the same value and check equality
  387. now := tmtime.Now()
  388. lastVote.Timestamp = now
  389. newVote.Timestamp = now
  390. return lastTime, proto.Equal(&newVote, &lastVote), nil
  391. }
  392. // returns the timestamp from the lastSignBytes.
  393. // returns true if the only difference in the proposals is their timestamp
  394. func checkProposalsOnlyDifferByTimestamp(lastSignBytes, newSignBytes []byte) (time.Time, bool, error) {
  395. var lastProposal, newProposal tmproto.CanonicalProposal
  396. if err := protoio.UnmarshalDelimited(lastSignBytes, &lastProposal); err != nil {
  397. return time.Time{}, false, fmt.Errorf("LastSignBytes cannot be unmarshalled into proposal: %v", err)
  398. }
  399. if err := protoio.UnmarshalDelimited(newSignBytes, &newProposal); err != nil {
  400. return time.Time{}, false, fmt.Errorf("signBytes cannot be unmarshalled into proposal: %v", err)
  401. }
  402. lastTime := lastProposal.Timestamp
  403. // set the times to the same value and check equality
  404. now := tmtime.Now()
  405. lastProposal.Timestamp = now
  406. newProposal.Timestamp = now
  407. return lastTime, proto.Equal(&newProposal, &lastProposal), nil
  408. }