commons.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // Copyright 2018 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. "fmt"
  19. "math/big"
  20. "sync"
  21. "github.com/ethereum/go-ethereum/common"
  22. "github.com/ethereum/go-ethereum/core"
  23. "github.com/ethereum/go-ethereum/core/rawdb"
  24. "github.com/ethereum/go-ethereum/core/types"
  25. "github.com/ethereum/go-ethereum/eth"
  26. "github.com/ethereum/go-ethereum/ethclient"
  27. "github.com/ethereum/go-ethereum/ethdb"
  28. "github.com/ethereum/go-ethereum/les/checkpointoracle"
  29. "github.com/ethereum/go-ethereum/light"
  30. "github.com/ethereum/go-ethereum/log"
  31. "github.com/ethereum/go-ethereum/node"
  32. "github.com/ethereum/go-ethereum/p2p"
  33. "github.com/ethereum/go-ethereum/p2p/discv5"
  34. "github.com/ethereum/go-ethereum/p2p/enode"
  35. "github.com/ethereum/go-ethereum/params"
  36. )
  37. func errResp(code errCode, format string, v ...interface{}) error {
  38. return fmt.Errorf("%v - %v", code, fmt.Sprintf(format, v...))
  39. }
  40. func lesTopic(genesisHash common.Hash, protocolVersion uint) discv5.Topic {
  41. var name string
  42. switch protocolVersion {
  43. case lpv2:
  44. name = "LES2"
  45. default:
  46. panic(nil)
  47. }
  48. return discv5.Topic(name + "@" + common.Bytes2Hex(genesisHash.Bytes()[0:8]))
  49. }
  50. type chainReader interface {
  51. CurrentHeader() *types.Header
  52. }
  53. // lesCommons contains fields needed by both server and client.
  54. type lesCommons struct {
  55. genesis common.Hash
  56. config *eth.Config
  57. chainConfig *params.ChainConfig
  58. iConfig *light.IndexerConfig
  59. chainDb ethdb.Database
  60. chainReader chainReader
  61. chtIndexer, bloomTrieIndexer *core.ChainIndexer
  62. oracle *checkpointoracle.CheckpointOracle
  63. closeCh chan struct{}
  64. wg sync.WaitGroup
  65. }
  66. // NodeInfo represents a short summary of the Ethereum sub-protocol metadata
  67. // known about the host peer.
  68. type NodeInfo struct {
  69. Network uint64 `json:"network"` // Ethereum network ID (1=Frontier, 2=Morden, Ropsten=3, Rinkeby=4)
  70. Difficulty *big.Int `json:"difficulty"` // Total difficulty of the host's blockchain
  71. Genesis common.Hash `json:"genesis"` // SHA3 hash of the host's genesis block
  72. Config *params.ChainConfig `json:"config"` // Chain configuration for the fork rules
  73. Head common.Hash `json:"head"` // SHA3 hash of the host's best owned block
  74. CHT params.TrustedCheckpoint `json:"cht"` // Trused CHT checkpoint for fast catchup
  75. }
  76. // makeProtocols creates protocol descriptors for the given LES versions.
  77. func (c *lesCommons) makeProtocols(versions []uint, runPeer func(version uint, p *p2p.Peer, rw p2p.MsgReadWriter) error, peerInfo func(id enode.ID) interface{}, dialCandidates enode.Iterator) []p2p.Protocol {
  78. protos := make([]p2p.Protocol, len(versions))
  79. for i, version := range versions {
  80. version := version
  81. protos[i] = p2p.Protocol{
  82. Name: "les",
  83. Version: version,
  84. Length: ProtocolLengths[version],
  85. NodeInfo: c.nodeInfo,
  86. Run: func(peer *p2p.Peer, rw p2p.MsgReadWriter) error {
  87. return runPeer(version, peer, rw)
  88. },
  89. PeerInfo: peerInfo,
  90. DialCandidates: dialCandidates,
  91. }
  92. }
  93. return protos
  94. }
  95. // nodeInfo retrieves some protocol metadata about the running host node.
  96. func (c *lesCommons) nodeInfo() interface{} {
  97. head := c.chainReader.CurrentHeader()
  98. hash := head.Hash()
  99. return &NodeInfo{
  100. Network: c.config.NetworkId,
  101. Difficulty: rawdb.ReadTd(c.chainDb, hash, head.Number.Uint64()),
  102. Genesis: c.genesis,
  103. Config: c.chainConfig,
  104. Head: hash,
  105. CHT: c.latestLocalCheckpoint(),
  106. }
  107. }
  108. // latestLocalCheckpoint finds the common stored section index and returns a set
  109. // of post-processed trie roots (CHT and BloomTrie) associated with the appropriate
  110. // section index and head hash as a local checkpoint package.
  111. func (c *lesCommons) latestLocalCheckpoint() params.TrustedCheckpoint {
  112. sections, _, _ := c.chtIndexer.Sections()
  113. sections2, _, _ := c.bloomTrieIndexer.Sections()
  114. // Cap the section index if the two sections are not consistent.
  115. if sections > sections2 {
  116. sections = sections2
  117. }
  118. if sections == 0 {
  119. // No checkpoint information can be provided.
  120. return params.TrustedCheckpoint{}
  121. }
  122. return c.localCheckpoint(sections - 1)
  123. }
  124. // localCheckpoint returns a set of post-processed trie roots (CHT and BloomTrie)
  125. // associated with the appropriate head hash by specific section index.
  126. //
  127. // The returned checkpoint is only the checkpoint generated by the local indexers,
  128. // not the stable checkpoint registered in the registrar contract.
  129. func (c *lesCommons) localCheckpoint(index uint64) params.TrustedCheckpoint {
  130. sectionHead := c.chtIndexer.SectionHead(index)
  131. return params.TrustedCheckpoint{
  132. SectionIndex: index,
  133. SectionHead: sectionHead,
  134. CHTRoot: light.GetChtRoot(c.chainDb, index, sectionHead),
  135. BloomRoot: light.GetBloomTrieRoot(c.chainDb, index, sectionHead),
  136. }
  137. }
  138. // setupOracle sets up the checkpoint oracle contract client.
  139. func (c *lesCommons) setupOracle(node *node.Node, genesis common.Hash, ethconfig *eth.Config) *checkpointoracle.CheckpointOracle {
  140. config := ethconfig.CheckpointOracle
  141. if config == nil {
  142. // Try loading default config.
  143. config = params.CheckpointOracles[genesis]
  144. }
  145. if config == nil {
  146. log.Info("Checkpoint registrar is not enabled")
  147. return nil
  148. }
  149. if config.Address == (common.Address{}) || uint64(len(config.Signers)) < config.Threshold {
  150. log.Warn("Invalid checkpoint registrar config")
  151. return nil
  152. }
  153. oracle := checkpointoracle.New(config, c.localCheckpoint)
  154. rpcClient, _ := node.Attach()
  155. client := ethclient.NewClient(rpcClient)
  156. oracle.Start(client)
  157. log.Info("Configured checkpoint registrar", "address", config.Address, "signers", len(config.Signers), "threshold", config.Threshold)
  158. return oracle
  159. }