github.com/juliankolbe/go-ethereum@v1.9.992/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/juliankolbe/go-ethereum/common" 21 "github.com/juliankolbe/go-ethereum/log" 22 "github.com/juliankolbe/go-ethereum/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 return p2p.Send(p.rw, GetAccountRangeMsg, &GetAccountRangePacket{ 69 ID: id, 70 Root: root, 71 Origin: origin, 72 Limit: limit, 73 Bytes: bytes, 74 }) 75 } 76 77 // RequestStorageRange fetches a batch of storage slots belonging to one or more 78 // accounts. If slots from only one accout is requested, an origin marker may also 79 // be used to retrieve from there. 80 func (p *Peer) RequestStorageRanges(id uint64, root common.Hash, accounts []common.Hash, origin, limit []byte, bytes uint64) error { 81 if len(accounts) == 1 && origin != nil { 82 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)) 83 } else { 84 p.logger.Trace("Fetching ranges of small storage slots", "reqid", id, "root", root, "accounts", len(accounts), "first", accounts[0], "bytes", common.StorageSize(bytes)) 85 } 86 return p2p.Send(p.rw, GetStorageRangesMsg, &GetStorageRangesPacket{ 87 ID: id, 88 Root: root, 89 Accounts: accounts, 90 Origin: origin, 91 Limit: limit, 92 Bytes: bytes, 93 }) 94 } 95 96 // RequestByteCodes fetches a batch of bytecodes by hash. 97 func (p *Peer) RequestByteCodes(id uint64, hashes []common.Hash, bytes uint64) error { 98 p.logger.Trace("Fetching set of byte codes", "reqid", id, "hashes", len(hashes), "bytes", common.StorageSize(bytes)) 99 return p2p.Send(p.rw, GetByteCodesMsg, &GetByteCodesPacket{ 100 ID: id, 101 Hashes: hashes, 102 Bytes: bytes, 103 }) 104 } 105 106 // RequestTrieNodes fetches a batch of account or storage trie nodes rooted in 107 // a specificstate trie. 108 func (p *Peer) RequestTrieNodes(id uint64, root common.Hash, paths []TrieNodePathSet, bytes uint64) error { 109 p.logger.Trace("Fetching set of trie nodes", "reqid", id, "root", root, "pathsets", len(paths), "bytes", common.StorageSize(bytes)) 110 return p2p.Send(p.rw, GetTrieNodesMsg, &GetTrieNodesPacket{ 111 ID: id, 112 Root: root, 113 Paths: paths, 114 Bytes: bytes, 115 }) 116 }