github.com/ylsGit/go-ethereum@v1.6.5/les/peer.go (about) 1 // Copyright 2016 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 implements the Light Ethereum Subprotocol. 18 package les 19 20 import ( 21 "errors" 22 "fmt" 23 "math/big" 24 "sync" 25 "time" 26 27 "github.com/ethereum/go-ethereum/common" 28 "github.com/ethereum/go-ethereum/core/types" 29 "github.com/ethereum/go-ethereum/eth" 30 "github.com/ethereum/go-ethereum/les/flowcontrol" 31 "github.com/ethereum/go-ethereum/p2p" 32 "github.com/ethereum/go-ethereum/rlp" 33 ) 34 35 var ( 36 errClosed = errors.New("peer set is closed") 37 errAlreadyRegistered = errors.New("peer is already registered") 38 errNotRegistered = errors.New("peer is not registered") 39 ) 40 41 const ( 42 maxHeadInfoLen = 20 43 maxResponseErrors = 50 // number of invalid responses tolerated (makes the protocol less brittle but still avoids spam) 44 ) 45 46 type peer struct { 47 *p2p.Peer 48 49 rw p2p.MsgReadWriter 50 51 version int // Protocol version negotiated 52 network uint64 // Network ID being on 53 54 id string 55 56 headInfo *announceData 57 lock sync.RWMutex 58 59 announceChn chan announceData 60 sendQueue *execQueue 61 62 poolEntry *poolEntry 63 hasBlock func(common.Hash, uint64) bool 64 responseErrors int 65 66 fcClient *flowcontrol.ClientNode // nil if the peer is server only 67 fcServer *flowcontrol.ServerNode // nil if the peer is client only 68 fcServerParams *flowcontrol.ServerParams 69 fcCosts requestCostTable 70 } 71 72 func newPeer(version int, network uint64, p *p2p.Peer, rw p2p.MsgReadWriter) *peer { 73 id := p.ID() 74 75 return &peer{ 76 Peer: p, 77 rw: rw, 78 version: version, 79 network: network, 80 id: fmt.Sprintf("%x", id[:8]), 81 announceChn: make(chan announceData, 20), 82 } 83 } 84 85 func (p *peer) canQueue() bool { 86 return p.sendQueue.canQueue() 87 } 88 89 func (p *peer) queueSend(f func()) { 90 p.sendQueue.queue(f) 91 } 92 93 // Info gathers and returns a collection of metadata known about a peer. 94 func (p *peer) Info() *eth.PeerInfo { 95 return ð.PeerInfo{ 96 Version: p.version, 97 Difficulty: p.Td(), 98 Head: fmt.Sprintf("%x", p.Head()), 99 } 100 } 101 102 // Head retrieves a copy of the current head (most recent) hash of the peer. 103 func (p *peer) Head() (hash common.Hash) { 104 p.lock.RLock() 105 defer p.lock.RUnlock() 106 107 copy(hash[:], p.headInfo.Hash[:]) 108 return hash 109 } 110 111 func (p *peer) HeadAndTd() (hash common.Hash, td *big.Int) { 112 p.lock.RLock() 113 defer p.lock.RUnlock() 114 115 copy(hash[:], p.headInfo.Hash[:]) 116 return hash, p.headInfo.Td 117 } 118 119 func (p *peer) headBlockInfo() blockInfo { 120 p.lock.RLock() 121 defer p.lock.RUnlock() 122 123 return blockInfo{Hash: p.headInfo.Hash, Number: p.headInfo.Number, Td: p.headInfo.Td} 124 } 125 126 // Td retrieves the current total difficulty of a peer. 127 func (p *peer) Td() *big.Int { 128 p.lock.RLock() 129 defer p.lock.RUnlock() 130 131 return new(big.Int).Set(p.headInfo.Td) 132 } 133 134 // waitBefore implements distPeer interface 135 func (p *peer) waitBefore(maxCost uint64) (time.Duration, float64) { 136 return p.fcServer.CanSend(maxCost) 137 } 138 139 func sendRequest(w p2p.MsgWriter, msgcode, reqID, cost uint64, data interface{}) error { 140 type req struct { 141 ReqID uint64 142 Data interface{} 143 } 144 return p2p.Send(w, msgcode, req{reqID, data}) 145 } 146 147 func sendResponse(w p2p.MsgWriter, msgcode, reqID, bv uint64, data interface{}) error { 148 type resp struct { 149 ReqID, BV uint64 150 Data interface{} 151 } 152 return p2p.Send(w, msgcode, resp{reqID, bv, data}) 153 } 154 155 func (p *peer) GetRequestCost(msgcode uint64, amount int) uint64 { 156 p.lock.RLock() 157 defer p.lock.RUnlock() 158 159 cost := p.fcCosts[msgcode].baseCost + p.fcCosts[msgcode].reqCost*uint64(amount) 160 if cost > p.fcServerParams.BufLimit { 161 cost = p.fcServerParams.BufLimit 162 } 163 return cost 164 } 165 166 // HasBlock checks if the peer has a given block 167 func (p *peer) HasBlock(hash common.Hash, number uint64) bool { 168 p.lock.RLock() 169 hashBlock := p.hasBlock 170 p.lock.RUnlock() 171 return hashBlock != nil && hashBlock(hash, number) 172 } 173 174 // SendAnnounce announces the availability of a number of blocks through 175 // a hash notification. 176 func (p *peer) SendAnnounce(request announceData) error { 177 return p2p.Send(p.rw, AnnounceMsg, request) 178 } 179 180 // SendBlockHeaders sends a batch of block headers to the remote peer. 181 func (p *peer) SendBlockHeaders(reqID, bv uint64, headers []*types.Header) error { 182 return sendResponse(p.rw, BlockHeadersMsg, reqID, bv, headers) 183 } 184 185 // SendBlockBodiesRLP sends a batch of block contents to the remote peer from 186 // an already RLP encoded format. 187 func (p *peer) SendBlockBodiesRLP(reqID, bv uint64, bodies []rlp.RawValue) error { 188 return sendResponse(p.rw, BlockBodiesMsg, reqID, bv, bodies) 189 } 190 191 // SendCodeRLP sends a batch of arbitrary internal data, corresponding to the 192 // hashes requested. 193 func (p *peer) SendCode(reqID, bv uint64, data [][]byte) error { 194 return sendResponse(p.rw, CodeMsg, reqID, bv, data) 195 } 196 197 // SendReceiptsRLP sends a batch of transaction receipts, corresponding to the 198 // ones requested from an already RLP encoded format. 199 func (p *peer) SendReceiptsRLP(reqID, bv uint64, receipts []rlp.RawValue) error { 200 return sendResponse(p.rw, ReceiptsMsg, reqID, bv, receipts) 201 } 202 203 // SendProofs sends a batch of merkle proofs, corresponding to the ones requested. 204 func (p *peer) SendProofs(reqID, bv uint64, proofs proofsData) error { 205 return sendResponse(p.rw, ProofsMsg, reqID, bv, proofs) 206 } 207 208 // SendHeaderProofs sends a batch of header proofs, corresponding to the ones requested. 209 func (p *peer) SendHeaderProofs(reqID, bv uint64, proofs []ChtResp) error { 210 return sendResponse(p.rw, HeaderProofsMsg, reqID, bv, proofs) 211 } 212 213 // RequestHeadersByHash fetches a batch of blocks' headers corresponding to the 214 // specified header query, based on the hash of an origin block. 215 func (p *peer) RequestHeadersByHash(reqID, cost uint64, origin common.Hash, amount int, skip int, reverse bool) error { 216 p.Log().Debug("Fetching batch of headers", "count", amount, "fromhash", origin, "skip", skip, "reverse", reverse) 217 return sendRequest(p.rw, GetBlockHeadersMsg, reqID, cost, &getBlockHeadersData{Origin: hashOrNumber{Hash: origin}, Amount: uint64(amount), Skip: uint64(skip), Reverse: reverse}) 218 } 219 220 // RequestHeadersByNumber fetches a batch of blocks' headers corresponding to the 221 // specified header query, based on the number of an origin block. 222 func (p *peer) RequestHeadersByNumber(reqID, cost, origin uint64, amount int, skip int, reverse bool) error { 223 p.Log().Debug("Fetching batch of headers", "count", amount, "fromnum", origin, "skip", skip, "reverse", reverse) 224 return sendRequest(p.rw, GetBlockHeadersMsg, reqID, cost, &getBlockHeadersData{Origin: hashOrNumber{Number: origin}, Amount: uint64(amount), Skip: uint64(skip), Reverse: reverse}) 225 } 226 227 // RequestBodies fetches a batch of blocks' bodies corresponding to the hashes 228 // specified. 229 func (p *peer) RequestBodies(reqID, cost uint64, hashes []common.Hash) error { 230 p.Log().Debug("Fetching batch of block bodies", "count", len(hashes)) 231 return sendRequest(p.rw, GetBlockBodiesMsg, reqID, cost, hashes) 232 } 233 234 // RequestCode fetches a batch of arbitrary data from a node's known state 235 // data, corresponding to the specified hashes. 236 func (p *peer) RequestCode(reqID, cost uint64, reqs []*CodeReq) error { 237 p.Log().Debug("Fetching batch of codes", "count", len(reqs)) 238 return sendRequest(p.rw, GetCodeMsg, reqID, cost, reqs) 239 } 240 241 // RequestReceipts fetches a batch of transaction receipts from a remote node. 242 func (p *peer) RequestReceipts(reqID, cost uint64, hashes []common.Hash) error { 243 p.Log().Debug("Fetching batch of receipts", "count", len(hashes)) 244 return sendRequest(p.rw, GetReceiptsMsg, reqID, cost, hashes) 245 } 246 247 // RequestProofs fetches a batch of merkle proofs from a remote node. 248 func (p *peer) RequestProofs(reqID, cost uint64, reqs []*ProofReq) error { 249 p.Log().Debug("Fetching batch of proofs", "count", len(reqs)) 250 return sendRequest(p.rw, GetProofsMsg, reqID, cost, reqs) 251 } 252 253 // RequestHeaderProofs fetches a batch of header merkle proofs from a remote node. 254 func (p *peer) RequestHeaderProofs(reqID, cost uint64, reqs []*ChtReq) error { 255 p.Log().Debug("Fetching batch of header proofs", "count", len(reqs)) 256 return sendRequest(p.rw, GetHeaderProofsMsg, reqID, cost, reqs) 257 } 258 259 func (p *peer) SendTxs(reqID, cost uint64, txs types.Transactions) error { 260 p.Log().Debug("Fetching batch of transactions", "count", len(txs)) 261 return p2p.Send(p.rw, SendTxMsg, txs) 262 } 263 264 type keyValueEntry struct { 265 Key string 266 Value rlp.RawValue 267 } 268 type keyValueList []keyValueEntry 269 type keyValueMap map[string]rlp.RawValue 270 271 func (l keyValueList) add(key string, val interface{}) keyValueList { 272 var entry keyValueEntry 273 entry.Key = key 274 if val == nil { 275 val = uint64(0) 276 } 277 enc, err := rlp.EncodeToBytes(val) 278 if err == nil { 279 entry.Value = enc 280 } 281 return append(l, entry) 282 } 283 284 func (l keyValueList) decode() keyValueMap { 285 m := make(keyValueMap) 286 for _, entry := range l { 287 m[entry.Key] = entry.Value 288 } 289 return m 290 } 291 292 func (m keyValueMap) get(key string, val interface{}) error { 293 enc, ok := m[key] 294 if !ok { 295 return errResp(ErrHandshakeMissingKey, "%s", key) 296 } 297 if val == nil { 298 return nil 299 } 300 return rlp.DecodeBytes(enc, val) 301 } 302 303 func (p *peer) sendReceiveHandshake(sendList keyValueList) (keyValueList, error) { 304 // Send out own handshake in a new thread 305 errc := make(chan error, 1) 306 go func() { 307 errc <- p2p.Send(p.rw, StatusMsg, sendList) 308 }() 309 // In the mean time retrieve the remote status message 310 msg, err := p.rw.ReadMsg() 311 if err != nil { 312 return nil, err 313 } 314 if msg.Code != StatusMsg { 315 return nil, errResp(ErrNoStatusMsg, "first msg has code %x (!= %x)", msg.Code, StatusMsg) 316 } 317 if msg.Size > ProtocolMaxMsgSize { 318 return nil, errResp(ErrMsgTooLarge, "%v > %v", msg.Size, ProtocolMaxMsgSize) 319 } 320 // Decode the handshake 321 var recvList keyValueList 322 if err := msg.Decode(&recvList); err != nil { 323 return nil, errResp(ErrDecode, "msg %v: %v", msg, err) 324 } 325 if err := <-errc; err != nil { 326 return nil, err 327 } 328 return recvList, nil 329 } 330 331 // Handshake executes the les protocol handshake, negotiating version number, 332 // network IDs, difficulties, head and genesis blocks. 333 func (p *peer) Handshake(td *big.Int, head common.Hash, headNum uint64, genesis common.Hash, server *LesServer) error { 334 p.lock.Lock() 335 defer p.lock.Unlock() 336 337 var send keyValueList 338 send = send.add("protocolVersion", uint64(p.version)) 339 send = send.add("networkId", uint64(p.network)) 340 send = send.add("headTd", td) 341 send = send.add("headHash", head) 342 send = send.add("headNum", headNum) 343 send = send.add("genesisHash", genesis) 344 if server != nil { 345 send = send.add("serveHeaders", nil) 346 send = send.add("serveChainSince", uint64(0)) 347 send = send.add("serveStateSince", uint64(0)) 348 send = send.add("txRelay", nil) 349 send = send.add("flowControl/BL", server.defParams.BufLimit) 350 send = send.add("flowControl/MRR", server.defParams.MinRecharge) 351 list := server.fcCostStats.getCurrentList() 352 send = send.add("flowControl/MRC", list) 353 p.fcCosts = list.decode() 354 } 355 recvList, err := p.sendReceiveHandshake(send) 356 if err != nil { 357 return err 358 } 359 recv := recvList.decode() 360 361 var rGenesis, rHash common.Hash 362 var rVersion, rNetwork, rNum uint64 363 var rTd *big.Int 364 365 if err := recv.get("protocolVersion", &rVersion); err != nil { 366 return err 367 } 368 if err := recv.get("networkId", &rNetwork); err != nil { 369 return err 370 } 371 if err := recv.get("headTd", &rTd); err != nil { 372 return err 373 } 374 if err := recv.get("headHash", &rHash); err != nil { 375 return err 376 } 377 if err := recv.get("headNum", &rNum); err != nil { 378 return err 379 } 380 if err := recv.get("genesisHash", &rGenesis); err != nil { 381 return err 382 } 383 384 if rGenesis != genesis { 385 return errResp(ErrGenesisBlockMismatch, "%x (!= %x)", rGenesis[:8], genesis[:8]) 386 } 387 if rNetwork != p.network { 388 return errResp(ErrNetworkIdMismatch, "%d (!= %d)", rNetwork, p.network) 389 } 390 if int(rVersion) != p.version { 391 return errResp(ErrProtocolVersionMismatch, "%d (!= %d)", rVersion, p.version) 392 } 393 if server != nil { 394 // until we have a proper peer connectivity API, allow LES connection to other servers 395 /*if recv.get("serveStateSince", nil) == nil { 396 return errResp(ErrUselessPeer, "wanted client, got server") 397 }*/ 398 p.fcClient = flowcontrol.NewClientNode(server.fcManager, server.defParams) 399 } else { 400 if recv.get("serveChainSince", nil) != nil { 401 return errResp(ErrUselessPeer, "peer cannot serve chain") 402 } 403 if recv.get("serveStateSince", nil) != nil { 404 return errResp(ErrUselessPeer, "peer cannot serve state") 405 } 406 if recv.get("txRelay", nil) != nil { 407 return errResp(ErrUselessPeer, "peer cannot relay transactions") 408 } 409 params := &flowcontrol.ServerParams{} 410 if err := recv.get("flowControl/BL", ¶ms.BufLimit); err != nil { 411 return err 412 } 413 if err := recv.get("flowControl/MRR", ¶ms.MinRecharge); err != nil { 414 return err 415 } 416 var MRC RequestCostList 417 if err := recv.get("flowControl/MRC", &MRC); err != nil { 418 return err 419 } 420 p.fcServerParams = params 421 p.fcServer = flowcontrol.NewServerNode(params) 422 p.fcCosts = MRC.decode() 423 } 424 425 p.headInfo = &announceData{Td: rTd, Hash: rHash, Number: rNum} 426 return nil 427 } 428 429 // String implements fmt.Stringer. 430 func (p *peer) String() string { 431 return fmt.Sprintf("Peer %s [%s]", p.id, 432 fmt.Sprintf("les/%d", p.version), 433 ) 434 } 435 436 // peerSet represents the collection of active peers currently participating in 437 // the Light Ethereum sub-protocol. 438 type peerSet struct { 439 peers map[string]*peer 440 lock sync.RWMutex 441 closed bool 442 } 443 444 // newPeerSet creates a new peer set to track the active participants. 445 func newPeerSet() *peerSet { 446 return &peerSet{ 447 peers: make(map[string]*peer), 448 } 449 } 450 451 // Register injects a new peer into the working set, or returns an error if the 452 // peer is already known. 453 func (ps *peerSet) Register(p *peer) error { 454 ps.lock.Lock() 455 defer ps.lock.Unlock() 456 457 if ps.closed { 458 return errClosed 459 } 460 if _, ok := ps.peers[p.id]; ok { 461 return errAlreadyRegistered 462 } 463 ps.peers[p.id] = p 464 p.sendQueue = newExecQueue(100) 465 return nil 466 } 467 468 // Unregister removes a remote peer from the active set, disabling any further 469 // actions to/from that particular entity. 470 func (ps *peerSet) Unregister(id string) error { 471 ps.lock.Lock() 472 defer ps.lock.Unlock() 473 474 if p, ok := ps.peers[id]; !ok { 475 return errNotRegistered 476 } else { 477 p.sendQueue.quit() 478 } 479 delete(ps.peers, id) 480 return nil 481 } 482 483 // AllPeerIDs returns a list of all registered peer IDs 484 func (ps *peerSet) AllPeerIDs() []string { 485 ps.lock.RLock() 486 defer ps.lock.RUnlock() 487 488 res := make([]string, len(ps.peers)) 489 idx := 0 490 for id := range ps.peers { 491 res[idx] = id 492 idx++ 493 } 494 return res 495 } 496 497 // Peer retrieves the registered peer with the given id. 498 func (ps *peerSet) Peer(id string) *peer { 499 ps.lock.RLock() 500 defer ps.lock.RUnlock() 501 502 return ps.peers[id] 503 } 504 505 // Len returns if the current number of peers in the set. 506 func (ps *peerSet) Len() int { 507 ps.lock.RLock() 508 defer ps.lock.RUnlock() 509 510 return len(ps.peers) 511 } 512 513 // BestPeer retrieves the known peer with the currently highest total difficulty. 514 func (ps *peerSet) BestPeer() *peer { 515 ps.lock.RLock() 516 defer ps.lock.RUnlock() 517 518 var ( 519 bestPeer *peer 520 bestTd *big.Int 521 ) 522 for _, p := range ps.peers { 523 if td := p.Td(); bestPeer == nil || td.Cmp(bestTd) > 0 { 524 bestPeer, bestTd = p, td 525 } 526 } 527 return bestPeer 528 } 529 530 // AllPeers returns all peers in a list 531 func (ps *peerSet) AllPeers() []*peer { 532 ps.lock.RLock() 533 defer ps.lock.RUnlock() 534 535 list := make([]*peer, len(ps.peers)) 536 i := 0 537 for _, peer := range ps.peers { 538 list[i] = peer 539 i++ 540 } 541 return list 542 } 543 544 // Close disconnects all peers. 545 // No new peers can be registered after Close has returned. 546 func (ps *peerSet) Close() { 547 ps.lock.Lock() 548 defer ps.lock.Unlock() 549 550 for _, p := range ps.peers { 551 p.Disconnect(p2p.DiscQuitting) 552 } 553 ps.closed = true 554 }