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