github.com/bcskill/bcschain/v3@v3.4.9-beta2/eth/downloader/fakepeer.go (about)

     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  
    17  package downloader
    18  
    19  import (
    20  	"math/big"
    21  
    22  	"github.com/bcskill/bcschain/v3/common"
    23  	"github.com/bcskill/bcschain/v3/core"
    24  	"github.com/bcskill/bcschain/v3/core/rawdb"
    25  	"github.com/bcskill/bcschain/v3/core/types"
    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 existing local database.
    31  type FakePeer struct {
    32  	id string
    33  	db common.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 common.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 associated 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 associated 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 txs [][]*types.Transaction
   125  	for _, hash := range hashes {
   126  		block := rawdb.ReadBlock(p.db, hash, *p.hc.GetBlockNumber(hash))
   127  
   128  		txs = append(txs, block.Transactions())
   129  	}
   130  	p.dl.DeliverBodies(p.id, txs)
   131  	return nil
   132  }
   133  
   134  // RequestReceipts implements downloader.Peer, returning a batch of transaction
   135  // receipts corresponding to the specified block hashes.
   136  func (p *FakePeer) RequestReceipts(hashes []common.Hash) error {
   137  	var receipts [][]*types.Receipt
   138  	for _, hash := range hashes {
   139  		receipts = append(receipts, rawdb.ReadRawReceipts(p.db.ReceiptTable(), hash, *p.hc.GetBlockNumber(hash)))
   140  	}
   141  	p.dl.DeliverReceipts(p.id, receipts)
   142  	return nil
   143  }
   144  
   145  // RequestNodeData implements downloader.Peer, returning a batch of state trie
   146  // nodes corresponding to the specified trie hashes.
   147  func (p *FakePeer) RequestNodeData(hashes []common.Hash) error {
   148  	var data [][]byte
   149  	for _, hash := range hashes {
   150  		if entry, err := p.db.GlobalTable().Get(hash.Bytes()); err == nil {
   151  			data = append(data, entry)
   152  		}
   153  	}
   154  	p.dl.DeliverNodeData(p.id, data)
   155  	return nil
   156  }