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