peer.go 4.1 KB

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