checkpointoracle.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. running int32 // Flag whether the contract backend is set or not
  34. getLocal func(uint64) params.TrustedCheckpoint // Function used to retrieve local checkpoint
  35. }
  36. // newCheckpointOracle returns a checkpoint registrar handler.
  37. func newCheckpointOracle(config *params.CheckpointOracleConfig, getLocal func(uint64) params.TrustedCheckpoint) *checkpointOracle {
  38. if config == nil {
  39. log.Info("Checkpoint registrar is not enabled")
  40. return nil
  41. }
  42. if config.Address == (common.Address{}) || uint64(len(config.Signers)) < config.Threshold {
  43. log.Warn("Invalid checkpoint registrar config")
  44. return nil
  45. }
  46. log.Info("Configured checkpoint registrar", "address", config.Address, "signers", len(config.Signers), "threshold", config.Threshold)
  47. return &checkpointOracle{
  48. config: config,
  49. getLocal: getLocal,
  50. }
  51. }
  52. // start binds the registrar contract and start listening to the
  53. // newCheckpointEvent for the server side.
  54. func (reg *checkpointOracle) start(backend bind.ContractBackend) {
  55. contract, err := checkpointoracle.NewCheckpointOracle(reg.config.Address, backend)
  56. if err != nil {
  57. log.Error("Oracle contract binding failed", "err", err)
  58. return
  59. }
  60. if !atomic.CompareAndSwapInt32(&reg.running, 0, 1) {
  61. log.Error("Already bound and listening to registrar")
  62. return
  63. }
  64. reg.contract = contract
  65. }
  66. // isRunning returns an indicator whether the registrar is running.
  67. func (reg *checkpointOracle) isRunning() bool {
  68. return atomic.LoadInt32(&reg.running) == 1
  69. }
  70. // stableCheckpoint returns the stable checkpoint which was generated by local
  71. // indexers and announced by trusted signers.
  72. func (reg *checkpointOracle) stableCheckpoint() (*params.TrustedCheckpoint, uint64) {
  73. // Retrieve the latest checkpoint from the contract, abort if empty
  74. latest, hash, height, err := reg.contract.Contract().GetLatestCheckpoint(nil)
  75. if err != nil || (latest == 0 && hash == [32]byte{}) {
  76. return nil, 0
  77. }
  78. local := reg.getLocal(latest)
  79. // The following scenarios may occur:
  80. //
  81. // * local node is out of sync so that it doesn't have the
  82. // checkpoint which registered in the contract.
  83. // * local checkpoint doesn't match with the registered one.
  84. //
  85. // In both cases, server won't send the **stable** checkpoint
  86. // to the client(no worry, client can use hardcoded one instead).
  87. if local.HashEqual(common.Hash(hash)) {
  88. return &local, height.Uint64()
  89. }
  90. return nil, 0
  91. }
  92. // verifySigners recovers the signer addresses according to the signature and
  93. // checks whether there are enough approvals to finalize the checkpoint.
  94. func (reg *checkpointOracle) verifySigners(index uint64, hash [32]byte, signatures [][]byte) (bool, []common.Address) {
  95. // Short circuit if the given signatures doesn't reach the threshold.
  96. if len(signatures) < int(reg.config.Threshold) {
  97. return false, nil
  98. }
  99. var (
  100. signers []common.Address
  101. checked = make(map[common.Address]struct{})
  102. )
  103. for i := 0; i < len(signatures); i++ {
  104. if len(signatures[i]) != 65 {
  105. continue
  106. }
  107. // EIP 191 style signatures
  108. //
  109. // Arguments when calculating hash to validate
  110. // 1: byte(0x19) - the initial 0x19 byte
  111. // 2: byte(0) - the version byte (data with intended validator)
  112. // 3: this - the validator address
  113. // -- Application specific data
  114. // 4 : checkpoint section_index (uint64)
  115. // 5 : checkpoint hash (bytes32)
  116. // hash = keccak256(checkpoint_index, section_head, cht_root, bloom_root)
  117. buf := make([]byte, 8)
  118. binary.BigEndian.PutUint64(buf, index)
  119. data := append([]byte{0x19, 0x00}, append(reg.config.Address.Bytes(), append(buf, hash[:]...)...)...)
  120. signatures[i][64] -= 27 // Transform V from 27/28 to 0/1 according to the yellow paper for verification.
  121. pubkey, err := crypto.Ecrecover(crypto.Keccak256(data), signatures[i])
  122. if err != nil {
  123. return false, nil
  124. }
  125. var signer common.Address
  126. copy(signer[:], crypto.Keccak256(pubkey[1:])[12:])
  127. if _, exist := checked[signer]; exist {
  128. continue
  129. }
  130. for _, s := range reg.config.Signers {
  131. if s == signer {
  132. signers = append(signers, signer)
  133. checked[signer] = struct{}{}
  134. }
  135. }
  136. }
  137. threshold := reg.config.Threshold
  138. if uint64(len(signers)) < threshold {
  139. log.Warn("Not enough signers to approve checkpoint", "signers", len(signers), "threshold", threshold)
  140. return false, nil
  141. }
  142. return true, signers
  143. }