github.com/nitinawathare/ethereumassignment3@v0.0.0-20211021213010-f07344c2b868/go-ethereum/les/odr.go (about) 1 // Copyright 2016 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 les 18 19 import ( 20 "context" 21 22 "github.com/ethereum/go-ethereum/core" 23 "github.com/ethereum/go-ethereum/ethdb" 24 "github.com/ethereum/go-ethereum/light" 25 "github.com/ethereum/go-ethereum/log" 26 ) 27 28 // LesOdr implements light.OdrBackend 29 type LesOdr struct { 30 db ethdb.Database 31 indexerConfig *light.IndexerConfig 32 chtIndexer, bloomTrieIndexer, bloomIndexer *core.ChainIndexer 33 retriever *retrieveManager 34 stop chan struct{} 35 } 36 37 func NewLesOdr(db ethdb.Database, config *light.IndexerConfig, retriever *retrieveManager) *LesOdr { 38 return &LesOdr{ 39 db: db, 40 indexerConfig: config, 41 retriever: retriever, 42 stop: make(chan struct{}), 43 } 44 } 45 46 // Stop cancels all pending retrievals 47 func (odr *LesOdr) Stop() { 48 close(odr.stop) 49 } 50 51 // Database returns the backing database 52 func (odr *LesOdr) Database() ethdb.Database { 53 return odr.db 54 } 55 56 // SetIndexers adds the necessary chain indexers to the ODR backend 57 func (odr *LesOdr) SetIndexers(chtIndexer, bloomTrieIndexer, bloomIndexer *core.ChainIndexer) { 58 odr.chtIndexer = chtIndexer 59 odr.bloomTrieIndexer = bloomTrieIndexer 60 odr.bloomIndexer = bloomIndexer 61 } 62 63 // ChtIndexer returns the CHT chain indexer 64 func (odr *LesOdr) ChtIndexer() *core.ChainIndexer { 65 return odr.chtIndexer 66 } 67 68 // BloomTrieIndexer returns the bloom trie chain indexer 69 func (odr *LesOdr) BloomTrieIndexer() *core.ChainIndexer { 70 return odr.bloomTrieIndexer 71 } 72 73 // BloomIndexer returns the bloombits chain indexer 74 func (odr *LesOdr) BloomIndexer() *core.ChainIndexer { 75 return odr.bloomIndexer 76 } 77 78 // IndexerConfig returns the indexer config. 79 func (odr *LesOdr) IndexerConfig() *light.IndexerConfig { 80 return odr.indexerConfig 81 } 82 83 const ( 84 MsgBlockBodies = iota 85 MsgCode 86 MsgReceipts 87 MsgProofsV2 88 MsgHelperTrieProofs 89 ) 90 91 // Msg encodes a LES message that delivers reply data for a request 92 type Msg struct { 93 MsgType int 94 ReqID uint64 95 Obj interface{} 96 } 97 98 // Retrieve tries to fetch an object from the LES network. 99 // If the network retrieval was successful, it stores the object in local db. 100 func (odr *LesOdr) Retrieve(ctx context.Context, req light.OdrRequest) (err error) { 101 lreq := LesRequest(req) 102 103 reqID := genReqID() 104 rq := &distReq{ 105 getCost: func(dp distPeer) uint64 { 106 return lreq.GetCost(dp.(*peer)) 107 }, 108 canSend: func(dp distPeer) bool { 109 p := dp.(*peer) 110 if !p.isOnlyAnnounce { 111 return lreq.CanSend(p) 112 } 113 return false 114 }, 115 request: func(dp distPeer) func() { 116 p := dp.(*peer) 117 cost := lreq.GetCost(p) 118 p.fcServer.QueuedRequest(reqID, cost) 119 return func() { lreq.Request(reqID, p) } 120 }, 121 } 122 123 if err = odr.retriever.retrieve(ctx, reqID, rq, func(p distPeer, msg *Msg) error { return lreq.Validate(odr.db, msg) }, odr.stop); err == nil { 124 // retrieved from network, store in db 125 req.StoreResult(odr.db) 126 } else { 127 log.Debug("Failed to retrieve data from network", "err", err) 128 } 129 return 130 }