github.com/phillinzzz/newBsc@v1.1.6/eth/protocols/snap/peer.go (about) 1 // Copyright 2020 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 snap 18 19 import ( 20 "github.com/phillinzzz/newBsc/common" 21 "github.com/phillinzzz/newBsc/log" 22 "github.com/phillinzzz/newBsc/p2p" 23 ) 24 25 // Peer is a collection of relevant information we have about a `snap` peer. 26 type Peer struct { 27 id string // Unique ID for the peer, cached 28 29 *p2p.Peer // The embedded P2P package peer 30 rw p2p.MsgReadWriter // Input/output streams for snap 31 version uint // Protocol version negotiated 32 33 logger log.Logger // Contextual logger with the peer id injected 34 } 35 36 // newPeer create a wrapper for a network connection and negotiated protocol 37 // version. 38 func newPeer(version uint, p *p2p.Peer, rw p2p.MsgReadWriter) *Peer { 39 id := p.ID().String() 40 return &Peer{ 41 id: id, 42 Peer: p, 43 rw: rw, 44 version: version, 45 logger: log.New("peer", id[:8]), 46 } 47 } 48 49 // ID retrieves the peer's unique identifier. 50 func (p *Peer) ID() string { 51 return p.id 52 } 53 54 // Version retrieves the peer's negoatiated `snap` protocol version. 55 func (p *Peer) Version() uint { 56 return p.version 57 } 58 59 // Log overrides the P2P logget with the higher level one containing only the id. 60 func (p *Peer) Log() log.Logger { 61 return p.logger 62 } 63 64 // RequestAccountRange fetches a batch of accounts rooted in a specific account 65 // trie, starting with the origin. 66 func (p *Peer) RequestAccountRange(id uint64, root common.Hash, origin, limit common.Hash, bytes uint64) error { 67 p.logger.Trace("Fetching range of accounts", "reqid", id, "root", root, "origin", origin, "limit", limit, "bytes", common.StorageSize(bytes)) 68 69 requestTracker.Track(p.id, p.version, GetAccountRangeMsg, AccountRangeMsg, id) 70 return p2p.Send(p.rw, GetAccountRangeMsg, &GetAccountRangePacket{ 71 ID: id, 72 Root: root, 73 Origin: origin, 74 Limit: limit, 75 Bytes: bytes, 76 }) 77 } 78 79 // RequestStorageRange fetches a batch of storage slots belonging to one or more 80 // accounts. If slots from only one accout is requested, an origin marker may also 81 // be used to retrieve from there. 82 func (p *Peer) RequestStorageRanges(id uint64, root common.Hash, accounts []common.Hash, origin, limit []byte, bytes uint64) error { 83 if len(accounts) == 1 && origin != nil { 84 p.logger.Trace("Fetching range of large storage slots", "reqid", id, "root", root, "account", accounts[0], "origin", common.BytesToHash(origin), "limit", common.BytesToHash(limit), "bytes", common.StorageSize(bytes)) 85 } else { 86 p.logger.Trace("Fetching ranges of small storage slots", "reqid", id, "root", root, "accounts", len(accounts), "first", accounts[0], "bytes", common.StorageSize(bytes)) 87 } 88 requestTracker.Track(p.id, p.version, GetStorageRangesMsg, StorageRangesMsg, id) 89 return p2p.Send(p.rw, GetStorageRangesMsg, &GetStorageRangesPacket{ 90 ID: id, 91 Root: root, 92 Accounts: accounts, 93 Origin: origin, 94 Limit: limit, 95 Bytes: bytes, 96 }) 97 } 98 99 // RequestByteCodes fetches a batch of bytecodes by hash. 100 func (p *Peer) RequestByteCodes(id uint64, hashes []common.Hash, bytes uint64) error { 101 p.logger.Trace("Fetching set of byte codes", "reqid", id, "hashes", len(hashes), "bytes", common.StorageSize(bytes)) 102 103 requestTracker.Track(p.id, p.version, GetByteCodesMsg, ByteCodesMsg, id) 104 return p2p.Send(p.rw, GetByteCodesMsg, &GetByteCodesPacket{ 105 ID: id, 106 Hashes: hashes, 107 Bytes: bytes, 108 }) 109 } 110 111 // RequestTrieNodes fetches a batch of account or storage trie nodes rooted in 112 // a specificstate trie. 113 func (p *Peer) RequestTrieNodes(id uint64, root common.Hash, paths []TrieNodePathSet, bytes uint64) error { 114 p.logger.Trace("Fetching set of trie nodes", "reqid", id, "root", root, "pathsets", len(paths), "bytes", common.StorageSize(bytes)) 115 116 requestTracker.Track(p.id, p.version, GetTrieNodesMsg, TrieNodesMsg, id) 117 return p2p.Send(p.rw, GetTrieNodesMsg, &GetTrieNodesPacket{ 118 ID: id, 119 Root: root, 120 Paths: paths, 121 Bytes: bytes, 122 }) 123 }