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