types.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Copyright 2015 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 downloader
  17. import (
  18. "fmt"
  19. "github.com/ethereum/go-ethereum/core/types"
  20. )
  21. // peerDropFn is a callback type for dropping a peer detected as malicious.
  22. type peerDropFn func(id string)
  23. // dataPack is a data message returned by a peer for some query.
  24. type dataPack interface {
  25. PeerId() string
  26. Items() int
  27. Stats() string
  28. }
  29. // headerPack is a batch of block headers returned by a peer.
  30. type headerPack struct {
  31. peerID string
  32. headers []*types.Header
  33. }
  34. func (p *headerPack) PeerId() string { return p.peerID }
  35. func (p *headerPack) Items() int { return len(p.headers) }
  36. func (p *headerPack) Stats() string { return fmt.Sprintf("%d", len(p.headers)) }
  37. // bodyPack is a batch of block bodies returned by a peer.
  38. type bodyPack struct {
  39. peerID string
  40. transactions [][]*types.Transaction
  41. uncles [][]*types.Header
  42. }
  43. func (p *bodyPack) PeerId() string { return p.peerID }
  44. func (p *bodyPack) Items() int {
  45. if len(p.transactions) <= len(p.uncles) {
  46. return len(p.transactions)
  47. }
  48. return len(p.uncles)
  49. }
  50. func (p *bodyPack) Stats() string { return fmt.Sprintf("%d:%d", len(p.transactions), len(p.uncles)) }
  51. // receiptPack is a batch of receipts returned by a peer.
  52. type receiptPack struct {
  53. peerID string
  54. receipts [][]*types.Receipt
  55. }
  56. func (p *receiptPack) PeerId() string { return p.peerID }
  57. func (p *receiptPack) Items() int { return len(p.receipts) }
  58. func (p *receiptPack) Stats() string { return fmt.Sprintf("%d", len(p.receipts)) }
  59. // statePack is a batch of states returned by a peer.
  60. type statePack struct {
  61. peerID string
  62. states [][]byte
  63. }
  64. func (p *statePack) PeerId() string { return p.peerID }
  65. func (p *statePack) Items() int { return len(p.states) }
  66. func (p *statePack) Stats() string { return fmt.Sprintf("%d", len(p.states)) }