github.com/neatio-net/neatio@v1.7.3-0.20231114194659-f4d7a2226baa/neatptc/downloader/fakepeer.go (about)

     1  package downloader
     2  
     3  import (
     4  	"math/big"
     5  
     6  	"github.com/neatio-net/neatio/chain/core"
     7  	"github.com/neatio-net/neatio/chain/core/rawdb"
     8  	"github.com/neatio-net/neatio/chain/core/types"
     9  	"github.com/neatio-net/neatio/neatdb"
    10  	"github.com/neatio-net/neatio/utilities/common"
    11  )
    12  
    13  type FakePeer struct {
    14  	id string
    15  	db neatdb.Database
    16  	hc *core.HeaderChain
    17  	dl *Downloader
    18  }
    19  
    20  func NewFakePeer(id string, db neatdb.Database, hc *core.HeaderChain, dl *Downloader) *FakePeer {
    21  	return &FakePeer{id: id, db: db, hc: hc, dl: dl}
    22  }
    23  
    24  func (p *FakePeer) Head() (common.Hash, *big.Int) {
    25  	header := p.hc.CurrentHeader()
    26  	return header.Hash(), header.Number
    27  }
    28  
    29  func (p *FakePeer) RequestHeadersByHash(hash common.Hash, amount int, skip int, reverse bool) error {
    30  	var (
    31  		headers []*types.Header
    32  		unknown bool
    33  	)
    34  	for !unknown && len(headers) < amount {
    35  		origin := p.hc.GetHeaderByHash(hash)
    36  		if origin == nil {
    37  			break
    38  		}
    39  		number := origin.Number.Uint64()
    40  		headers = append(headers, origin)
    41  		if reverse {
    42  			for i := 0; i <= skip; i++ {
    43  				if header := p.hc.GetHeader(hash, number); header != nil {
    44  					hash = header.ParentHash
    45  					number--
    46  				} else {
    47  					unknown = true
    48  					break
    49  				}
    50  			}
    51  		} else {
    52  			var (
    53  				current = origin.Number.Uint64()
    54  				next    = current + uint64(skip) + 1
    55  			)
    56  			if header := p.hc.GetHeaderByNumber(next); header != nil {
    57  				if p.hc.GetBlockHashesFromHash(header.Hash(), uint64(skip+1))[skip] == hash {
    58  					hash = header.Hash()
    59  				} else {
    60  					unknown = true
    61  				}
    62  			} else {
    63  				unknown = true
    64  			}
    65  		}
    66  	}
    67  	p.dl.DeliverHeaders(p.id, headers)
    68  	return nil
    69  }
    70  
    71  func (p *FakePeer) RequestHeadersByNumber(number uint64, amount int, skip int, reverse bool) error {
    72  	var (
    73  		headers []*types.Header
    74  		unknown bool
    75  	)
    76  	for !unknown && len(headers) < amount {
    77  		origin := p.hc.GetHeaderByNumber(number)
    78  		if origin == nil {
    79  			break
    80  		}
    81  		if reverse {
    82  			if number >= uint64(skip+1) {
    83  				number -= uint64(skip + 1)
    84  			} else {
    85  				unknown = true
    86  			}
    87  		} else {
    88  			number += uint64(skip + 1)
    89  		}
    90  		headers = append(headers, origin)
    91  	}
    92  	p.dl.DeliverHeaders(p.id, headers)
    93  	return nil
    94  }
    95  
    96  func (p *FakePeer) RequestBodies(hashes []common.Hash) error {
    97  	var (
    98  		txs    [][]*types.Transaction
    99  		uncles [][]*types.Header
   100  	)
   101  	for _, hash := range hashes {
   102  		block := rawdb.ReadBlock(p.db, hash, *p.hc.GetBlockNumber(hash))
   103  
   104  		txs = append(txs, block.Transactions())
   105  		uncles = append(uncles, block.Uncles())
   106  	}
   107  	p.dl.DeliverBodies(p.id, txs, uncles)
   108  	return nil
   109  }
   110  
   111  func (p *FakePeer) RequestReceipts(hashes []common.Hash) error {
   112  	var receipts [][]*types.Receipt
   113  	for _, hash := range hashes {
   114  		receipts = append(receipts, rawdb.ReadReceipts(p.db, hash, *p.hc.GetBlockNumber(hash)))
   115  	}
   116  	p.dl.DeliverReceipts(p.id, receipts)
   117  	return nil
   118  }
   119  
   120  func (p *FakePeer) RequestNodeData(hashes []common.Hash) error {
   121  	var data [][]byte
   122  	for _, hash := range hashes {
   123  		if entry, err := p.db.Get(hash.Bytes()); err == nil {
   124  			data = append(data, entry)
   125  		}
   126  	}
   127  	p.dl.DeliverNodeData(p.id, data)
   128  	return nil
   129  }