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.

216 lines
6.0 KiB

  1. package lite
  2. import (
  3. "bytes"
  4. lerr "github.com/tendermint/tendermint/lite/errors"
  5. "github.com/tendermint/tendermint/types"
  6. log "github.com/tendermint/tmlibs/log"
  7. )
  8. var _ Certifier = (*InquiringCertifier)(nil)
  9. // InquiringCertifier implements an auto-updating certifier. It uses a
  10. // "source" provider to obtain the needed FullCommits to securely sync with
  11. // validator set changes. It stores properly validated data on the
  12. // "trusted" local system.
  13. type InquiringCertifier struct {
  14. logger log.Logger
  15. chainID string
  16. // These are only properly validated data, from local system.
  17. trusted PersistentProvider
  18. // This is a source of new info, like a node rpc, or other import method.
  19. source Provider
  20. }
  21. // NewInquiringCertifier returns a new InquiringCertifier. It uses the
  22. // trusted provider to store validated data and the source provider to
  23. // obtain missing data (e.g. FullCommits).
  24. //
  25. // The trusted provider should a CacheProvider, MemProvider or
  26. // files.Provider. The source provider should be a client.HTTPProvider.
  27. func NewInquiringCertifier(chainID string, trusted PersistentProvider, source Provider) *InquiringCertifier {
  28. return &InquiringCertifier{
  29. logger: log.NewNopLogger(),
  30. chainID: chainID,
  31. trusted: trusted,
  32. source: source,
  33. }
  34. }
  35. func (ic *InquiringCertifier) SetLogger(logger log.Logger) {
  36. logger = logger.With("module", "lite")
  37. ic.logger = logger
  38. ic.trusted.SetLogger(logger)
  39. ic.source.SetLogger(logger)
  40. }
  41. // Implements Certifier.
  42. func (ic *InquiringCertifier) ChainID() string {
  43. return ic.chainID
  44. }
  45. // Implements Certifier.
  46. //
  47. // If the validators have changed since the last know time, it looks to
  48. // ic.trusted and ic.source to prove the new validators. On success, it will
  49. // try to store the SignedHeader in ic.trusted if the next
  50. // validator can be sourced.
  51. func (ic *InquiringCertifier) Certify(shdr types.SignedHeader) error {
  52. // Get the latest known full commit <= h-1 from our trusted providers.
  53. // The full commit at h-1 contains the valset to sign for h.
  54. h := shdr.Height - 1
  55. tfc, err := ic.trusted.LatestFullCommit(ic.chainID, 1, h)
  56. if err != nil {
  57. return err
  58. }
  59. if tfc.Height() == h {
  60. // Return error if valset doesn't match.
  61. if !bytes.Equal(
  62. tfc.NextValidators.Hash(),
  63. shdr.Header.ValidatorsHash) {
  64. return lerr.ErrUnexpectedValidators(
  65. tfc.NextValidators.Hash(),
  66. shdr.Header.ValidatorsHash)
  67. }
  68. } else {
  69. // If valset doesn't match...
  70. if !bytes.Equal(tfc.NextValidators.Hash(),
  71. shdr.Header.ValidatorsHash) {
  72. // ... update.
  73. tfc, err = ic.updateToHeight(h)
  74. if err != nil {
  75. return err
  76. }
  77. // Return error if valset _still_ doesn't match.
  78. if !bytes.Equal(tfc.NextValidators.Hash(),
  79. shdr.Header.ValidatorsHash) {
  80. return lerr.ErrUnexpectedValidators(
  81. tfc.NextValidators.Hash(),
  82. shdr.Header.ValidatorsHash)
  83. }
  84. }
  85. }
  86. // Certify the signed header using the matching valset.
  87. cert := NewBaseCertifier(ic.chainID, tfc.Height()+1, tfc.NextValidators)
  88. err = cert.Certify(shdr)
  89. if err != nil {
  90. return err
  91. }
  92. // Get the next validator set.
  93. nextValset, err := ic.source.ValidatorSet(ic.chainID, shdr.Height+1)
  94. if lerr.IsErrMissingValidators(err) {
  95. // Ignore this error.
  96. return nil
  97. } else if err != nil {
  98. return err
  99. }
  100. // Create filled FullCommit.
  101. nfc := FullCommit{
  102. SignedHeader: shdr,
  103. Validators: tfc.NextValidators,
  104. NextValidators: nextValset,
  105. }
  106. // Validate the full commit. This checks the cryptographic
  107. // signatures of Commit against Validators.
  108. if err := nfc.ValidateFull(ic.chainID); err != nil {
  109. return err
  110. }
  111. // Trust it.
  112. return ic.trusted.SaveFullCommit(nfc)
  113. }
  114. // verifyAndSave will verify if this is a valid source full commit given the
  115. // best match trusted full commit, and if good, persist to ic.trusted.
  116. // Returns ErrTooMuchChange when >2/3 of tfc did not sign sfc.
  117. // Panics if tfc.Height() >= sfc.Height().
  118. func (ic *InquiringCertifier) verifyAndSave(tfc, sfc FullCommit) error {
  119. if tfc.Height() >= sfc.Height() {
  120. panic("should not happen")
  121. }
  122. err := tfc.NextValidators.VerifyFutureCommit(
  123. sfc.Validators,
  124. ic.chainID, sfc.SignedHeader.Commit.BlockID,
  125. sfc.SignedHeader.Height, sfc.SignedHeader.Commit,
  126. )
  127. if err != nil {
  128. return err
  129. }
  130. return ic.trusted.SaveFullCommit(sfc)
  131. }
  132. // updateToHeight will use divide-and-conquer to find a path to h.
  133. // Returns nil error iff we successfully verify and persist a full commit
  134. // for height h, using repeated applications of bisection if necessary.
  135. //
  136. // Returns ErrCommitNotFound if source provider doesn't have the commit for h.
  137. func (ic *InquiringCertifier) updateToHeight(h int64) (FullCommit, error) {
  138. // Fetch latest full commit from source.
  139. sfc, err := ic.source.LatestFullCommit(ic.chainID, h, h)
  140. if err != nil {
  141. return FullCommit{}, err
  142. }
  143. // Validate the full commit. This checks the cryptographic
  144. // signatures of Commit against Validators.
  145. if err := sfc.ValidateFull(ic.chainID); err != nil {
  146. return FullCommit{}, err
  147. }
  148. // If sfc.Height() != h, we can't do it.
  149. if sfc.Height() != h {
  150. return FullCommit{}, lerr.ErrCommitNotFound()
  151. }
  152. FOR_LOOP:
  153. for {
  154. // Fetch latest full commit from trusted.
  155. tfc, err := ic.trusted.LatestFullCommit(ic.chainID, 1, h)
  156. if err != nil {
  157. return FullCommit{}, err
  158. }
  159. // We have nothing to do.
  160. if tfc.Height() == h {
  161. return tfc, nil
  162. }
  163. // Try to update to full commit with checks.
  164. err = ic.verifyAndSave(tfc, sfc)
  165. if err == nil {
  166. // All good!
  167. return sfc, nil
  168. }
  169. // Handle special case when err is ErrTooMuchChange.
  170. if lerr.IsErrTooMuchChange(err) {
  171. // Divide and conquer.
  172. start, end := tfc.Height(), sfc.Height()
  173. if !(start < end) {
  174. panic("should not happen")
  175. }
  176. mid := (start + end) / 2
  177. _, err = ic.updateToHeight(mid)
  178. if err != nil {
  179. return FullCommit{}, err
  180. }
  181. // If we made it to mid, we retry.
  182. continue FOR_LOOP
  183. }
  184. return FullCommit{}, err
  185. }
  186. }
  187. func (ic *InquiringCertifier) LastTrustedHeight() int64 {
  188. fc, err := ic.trusted.LatestFullCommit(ic.chainID, 1, 1<<63-1)
  189. if err != nil {
  190. panic("should not happen")
  191. }
  192. return fc.Height()
  193. }