peer.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. // ID retrieves the peer's unique identifier.
  43. func (p *Peer) ID() string {
  44. return p.id
  45. }
  46. // Version retrieves the peer's negoatiated `snap` protocol version.
  47. func (p *Peer) Version() uint {
  48. return p.version
  49. }
  50. // Log overrides the P2P logget with the higher level one containing only the id.
  51. func (p *Peer) Log() log.Logger {
  52. return p.logger
  53. }
  54. // RequestAccountRange fetches a batch of accounts rooted in a specific account
  55. // trie, starting with the origin.
  56. func (p *Peer) RequestAccountRange(id uint64, root common.Hash, origin, limit common.Hash, bytes uint64) error {
  57. p.logger.Trace("Fetching range of accounts", "reqid", id, "root", root, "origin", origin, "limit", limit, "bytes", common.StorageSize(bytes))
  58. requestTracker.Track(p.id, p.version, GetAccountRangeMsg, AccountRangeMsg, id)
  59. return p2p.Send(p.rw, GetAccountRangeMsg, &GetAccountRangePacket{
  60. ID: id,
  61. Root: root,
  62. Origin: origin,
  63. Limit: limit,
  64. Bytes: bytes,
  65. })
  66. }
  67. // RequestStorageRange fetches a batch of storage slots belonging to one or more
  68. // accounts. If slots from only one accout is requested, an origin marker may also
  69. // be used to retrieve from there.
  70. func (p *Peer) RequestStorageRanges(id uint64, root common.Hash, accounts []common.Hash, origin, limit []byte, bytes uint64) error {
  71. if len(accounts) == 1 && origin != nil {
  72. 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))
  73. } else {
  74. p.logger.Trace("Fetching ranges of small storage slots", "reqid", id, "root", root, "accounts", len(accounts), "first", accounts[0], "bytes", common.StorageSize(bytes))
  75. }
  76. requestTracker.Track(p.id, p.version, GetStorageRangesMsg, StorageRangesMsg, id)
  77. return p2p.Send(p.rw, GetStorageRangesMsg, &GetStorageRangesPacket{
  78. ID: id,
  79. Root: root,
  80. Accounts: accounts,
  81. Origin: origin,
  82. Limit: limit,
  83. Bytes: bytes,
  84. })
  85. }
  86. // RequestByteCodes fetches a batch of bytecodes by hash.
  87. func (p *Peer) RequestByteCodes(id uint64, hashes []common.Hash, bytes uint64) error {
  88. p.logger.Trace("Fetching set of byte codes", "reqid", id, "hashes", len(hashes), "bytes", common.StorageSize(bytes))
  89. requestTracker.Track(p.id, p.version, GetByteCodesMsg, ByteCodesMsg, id)
  90. return p2p.Send(p.rw, GetByteCodesMsg, &GetByteCodesPacket{
  91. ID: id,
  92. Hashes: hashes,
  93. Bytes: bytes,
  94. })
  95. }
  96. // RequestTrieNodes fetches a batch of account or storage trie nodes rooted in
  97. // a specificstate trie.
  98. func (p *Peer) RequestTrieNodes(id uint64, root common.Hash, paths []TrieNodePathSet, bytes uint64) error {
  99. p.logger.Trace("Fetching set of trie nodes", "reqid", id, "root", root, "pathsets", len(paths), "bytes", common.StorageSize(bytes))
  100. requestTracker.Track(p.id, p.version, GetTrieNodesMsg, TrieNodesMsg, id)
  101. return p2p.Send(p.rw, GetTrieNodesMsg, &GetTrieNodesPacket{
  102. ID: id,
  103. Root: root,
  104. Paths: paths,
  105. Bytes: bytes,
  106. })
  107. }