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