github.com/theQRL/go-zond@v0.1.1/zond/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/theQRL/go-zond/common" 21 "github.com/theQRL/go-zond/log" 22 "github.com/theQRL/go-zond/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 // NewFakePeer create a fake snap peer without a backing p2p peer, for testing purposes. 50 func NewFakePeer(version uint, id string, rw p2p.MsgReadWriter) *Peer { 51 return &Peer{ 52 id: id, 53 rw: rw, 54 version: version, 55 logger: log.New("peer", id[:8]), 56 } 57 } 58 59 // ID retrieves the peer's unique identifier. 60 func (p *Peer) ID() string { 61 return p.id 62 } 63 64 // Version retrieves the peer's negotiated `snap` protocol version. 65 func (p *Peer) Version() uint { 66 return p.version 67 } 68 69 // Log overrides the P2P logger with the higher level one containing only the id. 70 func (p *Peer) Log() log.Logger { 71 return p.logger 72 } 73 74 // RequestAccountRange fetches a batch of accounts rooted in a specific account 75 // trie, starting with the origin. 76 func (p *Peer) RequestAccountRange(id uint64, root common.Hash, origin, limit common.Hash, bytes uint64) error { 77 p.logger.Trace("Fetching range of accounts", "reqid", id, "root", root, "origin", origin, "limit", limit, "bytes", common.StorageSize(bytes)) 78 79 requestTracker.Track(p.id, p.version, GetAccountRangeMsg, AccountRangeMsg, id) 80 return p2p.Send(p.rw, GetAccountRangeMsg, &GetAccountRangePacket{ 81 ID: id, 82 Root: root, 83 Origin: origin, 84 Limit: limit, 85 Bytes: bytes, 86 }) 87 } 88 89 // RequestStorageRanges fetches a batch of storage slots belonging to one or more 90 // accounts. If slots from only one account is requested, an origin marker may also 91 // be used to retrieve from there. 92 func (p *Peer) RequestStorageRanges(id uint64, root common.Hash, accounts []common.Hash, origin, limit []byte, bytes uint64) error { 93 if len(accounts) == 1 && origin != nil { 94 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)) 95 } else { 96 p.logger.Trace("Fetching ranges of small storage slots", "reqid", id, "root", root, "accounts", len(accounts), "first", accounts[0], "bytes", common.StorageSize(bytes)) 97 } 98 requestTracker.Track(p.id, p.version, GetStorageRangesMsg, StorageRangesMsg, id) 99 return p2p.Send(p.rw, GetStorageRangesMsg, &GetStorageRangesPacket{ 100 ID: id, 101 Root: root, 102 Accounts: accounts, 103 Origin: origin, 104 Limit: limit, 105 Bytes: bytes, 106 }) 107 } 108 109 // RequestByteCodes fetches a batch of bytecodes by hash. 110 func (p *Peer) RequestByteCodes(id uint64, hashes []common.Hash, bytes uint64) error { 111 p.logger.Trace("Fetching set of byte codes", "reqid", id, "hashes", len(hashes), "bytes", common.StorageSize(bytes)) 112 113 requestTracker.Track(p.id, p.version, GetByteCodesMsg, ByteCodesMsg, id) 114 return p2p.Send(p.rw, GetByteCodesMsg, &GetByteCodesPacket{ 115 ID: id, 116 Hashes: hashes, 117 Bytes: bytes, 118 }) 119 } 120 121 // RequestTrieNodes fetches a batch of account or storage trie nodes rooted in 122 // a specific state trie. 123 func (p *Peer) RequestTrieNodes(id uint64, root common.Hash, paths []TrieNodePathSet, bytes uint64) error { 124 p.logger.Trace("Fetching set of trie nodes", "reqid", id, "root", root, "pathsets", len(paths), "bytes", common.StorageSize(bytes)) 125 126 requestTracker.Track(p.id, p.version, GetTrieNodesMsg, TrieNodesMsg, id) 127 return p2p.Send(p.rw, GetTrieNodesMsg, &GetTrieNodesPacket{ 128 ID: id, 129 Root: root, 130 Paths: paths, 131 Bytes: bytes, 132 }) 133 }