checkpointoracle.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // Copyright 2019 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package les
  17. import (
  18. "encoding/binary"
  19. "sync/atomic"
  20. "github.com/ethereum/go-ethereum/accounts/abi/bind"
  21. "github.com/ethereum/go-ethereum/common"
  22. "github.com/ethereum/go-ethereum/contracts/checkpointoracle"
  23. "github.com/ethereum/go-ethereum/crypto"
  24. "github.com/ethereum/go-ethereum/log"
  25. "github.com/ethereum/go-ethereum/params"
  26. )
  27. // checkpointOracle is responsible for offering the latest stable checkpoint
  28. // generated and announced by the contract admins on-chain. The checkpoint is
  29. // verified by clients locally during the checkpoint syncing.
  30. type checkpointOracle struct {
  31. config *params.CheckpointOracleConfig
  32. contract *checkpointoracle.CheckpointOracle
  33. // Whether the contract backend is set.
  34. running int32
  35. getLocal func(uint64) params.TrustedCheckpoint // Function used to retrieve local checkpoint
  36. syncDoneHook func() // Function used to notify that light syncing has completed.
  37. }
  38. // newCheckpointOracle returns a checkpoint registrar handler.
  39. func newCheckpointOracle(config *params.CheckpointOracleConfig, getLocal func(uint64) params.TrustedCheckpoint) *checkpointOracle {
  40. if config == nil {
  41. log.Info("Checkpoint registrar is not enabled")
  42. return nil
  43. }
  44. if config.Address == (common.Address{}) || uint64(len(config.Signers)) < config.Threshold {
  45. log.Warn("Invalid checkpoint registrar config")
  46. return nil
  47. }
  48. log.Info("Configured checkpoint registrar", "address", config.Address, "signers", len(config.Signers), "threshold", config.Threshold)
  49. return &checkpointOracle{
  50. config: config,
  51. getLocal: getLocal,
  52. }
  53. }
  54. // start binds the registrar contract and start listening to the
  55. // newCheckpointEvent for the server side.
  56. func (reg *checkpointOracle) start(backend bind.ContractBackend) {
  57. contract, err := checkpointoracle.NewCheckpointOracle(reg.config.Address, backend)
  58. if err != nil {
  59. log.Error("Oracle contract binding failed", "err", err)
  60. return
  61. }
  62. if !atomic.CompareAndSwapInt32(&reg.running, 0, 1) {
  63. log.Error("Already bound and listening to registrar")
  64. return
  65. }
  66. reg.contract = contract
  67. }
  68. // isRunning returns an indicator whether the registrar is running.
  69. func (reg *checkpointOracle) isRunning() bool {
  70. return atomic.LoadInt32(&reg.running) == 1
  71. }
  72. // stableCheckpoint returns the stable checkpoint which was generated by local
  73. // indexers and announced by trusted signers.
  74. func (reg *checkpointOracle) stableCheckpoint() (*params.TrustedCheckpoint, uint64) {
  75. // Retrieve the latest checkpoint from the contract, abort if empty
  76. latest, hash, height, err := reg.contract.Contract().GetLatestCheckpoint(nil)
  77. if err != nil || (latest == 0 && hash == [32]byte{}) {
  78. return nil, 0
  79. }
  80. local := reg.getLocal(latest)
  81. // The following scenarios may occur:
  82. //
  83. // * local node is out of sync so that it doesn't have the
  84. // checkpoint which registered in the contract.
  85. // * local checkpoint doesn't match with the registered one.
  86. //
  87. // In both cases, server won't send the **stable** checkpoint
  88. // to the client(no worry, client can use hardcoded one instead).
  89. if local.HashEqual(common.Hash(hash)) {
  90. return &local, height.Uint64()
  91. }
  92. return nil, 0
  93. }
  94. // verifySigners recovers the signer addresses according to the signature and
  95. // checks whether there are enough approvals to finalize the checkpoint.
  96. func (reg *checkpointOracle) verifySigners(index uint64, hash [32]byte, signatures [][]byte) (bool, []common.Address) {
  97. // Short circuit if the given signatures doesn't reach the threshold.
  98. if len(signatures) < int(reg.config.Threshold) {
  99. return false, nil
  100. }
  101. var (
  102. signers []common.Address
  103. checked = make(map[common.Address]struct{})
  104. )
  105. for i := 0; i < len(signatures); i++ {
  106. if len(signatures[i]) != 65 {
  107. continue
  108. }
  109. // EIP 191 style signatures
  110. //
  111. // Arguments when calculating hash to validate
  112. // 1: byte(0x19) - the initial 0x19 byte
  113. // 2: byte(0) - the version byte (data with intended validator)
  114. // 3: this - the validator address
  115. // -- Application specific data
  116. // 4 : checkpoint section_index (uint64)
  117. // 5 : checkpoint hash (bytes32)
  118. // hash = keccak256(checkpoint_index, section_head, cht_root, bloom_root)
  119. buf := make([]byte, 8)
  120. binary.BigEndian.PutUint64(buf, index)
  121. data := append([]byte{0x19, 0x00}, append(reg.config.Address.Bytes(), append(buf, hash[:]...)...)...)
  122. signatures[i][64] -= 27 // Transform V from 27/28 to 0/1 according to the yellow paper for verification.
  123. pubkey, err := crypto.Ecrecover(crypto.Keccak256(data), signatures[i])
  124. if err != nil {
  125. return false, nil
  126. }
  127. var signer common.Address
  128. copy(signer[:], crypto.Keccak256(pubkey[1:])[12:])
  129. if _, exist := checked[signer]; exist {
  130. continue
  131. }
  132. for _, s := range reg.config.Signers {
  133. if s == signer {
  134. signers = append(signers, signer)
  135. checked[signer] = struct{}{}
  136. }
  137. }
  138. }
  139. threshold := reg.config.Threshold
  140. if uint64(len(signers)) < threshold {
  141. log.Warn("Not enough signers to approve checkpoint", "signers", len(signers), "threshold", threshold)
  142. return false, nil
  143. }
  144. return true, signers
  145. }