github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/mobile/geth.go (about) 1 2 //此源码被清华学神尹成大魔王专业翻译分析并修改 3 //尹成QQ77025077 4 //尹成微信18510341407 5 //尹成所在QQ群721929980 6 //尹成邮箱 yinc13@mails.tsinghua.edu.cn 7 //尹成毕业于清华大学,微软区块链领域全球最有价值专家 8 //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620 9 //版权所有2016 Go Ethereum作者 10 //此文件是Go以太坊库的一部分。 11 // 12 //Go-Ethereum库是免费软件:您可以重新分发它和/或修改 13 //根据GNU发布的较低通用公共许可证的条款 14 //自由软件基金会,或者许可证的第3版,或者 15 //(由您选择)任何更高版本。 16 // 17 //Go以太坊图书馆的发行目的是希望它会有用, 18 //但没有任何保证;甚至没有 19 //适销性或特定用途的适用性。见 20 //GNU较低的通用公共许可证,了解更多详细信息。 21 // 22 //你应该收到一份GNU较低级别的公共许可证副本 23 //以及Go以太坊图书馆。如果没有,请参见<http://www.gnu.org/licenses/>。 24 25 //包含节点包中支持客户端节点的所有包装器 26 //移动平台管理。 27 28 package geth 29 30 import ( 31 "encoding/json" 32 "fmt" 33 "path/filepath" 34 35 "github.com/ethereum/go-ethereum/core" 36 "github.com/ethereum/go-ethereum/eth" 37 "github.com/ethereum/go-ethereum/eth/downloader" 38 "github.com/ethereum/go-ethereum/ethclient" 39 "github.com/ethereum/go-ethereum/ethstats" 40 "github.com/ethereum/go-ethereum/internal/debug" 41 "github.com/ethereum/go-ethereum/les" 42 "github.com/ethereum/go-ethereum/node" 43 "github.com/ethereum/go-ethereum/p2p" 44 "github.com/ethereum/go-ethereum/p2p/nat" 45 "github.com/ethereum/go-ethereum/params" 46 whisper "github.com/ethereum/go-ethereum/whisper/whisperv6" 47 ) 48 49 //nodeconfig表示对geth进行微调的配置值集合。 50 //嵌入到移动进程中的节点。可用值是 51 //Go Ethereum提供的整个API,以减少维护面和开发 52 //复杂性。 53 type NodeConfig struct { 54 //引导节点用于建立与网络其余部分的连接。 55 BootstrapNodes *Enodes 56 57 //MaxPeers是可以连接的最大对等数。如果这是 58 //设置为零,则只有配置的静态和受信任对等方可以连接。 59 MaxPeers int 60 61 //ethereumenabled指定节点是否应运行ethereum协议。 62 EthereumEnabled bool 63 64 //ethereumnetworkid是以太坊协议用于 65 //决定是否接受远程对等。 66 EthereumNetworkID int64 //UIT64实际上是,但Java不能处理… 67 68 //以太坊是用来为区块链播种的Genesis JSON。安 69 //空的genesis状态相当于使用mainnet的状态。 70 EthereumGenesis string 71 72 //ethereumdatabasecache是以MB为单位分配给数据库缓存的系统内存。 73 //始终保留至少16MB。 74 EthereumDatabaseCache int 75 76 //ethereumnetstats是一个netstats连接字符串,用于报告各种 77 //链、事务和节点状态到监控服务器。 78 // 79 //格式为“nodename:secret@host:port” 80 EthereumNetStats string 81 82 //WhisperEnabled指定节点是否应运行Whisper协议。 83 WhisperEnabled bool 84 85 //pprof服务器的侦听地址。 86 PprofAddress string 87 } 88 89 //defaultnodeconfig包含默认节点配置值(如果全部使用) 90 //或者用户指定列表中缺少某些字段。 91 var defaultNodeConfig = &NodeConfig{ 92 BootstrapNodes: FoundationBootnodes(), 93 MaxPeers: 25, 94 EthereumEnabled: true, 95 EthereumNetworkID: 1, 96 EthereumDatabaseCache: 16, 97 } 98 99 //newnodeconfig创建一个新的节点选项集,初始化为默认值。 100 func NewNodeConfig() *NodeConfig { 101 config := *defaultNodeConfig 102 return &config 103 } 104 105 //节点表示一个geth-ethereum节点实例。 106 type Node struct { 107 node *node.Node 108 } 109 110 //new node创建和配置新的geth节点。 111 func NewNode(datadir string, config *NodeConfig) (stack *Node, _ error) { 112 //如果未指定或未指定部分配置,请使用默认值 113 if config == nil { 114 config = NewNodeConfig() 115 } 116 if config.MaxPeers == 0 { 117 config.MaxPeers = defaultNodeConfig.MaxPeers 118 } 119 if config.BootstrapNodes == nil || config.BootstrapNodes.Size() == 0 { 120 config.BootstrapNodes = defaultNodeConfig.BootstrapNodes 121 } 122 123 if config.PprofAddress != "" { 124 debug.StartPProf(config.PprofAddress) 125 } 126 127 //创建空的网络堆栈 128 nodeConf := &node.Config{ 129 Name: clientIdentifier, 130 Version: params.VersionWithMeta, 131 DataDir: datadir, 132 KeyStoreDir: filepath.Join(datadir, "keystore"), //手机不应使用内部密钥库! 133 P2P: p2p.Config{ 134 NoDiscovery: true, 135 DiscoveryV5: true, 136 BootstrapNodesV5: config.BootstrapNodes.nodes, 137 ListenAddr: ":0", 138 NAT: nat.Any(), 139 MaxPeers: config.MaxPeers, 140 }, 141 } 142 rawStack, err := node.New(nodeConf) 143 if err != nil { 144 return nil, err 145 } 146 147 debug.Memsize.Add("node", rawStack) 148 149 var genesis *core.Genesis 150 if config.EthereumGenesis != "" { 151 //分析用户提供的Genesis规范(如果不是Mainnet) 152 genesis = new(core.Genesis) 153 if err := json.Unmarshal([]byte(config.EthereumGenesis), genesis); err != nil { 154 return nil, fmt.Errorf("invalid genesis spec: %v", err) 155 } 156 //如果我们有了测试网,那么也要对链配置进行硬编码。 157 if config.EthereumGenesis == TestnetGenesis() { 158 genesis.Config = params.TestnetChainConfig 159 if config.EthereumNetworkID == 1 { 160 config.EthereumNetworkID = 3 161 } 162 } 163 } 164 //如果需要,注册以太坊协议 165 if config.EthereumEnabled { 166 ethConf := eth.DefaultConfig 167 ethConf.Genesis = genesis 168 ethConf.SyncMode = downloader.LightSync 169 ethConf.NetworkId = uint64(config.EthereumNetworkID) 170 ethConf.DatabaseCache = config.EthereumDatabaseCache 171 if err := rawStack.Register(func(ctx *node.ServiceContext) (node.Service, error) { 172 return les.New(ctx, ðConf) 173 }); err != nil { 174 return nil, fmt.Errorf("ethereum init: %v", err) 175 } 176 //如果请求Netstats报告,请执行此操作 177 if config.EthereumNetStats != "" { 178 if err := rawStack.Register(func(ctx *node.ServiceContext) (node.Service, error) { 179 var lesServ *les.LightEthereum 180 ctx.Service(&lesServ) 181 182 return ethstats.New(config.EthereumNetStats, nil, lesServ) 183 }); err != nil { 184 return nil, fmt.Errorf("netstats init: %v", err) 185 } 186 } 187 } 188 //如有要求,注册窃听协议 189 if config.WhisperEnabled { 190 if err := rawStack.Register(func(*node.ServiceContext) (node.Service, error) { 191 return whisper.New(&whisper.DefaultConfig), nil 192 }); err != nil { 193 return nil, fmt.Errorf("whisper init: %v", err) 194 } 195 } 196 return &Node{rawStack}, nil 197 } 198 199 //start创建一个活动的p2p节点并开始运行它。 200 func (n *Node) Start() error { 201 return n.node.Start() 202 } 203 204 //stop终止正在运行的节点及其所有服务。如果节点是 205 //未启动,返回错误。 206 func (n *Node) Stop() error { 207 return n.node.Stop() 208 } 209 210 //getethereumclient检索客户端以访问ethereum子系统。 211 func (n *Node) GetEthereumClient() (client *EthereumClient, _ error) { 212 rpc, err := n.node.Attach() 213 if err != nil { 214 return nil, err 215 } 216 return &EthereumClient{ethclient.NewClient(rpc)}, nil 217 } 218 219 //getnodeinfo收集并返回有关主机的已知元数据集合。 220 func (n *Node) GetNodeInfo() *NodeInfo { 221 return &NodeInfo{n.node.Server().NodeInfo()} 222 } 223 224 //GetPeerSinfo返回描述已连接对等端的元数据对象数组。 225 func (n *Node) GetPeersInfo() *PeerInfos { 226 return &PeerInfos{n.node.Server().PeersInfo()} 227 }