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