github.com/SmartMeshFoundation/Spectrum@v0.0.0-20220621030607-452a266fee1e/eth/downloader/fakepeer.go (about)

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