github.com/aquanetwork/aquachain@v1.7.8/aqua/downloader/fakepeer.go (about) 1 // Copyright 2017 The aquachain Authors 2 // This file is part of the aquachain library. 3 // 4 // The aquachain 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 aquachain 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 aquachain library. If not, see <http://www.gnu.org/licenses/>. 16 17 package downloader 18 19 import ( 20 "math/big" 21 22 "gitlab.com/aquachain/aquachain/aquadb" 23 "gitlab.com/aquachain/aquachain/common" 24 "gitlab.com/aquachain/aquachain/core" 25 "gitlab.com/aquachain/aquachain/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 xisting local database. 31 type FakePeer struct { 32 id string 33 db aquadb.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 aquadb.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.GetBlockNoVersion(p.db, hash, p.hc.GetBlockNumber(hash)) 130 // dont need version for body with known hash 131 132 txs = append(txs, block.Transactions()) 133 uncles = append(uncles, block.Uncles()) 134 } 135 p.dl.DeliverBodies(p.id, txs, uncles) 136 return nil 137 } 138 139 // RequestReceipts implements downloader.Peer, returning a batch of transaction 140 // receipts corresponding to the specified block hashes. 141 func (p *FakePeer) RequestReceipts(hashes []common.Hash) error { 142 var receipts [][]*types.Receipt 143 for _, hash := range hashes { 144 receipts = append(receipts, core.GetBlockReceipts(p.db, hash, p.hc.GetBlockNumber(hash))) 145 } 146 p.dl.DeliverReceipts(p.id, receipts) 147 return nil 148 } 149 150 // RequestNodeData implements downloader.Peer, returning a batch of state trie 151 // nodes corresponding to the specified trie hashes. 152 func (p *FakePeer) RequestNodeData(hashes []common.Hash) error { 153 var data [][]byte 154 for _, hash := range hashes { 155 if entry, err := p.db.Get(hash.Bytes()); err == nil { 156 data = append(data, entry) 157 } 158 } 159 p.dl.DeliverNodeData(p.id, data) 160 return nil 161 }