fakepeer.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // Copyright 2017 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. "math/big"
  19. "github.com/ethereum/go-ethereum/common"
  20. "github.com/ethereum/go-ethereum/core"
  21. "github.com/ethereum/go-ethereum/core/rawdb"
  22. "github.com/ethereum/go-ethereum/core/types"
  23. "github.com/ethereum/go-ethereum/ethdb"
  24. )
  25. // FakePeer is a mock downloader peer that operates on a local database instance
  26. // instead of being an actual live node. It's useful for testing and to implement
  27. // sync commands from an existing local database.
  28. type FakePeer struct {
  29. id string
  30. db ethdb.Database
  31. hc *core.HeaderChain
  32. dl *Downloader
  33. }
  34. // NewFakePeer creates a new mock downloader peer with the given data sources.
  35. func NewFakePeer(id string, db ethdb.Database, hc *core.HeaderChain, dl *Downloader) *FakePeer {
  36. return &FakePeer{id: id, db: db, hc: hc, dl: dl}
  37. }
  38. // Head implements downloader.Peer, returning the current head hash and number
  39. // of the best known header.
  40. func (p *FakePeer) Head() (common.Hash, *big.Int) {
  41. header := p.hc.CurrentHeader()
  42. return header.Hash(), header.Number
  43. }
  44. // RequestHeadersByHash implements downloader.Peer, returning a batch of headers
  45. // defined by the origin hash and the associated query parameters.
  46. func (p *FakePeer) RequestHeadersByHash(hash common.Hash, amount int, skip int, reverse bool) error {
  47. var (
  48. headers []*types.Header
  49. unknown bool
  50. )
  51. for !unknown && len(headers) < amount {
  52. origin := p.hc.GetHeaderByHash(hash)
  53. if origin == nil {
  54. break
  55. }
  56. number := origin.Number.Uint64()
  57. headers = append(headers, origin)
  58. if reverse {
  59. for i := 0; i <= skip; i++ {
  60. if header := p.hc.GetHeader(hash, number); header != nil {
  61. hash = header.ParentHash
  62. number--
  63. } else {
  64. unknown = true
  65. break
  66. }
  67. }
  68. } else {
  69. var (
  70. current = origin.Number.Uint64()
  71. next = current + uint64(skip) + 1
  72. )
  73. if header := p.hc.GetHeaderByNumber(next); header != nil {
  74. if p.hc.GetBlockHashesFromHash(header.Hash(), uint64(skip+1))[skip] == hash {
  75. hash = header.Hash()
  76. } else {
  77. unknown = true
  78. }
  79. } else {
  80. unknown = true
  81. }
  82. }
  83. }
  84. p.dl.DeliverHeaders(p.id, headers)
  85. return nil
  86. }
  87. // RequestHeadersByNumber implements downloader.Peer, returning a batch of headers
  88. // defined by the origin number and the associated query parameters.
  89. func (p *FakePeer) RequestHeadersByNumber(number uint64, amount int, skip int, reverse bool) error {
  90. var (
  91. headers []*types.Header
  92. unknown bool
  93. )
  94. for !unknown && len(headers) < amount {
  95. origin := p.hc.GetHeaderByNumber(number)
  96. if origin == nil {
  97. break
  98. }
  99. if reverse {
  100. if number >= uint64(skip+1) {
  101. number -= uint64(skip + 1)
  102. } else {
  103. unknown = true
  104. }
  105. } else {
  106. number += uint64(skip + 1)
  107. }
  108. headers = append(headers, origin)
  109. }
  110. p.dl.DeliverHeaders(p.id, headers)
  111. return nil
  112. }
  113. // RequestBodies implements downloader.Peer, returning a batch of block bodies
  114. // corresponding to the specified block hashes.
  115. func (p *FakePeer) RequestBodies(hashes []common.Hash) error {
  116. var (
  117. txs [][]*types.Transaction
  118. uncles [][]*types.Header
  119. )
  120. for _, hash := range hashes {
  121. block := rawdb.ReadBlock(p.db, hash, *p.hc.GetBlockNumber(hash))
  122. txs = append(txs, block.Transactions())
  123. uncles = append(uncles, block.Uncles())
  124. }
  125. p.dl.DeliverBodies(p.id, txs, uncles)
  126. return nil
  127. }
  128. // RequestReceipts implements downloader.Peer, returning a batch of transaction
  129. // receipts corresponding to the specified block hashes.
  130. func (p *FakePeer) RequestReceipts(hashes []common.Hash) error {
  131. var receipts [][]*types.Receipt
  132. for _, hash := range hashes {
  133. receipts = append(receipts, rawdb.ReadReceipts(p.db, hash, *p.hc.GetBlockNumber(hash)))
  134. }
  135. p.dl.DeliverReceipts(p.id, receipts)
  136. return nil
  137. }
  138. // RequestNodeData implements downloader.Peer, returning a batch of state trie
  139. // nodes corresponding to the specified trie hashes.
  140. func (p *FakePeer) RequestNodeData(hashes []common.Hash) error {
  141. var data [][]byte
  142. for _, hash := range hashes {
  143. if entry, err := p.db.Get(hash.Bytes()); err == nil {
  144. data = append(data, entry)
  145. }
  146. }
  147. p.dl.DeliverNodeData(p.id, data)
  148. return nil
  149. }