commons.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. "github.com/ethereum/go-ethereum/common"
  21. "github.com/ethereum/go-ethereum/core"
  22. "github.com/ethereum/go-ethereum/eth"
  23. "github.com/ethereum/go-ethereum/ethdb"
  24. "github.com/ethereum/go-ethereum/light"
  25. "github.com/ethereum/go-ethereum/p2p"
  26. "github.com/ethereum/go-ethereum/p2p/enode"
  27. "github.com/ethereum/go-ethereum/params"
  28. )
  29. // lesCommons contains fields needed by both server and client.
  30. type lesCommons struct {
  31. config *eth.Config
  32. iConfig *light.IndexerConfig
  33. chainDb ethdb.Database
  34. protocolManager *ProtocolManager
  35. chtIndexer, bloomTrieIndexer *core.ChainIndexer
  36. }
  37. // NodeInfo represents a short summary of the Ethereum sub-protocol metadata
  38. // known about the host peer.
  39. type NodeInfo struct {
  40. Network uint64 `json:"network"` // Ethereum network ID (1=Frontier, 2=Morden, Ropsten=3, Rinkeby=4)
  41. Difficulty *big.Int `json:"difficulty"` // Total difficulty of the host's blockchain
  42. Genesis common.Hash `json:"genesis"` // SHA3 hash of the host's genesis block
  43. Config *params.ChainConfig `json:"config"` // Chain configuration for the fork rules
  44. Head common.Hash `json:"head"` // SHA3 hash of the host's best owned block
  45. CHT params.TrustedCheckpoint `json:"cht"` // Trused CHT checkpoint for fast catchup
  46. }
  47. // makeProtocols creates protocol descriptors for the given LES versions.
  48. func (c *lesCommons) makeProtocols(versions []uint) []p2p.Protocol {
  49. protos := make([]p2p.Protocol, len(versions))
  50. for i, version := range versions {
  51. version := version
  52. protos[i] = p2p.Protocol{
  53. Name: "les",
  54. Version: version,
  55. Length: ProtocolLengths[version],
  56. NodeInfo: c.nodeInfo,
  57. Run: func(p *p2p.Peer, rw p2p.MsgReadWriter) error {
  58. return c.protocolManager.runPeer(version, p, rw)
  59. },
  60. PeerInfo: func(id enode.ID) interface{} {
  61. if p := c.protocolManager.peers.Peer(fmt.Sprintf("%x", id.Bytes())); p != nil {
  62. return p.Info()
  63. }
  64. return nil
  65. },
  66. }
  67. }
  68. return protos
  69. }
  70. // nodeInfo retrieves some protocol metadata about the running host node.
  71. func (c *lesCommons) nodeInfo() interface{} {
  72. var cht params.TrustedCheckpoint
  73. sections, _, _ := c.chtIndexer.Sections()
  74. sections2, _, _ := c.bloomTrieIndexer.Sections()
  75. if sections2 < sections {
  76. sections = sections2
  77. }
  78. if sections > 0 {
  79. sectionIndex := sections - 1
  80. sectionHead := c.bloomTrieIndexer.SectionHead(sectionIndex)
  81. cht = params.TrustedCheckpoint{
  82. SectionIndex: sectionIndex,
  83. SectionHead: sectionHead,
  84. CHTRoot: light.GetChtRoot(c.chainDb, sectionIndex, sectionHead),
  85. BloomRoot: light.GetBloomTrieRoot(c.chainDb, sectionIndex, sectionHead),
  86. }
  87. }
  88. chain := c.protocolManager.blockchain
  89. head := chain.CurrentHeader()
  90. hash := head.Hash()
  91. return &NodeInfo{
  92. Network: c.config.NetworkId,
  93. Difficulty: chain.GetTd(hash, head.Number.Uint64()),
  94. Genesis: chain.Genesis().Hash(),
  95. Config: chain.Config(),
  96. Head: chain.CurrentHeader().Hash(),
  97. CHT: cht,
  98. }
  99. }