github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/les/commons.go (about) 1 2 //<developer> 3 // <name>linapex 曹一峰</name> 4 // <email>linapex@163.com</email> 5 // <wx>superexc</wx> 6 // <qqgroup>128148617</qqgroup> 7 // <url>https://jsq.ink</url> 8 // <role>pku engineer</role> 9 // <date>2019-03-16 19:16:38</date> 10 //</624450093306613760> 11 12 13 package les 14 15 import ( 16 "fmt" 17 "math/big" 18 19 "github.com/ethereum/go-ethereum/common" 20 "github.com/ethereum/go-ethereum/core" 21 "github.com/ethereum/go-ethereum/eth" 22 "github.com/ethereum/go-ethereum/ethdb" 23 "github.com/ethereum/go-ethereum/light" 24 "github.com/ethereum/go-ethereum/p2p" 25 "github.com/ethereum/go-ethereum/p2p/enode" 26 "github.com/ethereum/go-ethereum/params" 27 ) 28 29 //lecommons包含服务器和客户机都需要的字段。 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 38 //nodeinfo表示以太坊子协议元数据的简短摘要 39 //了解主机对等机。 40 type NodeInfo struct { 41 Network uint64 `json:"network"` //以太坊网络ID(1=前沿,2=现代,Ropsten=3,Rinkeby=4) 42 Difficulty *big.Int `json:"difficulty"` //主机区块链的总难度 43 Genesis common.Hash `json:"genesis"` //寄主创世纪区块的沙3哈希 44 Config *params.ChainConfig `json:"config"` //分叉规则的链配置 45 Head common.Hash `json:"head"` //主机最好拥有的块的sha3哈希 46 CHT params.TrustedCheckpoint `json:"cht"` //桁架式CHT检查站,快速接球 47 } 48 49 //makeprotocols为给定的les版本创建协议描述符。 50 func (c *lesCommons) makeProtocols(versions []uint) []p2p.Protocol { 51 protos := make([]p2p.Protocol, len(versions)) 52 for i, version := range versions { 53 version := version 54 protos[i] = p2p.Protocol{ 55 Name: "les", 56 Version: version, 57 Length: ProtocolLengths[version], 58 NodeInfo: c.nodeInfo, 59 Run: func(p *p2p.Peer, rw p2p.MsgReadWriter) error { 60 return c.protocolManager.runPeer(version, p, rw) 61 }, 62 PeerInfo: func(id enode.ID) interface{} { 63 if p := c.protocolManager.peers.Peer(fmt.Sprintf("%x", id[:8])); p != nil { 64 return p.Info() 65 } 66 return nil 67 }, 68 } 69 } 70 return protos 71 } 72 73 //nodeinfo检索有关正在运行的主机节点的一些协议元数据。 74 func (c *lesCommons) nodeInfo() interface{} { 75 var cht params.TrustedCheckpoint 76 sections, _, _ := c.chtIndexer.Sections() 77 sections2, _, _ := c.bloomTrieIndexer.Sections() 78 79 if !c.protocolManager.lightSync { 80 //如果在服务器模式下运行,则转换为客户端节大小 81 sections /= c.iConfig.PairChtSize / c.iConfig.ChtSize 82 } 83 84 if sections2 < sections { 85 sections = sections2 86 } 87 if sections > 0 { 88 sectionIndex := sections - 1 89 sectionHead := c.bloomTrieIndexer.SectionHead(sectionIndex) 90 var chtRoot common.Hash 91 if c.protocolManager.lightSync { 92 chtRoot = light.GetChtRoot(c.chainDb, sectionIndex, sectionHead) 93 } else { 94 idxV2 := (sectionIndex+1)*c.iConfig.PairChtSize/c.iConfig.ChtSize - 1 95 chtRoot = light.GetChtRoot(c.chainDb, idxV2, sectionHead) 96 } 97 cht = params.TrustedCheckpoint{ 98 SectionIndex: sectionIndex, 99 SectionHead: sectionHead, 100 CHTRoot: chtRoot, 101 BloomRoot: light.GetBloomTrieRoot(c.chainDb, sectionIndex, sectionHead), 102 } 103 } 104 105 chain := c.protocolManager.blockchain 106 head := chain.CurrentHeader() 107 hash := head.Hash() 108 return &NodeInfo{ 109 Network: c.config.NetworkId, 110 Difficulty: chain.GetTd(hash, head.Number.Uint64()), 111 Genesis: chain.Genesis().Hash(), 112 Config: chain.Config(), 113 Head: chain.CurrentHeader().Hash(), 114 CHT: cht, 115 } 116 } 117