github.com/c4dt/go-ethereum@v1.9.2/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 MsgTxStatus 90 ) 91 92 // Msg encodes a LES message that delivers reply data for a request 93 type Msg struct { 94 MsgType int 95 ReqID uint64 96 Obj interface{} 97 } 98 99 // Retrieve tries to fetch an object from the LES network. 100 // If the network retrieval was successful, it stores the object in local db. 101 func (odr *LesOdr) Retrieve(ctx context.Context, req light.OdrRequest) (err error) { 102 lreq := LesRequest(req) 103 104 reqID := genReqID() 105 rq := &distReq{ 106 getCost: func(dp distPeer) uint64 { 107 return lreq.GetCost(dp.(*peer)) 108 }, 109 canSend: func(dp distPeer) bool { 110 p := dp.(*peer) 111 if !p.onlyAnnounce { 112 return lreq.CanSend(p) 113 } 114 return false 115 }, 116 request: func(dp distPeer) func() { 117 p := dp.(*peer) 118 cost := lreq.GetCost(p) 119 p.fcServer.QueuedRequest(reqID, cost) 120 return func() { lreq.Request(reqID, p) } 121 }, 122 } 123 124 if err = odr.retriever.retrieve(ctx, reqID, rq, func(p distPeer, msg *Msg) error { return lreq.Validate(odr.db, msg) }, odr.stop); err == nil { 125 // retrieved from network, store in db 126 req.StoreResult(odr.db) 127 } else { 128 log.Debug("Failed to retrieve data from network", "err", err) 129 } 130 return 131 }