peer.go 4.8 KB

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