commons.go 5.4 KB

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