commons.go 6.1 KB

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