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